diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu')
6 files changed, 193 insertions, 197 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend index b8c52af4..37fb81ae 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend | |||
@@ -1,5 +1,4 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | 1 | package hu.bme.mit.inf.dslreasoner.viatra2logic |
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | 2 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic |
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | 3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | 4 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder |
@@ -17,27 +16,27 @@ import org.eclipse.emf.ecore.EStructuralFeature | |||
17 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey | 16 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey |
18 | import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey | 17 | import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey |
19 | import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey | 18 | import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey |
19 | import org.eclipse.viatra.query.runtime.matchers.aggregators.DoubleSumOperator | ||
20 | import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator | ||
21 | import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator.Extreme | ||
22 | import org.eclipse.viatra.query.runtime.matchers.aggregators.IntegerSumOperator | ||
23 | import org.eclipse.viatra.query.runtime.matchers.aggregators.LongSumOperator | ||
20 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | 24 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint |
21 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | 25 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable |
26 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.AggregatorConstraint | ||
22 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality | 27 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality |
23 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter | 28 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter |
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation | ||
24 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality | 30 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality |
25 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall | 31 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall |
32 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.PatternMatchCounter | ||
33 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint | ||
26 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure | 34 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure |
27 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.ConstantValue | 35 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.ConstantValue |
28 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall | 36 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall |
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint | 37 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint |
30 | 38 | ||
31 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | 39 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* |
32 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint | ||
33 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.AggregatorConstraint | ||
34 | import org.eclipse.viatra.query.runtime.matchers.aggregators.DoubleSumOperator | ||
35 | import org.eclipse.viatra.query.runtime.matchers.aggregators.IntegerSumOperator | ||
36 | import org.eclipse.viatra.query.runtime.matchers.aggregators.LongSumOperator | ||
37 | import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator | ||
38 | import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator.Extreme | ||
39 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.PatternMatchCounter | ||
40 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation | ||
41 | 40 | ||
42 | class Constraint2Logic { | 41 | class Constraint2Logic { |
43 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | 42 | val extension LogicProblemBuilder builder = new LogicProblemBuilder |
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend index b4303739..6393121b 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend | |||
@@ -12,11 +12,15 @@ import org.eclipse.xtext.xbase.XMemberFeatureCall | |||
12 | import org.eclipse.xtext.xbase.XNumberLiteral | 12 | import org.eclipse.xtext.xbase.XNumberLiteral |
13 | import org.eclipse.xtext.xbase.XUnaryOperation | 13 | import org.eclipse.xtext.xbase.XUnaryOperation |
14 | 14 | ||
15 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
16 | |||
17 | class ExpressionEvaluation2Logic { | 15 | class ExpressionEvaluation2Logic { |
18 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | 16 | val extension LogicProblemBuilder builder = new LogicProblemBuilder |
19 | val NumericProblemSolver numericSolver = new NumericProblemSolver | 17 | var NumericProblemSolver _numericSolver = null //new NumericProblemSolver |
18 | def getNumericSolver() { | ||
19 | if(_numericSolver === null) { | ||
20 | _numericSolver = new NumericProblemSolver | ||
21 | } | ||
22 | return _numericSolver | ||
23 | } | ||
20 | 24 | ||
21 | def Term transformCheck(XExpression expression, Map<PVariable, Variable> variable2Variable) { | 25 | def Term transformCheck(XExpression expression, Map<PVariable, Variable> variable2Variable) { |
22 | return expression.transform(variable2Variable) | 26 | return expression.transform(variable2Variable) |
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericProblemSolver.java b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericProblemSolver.java index 0b249962..f1314925 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericProblemSolver.java +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericProblemSolver.java | |||
@@ -1,17 +1,10 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic; | 1 | package hu.bme.mit.inf.dslreasoner.viatra2logic; |
2 | 2 | ||
3 | import java.math.BigDecimal; | ||
4 | import java.util.ArrayList; | 3 | import java.util.ArrayList; |
5 | import java.util.HashMap; | 4 | import java.util.HashMap; |
6 | import java.util.HashSet; | ||
7 | import java.util.List; | 5 | import java.util.List; |
8 | import java.util.Map; | 6 | import java.util.Map; |
9 | import java.util.Map.Entry; | ||
10 | import java.util.Random; | ||
11 | import java.util.Set; | ||
12 | 7 | ||
13 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint; | ||
14 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation; | ||
15 | import org.eclipse.xtext.common.types.JvmIdentifiableElement; | 8 | import org.eclipse.xtext.common.types.JvmIdentifiableElement; |
16 | import org.eclipse.xtext.xbase.XBinaryOperation; | 9 | import org.eclipse.xtext.xbase.XBinaryOperation; |
17 | import org.eclipse.xtext.xbase.XExpression; | 10 | import org.eclipse.xtext.xbase.XExpression; |
@@ -29,10 +22,9 @@ import com.microsoft.z3.Solver; | |||
29 | import com.microsoft.z3.Status; | 22 | import com.microsoft.z3.Status; |
30 | import com.microsoft.z3.enumerations.Z3_ast_print_mode; | 23 | import com.microsoft.z3.enumerations.Z3_ast_print_mode; |
31 | 24 | ||
32 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term; | ||
33 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement; | 25 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement; |
34 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement; | ||
35 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement; | 26 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement; |
27 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement; | ||
36 | 28 | ||
37 | 29 | ||
38 | public class NumericProblemSolver { | 30 | public class NumericProblemSolver { |
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericTranslator.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericTranslator.xtend index 81bc1796..b9eda7b3 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericTranslator.xtend +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericTranslator.xtend | |||
@@ -1,19 +1,17 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | 1 | package hu.bme.mit.inf.dslreasoner.viatra2logic |
2 | 2 | ||
3 | import org.eclipse.xtext.xbase.XExpression | ||
4 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation | ||
5 | import org.eclipse.xtext.common.types.JvmIdentifiableElement | ||
6 | import java.util.Set | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement | 3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement |
8 | import java.util.Map | ||
9 | import com.microsoft.z3.BoolExpr | ||
10 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | ||
11 | import java.util.Map.Entry | ||
12 | import org.eclipse.xtext.xbase.XFeatureCall | ||
13 | import java.util.Comparator | ||
14 | import java.util.ArrayList | 4 | import java.util.ArrayList |
5 | import java.util.Comparator | ||
15 | import java.util.HashMap | 6 | import java.util.HashMap |
16 | import java.util.List | 7 | import java.util.List |
8 | import java.util.Map | ||
9 | import java.util.Map.Entry | ||
10 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | ||
11 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation | ||
12 | import org.eclipse.xtext.common.types.JvmIdentifiableElement | ||
13 | import org.eclipse.xtext.xbase.XExpression | ||
14 | import org.eclipse.xtext.xbase.XFeatureCall | ||
17 | 15 | ||
18 | class NumericTranslator { | 16 | class NumericTranslator { |
19 | 17 | ||
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2Logic.xtend index 74e03f71..2f7febd0 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2Logic.xtend +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2Logic.xtend | |||
@@ -1,5 +1,6 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | 1 | package hu.bme.mit.inf.dslreasoner.viatra2logic |
2 | 2 | ||
3 | import com.google.inject.Injector | ||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | 4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic |
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | 5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder |
@@ -50,7 +51,7 @@ class Viatra2LogicConfiguration { | |||
50 | public var normalize = true | 51 | public var normalize = true |
51 | } | 52 | } |
52 | 53 | ||
53 | class Viatra2Logic { | 54 | class Viatra2Logic { |
54 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | 55 | val extension LogicProblemBuilder builder = new LogicProblemBuilder |
55 | val extension Viatra2LogicAnnotationsFactory factory = Viatra2LogicAnnotationsFactory.eINSTANCE | 56 | val extension Viatra2LogicAnnotationsFactory factory = Viatra2LogicAnnotationsFactory.eINSTANCE |
56 | val normalizer = new PBodyNormalizer(EMFQueryMetaContext.DEFAULT) | 57 | val normalizer = new PBodyNormalizer(EMFQueryMetaContext.DEFAULT) |
@@ -59,12 +60,16 @@ class Viatra2Logic { | |||
59 | val Ecore2Logic ecore2Logic | 60 | val Ecore2Logic ecore2Logic |
60 | Constraint2Logic constraint2Logic | 61 | Constraint2Logic constraint2Logic |
61 | 62 | ||
62 | new(Ecore2Logic ecore2Logic) { | 63 | new(Ecore2Logic ecore2Logic, Injector injector) { |
63 | this.ecore2Logic = ecore2Logic | 64 | this.ecore2Logic = ecore2Logic |
64 | this.typeInferer = new Viatra2LogicTypeInferer(ecore2Logic) | 65 | this.typeInferer = new Viatra2LogicTypeInferer(ecore2Logic, injector) |
65 | this.constraint2Logic = new Constraint2Logic(ecore2Logic) | 66 | this.constraint2Logic = new Constraint2Logic(ecore2Logic) |
66 | } | 67 | } |
67 | 68 | ||
69 | new(Ecore2Logic ecore2Logic) { | ||
70 | this(ecore2Logic, null) | ||
71 | } | ||
72 | |||
68 | def TracedOutput<LogicProblem,Viatra2LogicTrace> transformQueries( | 73 | def TracedOutput<LogicProblem,Viatra2LogicTrace> transformQueries( |
69 | ViatraQuerySetDescriptor queries, | 74 | ViatraQuerySetDescriptor queries, |
70 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | 75 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, |
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2LogicTypeInferer.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2LogicTypeInferer.xtend index b8a6b9c1..1a96bf07 100644 --- a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2LogicTypeInferer.xtend +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Viatra2LogicTypeInferer.xtend | |||
@@ -1,5 +1,6 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | 1 | package hu.bme.mit.inf.dslreasoner.viatra2logic |
2 | 2 | ||
3 | import com.google.inject.Injector | ||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | 4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic |
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | 5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder |
@@ -18,7 +19,6 @@ import java.util.Set | |||
18 | import org.eclipse.emf.ecore.EEnum | 19 | import org.eclipse.emf.ecore.EEnum |
19 | import org.eclipse.emf.ecore.EcorePackage | 20 | import org.eclipse.emf.ecore.EcorePackage |
20 | import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup | 21 | import org.eclipse.viatra.query.patternlanguage.emf.EMFPatternLanguageStandaloneSetup |
21 | import org.eclipse.viatra.query.patternlanguage.emf.specification.XBaseEvaluator | ||
22 | import org.eclipse.viatra.query.runtime.emf.EMFQueryMetaContext | 22 | import org.eclipse.viatra.query.runtime.emf.EMFQueryMetaContext |
23 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey | 23 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey |
24 | import org.eclipse.viatra.query.runtime.emf.types.EClassUnscopedTransitiveInstancesKey | 24 | import org.eclipse.viatra.query.runtime.emf.types.EClassUnscopedTransitiveInstancesKey |
@@ -26,7 +26,6 @@ import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey | |||
26 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey | 26 | import org.eclipse.viatra.query.runtime.matchers.context.IInputKey |
27 | import org.eclipse.viatra.query.runtime.matchers.context.common.JavaTransitiveInstancesKey | 27 | import org.eclipse.viatra.query.runtime.matchers.context.common.JavaTransitiveInstancesKey |
28 | import org.eclipse.viatra.query.runtime.matchers.planning.helpers.TypeHelper | 28 | import org.eclipse.viatra.query.runtime.matchers.planning.helpers.TypeHelper |
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.IExpressionEvaluator | ||
30 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody | 29 | import org.eclipse.viatra.query.runtime.matchers.psystem.PBody |
31 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | 30 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable |
32 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.AggregatorConstraint | 31 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.AggregatorConstraint |
@@ -40,285 +39,275 @@ import org.eclipse.xtext.xbase.typesystem.IBatchTypeResolver | |||
40 | import org.eclipse.xtext.xbase.typesystem.references.UnknownTypeReference | 39 | import org.eclipse.xtext.xbase.typesystem.references.UnknownTypeReference |
41 | 40 | ||
42 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | 41 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* |
43 | import org.eclipse.xtext.xbase.typesystem.references.InnerTypeReference | ||
44 | 42 | ||
45 | class Viatra2LogicTypeInferer{ | 43 | class Viatra2LogicTypeInferer { |
46 | val Ecore2Logic ecore2Logic | 44 | val Ecore2Logic ecore2Logic |
47 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | 45 | val extension LogicProblemBuilder builder = new LogicProblemBuilder |
48 | /**Typeresolver uses the same resolver as EMFPatternLanguageStandaloneSetup.*/ | 46 | /**Typeresolver uses the same resolver as EMFPatternLanguageStandaloneSetup.*/ |
49 | val IBatchTypeResolver typeResolver = | 47 | val IBatchTypeResolver typeResolver |
50 | (new EMFPatternLanguageStandaloneSetup).createInjector.getInstance(IBatchTypeResolver) | ||
51 | val expressionExtractor = new XExpressionExtractor | 48 | val expressionExtractor = new XExpressionExtractor |
52 | 49 | ||
53 | new(Ecore2Logic ecore2Logic) { | 50 | new(Ecore2Logic ecore2Logic, Injector injector) { |
54 | this.ecore2Logic = ecore2Logic | 51 | this.ecore2Logic = ecore2Logic |
52 | val selectedInjector = injector ?: | ||
53 | new EMFPatternLanguageStandaloneSetup().createStandaloneInjectorWithSeparateModules | ||
54 | this.typeResolver = selectedInjector.getInstance(IBatchTypeResolver) | ||
55 | } | ||
56 | |||
57 | new(Ecore2Logic ecore2Logic) { | ||
58 | this(ecore2Logic, null) | ||
55 | } | 59 | } |
56 | 60 | ||
57 | def Viatra2LogicTypeResult inferTypes(List<PQuery> pQueries, TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, Viatra2LogicTrace viatra2LogicTrace) { | 61 | def Viatra2LogicTypeResult inferTypes(List<PQuery> pQueries, |
58 | val Viatra2LogicTypeResult result = new Viatra2LogicTypeResult(new HashMap,new HashMap); | 62 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, Viatra2LogicTrace viatra2LogicTrace) { |
59 | for(query : pQueries) { | 63 | val Viatra2LogicTypeResult result = new Viatra2LogicTypeResult(new HashMap, new HashMap); |
60 | for(body: query.lookup(viatra2LogicTrace.query2Disjunction).bodies) { | 64 | for (query : pQueries) { |
61 | for(variable : body.uniqueVariables) { | 65 | for (body : query.lookup(viatra2LogicTrace.query2Disjunction).bodies) { |
62 | getOrMakeTypeDecision(result,variable,body,ecore2LogicTrace,viatra2LogicTrace,emptySet) | 66 | for (variable : body.uniqueVariables) { |
67 | getOrMakeTypeDecision(result, variable, body, ecore2LogicTrace, viatra2LogicTrace, emptySet) | ||
63 | } | 68 | } |
64 | } | 69 | } |
65 | for(parameter: query.parameters) { | 70 | for (parameter : query.parameters) { |
66 | getOrMakeTypeDecision(result,query,parameter,ecore2LogicTrace,viatra2LogicTrace,emptySet) | 71 | getOrMakeTypeDecision(result, query, parameter, ecore2LogicTrace, viatra2LogicTrace, emptySet) |
67 | } | 72 | } |
68 | } | 73 | } |
69 | return result | 74 | return result |
70 | } | 75 | } |
71 | 76 | ||
72 | private def TypeReference getOrMakeTypeDecision( | 77 | private def TypeReference getOrMakeTypeDecision(Viatra2LogicTypeResult result, PVariable variable, PBody body, |
73 | Viatra2LogicTypeResult result, | 78 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, Viatra2LogicTrace viatra2LogicTrace, |
74 | PVariable variable, PBody body, | 79 | Set<? extends PParameter> checkedInDecisionMaking) { |
75 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | 80 | if (result.containsSolution(body, variable)) { |
76 | Viatra2LogicTrace viatra2LogicTrace, | 81 | return result.getType(body, variable) |
77 | Set<? extends PParameter> checkedInDecisionMaking) | ||
78 | { | ||
79 | if(result.containsSolution(body,variable)) { | ||
80 | return result.getType(body,variable) | ||
81 | } else { | 82 | } else { |
82 | val inferredTypesByViatra = TypeHelper::inferUnaryTypesFor(body.uniqueVariables, body.constraints, EMFQueryMetaContext.DEFAULT) | 83 | val inferredTypesByViatra = TypeHelper::inferUnaryTypesFor(body.uniqueVariables, body.constraints, |
84 | EMFQueryMetaContext.DEFAULT) | ||
83 | val constraintsForVariable = variable.lookup(inferredTypesByViatra) | 85 | val constraintsForVariable = variable.lookup(inferredTypesByViatra) |
84 | 86 | ||
85 | val typeConstraintsDerivedByTypeHelper = constraintsForVariable.map[transformTypeReference(ecore2LogicTrace)] | 87 | val typeConstraintsDerivedByTypeHelper = constraintsForVariable.map [ |
88 | transformTypeReference(ecore2LogicTrace) | ||
89 | ] | ||
86 | val typesFromEval = variable.getTypesFromEval(typeResolver) | 90 | val typesFromEval = variable.getTypesFromEval(typeResolver) |
87 | val typesFromAggregatorResult = variable.getTypeFromPassivePatternCallConstraintResult( | 91 | val typesFromAggregatorResult = variable. |
88 | result, | 92 | getTypeFromPassivePatternCallConstraintResult(result, ecore2LogicTrace, viatra2LogicTrace, |
89 | ecore2LogicTrace, | ||
90 | viatra2LogicTrace, | ||
91 | checkedInDecisionMaking) | ||
92 | |||
93 | val typesFromPositiveReasoning = (typeConstraintsDerivedByTypeHelper + typesFromEval + typesFromAggregatorResult).filterNull | ||
94 | |||
95 | val types = if(!typesFromPositiveReasoning.empty) { | ||
96 | typesFromPositiveReasoning | ||
97 | } else { | ||
98 | variable.getTypeFromPassivePatternCallConstraints( | ||
99 | result, | ||
100 | ecore2LogicTrace, | ||
101 | viatra2LogicTrace, | ||
102 | checkedInDecisionMaking) | 93 | checkedInDecisionMaking) |
103 | } | 94 | |
104 | 95 | val typesFromPositiveReasoning = (typeConstraintsDerivedByTypeHelper + typesFromEval + | |
96 | typesFromAggregatorResult).filterNull | ||
97 | |||
98 | val types = if (!typesFromPositiveReasoning.empty) { | ||
99 | typesFromPositiveReasoning | ||
100 | } else { | ||
101 | variable.getTypeFromPassivePatternCallConstraints(result, ecore2LogicTrace, viatra2LogicTrace, | ||
102 | checkedInDecisionMaking) | ||
103 | } | ||
104 | |||
105 | val commonSubtype = this.calculateCommonSubtype(types) | 105 | val commonSubtype = this.calculateCommonSubtype(types) |
106 | 106 | ||
107 | result.addType(body,variable,commonSubtype) | 107 | result.addType(body, variable, commonSubtype) |
108 | return commonSubtype | 108 | return commonSubtype |
109 | } | 109 | } |
110 | } | 110 | } |
111 | 111 | ||
112 | private def TypeReference getOrMakeTypeDecision( | 112 | private def TypeReference getOrMakeTypeDecision(Viatra2LogicTypeResult result, PQuery query, PParameter parameter, |
113 | Viatra2LogicTypeResult result, | 113 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, Viatra2LogicTrace viatra2LogicTrace, |
114 | PQuery query, | 114 | Set<? extends PParameter> checkedInDecisionMaking) { |
115 | PParameter parameter, | 115 | if (checkedInDecisionMaking.contains(parameter)) { |
116 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
117 | Viatra2LogicTrace viatra2LogicTrace, | ||
118 | Set<? extends PParameter> checkedInDecisionMaking) | ||
119 | { | ||
120 | if(checkedInDecisionMaking.contains(parameter)) { | ||
121 | return null | 116 | return null |
122 | } | 117 | } |
123 | if(result.containsSolution(parameter)) { | 118 | if (result.containsSolution(parameter)) { |
124 | return result.getType(query, parameter) | 119 | return result.getType(query, parameter) |
125 | } | 120 | } |
126 | 121 | ||
127 | var TypeReference typeReference; | 122 | var TypeReference typeReference; |
128 | 123 | ||
129 | if(parameter.declaredUnaryType !== null) { | 124 | if (parameter.declaredUnaryType !== null) { |
130 | val key = parameter.declaredUnaryType | 125 | val key = parameter.declaredUnaryType |
131 | typeReference = key.transformTypeReference(ecore2LogicTrace) | 126 | typeReference = key.transformTypeReference(ecore2LogicTrace) |
132 | } else { | 127 | } else { |
133 | val bodies = query.lookup(viatra2LogicTrace.query2Disjunction).bodies | 128 | val bodies = query.lookup(viatra2LogicTrace.query2Disjunction).bodies |
134 | val newChecked = new HashSet(checkedInDecisionMaking) => [add(parameter)] | 129 | val newChecked = new HashSet(checkedInDecisionMaking) => [add(parameter)] |
135 | val Iterable<TypeReference> variableTypes = bodies.map[body| | 130 | val Iterable<TypeReference> variableTypes = bodies.map [ body | |
136 | val symbolicParameter = body.symbolicParameters.filter[patternParameter === parameter].head | 131 | val symbolicParameter = body.symbolicParameters.filter[patternParameter === parameter].head |
137 | val variable = symbolicParameter.parameterVariable | 132 | val variable = symbolicParameter.parameterVariable |
138 | getOrMakeTypeDecision(result,variable,body,ecore2LogicTrace,viatra2LogicTrace,newChecked) | 133 | getOrMakeTypeDecision(result, variable, body, ecore2LogicTrace, viatra2LogicTrace, newChecked) |
139 | ] | 134 | ] |
140 | typeReference = calculateCommonSupertype(variableTypes) | 135 | typeReference = calculateCommonSupertype(variableTypes) |
141 | } | 136 | } |
142 | result.addType(query,parameter,typeReference) | 137 | result.addType(query, parameter, typeReference) |
143 | return typeReference | 138 | return typeReference |
144 | } | 139 | } |
145 | 140 | ||
146 | private def Iterable<? extends TypeReference> getTypesFromEval(PVariable v, IBatchTypeResolver typeResolver) { | 141 | private def Iterable<? extends TypeReference> getTypesFromEval(PVariable v, IBatchTypeResolver typeResolver) { |
147 | val constraints = v.getReferringConstraintsOfType( | 142 | val constraints = v.getReferringConstraintsOfType( |
148 | typeof(ExpressionEvaluation) | 143 | typeof(ExpressionEvaluation) |
149 | ).filter[ | 144 | ).filter [ |
150 | it.outputVariable === v | 145 | it.outputVariable === v |
151 | ] | 146 | ] |
152 | val res = constraints.map[getTypeFromEval] | 147 | val res = constraints.map[getTypeFromEval] |
153 | return res | 148 | return res |
154 | } | 149 | } |
155 | 150 | ||
156 | def TypeReference getTypeFromEval(ExpressionEvaluation evaluation) { | 151 | def TypeReference getTypeFromEval(ExpressionEvaluation evaluation) { |
157 | val XExpression expression = expressionExtractor.extractExpression(evaluation.evaluator) | 152 | val XExpression expression = expressionExtractor.extractExpression(evaluation.evaluator) |
158 | val returnType = typeResolver.resolveTypes(expression).getReturnType(expression); | 153 | val returnType = typeResolver.resolveTypes(expression).getReturnType(expression); |
159 | if(returnType === null || returnType instanceof UnknownTypeReference) { | 154 | if (returnType === null || returnType instanceof UnknownTypeReference) { |
160 | return null | 155 | return null |
161 | } else { | 156 | } else { |
162 | val javaIdentifier = returnType.wrapperTypeIfPrimitive.javaIdentifier | 157 | val javaIdentifier = returnType.wrapperTypeIfPrimitive.javaIdentifier |
163 | if(javaIdentifier == Boolean.name) { | 158 | if (javaIdentifier == Boolean.name) { |
164 | return LogicBool | 159 | return LogicBool |
165 | } else if(javaIdentifier == Integer.name || javaIdentifier == Short.name) { | 160 | } else if (javaIdentifier == Integer.name || javaIdentifier == Short.name) { |
166 | return LogicInt | 161 | return LogicInt |
167 | } else if(javaIdentifier == Double.name || javaIdentifier == Float.name){ | 162 | } else if (javaIdentifier == Double.name || javaIdentifier == Float.name) { |
168 | return LogicReal | 163 | return LogicReal |
169 | } else if(javaIdentifier == String.name) { | 164 | } else if (javaIdentifier == String.name) { |
170 | return LogicString | 165 | return LogicString |
171 | } else { | 166 | } else { |
172 | throw new UnsupportedOperationException('''Unsupported eval type: "«javaIdentifier»"!''') | 167 | throw new UnsupportedOperationException('''Unsupported eval type: "«javaIdentifier»"!''') |
173 | } | 168 | } |
174 | } | 169 | } |
175 | } | 170 | } |
176 | 171 | ||
177 | private def getTypeFromPassivePatternCallConstraintResult( | 172 | private def getTypeFromPassivePatternCallConstraintResult( |
178 | PVariable v, | 173 | PVariable v, |
179 | Viatra2LogicTypeResult result, | 174 | Viatra2LogicTypeResult result, |
180 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | 175 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, |
181 | Viatra2LogicTrace viatra2LogicTrace, | 176 | Viatra2LogicTrace viatra2LogicTrace, |
182 | Set<? extends PParameter> checkedInDecisionMaking | 177 | Set<? extends PParameter> checkedInDecisionMaking |
183 | ) { | 178 | ) { |
184 | val referringConstraints = v.referringConstraints | 179 | val referringConstraints = v.referringConstraints |
185 | 180 | ||
186 | val referringCountMatcherTargeting = referringConstraints | 181 | val referringCountMatcherTargeting = referringConstraints.filter(PatternMatchCounter).filter [ |
187 | .filter(PatternMatchCounter) | 182 | it.resultVariable === v |
188 | .filter[it.resultVariable === v] | 183 | ].map[builder.LogicInt] |
189 | .map[builder.LogicInt] | 184 | val referringAggregatorConstraintsTargeting = referringConstraints.filter(AggregatorConstraint).filter [ |
190 | val referringAggregatorConstraintsTargeting = referringConstraints | 185 | it.resultVariable === v |
191 | .filter(AggregatorConstraint) | 186 | ].map [ // get the type of the referred column |
192 | .filter[it.resultVariable === v] | 187 | getOrMakeTypeDecision(result, it.referredQuery, it.referredQuery.parameters.get(aggregatedColumn), |
193 | .map[ // get the type of the referred column | 188 | ecore2LogicTrace, viatra2LogicTrace, checkedInDecisionMaking) |
194 | getOrMakeTypeDecision( | 189 | ] |
195 | result, | 190 | |
196 | it.referredQuery, | ||
197 | it.referredQuery.parameters.get(aggregatedColumn), | ||
198 | ecore2LogicTrace, | ||
199 | viatra2LogicTrace, | ||
200 | checkedInDecisionMaking)] | ||
201 | |||
202 | return referringCountMatcherTargeting + referringAggregatorConstraintsTargeting | 191 | return referringCountMatcherTargeting + referringAggregatorConstraintsTargeting |
203 | } | 192 | } |
204 | 193 | ||
205 | private def getTypeFromPassivePatternCallConstraints( | 194 | private def getTypeFromPassivePatternCallConstraints( |
206 | PVariable v, | 195 | PVariable v, |
207 | Viatra2LogicTypeResult result, | 196 | Viatra2LogicTypeResult result, |
208 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | 197 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, |
209 | Viatra2LogicTrace viatra2LogicTrace, | 198 | Viatra2LogicTrace viatra2LogicTrace, |
210 | Set<? extends PParameter> checkedInDecisionMaking | 199 | Set<? extends PParameter> checkedInDecisionMaking |
211 | ) { | 200 | ) { |
212 | val referringConstraints = v.referringConstraints | 201 | val referringConstraints = v.referringConstraints |
213 | if(referringConstraints.size === 1) { | 202 | if (referringConstraints.size === 1) { |
214 | val onlyConstraint = referringConstraints.head | 203 | val onlyConstraint = referringConstraints.head |
215 | 204 | ||
216 | if(onlyConstraint instanceof PatternCallBasedDeferred) { | 205 | if (onlyConstraint instanceof PatternCallBasedDeferred) { |
217 | val indexOfVariable = v.lookup(onlyConstraint.actualParametersTuple.invertIndex) | 206 | val indexOfVariable = v.lookup(onlyConstraint.actualParametersTuple.invertIndex) |
218 | val parameter = onlyConstraint.referredQuery.parameters.get(indexOfVariable) | 207 | val parameter = onlyConstraint.referredQuery.parameters.get(indexOfVariable) |
219 | val res = getOrMakeTypeDecision(result, onlyConstraint.referredQuery, parameter, ecore2LogicTrace,viatra2LogicTrace,checkedInDecisionMaking) | 208 | val res = getOrMakeTypeDecision(result, onlyConstraint.referredQuery, parameter, ecore2LogicTrace, |
209 | viatra2LogicTrace, checkedInDecisionMaking) | ||
220 | return #[res] | 210 | return #[res] |
221 | } else { | 211 | } else { |
222 | throw new IllegalArgumentException('''A non-PatternCallBasedDeferred type constraint is referring to the variable "«v.name»"!''') | 212 | throw new IllegalArgumentException('''A non-PatternCallBasedDeferred type constraint is referring to the variable "«v.name»"!''') |
223 | } | 213 | } |
224 | } else { | 214 | } else { |
225 | throw new IllegalArgumentException('''Multiple («referringConstraints.size», «FOR c:referringConstraints SEPARATOR ", "»«c»«ENDFOR») constraints are referring to variable "«v.name»", but no type is inferred!''') | 215 | throw new IllegalArgumentException('''Multiple («referringConstraints.size», «FOR c : referringConstraints SEPARATOR ", "»«c»«ENDFOR») constraints are referring to variable "«v.name»", but no type is inferred!''') |
226 | } | 216 | } |
227 | } | 217 | } |
228 | 218 | ||
229 | def TypeReference calculateCommonSubtype(Iterable<TypeReference> types) { | 219 | def TypeReference calculateCommonSubtype(Iterable<TypeReference> types) { |
230 | val primitiveTypeReferences = types.filter(PrimitiveTypeReference) | 220 | val primitiveTypeReferences = types.filter(PrimitiveTypeReference) |
231 | val complexTypeReferences = types.filter(ComplexTypeReference) | 221 | val complexTypeReferences = types.filter(ComplexTypeReference) |
232 | if(complexTypeReferences.isEmpty) { | 222 | if (complexTypeReferences.isEmpty) { |
233 | // If there is an int type, ... | 223 | // If there is an int type, ... |
234 | if(primitiveTypeReferences.exists[it instanceof IntTypeReference]) { | 224 | if (primitiveTypeReferences.exists[it instanceof IntTypeReference]) { |
235 | // ... and all types are either real or int, then return int! | 225 | // ... and all types are either real or int, then return int! |
236 | if(primitiveTypeReferences.forall[it instanceof RealTypeReference || it instanceof IntTypeReference]) { | 226 | if (primitiveTypeReferences.forall[it instanceof RealTypeReference || it instanceof IntTypeReference]) { |
237 | return primitiveTypeReferences.filter(IntTypeReference).head | 227 | return primitiveTypeReferences.filter(IntTypeReference).head |
238 | } | 228 | } // Otherwise, the types are inconsistent, because they mixing numeric and non-numeric types. |
239 | // Otherwise, the types are inconsistent, because they mixing numeric and non-numeric types. | 229 | else |
240 | else throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') | 230 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') |
241 | } | 231 | } |
242 | // If there is no Real, then the types should be homogenious | 232 | // If there is no Real, then the types should be homogenious |
243 | val head = primitiveTypeReferences.head | 233 | val head = primitiveTypeReferences.head |
244 | if(primitiveTypeReferences.exists[it.eClass !== head.eClass]) { | 234 | if (primitiveTypeReferences.exists[it.eClass !== head.eClass]) { |
245 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') | 235 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') |
246 | } | 236 | } |
247 | return head | 237 | return head |
248 | } else if(primitiveTypeReferences.isEmpty) { | 238 | } else if (primitiveTypeReferences.isEmpty) { |
249 | val complexTypes = complexTypeReferences.map[it.referred].toSet | 239 | val complexTypes = complexTypeReferences.map[it.referred].toSet |
250 | if(complexTypes.size === 1) { | 240 | if (complexTypes.size === 1) { |
251 | return builder.toTypeReference(complexTypes.head) | 241 | return builder.toTypeReference(complexTypes.head) |
252 | } | 242 | } |
253 | // Collect possible subtypes | 243 | // Collect possible subtypes |
254 | val subtypeSets = complexTypes.map[it.transitiveClosureStar[it.subtypes].toSet] | 244 | val subtypeSets = complexTypes.map[it.transitiveClosureStar[it.subtypes].toSet] |
255 | val commonTypeSet = new HashSet(subtypeSets.head) | 245 | val commonTypeSet = new HashSet(subtypeSets.head) |
256 | val otherSets = subtypeSets.tail | 246 | val otherSets = subtypeSets.tail |
257 | for(otherSet : otherSets) { | 247 | for (otherSet : otherSets) { |
258 | commonTypeSet.retainAll(otherSet) | 248 | commonTypeSet.retainAll(otherSet) |
259 | } | 249 | } |
260 | if(commonTypeSet.empty) { | 250 | if (commonTypeSet.empty) { |
261 | throw new IllegalArgumentException('''Inconsistent types: «complexTypes.map[name].toList»''') | 251 | throw new IllegalArgumentException('''Inconsistent types: «complexTypes.map[name].toList»''') |
262 | } | 252 | } |
263 | 253 | ||
264 | |||
265 | return calculateCommonComplexSupertype(commonTypeSet) | 254 | return calculateCommonComplexSupertype(commonTypeSet) |
266 | 255 | ||
267 | } else { | 256 | } else { |
268 | throw new IllegalArgumentException(''' | 257 | throw new IllegalArgumentException(''' |
269 | Inconsistent types, mixing primitive and complex types: | 258 | Inconsistent types, mixing primitive and complex types: |
270 | «primitiveTypeReferences.map[eClass.name].toSet.toList» | 259 | «primitiveTypeReferences.map[eClass.name].toSet.toList» |
271 | and | 260 | and |
272 | «complexTypeReferences.map[it.referred].toSet.map[name].toList»''') | 261 | «complexTypeReferences.map[it.referred].toSet.map[name].toList»''') |
273 | 262 | ||
274 | } | 263 | } |
275 | } | 264 | } |
276 | 265 | ||
277 | |||
278 | def TypeReference calculateCommonSupertype(Iterable<TypeReference> types) { | 266 | def TypeReference calculateCommonSupertype(Iterable<TypeReference> types) { |
279 | val primitiveTypeReferences = types.filter(PrimitiveTypeReference) | 267 | val primitiveTypeReferences = types.filter(PrimitiveTypeReference) |
280 | val complexTypeReferences = types.filter(ComplexTypeReference) | 268 | val complexTypeReferences = types.filter(ComplexTypeReference) |
281 | if(complexTypeReferences.isEmpty) { | 269 | if (complexTypeReferences.isEmpty) { |
282 | // If there is a real type, ... | 270 | // If there is a real type, ... |
283 | if(primitiveTypeReferences.exists[it instanceof RealTypeReference]) { | 271 | if (primitiveTypeReferences.exists[it instanceof RealTypeReference]) { |
284 | // ... and all types are either real or int, then return real! | 272 | // ... and all types are either real or int, then return real! |
285 | if(primitiveTypeReferences.forall[it instanceof RealTypeReference || it instanceof IntTypeReference]) { | 273 | if (primitiveTypeReferences.forall[it instanceof RealTypeReference || it instanceof IntTypeReference]) { |
286 | return primitiveTypeReferences.filter(RealTypeReference).head | 274 | return primitiveTypeReferences.filter(RealTypeReference).head |
287 | } | 275 | } // Otherwise, the types are inconsistent, because they mixing numeric and non-numeric types. |
288 | // Otherwise, the types are inconsistent, because they mixing numeric and non-numeric types. | 276 | else |
289 | else throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') | 277 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') |
290 | } | 278 | } |
291 | // If there is no Real, then the types should be homogenious | 279 | // If there is no Real, then the types should be homogenious |
292 | val head = primitiveTypeReferences.head | 280 | val head = primitiveTypeReferences.head |
293 | if(primitiveTypeReferences.exists[it.eClass !== head.eClass]) { | 281 | if (primitiveTypeReferences.exists[it.eClass !== head.eClass]) { |
294 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') | 282 | throw new IllegalArgumentException('''Inconsistent types: «primitiveTypeReferences.map[eClass.name].toSet.toList»''') |
295 | } | 283 | } |
296 | return head | 284 | return head |
297 | } else if(primitiveTypeReferences.isEmpty) { | 285 | } else if (primitiveTypeReferences.isEmpty) { |
298 | val complexTypes = complexTypeReferences.map[it.referred].toSet | 286 | val complexTypes = complexTypeReferences.map[it.referred].toSet |
299 | return calculateCommonComplexSupertype(complexTypes) | 287 | return calculateCommonComplexSupertype(complexTypes) |
300 | 288 | ||
301 | } else { | 289 | } else { |
302 | throw new IllegalArgumentException(''' | 290 | throw new IllegalArgumentException(''' |
303 | Inconsistent types, mixing primitive and complex types: | 291 | Inconsistent types, mixing primitive and complex types: |
304 | «primitiveTypeReferences.map[eClass.name].toSet.toList» | 292 | «primitiveTypeReferences.map[eClass.name].toSet.toList» |
305 | and | 293 | and |
306 | «complexTypeReferences.map[it.referred].toSet.map[name].toList»''') | 294 | «complexTypeReferences.map[it.referred].toSet.map[name].toList»''') |
307 | 295 | ||
308 | } | 296 | } |
309 | } | 297 | } |
298 | |||
310 | def TypeReference calculateCommonComplexSupertype(Set<Type> complexTypes) { | 299 | def TypeReference calculateCommonComplexSupertype(Set<Type> complexTypes) { |
311 | if(complexTypes.size === 1) { | 300 | if (complexTypes.size === 1) { |
312 | return builder.toTypeReference(complexTypes.head) | 301 | return builder.toTypeReference(complexTypes.head) |
313 | } | 302 | } |
314 | // Collect possible supertypes | 303 | // Collect possible supertypes |
315 | val supertypeSets = complexTypes.map[it.transitiveClosureStar[it.supertypes].toSet] | 304 | val supertypeSets = complexTypes.map[it.transitiveClosureStar[it.supertypes].toSet] |
316 | val commonTypeSet = new HashSet(supertypeSets.head) | 305 | val commonTypeSet = new HashSet(supertypeSets.head) |
317 | val otherSets = supertypeSets.tail | 306 | val otherSets = supertypeSets.tail |
318 | for(otherSet : otherSets) { | 307 | for (otherSet : otherSets) { |
319 | commonTypeSet.retainAll(otherSet) | 308 | commonTypeSet.retainAll(otherSet) |
320 | } | 309 | } |
321 | if(commonTypeSet.empty) { | 310 | if (commonTypeSet.empty) { |
322 | throw new IllegalArgumentException('''Inconsistent types: «complexTypes.map[name].toList»''') | 311 | throw new IllegalArgumentException('''Inconsistent types: «complexTypes.map[name].toList»''') |
323 | } | 312 | } |
324 | // Remove type that already have covered | 313 | // Remove type that already have covered |
@@ -326,40 +315,44 @@ class Viatra2LogicTypeInferer{ | |||
326 | commonTypeSet.removeAll(coveredTypes) | 315 | commonTypeSet.removeAll(coveredTypes) |
327 | return builder.toTypeReference(commonTypeSet.head) | 316 | return builder.toTypeReference(commonTypeSet.head) |
328 | } | 317 | } |
329 | 318 | ||
330 | /** | 319 | /** |
331 | * Transforms a Viatra type reference to a logic type. | 320 | * Transforms a Viatra type reference to a logic type. |
332 | */ | 321 | */ |
333 | def dispatch TypeReference transformTypeReference(EDataTypeInSlotsKey k,TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | 322 | def dispatch TypeReference transformTypeReference(EDataTypeInSlotsKey k, |
334 | val w = k.wrappedKey | 323 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { |
335 | if(w == EcorePackage.Literals.EINT || w == EcorePackage.Literals.ESHORT || w == EcorePackage.Literals.ELONG) { | 324 | val w = k.wrappedKey |
325 | if (w == EcorePackage.Literals.EINT || w == EcorePackage.Literals.ESHORT || w == EcorePackage.Literals.ELONG) { | ||
336 | return builder.LogicInt | 326 | return builder.LogicInt |
337 | } else if(w == EcorePackage.Literals.EDOUBLE || w == EcorePackage.Literals.EFLOAT) { | 327 | } else if (w == EcorePackage.Literals.EDOUBLE || w == EcorePackage.Literals.EFLOAT) { |
338 | return builder.LogicReal | 328 | return builder.LogicReal |
339 | } else if(w == EcorePackage.Literals.EBOOLEAN) { | 329 | } else if (w == EcorePackage.Literals.EBOOLEAN) { |
340 | return builder.LogicBool | 330 | return builder.LogicBool |
341 | } else if(w == EcorePackage.Literals.ESTRING) { | 331 | } else if (w == EcorePackage.Literals.ESTRING) { |
342 | return builder.LogicString | 332 | return builder.LogicString |
343 | } else if(w instanceof EEnum) { | 333 | } else if (w instanceof EEnum) { |
344 | val c = this.ecore2Logic.TypeofEEnum(ecore2LogicTrace.trace,w) | 334 | val c = this.ecore2Logic.TypeofEEnum(ecore2LogicTrace.trace, w) |
345 | return builder.toTypeReference(c); | 335 | return builder.toTypeReference(c); |
346 | } else throw new UnsupportedOperationException('''Unknown reference type «w.class.name»''') | 336 | } else |
337 | throw new UnsupportedOperationException('''Unknown reference type «w.class.name»''') | ||
347 | } | 338 | } |
348 | def dispatch TypeReference transformTypeReference(JavaTransitiveInstancesKey k,TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | 339 | |
340 | def dispatch TypeReference transformTypeReference(JavaTransitiveInstancesKey k, | ||
341 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | ||
349 | val c = k.wrapperInstanceClass | 342 | val c = k.wrapperInstanceClass |
350 | if(c == Integer || c == Long || c == Short) { | 343 | if (c == Integer || c == Long || c == Short) { |
351 | return LogicInt | 344 | return LogicInt |
352 | } else if(c == Float || c == Double) { | 345 | } else if (c == Float || c == Double) { |
353 | return LogicReal | 346 | return LogicReal |
354 | } else if(c == Boolean) { | 347 | } else if (c == Boolean) { |
355 | return LogicBool | 348 | return LogicBool |
356 | } else if(c == String) { | 349 | } else if (c == String) { |
357 | return LogicString | 350 | return LogicString |
358 | } else if(c.superclass == Enum){ | 351 | } else if (c.superclass == Enum) { |
359 | val enums = ecore2Logic.allEnumsInScope(ecore2LogicTrace.trace) | 352 | val enums = ecore2Logic.allEnumsInScope(ecore2LogicTrace.trace) |
360 | for(enum : enums) { | 353 | for (enum : enums) { |
361 | if(c == enum.instanceClass) { | 354 | if (c == enum.instanceClass) { |
362 | return builder.toTypeReference(ecore2Logic.TypeofEEnum(ecore2LogicTrace.trace,enum)) | 355 | return builder.toTypeReference(ecore2Logic.TypeofEEnum(ecore2LogicTrace.trace, enum)) |
363 | } | 356 | } |
364 | } | 357 | } |
365 | throw new IllegalArgumentException('''Enum type «c.simpleName» is not mapped to logic!''') | 358 | throw new IllegalArgumentException('''Enum type «c.simpleName» is not mapped to logic!''') |
@@ -367,26 +360,31 @@ class Viatra2LogicTypeInferer{ | |||
367 | return null | 360 | return null |
368 | } | 361 | } |
369 | } | 362 | } |
370 | def dispatch TypeReference transformTypeReference(EClassTransitiveInstancesKey k,TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | 363 | |
364 | def dispatch TypeReference transformTypeReference(EClassTransitiveInstancesKey k, | ||
365 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | ||
371 | val c = k.wrappedKey | 366 | val c = k.wrappedKey |
372 | 367 | ||
373 | if(this.ecore2Logic.allClassesInScope(ecore2LogicTrace.trace).toList.contains(c)) { | 368 | if (this.ecore2Logic.allClassesInScope(ecore2LogicTrace.trace).toList.contains(c)) { |
374 | return builder.toTypeReference(this.ecore2Logic.TypeofEClass(ecore2LogicTrace.trace,k.wrappedKey)) | 369 | return builder.toTypeReference(this.ecore2Logic.TypeofEClass(ecore2LogicTrace.trace, k.wrappedKey)) |
375 | } else { | 370 | } else { |
376 | return null | 371 | return null |
377 | } | 372 | } |
378 | } | 373 | } |
379 | def dispatch TypeReference transformTypeReference(EClassUnscopedTransitiveInstancesKey k, TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | 374 | |
375 | def dispatch TypeReference transformTypeReference(EClassUnscopedTransitiveInstancesKey k, | ||
376 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | ||
380 | val c = k.wrappedKey | 377 | val c = k.wrappedKey |
381 | 378 | ||
382 | if(this.ecore2Logic.allClassesInScope(ecore2LogicTrace.trace).toList.contains(c)) { | 379 | if (this.ecore2Logic.allClassesInScope(ecore2LogicTrace.trace).toList.contains(c)) { |
383 | return builder.toTypeReference(this.ecore2Logic.TypeofEClass(ecore2LogicTrace.trace,k.wrappedKey)) | 380 | return builder.toTypeReference(this.ecore2Logic.TypeofEClass(ecore2LogicTrace.trace, k.wrappedKey)) |
384 | } else { | 381 | } else { |
385 | return null | 382 | return null |
386 | } | 383 | } |
387 | } | 384 | } |
388 | 385 | ||
389 | def dispatch TypeReference transformTypeReference(IInputKey k,TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | 386 | def dispatch TypeReference transformTypeReference(IInputKey k, |
387 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace) { | ||
390 | throw new IllegalArgumentException('''Unsupported type: «k.class.simpleName»''') | 388 | throw new IllegalArgumentException('''Unsupported type: «k.class.simpleName»''') |
391 | } | 389 | } |
392 | } \ No newline at end of file | 390 | } |