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