aboutsummaryrefslogtreecommitdiffstats
path: root/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguage.xtext
blob: 5abeb891e7e96069262d73e88ab0c901fde43038 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
grammar org.eclipse.viatra.solver.language.SolverLanguage with org.eclipse.xtext.common.Terminals
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate solverLanguage "http://www.eclipse.org/viatra/solver/language/SolverLanguage"

Problem:
	(statements+=Statement)*;

Statement: 
	(
		AssertionOrDefinition | PredicateDefinition | UnnamedErrorPrediateDefinition | DefaultDefinition | ExternPredicateDefinition |
		MetricDefinition | ExternMetricDefinition | ClassDefinition | ScopeDefinition | ObjectiveDefinition
	) DOT;

AssertionOrDefinition returns Statement:
	Expression (
		{Assertion.body=current} (":" range=Expression)? |
		{PredicateDefinition.head=current} ":-" body=Expression |
		{MetricDefinition.head=current} "=" body=Expression
	);

PredicateDefinition:
	(functional?="functional" error?="error"? | error?="error" functional?="functional"?) head=Call ":-" body=Expression;

UnnamedErrorPrediateDefinition:
	"error" argumentList=ArgumentList ":-" body=Expression;

DefaultDefinition:
	"default" head=Call ":" range=Expression;

ExternPredicateDefinition:
	"extern" head=Call ".";

enum MetricType:
	INT="int" | REAL="real";

MetricDefinition:
	type=MetricType head=Expression "=" body=Expression;

ExternMetricDefinition:
	"extern" type=MetricType head=Call;

Expression:
	IfElse | DisjunctiveExpression;

IfElse:
	"if" condition=Expression "then" then=Expression "else" else=Expression;

DisjunctiveExpression returns Expression:
	ConjunctiveExpression (
		{Disjunction.children+=current} (";" children+=ConjunctiveExpression)+ |
		{Case.condition=current} "->" body=ConjunctiveExpression {Switch.cases+=current} (";" cases+=Case)*
	)?;

Case:
	condition=ConjunctiveExpression "->" body=ConjunctiveExpression;

ConjunctiveExpression returns Expression:
	ComparisonExpression ({Conjunction.children+=current} ("," children+=ComparisonExpression)+)?;

enum BinaryOperator:
	EQ | NOT_EQ | LESS | LESS_EQ | GREATER | GREATER_EQ | IN | ADD | SUB | MUL | DIV | POW;

enum ComparisonOperator returns BinaryOperator:
	EQ="==" | NOT_EQ="!=" | LESS="<" | LESS_EQ="<=" | GREATER=">" | GREATER_EQ=">=" | IN="in";

ComparisonExpression returns Expression:
	AdditiveExpression ({Comparison.left=current} op=ComparisonOperator right=AdditiveExpression)?;

enum AdditiveBinaryOperator returns BinaryOperator:
	ADD="+" | SUB="-";

AdditiveExpression returns Expression:
	MultiplicativeExpression ({BinaryExpression.left=current} op=AdditiveBinaryOperator right=MultiplicativeExpression)*;

enum MultiplicativeBinaryOperator returns BinaryOperator:
	MUL="*" | DIV="/";

MultiplicativeExpression returns Expression:
	ExponentialExpression ({BinaryExpression.left=current} op=MultiplicativeBinaryOperator right=ExponentialExpression)*;

enum ExponentialOp returns BinaryOperator:
	POW="^";

ExponentialExpression returns Expression:
	UnaryExpression ({BinaryExpression.left=current} op=ExponentialOp right=ExponentialExpression)?;

enum UnaryOp:
	NEG="!" | PLUS="+" | MINUS="-" | MAY="may" | MUST="must" | CURRENT="current";

UnaryExpression returns Expression:
	AggregationExpression | {UnaryExpression} op=UnaryOp body=AggregationExpression;

AggregationExpression returns Expression:
	AtomicExpression | Count | Aggregation;

Count:
	"count" "{" body=Expression "}";

