Compare commits

..

5 Commits

Author SHA1 Message Date
NoName11234
8fb7d3e7b5 removed ThreadCounter from TypeUnifyTask 2024-03-03 17:17:25 +01:00
NoName11234
e4af54a2bf added unifyTest 2024-02-11 14:38:15 +01:00
NoName11234
d7b693204e changed java version to 21 2024-02-11 14:37:59 +01:00
NoName11234
697cfcc2af changed Writer to NullWriter 2024-02-11 14:37:05 +01:00
NoName11234
73f996991e added unifyTest 2024-02-11 14:35:05 +01:00
3 changed files with 57 additions and 56 deletions

View File

@@ -13,6 +13,7 @@ import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask; import java.util.concurrent.RecursiveTask;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
@@ -56,6 +57,7 @@ import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import org.apache.commons.io.output.NullWriter;
/** /**
@@ -78,11 +80,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
* Fuer die Threads * Fuer die Threads
*/ */
UnifyResultModel urm; UnifyResultModel urm;
protected static int noOfThread = 0;
private static int totalnoOfThread = 0; private static int totalnoOfThread = 0;
int thNo; int thNo;
protected boolean one = false; protected boolean one = false;
Integer MaxNoOfThreads = 128;
public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/"; public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/";
Writer logFile; Writer logFile;
@@ -162,6 +162,9 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
} }
public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) { public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Constraint<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm, UnifyTaskModel usedTasks) {
synchronized (this) { synchronized (this) {
if(statistics==null){
statistics = new NullWriter();
}
this.eq = eq; 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)); //this.oderConstraints = oderConstraints.stream().map(x -> x.stream().map(y -> new HashSet<>(y)).collect(Collectors.toSet(HashSet::new))).collect(Collectors.toList(ArrayList::new));
this.oderConstraintsField = oderConstraints; /*.stream().map(x -> { this.oderConstraintsField = oderConstraints; /*.stream().map(x -> {
@@ -181,7 +184,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
this.logFile = logFile; this.logFile = logFile;
this.log = log; this.log = log;
noOfThread++;
totalnoOfThread++; totalnoOfThread++;
//writeLog("thNo1 " + thNo); //writeLog("thNo1 " + thNo);
thNo = totalnoOfThread; thNo = totalnoOfThread;
@@ -270,7 +272,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
.filter(x -> x.size()>1) .filter(x -> x.size()>1)
.collect(Collectors.toCollection(ArrayList::new)); .collect(Collectors.toCollection(ArrayList::new));
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, new HashSet<>()); Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, new HashSet<>());
noOfThread--;
try { try {
logFile.close(); logFile.close();
} }
@@ -925,7 +926,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/* Wenn parallel gearbeitet wird, wird je nach Varianz ein neuer Thread /* Wenn parallel gearbeitet wird, wird je nach Varianz ein neuer Thread
* gestartet, der parallel weiterarbeitet. * gestartet, der parallel weiterarbeitet.
*/ */
if(parallel && (variance == 1) && noOfThread <= MaxNoOfThreads) { if(parallel && (variance == 1)) {
Set<TypeUnify2Task> forks = new HashSet<>(); Set<TypeUnify2Task> forks = new HashSet<>();
Set<UnifyPair> newEqOrig = new HashSet<>(eq); Set<UnifyPair> newEqOrig = new HashSet<>(eq);
Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems); Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems);
@@ -985,7 +986,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/* FORK ANFANG */ /* FORK ANFANG */
synchronized (this) { synchronized (this) {
writeLog("wait "+ forkOrig.thNo); writeLog("wait "+ forkOrig.thNo);
noOfThread--;
res = forkOrig.join(); res = forkOrig.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -1005,7 +1005,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
forks.forEach(x -> writeLog("wait: " + x.thNo)); forks.forEach(x -> writeLog("wait: " + x.thNo));
for(TypeUnify2Task fork : forks) { for(TypeUnify2Task fork : forks) {
synchronized (this) { synchronized (this) {
noOfThread--;
Set<Set<UnifyPair>> fork_res = fork.join(); Set<Set<UnifyPair>> fork_res = fork.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -1027,7 +1026,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
} }
//noOfThread++; //noOfThread++;
} else { } else {
if(parallel && (variance == -1) && noOfThread <= MaxNoOfThreads) { if(parallel && (variance == -1)) {
Set<TypeUnify2Task> forks = new HashSet<>(); Set<TypeUnify2Task> forks = new HashSet<>();
Set<UnifyPair> newEqOrig = new HashSet<>(eq); Set<UnifyPair> newEqOrig = new HashSet<>(eq);
Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems); Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems);
@@ -1086,7 +1085,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/* FORK ANFANG */ /* FORK ANFANG */
synchronized (this) { synchronized (this) {
writeLog("wait "+ forkOrig.thNo); writeLog("wait "+ forkOrig.thNo);
noOfThread--;
res = forkOrig.join(); res = forkOrig.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -1106,7 +1104,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
forks.forEach(x -> writeLog("wait: " + x.thNo)); forks.forEach(x -> writeLog("wait: " + x.thNo));
for(TypeUnify2Task fork : forks) { for(TypeUnify2Task fork : forks) {
synchronized (this) { synchronized (this) {
noOfThread--;
Set<Set<UnifyPair>> fork_res = fork.join(); Set<Set<UnifyPair>> fork_res = fork.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -1128,7 +1125,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
} }
//noOfThread++; //noOfThread++;
} else { } else {
if(parallel && (variance == 2) && noOfThread <= MaxNoOfThreads) { if(parallel && (variance == 2)) {
writeLog("var2einstieg"); writeLog("var2einstieg");
Set<TypeUnify2Task> forks = new HashSet<>(); Set<TypeUnify2Task> forks = new HashSet<>();
Set<UnifyPair> newEqOrig = new HashSet<>(eq); Set<UnifyPair> newEqOrig = new HashSet<>(eq);
@@ -1172,7 +1169,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
/* FORK ANFANG */ /* FORK ANFANG */
synchronized (this) { synchronized (this) {
writeLog("wait "+ forkOrig.thNo); writeLog("wait "+ forkOrig.thNo);
noOfThread--;
res = forkOrig.join(); res = forkOrig.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -1191,7 +1187,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
forks.forEach(x -> writeLog("wait: " + x.thNo)); forks.forEach(x -> writeLog("wait: " + x.thNo));
for(TypeUnify2Task fork : forks) { for(TypeUnify2Task fork : forks) {
synchronized (this) { synchronized (this) {
noOfThread--;
Set<Set<UnifyPair>> fork_res = fork.join(); Set<Set<UnifyPair>> fork_res = fork.join();
synchronized (usedTasks) { synchronized (usedTasks) {
if (this.myIsCancelled()) { if (this.myIsCancelled()) {
@@ -2613,7 +2608,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
if (log && finalresult) { if (log && finalresult) {
try { try {
logFile.write("Thread no.:" + thNo + "\n"); logFile.write("Thread no.:" + thNo + "\n");
logFile.write("noOfThread:" + noOfThread + "\n");
logFile.write("parallel:" + parallel + "\n"); logFile.write("parallel:" + parallel + "\n");
logFile.write(str+"\n\n"); logFile.write(str+"\n\n");
logFile.flush(); logFile.flush();

View File

@@ -1,13 +1,8 @@
package typeinference; package typeinference;
import com.google.common.base.Optional;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile; import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsert;
@@ -39,48 +34,58 @@ public class UnifyTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/"; public static final String rootDirectory = System.getProperty("user.dir")+"/resources/javFiles/";
private UnifyPair genPairListOfInteger(String name){
UnifyType type1 = new PlaceholderType(name);
UnifyType type2 = new ReferenceType("List", new TypeParams(new ReferenceType("Integer")));
UnifyPair pair1 = new UnifyPair(type2, type1, PairOperator.SMALLERDOT);
return pair1;
}
private UnifyPair genPairListOfString(String name){
PlaceholderType type1 = new PlaceholderType(name);
UnifyType type2 = new ReferenceType("List", new TypeParams(new ReferenceType("String")));
UnifyPair pair1 = new UnifyPair(type2, type1, PairOperator.SMALLERDOT);
return pair1;
}
@Test @Test
public void unifyTest(){ public void unifyTest(){
UnifyType type1 = new PlaceholderType("a"); UnifyType type1;
UnifyType type2 = new ReferenceType("List", new TypeParams(new PlaceholderType("b"))); UnifyType type2;
UnifyPair pair1 = new UnifyPair(type1, type2, PairOperator.SMALLERDOT);
type1 = new ReferenceType("List", new TypeParams(new PlaceholderType("c")));
type2 = new PlaceholderType("a");
UnifyPair pair2 = new UnifyPair(type1, type2, PairOperator.SMALLERDOT);
type1 = new ReferenceType("String");
type2 = new PlaceholderType("b");
UnifyPair pair3 = new UnifyPair(type1, type2, PairOperator.SMALLERDOT);
type1 = new ReferenceType("Integer");
type2 = new PlaceholderType("c");
UnifyPair pair4 = new UnifyPair(type1, type2, PairOperator.SMALLERDOT);
Set<UnifyPair> undConstraints = new HashSet<>(); Set<UnifyPair> undConstraints = new HashSet<>();
undConstraints.add(pair1); undConstraints.add(genPairListOfInteger("a"));
undConstraints.add(pair2); undConstraints.add(genPairListOfString("a"));
undConstraints.add(pair3);
undConstraints.add(pair4); undConstraints.add(genPairListOfInteger("b"));
undConstraints.add(genPairListOfString("b"));
undConstraints.add(genPairListOfInteger("c"));
undConstraints.add(genPairListOfString("c"));
undConstraints.add(genPairListOfInteger("d"));
undConstraints.add(genPairListOfString("d"));
undConstraints.add(genPairListOfInteger("e"));
undConstraints.add(genPairListOfString("e"));
undConstraints.add(genPairListOfInteger("e1"));
undConstraints.add(genPairListOfString("e1"));
undConstraints.add(genPairListOfInteger("e2"));
undConstraints.add(genPairListOfString("e2"));
undConstraints.add(genPairListOfInteger("e3"));
undConstraints.add(genPairListOfString("e3"));
List<Set<Constraint<UnifyPair>>> oderConstraints = new ArrayList<>(); List<Set<Constraint<UnifyPair>>> oderConstraints = new ArrayList<>();
Set<UnifyPair> constraints = new HashSet<>(); Set<UnifyPair> constraints = new HashSet<>();
type1 = new ReferenceType("Object"); type1 = new ReferenceType("Object");
type2 = new ReferenceType("String");
constraints.add(new UnifyPair(type1, type2, PairOperator.SMALLER));
type1 = new ReferenceType("Number");
type2 = new ReferenceType("Integer");
constraints.add(new UnifyPair(type1, type2, PairOperator.SMALLER));
type1 = new ReferenceType("Object");
type2 = new ReferenceType("Number");
constraints.add(new UnifyPair(type1, type2, PairOperator.SMALLER));
type1 = new ReferenceType("AbstractList", new TypeParams(new PlaceholderType("X")));
type2 = new ReferenceType("List", new TypeParams(new PlaceholderType("X"))); type2 = new ReferenceType("List", new TypeParams(new PlaceholderType("X")));
constraints.add(new UnifyPair(type1, type2, PairOperator.SMALLER)); constraints.add(new UnifyPair(type2, type1, PairOperator.SMALLER));
type1 = new ReferenceType("Object"); type1 = new ReferenceType("Object");
type2 = new ReferenceType("AbstractList", new TypeParams(new PlaceholderType("X"))); type2 = new ReferenceType("Integer");
constraints.add(new UnifyPair(type1, type2, PairOperator.SMALLER)); constraints.add(new UnifyPair(type2, type1, PairOperator.SMALLER));
type1 = new ReferenceType("Object");
type2 = new ReferenceType("String");
constraints.add(new UnifyPair(type2, type1, PairOperator.SMALLER));
IFiniteClosure finiteClosure = new FiniteClosure(constraints, new NullWriter()); IFiniteClosure finiteClosure = new FiniteClosure(constraints, new NullWriter());
@@ -88,7 +93,9 @@ public class UnifyTest {
ConstraintSet<Pair> cons = new ConstraintSet<>(); ConstraintSet<Pair> cons = new ConstraintSet<>();
UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure); UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure);
UnifyTaskModel tasks = new UnifyTaskModel(); UnifyTaskModel tasks = new UnifyTaskModel();
urm = unifyAlgo.unifyParallel(undConstraints, oderConstraints, finiteClosure, new NullWriter(), false, urm, tasks); Set<Set<UnifyPair>> solution = unifyAlgo.unify(undConstraints, oderConstraints, finiteClosure, new NullWriter(), false, urm, tasks);
System.out.println(solution.size());
System.out.println(solution);
} }
/* /*
@Test @Test