diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 3355975..12ec03b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -9,6 +9,7 @@ import java.util.Optional; import java.util.Queue; import java.util.Set; import java.util.Stack; +import java.util.concurrent.ForkJoinPool; import java.util.function.Function; import java.util.stream.Collectors; @@ -35,6 +36,7 @@ import java.io.Writer; import java.io.OutputStreamWriter; import org.apache.commons.io.output.NullOutputStream; +import org.apache.commons.io.output.NullWriter; /** * Implementation of the type inference rules. @@ -43,14 +45,14 @@ import org.apache.commons.io.output.NullOutputStream; */ public class RuleSet implements IRuleSet{ - Writer logFile; + WriterActiveObject logFile; public RuleSet() { super(); - logFile = new OutputStreamWriter(new NullOutputStream()); + logFile = new WriterActiveObject(new NullWriter(), ForkJoinPool.commonPool()); } - RuleSet(Writer logFile) { + RuleSet(WriterActiveObject logFile) { this.logFile = logFile; } @@ -866,14 +868,8 @@ public class RuleSet implements IRuleSet{ UnifyType r = x.getRhsType(); if (r instanceof PlaceholderType) { ((PlaceholderType)r).disableWildcardtable(); } } ); - try { - logFile.write("FUNgreater: " + pair + "\n"); - logFile.write("FUNred: " + result + "\n"); - logFile.flush(); - } - catch (IOException e) { - System.out.println("logFile-Error"); - } + logFile.write("FUNgreater: " + pair + "\n"); + logFile.write("FUNred: " + result + "\n"); return Optional.of(result); } @@ -916,14 +912,9 @@ public class RuleSet implements IRuleSet{ UnifyType r = x.getRhsType(); if (r instanceof PlaceholderType) { ((PlaceholderType)r).disableWildcardtable(); } } ); - try { - logFile.write("FUNgreater: " + pair + "\n"); - logFile.write("FUNgreater: " + result + "\n"); - logFile.flush(); - } - catch (IOException e) { - System.out.println("lofFile-Error"); - } + + logFile.write("FUNgreater: " + pair + "\n"); + logFile.write("FUNgreater: " + result + "\n"); return Optional.of(result); } @@ -966,14 +957,9 @@ public class RuleSet implements IRuleSet{ UnifyType r = x.getRhsType(); if (r instanceof PlaceholderType) { ((PlaceholderType)r).disableWildcardtable(); } } ); - try { - logFile.write("FUNgreater: " + pair + "\n"); - logFile.write("FUNsmaller: " + result + "\n"); - logFile.flush(); - } - catch (IOException e) { - System.out.println("lofFile-Error"); - } + logFile.write("FUNgreater: " + pair + "\n"); + logFile.write("FUNsmaller: " + result + "\n"); + return Optional.of(result); } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java index f988235..f92a2c6 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify.java @@ -29,8 +29,8 @@ public class TypeUnify { * @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); ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, new WriterActiveObject(logFile, pool), log, 0, ret, usedTasks); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -55,8 +55,8 @@ public class TypeUnify { * @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); ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, new WriterActiveObject(logFile, pool), log, 0, ret, usedTasks); pool.invoke(unifyTask); return ret; } @@ -73,9 +73,9 @@ public class TypeUnify { * @return */ public UnifyResultModel unifyParallel(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); - new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks, statistics); ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); + TypeUnifyTask unifyTask = //new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, usedTasks); + new TypeUnifyTask(undConstrains, oderConstraints, fc, true, new WriterActiveObject(logFile, pool), log, 0, ret, usedTasks, new WriterActiveObject(statistics, pool)); pool.invoke(unifyTask); Set> res = unifyTask.join(); try { @@ -109,8 +109,7 @@ public class TypeUnify { * @return */ public Set> unifyOderConstraints(Set undConstrains, List>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret, UnifyTaskModel usedTasks) { - TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret, usedTasks); - unifyTask.statistics = statistics; + TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, new WriterActiveObject(logFile, ForkJoinPool.commonPool()), log, 0, ret, usedTasks, new WriterActiveObject(statistics, ForkJoinPool.commonPool())); Set> res = unifyTask.compute(); try { logFile.write("\nnoShortendElements: " + unifyTask.noShortendElements +"\n"); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnify2Task.java index 61d6ce8..0ad4cd5 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, - Set methodSignatureConstraintUebergabe, Writer statistics) { + IFiniteClosure fc, boolean parallel, WriterActiveObject logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, + Set methodSignatureConstraintUebergabe, WriterActiveObject 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, WriterActiveObject logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, Set methodSignatureConstraintUebergabe) { super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, usedTasks); this.setToFlatten = setToFlatten; this.nextSetElement = nextSetElement; @@ -64,13 +64,6 @@ public class TypeUnify2Task extends TypeUnifyTask { } public void closeLogFile() { - - try { - logFile.close(); - } - catch (IOException ioE) { - System.err.println("no log-File" + thNo); - } - + logFile.close(); } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 456e16d..c9cda7f 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; import java.util.function.BiFunction; import java.util.function.BinaryOperator; @@ -79,7 +80,7 @@ public class TypeUnifyTask extends RecursiveTask>> { protected boolean one = false; public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/"; - Writer logFile; + protected WriterActiveObject logFile; /** * The implementation of setOps that will be used during the unification @@ -129,7 +130,7 @@ public class TypeUnifyTask extends RecursiveTask>> { volatile UnifyTaskModel usedTasks; - static Writer statistics; + static WriterActiveObject statistics; public TypeUnifyTask() { rules = new RuleSet(); @@ -150,13 +151,13 @@ 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, WriterActiveObject logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks, WriterActiveObject 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, WriterActiveObject logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) { if(statistics==null){ - statistics = new NullWriter(); + statistics = new WriterActiveObject(new NullWriter(), ForkJoinPool.commonPool()); } this.eq = eq; //this.oderConstraints = oderConstraints.stream().map(x -> x.stream().map(y -> new HashSet<>(y)).collect(Collectors.toSet(HashSet::new))).collect(Collectors.toList(ArrayList::new)); @@ -182,9 +183,11 @@ public class TypeUnifyTask extends RecursiveTask>> { thNo = totalnoOfThread; writeLog("thNo2 " + thNo); try { - this.logFile = log ? new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "Thread_"+thNo)) - : new OutputStreamWriter(new NullOutputStream()); - logFile.write(""); + if(log){ + this.logFile = new WriterActiveObject(new FileWriter(new File(System.getProperty("user.dir") + "/logFiles/" + "Thread_"+thNo)), ForkJoinPool.commonPool()); + }else{ + this.logFile = new WriterActiveObject(new NullWriter(), ForkJoinPool.commonPool()); + } } catch (IOException e) { System.err.println("log-File nicht vorhanden"); @@ -264,12 +267,8 @@ public class TypeUnifyTask extends RecursiveTask>> { .filter(x -> x.size()>1) .collect(Collectors.toCollection(ArrayList::new)); Set> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, new HashSet<>()); - try { - logFile.close(); - } - catch (IOException ioE) { - System.err.println("no log-File"); - } + logFile.close(); + if (isUndefinedPairSetSet(res)) { //fuer debug-Zwecke ArrayList al = res.stream().map(x -> x.stream().collect(Collectors.toCollection(ArrayList::new))) @@ -2253,27 +2252,13 @@ public class TypeUnifyTask extends RecursiveTask>> { void writeLog(String str) { if (log && finalresult) { - try { - logFile.write("Thread no.:" + thNo + "\n"); - logFile.write("parallel:" + parallel + "\n"); - logFile.write(str+"\n\n"); - logFile.flush(); - } - catch (IOException e) { - System.err.println("kein LogFile"); - } + logFile.write("Thread no.:" + thNo + "\n" + "parallel:" + parallel + "\n" + str+"\n\n"); } } void writeStatistics(String str) { if (finalresult) { - try { - statistics.write("Thread No. " + thNo + ": " + str + "\n"); - statistics.flush(); - } - catch (IOException e) { - System.err.println("kein StatisticsFile"); - } + statistics.write("Thread No. " + thNo + ": " + str + "\n"); } }