diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java new file mode 100644 index 0000000..b825e88 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.typeinference.unify; + +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +import java.util.Set; + +public interface IUnifyResultListener { + + void onNewTypeResultFound(Set> evt); + +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index 8212489..1beba7c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -3,16 +3,12 @@ package de.dhbwstuttgart.typeinference.unify; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; -import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.concurrent.ForkJoinPool; import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; public class TypeUnify { @@ -28,9 +24,10 @@ public class TypeUnify { * @param cons * @return */ - public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { + public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); usedTasks.setPool(pool); + ret.setPool(pool); TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret); pool.invoke(unifyTask); Set> res = unifyTask.join(); @@ -55,9 +52,10 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { + public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); usedTasks.setPool(pool); + ret.setPool(pool); TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret); pool.invoke(unifyTask); return ret; @@ -74,11 +72,11 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { + public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModelParallel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); usedTasks.setPool(pool); - TypeUnifyTask unifyTask = //new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); - new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, statistics); + ret.setPool(pool); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, statistics); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -111,7 +109,7 @@ public class TypeUnify { * @param cons * @return */ - public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModelParallel usedTasks) { + public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModelParallel usedTasks) { TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret); unifyTask.statistics = statistics; Set> res = unifyTask.compute(); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java index d0742ea..12e041c 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java @@ -23,13 +23,13 @@ public class TypeUnify2Task extends TypeUnifyTask { TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, - IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, + IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, Set methodSignatureConstraintUebergabe, Writer statistics) { this(setToFlatten, eq, oderConstraints, nextSetElement, fc, parallel, logFile, log, rekTiefe, urm, methodSignatureConstraintUebergabe ); } - public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, Set methodSignatureConstraintUebergabe) { + public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, Set methodSignatureConstraintUebergabe) { super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm); this.setToFlatten = setToFlatten; this.nextSetElement = nextSetElement; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 24d3968..8308b1d 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -73,7 +73,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /** * Fuer die Threads */ - UnifyResultModel urm; + UnifyResultModelParallel urm; private static int totalnoOfThread = 0; int thNo; protected boolean one = false; @@ -146,11 +146,11 @@ public class TypeUnifyTask extends RecursiveTask>> { */ //statistics - public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, Writer statistics) { + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, Writer statistics) { this(eq,oderConstraints, fc, parallel, logFile, log, rekTiefe, urm); this.statistics = statistics; } - public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) { + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm) { synchronized (this) { if(statistics==null){ statistics = new NullWriter(); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java deleted file mode 100644 index f490ccd..0000000 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify; - -public interface UnifyResultListener { - - void onNewTypeResultFound(UnifyResultEvent evt); - -} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java deleted file mode 100644 index f66400b..0000000 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListenerImpl.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify; - -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.List; - -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; - -public class UnifyResultListenerImpl implements UnifyResultListener { - - List results = new ArrayList<>(); - - public synchronized void onNewTypeResultFound(UnifyResultEvent evt) { - results.addAll(evt.getNewTypeResult()); - } - - public List getResults() { - return results; - } - -} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java deleted file mode 100644 index 2168a65..0000000 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java +++ /dev/null @@ -1,41 +0,0 @@ -package de.dhbwstuttgart.typeinference.unify; - -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.PairOperator; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; - -public class UnifyResultModel { - - ConstraintSet cons; - - IFiniteClosure fc; - - public UnifyResultModel(ConstraintSet cons, - IFiniteClosure fc) { - this.cons = cons; - this.fc = fc; - } - - private List listeners = new ArrayList<>(); - - public void addUnifyResultListener(UnifyResultListener listenerToAdd) { - listeners.add(listenerToAdd); - } - - public void removeUnifyResultListener(UnifyResultListener listenerToRemove) { - listeners.remove(listenerToRemove); - } - - public void notify(Set> eqPrimePrimeSet) { - - } -} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java new file mode 100644 index 0000000..f4a831b --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java @@ -0,0 +1,51 @@ +package de.dhbwstuttgart.typeinference.unify; + +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +import java.util.*; +import java.util.concurrent.ForkJoinPool; +import java.util.stream.Collectors; + +public class UnifyResultModelParallel { + private ForkJoinPool pool; + private ConstraintSet cons; + private IFiniteClosure fc; + private List listeners = new ArrayList<>(); + + public UnifyResultModelParallel(ConstraintSet cons, IFiniteClosure fc){ + this.cons = cons; + this.fc = fc; + } + + public void setPool(ForkJoinPool pool){ + this.pool = pool; + } + public void addUnifyResultListener(IUnifyResultListener listenerToAdd) { + listeners.add(listenerToAdd); + } + public void removeUnifyResultListener(IUnifyResultListener listenerToRemove) { + listeners.remove(listenerToRemove); + } + public void notify(Set> eqPrimePrimeSet){ + pool.execute(()->{ + Set> eqPrimePrimeSetRet = eqPrimePrimeSet.stream().map(x -> { + Optional> res = new RuleSet().subst(x.stream().map(y -> { + if (y.getPairOp() == PairOperator.SMALLERDOTWC) y.setPairOp(PairOperator.EQUALSDOT); + return y; //alle Paare a <.? b erden durch a =. b ersetzt + }).collect(Collectors.toCollection(HashSet::new))); + if (res.isPresent()) {//wenn subst ein Erg liefert wurde was veraendert + return new TypeUnifyTask().applyTypeUnificationRules(res.get(), fc); + } + else return x; //wenn nichts veraendert wurde wird x zurueckgegeben + }).collect(Collectors.toCollection(HashSet::new)); + + for (IUnifyResultListener listener : listeners) { + listener.onNewTypeResultFound(eqPrimePrimeSetRet); + } + }); + } +} diff --git a/src/test/java/UnifyTest.java b/src/test/java/UnifyTest.java index 84b0d50..1be6eb7 100644 --- a/src/test/java/UnifyTest.java +++ b/src/test/java/UnifyTest.java @@ -2,7 +2,7 @@ import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.TypeUnify; -import de.dhbwstuttgart.typeinference.unify.UnifyResultModel; +import de.dhbwstuttgart.typeinference.unify.UnifyResultModelParallel; import de.dhbwstuttgart.typeinference.unify.UnifyTaskModelParallel; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.model.*; @@ -76,7 +76,7 @@ public class UnifyTest { TypeUnify unifyAlgo = new TypeUnify(); ConstraintSet cons = new ConstraintSet<>(); - UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure); + UnifyResultModelParallel urm = new UnifyResultModelParallel(cons, finiteClosure); UnifyTaskModelParallel tasks = new UnifyTaskModelParallel(); Set> solution = unifyAlgo.unify(undConstraints, oderConstraints, finiteClosure, new NullWriter(), false, urm, tasks); System.out.println(solution.size());