aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src
diff options
context:
space:
mode:
authorLibravatar OszkarSemerath <oszka@152.66.252.189>2017-06-10 19:05:05 +0200
committerLibravatar OszkarSemerath <oszka@152.66.252.189>2017-06-10 19:05:05 +0200
commit60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch)
tree5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src
parentInitial commit, migrating from SVN (diff)
downloadVIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.gz
VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.zst
VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.zip
Migrating Additional projects
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src')
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend119
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend133
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend53
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend60
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend154
-rw-r--r--Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend98
6 files changed, 617 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend
new file mode 100644
index 00000000..ebf49196
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend
@@ -0,0 +1,119 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory
4import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion
6import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
7import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term
8import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription
9import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDescriptor
10import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
11import java.util.HashMap
12import java.util.Map
13import org.eclipse.emf.ecore.EAttribute
14import org.eclipse.emf.ecore.EEnum
15
16interface EAttributeMapper {
17 def void transformEAttributes(Ecore2Logic_Trace trace, LogicProblem logicProblem, Iterable<EAttribute> attributes)
18 def Term IsAttributeValue(Ecore2Logic_Trace trace, TermDescription object, TermDescription value, EAttribute attribute)
19 def RelationDeclaration relationOfAttribute(Ecore2Logic_Trace trace, EAttribute attribute)
20 def TypeDescriptor TypeOfRange(Ecore2Logic_Trace trace, EAttribute attribute)
21}
22
23class EAttributeMapper_RelationsOverTypes_Trace implements Trace<EAttributeMapper_RelationsOverTypes>{
24 public var Map<EAttribute, RelationDeclaration> indicators = new HashMap
25 public var Map<EAttribute, Assertion> typeCompliance = new HashMap
26 public var Map<EAttribute, Assertion> lowerMultiplicity = new HashMap
27 public var Map<EAttribute, Assertion> upperMultiplicity = new HashMap
28}
29
30class EAttributeMapper_RelationsOverTypes implements EAttributeMapper {
31 protected val extension LogicProblemBuilder builder = new LogicProblemBuilder
32 val extension Ecore2logicannotationsFactory builder2 = Ecore2logicannotationsFactory.eINSTANCE
33 protected val extension EClassMapper classMapper
34 protected val extension EEnumMapper enumMapper;
35
36 public new(EClassMapper classMapper, EEnumMapper enumMapper) {
37 this.enumMapper = enumMapper
38 this.classMapper = classMapper
39 }
40
41 public override transformEAttributes(Ecore2Logic_Trace trace, LogicProblem logicProblem, Iterable<EAttribute> attributes) {
42 val attributeMapperTrace = new EAttributeMapper_RelationsOverTypes_Trace
43 trace.attributeMapperTrace = attributeMapperTrace
44
45 for(attribute : attributes) {
46 val sourceType = trace.TypeofEClass(attribute.EContainingClass)
47 val rangeType = trace.TypeOfRange(attribute)
48
49 // relations
50 val indicator = '''inAttribute «attribute.name» «attribute.EContainingClass.name»'''.RelationDeclaration(sourceType,rangeType)
51
52 logicProblem.add(indicator)
53 attributeMapperTrace.indicators.put(attribute,indicator)
54
55 // lower multiplicity
56 var Assertion lowerMultiplicity = null
57 if(attribute.lowerBound != 0) {
58 lowerMultiplicity = '''lowerMultiplicity «attribute.name» «attribute.EContainingClass.name»'''.Assertion(
59 Forall[
60 val source = addVar('''src''', sourceType)
61 Exists[ context |
62 val targets = (1 .. attribute.lowerBound).map[context.addVar('''trg «it»''', rangeType)].toList
63 val attributeValue = And(targets.map[IsAttributeValue(trace, source, it, attribute)])
64 if(targets.size > 1) return Distinct(targets) && attributeValue
65 else return attributeValue
66 ]
67 ]
68 )
69 val annotation = createLowerMultiplicityAssertion => [
70 it.lower = attribute.lowerBound
71 it.relation = indicator
72 ]
73 logicProblem.add(lowerMultiplicity)
74 logicProblem.annotations += annotation
75 lowerMultiplicity.annotations +=annotation
76 }
77 attributeMapperTrace.lowerMultiplicity.put(attribute,lowerMultiplicity)
78
79 var Assertion upperMultiplicity = null
80 if(attribute.upperBound > 0) {
81 upperMultiplicity = '''upperMultiplicity «attribute.name» «attribute.EContainingClass.name»'''.Assertion(
82 Forall[ context |
83 val source = context.addVar('''src''', sourceType)
84 val targets = (1 .. attribute.upperBound+1).map[context.addVar('''trg «it»''', rangeType)].toList
85 And(targets.map[IsAttributeValue(trace, source, it, attribute)]) => ! Distinct(targets)
86 ])
87
88 val annotation = createUpperMultiplicityAssertion => [
89 it.upper = attribute.upperBound
90 it.relation = indicator
91 ]
92
93 logicProblem.add(upperMultiplicity)
94 logicProblem.annotations += annotation
95 upperMultiplicity.annotations += annotation
96 }
97 attributeMapperTrace.upperMultiplicity.put(attribute,upperMultiplicity)
98 }
99 }
100
101 def asTrace(Trace<? extends EAttributeMapper> trace) { return trace as EAttributeMapper_RelationsOverTypes_Trace}
102
103 override IsAttributeValue(Ecore2Logic_Trace trace, TermDescription object, TermDescription value, EAttribute attribute) {
104 trace.attributeMapperTrace.asTrace.indicators.get(attribute).call(object,value)
105 }
106
107 override TypeDescriptor TypeOfRange(Ecore2Logic_Trace trace, EAttribute attribute) {
108 if(attribute.EType instanceof EEnum) return enumMapper.TypeofEEnum(trace,attribute.EType as EEnum)
109 else if(attribute.EType.name.equals("EInt")) return LogicInt
110 else if(attribute.EType.name.equals("EBoolean")) return LogicBool
111 else if(attribute.EType.name.equals("EDouble") ||
112 attribute.EType.name.equals("EFloat")) return LogicReal
113 else throw new UnsupportedOperationException('''Unsupported attribute type: «attribute.EType.name»''')
114 }
115
116 override relationOfAttribute(Ecore2Logic_Trace trace, EAttribute attribute) {
117 trace.attributeMapperTrace.asTrace.indicators.get(attribute)
118 }
119} \ No newline at end of file
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend
new file mode 100644
index 00000000..492be59f
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/ContainmentMapper.xtend
@@ -0,0 +1,133 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Constant
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation
6import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
7import org.eclipse.emf.ecore.EReference
8import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion
9
10interface ContainmentMapper {
11 def void transformContainment(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references, boolean singleRoot);
12}
13
14class ContainmentMapper_ReferenceConjuction_Trace implements Trace<ContainmentMapper_ReferenceConjuction> {
15 public var Constant root;
16 public var Relation contains
17 public var Assertion containsDefinition;
18 public var Assertion rootIsNotContained;
19 public var Assertion everithingElseContained;
20 public var Assertion notContainedByMore;
21}
22
23class ContainmentMapper_ReferenceConjuction implements ContainmentMapper{
24 val extension LogicProblemBuilder builder = new LogicProblemBuilder
25 val extension EClassMapper classMapper;
26 val extension EReferenceMapper referenceMapper
27 val int containmentAcyclicityApproximationLevel;
28
29 public new(EClassMapper classMapper, EReferenceMapper referenceMapper, int containmentAcyclicityApproximationLevel) {
30 this.classMapper = classMapper
31 this.referenceMapper = referenceMapper
32 this.containmentAcyclicityApproximationLevel = containmentAcyclicityApproximationLevel
33 }
34
35 override transformContainment(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references, boolean singleRoot) {
36 val allClasses = classMapper.allClassesInScope(trace).map[classMapper.TypeofEClass(trace,it)]
37 val containmentRelation = references.filter[isContainment].map[referenceMapper.relationOfReference(trace,it)]
38 val containmentHierarchy = ContainmentHierarchy(allClasses,#[],containmentRelation,null)
39 problem.add(containmentHierarchy)
40 }
41}
42
43/*val containmentMapperTrace = new ContainmentMapper_ReferenceConjuction_Trace
44 trace.containmentMapperTrace = containmentMapperTrace
45
46 val containment = references.filter[containment]
47
48 val contains = problem.add(RelationDeclaration('''contains''', getType(trace,references),getType(trace,references)))
49 containmentMapperTrace.contains = contains
50 containmentMapperTrace.containsDefinition = problem.add(Assertion('''containsDefinition''',
51 Forall[
52 val parent = addVar("parent",getType(trace,references))
53 val child = addVar("child",getType(trace,references))
54 contains.call(parent,child) <=> Or(containment.map[IsInReference(trace,parent,child,it)])
55 ]
56 ))
57
58
59
60 containmentMapperTrace.notContainedByMore = problem.add(Assertion('''notContainedByMore''',
61 Forall[
62 val child = addVar('''child''',getType(trace,references))
63 val parent1 = addVar('''parent 1''',getType(trace,references))
64 val parent2 = addVar('''parent 2''',getType(trace,references))
65 (contains.call(parent1,child) && contains.call(parent2,child)) => (parent1 == parent2)
66 ]
67 ))
68
69 (2..containmentAcyclicityApproximationLevel).forEach[problem.add(getAcyclicityConstraint(trace,references,it))]
70
71 if (singleRoot) {
72 val root = problem.add(ConstantDeclaration('''root''', getType(trace, references)))
73 containmentMapperTrace.root = root
74
75 containmentMapperTrace.rootIsNotContained = problem.add(
76 Assertion(
77 '''rootIsNotContained''',
78 Forall[
79 val parent = addVar("parent", getType(trace, references))
80 !contains.call(parent, root)
81 ]
82 ))
83
84 containmentMapperTrace.everithingElseContained = problem.add(
85 Assertion(
86 '''nonrootIsContained''',
87 Forall[
88 val child = addVar("child", getType(trace, references))
89 (child != root) => Exists[
90 val parent = addVar("parent", getType(trace, references))
91 contains.call(parent, child)
92 ]
93 ]
94 ))
95 }*/
96
97
98// def asTrace(Trace<? extends ContainmentMapper> trace) { trace as ContainmentMapper_ReferenceConjuction_Trace }
99//
100// def getAcyclicityConstraint(Ecore2Logic_Trace trace, Iterable<EReference> references,int circleSize) {
101// val newTrace = trace.containmentMapperTrace.asTrace
102// //val newTrace2 = trace.containmentMapperTrace as ContainmentMapper_ReferenceConjuction_Trace // ???
103//
104// Forall[context |
105// val elements = (0..circleSize).map[context.addVar(getType(trace,references))].toList
106// !(And((0..circleSize-1).map[newTrace.contains.call(elements.get(it),elements.get(it+1))]) &&
107// newTrace.contains.call(elements.last,elements.head))
108// ]
109// }
110//
111// def private getType(Ecore2Logic_Trace trace, Iterable<EReference> references) {
112// trace.TypeofEClass(references.head.EContainingClass)
113// }
114
115 /*// This type is contained -> (From this type -> By those references)
116 val Map<TypeDescriptor,Map<TypeDescriptor,Set<EReference>>> typeContainedBy = new HashMap
117 for(reference: references.filter[containment]) {
118 val sourceType = TypeofEClass(classMapperTrace, reference.EContainingClass)
119 val targetType = TypeofEClass(classMapperTrace, reference.EType as EClass)
120 if(typeContainedBy.containsKey(targetType)) {
121 val m = typeContainedBy.get(targetType)
122 if(m.containsKey(sourceType)) {
123 m.get(sourceType).add(reference)
124 } else {
125 m.put(sourceType,new HashSet => [add(reference)])
126 }
127 } else { // initialise the collection
128 val Map<TypeDescriptor,Set<EReference>> m = new HashMap
129 m.put(sourceType,new HashSet => [add(reference)])
130 typeContainedBy.put(targetType,m)
131 }
132 }*/
133 // Only one type \ No newline at end of file
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend
new file mode 100644
index 00000000..3949749b
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EClassMapper.xtend
@@ -0,0 +1,53 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
5import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
6import java.util.Map
7import org.eclipse.emf.ecore.EClass
8import java.util.HashMap
9
10interface EClassMapper {
11 def void transformEClasses(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EClass> classes, int numberOfObjects, EcoreMetamodelDescriptor metamodelDescriptor)
12 def Iterable<EClass> allClassesInScope(Ecore2Logic_Trace trace)
13 def Type TypeofEClass(Ecore2Logic_Trace trace, EClass type)
14}
15
16class EClassMapper_AllElementAsObject_Trace implements Trace<EClassMapper_AllElementAsObject>{
17 public var Map<EClass, Type> typeMap = new HashMap;
18}
19
20class EClassMapper_AllElementAsObject implements EClassMapper{
21 val extension LogicProblemBuilder builder = new LogicProblemBuilder
22
23 override transformEClasses(Ecore2Logic_Trace trace, LogicProblem problem,
24 Iterable<EClass> classes, int numberOfObjects, EcoreMetamodelDescriptor metamodelDescriptor
25 ) {
26 val classMapperTrace = new EClassMapper_AllElementAsObject_Trace
27 trace.classMapperTrace = classMapperTrace
28 for(c:classes) {
29 val logicType = problem.add(TypeDeclaration('''class «c.name»''',c.isAbstract || c.isInterface))
30 classMapperTrace.typeMap.put(c,logicType)
31 }
32 for(c:classes) {
33 for(s : c.ESuperTypes) {
34 Supertype(classMapperTrace.typeMap.get(c),classMapperTrace.typeMap.get(s))
35 }
36 }
37 }
38
39 def asTrace(Trace<? extends EClassMapper> o) { o as EClassMapper_AllElementAsObject_Trace }
40
41 override TypeofEClass(Ecore2Logic_Trace trace, EClass type) {
42 val typeMap = trace.classMapperTrace.asTrace.typeMap
43 if(typeMap.containsKey(type)) {
44 typeMap.get(type)
45 } else {
46 throw new IllegalArgumentException('''Class «type.name» is not translated to logic!''')
47 }
48 }
49
50 override allClassesInScope(Ecore2Logic_Trace trace) {
51 trace.classMapperTrace.asTrace.typeMap.keySet
52 }
53}
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend
new file mode 100644
index 00000000..eb7e9e44
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EEnumMapper.xtend
@@ -0,0 +1,60 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription
6import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
7import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
8import java.util.ArrayList
9import java.util.HashMap
10import java.util.Map
11import org.eclipse.emf.common.util.Enumerator
12import org.eclipse.emf.ecore.EEnum
13
14import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
15
16interface EEnumMapper {
17 def void transformEEnums(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EEnum> enums,Ecore2LogicConfiguration config)
18 def Type TypeofEEnum(Ecore2Logic_Trace trace, EEnum type)
19 def TermDescription Literal(Ecore2Logic_Trace trace, Enumerator literal)
20}
21
22class EEnumMapper_PredefinedClasses_Trace implements Trace<EEnumMapper_PredefinedClasses>{
23 public var Map<EEnum, Type> enums
24
25 public var Map<Enumerator,DefinedElement> literals
26}
27
28class EEnumMapper_PredefinedClasses implements EEnumMapper {
29 val extension LogicProblemBuilder builder = new LogicProblemBuilder
30
31 override transformEEnums(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EEnum> enums, Ecore2LogicConfiguration config) {
32 val enumTrace = new EEnumMapper_PredefinedClasses_Trace
33 trace.enumMapperTrace = enumTrace
34 enumTrace.enums = new HashMap
35 enumTrace.literals = new HashMap
36
37 for(enum : enums) {
38 val l = new ArrayList<DefinedElement>(enum.ELiterals.size)
39 for(literal : enum.ELiterals.map[instance]) {
40 val element = Element('''«literal.name» «enum.name»''')
41 l+=element
42 enumTrace.literals.put(literal,element)
43 }
44 val type = TypeDefinition('''enum «enum.name»''',false,l)
45 problem.add(type)
46 enumTrace.enums.put(enum,type)
47 }
48
49 }
50
51 private def asTrace(Trace<? extends EEnumMapper> o) { o as EEnumMapper_PredefinedClasses_Trace }
52
53 override TypeofEEnum(Ecore2Logic_Trace trace, EEnum type) {
54 trace.enumMapperTrace.asTrace.enums.get(type)
55 }
56
57 override Literal(Ecore2Logic_Trace trace, Enumerator literal) {
58 literal.lookup(trace.enumMapperTrace.asTrace.literals)
59 }
60} \ No newline at end of file
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend
new file mode 100644
index 00000000..e5de6584
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/EReferenceMapper.xtend
@@ -0,0 +1,154 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory
4import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion
6import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
7import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term
8import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription
9import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
10import java.util.HashMap
11import java.util.Map
12import org.eclipse.emf.ecore.EClass
13import org.eclipse.emf.ecore.EReference
14
15interface EReferenceMapper{
16 def void transformEReferences(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> classes);
17 def Term IsInReference(Ecore2Logic_Trace trace, TermDescription source, TermDescription target, EReference type)
18 def RelationDeclaration relationOfReference(Ecore2Logic_Trace trace, EReference reference)
19}
20
21class EReferenceMapper_RelationsOverTypes_Trace implements Trace<EReferenceMapper_RelationsOverTypes> {
22 public var Map<EReference, RelationDeclaration> indicators;
23 public var Map<EReference, Assertion> typeCompliance
24 public var Map<EReference, Assertion> lowerMultiplicity
25 public var Map<EReference, Assertion> upperMultiplicity
26 public var Map<EReference, Assertion> inverseEdges
27}
28
29class EReferenceMapper_RelationsOverTypes implements EReferenceMapper{
30 val extension LogicProblemBuilder builder = new LogicProblemBuilder
31 val extension Ecore2logicannotationsFactory builder2 = Ecore2logicannotationsFactory.eINSTANCE
32 val extension EClassMapper classMapper;
33
34 public new(EClassMapper classMapper) {
35 this.classMapper = classMapper
36 }
37
38 def asTrace(Trace<? extends EReferenceMapper> o) { o as EReferenceMapper_RelationsOverTypes_Trace }
39
40 override transformEReferences(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references) {
41 val newTrace = new EReferenceMapper_RelationsOverTypes_Trace
42 trace.referenceMapperTrace = newTrace
43 trace.createIndicatorDeclarations(problem,references)
44 trace.createMultiplicityConstraints(problem,references)
45 trace.createInverseReferenceConstraints(problem,references)
46 }
47
48 def protected void createIndicatorDeclarations(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references) {
49 trace.referenceMapperTrace.asTrace.indicators = new HashMap
50 for(reference : references) {
51 val relation = problem.add(RelationDeclaration(
52 '''inreference «reference.name» «reference.EContainingClass.name»''',
53 TypeofEClass(trace,reference.EContainingClass),
54 TypeofEClass(trace,reference.EType as EClass)))
55 trace.referenceMapperTrace.asTrace.indicators.put(reference,relation as RelationDeclaration)
56 }
57 }
58
59 def void createMultiplicityConstraints(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references) {
60 trace.referenceMapperTrace.asTrace.lowerMultiplicity = new HashMap
61 trace.referenceMapperTrace.asTrace.upperMultiplicity = new HashMap
62
63 for(reference : references) {
64 val sourceType = reference.EContainingClass
65 val targetType = reference.EType as EClass
66 val lower = reference.lowerBound
67 val upper = reference.upperBound
68
69 if (lower > 0) {
70 val assertion = Assertion('''lowerMultiplicity «reference.name» «sourceType.name»''',
71 Forall[
72 val source = addVar('''src''', trace.TypeofEClass(sourceType))
73 Exists[ context |
74 val targets = (1 .. lower).map[
75 context.addVar('''trg «it»''', trace.TypeofEClass(targetType))].toList
76 val inReference = And(targets.map[IsInReference(trace, source, it, reference)])
77 if(targets.size > 1) return Distinct(targets) && inReference
78 else return inReference
79 ]
80 ]
81 )
82 val annotation = createLowerMultiplicityAssertion => [
83 it.lower = lower
84 it.relation = trace.referenceMapperTrace.asTrace.indicators.get(reference)
85 ]
86
87 problem.add(assertion)
88 problem.annotations += annotation
89 assertion.annotations +=annotation
90 trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,assertion)
91 }
92 else trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,null)
93
94 if(upper != -1) {
95 val assertion = Assertion('''upperMultiplicity «reference.name» «sourceType.name»''',
96 Forall[ context |
97 val source = context.addVar('''src''', trace.TypeofEClass(sourceType))
98 val targets = (1 .. upper+1).map[
99 context.addVar('''trg «it»''', trace.TypeofEClass(targetType))].toList
100 And(targets.map[IsInReference(trace, source, it, reference)]) => ! Distinct(targets)
101 ])
102 val annotation = createUpperMultiplicityAssertion => [
103 it.upper = upper
104 it.relation = trace.referenceMapperTrace.asTrace.indicators.get(reference)
105 ]
106
107 problem.add(assertion)
108 problem.annotations += annotation
109 assertion.annotations += annotation
110 trace.referenceMapperTrace.asTrace.lowerMultiplicity.put(reference,assertion)
111 }
112 else trace.referenceMapperTrace.asTrace.upperMultiplicity.put(reference,null)
113 }
114 }
115
116 def createInverseReferenceConstraints(Ecore2Logic_Trace trace, LogicProblem problem, Iterable<EReference> references) {
117 trace.referenceMapperTrace.asTrace.inverseEdges = new HashMap
118 for(reference : references) {
119 if(reference.EOpposite!=null) {
120 val opposite = reference.EOpposite
121 if(trace.referenceMapperTrace.asTrace.inverseEdges.containsKey(opposite)) {
122 trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,trace.referenceMapperTrace.asTrace.inverseEdges.get(opposite))
123 } else {
124 val sourceType = reference.EContainingClass
125 val targetType = reference.EType as EClass
126 val assertion = Assertion('''oppositeReference «reference.name» «sourceType.name»''',
127 Forall[
128 val src = addVar('''src''', trace.TypeofEClass(sourceType))
129 val trg = addVar('''trg''', trace.TypeofEClass(targetType))
130 IsInReference(trace,src,trg,reference) <=> IsInReference(trace,trg,src,opposite)
131 ]
132 )
133 problem.add(assertion)
134 trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,assertion)
135 val annotation = createInverseRelationAssertion => [
136 it.target = assertion
137 it.inverseA = trace.referenceMapperTrace.asTrace.indicators.get(reference)
138 it.inverseB = trace.referenceMapperTrace.asTrace.indicators.get(reference.EOpposite)
139 ]
140 problem.annotations += annotation
141 }
142 }
143 else trace.referenceMapperTrace.asTrace.inverseEdges.put(reference,null)
144 }
145 }
146
147 override IsInReference(Ecore2Logic_Trace trace, TermDescription source, TermDescription target, EReference type) {
148 trace.referenceMapperTrace.asTrace.indicators.get(type).call(source,target)
149 }
150
151 override relationOfReference(Ecore2Logic_Trace trace, EReference reference) {
152 trace.referenceMapperTrace.asTrace.indicators.get(reference)
153 }
154} \ No newline at end of file
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend
new file mode 100644
index 00000000..cae489e3
--- /dev/null
+++ b/Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/Ecore2Logic.xtend
@@ -0,0 +1,98 @@
1package hu.bme.mit.inf.dslreasoner.ecore2logic
2
3import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
4import java.util.List
5import org.eclipse.emf.ecore.EAttribute
6import org.eclipse.emf.ecore.EClass
7import org.eclipse.emf.ecore.EEnum
8import org.eclipse.emf.ecore.EEnumLiteral
9import org.eclipse.emf.ecore.EReference
10import org.eclipse.xtend.lib.annotations.Data
11import org.eclipse.xtend.lib.annotations.Delegate
12import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput
13import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
14import java.util.Set
15import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.ContainmentHierarchy
16
17@Data class EcoreMetamodelDescriptor {
18 val List<EClass> classes;
19 val Set<EClass> concretised;
20 val boolean isEObjectConcretised;
21 val List<EEnum> enums;
22 val List<EEnumLiteral> literals;
23 val List<EReference> references;
24 val List<EAttribute> attributes;
25
26 def isConcretised(EClass clazz) {concretised.contains(clazz)}
27}
28
29interface Trace<Mapper> {}
30
31class Ecore2Logic_Trace implements Trace<Ecore2Logic> {
32 public var ContainmentHierarchy containmentHierarchy;
33 public var Trace<? extends EClassMapper> classMapperTrace
34 public var Trace<? extends EEnumMapper> enumMapperTrace
35 public var Trace<? extends EReferenceMapper> referenceMapperTrace
36 public var Trace<? extends ContainmentMapper> containmentMapperTrace
37 public var Trace<? extends EAttributeMapper> attributeMapperTrace
38}
39
40class Ecore2LogicConfiguration {
41 var public boolean singleRoot = true
42 var public int numberOfObjects = -1
43 public static val Undefined = -1
44}
45
46class Ecore2LogicScope {
47 public static val int Unlimited = -1
48 public var numberOfObjects = Unlimited
49}
50
51class Ecore2Logic implements EClassMapper, EEnumMapper, EReferenceMapper, ContainmentMapper, EAttributeMapper {
52 @Delegate protected val EClassMapper classMapper
53 @Delegate protected val EEnumMapper enumMapper
54 @Delegate protected val EReferenceMapper referenceMapper
55 @Delegate protected val ContainmentMapper containmentMapper
56 @Delegate protected val EAttributeMapper attributeMapper
57
58 public new() {
59 this.classMapper = new EClassMapper_AllElementAsObject
60 this.enumMapper = new EEnumMapper_PredefinedClasses
61 this.referenceMapper = new EReferenceMapper_RelationsOverTypes(this.classMapper)
62 this.containmentMapper = new ContainmentMapper_ReferenceConjuction(this.classMapper, this.referenceMapper,0)
63 this.attributeMapper = new EAttributeMapper_RelationsOverTypes(this.classMapper, this.enumMapper)
64 }
65
66 public new(
67 EClassMapper classMapper,
68 EEnumMapper enumMapper,
69 EReferenceMapper referenceMapper,
70 ContainmentMapper containmentMapper,
71 EAttributeMapper attributeMapper)
72 {
73 if(classMapper === null || enumMapper === null || referenceMapper === null || containmentMapper === null ||
74 attributeMapper === null) {
75 throw new IllegalArgumentException('''The mappers should not be null!''')
76 }
77
78 this.classMapper = classMapper
79 this.enumMapper = enumMapper
80 this.referenceMapper = referenceMapper
81 this.containmentMapper = containmentMapper
82 this.attributeMapper = attributeMapper
83 }
84
85 def public TracedOutput<LogicProblem,Ecore2Logic_Trace> transformMetamodel(EcoreMetamodelDescriptor metamodel, Ecore2LogicConfiguration config) {
86
87
88 val Ecore2Logic_Trace trace = new Ecore2Logic_Trace
89 val LogicProblem problem = (new LogicProblemBuilder).createProblem
90 transformEClasses(trace,problem,metamodel.classes,config.numberOfObjects,metamodel)
91 transformEEnums(trace,problem,metamodel.enums,config)
92 transformEReferences(trace,problem,metamodel.references)
93 transformContainment(trace,problem,metamodel.references, config.singleRoot)
94 transformEAttributes(trace,problem,metamodel.attributes)
95
96 return new TracedOutput(problem,trace)
97 }
98}