aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend')
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend158
1 files changed, 83 insertions, 75 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend
index b4403979..29d3eb61 100644
--- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend
+++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend
@@ -1,10 +1,13 @@
1package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns 1package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns
2 2
3import com.google.common.collect.ImmutableMap
4import com.google.common.collect.ImmutableSet
3import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.UpperMultiplicityAssertion 5import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.UpperMultiplicityAssertion
4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation 6import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration 7import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
6import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem 8import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
7import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality 9import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality
10import java.util.Collection
8import java.util.HashMap 11import java.util.HashMap
9import java.util.List 12import java.util.List
10import java.util.Map 13import java.util.Map
@@ -14,41 +17,40 @@ import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
14 17
15class RelationDeclarationIndexer { 18class RelationDeclarationIndexer {
16 val PatternGenerator base; 19 val PatternGenerator base;
17 20
18 new(PatternGenerator base) { 21 new(PatternGenerator base) {
19 this.base = base 22 this.base = base
20 } 23 }
21 24
22 def generateRelationIndexers(LogicProblem problem, Iterable<RelationDeclaration> relations, Map<String,PQuery> fqn2PQuery) { 25 def generateRelationIndexers(LogicProblem problem, Iterable<RelationDeclaration> relations,
26 Iterable<UnitPropagationPatternGenerator> unitPropagationPatternGenerators, Map<String, PQuery> fqn2PQuery) {
23 val upperMultiplicities = new HashMap 27 val upperMultiplicities = new HashMap
24 problem.annotations.filter(UpperMultiplicityAssertion).forEach[ 28 problem.annotations.filter(UpperMultiplicityAssertion).forEach [
25 upperMultiplicities.put(it.relation,it.upper) 29 upperMultiplicities.put(it.relation, it.upper)
26 ] 30 ]
27 31 val mustNotRelations = ImmutableMap.copyOf(unitPropagationPatternGenerators.flatMap[mustNotPatterns.entrySet].
32 groupBy[key].mapValues[ImmutableSet.copyOf(map[value])])
33
28 return ''' 34 return '''
29 «FOR relation : relations» 35 «FOR relation : relations»
30 «IF base.isDerived(relation)» 36 «IF base.isDerived(relation)»
31 «generateDerivedMustRelation(problem,relation,base.getDerivedDefinition(relation).patternFullyQualifiedName.lookup(fqn2PQuery))» 37 «generateDerivedMustRelation(problem,relation,base.getDerivedDefinition(relation).patternFullyQualifiedName.lookup(fqn2PQuery))»
32 «generateDerivedMayRelation(problem,relation,base.getDerivedDefinition(relation).patternFullyQualifiedName.lookup(fqn2PQuery))» 38 «generateDerivedMayRelation(problem,relation,base.getDerivedDefinition(relation).patternFullyQualifiedName.lookup(fqn2PQuery))»
33 «ELSE» 39 «ELSE»
34 «generateMustRelation(problem,relation)» 40 «generateMustRelation(problem, relation)»
35 «generateMayRelation(problem,relation,upperMultiplicities,base.getContainments(problem),base.getInverseRelations(problem),fqn2PQuery)» 41 «generateMayRelation(problem, relation, upperMultiplicities, base.getContainments(problem), base.getInverseRelations(problem), mustNotRelations.get(relation) ?: emptySet, fqn2PQuery)»
36 «ENDIF» 42 «ENDIF»
37 «ENDFOR» 43 «ENDFOR»
38 ''' 44 '''
39 } 45 }
40 46
41 def private patternName(RelationDeclaration r, Modality modality) { 47 def private patternName(RelationDeclaration r, Modality modality) {
42 '''«modality.name.toLowerCase»InRelation«base.canonizeName(r.name)»''' 48 '''«modality.name.toLowerCase»InRelation«base.canonizeName(r.name)»'''
43 } 49 }
44 50
45 def referRelation( 51 def referRelation(RelationDeclaration referred, String sourceVariable, String targetVariable,
46 RelationDeclaration referred, 52 Modality modality) '''find «referred.patternName(modality)»(problem,interpretation,«sourceVariable»,«targetVariable»);'''
47 String sourceVariable, 53
48 String targetVariable,
49 Modality modality)
50 '''find «referred.patternName(modality)»(problem,interpretation,«sourceVariable»,«targetVariable»);'''
51
52 def generateMustRelation(LogicProblem problem, RelationDeclaration relation) ''' 54 def generateMustRelation(LogicProblem problem, RelationDeclaration relation) '''
53 /** 55 /**
54 * Matcher for detecting tuples t where []«relation.name»(source,target) 56 * Matcher for detecting tuples t where []«relation.name»(source,target)
@@ -65,59 +67,64 @@ class RelationDeclarationIndexer {
65 BinaryElementRelationLink.param2(link,target); 67 BinaryElementRelationLink.param2(link,target);
66 } 68 }
67 ''' 69 '''
70
68 def generateMayRelation(LogicProblem problem, RelationDeclaration relation, 71 def generateMayRelation(LogicProblem problem, RelationDeclaration relation,
69 Map<Relation, Integer> upperMultiplicities, 72 Map<Relation, Integer> upperMultiplicities, List<Relation> containments,
70 List<Relation> containments, 73 HashMap<Relation, Relation> inverseRelations, Collection<String> mustNotRelations,
71 HashMap<Relation, Relation> inverseRelations, 74 Map<String, PQuery> fqn2PQuery) {
72 Map<String,PQuery> fqn2PQuery)
73 {
74 return ''' 75 return '''
75 /** 76 /**
76 * Matcher for detecting tuples t where <>«relation.name»(source,target) 77 * Matcher for detecting tuples t where <>«relation.name»(source,target)
77 */ 78 */
78 private pattern «relation.patternName(Modality.MAY)»( 79 private pattern «relation.patternName(Modality.MAY)»(
79 problem:LogicProblem, interpretation:PartialInterpretation, 80 problem:LogicProblem, interpretation:PartialInterpretation,
80 source: DefinedElement, target:DefinedElement) 81 source: DefinedElement, target:DefinedElement)
81 { 82 {
82 find interpretation(problem,interpretation); 83 find interpretation(problem,interpretation);
83 // The two endpoint of the link have to exist 84 // The two endpoint of the link have to exist
84 find mayExist(problem, interpretation, source); 85 find mayExist(problem, interpretation, source);
85 find mayExist(problem, interpretation, target); 86 find mayExist(problem, interpretation, target);
86 // Type consistency 87 // Type consistency
87 «base.typeIndexer.referInstanceOfByReference(relation.parameters.get(0),Modality.MAY,"source")» 88 «base.typeIndexer.referInstanceOfByReference(relation.parameters.get(0),Modality.MAY,"source")»
88 «base.typeIndexer.referInstanceOfByReference(relation.parameters.get(1),Modality.MAY,"target")» 89 «base.typeIndexer.referInstanceOfByReference(relation.parameters.get(1),Modality.MAY,"target")»
89 «IF upperMultiplicities.containsKey(relation)» 90 «IF upperMultiplicities.containsKey(relation)»
90 // There are "numberOfExistingReferences" currently existing instances of the reference from the source, 91 // There are "numberOfExistingReferences" currently existing instances of the reference from the source,
91 // the upper bound of the multiplicity should be considered. 92 // the upper bound of the multiplicity should be considered.
92 numberOfExistingReferences == count «referRelation(relation,"source","_",Modality.MUST)» 93 numberOfExistingReferences == count «referRelation(relation,"source","_",Modality.MUST)»
93 numberOfExistingReferences != «upperMultiplicities.get(relation)»; 94 numberOfExistingReferences != «upperMultiplicities.get(relation)»;
94 «ENDIF» 95 «ENDIF»
95 «IF inverseRelations.containsKey(relation) && upperMultiplicities.containsKey(inverseRelations.get(relation))» 96 «IF inverseRelations.containsKey(relation) && upperMultiplicities.containsKey(inverseRelations.get(relation))»
96 // There are "numberOfExistingReferences" currently existing instances of the reference to the target, 97 // There are "numberOfExistingReferences" currently existing instances of the reference to the target,
97 // the upper bound of the opposite reference multiplicity should be considered. 98 // the upper bound of the opposite reference multiplicity should be considered.
98 numberOfExistingOppositeReferences == count «base.referRelation(inverseRelations.get(relation),"target","_",Modality.MUST,fqn2PQuery)» 99 numberOfExistingOppositeReferences == count «base.referRelation(inverseRelations.get(relation),"target","_",Modality.MUST,fqn2PQuery)»
99 numberOfExistingOppositeReferences != «upperMultiplicities.get(inverseRelations.get(relation))»; 100 numberOfExistingOppositeReferences != «upperMultiplicities.get(inverseRelations.get(relation))»;
100 «ENDIF» 101 «ENDIF»
101 «IF containments.contains(relation)» 102 «IF containments.contains(relation)»
102 // The reference is containment, then a new reference cannot be create if: 103 // The reference is containment, then a new reference cannot be create if:
103 // 1. Multiple parents 104 // 1. Multiple parents
104 neg «base.containmentIndexer.referMustContaint("_","target")» 105 neg «base.containmentIndexer.referMustContaint("_","target")»
105 // 2. Circle in the containment hierarchy 106 // 2. Circle in the containment hierarchy
106 neg «base.containmentIndexer.referTransitiveMustContains("target","source")» 107 neg «base.containmentIndexer.referTransitiveMustContains("target","source")»
107 «ENDIF» 108 «ENDIF»
108 «IF inverseRelations.containsKey(relation) && containments.contains(inverseRelations.get(relation))» 109 «IF inverseRelations.containsKey(relation) && containments.contains(inverseRelations.get(relation))»
109 // The eOpposite of the reference is containment, then a referene cannot be created if 110 // The eOpposite of the reference is containment, then a referene cannot be created if
110 // 1. Multiple parents 111 // 1. Multiple parents
111 neg «base.containmentIndexer.referMustContaint("source","_")» 112 neg «base.containmentIndexer.referMustContaint("source","_")»
112 // 2. Circle in the containment hierarchy 113 // 2. Circle in the containment hierarchy
113 neg «base.containmentIndexer.referTransitiveMustContains("source","target")» 114 neg «base.containmentIndexer.referTransitiveMustContains("source","target")»
114 «ENDIF» 115 «ENDIF»
115 } or { 116 «IF mustNotRelations.empty»
116 «relation.referRelation("source","target",Modality.MUST)» 117 // ![] unit propagation relations
117 } 118 «FOR mustNotRelation : mustNotRelations»
118 ''' 119 neg find «mustNotRelation»(problem, interpretation, source, target);
120 «ENDFOR»
121 «ENDIF»
122 } or {
123 «relation.referRelation("source","target",Modality.MUST)»
124 }
125 '''
119 } 126 }
120 127
121 def generateDerivedMustRelation(LogicProblem problem, RelationDeclaration relation, PQuery definition) ''' 128 def generateDerivedMustRelation(LogicProblem problem, RelationDeclaration relation, PQuery definition) '''
122 /** 129 /**
123 * Matcher for detecting tuples t where []«relation.name»(source,target) 130 * Matcher for detecting tuples t where []«relation.name»(source,target)
@@ -129,6 +136,7 @@ class RelationDeclarationIndexer {
129 «base.relationDefinitionIndexer.referPattern(definition,#["source","target"],Modality::MUST,true,false)» 136 «base.relationDefinitionIndexer.referPattern(definition,#["source","target"],Modality::MUST,true,false)»
130 } 137 }
131 ''' 138 '''
139
132 def generateDerivedMayRelation(LogicProblem problem, RelationDeclaration relation, PQuery definition) ''' 140 def generateDerivedMayRelation(LogicProblem problem, RelationDeclaration relation, PQuery definition) '''
133 /** 141 /**
134 * Matcher for detecting tuples t where []«relation.name»(source,target) 142 * Matcher for detecting tuples t where []«relation.name»(source,target)
@@ -140,4 +148,4 @@ class RelationDeclarationIndexer {
140 «base.relationDefinitionIndexer.referPattern(definition,#["source","target"],Modality::MAY,true,false)» 148 «base.relationDefinitionIndexer.referPattern(definition,#["source","target"],Modality::MAY,true,false)»
141 } 149 }
142 ''' 150 '''
143} \ No newline at end of file 151}