enum AggregationOp:
	ONLY="only" | SUM="sum" | PROD="prod" | AVG="avg" | MIN="min" | MAX="max";

Aggregation:
	op=AggregationOp "{" body=Expression "|" condition=Expression "}";

AtomicExpression returns Expression:
	Reference ({Call.functor=current} -> argumentList=ArgumentList)? | Interval | Literal | "(" Expression ")";

Call:
	functor=Reference (transitiveClosure?=STAR | reflexiveTransitiveClosure?=PLUS)? argumentList=ArgumentList;

ArgumentList:
	{ArgumentList} "(" (arguments+=Argument ("," arguments+=Argument)*)? ")";

Argument:
	ExpressionArgument | StarArgument | TypedArgument | TypedStarArgument;

ExpressionArgument:
	body=ComparisonExpression;

StarArgument:
	{StarArgument} "*";

TypedArgument:
	type=[NamedElement|QualifiedName] variable=[NamedElement|QualifiedName];

TypedStarArgument:
	type=[NamedElement|QualifiedName] "*";

Reference:
	referred=[NamedElement|QualifiedName];

Interval:
	"[" lowerBound=Expression ".." upperBound=Expression "]";

Literal:
	LogicLiteral | NumericLiteral | InfinityLiteral | EmptyIntervalLiteral | StringLiteral;

enum LogicValue:
	TRUE="true" | FALSE="false" | UNKNOWN="unknown" | ERROR="error";

LogicLiteral:
	value=LogicValue;

NumericLiteral:
	value=Real;

InfinityLiteral:
	{InfinityLiteral} "inf";

EmptyIntervalLiteral:
	{EmptyIntervalLiteral} "empty";

StringLiteral:
	value=STRING;

ClassDefinition returns Statement:
	abstract?="abstract"? "class" name=ID ("extends" superclasses+=[NamedElement|QualifiedName] ("," superclasses+=[NamedElement|QualifiedName])*)?
	"{" members+=MemberDefinition* "}";

MemberDefinition:
	containment?="contains"? type=[NamedElement|QualifiedName] multiplicity=Multiplicity? name=ID ("opposite" opposite=[NamedElement|QualifiedName])? ";"?;

Multiplicity:
	ManyMultiplicity | ExactMultiplicity | BoundedMultiplicity;

ManyMultiplicity:
	{ManyMultiplicity} "[" "]";

ExactMultiplicity:
	"[" multiplicity=UpperMultiplicty "]";

BoundedMultiplicity:
	"[" lowerBound=INT ".." upperBound=UpperMultiplicty "]";

ScopeDefinition:
	ExactScopeDefinition | BoundedScopeDefinition | LowerBoundedScopeDefinition;

ExactScopeDefinition:
	"scope" type=[NamedElement|QualifiedName] "==" exactScope=INT;

BoundedScopeDefinition:
	"scope" (
		(lowerBound=INT "<=")? type=[NamedElement|QualifiedName] "<=" upperBound=INT |
		upperBound=INT ">=" type=[NamedElement|QualifiedName] (">=" lowerBound=INT)?
	) ".";

LowerBoundedScopeDefinition:
	"scope" (
		lowerBound=INT "<=" type=[NamedElement|QualifiedName] |
		type=[NamedElement|QualifiedName] ">=" lowerBound=INT
	) ".";

enum ObjectiveKind:
	MINIMIZE="minimize" | MAXIMIZE="maximize";

ObjectiveDefinition:
	kind=ObjectiveKind objective=Expression;

UpperMultiplicty returns ecore::EInt:
	INT | "*";

Real returns ecore::EBigDecimal hidden():
	INT ("." INT)?;

QualifiedName hidden():
	ID ("." ID)* | QUOTED_ID;

@Override
terminal STRING returns ecore::EString:
	'"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"';

terminal QUOTED_ID returns ecore::EString:
	'\'' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'\'') )* '\'';

terminal PLUS:
	"synthetic::plus";

terminal STAR:
	"synthetic::star";

terminal DOT:
	"synthetic::dot";

NamedElement:
	name=QualifiedName;