diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util')
4 files changed, 261 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend new file mode 100644 index 00000000..25cddc05 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend | |||
@@ -0,0 +1,82 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util | ||
2 | |||
3 | import java.util.HashSet | ||
4 | import java.util.LinkedList | ||
5 | import java.util.List | ||
6 | import java.util.Map | ||
7 | import java.util.Set | ||
8 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | ||
9 | import java.util.HashMap | ||
10 | import java.util.LinkedHashMap | ||
11 | |||
12 | class CollectionsUtil { | ||
13 | public def static <FROM,TO> TO lookup(FROM from, Map<? super FROM,TO> map) { | ||
14 | if(map.containsKey(from)) { | ||
15 | return map.get(from) | ||
16 | } else throw new IllegalArgumentException(''' | ||
17 | The map does not contains the key "«from.toString»"! | ||
18 | --- Elements: --- | ||
19 | «FOR entry : map.entrySet SEPARATOR '\n'»«entry.key» -> «entry.value»«ENDFOR» | ||
20 | -----------------'''); | ||
21 | } | ||
22 | public def <FROM,TO> TO ifThenElse(FROM source, Function1<FROM,Boolean> condition, Function1<FROM,TO> ifTrue, Function1<FROM,TO> ifFalse) { | ||
23 | if(condition.apply(source)) { | ||
24 | return ifTrue.apply(source) | ||
25 | } | ||
26 | else { | ||
27 | return ifFalse.apply(source) | ||
28 | } | ||
29 | } | ||
30 | public def static <Key,Value> Union(Map<Key,Value> a, Map<Key,Value> b) { | ||
31 | (a.keySet + b.keySet).toInvertedMap[key | | ||
32 | if(a.containsKey(key)) a.get(key) | ||
33 | else b.get(key) | ||
34 | ] | ||
35 | } | ||
36 | public def static <Key,Value> putOrAddToSet(Map<Key,Set<Value>> map, Key key, Value value) { | ||
37 | if(map.containsKey(key)) { | ||
38 | map.get(key).add(value) | ||
39 | }else{ | ||
40 | val set = new HashSet<Value>() => [it.add(value)] | ||
41 | map.put(key, set) | ||
42 | } | ||
43 | } | ||
44 | public def static <Key,Value> putOrAddToList(Map<Key,List<Value>> map, Key key, Value value) { | ||
45 | if(map.containsKey(key)) { | ||
46 | map.get(key).add(value) | ||
47 | }else{ | ||
48 | val set = new LinkedList<Value>() => [it.add(value)] | ||
49 | map.put(key, set) | ||
50 | } | ||
51 | } | ||
52 | def public static <From,To,Property> Map<From,To> copyMap(Map<From,To> oldMap, Iterable<To> newValues, Function1<To,Property> indexExtractor) { | ||
53 | val Map<Property,To> valueIndexes = oldMap.values.toMap[to|indexExtractor.apply(to)]; | ||
54 | val res = oldMap.mapValues[value | indexExtractor.apply(value).lookup(valueIndexes)] | ||
55 | return res | ||
56 | } | ||
57 | def public static <From,To> Map<To,From> bijectiveInverse(Map<From,To> m) { m.keySet.toMap[x|x.lookup(m)] } | ||
58 | def public static <From,To> Map<To,List<From>> inverse(Map<From,To> m) { | ||
59 | val res = new LinkedHashMap<To,List<From>> | ||
60 | m.entrySet.forEach[res.putOrAddToList(it.value,it.key)] | ||
61 | return res | ||
62 | } | ||
63 | |||
64 | def public static <Type> List<Type> transitiveClosurePlus(Type source, Function1<Type,Iterable<Type>> next) { | ||
65 | val res = new LinkedList() | ||
66 | transitiveClosureHelper(res,source,next) | ||
67 | return res | ||
68 | } | ||
69 | def public static <Type> List<Type> transitiveClosureStar(Type source, Function1<Type,Iterable<Type>> next) { | ||
70 | val res = new LinkedList() | ||
71 | res += source | ||
72 | transitiveClosureHelper(res,source,next) | ||
73 | return res | ||
74 | } | ||
75 | def private static <Type> void transitiveClosureHelper(List<Type> result, Type actual, Function1<Type,Iterable<Type>> next) { | ||
76 | val front = next.apply(actual) | ||
77 | for(elementInFront : front) { | ||
78 | result += elementInFront | ||
79 | transitiveClosureHelper(result,elementInFront,next) | ||
80 | } | ||
81 | } | ||
82 | } | ||
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/LogicProblemBuilder_AdvancedConstructs.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/LogicProblemBuilder_AdvancedConstructs.xtend new file mode 100644 index 00000000..3db0e2a6 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/LogicProblemBuilder_AdvancedConstructs.xtend | |||
@@ -0,0 +1,71 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | ||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IfThenElse | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable | ||
7 | import java.util.ArrayList | ||
8 | import java.util.Collection | ||
9 | import java.util.List | ||
10 | import java.util.Map | ||
11 | |||
12 | class LogicProblemBuilder_AdvancedConstructs { | ||
13 | val extension LogicProblemBuilder builder; | ||
14 | public new(LogicProblemBuilder builder) { | ||
15 | this.builder = builder | ||
16 | } | ||
17 | |||
18 | def public FunctionDefinitionBody(Iterable<Variable> variables, Map<List<Term>,Term> parametersToValue, Term other) { | ||
19 | val variableList = variables.toList | ||
20 | val entryList = parametersToValue.entrySet.toList | ||
21 | |||
22 | // Size = 0 | ||
23 | if(entryList.empty && other === null) { | ||
24 | throw new IllegalArgumentException('''No possible value is specified!''') | ||
25 | // Size = 1 | ||
26 | } else if(entryList.size == 1 && other === null) { | ||
27 | return entryList.head.value | ||
28 | // Size = 1 | ||
29 | } else if(entryList.empty && !(other === null)) { | ||
30 | return other | ||
31 | // Size > 1 | ||
32 | }else { | ||
33 | // Transforming values to IF-Then-Else structures | ||
34 | /**The number of IF-THEN-ELSE structures needed*/ | ||
35 | var int iteNumber | ||
36 | if(other === null) iteNumber = entryList.size-1 | ||
37 | else iteNumber = entryList.size | ||
38 | |||
39 | val ites = new ArrayList<IfThenElse>(iteNumber) | ||
40 | for (element : 0 ..< iteNumber) { | ||
41 | ites += ITE( | ||
42 | entryList.get(element).key.substitutionIsEqual(variableList), | ||
43 | entryList.get(element).value, | ||
44 | null) | ||
45 | } | ||
46 | |||
47 | // Linking the IF-Then-Else structures to a chain | ||
48 | for (optionIndex : 1 ..< ites.size) { | ||
49 | val prev = ites.get(optionIndex - 1) | ||
50 | val next = ites.get(optionIndex) | ||
51 | prev.ifFalse = next | ||
52 | } | ||
53 | |||
54 | if(other === null) ites.last.ifFalse = entryList.last.value | ||
55 | else ites.last.ifFalse = other | ||
56 | |||
57 | // return the head of the chain | ||
58 | return ites.head | ||
59 | } | ||
60 | } | ||
61 | |||
62 | def public RelationDefinitionBody(Iterable<Variable> variables, Collection<List<Term>> elements) { | ||
63 | val variableList = variables.toList | ||
64 | return elements.map[row | row.substitutionIsEqual(variableList)].Or | ||
65 | } | ||
66 | |||
67 | def private substitutionIsEqual(List<Term> substitution, List<Variable> variables) { | ||
68 | val parameterIndexes = 0..<variables.size | ||
69 | return And(parameterIndexes.map[index | substitution.get(index) == variables.get(index)]) | ||
70 | } | ||
71 | } \ No newline at end of file | ||
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend new file mode 100644 index 00000000..ae2cfb04 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend | |||
@@ -0,0 +1,103 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util | ||
2 | |||
3 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | ||
4 | import java.util.HashMap | ||
5 | import java.util.Set | ||
6 | import java.util.Collection | ||
7 | import java.util.HashSet | ||
8 | |||
9 | class SetWithCustomEquivalence<Type,Representation> implements Set<Type>{ | ||
10 | val Function1<Type,Representation> representer; | ||
11 | val HashMap<Representation,Type> map | ||
12 | |||
13 | public new(Function1<Type,Representation> representer) { | ||
14 | this.representer = representer | ||
15 | this.map = new HashMap<Representation,Type> | ||
16 | } | ||
17 | public new(Function1<Type,Representation> representer, Collection<? extends Type> initialElements) { | ||
18 | this.representer = representer | ||
19 | this.map = new HashMap | ||
20 | initialElements.forEach[add] | ||
21 | } | ||
22 | |||
23 | override add(Type arg0) { | ||
24 | val representation = representer.apply(arg0) | ||
25 | if(!map.containsKey(representation)) { | ||
26 | map.put(representation,arg0); | ||
27 | return true | ||
28 | } else return false | ||
29 | } | ||
30 | |||
31 | override addAll(Collection<? extends Type> arg0) { | ||
32 | val originalSize = this.size | ||
33 | arg0.forEach[add(it)] | ||
34 | return (this.size != originalSize) | ||
35 | } | ||
36 | |||
37 | override clear() { | ||
38 | map.clear | ||
39 | } | ||
40 | |||
41 | override contains(Object arg0) { | ||
42 | try { | ||
43 | val rep = this.representer.apply(arg0 as Type) | ||
44 | return map.containsKey(rep) | ||
45 | } catch (ClassCastException e) { | ||
46 | return false | ||
47 | } | ||
48 | } | ||
49 | |||
50 | override containsAll(Collection<?> arg0) { | ||
51 | arg0.forall[it.contains] | ||
52 | } | ||
53 | |||
54 | override isEmpty() { | ||
55 | return map.isEmpty | ||
56 | } | ||
57 | |||
58 | override iterator() { | ||
59 | return map.values.iterator | ||
60 | } | ||
61 | |||
62 | override remove(Object arg0) { | ||
63 | try { | ||
64 | val rep = this.representer.apply(arg0 as Type) | ||
65 | return map.remove(rep) != null | ||
66 | } catch (ClassCastException e) { | ||
67 | return false | ||
68 | } | ||
69 | } | ||
70 | |||
71 | override removeAll(Collection<?> arg0) { | ||
72 | val originalSize = this.size | ||
73 | arg0.forEach[remove(it)] | ||
74 | return (this.size != originalSize) | ||
75 | } | ||
76 | |||
77 | override retainAll(Collection<?> arg0) { | ||
78 | val Set<Representation> representationsOfArg0 = new HashSet | ||
79 | for(element: arg0) { | ||
80 | try { | ||
81 | representationsOfArg0 += this.representer.apply(element as Type) | ||
82 | } catch(ClassCastException e) {} | ||
83 | } | ||
84 | val originalSize = this.size | ||
85 | for(r:this.map.keySet) { | ||
86 | if(!representationsOfArg0.contains(r)) | ||
87 | this.map.remove(r) | ||
88 | } | ||
89 | return (this.size != originalSize) | ||
90 | } | ||
91 | |||
92 | override size() { | ||
93 | return this.map.size | ||
94 | } | ||
95 | |||
96 | override toArray() { | ||
97 | map.values.toArray | ||
98 | } | ||
99 | |||
100 | override <T> toArray(T[] arg0) { | ||
101 | map.values.toArray(arg0) | ||
102 | } | ||
103 | } \ No newline at end of file | ||
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/visualisation/Interpretation2Gml.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/visualisation/Interpretation2Gml.xtend new file mode 100644 index 00000000..472799f9 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/visualisation/Interpretation2Gml.xtend | |||
@@ -0,0 +1,5 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util.visualisation | ||
2 | |||
3 | class Interpretation2Gml { | ||
4 | |||
5 | } \ No newline at end of file | ||