diff options
Diffstat (limited to 'Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/src/hu/bme/mit/inf/dlsreasoner/alloy/reasoner/builder/Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal.xtend')
-rw-r--r-- | Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/src/hu/bme/mit/inf/dlsreasoner/alloy/reasoner/builder/Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal.xtend | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/src/hu/bme/mit/inf/dlsreasoner/alloy/reasoner/builder/Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal.xtend b/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/src/hu/bme/mit/inf/dlsreasoner/alloy/reasoner/builder/Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal.xtend new file mode 100644 index 00000000..7d90b2b0 --- /dev/null +++ b/Solvers/Alloy-Solver2/hu.bme.mit.inf.dlsreasoner.alloy.reasoner/src/hu/bme/mit/inf/dlsreasoner/alloy/reasoner/builder/Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal.xtend | |||
@@ -0,0 +1,122 @@ | |||
1 | package hu.bme.mit.inf.dlsreasoner.alloy.reasoner.builder | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.alloyLanguage.ALSMultiplicity | ||
4 | import hu.bme.mit.inf.dslreasoner.alloyLanguage.ALSSignatureBody | ||
5 | import hu.bme.mit.inf.dslreasoner.alloyLanguage.AlloyLanguageFactory | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | ||
8 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDefinition | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicproblemPackage | ||
10 | import java.util.Collection | ||
11 | import java.util.LinkedList | ||
12 | |||
13 | class Logic2AlloyLanguageMapper_TypeMapper_InheritanceAndHorizontal implements Logic2AlloyLanguageMapper_TypeMapper{ | ||
14 | private val extension AlloyLanguageFactory factory = AlloyLanguageFactory.eINSTANCE | ||
15 | private val Logic2AlloyLanguageMapper_Support support = new Logic2AlloyLanguageMapper_Support; | ||
16 | |||
17 | new() { | ||
18 | LogicproblemPackage.eINSTANCE.class | ||
19 | } | ||
20 | |||
21 | override transformTypes(Collection<Type> types, Collection<DefinedElement> elements, Logic2AlloyLanguageMapper mapper, Logic2AlloyLanguageMapperTrace trace) { | ||
22 | val typeTrace = new Logic2AlloyLanguageMapper_TypeMapperTrace_InheritanceAndHorizontal | ||
23 | trace.typeMapperTrace = typeTrace | ||
24 | |||
25 | // 1. A global type for Objects is created | ||
26 | val objectSig = createALSSignatureDeclaration => [it.name = support.toID(#["util","Object"])] | ||
27 | val objectBody = createALSSignatureBody => [ | ||
28 | it.declarations += objectSig | ||
29 | it.abstract = true | ||
30 | ] | ||
31 | typeTrace.objectSupperClass = objectSig | ||
32 | trace.specification.signatureBodies += objectBody | ||
33 | |||
34 | // 2. Each type is mapped to a unique sig | ||
35 | for(type : types) { | ||
36 | val sig = createALSSignatureDeclaration => [it.name = support.toIDMultiple("type",type.name)] | ||
37 | val body = createALSSignatureBody => [it.declarations += sig] | ||
38 | body.abstract = type.isIsAbstract || (type instanceof TypeDefinition) | ||
39 | |||
40 | trace.specification.signatureBodies += body | ||
41 | typeTrace.type2ALSType.put(type,sig) | ||
42 | |||
43 | typeTrace.typeSelection.put(type,new LinkedList()=>[add(sig)]) | ||
44 | } | ||
45 | |||
46 | for(element : elements) { | ||
47 | val mostSpecificTypes = element.definedInType.filter[it.subtypes.empty] | ||
48 | if(mostSpecificTypes.size== 1) { | ||
49 | val mostSpecificSubtype = mostSpecificTypes.head | ||
50 | val elementContainer = createALSSignatureBody => [ | ||
51 | it.multiplicity = ALSMultiplicity::ONE | ||
52 | it.supertype =typeTrace.type2ALSType.get(mostSpecificSubtype) | ||
53 | ] | ||
54 | val signature = createALSSignatureDeclaration => [it.name = support.toIDMultiple("element",element.name)] | ||
55 | elementContainer.declarations += signature | ||
56 | typeTrace.definedElement2Declaration.put(element,signature) | ||
57 | trace.specification.signatureBodies += elementContainer | ||
58 | } else { | ||
59 | throw new UnsupportedOperationException | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // 6. Each inheritance is modeled by extend keyword | ||
64 | for(type : types) { | ||
65 | if(type.supertypes.size == 0) { | ||
66 | (typeTrace.type2ALSType.get(type).eContainer as ALSSignatureBody).supertype = typeTrace.objectSupperClass | ||
67 | }if(type.supertypes.size == 1) { | ||
68 | val alsType = typeTrace.type2ALSType.get(type.supertypes.head) | ||
69 | (typeTrace.type2ALSType.get(type).eContainer as ALSSignatureBody).supertype = alsType | ||
70 | |||
71 | } else if(type.supertypes.size > 1){ | ||
72 | val alsMainType = typeTrace.type2ALSType.get(type.supertypes.head) | ||
73 | (typeTrace.type2ALSType.get(type).eContainer as ALSSignatureBody).supertype = alsMainType | ||
74 | for(otherType : type.supertypes.filter[it != alsMainType]) { | ||
75 | typeTrace.typeSelection.get(otherType).add(typeTrace.type2ALSType.get(type)) | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | private def boolean hasDefinedSupertype(Type type) { | ||
82 | if(type instanceof TypeDefinition) { | ||
83 | return true | ||
84 | } else { | ||
85 | if(type.supertypes.empty) return false | ||
86 | else return type.supertypes.exists[it.hasDefinedSupertype] | ||
87 | } | ||
88 | } | ||
89 | |||
90 | def getTypeTrace(Logic2AlloyLanguageMapperTrace trace) { | ||
91 | val res = trace.typeMapperTrace | ||
92 | if(res instanceof Logic2AlloyLanguageMapper_TypeMapperTrace_InheritanceAndHorizontal) { | ||
93 | return res | ||
94 | } else { | ||
95 | throw new IllegalArgumentException(''' | ||
96 | Expected type mapping trace: «Logic2AlloyLanguageMapper_TypeMapperTrace_FilteredTypes.name», | ||
97 | but found «res.class.name»''') | ||
98 | } | ||
99 | } | ||
100 | |||
101 | override transformTypeReference(Type referred, Logic2AlloyLanguageMapper mapper, Logic2AlloyLanguageMapperTrace trace) { | ||
102 | trace.typeTrace.typeSelection.get(referred) | ||
103 | } | ||
104 | |||
105 | override getUndefinedSupertype(Logic2AlloyLanguageMapperTrace trace) { | ||
106 | trace.typeTrace.objectSupperClass | ||
107 | } | ||
108 | |||
109 | override transformReference(DefinedElement referred, Logic2AlloyLanguageMapperTrace trace) { | ||
110 | val r = trace.typeTrace.definedElement2Declaration.get(referred) | ||
111 | return createALSReference => [it.referred =r] | ||
112 | } | ||
113 | |||
114 | override getUndefinedSupertypeScope(int undefinedScope, Logic2AlloyLanguageMapperTrace trace) { | ||
115 | return undefinedScope + trace.typeTrace.definedElement2Declaration.size | ||
116 | } | ||
117 | |||
118 | override getTypeInterpreter() { | ||
119 | return new AlloyModelInterpretation_TypeInterpretation_InheritanceAndHorizontal | ||
120 | } | ||
121 | |||
122 | } \ No newline at end of file | ||