From 60f01f46ba232ed6416054f0a6115cb2a9b70b4e Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Sat, 10 Jun 2017 19:05:05 +0200 Subject: Migrating Additional projects --- .../mit/inf/dslreasoner/util/CollectionsUtil.xtend | 82 ++++++++++++++++ .../LogicProblemBuilder_AdvancedConstructs.xtend | 71 ++++++++++++++ .../util/SetWithCustomEquivalence.xtend | 103 +++++++++++++++++++++ .../util/visualisation/Interpretation2Gml.xtend | 5 + 4 files changed, 261 insertions(+) create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/LogicProblemBuilder_AdvancedConstructs.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/visualisation/Interpretation2Gml.xtend (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util') 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 @@ +package hu.bme.mit.inf.dslreasoner.util + +import java.util.HashSet +import java.util.LinkedList +import java.util.List +import java.util.Map +import java.util.Set +import org.eclipse.xtext.xbase.lib.Functions.Function1 +import java.util.HashMap +import java.util.LinkedHashMap + +class CollectionsUtil { + public def static TO lookup(FROM from, Map map) { + if(map.containsKey(from)) { + return map.get(from) + } else throw new IllegalArgumentException(''' + The map does not contains the key "«from.toString»"! + --- Elements: --- + «FOR entry : map.entrySet SEPARATOR '\n'»«entry.key» -> «entry.value»«ENDFOR» + -----------------'''); + } + public def TO ifThenElse(FROM source, Function1 condition, Function1 ifTrue, Function1 ifFalse) { + if(condition.apply(source)) { + return ifTrue.apply(source) + } + else { + return ifFalse.apply(source) + } + } + public def static Union(Map a, Map b) { + (a.keySet + b.keySet).toInvertedMap[key | + if(a.containsKey(key)) a.get(key) + else b.get(key) + ] + } + public def static putOrAddToSet(Map> map, Key key, Value value) { + if(map.containsKey(key)) { + map.get(key).add(value) + }else{ + val set = new HashSet() => [it.add(value)] + map.put(key, set) + } + } + public def static putOrAddToList(Map> map, Key key, Value value) { + if(map.containsKey(key)) { + map.get(key).add(value) + }else{ + val set = new LinkedList() => [it.add(value)] + map.put(key, set) + } + } + def public static Map copyMap(Map oldMap, Iterable newValues, Function1 indexExtractor) { + val Map valueIndexes = oldMap.values.toMap[to|indexExtractor.apply(to)]; + val res = oldMap.mapValues[value | indexExtractor.apply(value).lookup(valueIndexes)] + return res + } + def public static Map bijectiveInverse(Map m) { m.keySet.toMap[x|x.lookup(m)] } + def public static Map> inverse(Map m) { + val res = new LinkedHashMap> + m.entrySet.forEach[res.putOrAddToList(it.value,it.key)] + return res + } + + def public static List transitiveClosurePlus(Type source, Function1> next) { + val res = new LinkedList() + transitiveClosureHelper(res,source,next) + return res + } + def public static List transitiveClosureStar(Type source, Function1> next) { + val res = new LinkedList() + res += source + transitiveClosureHelper(res,source,next) + return res + } + def private static void transitiveClosureHelper(List result, Type actual, Function1> next) { + val front = next.apply(actual) + for(elementInFront : front) { + result += elementInFront + transitiveClosureHelper(result,elementInFront,next) + } + } +} 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 @@ +package hu.bme.mit.inf.dslreasoner.util + +import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IfThenElse +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term +import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable +import java.util.ArrayList +import java.util.Collection +import java.util.List +import java.util.Map + +class LogicProblemBuilder_AdvancedConstructs { + val extension LogicProblemBuilder builder; + public new(LogicProblemBuilder builder) { + this.builder = builder + } + + def public FunctionDefinitionBody(Iterable variables, Map,Term> parametersToValue, Term other) { + val variableList = variables.toList + val entryList = parametersToValue.entrySet.toList + + // Size = 0 + if(entryList.empty && other === null) { + throw new IllegalArgumentException('''No possible value is specified!''') + // Size = 1 + } else if(entryList.size == 1 && other === null) { + return entryList.head.value + // Size = 1 + } else if(entryList.empty && !(other === null)) { + return other + // Size > 1 + }else { + // Transforming values to IF-Then-Else structures + /**The number of IF-THEN-ELSE structures needed*/ + var int iteNumber + if(other === null) iteNumber = entryList.size-1 + else iteNumber = entryList.size + + val ites = new ArrayList(iteNumber) + for (element : 0 ..< iteNumber) { + ites += ITE( + entryList.get(element).key.substitutionIsEqual(variableList), + entryList.get(element).value, + null) + } + + // Linking the IF-Then-Else structures to a chain + for (optionIndex : 1 ..< ites.size) { + val prev = ites.get(optionIndex - 1) + val next = ites.get(optionIndex) + prev.ifFalse = next + } + + if(other === null) ites.last.ifFalse = entryList.last.value + else ites.last.ifFalse = other + + // return the head of the chain + return ites.head + } + } + + def public RelationDefinitionBody(Iterable variables, Collection> elements) { + val variableList = variables.toList + return elements.map[row | row.substitutionIsEqual(variableList)].Or + } + + def private substitutionIsEqual(List substitution, List variables) { + val parameterIndexes = 0.. implements Set{ + val Function1 representer; + val HashMap map + + public new(Function1 representer) { + this.representer = representer + this.map = new HashMap + } + public new(Function1 representer, Collection initialElements) { + this.representer = representer + this.map = new HashMap + initialElements.forEach[add] + } + + override add(Type arg0) { + val representation = representer.apply(arg0) + if(!map.containsKey(representation)) { + map.put(representation,arg0); + return true + } else return false + } + + override addAll(Collection arg0) { + val originalSize = this.size + arg0.forEach[add(it)] + return (this.size != originalSize) + } + + override clear() { + map.clear + } + + override contains(Object arg0) { + try { + val rep = this.representer.apply(arg0 as Type) + return map.containsKey(rep) + } catch (ClassCastException e) { + return false + } + } + + override containsAll(Collection arg0) { + arg0.forall[it.contains] + } + + override isEmpty() { + return map.isEmpty + } + + override iterator() { + return map.values.iterator + } + + override remove(Object arg0) { + try { + val rep = this.representer.apply(arg0 as Type) + return map.remove(rep) != null + } catch (ClassCastException e) { + return false + } + } + + override removeAll(Collection arg0) { + val originalSize = this.size + arg0.forEach[remove(it)] + return (this.size != originalSize) + } + + override retainAll(Collection arg0) { + val Set representationsOfArg0 = new HashSet + for(element: arg0) { + try { + representationsOfArg0 += this.representer.apply(element as Type) + } catch(ClassCastException e) {} + } + val originalSize = this.size + for(r:this.map.keySet) { + if(!representationsOfArg0.contains(r)) + this.map.remove(r) + } + return (this.size != originalSize) + } + + override size() { + return this.map.size + } + + override toArray() { + map.values.toArray + } + + override toArray(T[] arg0) { + map.values.toArray(arg0) + } +} \ 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 @@ +package hu.bme.mit.inf.dslreasoner.util.visualisation + +class Interpretation2Gml { + +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf