forked from JavaTX/JavaCompilerCore
modified: ../../src/de/dhbwstuttgart/core/JavaTXCompiler.java
modified: ../../src/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java
This commit is contained in:
parent
60be47c0f1
commit
6ce9f4415e
@ -215,6 +215,118 @@ public class JavaTXCompiler {
|
|||||||
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
|
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<ResultSet> typeInferenceNew() throws ClassNotFoundException {
|
||||||
|
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
|
||||||
|
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
|
||||||
|
for(SourceFile sf : this.sourceFiles.values()) {
|
||||||
|
allClasses.addAll(getAvailableClasses(sf));
|
||||||
|
allClasses.addAll(sf.getClasses());
|
||||||
|
}
|
||||||
|
|
||||||
|
final ConstraintSet<Pair> cons = getConstraints();
|
||||||
|
|
||||||
|
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
|
||||||
|
System.out.println(finiteClosure);
|
||||||
|
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
|
||||||
|
|
||||||
|
TypeUnify unify = new TypeUnify();
|
||||||
|
Set<Set<UnifyPair>> results = new HashSet<>();
|
||||||
|
try {
|
||||||
|
FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/test/logFiles/"+"log"));
|
||||||
|
logFile.write("FC:\\" + finiteClosure.toString()+"\n");
|
||||||
|
for(SourceFile sf : this.sourceFiles.values()) {
|
||||||
|
logFile.write(ASTTypePrinter.print(sf));
|
||||||
|
}
|
||||||
|
logFile.flush();
|
||||||
|
Set<List<Constraint<UnifyPair>>> cardProd = unifyCons.cartesianProduct();
|
||||||
|
for (List<Constraint<UnifyPair>> xCons : cardProd ){
|
||||||
|
Set<UnifyPair> xConsSet = new HashSet<>();
|
||||||
|
for (Constraint<UnifyPair> constraint : xCons) {
|
||||||
|
xConsSet.addAll(constraint);
|
||||||
|
}
|
||||||
|
//.collect(Collectors.toCollection(ArrayList::new))))
|
||||||
|
System.out.println(xConsSet);
|
||||||
|
Set<String> paraTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().map(y -> y.getParameterList().getFormalparalist()
|
||||||
|
.stream().filter(z -> z.getType() instanceof TypePlaceholder)
|
||||||
|
.map(z -> ((TypePlaceholder)z.getType()).getName()).collect(Collectors.toCollection(HashSet::new)))
|
||||||
|
.reduce(new HashSet<String>(), (a,b) -> { a.addAll(b); return a;}, (a,b) -> { a.addAll(b); return a;} ) )
|
||||||
|
.reduce(new HashSet<String>(), (a,b) -> { a.addAll(b); return a;} );
|
||||||
|
|
||||||
|
Set<String> returnTypeVarNames = allClasses.stream().map(x -> x.getMethods().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder)
|
||||||
|
.map(z -> ((TypePlaceholder)z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;} ).get();
|
||||||
|
|
||||||
|
Set<String> fieldTypeVarNames = allClasses.stream().map(x -> x.getFieldDecl().stream().filter(y -> y.getReturnType() instanceof TypePlaceholder)
|
||||||
|
.map(z -> ((TypePlaceholder)z.getReturnType()).getName()).collect(Collectors.toCollection(HashSet::new))).reduce((a,b) -> { a.addAll(b); return a;} ).get();
|
||||||
|
|
||||||
|
returnTypeVarNames.addAll(fieldTypeVarNames);
|
||||||
|
|
||||||
|
unifyCons = unifyCons.map(x -> {
|
||||||
|
//Hier muss ueberlegt werden, ob
|
||||||
|
//1. alle Argument- und Retuntyp-Variablen in allen UnifyPairs
|
||||||
|
// mit disableWildcardtable() werden.
|
||||||
|
//2. alle Typvariablen mit Argument- oder Retuntyp-Variablen
|
||||||
|
//in Beziehung auch auf disableWildcardtable() gesetzt werden muessen
|
||||||
|
//PL 2018-04-23
|
||||||
|
if ((x.getLhsType() instanceof PlaceholderType)) {
|
||||||
|
if (paraTypeVarNames.contains(x.getLhsType().getName())) {
|
||||||
|
((PlaceholderType)x.getLhsType()).setVariance((byte)1);
|
||||||
|
((PlaceholderType)x.getLhsType()).disableWildcardtable();
|
||||||
|
}
|
||||||
|
if (returnTypeVarNames.contains(x.getLhsType().getName())) {
|
||||||
|
((PlaceholderType)x.getLhsType()).setVariance((byte)-1);
|
||||||
|
((PlaceholderType)x.getLhsType()).disableWildcardtable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((x.getRhsType() instanceof PlaceholderType)) {
|
||||||
|
if (paraTypeVarNames.contains(x.getRhsType().getName())) {
|
||||||
|
((PlaceholderType)x.getRhsType()).setVariance((byte)1);
|
||||||
|
((PlaceholderType)x.getRhsType()).disableWildcardtable();
|
||||||
|
}
|
||||||
|
if (returnTypeVarNames.contains(x.getRhsType().getName())) {
|
||||||
|
((PlaceholderType)x.getRhsType()).setVariance((byte)-1);
|
||||||
|
((PlaceholderType)x.getRhsType()).disableWildcardtable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return x;//HIER DIE JEWEILS RECHT BZW. LINKE SEITE AUF GLEICHE VARIANZ SETZEN WIE DIE JEWEILS ANDERE SEITE
|
||||||
|
}).map( y -> {
|
||||||
|
if ((y.getLhsType() instanceof PlaceholderType) && (y.getRhsType() instanceof PlaceholderType)) {
|
||||||
|
if (((PlaceholderType)y.getLhsType()).getVariance() != 0 && ((PlaceholderType)y.getRhsType()).getVariance() == 0) {
|
||||||
|
((PlaceholderType)y.getRhsType()).setVariance(((PlaceholderType)y.getLhsType()).getVariance());
|
||||||
|
}
|
||||||
|
if (((PlaceholderType)y.getLhsType()).getVariance() == 0 && ((PlaceholderType)y.getRhsType()).getVariance() != 0) {
|
||||||
|
((PlaceholderType)y.getLhsType()).setVariance(((PlaceholderType)y.getRhsType()).getVariance());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return y; } );
|
||||||
|
varianceInheritanceConstrainSet(unifyCons);
|
||||||
|
Set<Set<UnifyPair>> result = unify.unifySequential(xConsSet, finiteClosure, logFile, log);
|
||||||
|
//Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
|
||||||
|
System.out.println("RESULT: " + result);
|
||||||
|
logFile.write("RES: " + result.toString()+"\n");
|
||||||
|
logFile.flush();
|
||||||
|
results.addAll(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
results = results.stream().map(x -> {
|
||||||
|
Optional<Set<UnifyPair>> 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(), finiteClosure);
|
||||||
|
}
|
||||||
|
else return x; //wenn nichts veraendert wurde wird x zurueckgegeben
|
||||||
|
}).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
System.out.println("RESULT Final: " + results);
|
||||||
|
logFile.write("RES_FINAL: " + results.toString()+"\n");
|
||||||
|
logFile.flush();
|
||||||
|
}
|
||||||
|
catch (IOException e) { }
|
||||||
|
return results.stream().map((unifyPairs ->
|
||||||
|
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vererbt alle Variancen
|
* Vererbt alle Variancen
|
||||||
* @param eq The set of constraints
|
* @param eq The set of constraints
|
||||||
@ -245,6 +357,49 @@ public class JavaTXCompiler {
|
|||||||
phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x)));
|
phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vererbt alle Variancen
|
||||||
|
* @param eq The set of constraints
|
||||||
|
*/
|
||||||
|
private void varianceInheritanceConstrainSet(ConstraintSet<UnifyPair> cons) {
|
||||||
|
Set<UnifyPair> eq = cons.getAll();
|
||||||
|
Set<PlaceholderType> usedTPH = new HashSet<>();
|
||||||
|
Set<PlaceholderType> phSet = eq.stream().map(x -> {
|
||||||
|
Set<PlaceholderType> pair = new HashSet<>();
|
||||||
|
if (x.getLhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getLhsType());
|
||||||
|
if (x.getRhsType() instanceof PlaceholderType) pair.add((PlaceholderType)x.getRhsType());
|
||||||
|
return pair;
|
||||||
|
}).reduce(new HashSet<>(), (a,b) -> { a.addAll(b); return a;} , (c,d) -> { c.addAll(d); return c;});
|
||||||
|
|
||||||
|
ArrayList<PlaceholderType> phSetVariance = new ArrayList<>(phSet);
|
||||||
|
phSetVariance.removeIf(x -> (x.getVariance() == 0));
|
||||||
|
while(!phSetVariance.isEmpty()) {
|
||||||
|
PlaceholderType a = phSetVariance.remove(0);
|
||||||
|
usedTPH.add(a);
|
||||||
|
//HashMap<PlaceholderType,Integer> ht = new HashMap<>();
|
||||||
|
//ht.put(a, a.getVariance());
|
||||||
|
//ConstraintSet<UnifyPair> eq1 = cons;
|
||||||
|
//eq1.removeIf(x -> !(x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a)));
|
||||||
|
//durch if-Abfrage im foreach geloest
|
||||||
|
cons.forEach(x -> {
|
||||||
|
if (x.getLhsType() instanceof PlaceholderType && ((PlaceholderType)x.getLhsType()).equals(a)) {
|
||||||
|
x.getRhsType().accept(new distributeVariance(), a.getVariance());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//` eq1 = new HashSet<>(eq);
|
||||||
|
//eq1.removeIf(x -> !(x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a)));
|
||||||
|
//durch if-Abfrage im foreach geloest
|
||||||
|
cons.forEach(x -> {
|
||||||
|
if (x.getRhsType() instanceof PlaceholderType && ((PlaceholderType)x.getRhsType()).equals(a)) {
|
||||||
|
x.getLhsType().accept(new distributeVariance(), a.getVariance());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//phSetVariance = new ArrayList<>(phSet); macht vermutlich keinen Sinn PL 2018-10-18
|
||||||
|
//phSetVariance.removeIf(x -> (x.getVariance() == 0 || usedTPH.contains(x)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints) {
|
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints) {
|
||||||
HashMap<String, TypePlaceholder> ret = new HashMap<>();
|
HashMap<String, TypePlaceholder> ret = new HashMap<>();
|
||||||
|
@ -5,6 +5,7 @@ import de.dhbwstuttgart.typeinference.unify.GuavaSetOperations;
|
|||||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -52,4 +53,21 @@ public class ConstraintSet<A> {
|
|||||||
ret.oderConstraints = newOder;
|
ret.oderConstraints = newOder;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void forEach (Consumer<? super A> c) {
|
||||||
|
undConstraints.stream().forEach(c);
|
||||||
|
for(Set<Constraint<A>> oderConstraint : oderConstraints){
|
||||||
|
oderConstraint.parallelStream().forEach((Constraint<A> as) ->
|
||||||
|
as.stream().forEach(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<A> getAll () {
|
||||||
|
Set<A> ret = new HashSet<>();
|
||||||
|
ret.addAll(undConstraints);
|
||||||
|
for(Set<Constraint<A>> oderConstraint : oderConstraints){
|
||||||
|
oderConstraint.parallelStream().forEach((Constraint<A> as) -> ret.addAll(as));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user