diff options
author | OszkarSemerath <oszkar.semerath@gmail.com> | 2018-05-14 12:55:47 +0200 |
---|---|---|
committer | OszkarSemerath <oszkar.semerath@gmail.com> | 2018-05-14 12:55:47 +0200 |
commit | a993b72eb3e038897767c79d054467a6986d2085 (patch) | |
tree | 6ebd0c0e4d074bc7a97651e780c228014dccf42d /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend | |
parent | derived feature -> partial relation bugfix (diff) | |
download | VIATRA-Generator-a993b72eb3e038897767c79d054467a6986d2085.tar.gz VIATRA-Generator-a993b72eb3e038897767c79d054467a6986d2085.tar.zst VIATRA-Generator-a993b72eb3e038897767c79d054467a6986d2085.zip |
Moved partial model management is moved to one project (+1 PM edge fix)
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend')
1 files changed, 144 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend new file mode 100644 index 00000000..9a737ab9 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretation2logic/InstanceModel2PartialInterpretation.xtend | |||
@@ -0,0 +1,144 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretation2logic | ||
2 | |||
3 | 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.logic.model.builder.TracedOutput | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.TypeScopes | ||
7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
8 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguageFactory | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | ||
10 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
11 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.PartialInterpretationInitialiser | ||
12 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.Problem2PartialInterpretationTrace | ||
13 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRelationInterpretation | ||
14 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory | ||
15 | import java.util.HashMap | ||
16 | import java.util.List | ||
17 | import java.util.Map | ||
18 | import org.eclipse.emf.common.util.Enumerator | ||
19 | import org.eclipse.emf.ecore.EObject | ||
20 | import org.eclipse.emf.ecore.resource.Resource | ||
21 | |||
22 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
23 | |||
24 | class InstanceModel2PartialInterpretation { | ||
25 | val extension LogiclanguageFactory factory = LogiclanguageFactory.eINSTANCE | ||
26 | val extension PartialinterpretationFactory factory2 = PartialinterpretationFactory.eINSTANCE | ||
27 | val Ecore2Logic ecore2Logic = new Ecore2Logic | ||
28 | val partialInterpretationInitialiser = new PartialInterpretationInitialiser | ||
29 | |||
30 | public def transform( | ||
31 | TracedOutput<LogicProblem, Ecore2Logic_Trace> metamodelTranslationResult, | ||
32 | Resource resource, | ||
33 | boolean withID) | ||
34 | { | ||
35 | val objects = resource.allContents.toList | ||
36 | return transform(metamodelTranslationResult,objects,withID) | ||
37 | } | ||
38 | |||
39 | public def transform( | ||
40 | TracedOutput<LogicProblem, Ecore2Logic_Trace> metamodelTranslationResult, | ||
41 | List<EObject> objects, | ||
42 | boolean withID) | ||
43 | { | ||
44 | val problem = metamodelTranslationResult.output | ||
45 | val ecore2LogicTrace = metamodelTranslationResult.trace | ||
46 | val tracedOutput = partialInterpretationInitialiser.initialisePartialInterpretation(problem, new TypeScopes) | ||
47 | val partialInterpretation = tracedOutput.output | ||
48 | val partialInterpretationTrace = tracedOutput.trace | ||
49 | |||
50 | val Map<EObject,DefinedElement> object2DefinedElement = new HashMap | ||
51 | |||
52 | // EObject -> DefinedElement | ||
53 | for(objectIndex : 0..<objects.size) { | ||
54 | val object = objects.get(objectIndex) | ||
55 | val element = createDefinedElement => [ | ||
56 | it.name = if(withID) | ||
57 | { '''PartialObject«objectIndex»''' } else | ||
58 | { null } | ||
59 | ] | ||
60 | |||
61 | // Add element to interpretation | ||
62 | partialInterpretation.newElements += element | ||
63 | |||
64 | // Define the types | ||
65 | val type = ecore2Logic.TypeofEClass(ecore2LogicTrace,object.eClass) as TypeDeclaration | ||
66 | val interpretation = type.lookup(partialInterpretationTrace.type2Interpretation) | ||
67 | interpretation.elements += element | ||
68 | interpretation.supertypeInterpretation.forEach[elements += element] | ||
69 | |||
70 | // Trace handling | ||
71 | object2DefinedElement.put(object, element) | ||
72 | } | ||
73 | |||
74 | val referencesUsed = ecore2Logic.allReferencesInScope(ecore2LogicTrace).toSet | ||
75 | val attributesUsed = ecore2Logic.allAttributesInScope(ecore2LogicTrace).toSet | ||
76 | for(source : objects) { | ||
77 | // Translating the references | ||
78 | for(reference : source.eClass.EAllReferences.filter[ | ||
79 | referencesUsed.contains(it) && !it.derived]) | ||
80 | { | ||
81 | val type = ecore2Logic.relationOfReference(ecore2LogicTrace,reference) | ||
82 | val interpretation = type.lookup(partialInterpretationTrace.relation2Interpretation) | ||
83 | val sourceElement = source.lookup(object2DefinedElement) | ||
84 | if(reference.isMany) { | ||
85 | val listOfTargets = source.eGet(reference) as List<? extends EObject> | ||
86 | for(target : listOfTargets) { | ||
87 | if(target !== null && object2DefinedElement.containsKey(target)) { | ||
88 | val targetElement = target.lookup(object2DefinedElement) | ||
89 | translateLink(interpretation,sourceElement,targetElement) | ||
90 | } | ||
91 | } | ||
92 | } else { | ||
93 | val target = source.eGet(reference) as EObject | ||
94 | if(target !== null && object2DefinedElement.containsKey(target)) { | ||
95 | val targetElement = target.lookup(object2DefinedElement) | ||
96 | translateLink(interpretation,sourceElement,targetElement) | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | // Transforming the attributes | ||
102 | for(attribute : source.eClass.EAllAttributes.filter[attributesUsed.contains(it) && !it.derived]) { | ||
103 | val type = ecore2Logic.relationOfAttribute(ecore2LogicTrace,attribute) | ||
104 | val interpretation = type.lookup(partialInterpretationTrace.relation2Interpretation) | ||
105 | val sourceElement = source.lookup(object2DefinedElement) | ||
106 | if(attribute.isMany) { | ||
107 | val listOfTargets = source.eGet(attribute) as List<? extends EObject> | ||
108 | for(target : listOfTargets) { | ||
109 | val value = translateValue(target,ecore2LogicTrace,partialInterpretationTrace) | ||
110 | if(value !== null) { | ||
111 | translateLink(interpretation,sourceElement,value) | ||
112 | } | ||
113 | } | ||
114 | } else { | ||
115 | val target = source.eGet(attribute) | ||
116 | if(target !== null) { | ||
117 | val value = translateValue(target,ecore2LogicTrace,partialInterpretationTrace) | ||
118 | if(value !== null) { | ||
119 | translateLink(interpretation,sourceElement,value) | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | |||
126 | return partialInterpretation | ||
127 | } | ||
128 | |||
129 | protected def translateLink(PartialRelationInterpretation interpretation, DefinedElement source, DefinedElement target) { | ||
130 | interpretation.relationlinks += createBinaryElementRelationLink => [it.param1 = source it.param2 = target] | ||
131 | } | ||
132 | |||
133 | dispatch protected def translateValue(Enumerator value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { | ||
134 | val term = this.ecore2Logic.Literal(ecore2LogicTrace,value) | ||
135 | if(term instanceof DefinedElement) { | ||
136 | return term | ||
137 | } | ||
138 | else throw new AssertionError('''term should be a defined element?''') | ||
139 | } | ||
140 | |||
141 | dispatch protected def translateValue(Object value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) { | ||
142 | //throw new UnsupportedOperationException('''Mapping of «value.class.simpleName» in partial models is currently not supported!''') | ||
143 | } | ||
144 | } \ No newline at end of file | ||