diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java similarity index 82% rename from src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java rename to src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java index c88bad4..b825e88 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultListener.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/IUnifyResultListener.java @@ -4,7 +4,7 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import java.util.Set; -public interface UnifyResultListener { +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 f988235..27a7218 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, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + public Set> unify(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + ret.setPool(pool); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -54,9 +51,10 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + public UnifyResultModelParallel unifyAsync(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModel usedTasks) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + ret.setPool(pool); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); pool.invoke(unifyTask); return ret; } @@ -72,10 +70,11 @@ public class TypeUnify { * @param ret * @return */ - public UnifyResultModel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { + public UnifyResultModelParallel unifyParallel(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModel usedTasks) { + ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + ret.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, usedTasks, statistics); - ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -108,7 +107,7 @@ public class TypeUnify { * @param cons * @return */ - public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { + public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModelParallel ret, UnifyTaskModel usedTasks) { TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret, usedTasks); 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 61d6ce8..bd33c7f 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, UnifyTaskModel usedTasks, + IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, UnifyTaskModel usedTasks, Set methodSignatureConstraintUebergabe, Writer statistics) { this(setToFlatten, eq, oderConstraints, nextSetElement, fc, parallel, logFile, log, rekTiefe, urm, usedTasks, methodSignatureConstraintUebergabe ); } - public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, Set methodSignatureConstraintUebergabe) { + public TypeUnify2Task(Set> setToFlatten, Set eq, List>> oderConstraints, Set nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, UnifyTaskModel usedTasks, Set methodSignatureConstraintUebergabe) { super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks); 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 9efd728..8be220d 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; @@ -150,11 +150,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, UnifyTaskModel usedTasks, Writer statistics) { + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, UnifyTaskModel usedTasks, Writer statistics) { this(eq,oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks); this.statistics = statistics; } - public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) { + public TypeUnifyTask(Set eq, List>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModelParallel urm, UnifyTaskModel usedTasks) { synchronized (this) { if(statistics==null){ statistics = new NullWriter(); 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 index 2168a65..951f52f 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModel.java @@ -1,16 +1,11 @@ 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 { @@ -25,13 +20,13 @@ public class UnifyResultModel { this.fc = fc; } - private List listeners = new ArrayList<>(); + private List listeners = new ArrayList<>(); - public void addUnifyResultListener(UnifyResultListener listenerToAdd) { + public void addUnifyResultListener(IUnifyResultListener listenerToAdd) { listeners.add(listenerToAdd); } - public void removeUnifyResultListener(UnifyResultListener listenerToRemove) { + public void removeUnifyResultListener(IUnifyResultListener listenerToRemove) { listeners.remove(listenerToRemove); } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java index 9521c1c..f4a831b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/UnifyResultModelParallel.java @@ -14,7 +14,7 @@ public class UnifyResultModelParallel { private ForkJoinPool pool; private ConstraintSet cons; private IFiniteClosure fc; - private List listeners = new ArrayList<>(); + private List listeners = new ArrayList<>(); public UnifyResultModelParallel(ConstraintSet cons, IFiniteClosure fc){ this.cons = cons; @@ -24,10 +24,10 @@ public class UnifyResultModelParallel { public void setPool(ForkJoinPool pool){ this.pool = pool; } - public void addUnifyResultListener(UnifyResultListener listenerToAdd) { + public void addUnifyResultListener(IUnifyResultListener listenerToAdd) { listeners.add(listenerToAdd); } - public void removeUnifyResultListener(UnifyResultListener listenerToRemove) { + public void removeUnifyResultListener(IUnifyResultListener listenerToRemove) { listeners.remove(listenerToRemove); } public void notify(Set> eqPrimePrimeSet){ @@ -43,7 +43,7 @@ public class UnifyResultModelParallel { else return x; //wenn nichts veraendert wurde wird x zurueckgegeben }).collect(Collectors.toCollection(HashSet::new)); - for (UnifyResultListener listener : listeners) { + for (IUnifyResultListener listener : listeners) { listener.onNewTypeResultFound(eqPrimePrimeSetRet); } }); diff --git a/src/test/java/UnifyTest.java b/src/test/java/UnifyTest.java index 6f75a89..7eea015 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.UnifyTaskModel; 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); UnifyTaskModel tasks = new UnifyTaskModel(); Set> solution = unifyAlgo.unify(undConstraints, oderConstraints, finiteClosure, new NullWriter(), false, urm, tasks); System.out.println(solution.size());