diff options
author | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
---|---|---|
committer | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
commit | 60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch) | |
tree | 5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend | |
parent | Initial commit, migrating from SVN (diff) | |
download | VIATRA-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/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.ecore2logic/src/hu/bme/mit/inf/dslreasoner/ecore2logic/AttributeMapper.xtend | 119 |
1 files changed, 119 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 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.ecore2logic | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.Ecore2logicannotationsFactory | ||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Assertion | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration | ||
7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term | ||
8 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TermDescription | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDescriptor | ||
10 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
11 | import java.util.HashMap | ||
12 | import java.util.Map | ||
13 | import org.eclipse.emf.ecore.EAttribute | ||
14 | import org.eclipse.emf.ecore.EEnum | ||
15 | |||
16 | interface 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 | |||
23 | class 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 | |||
30 | class 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 | ||