Compare commits
95 Commits
unify-Part
...
plugin
Author | SHA1 | Date | |
---|---|---|---|
8c80a11675 | |||
789b13aea9 | |||
0f1526daa2 | |||
4ba4292db4 | |||
57b5a51895 | |||
986ce8e6e0 | |||
8a6646bd7c | |||
01a548215a | |||
66cd1e7012 | |||
b3897d2260 | |||
3649f7f767 | |||
d288a0f27d | |||
850fe39426 | |||
212aeaa3a0 | |||
7cb2eed36e | |||
82f330eab5 | |||
9ab98a7e1d | |||
6c83206f3a | |||
f25ba4d13e | |||
4d4be5420b | |||
a01779d6df | |||
44ed1d8e8d | |||
75adbaf579 | |||
411629279d | |||
47783083b0 | |||
94c32609c6 | |||
6175fb3ee3 | |||
2bd6147517 | |||
728b729f08 | |||
27c6351aed | |||
fc14cf9893 | |||
b46e0b855c | |||
88ec8823fd | |||
c77a2b3354 | |||
8a5dbb28ac | |||
86768153c0 | |||
5722f0285b | |||
6c19a9f78c | |||
3e186334a2 | |||
6a6e6b343d | |||
9d93fa63fa | |||
666bf26594 | |||
fcad60996c | |||
29963dfbc3 | |||
82d8ecba74 | |||
9c648546e4 | |||
8e4b39b05e | |||
926d911d21 | |||
42aee3dbec | |||
d010c843df | |||
72705ac868 | |||
87547fdcd6 | |||
362e797b10 | |||
1f12aaec82 | |||
08d69ce6e3 | |||
396efb52de | |||
67469bbb84 | |||
d2581b02ab | |||
28c6f78715 | |||
2eaebbf0f8 | |||
1e5e9f8cd4 | |||
85144cb6d8 | |||
fde462eb16 | |||
9805f956c0 | |||
0a4a625198 | |||
fd72ed340d | |||
d6961b0d0b | |||
d4d668b662 | |||
f7e1a34c5a | |||
21c92d4cab | |||
a373aa7313 | |||
898aedcb4a | |||
f9c0ea8b52 | |||
d9cda2779f | |||
aa662b58fe | |||
2908613499 | |||
a867231348 | |||
757c6e0ec1 | |||
65e0a22477 | |||
f79e4c6df0 | |||
67e35ed8d9 | |||
07c35fef10 | |||
66b6bb7c5d | |||
978f222dfa | |||
8e6b9a9ece | |||
7417a3abe1 | |||
fd8568532a | |||
b416931dee | |||
dd8dbd755e | |||
cb13b0baa9 | |||
03ef170b6e | |||
4e939a6131 | |||
ac49f26df3 | |||
57a82e8863 | |||
89b7d99621 |
doc
pom.xmlsrc
de
dhbwstuttgart
typeinference
unify
main
java
de
dhbwstuttgart
bytecode
core
parser
SyntaxTreeGenerator
syntaxtree
typeinference
test
java
bytecode
FieldTphConsMethTest.javaFieldTphMMethTest.javaInfTest.javaKompTphTest.javaMatrixOpTest.javaMatrixTest.java
simplifyalgo
logFiles
resources
Binary file not shown.
14
pom.xml
14
pom.xml
@ -55,6 +55,14 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<testOutputDirectory>target/test-classes</testOutputDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M3</version>
|
||||
<configuration>
|
||||
<skipTests>true</skipTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
@ -138,10 +146,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-p2-repository-plugin</artifactId>
|
||||
<version>${tycho.version}</version> <executions> <execution> <phase>package</phase>
|
||||
<goals> <goal>archive-repository</goal> </goals> </execution> </executions>
|
||||
</plugin -->
|
||||
</plugins>
|
||||
</build>
|
||||
<pluginRepositories>
|
||||
@ -169,5 +173,3 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
</project>
|
||||
|
||||
|
||||
|
929
src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java
Normal file
929
src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java
Normal file
@ -0,0 +1,929 @@
|
||||
package de.dhbwstuttgart.typeinference.unify;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.RecursiveTask;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations;
|
||||
import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.ExtendsType;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.SuperType;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.TypeParams;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.Unifier;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.OrderingUnifyPair;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.google.common.collect.Ordering;
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of the type unification algorithm
|
||||
* @author Florian Steurer
|
||||
*/
|
||||
public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static int i = 0;
|
||||
private boolean printtag = false;
|
||||
|
||||
public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/";
|
||||
FileWriter logFile;
|
||||
|
||||
/**
|
||||
* The implementation of setOps that will be used during the unification
|
||||
*/
|
||||
protected ISetOperations setOps = new GuavaSetOperations();
|
||||
|
||||
/**
|
||||
* The implementation of the standard unify that will be used during the unification
|
||||
*/
|
||||
protected IUnify stdUnify = new MartelliMontanariUnify();
|
||||
|
||||
/**
|
||||
* The implementation of the rules that will be used during the unification.
|
||||
*/
|
||||
protected IRuleSet rules;
|
||||
|
||||
protected Set<UnifyPair> eq;
|
||||
|
||||
protected IFiniteClosure fc;
|
||||
|
||||
protected Ordering<Set<UnifyPair>> oup;
|
||||
|
||||
protected boolean parallel;
|
||||
|
||||
public TypeUnifyTask() {
|
||||
rules = new RuleSet();
|
||||
}
|
||||
|
||||
public TypeUnifyTask(Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel, FileWriter logFile) {
|
||||
this.eq = eq;
|
||||
this.fc = fc;
|
||||
this.oup = new OrderingUnifyPair(fc);
|
||||
this.parallel = parallel;
|
||||
this.logFile = logFile;
|
||||
rules = new RuleSet(logFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Set<UnifyPair>> compute() {
|
||||
return unify(eq, fc, parallel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes all principal type unifiers for a set of constraints.
|
||||
* @param eq The set of constraints
|
||||
* @param fc The finite closure
|
||||
* @return The set of all principal type unifiers
|
||||
*/
|
||||
protected Set<Set<UnifyPair>> unify(Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
|
||||
/*
|
||||
* Step 1: Repeated application of reduce, adapt, erase, swap
|
||||
*/
|
||||
writeLog("Unifikation: " + eq.toString());
|
||||
eq = eq.stream().map(x -> {x.setVariance((byte)-1); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
Set<UnifyPair> eq0 = applyTypeUnificationRules(eq, fc);
|
||||
|
||||
/*
|
||||
* Step 2 and 3: Create a subset eq1s of pairs where both sides are TPH and eq2s of the other pairs
|
||||
*/
|
||||
Set<UnifyPair> eq1s = new HashSet<>();
|
||||
Set<UnifyPair> eq2s = new HashSet<>();
|
||||
splitEq(eq0, eq1s, eq2s);
|
||||
|
||||
/*
|
||||
* Step 4: Create possible typings
|
||||
*
|
||||
* "Manche Autoren identifizieren die Paare (a, (b,c)) und ((a,b),c)
|
||||
* mit dem geordneten Tripel (a,b,c), wodurch das kartesische Produkt auch assoziativ wird." - Wikipedia
|
||||
*/
|
||||
|
||||
// There are up to 10 toplevel set. 8 of 10 are the result of the
|
||||
// cartesian product of the sets created by pattern matching.
|
||||
List<Set<Set<UnifyPair>>> topLevelSets = new ArrayList<>();
|
||||
|
||||
//System.out.println(eq2s);
|
||||
|
||||
if(eq1s.size() != 0) { // Do not add empty sets or the cartesian product will always be empty.
|
||||
Set<Set<UnifyPair>> wrap = new HashSet<>();
|
||||
wrap.add(eq1s);
|
||||
topLevelSets.add(wrap); // Add Eq1'
|
||||
}
|
||||
|
||||
// Add the set of [a =. Theta | (a=. Theta) in Eq2']
|
||||
Set<UnifyPair> bufferSet = eq2s.stream()
|
||||
.filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
if(bufferSet.size() != 0) { // Do not add empty sets or the cartesian product will always be empty.
|
||||
Set<Set<UnifyPair>> wrap = new HashSet<>();
|
||||
wrap.add(bufferSet);
|
||||
topLevelSets.add(wrap);
|
||||
eq2s.removeAll(bufferSet);
|
||||
}
|
||||
|
||||
// Sets that originate from pair pattern matching
|
||||
// Sets of the "second level"
|
||||
Set<UnifyPair> undefinedPairs = new HashSet<>();
|
||||
if (printtag) System.out.println("eq2s " + eq2s);
|
||||
//writeLog("BufferSet: " + bufferSet.toString()+"\n");
|
||||
Set<Set<Set<Set<UnifyPair>>>> secondLevelSets = calculatePairSets(eq2s, fc, undefinedPairs);
|
||||
//PL 2017-09-20: Im calculatePairSets wird möglicherweise O .< java.lang.Integer
|
||||
//nicht ausgewertet Faculty Beispiel im 1. Schritt
|
||||
//PL 2017-10-03 geloest, muesste noch mit FCs mit kleineren
|
||||
//Typen getestet werden.
|
||||
if (printtag) System.out.println("secondLevelSets:" +secondLevelSets);
|
||||
// If pairs occured that did not match one of the cartesian product cases,
|
||||
// those pairs are contradictory and the unification is impossible.
|
||||
if(!undefinedPairs.isEmpty()) {
|
||||
writeLog("UndefinedPairs; " + undefinedPairs);
|
||||
Set<Set<UnifyPair>> error = new HashSet<>();
|
||||
error.add(undefinedPairs);
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Up to here, no cartesian products are calculated.
|
||||
* filters for pairs and sets can be applied here */
|
||||
|
||||
// Alternative: Sub cartesian products of the second level (pattern matched) sets
|
||||
// "the big (x)"
|
||||
/* for(Set<Set<Set<UnifyPair>>> secondLevelSet : secondLevelSets) {
|
||||
//System.out.println("secondLevelSet "+secondLevelSet.size());
|
||||
List<Set<Set<UnifyPair>>> secondLevelSetList = new ArrayList<>(secondLevelSet);
|
||||
Set<List<Set<UnifyPair>>> cartResult = setOps.cartesianProduct(secondLevelSetList);
|
||||
//System.out.println("CardResult: "+cartResult.size());
|
||||
// Flatten and add to top level sets
|
||||
Set<Set<UnifyPair>> flat = new HashSet<>();
|
||||
int j = 0;
|
||||
for(List<Set<UnifyPair>> s : cartResult) {
|
||||
j++;
|
||||
//System.out.println("s from CardResult: "+cartResult.size() + " " + j);
|
||||
Set<UnifyPair> flat1 = new HashSet<>();
|
||||
for(Set<UnifyPair> s1 : s)
|
||||
flat1.addAll(s1);
|
||||
flat.add(flat1);
|
||||
}
|
||||
//topLevelSets.add(flat);
|
||||
}
|
||||
*/
|
||||
|
||||
//Alternative KEIN KARTESISCHES PRODUKT der secondlevel Ebene bilden
|
||||
for(Set<Set<Set<UnifyPair>>> secondLevelSet : secondLevelSets) {
|
||||
for (Set<Set<UnifyPair>> secondlevelelem : secondLevelSet) {
|
||||
topLevelSets.add(secondlevelelem);
|
||||
}
|
||||
}
|
||||
//System.out.println(topLevelSets);
|
||||
//System.out.println();
|
||||
|
||||
|
||||
//Aufruf von computeCartesianRecursive ANFANG
|
||||
return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, fc, parallel);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
|
||||
//Aufruf von computeCartesianRecursive ENDE
|
||||
|
||||
//keine Ahnung woher das kommt
|
||||
//Set<Set<UnifyPair>> setToFlatten = topLevelSets.stream().map(x -> x.iterator().next()).collect(Collectors.toCollection(HashSet::new));
|
||||
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
|
||||
// Cartesian product over all (up to 10) top level sets
|
||||
//Set<Set<Set<UnifyPair>>> eqPrimeSet = setOps.cartesianProduct(topLevelSets)
|
||||
// .stream().map(x -> new HashSet<>(x))
|
||||
// .collect(Collectors.toCollection(HashSet::new));
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ENDE
|
||||
|
||||
Set<Set<UnifyPair>> eqPrimePrimeSet = new HashSet<>();
|
||||
|
||||
Set<TypeUnifyTask> forks = new HashSet<>();
|
||||
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
|
||||
//for(Set<Set<UnifyPair>> setToFlatten : eqPrimeSet) {
|
||||
// Flatten the cartesian product
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ENDE
|
||||
Set<UnifyPair> eqPrime = new HashSet<>();
|
||||
setToFlatten.stream().forEach(x -> eqPrime.addAll(x));
|
||||
|
||||
/*
|
||||
* Step 5: Substitution
|
||||
*/
|
||||
//System.out.println("vor Subst: " + eqPrime);
|
||||
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime);
|
||||
|
||||
/*
|
||||
* Step 6 a) Restart (fork) for pairs where subst was applied
|
||||
*/
|
||||
if(parallel) {
|
||||
if (eqPrime.equals(eq)) //PL 2017-09-29 auskommentiert und durch
|
||||
//(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt
|
||||
//Begruendung: Wenn in der Substitution keine Veraenderung
|
||||
//(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt.
|
||||
eqPrimePrimeSet.add(eqPrime);
|
||||
else if(eqPrimePrime.isPresent()) {
|
||||
//System.out.println("nextStep: " + eqPrimePrime.get());
|
||||
TypeUnifyTask fork = new TypeUnifyTask(eqPrimePrime.get(), fc, true, logFile);
|
||||
forks.add(fork);
|
||||
fork.fork();
|
||||
}
|
||||
else {
|
||||
//System.out.println("nextStep: " + eqPrime);
|
||||
TypeUnifyTask fork = new TypeUnifyTask(eqPrime, fc, true, logFile);
|
||||
forks.add(fork);
|
||||
fork.fork();
|
||||
}
|
||||
}
|
||||
else { // sequentiell (Step 6b is included)
|
||||
if (printtag) System.out.println("nextStep: " + eqPrimePrime);
|
||||
if (eqPrime.equals(eq)) { //PL 2017-09-29 auskommentiert und durch
|
||||
//(!eqPrimePrime.isPresent()) //PL 2071-09-29 dies ersetzt
|
||||
//Begruendung: Wenn in der Substitution keine Veraenderung
|
||||
//(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt.
|
||||
try {
|
||||
if (isSolvedForm(eqPrime)) {
|
||||
logFile.write(eqPrime.toString()+"\n");
|
||||
logFile.flush();
|
||||
}
|
||||
}
|
||||
catch (IOException e) { }
|
||||
eqPrimePrimeSet.add(eqPrime);
|
||||
}
|
||||
else if(eqPrimePrime.isPresent()) {
|
||||
Set<Set<UnifyPair>> unifyres = unify(eqPrimePrime.get(), fc, false);
|
||||
|
||||
eqPrimePrimeSet.addAll(unifyres);
|
||||
}
|
||||
else {
|
||||
Set<Set<UnifyPair>> unifyres = unify(eqPrime, fc, false);
|
||||
|
||||
|
||||
eqPrimePrimeSet.addAll(unifyres);
|
||||
}
|
||||
}
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ANFANG
|
||||
//}
|
||||
//Muss auskommentiert werden, wenn computeCartesianRecursive ENDE
|
||||
|
||||
/*
|
||||
* Step 6 b) Build the union over everything.
|
||||
*/
|
||||
|
||||
if(parallel)
|
||||
for(TypeUnifyTask fork : forks)
|
||||
eqPrimePrimeSet.addAll(fork.join());
|
||||
|
||||
/*
|
||||
* Step 7: Filter empty sets;
|
||||
*/
|
||||
eqPrimePrimeSet = eqPrimePrimeSet.stream().filter(x -> isSolvedForm(x)).collect(Collectors.toCollection(HashSet::new));
|
||||
if (!eqPrimePrimeSet.isEmpty())
|
||||
writeLog("Result " + eqPrimePrimeSet.toString());
|
||||
return eqPrimePrimeSet;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, IFiniteClosure fc, boolean parallel) {
|
||||
ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
|
||||
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
|
||||
ArrayList<Set<UnifyPair>> nextSetasList = new ArrayList<>(nextSet);
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
int i = 0;
|
||||
byte variance = nextSetasList.iterator().next().iterator().next().getVariance();
|
||||
Set<UnifyPair> a_next = null;
|
||||
if (nextSetasList.iterator().next().iterator().next().getLhsType().getName().equals("D"))
|
||||
System.out.print("");
|
||||
if (nextSetasList.size()>1) {
|
||||
if (variance == 1) {
|
||||
a_next = oup.max(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == -1) {
|
||||
a_next = oup.min(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == 0) {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
while (nextSetasList.size() != 0) {
|
||||
Set<UnifyPair> a = a_next;
|
||||
//writeLog("nextSet: " + nextSetasList.toString()+ "\n");
|
||||
nextSetasList.remove(a);
|
||||
if (nextSetasList.size() > 0) {
|
||||
if (nextSetasList.size()>1) {
|
||||
if (variance == 1) {
|
||||
a_next = oup.max(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == -1) {
|
||||
a_next = oup.min(nextSetasList.iterator());
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
//PL 2018-03-01
|
||||
//TODO: 1. Maximum und Minimum unterscheiden
|
||||
//TODO: 2. compare noch für alle Elmemente die nicht X =. ty sind erweitern
|
||||
//for(Set<UnifyPair> a : newSet) {
|
||||
i++;
|
||||
Set<Set<UnifyPair>> elems = new HashSet<Set<UnifyPair>>(fstElems);
|
||||
elems.add(a);
|
||||
if (remainingSets.isEmpty()) {
|
||||
result.addAll(unify2(elems, eq, fc, parallel));
|
||||
System.out.println("");
|
||||
}
|
||||
else {
|
||||
result.addAll(computeCartesianRecursive(elems, remainingSets, eq, fc, parallel));
|
||||
}
|
||||
if (!result.isEmpty()) {
|
||||
if (variance == 1) {
|
||||
if (a.iterator().next().getLhsType().getName().equals("WL"))
|
||||
System.out.print("");
|
||||
if (a.equals(a_next) ||
|
||||
(oup.compare(a, a_next) == 1)) {
|
||||
System.out.print("");
|
||||
break;
|
||||
}
|
||||
else {
|
||||
System.out.print("");
|
||||
}
|
||||
}
|
||||
else { if (variance == -1) {
|
||||
if (a.iterator().next().getLhsType().getName().equals("A"))
|
||||
System.out.print("");
|
||||
if (a.equals(a_next) || (oup.compare(a, a_next) == -1)) {
|
||||
System.out.print("");
|
||||
break;
|
||||
}
|
||||
else {
|
||||
System.out.print("");
|
||||
}
|
||||
}
|
||||
else if (variance == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected boolean isUndefinedPairSet(Set<Set<UnifyPair>> s) {
|
||||
boolean res = true;
|
||||
if (s.size() ==1) {
|
||||
s.iterator().next().stream().forEach(x -> { res = res && x.isUndefinedPair(); return; });
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Checks whether a set of pairs is in solved form.
|
||||
* @param eqPrimePrime The set of pair
|
||||
* @return True if in solved form, false otherwise.
|
||||
*/
|
||||
protected boolean isSolvedForm(Set<UnifyPair> eqPrimePrime) {
|
||||
for(UnifyPair pair : eqPrimePrime) {
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
|
||||
if(!(lhsType instanceof PlaceholderType))
|
||||
return false;
|
||||
|
||||
// If operator is not equals, both sides must be placeholders
|
||||
if(pair.getPairOp() != PairOperator.EQUALSDOT && !(rhsType instanceof PlaceholderType))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeatedly applies type unification rules to a set of equations.
|
||||
* This is step one of the unification algorithm.
|
||||
* @return The set of pairs that results from repeated application of the inference rules.
|
||||
*/
|
||||
public Set<UnifyPair> applyTypeUnificationRules(Set<UnifyPair> eq, IFiniteClosure fc) {
|
||||
|
||||
/*
|
||||
* Rule Application Strategy:
|
||||
*
|
||||
* 1. Swap all pairs and erase all erasable pairs
|
||||
* 2. Apply all possible rules to a single pair, then move it to the result set.
|
||||
* Iterating over pairs first, then iterating over rules prevents the application
|
||||
* of rules to a "finished" pair over and over.
|
||||
* 2.1 Apply all rules repeatedly except for erase rules. If
|
||||
* the application of a rule creates new pairs, check immediately
|
||||
* against the erase rules.
|
||||
*/
|
||||
|
||||
|
||||
LinkedHashSet<UnifyPair> targetSet = new LinkedHashSet<UnifyPair>();
|
||||
LinkedList<UnifyPair> eqQueue = new LinkedList<>();
|
||||
|
||||
/*
|
||||
* Swap all pairs and erase all erasable pairs
|
||||
*/
|
||||
eq.forEach(x -> swapAddOrErase(x, fc, eqQueue));
|
||||
|
||||
/*
|
||||
* Apply rules until the queue is empty
|
||||
*/
|
||||
while(!eqQueue.isEmpty()) {
|
||||
UnifyPair pair = eqQueue.pollFirst();
|
||||
|
||||
// ReduceUp, ReduceLow, ReduceUpLow
|
||||
Optional<UnifyPair> opt = rules.reduceUpLow(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceLow(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceUp(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceWildcardLow(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceWildcardLowRight(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceWildcardUp(pair);
|
||||
opt = opt.isPresent() ? opt : rules.reduceWildcardUpRight(pair);
|
||||
//PL 2018-03-06 auskommentiert muesste falsch sein vgl. JAVA_BSP/Wildcard6.java
|
||||
//opt = opt.isPresent() ? opt : rules.reduceWildcardLowUp(pair);
|
||||
//opt = opt.isPresent() ? opt : rules.reduceWildcardUpLow(pair);
|
||||
//opt = opt.isPresent() ? opt : rules.reduceWildcardLeft(pair);
|
||||
|
||||
// Reduce TPH
|
||||
opt = opt.isPresent() ? opt : rules.reduceTph(pair);
|
||||
|
||||
// One of the rules has been applied
|
||||
if(opt.isPresent()) {
|
||||
swapAddOrErase(opt.get(), fc, eqQueue);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Reduce1, Reduce2, ReduceExt, ReduceSup, ReduceEq
|
||||
//try {
|
||||
// logFile.write("PAIR1 " + pair + "\n");
|
||||
// logFile.flush();
|
||||
//}
|
||||
//catch (IOException e) { }
|
||||
|
||||
Optional<Set<UnifyPair>> optSet = rules.reduce1(pair, fc);
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduce2(pair);
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceExt(pair, fc);
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceSup(pair, fc);
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceEq(pair);
|
||||
|
||||
// ReduceTphExt, ReduceTphSup
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceTphExt(pair);
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceTphSup(pair);
|
||||
|
||||
|
||||
// FunN Rules
|
||||
optSet = optSet.isPresent() ? optSet : rules.reduceFunN(pair);
|
||||
optSet = optSet.isPresent() ? optSet : rules.greaterFunN(pair);
|
||||
optSet = optSet.isPresent() ? optSet : rules.smallerFunN(pair);
|
||||
|
||||
// One of the rules has been applied
|
||||
if(optSet.isPresent()) {
|
||||
optSet.get().forEach(x -> swapAddOrErase(x, fc, eqQueue));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Adapt, AdaptExt, AdaptSup
|
||||
//try {
|
||||
// logFile.write("PAIR2 " + pair + "\n");
|
||||
// logFile.flush();
|
||||
//}
|
||||
//catch (IOException e) { }
|
||||
opt = rules.adapt(pair, fc);
|
||||
opt = opt.isPresent() ? opt : rules.adaptExt(pair, fc);
|
||||
opt = opt.isPresent() ? opt : rules.adaptSup(pair, fc);
|
||||
|
||||
// One of the rules has been applied
|
||||
if(opt.isPresent()) {
|
||||
swapAddOrErase(opt.get(), fc, eqQueue);
|
||||
continue;
|
||||
}
|
||||
|
||||
// None of the rules has been applied
|
||||
targetSet.add(pair);
|
||||
}
|
||||
|
||||
return targetSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the rule swap to a pair if possible. Then adds the pair to the set if no erase rule applies.
|
||||
* If an erase rule applies, the pair is not added (erased).
|
||||
* @param pair The pair to swap and add or erase.
|
||||
* @param collection The collection to which the pairs are added.
|
||||
*/
|
||||
protected void swapAddOrErase(UnifyPair pair, IFiniteClosure fc, Collection<UnifyPair> collection) {
|
||||
Optional<UnifyPair> opt = rules.swap(pair);
|
||||
UnifyPair pair2 = opt.isPresent() ? opt.get() : pair;
|
||||
|
||||
if(rules.erase1(pair2, fc) || rules.erase3(pair2) || rules.erase2(pair2, fc))
|
||||
return;
|
||||
|
||||
collection.add(pair2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the equation eq into a set eq1s where both terms are type variables,
|
||||
* and a set eq2s where one of both terms is not a type variable.
|
||||
* @param eq Set of pairs to be splitted.
|
||||
* @param eq1s Subset of eq where both terms are type variables.
|
||||
* @param eq2s eq/eq1s.
|
||||
*/
|
||||
protected void splitEq(Set<UnifyPair> eq, Set<UnifyPair> eq1s, Set<UnifyPair> eq2s) {
|
||||
for(UnifyPair pair : eq)
|
||||
if(pair.getLhsType() instanceof PlaceholderType && pair.getRhsType() instanceof PlaceholderType)
|
||||
eq1s.add(pair);
|
||||
else
|
||||
eq2s.add(pair);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates sets of pairs specified in the fourth step. Does not calculate cartesian products.
|
||||
* @param undefined All pairs that did not match one of the 8 cases are added to this set.
|
||||
* @return The set of the eight cases (without empty sets). Each case is a set, containing sets generated
|
||||
* from the pairs that matched the case. Each generated set contains singleton sets or sets with few elements
|
||||
* (as in case 1 where sigma is added to the innermost set).
|
||||
*/
|
||||
protected Set<Set<Set<Set<UnifyPair>>>> calculatePairSets(Set<UnifyPair> eq2s, IFiniteClosure fc, Set<UnifyPair> undefined) {
|
||||
List<Set<Set<Set<UnifyPair>>>> result = new ArrayList<>(8);
|
||||
|
||||
// Init all 8 cases
|
||||
for(int i = 0; i < 8; i++)
|
||||
result.add(new HashSet<>());
|
||||
Boolean first = true;
|
||||
for(UnifyPair pair : eq2s) {
|
||||
PairOperator pairOp = pair.getPairOp();
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
|
||||
// Case 1: (a <. Theta')
|
||||
if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) {
|
||||
//System.out.println(pair);
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
Set<Set<UnifyPair>> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), (byte)1, fc);
|
||||
//System.out.println(x1);
|
||||
result.get(0).add(x1);
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> s1 = new HashSet<>();
|
||||
s1.add(pair);
|
||||
Set<Set<UnifyPair>> s2 = new HashSet<>();
|
||||
s2.add(s1);
|
||||
result.get(0).add(s2);
|
||||
}
|
||||
|
||||
}
|
||||
// Case 2: (a <.? ? ext Theta')
|
||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType)
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), (byte)0, fc));
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> s1 = new HashSet<>();
|
||||
s1.add(pair);
|
||||
Set<Set<UnifyPair>> s2 = new HashSet<>();
|
||||
s2.add(s1);
|
||||
result.get(1).add(s2);
|
||||
}
|
||||
|
||||
// Case 3: (a <.? ? sup Theta')
|
||||
else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof SuperType)
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
result.get(2).add(unifyCase3((PlaceholderType) lhsType, (SuperType) rhsType, (byte)0, fc));
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> s1 = new HashSet<>();
|
||||
s1.add(pair);
|
||||
Set<Set<UnifyPair>> s2 = new HashSet<>();
|
||||
s2.add(s1);
|
||||
result.get(2).add(s2);
|
||||
}
|
||||
|
||||
// Case 4 was replaced by an inference rule
|
||||
// Case 4: (a <.? Theta')
|
||||
//else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType)
|
||||
// result.get(3).add(unifyCase4((PlaceholderType) lhsType, rhsType, fc));
|
||||
|
||||
// Case 5: (Theta <. a)
|
||||
else if(pairOp == PairOperator.SMALLERDOT && rhsType instanceof PlaceholderType)
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
if (rhsType.getName().equals("A"))
|
||||
System.out.println();
|
||||
result.get(4).add(unifyCase5(lhsType, (PlaceholderType) rhsType, (byte)-1, fc));
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> s1 = new HashSet<>();
|
||||
s1.add(pair);
|
||||
Set<Set<UnifyPair>> s2 = new HashSet<>();
|
||||
s2.add(s1);
|
||||
result.get(4).add(s2);
|
||||
}
|
||||
|
||||
// Case 6 was replaced by an inference rule.
|
||||
// Case 6: (? ext Theta <.? a)
|
||||
//else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof ExtendsType && rhsType instanceof PlaceholderType)
|
||||
// result.get(5).add(unifyCase6((ExtendsType) lhsType, (PlaceholderType) rhsType, fc));
|
||||
|
||||
// Case 7 was replaced by an inference rule
|
||||
// Case 7: (? sup Theta <.? a)
|
||||
//else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof SuperType && rhsType instanceof PlaceholderType)
|
||||
// result.get(6).add(unifyCase7((SuperType) lhsType, (PlaceholderType) rhsType, fc));
|
||||
|
||||
// Case 8: (Theta <.? a)
|
||||
else if(pairOp == PairOperator.SMALLERDOTWC && rhsType instanceof PlaceholderType)
|
||||
if (first) { //writeLog(pair.toString()+"\n");
|
||||
result.get(7).add(
|
||||
unifyCase8(lhsType, (PlaceholderType) rhsType, (byte)0, fc));
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> s1 = new HashSet<>();
|
||||
s1.add(pair);
|
||||
Set<Set<UnifyPair>> s2 = new HashSet<>();
|
||||
s2.add(s1);
|
||||
result.get(7).add(s2);
|
||||
}
|
||||
// Case unknown: If a pair fits no other case, then the type unification has failed.
|
||||
// Through application of the rules, every pair should have one of the above forms.
|
||||
// Pairs that do not have one of the aboves form are contradictory.
|
||||
else {
|
||||
// If a pair is not defined, the unificiation will fail, so the loop can be stopped here.
|
||||
undefined.add(pair);
|
||||
break;
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
|
||||
// Filter empty sets or sets that only contain an empty set.
|
||||
return result.stream().map(x -> x.stream().filter(y -> y.size() > 0).collect(Collectors.toCollection(HashSet::new)))
|
||||
.filter(x -> x.size() > 0).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cartesian product Case 1: (a <. Theta')
|
||||
*/
|
||||
protected Set<Set<UnifyPair>> unifyCase1(PlaceholderType a, UnifyType thetaPrime, byte variance, IFiniteClosure fc) {
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
|
||||
boolean allGen = thetaPrime.getTypeParams().size() > 0;
|
||||
for(UnifyType t : thetaPrime.getTypeParams())
|
||||
if(!(t instanceof PlaceholderType) || !((PlaceholderType) t).isGenerated()) {
|
||||
allGen = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Set<UnifyType> cs = fc.getAllTypesByName(thetaPrime.getName());//cs= [java.util.Vector<NP>, java.util.Vector<java.util.Vector<java.lang.Integer>>, ????java.util.Vector<gen_hv>???]
|
||||
|
||||
//PL 18-02-06 entfernt, kommt durch unify wieder rein
|
||||
//cs.add(thetaPrime);
|
||||
//PL 18-02-06 entfernt
|
||||
|
||||
for(UnifyType c : cs) {
|
||||
//PL 18-02-05 getChildren durch smaller ersetzt in getChildren werden die Varianlen nicht ersetzt.
|
||||
Set<UnifyType> thetaQs = fc.smaller(c).stream().collect(Collectors.toCollection(HashSet::new));
|
||||
//Set<UnifyType> thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new));
|
||||
//thetaQs.add(thetaPrime); //PL 18-02-05 wieder geloescht
|
||||
//PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert,
|
||||
//da children offensichtlich ein echtes kleiner und kein kleinergleich ist
|
||||
|
||||
//PL 18-02-06: eingefuegt, thetaQs der Form V<V<...>> <. V'<V<...>> werden entfernt
|
||||
thetaQs = thetaQs.stream().filter(ut -> ut.getTypeParams().arePlaceholders()).collect(Collectors.toCollection(HashSet::new));
|
||||
//PL 18-02-06: eingefuegt
|
||||
|
||||
Set<UnifyType> thetaQPrimes = new HashSet<>();
|
||||
TypeParams cParams = c.getTypeParams();
|
||||
if(cParams.size() == 0)
|
||||
thetaQPrimes.add(c);
|
||||
else {
|
||||
ArrayList<Set<UnifyType>> candidateParams = new ArrayList<>();
|
||||
|
||||
for(UnifyType param : cParams)
|
||||
candidateParams.add(fc.grArg(param));
|
||||
|
||||
for(TypeParams tp : permuteParams(candidateParams))
|
||||
thetaQPrimes.add(c.setTypeParams(tp));
|
||||
}
|
||||
|
||||
for(UnifyType tqp : thetaQPrimes) {
|
||||
//System.out.println(tqp.toString());
|
||||
//i++;
|
||||
//System.out.println(i);
|
||||
//if (i == 62)
|
||||
// System.out.println(tqp.toString());
|
||||
Optional<Unifier> opt = stdUnify.unify(tqp, thetaPrime);
|
||||
if (!opt.isPresent())
|
||||
continue;
|
||||
|
||||
Unifier unifier = opt.get();
|
||||
unifier.swapPlaceholderSubstitutions(thetaPrime.getTypeParams());
|
||||
Set<UnifyPair> substitutionSet = new HashSet<>();
|
||||
for (Entry<PlaceholderType, UnifyType> sigma : unifier)
|
||||
substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT));
|
||||
|
||||
//List<UnifyType> freshTphs = new ArrayList<>(); PL 18-02-06 in die For-Schleife verschoben
|
||||
for (UnifyType tq : thetaQs) {
|
||||
Set<UnifyType> smaller = fc.smaller(unifier.apply(tq));
|
||||
for(UnifyType theta : smaller) {
|
||||
List<UnifyType> freshTphs = new ArrayList<>();
|
||||
Set<UnifyPair> resultPrime = new HashSet<>();
|
||||
|
||||
for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) {
|
||||
if(freshTphs.size()-1 < i)
|
||||
freshTphs.add(PlaceholderType.freshPlaceholder());
|
||||
resultPrime.add(new UnifyPair(freshTphs.get(i), theta.getTypeParams().get(i), PairOperator.SMALLERDOTWC));
|
||||
}
|
||||
|
||||
if(allGen)
|
||||
resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT));
|
||||
else
|
||||
resultPrime.add(new UnifyPair(a, theta.setTypeParams(new TypeParams(freshTphs.toArray(new UnifyType[0]))), PairOperator.EQUALSDOT));
|
||||
resultPrime.addAll(substitutionSet);
|
||||
//writeLog("Substitution: " + substitutionSet.toString());
|
||||
resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
result.add(resultPrime);
|
||||
//writeLog("Result: " + resultPrime.toString());
|
||||
//writeLog("MAX: " + oup.max(resultPrime.iterator()).toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cartesian Product Case 2: (a <.? ? ext Theta')
|
||||
*/
|
||||
private Set<Set<UnifyPair>> unifyCase2(PlaceholderType a, ExtendsType extThetaPrime, byte variance, IFiniteClosure fc) {
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
|
||||
UnifyType aPrime = PlaceholderType.freshPlaceholder();
|
||||
UnifyType extAPrime = new ExtendsType(aPrime);
|
||||
UnifyType thetaPrime = extThetaPrime.getExtendedType();
|
||||
Set<UnifyPair> resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, thetaPrime, PairOperator.SMALLERDOT));
|
||||
result.add(resultPrime);
|
||||
|
||||
resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, extAPrime, PairOperator.EQUALSDOT));
|
||||
resultPrime.add(new UnifyPair(aPrime, thetaPrime, PairOperator.SMALLERDOT));
|
||||
resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
result.add(resultPrime);
|
||||
//writeLog("Result: " + resultPrime.toString());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cartesian Product Case 3: (a <.? ? sup Theta')
|
||||
*/
|
||||
private Set<Set<UnifyPair>> unifyCase3(PlaceholderType a, SuperType subThetaPrime, byte variance, IFiniteClosure fc) {
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
|
||||
UnifyType aPrime = PlaceholderType.freshPlaceholder();
|
||||
UnifyType supAPrime = new SuperType(aPrime);
|
||||
UnifyType thetaPrime = subThetaPrime.getSuperedType();
|
||||
Set<UnifyPair> resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(thetaPrime, a, PairOperator.SMALLERDOT));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
|
||||
resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, supAPrime, PairOperator.EQUALSDOT));
|
||||
resultPrime.add(new UnifyPair(thetaPrime, aPrime, PairOperator.SMALLERDOT));
|
||||
resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cartesian Product Case 5: (Theta <. a)
|
||||
*/
|
||||
private Set<Set<UnifyPair>> unifyCase5(UnifyType theta, PlaceholderType a, byte variance, IFiniteClosure fc) {
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
|
||||
boolean allGen = theta.getTypeParams().size() > 0;
|
||||
for(UnifyType t : theta.getTypeParams())
|
||||
if(!(t instanceof PlaceholderType) || !((PlaceholderType) t).isGenerated()) {
|
||||
allGen = false;
|
||||
break;
|
||||
}
|
||||
|
||||
for(UnifyType thetaS : fc.greater(theta)) {
|
||||
Set<UnifyPair> resultPrime = new HashSet<>();
|
||||
|
||||
UnifyType[] freshTphs = new UnifyType[thetaS.getTypeParams().size()];
|
||||
for(int i = 0; !allGen && i < freshTphs.length; i++) {
|
||||
freshTphs[i] = PlaceholderType.freshPlaceholder();
|
||||
resultPrime.add(new UnifyPair(thetaS.getTypeParams().get(i), freshTphs[i], PairOperator.SMALLERDOTWC));
|
||||
}
|
||||
|
||||
if(allGen)
|
||||
resultPrime.add(new UnifyPair(a, thetaS, PairOperator.EQUALSDOT));
|
||||
else
|
||||
resultPrime.add(new UnifyPair(a, thetaS.setTypeParams(new TypeParams(freshTphs)), PairOperator.EQUALSDOT));
|
||||
resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cartesian Product Case 8: (Theta <.? a)
|
||||
*/
|
||||
private Set<Set<UnifyPair>> unifyCase8(UnifyType theta, PlaceholderType a, byte variance, IFiniteClosure fc) {
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
//for(UnifyType thetaS : fc.grArg(theta)) {
|
||||
Set<UnifyPair> resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, theta, PairOperator.EQUALSDOT));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
|
||||
UnifyType freshTph = PlaceholderType.freshPlaceholder();
|
||||
resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, new ExtendsType(freshTph), PairOperator.EQUALSDOT));
|
||||
resultPrime.add(new UnifyPair(theta, freshTph, PairOperator.SMALLERDOT));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
|
||||
resultPrime = new HashSet<>();
|
||||
resultPrime.add(new UnifyPair(a, new SuperType(freshTph), PairOperator.EQUALSDOT));
|
||||
resultPrime.add(new UnifyPair(freshTph, theta, PairOperator.SMALLERDOT));
|
||||
resultPrime = resultPrime.stream().map(x -> { x.setVariance(variance); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
result.add(resultPrime);
|
||||
//writeLog(resultPrime.toString());
|
||||
//}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a set of candidates for each position and computes all possible permutations.
|
||||
* @param candidates The length of the list determines the number of type params. Each set
|
||||
* contains the candidates for the corresponding position.
|
||||
*/
|
||||
protected Set<TypeParams> permuteParams(ArrayList<Set<UnifyType>> candidates) {
|
||||
Set<TypeParams> result = new HashSet<>();
|
||||
permuteParams(candidates, 0, result, new UnifyType[candidates.size()]);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a set of candidates for each position and computes all possible permutations.
|
||||
* @param candidates The length of the list determines the number of type params. Each set
|
||||
* contains the candidates for the corresponding position.
|
||||
* @param idx Idx for the current permutatiton.
|
||||
* @param result Set of all permutations found so far
|
||||
* @param current The permutation of type params that is currently explored
|
||||
*/
|
||||
private void permuteParams(ArrayList<Set<UnifyType>> candidates, int idx, Set<TypeParams> result, UnifyType[] current) {
|
||||
if(candidates.size() == idx) {
|
||||
result.add(new TypeParams(Arrays.copyOf(current, current.length)));
|
||||
return;
|
||||
}
|
||||
|
||||
Set<UnifyType> localCandidates = candidates.get(idx);
|
||||
|
||||
for(UnifyType t : localCandidates) {
|
||||
current[idx] = t;
|
||||
permuteParams(candidates, idx+1, result, current);
|
||||
}
|
||||
}
|
||||
|
||||
void writeLog(String str) {
|
||||
try {
|
||||
logFile.write(str+"\n");
|
||||
logFile.flush();
|
||||
|
||||
}
|
||||
catch (IOException e) { }
|
||||
}
|
||||
}
|
117
src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java
Normal file
117
src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java
Normal file
@ -0,0 +1,117 @@
|
||||
package de.dhbwstuttgart.typeinference.unify.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* A pair which contains two types and an operator, e.q. (Integer <. a).
|
||||
* @author Florian Steurer
|
||||
*/
|
||||
public class UnifyPair {
|
||||
|
||||
/**
|
||||
* The type on the left hand side of the pair.
|
||||
*/
|
||||
private final UnifyType lhs;
|
||||
|
||||
/**
|
||||
* The type on the right hand side of the pair.
|
||||
*/
|
||||
private final UnifyType rhs;
|
||||
|
||||
/**
|
||||
* The operator that determines the relation between the left and right hand side type.
|
||||
*/
|
||||
private PairOperator pairOp;
|
||||
|
||||
private byte variance = 0;
|
||||
|
||||
private boolean undefinedPair = false;
|
||||
|
||||
private final int hashCode;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the pair.
|
||||
* @param lhs The type on the left hand side of the pair.
|
||||
* @param rhs The type on the right hand side of the pair.
|
||||
* @param op The operator that determines the relation between the left and right hand side type.
|
||||
*/
|
||||
public UnifyPair(UnifyType lhs, UnifyType rhs, PairOperator op) {
|
||||
this.lhs = lhs;
|
||||
this.rhs = rhs;
|
||||
pairOp = op;
|
||||
|
||||
// Caching hashcode
|
||||
hashCode = 17 + 31 * lhs.hashCode() + 31 * rhs.hashCode() + 31 * pairOp.hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type on the left hand side of the pair.
|
||||
*/
|
||||
public UnifyType getLhsType() {
|
||||
return lhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type on the right hand side of the pair.
|
||||
*/
|
||||
public UnifyType getRhsType() {
|
||||
return rhs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the operator that determines the relation between the left and right hand side type.
|
||||
*/
|
||||
public PairOperator getPairOp() {
|
||||
return pairOp;
|
||||
}
|
||||
|
||||
public byte getVariance() {
|
||||
return variance;
|
||||
}
|
||||
|
||||
public void setVariance(byte v) {
|
||||
variance = v;
|
||||
}
|
||||
|
||||
public boolean isUndefinedPair() {
|
||||
return undefinedPair;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(!(obj instanceof UnifyPair))
|
||||
return false;
|
||||
|
||||
if(obj.hashCode() != this.hashCode())
|
||||
return false;
|
||||
|
||||
UnifyPair other = (UnifyPair) obj;
|
||||
|
||||
return other.getPairOp() == pairOp
|
||||
&& other.getLhsType().equals(lhs)
|
||||
&& other.getRhsType().equals(rhs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(" + lhs + " " + pairOp + " " + rhs + ")";
|
||||
}
|
||||
|
||||
/*
|
||||
public List<? extends PlaceholderType> getInvolvedPlaceholderTypes() {
|
||||
ArrayList<PlaceholderType> ret = new ArrayList<>();
|
||||
ret.addAll(lhs.getInvolvedPlaceholderTypes());
|
||||
ret.addAll(rhs.getInvolvedPlaceholderTypes());
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
@ -147,23 +147,7 @@ public class BytecodeGen implements ASTVisitor {
|
||||
if(!tphExtractor.allTPHS.get(t))
|
||||
tphsClass.add(t);
|
||||
}
|
||||
|
||||
ArrayList<TPHConstraint> consClass = new ArrayList<>();
|
||||
for(TPHConstraint cons : tphExtractor.allCons) {
|
||||
TypePlaceholder right = null;
|
||||
for(TypePlaceholder tph : tphsClass) {
|
||||
if(cons.getLeft().equals(tph.getName())) {
|
||||
|
||||
consClass.add(cons);
|
||||
right = getTPH(cons.getRight());
|
||||
}
|
||||
}
|
||||
if(right != null) {
|
||||
tphsClass.add(right);
|
||||
removeFromMethod(right.getName());
|
||||
right = null;
|
||||
}
|
||||
}
|
||||
|
||||
String sig = null;
|
||||
/* if class has generics then creates signature
|
||||
* Signature looks like:
|
||||
@ -172,6 +156,23 @@ public class BytecodeGen implements ASTVisitor {
|
||||
if(classOrInterface.getGenerics().iterator().hasNext() || !commonPairs.isEmpty() ||
|
||||
classOrInterface.getSuperClass().acceptTV(new TypeToSignature()).contains("<")
|
||||
|| !tphsClass.isEmpty()) {
|
||||
HashMap<TPHConstraint, HashSet<String>> constraints = Simplify.simplifyConstraintsClass(tphExtractor,tphsClass);
|
||||
ArrayList<TPHConstraint> consClass = new ArrayList<>();
|
||||
for(TPHConstraint cons : constraints.keySet()) {
|
||||
TypePlaceholder right = null;
|
||||
for(TypePlaceholder tph : tphsClass) {
|
||||
if(cons.getLeft().equals(tph.getName())) {
|
||||
|
||||
consClass.add(cons);
|
||||
right = getTPH(cons.getRight());
|
||||
}
|
||||
}
|
||||
if(right != null) {
|
||||
tphsClass.add(right);
|
||||
removeFromMethod(right.getName());
|
||||
right = null;
|
||||
}
|
||||
}
|
||||
Signature signature = new Signature(classOrInterface, genericsAndBounds,commonPairs,tphsClass, consClass);
|
||||
sig = signature.toString();
|
||||
System.out.println("Signature: => " + sig);
|
||||
|
@ -5,13 +5,19 @@ package de.dhbwstuttgart.bytecode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
|
||||
import de.dhbwstuttgart.syntaxtree.Constructor;
|
||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
import de.dhbwstuttgart.syntaxtree.ParameterList;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
@ -20,36 +26,42 @@ import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
* @author Fayez Abu Alia
|
||||
*
|
||||
*/
|
||||
public class TPHExtractor extends AbstractASTWalker{
|
||||
public class TPHExtractor extends AbstractASTWalker {
|
||||
// Alle TPHs der Felder werden iKopf der Klasse definiert
|
||||
// alle TPHs der Klasse: (TPH, is in Method?)
|
||||
final HashMap<TypePlaceholder,Boolean> allTPHS = new HashMap<>();
|
||||
final HashMap<TypePlaceholder, Boolean> allTPHS = new HashMap<>();
|
||||
MethodAndTPH methodAndTph;
|
||||
|
||||
Boolean inMethod = false;
|
||||
boolean inLocalOrParam = false;
|
||||
|
||||
public final ArrayList<MethodAndTPH> ListOfMethodsAndTph = new ArrayList<>();
|
||||
final ArrayList<GenericInsertPair> allPairs = new ArrayList<>();
|
||||
public final ArrayList<TPHConstraint> allCons = new ArrayList<>();
|
||||
private ResultSet resultSet;
|
||||
|
||||
|
||||
public TPHExtractor() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void setResultSet(ResultSet resultSet) {
|
||||
this.resultSet = resultSet;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(TypePlaceholder tph) {
|
||||
if(resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
|
||||
if (resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
|
||||
TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType;
|
||||
if(inMethod)
|
||||
if (inMethod) {
|
||||
methodAndTph.getTphs().add(resolvedTPH.getName());
|
||||
|
||||
allTPHS.put(resolvedTPH,inMethod);
|
||||
resultSet.resolveType(tph).additionalGenerics.forEach(ag ->{
|
||||
if(ag.contains(resolvedTPH)&&ag.TA1.equals(resolvedTPH)&&!contains(allPairs,ag)) {
|
||||
if(inMethod)
|
||||
if (inLocalOrParam)
|
||||
methodAndTph.getLocalTphs().add(resolvedTPH.getName());
|
||||
}
|
||||
|
||||
allTPHS.put(resolvedTPH, inMethod);
|
||||
resultSet.resolveType(tph).additionalGenerics.forEach(ag -> {
|
||||
if (ag.contains(resolvedTPH) && ag.TA1.equals(resolvedTPH) && !contains(allPairs, ag)) {
|
||||
if (inMethod)
|
||||
methodAndTph.getPairs().add(ag);
|
||||
allPairs.add(ag);
|
||||
TPHConstraint con = new ExtendsConstraint(ag.TA1.getName(), ag.TA2.getName(), Relation.EXTENDS);
|
||||
@ -58,14 +70,16 @@ public class TPHExtractor extends AbstractASTWalker{
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private boolean contains(ArrayList<GenericInsertPair> pairs, GenericInsertPair genPair) {
|
||||
for(int i=0; i<pairs.size();++i) {
|
||||
for (int i = 0; i < pairs.size(); ++i) {
|
||||
GenericInsertPair p = pairs.get(i);
|
||||
if(p.TA1.equals(genPair.TA1) && p.TA2.equals(genPair.TA2))
|
||||
if (p.TA1.equals(genPair.TA1) && p.TA2.equals(genPair.TA2))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Method method) {
|
||||
inMethod = true;
|
||||
@ -74,5 +88,33 @@ public class TPHExtractor extends AbstractASTWalker{
|
||||
inMethod = false;
|
||||
ListOfMethodsAndTph.add(methodAndTph);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(Constructor cons) {
|
||||
inMethod = false;
|
||||
super.visit(cons);
|
||||
inMethod = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LocalVarDecl localVarDecl) {
|
||||
inLocalOrParam = true;
|
||||
super.visit(localVarDecl);
|
||||
inLocalOrParam = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LocalVar localVar) {
|
||||
inLocalOrParam = true;
|
||||
super.visit(localVar);
|
||||
inLocalOrParam = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ParameterList formalParameters) {
|
||||
inLocalOrParam = true;
|
||||
super.visit(formalParameters);
|
||||
inLocalOrParam = false;
|
||||
}
|
||||
|
||||
}
|
@ -391,7 +391,7 @@ public class Signature {
|
||||
}
|
||||
|
||||
for(TPHConstraint cons : consClass) {
|
||||
if(!types.contains(cons.getRight())) {
|
||||
if(!types.contains(cons.getRight()) && !genericsAndBounds.keySet().contains(cons.getRight()+"$")) {
|
||||
String t = cons.getRight()+"$";
|
||||
String bound = Type.getInternalName(Object.class);
|
||||
sw.visitFormalTypeParameter(t);
|
||||
|
@ -0,0 +1,51 @@
|
||||
package de.dhbwstuttgart.bytecode.utilities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
|
||||
public class ConstraintsFinder {
|
||||
private List<TPHConstraint> allConstaints;
|
||||
|
||||
public ConstraintsFinder(List<TPHConstraint> allConstaints) {
|
||||
super();
|
||||
this.allConstaints = allConstaints;
|
||||
}
|
||||
|
||||
public List<List<TPHConstraint>> findConstraints() {
|
||||
List<List<TPHConstraint>> result = new ArrayList<>();
|
||||
|
||||
List<TPHConstraint> visitedCons = new ArrayList<>();
|
||||
for(TPHConstraint c : allConstaints) {
|
||||
if(c.getRel() == Relation.EXTENDS) {
|
||||
// get constraints with the same left side
|
||||
List<TPHConstraint> cons = getConstraints(c,visitedCons);
|
||||
if(cons.size()>1)
|
||||
result.add(cons);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<TPHConstraint> getConstraints(TPHConstraint c, List<TPHConstraint> visitedCons) {
|
||||
List<TPHConstraint> res = new ArrayList<>();
|
||||
for(TPHConstraint cons : allConstaints) {
|
||||
if(!isVisited(cons,visitedCons) && cons.getLeft().equals(c.getLeft())) {
|
||||
res.add(cons);
|
||||
visitedCons.add(cons);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean isVisited(TPHConstraint cons, List<TPHConstraint> visitedCons) {
|
||||
for(TPHConstraint c : visitedCons) {
|
||||
if(c.getLeft().equals(cons.getLeft()) && c.getRight().equals(cons.getRight()))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -10,6 +10,8 @@ public class MethodAndTPH {
|
||||
private String name;
|
||||
private final ArrayList<String> tphs = new ArrayList<>();
|
||||
private final ArrayList<GenericInsertPair> pairs = new ArrayList<>();
|
||||
// tphs of local variables and parameters
|
||||
private final ArrayList<String> localTphs = new ArrayList<>();
|
||||
|
||||
public MethodAndTPH(String name) {
|
||||
this.name = name;
|
||||
@ -26,4 +28,9 @@ public class MethodAndTPH {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ArrayList<String> getLocalTphs() {
|
||||
return localTphs;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package de.dhbwstuttgart.bytecode.utilities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
|
||||
|
||||
public class NameReplacer {
|
||||
private List<TPHConstraint> constraints;
|
||||
private List<TPHConstraint> allConstraints;
|
||||
private List<String> tphs;
|
||||
private List<String> localTphs;
|
||||
|
||||
public NameReplacer(List<TPHConstraint> constraints, List<TPHConstraint> allConstraints,List<String> tphs, ArrayList<String> localTphs) {
|
||||
super();
|
||||
this.constraints = constraints;
|
||||
this.allConstraints = allConstraints;
|
||||
this.tphs = tphs;
|
||||
this.localTphs = localTphs;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> replaceNames() {
|
||||
String newName = NameGenerator.makeNewName();
|
||||
ArrayList<String> names = new ArrayList<>();
|
||||
for(TPHConstraint cons : constraints) {
|
||||
names.add(cons.getRight());
|
||||
cons.setRight(newName);
|
||||
}
|
||||
|
||||
for(TPHConstraint cons : allConstraints) {
|
||||
if(names.contains(cons.getLeft()))
|
||||
cons.setLeft(newName);
|
||||
if(names.contains(cons.getRight()))
|
||||
cons.setRight(newName);
|
||||
}
|
||||
|
||||
tphs.removeAll(names);
|
||||
tphs.add(newName);
|
||||
localTphs.removeAll(names);
|
||||
localTphs.add(newName);
|
||||
|
||||
HashMap<String, List<String>> res = new HashMap<>();
|
||||
res.put(newName, names);
|
||||
return res;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -34,6 +34,7 @@ import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyType;
|
||||
import de.dhbwstuttgart.typeinference.unify.TypeUnifyTask;
|
||||
import de.dhbwstuttgart.typeinference.unify.UnifyResultListener;
|
||||
import de.dhbwstuttgart.typeinference.unify.UnifyResultListenerImpl;
|
||||
import de.dhbwstuttgart.typeinference.unify.UnifyResultModel;
|
||||
|
||||
@ -41,17 +42,20 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.antlr.v4.parse.ANTLRParser.throwsSpec_return;
|
||||
//import org.apache.commons.io.output.NullOutputStream;
|
||||
|
||||
public class JavaTXCompiler {
|
||||
|
||||
final CompilationEnvironment environment;
|
||||
Boolean resultmodel = true;
|
||||
Boolean resultmodel = false;
|
||||
public final Map<File, SourceFile> sourceFiles = new HashMap<>();
|
||||
Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll?
|
||||
|
||||
@ -274,7 +278,7 @@ public class JavaTXCompiler {
|
||||
}
|
||||
*/
|
||||
|
||||
public UnifyResultModel typeInferenceAsync() throws ClassNotFoundException {
|
||||
public UnifyResultModel typeInferenceAsync(UnifyResultListener resultListener, Writer logFile) 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()) {
|
||||
@ -284,12 +288,14 @@ public class JavaTXCompiler {
|
||||
|
||||
final ConstraintSet<Pair> cons = getConstraints();
|
||||
Set<Set<UnifyPair>> results = new HashSet<>();
|
||||
UnifyResultModel urm = new UnifyResultModel();
|
||||
UnifyResultModel urm = null;
|
||||
//urm.addUnifyResultListener(resultListener);
|
||||
try {
|
||||
FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName()));
|
||||
|
||||
logFile = logFile == null ? new FileWriter(new File("log_"+sourceFiles.keySet().iterator().next().getName())) : logFile;
|
||||
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses,logFile);
|
||||
System.out.println(finiteClosure);
|
||||
urm = new UnifyResultModel(cons, finiteClosure);
|
||||
urm.addUnifyResultListener(resultListener);
|
||||
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
|
||||
|
||||
Function<UnifyPair, UnifyPair> distributeInnerVars =
|
||||
@ -401,7 +407,7 @@ public class JavaTXCompiler {
|
||||
}
|
||||
return ret;
|
||||
}).collect(Collectors.toCollection(ArrayList::new));
|
||||
unify.unifyAsync(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, cons, urm);
|
||||
unify.unifyAsync(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm);
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("kein LogFile");
|
||||
@ -421,8 +427,8 @@ public class JavaTXCompiler {
|
||||
final ConstraintSet<Pair> cons = getConstraints();
|
||||
Set<Set<UnifyPair>> results = new HashSet<>();
|
||||
try {
|
||||
FileWriter logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName()));
|
||||
|
||||
Writer logFile = //new OutputStreamWriter(new NullOutputStream());
|
||||
new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"log_"+sourceFiles.keySet().iterator().next().getName()));
|
||||
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses,logFile);
|
||||
System.out.println(finiteClosure);
|
||||
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
|
||||
@ -538,10 +544,10 @@ public class JavaTXCompiler {
|
||||
}).collect(Collectors.toCollection(ArrayList::new));
|
||||
if (resultmodel) {
|
||||
/* UnifyResultModel Anfang */
|
||||
UnifyResultModel urm = new UnifyResultModel();
|
||||
UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure);
|
||||
UnifyResultListenerImpl li = new UnifyResultListenerImpl();
|
||||
urm.addUnifyResultListener(li);
|
||||
unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, cons, urm);
|
||||
unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm);
|
||||
System.out.println("RESULT Final: " + li.getResults());
|
||||
logFile.write("RES_FINAL: " + li.getResults().toString()+"\n");
|
||||
logFile.flush();
|
||||
@ -549,8 +555,8 @@ public class JavaTXCompiler {
|
||||
}
|
||||
/* UnifyResultModel End */
|
||||
else {
|
||||
Set<Set<UnifyPair>> result = unify.unify(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, cons);
|
||||
//Set<Set<UnifyPair>> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, cons);
|
||||
Set<Set<UnifyPair>> result = unify.unify(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure));
|
||||
//Set<Set<UnifyPair>> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure));
|
||||
System.out.println("RESULT: " + result);
|
||||
logFile.write("RES: " + result.toString()+"\n");
|
||||
logFile.flush();
|
||||
|
@ -3,31 +3,37 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||
import de.dhbwstuttgart.parser.antlr.Java8Parser;
|
||||
import de.dhbwstuttgart.parser.antlr.Java8Parser.UnannClassType_lfno_unannClassOrInterfaceTypeContext;
|
||||
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
||||
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
|
||||
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class TypeGenerator {
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
Java8Parser.TypeArgumentsContext arguments;
|
||||
Java8Parser.TypeArgumentsContext arguments=null;
|
||||
/* PL 2019-03-19 auskommentiert ANFANG
|
||||
if(unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() != null){
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
}else{// if(unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType() != null){
|
||||
arguments = unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
}
|
||||
PL 2019-03-19 auskommentiert ENDE */
|
||||
/**
|
||||
* Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten
|
||||
* Beispiel: Typ<String>.InnererTyp<Integer>
|
||||
@ -35,6 +41,28 @@ public class TypeGenerator {
|
||||
String name = unannClassOrInterfaceTypeContext.getText();
|
||||
if(name.contains("<")){
|
||||
name = name.split("<")[0]; //Der Typ ist alles vor den ersten Argumenten
|
||||
/* Fuer Debug-Zwecke
|
||||
unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
//UnannClassType_lfno_unannClassOrInterfaceTypeContext
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0);
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(0).getText();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1);
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(1).getText();
|
||||
unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(0).getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lf_unannClassOrInterfaceType(1).getText();
|
||||
//unannClassOrInterfaceTypeContext.unannInterfaceType_lfno_unannClassOrInterfaceType().getText();
|
||||
*/
|
||||
int lastElement = new ArrayList<>(unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType()).size()-1;
|
||||
if (lastElement >=0) {//qualifizierter Name z.B.: java.util.Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement).typeArguments();
|
||||
}
|
||||
else { //unqualifizierter Name z.B.: Vector
|
||||
arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType().typeArguments();
|
||||
}
|
||||
}
|
||||
return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics);
|
||||
}
|
||||
@ -132,6 +160,18 @@ public class TypeGenerator {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.WildcardContext wildcardContext, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
if(wildcardContext.wildcardBounds() != null){
|
||||
if(wildcardContext.wildcardBounds().getText().substring(0, 7).equals("extends")){
|
||||
return new ExtendsWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart());
|
||||
}else{
|
||||
return new SuperWildcardType(convert(wildcardContext.wildcardBounds().referenceType(), reg, generics), wildcardContext.getStart());
|
||||
}
|
||||
}else{
|
||||
throw new NotImplementedException(); //Wildcard ohne Bound
|
||||
}
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
|
||||
return convertTypeName(name, null, offset, reg, generics);
|
||||
@ -139,12 +179,17 @@ public class TypeGenerator {
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(
|
||||
String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
|
||||
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein
|
||||
if(!reg.contains(name)){ //Dann könnte es ein generischer Type oder ein FunN$$-Type sein
|
||||
if(generics.contains(name)){
|
||||
return new GenericRefType(name, offset);
|
||||
}else{
|
||||
Pattern p = Pattern.compile("Fun(\\d+)[$][$]");
|
||||
Matcher m = p.matcher(name);
|
||||
if (m.matches()) {//es ist FunN$$-Type
|
||||
return new RefType(new JavaClassName(name), convert(typeArguments, reg, generics), offset);
|
||||
} else {
|
||||
throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset);
|
||||
}
|
||||
}}
|
||||
}
|
||||
if(typeArguments == null){
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
||||
@ -162,7 +207,7 @@ public class TypeGenerator {
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
|
||||
for(Java8Parser.TypeArgumentContext arg : typeArguments.typeArgumentList().typeArgument()){
|
||||
if(arg.wildcard() != null){
|
||||
throw new NotImplementedException();
|
||||
ret.add(convert(arg.wildcard(), reg, generics));
|
||||
}else{
|
||||
ret.add(convert(arg.referenceType(), reg, generics));
|
||||
}
|
||||
|
@ -80,6 +80,9 @@ public abstract class AbstractASTWalker implements ASTVisitor{
|
||||
for(Field f : classOrInterface.getFieldDecl()){
|
||||
f.accept(this);
|
||||
}
|
||||
for(Constructor c : classOrInterface.getConstructors()){
|
||||
c.accept(this);
|
||||
}
|
||||
for(Method m : classOrInterface.getMethods()){
|
||||
m.accept(this);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.dhbwstuttgart.syntaxtree.factory;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -30,7 +31,7 @@ public class UnifyTypeFactory {
|
||||
|
||||
private static ArrayList<PlaceholderType> PLACEHOLDERS = new ArrayList<>();
|
||||
|
||||
public static FiniteClosure generateFC(List<ClassOrInterface> fromClasses, FileWriter logFile) throws ClassNotFoundException {
|
||||
public static FiniteClosure generateFC(List<ClassOrInterface> fromClasses, Writer logFile) throws ClassNotFoundException {
|
||||
/*
|
||||
Die transitive Hülle muss funktionieren.
|
||||
Man darf schreiben List<A> extends AL<A>
|
||||
@ -67,9 +68,6 @@ public class UnifyTypeFactory {
|
||||
public static UnifyType convert(RefTypeOrTPHOrWildcardOrGeneric t, Boolean innerType){
|
||||
if(t instanceof GenericRefType){
|
||||
return UnifyTypeFactory.convert((GenericRefType)t, innerType);
|
||||
}else
|
||||
if(t instanceof FunN){
|
||||
return UnifyTypeFactory.convert((FunN)t, innerType);
|
||||
}else if(t instanceof TypePlaceholder){
|
||||
return UnifyTypeFactory.convert((TypePlaceholder)t, innerType);
|
||||
}else if(t instanceof ExtendsWildcardType){
|
||||
@ -85,13 +83,13 @@ public class UnifyTypeFactory {
|
||||
|
||||
public static UnifyType convert(RefType t, Boolean innerType){
|
||||
//Check if it is a FunN Type:
|
||||
Pattern p = Pattern.compile("Fun(\\d+)");
|
||||
Pattern p = Pattern.compile("Fun(\\d+)[$][$]");
|
||||
Matcher m = p.matcher(t.getName().toString());
|
||||
boolean b = m.matches();
|
||||
if(b){
|
||||
Integer N = Integer.valueOf(m.group(1));
|
||||
if((N + 1) == t.getParaList().size()){
|
||||
return convert(new FunN(t.getParaList()), false);
|
||||
return convertFunN(t.getParaList(), false);
|
||||
}
|
||||
}
|
||||
UnifyType ret;
|
||||
@ -107,11 +105,11 @@ public class UnifyTypeFactory {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static UnifyType convert(FunN t, Boolean innerType){
|
||||
public static UnifyType convertFunN(List<RefTypeOrTPHOrWildcardOrGeneric> paraList, Boolean innerType){
|
||||
UnifyType ret;
|
||||
List<UnifyType> params = new ArrayList<>();
|
||||
if(t.getParaList() != null && t.getParaList().size() > 0){
|
||||
for(RefTypeOrTPHOrWildcardOrGeneric pT : t.getParaList()){
|
||||
if(paraList != null && paraList.size() > 0){
|
||||
for(RefTypeOrTPHOrWildcardOrGeneric pT : paraList){
|
||||
params.add(UnifyTypeFactory.convert(pT, false));
|
||||
}
|
||||
}
|
||||
@ -235,7 +233,7 @@ public class UnifyTypeFactory {
|
||||
}
|
||||
|
||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(FunNType t, Map<String,TypePlaceholder> tphs) {
|
||||
RefType ret = new RefType(new JavaClassName(t.getName()+"$$"), convert(t.getTypeParams(), tphs), new NullToken());
|
||||
RefType ret = new RefType(new JavaClassName(t.getName()), convert(t.getTypeParams(), tphs), new NullToken());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1,41 +0,0 @@
|
||||
package de.dhbwstuttgart.syntaxtree.type;
|
||||
|
||||
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @see Spezifikation "Complete Typeinference in Java 8" von Martin Plümicke
|
||||
* "interface FunN<R,T1, T2, ... ,TN> { R apply(T1 arg1, T2 arg2, ... , TN argN); }"
|
||||
* @author A10023 - Andreas Stadelmeier
|
||||
*
|
||||
* Bemerkung:
|
||||
* FunN ist ein RefType. Der RefType ist nicht mit einem FunNInterface verbunden.
|
||||
*
|
||||
*/
|
||||
public class FunN extends RefType {
|
||||
/**
|
||||
* @author Andreas Stadelmeier, a10023
|
||||
* Benötigt für den Typinferenzalgorithmus für Java 8
|
||||
* Generiert einen RefType auf eine FunN<R,T1,...,TN> - Klasse.
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public FunN(List<RefTypeOrTPHOrWildcardOrGeneric> params) {
|
||||
super(new JavaClassName("Fun"+(params.size()-1)), params, new NullToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Spezieller Konstruktor um eine FunN ohne Returntype zu generieren
|
||||
|
||||
protected FunN(List<? extends Type> list){
|
||||
super("",0);
|
||||
if(list==null)throw new NullPointerException();
|
||||
setT(list);
|
||||
this.name = new JavaClassName("Fun"+list.size());//getName();
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
@ -21,7 +21,7 @@ import java.util.Optional;
|
||||
|
||||
public class FunNClass extends ClassOrInterface {
|
||||
public FunNClass(List<GenericRefType> funNParams) {
|
||||
super(0, new JavaClassName("Fun"+(funNParams.size())), new ArrayList<>(), Optional.empty() /* eingefuegt PL 2018-11-24 */,
|
||||
super(0, new JavaClassName("Fun"+(funNParams.size()-1)), new ArrayList<>(), Optional.empty() /* eingefuegt PL 2018-11-24 */,
|
||||
createMethods(funNParams), new ArrayList<>(), createGenerics(funNParams),
|
||||
ASTFactory.createObjectType(), true, new ArrayList<>(), new NullToken());
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
package de.dhbwstuttgart.typeinference.assumptions;
|
||||
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
||||
import de.dhbwstuttgart.syntaxtree.TypeScope;
|
||||
import de.dhbwstuttgart.syntaxtree.type.FunN;
|
||||
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
@ -63,7 +63,7 @@ public class MethodAssumption extends Assumption{
|
||||
}
|
||||
RefTypeOrTPHOrWildcardOrGeneric receiverType;
|
||||
if(receiver instanceof FunNClass){
|
||||
receiverType = new FunN(params);
|
||||
receiverType = new RefType(new JavaClassName(receiver.getClassName().toString()+"$$"), params, new NullToken()); // new FunN(params);
|
||||
}else{
|
||||
receiverType = new RefType(receiver.getClassName(), params, new NullToken());
|
||||
}
|
||||
|
@ -65,7 +65,9 @@ public class TYPEStmt implements StatementVisitor{
|
||||
//lambdaParams.add(0,tphRetType);
|
||||
constraintsSet.addUndConstraint(
|
||||
new Pair(lambdaExpression.getType(),
|
||||
new FunN(lambdaParams),PairOperator.EQUALSDOT));
|
||||
new RefType(new JavaClassName("Fun"+(lambdaParams.size()-1)+"$$"), lambdaParams, new NullToken()),
|
||||
//new FunN(lambdaParams),
|
||||
PairOperator.EQUALSDOT));
|
||||
constraintsSet.addUndConstraint(
|
||||
new Pair(lambdaExpression.getReturnType(),
|
||||
tphRetType,PairOperator.EQUALSDOT));
|
||||
|
@ -4,10 +4,12 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.dhbwstuttgart.exceptions.DebugException;
|
||||
@ -28,6 +30,7 @@ import de.dhbwstuttgart.typeinference.unify.distributeVariance;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Implementation of the type inference rules.
|
||||
@ -36,13 +39,13 @@ import java.io.IOException;
|
||||
*/
|
||||
public class RuleSet implements IRuleSet{
|
||||
|
||||
FileWriter logFile;
|
||||
Writer logFile;
|
||||
|
||||
public RuleSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
RuleSet(FileWriter logFile) {
|
||||
RuleSet(Writer logFile) {
|
||||
this.logFile = logFile;
|
||||
}
|
||||
|
||||
@ -619,8 +622,12 @@ public class RuleSet implements IRuleSet{
|
||||
return succ ? permutation : new int[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Set<UnifyPair>> subst(Set<UnifyPair> pairs) {
|
||||
return subst(pairs, new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Set<UnifyPair>> subst(Set<UnifyPair> pairs, List<Set<Set<UnifyPair>>> oderConstraints) {
|
||||
HashMap<UnifyType, Integer> typeMap = new HashMap<>();
|
||||
|
||||
Stack<UnifyType> occuringTypes = new Stack<>();
|
||||
@ -660,13 +667,35 @@ public class RuleSet implements IRuleSet{
|
||||
if(lhsType != null
|
||||
//&& !((rhsType = pair.getRhsType()) instanceof PlaceholderType) //PL geloescht am 2017-09-29 Begründung: auch Typvariablen muessen ersetzt werden.
|
||||
&& typeMap.get(lhsType) > 1 // The type occurs in more pairs in the set than just the recent pair.
|
||||
&& !rhsType.getTypeParams().occurs(lhsType)) {
|
||||
&& !rhsType.getTypeParams().occurs(lhsType)
|
||||
&& !((rhsType instanceof WildcardType) && ((WildcardType)rhsType).getWildcardedType().equals(lhsType))) //PL eigefuegt 2018-02-18
|
||||
{
|
||||
Unifier uni = new Unifier(lhsType, rhsType);
|
||||
result = result.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(ArrayList::new));
|
||||
result1 = result1.stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(LinkedList::new));
|
||||
|
||||
Function<? super Set<UnifyPair>,? extends HashSet<UnifyPair>> applyUni = b -> b.stream().map(
|
||||
x -> uni.apply(pair,x)).collect(Collectors.toCollection(HashSet::new));
|
||||
List<Set<Set<UnifyPair>>> oderConstraintsRet = new ArrayList<>();
|
||||
for(Set<Set<UnifyPair>> oc : oderConstraints) {
|
||||
//Set<Set<UnifyPair>> ocRet = new HashSet<>();
|
||||
//for(Set<UnifyPair> cs : oc) {
|
||||
Set<Set<UnifyPair>> csRet = oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new));
|
||||
oderConstraintsRet.add(csRet);
|
||||
//}
|
||||
}
|
||||
oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new)));
|
||||
/*
|
||||
oderConstraints = oderConstraints.stream().map(
|
||||
a -> a.stream().map(applyUni
|
||||
//b -> b.stream().map(
|
||||
// x -> uni.apply(pair,x)).collect(Collectors.toCollection(HashSet::new) )
|
||||
).collect(Collectors.toCollection(HashSet::new))
|
||||
).collect(Collectors.toList(ArrayList::new));
|
||||
}
|
||||
*/
|
||||
applied = true;
|
||||
}
|
||||
|
||||
result.add(pair);
|
||||
}
|
||||
|
||||
@ -945,6 +974,8 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
UnifyType extendedType = ((ExtendsType)lhsType).getExtendedType();
|
||||
|
||||
if (extendedType.equals(rhsType)) return Optional.empty(); //PL 2019-02-18 eingefügt ? extends a <.? a
|
||||
|
||||
boolean isGen = extendedType instanceof PlaceholderType && !((PlaceholderType) extendedType).isGenerated();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
@ -971,6 +1002,8 @@ public class RuleSet implements IRuleSet{
|
||||
|
||||
UnifyType superedType = ((SuperType)lhsType).getSuperedType();
|
||||
|
||||
if (superedType.equals(rhsType)) return Optional.empty(); //PL 2019-02-18 eingefügt ? super a <.? a
|
||||
|
||||
boolean isGen = superedType instanceof PlaceholderType && !((PlaceholderType) superedType).isGenerated();
|
||||
|
||||
Set<UnifyPair> result = new HashSet<>();
|
||||
|
@ -1,6 +1,8 @@
|
||||
package de.dhbwstuttgart.typeinference.unify;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
@ -13,26 +15,73 @@ import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
||||
|
||||
public class TypeUnify {
|
||||
public Set<Set<UnifyPair>> unify(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, ConstraintSet<Pair> cons) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, new UnifyResultModel(), cons);
|
||||
|
||||
/**
|
||||
* unify parallel ohne result modell
|
||||
* @param undConstrains
|
||||
* @param oderConstraints
|
||||
* @param fc
|
||||
* @param logFile
|
||||
* @param log
|
||||
* @param cons
|
||||
* @return
|
||||
*/
|
||||
public Set<Set<UnifyPair>> unify(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
|
||||
ForkJoinPool pool = new ForkJoinPool();
|
||||
pool.invoke(unifyTask);
|
||||
Set<Set<UnifyPair>> res = unifyTask.join();
|
||||
try {
|
||||
logFile.write("\nnoShortendElements: " + unifyTask.noShortendElements + "\n");
|
||||
logFile.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("no log-File");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public UnifyResultModel unifyAsync(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, ConstraintSet<Pair> cons, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, cons);
|
||||
/**
|
||||
* unify asynchron mit Rückgabe UnifyResultModel ohne dass alle results gesammelt sind
|
||||
* @param undConstrains
|
||||
* @param oderConstraints
|
||||
* @param fc
|
||||
* @param logFile
|
||||
* @param log
|
||||
* @param cons
|
||||
* @param ret
|
||||
* @return
|
||||
*/
|
||||
public UnifyResultModel unifyAsync(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
|
||||
ForkJoinPool pool = new ForkJoinPool();
|
||||
pool.invoke(unifyTask);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public UnifyResultModel unifyParallel(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, ConstraintSet<Pair> cons, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret, cons);
|
||||
/**
|
||||
* unify parallel mit Rückgabe UnifyResultModel nachdem alle results gesammelt sind
|
||||
* @param undConstrains
|
||||
* @param oderConstraints
|
||||
* @param fc
|
||||
* @param logFile
|
||||
* @param log
|
||||
* @param cons
|
||||
* @param ret
|
||||
* @return
|
||||
*/
|
||||
public UnifyResultModel unifyParallel(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, true, logFile, log, 0, ret);
|
||||
ForkJoinPool pool = new ForkJoinPool();
|
||||
pool.invoke(unifyTask);
|
||||
Set<Set<UnifyPair>> res = unifyTask.join();
|
||||
try {
|
||||
logFile.write("\nnoShortendElements: " + unifyTask.noShortendElements +"\n");
|
||||
logFile.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("no log-File");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -44,9 +93,26 @@ public class TypeUnify {
|
||||
}
|
||||
*/
|
||||
|
||||
public Set<Set<UnifyPair>> unifyOderConstraints(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, FileWriter logFile, Boolean log, ConstraintSet<Pair> cons) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, new UnifyResultModel(), cons);
|
||||
/**
|
||||
* unify sequentiell mit oderconstraints
|
||||
* @param undConstrains
|
||||
* @param oderConstraints
|
||||
* @param fc
|
||||
* @param logFile
|
||||
* @param log
|
||||
* @param cons
|
||||
* @return
|
||||
*/
|
||||
public Set<Set<UnifyPair>> unifyOderConstraints(Set<UnifyPair> undConstrains, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, Writer logFile, Boolean log, UnifyResultModel ret) {
|
||||
TypeUnifyTask unifyTask = new TypeUnifyTask(undConstrains, oderConstraints, fc, false, logFile, log, 0, ret);
|
||||
Set<Set<UnifyPair>> res = unifyTask.compute();
|
||||
try {
|
||||
logFile.write("\nnoShortendElements: " + unifyTask.noShortendElements +"\n");
|
||||
logFile.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("no log-File");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package de.dhbwstuttgart.typeinference.unify;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -15,8 +17,8 @@ public class TypeUnify2Task extends TypeUnifyTask {
|
||||
|
||||
Set<Set<UnifyPair>> setToFlatten;
|
||||
|
||||
public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, FileWriter logFile, Boolean log, int rekTiefe, UnifyResultModel urm, ConstraintSet<Pair> cons) {
|
||||
super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
public TypeUnify2Task(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, Set<UnifyPair> nextSetElement, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) {
|
||||
super(eq, oderConstraints, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
this.setToFlatten = setToFlatten;
|
||||
this.nextSetElement = nextSetElement;
|
||||
}
|
||||
@ -31,12 +33,23 @@ public class TypeUnify2Task extends TypeUnifyTask {
|
||||
System.out.println("two");
|
||||
}
|
||||
one = true;
|
||||
Set<Set<UnifyPair>> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField);
|
||||
Set<Set<UnifyPair>> res = unify2(setToFlatten, eq, oderConstraintsField, fc, parallel, rekTiefeField, true);
|
||||
/*if (isUndefinedPairSetSet(res)) {
|
||||
return new HashSet<>(); }
|
||||
else
|
||||
*/
|
||||
noOfThread--;
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
public void closeLogFile() {
|
||||
|
||||
try {
|
||||
logFile.close();
|
||||
}
|
||||
catch (IOException ioE) {
|
||||
System.err.println("no log-File" + thNo);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ import de.dhbwstuttgart.typeinference.unify.model.Pair;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
import com.google.common.collect.Ordering;
|
||||
|
||||
@ -68,7 +69,6 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
* Fuer die Threads
|
||||
*/
|
||||
UnifyResultModel urm;
|
||||
ConstraintSet<de.dhbwstuttgart.typeinference.constraints.Pair> cons;
|
||||
protected static int noOfThread = 0;
|
||||
private static int totalnoOfThread = 0;
|
||||
int thNo;
|
||||
@ -76,7 +76,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Integer MaxNoOfThreads = 8;
|
||||
|
||||
public static final String rootDirectory = System.getProperty("user.dir")+"/test/logFiles/";
|
||||
FileWriter logFile;
|
||||
Writer logFile;
|
||||
|
||||
/**
|
||||
* The implementation of setOps that will be used during the unification
|
||||
@ -115,6 +115,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
static int noBacktracking;
|
||||
|
||||
static Integer noShortendElements = 0;
|
||||
|
||||
public TypeUnifyTask() {
|
||||
rules = new RuleSet();
|
||||
}
|
||||
@ -133,7 +135,8 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
}
|
||||
*/
|
||||
|
||||
public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, FileWriter logFile, Boolean log, int rekTiefe, UnifyResultModel urm, ConstraintSet<de.dhbwstuttgart.typeinference.constraints.Pair> cons2) {
|
||||
|
||||
public TypeUnifyTask(Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, Writer logFile, Boolean log, int rekTiefe, UnifyResultModel urm) {
|
||||
synchronized (this) {
|
||||
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));
|
||||
@ -153,16 +156,21 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
this.parallel = parallel;
|
||||
this.logFile = logFile;
|
||||
this.log = log;
|
||||
|
||||
noOfThread++;
|
||||
totalnoOfThread++;
|
||||
//writeLog("thNo1 " + thNo);
|
||||
thNo = totalnoOfThread;
|
||||
writeLog("thNo2 " + thNo);
|
||||
try {
|
||||
this.logFile = new FileWriter(new File(System.getProperty("user.dir")+"/src/test/java/logFiles/"+"Thread_"+thNo));
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("log-File nicht vorhanden");
|
||||
}
|
||||
rules = new RuleSet(logFile);
|
||||
this.rekTiefeField = rekTiefe;
|
||||
this.urm = urm;
|
||||
this.cons = cons2;
|
||||
|
||||
noOfThread++;
|
||||
totalnoOfThread++;
|
||||
writeLog("thNo1 " + thNo);
|
||||
thNo = totalnoOfThread;
|
||||
writeLog("thNo2 " + thNo);
|
||||
}
|
||||
}
|
||||
|
||||
@ -212,8 +220,14 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
ArrayList<Set<Set<UnifyPair>>> remainingOderconstraints = oderConstraintsField.stream()
|
||||
.filter(x -> x.size()>1)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField);
|
||||
Set<Set<UnifyPair>> res = unify(neweq, remainingOderconstraints, fc, parallel, rekTiefeField, true);
|
||||
noOfThread--;
|
||||
try {
|
||||
logFile.close();
|
||||
}
|
||||
catch (IOException ioE) {
|
||||
System.err.println("no log-File");
|
||||
}
|
||||
if (isUndefinedPairSetSet(res)) { return new HashSet<>(); }
|
||||
else return res;
|
||||
}
|
||||
@ -229,253 +243,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
*/
|
||||
|
||||
|
||||
public Set<Set<UnifyPair>> computeCartesianRecursiveOderConstraints(Set<Set<UnifyPair>> fstElems, List<Set<Set<UnifyPair>>> topLevelSets, IFiniteClosure fc, boolean parallel, int rekTiefe) {
|
||||
//ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
|
||||
fstElems.addAll(topLevelSets.stream()
|
||||
.filter(x -> x.size()==1)
|
||||
.map(y -> y.stream().findFirst().get())
|
||||
.collect(Collectors.toCollection(HashSet::new)));
|
||||
ArrayList<Set<Set<UnifyPair>>> remainingSets = topLevelSets.stream()
|
||||
.filter(x -> x.size()>1)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig
|
||||
Set<UnifyPair> eq = new HashSet<>();
|
||||
fstElems.stream().forEach(x -> eq.addAll(x));
|
||||
Set<Set<UnifyPair>> result = unify(eq, new ArrayList<>(), fc, parallel, rekTiefe);
|
||||
return result;
|
||||
}
|
||||
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
|
||||
writeLog("nextSet: " + nextSet.toString());
|
||||
List<Set<UnifyPair>> nextSetasList =new ArrayList<>(nextSet);
|
||||
try {
|
||||
//List<Set<UnifyPair>>
|
||||
//nextSetasList = oup.sortedCopy(nextSet);//new ArrayList<>(nextSet);
|
||||
}
|
||||
catch (java.lang.IllegalArgumentException e) {
|
||||
System.out.print("");
|
||||
}
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
int variance = 0;
|
||||
Optional<Integer> xi = nextSetasList.stream().map(x -> x.stream().filter(y -> y.getLhsType() instanceof PlaceholderType)
|
||||
.filter(z -> ((PlaceholderType)z.getLhsType()).getVariance() != 0)
|
||||
.map(c -> ((PlaceholderType)c.getLhsType()).getVariance())
|
||||
.reduce((a,b)-> {if (a==b) return a; else return 0; }))
|
||||
.filter(d -> d.isPresent())
|
||||
.map(e -> e.get())
|
||||
.findAny();
|
||||
if (xi.isPresent()) {
|
||||
variance = xi.get();
|
||||
}
|
||||
//if (variance == 1 && nextSetasList.size() > 1) {
|
||||
// List<Set<UnifyPair>> al = new ArrayList<>(nextSetasList.size());
|
||||
// for (int ii = 0; ii < nextSetasList.size();ii++) {
|
||||
// al.add(0,nextSetasList.get(ii));
|
||||
// }
|
||||
// nextSetasList = al;
|
||||
//}
|
||||
//Set<UnifyPair> a = nextSetasListIt.next();
|
||||
/*if (nextSetasList.size()>1) {zu loeschen
|
||||
if (nextSetasList.iterator().next().iterator().next().getLhsType().getName().equals("D"))
|
||||
System.out.print("");
|
||||
if (variance == 1) {
|
||||
a_next = oup.max(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == -1) {
|
||||
a_next = oup.min(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == 0) {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
*/
|
||||
if (!nextSetasList.iterator().hasNext())
|
||||
System.out.print("");
|
||||
if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1)
|
||||
System.out.print("");
|
||||
writeLog("nextSetasList: " + nextSetasList.toString());
|
||||
while (nextSetasList.size() > 0) { //(nextSetasList.size() != 0) {
|
||||
Set<UnifyPair> a = null;
|
||||
if (variance == 1) {
|
||||
a = oup.max(nextSetasList.iterator());
|
||||
nextSetasList.remove(a);
|
||||
}
|
||||
else if (variance == -1) {
|
||||
a = oup.min(nextSetasList.iterator());
|
||||
nextSetasList.remove(a);
|
||||
}
|
||||
else if (variance == 0) {
|
||||
a = nextSetasList.remove(0);
|
||||
}
|
||||
//writeLog("nextSet: " + nextSetasList.toString()+ "\n");
|
||||
//nextSetasList.remove(a);
|
||||
/* zu loeschen
|
||||
if (nextSetasList.size() > 0) {
|
||||
if (nextSetasList.size()>1) {
|
||||
if (variance == 1) {
|
||||
a_next = oup.max(nextSetasList.iterator());
|
||||
}
|
||||
else if (variance == -1) {
|
||||
a_next = oup.min(nextSetasList.iterator());
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
else {
|
||||
a_next = nextSetasList.iterator().next();
|
||||
}
|
||||
}
|
||||
*/
|
||||
//PL 2018-03-01
|
||||
//TODO: 1. Maximum und Minimum unterscheiden
|
||||
//TODO: 2. compare noch für alle Elmemente die nicht X =. ty sind erweitern
|
||||
//for(Set<UnifyPair> a : newSet) {
|
||||
i++;
|
||||
Set<Set<UnifyPair>> elems = new HashSet<Set<UnifyPair>>(fstElems);
|
||||
elems.add(a);
|
||||
Set<Set<UnifyPair>> res = new HashSet<>();
|
||||
if (remainingSets.isEmpty()) {
|
||||
noou++;
|
||||
writeLog("Vor unify Aufruf: " + eq.toString());
|
||||
writeLog("No of Unify " + noou);
|
||||
System.out.println(noou);
|
||||
Set<UnifyPair> eq = new HashSet<>();
|
||||
elems.stream().forEach(x -> eq.addAll(x));
|
||||
res = unify(eq, new ArrayList<>(), fc, parallel, rekTiefe);
|
||||
}
|
||||
else {//duerfte gar nicht mehr vorkommen PL 2018-04-03
|
||||
res = computeCartesianRecursiveOderConstraints(elems, remainingSets, fc, parallel, rekTiefe);
|
||||
|
||||
}
|
||||
if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) {
|
||||
//wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen
|
||||
result = res;
|
||||
}
|
||||
else {
|
||||
if ((isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result))
|
||||
|| (!isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result))
|
||||
|| result.isEmpty()) {
|
||||
//alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden
|
||||
result.addAll(res);
|
||||
}
|
||||
//else {
|
||||
//wenn Korrekte Ergebnisse da und Feherfälle dazukommen Fehlerfälle ignorieren
|
||||
// if (isUndefinedPairSetSet(res) && !isUndefinedPairSetSet(result)) {
|
||||
// result = result;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* auskommentiert um alle Max und min Betrachtung auszuschalten ANFANG */
|
||||
if (!result.isEmpty() && !isUndefinedPairSetSet(res)) {
|
||||
if (nextSetasList.iterator().hasNext() && nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("B")).findFirst().isPresent() && nextSetasList.size()>1)
|
||||
System.out.print("");
|
||||
Iterator<Set<UnifyPair>> nextSetasListIt = new ArrayList<Set<UnifyPair>>(nextSetasList).iterator();
|
||||
if (variance == 1) {
|
||||
System.out.println("");
|
||||
while (nextSetasListIt.hasNext()) {
|
||||
Set<UnifyPair> a_next = nextSetasListIt.next();
|
||||
if (a.equals(a_next) ||
|
||||
(oup.compare(a, a_next) == 1)) {
|
||||
nextSetasList.remove(a_next);
|
||||
}
|
||||
else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
else { if (variance == -1) {
|
||||
System.out.println("");
|
||||
while (nextSetasListIt.hasNext()) {
|
||||
Set<UnifyPair> a_next = nextSetasListIt.next();
|
||||
if (a.equals(a_next) ||
|
||||
(oup.compare(a, a_next) == -1)) {
|
||||
nextSetasList.remove(0);
|
||||
}
|
||||
else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (variance == 0) {
|
||||
//break;
|
||||
}}
|
||||
}
|
||||
/* auskommentiert um alle Max und min Betrachtung auszuschalten ENDE */
|
||||
|
||||
/* PL 2018-11-05 wird falsch weil es auf der obersten Ebene ist.
|
||||
if (isUndefinedPairSetSet(res)) {
|
||||
int nofstred= 0;
|
||||
Set<UnifyPair> abhSubst = res.stream()
|
||||
.map(b ->
|
||||
b.stream()
|
||||
.map(x -> x.getAllSubstitutions())
|
||||
.reduce((y,z) -> { y.addAll(z); return y;}).get())
|
||||
.reduce((y,z) -> { y.addAll(z); return y;}).get();
|
||||
Set<UnifyPair> b = a;//effective final a
|
||||
Set<UnifyPair> durchschnitt = abhSubst.stream()
|
||||
.filter(x -> b.contains(x))
|
||||
//.filter(y -> abhSubst.contains(y))
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
//Set<PlaceholderType> vars = durchschnitt.stream().map(x -> (PlaceholderType)x.getLhsType()).collect(Collectors.toCollection(HashSet::new));
|
||||
int len = nextSetasList.size();
|
||||
Set<UnifyPair> undefRes = res.stream().reduce((y,z) -> { y.addAll(z); return y;}).get(); //flatten aller undef results
|
||||
Set<Pair<Set<UnifyPair>, UnifyPair>> reducedUndefResSubstGroundedBasePair = undefRes.stream()
|
||||
.map(x -> { Set<UnifyPair> su = x.getAllSubstitutions(); //alle benutzten Substitutionen
|
||||
su.add(x.getGroundBasePair()); // urspruengliches Paar
|
||||
su.removeAll(durchschnitt); //alle aktuell genänderten Paare entfernen
|
||||
return new Pair<>(su, x.getGroundBasePair());})
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
if (res.size() > 1) {
|
||||
System.out.println();
|
||||
}
|
||||
nextSetasList = nextSetasList.stream().filter(x -> {
|
||||
//Boolean ret = false;
|
||||
//for (PlaceholderType var : vars) {
|
||||
// ret = ret || x.stream().map(b -> b.getLhsType().equals(var)).reduce((c,d) -> c || d).get();
|
||||
//}
|
||||
return (!x.containsAll(durchschnitt));//Was passiert wenn durchschnitt leer ist??
|
||||
})//.filter(y -> couldBecorrect(reducedUndefResSubstGroundedBasePair, y)) //fuer testzwecke auskommentiert um nofstred zu bestimmen PL 2018-10-10
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
nofstred = nextSetasList.size();
|
||||
//NOCH NICHT korrekt PL 2018-10-12
|
||||
//nextSetasList = nextSetasList.stream().filter(y -> couldBecorrect(reducedUndefResSubstGroundedBasePair, y))
|
||||
// .collect(Collectors.toCollection(ArrayList::new));
|
||||
writeLog("res (undef): " + res.toString());
|
||||
writeLog("abhSubst: " + abhSubst.toString());
|
||||
writeLog("a: " + a.toString());
|
||||
writeLog("Durchschnitt: " + durchschnitt.toString());
|
||||
writeLog("nextSet: " + nextSet.toString());
|
||||
writeLog("nextSetasList: " + nextSetasList.toString());
|
||||
writeLog("Number first erased Elements (undef): " + (len - nofstred));
|
||||
writeLog("Number second erased Elements (undef): " + (nofstred- nextSetasList.size()));
|
||||
writeLog("Number erased Elements (undef): " + (len - nextSetasList.size()));
|
||||
noAllErasedElements = noAllErasedElements + (len - nextSetasList.size());
|
||||
writeLog("Number of all erased Elements (undef): " + noAllErasedElements.toString());
|
||||
noBacktracking++;
|
||||
writeLog("Number of Backtracking: " + noBacktracking);
|
||||
System.out.println("");
|
||||
}
|
||||
*/
|
||||
//if (nextSetasList.size() == 0 && isUndefinedPairSetSet(result) && nextSet.size() > 1) {
|
||||
// return result;
|
||||
//}
|
||||
//else {
|
||||
// result.removeIf(y -> isUndefinedPairSet(y));
|
||||
//}
|
||||
//else result.stream().filter(y -> !isUndefinedPairSet(y));
|
||||
|
||||
|
||||
} // End of while (nextSetasList.size() > 0)
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Computes all principal type unifiers for a set of constraints.
|
||||
@ -483,7 +251,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
* @param fc The finite closure
|
||||
* @return The set of all principal type unifiers
|
||||
*/
|
||||
protected Set<Set<UnifyPair>> unify(final Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) {
|
||||
protected Set<Set<UnifyPair>> unify(final Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
|
||||
//Set<UnifyPair> aas = eq.stream().filter(x -> x.getLhsType().getName().equals("AA") //&& x.getPairOp().equals(PairOperator.SMALLERDOT)
|
||||
// ).collect(Collectors.toCollection(HashSet::new));
|
||||
//writeLog(nOfUnify.toString() + " AA: " + aas.toString());
|
||||
@ -518,8 +286,19 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
Set<UnifyPair> eq0;
|
||||
Set<UnifyPair> eq0Prime;
|
||||
Optional<Set<UnifyPair>> eqSubst = Optional.of(eq);
|
||||
do {
|
||||
eq0Prime = eqSubst.get();
|
||||
eq0 = applyTypeUnificationRules(eq0Prime, fc);
|
||||
eqSubst = rules.subst(eq0, oderConstraints);
|
||||
} while (eqSubst.isPresent());
|
||||
|
||||
Set<UnifyPair> eq0 = applyTypeUnificationRules(eq, fc);
|
||||
eq0.forEach(x -> x.disableCondWildcards());
|
||||
|
||||
writeLog(nOfUnify.toString() + " Unifikation nach applyTypeUnificationRules: " + eq.toString());
|
||||
writeLog(nOfUnify.toString() + " Oderconstraints nach applyTypeUnificationRules: " + oderConstraints.toString());
|
||||
|
||||
/*
|
||||
* Step 2 and 3: Create a subset eq1s of pairs where both sides are TPH and eq2s of the other pairs
|
||||
@ -548,6 +327,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
}
|
||||
|
||||
// Add the set of [a =. Theta | (a=. Theta) in Eq2']
|
||||
//TODO: Occurscheck anwenden als Fehler identifizieren
|
||||
Set<UnifyPair> bufferSet = eq2s.stream()
|
||||
.filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType)
|
||||
.collect(Collectors.toSet());
|
||||
@ -583,6 +363,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Set<Set<UnifyPair>> error = new HashSet<>();
|
||||
undefinedPairs = undefinedPairs.stream().map(x -> { x.setUndefinedPair(); return x;}).collect(Collectors.toCollection(HashSet::new));
|
||||
error.add(undefinedPairs);
|
||||
undefinedPairs.forEach(x -> writeLog("AllSubst: " +x.getAllSubstitutions().toString()));
|
||||
return error;
|
||||
}
|
||||
|
||||
@ -623,12 +404,12 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
//Aufruf von computeCartesianRecursive ANFANG
|
||||
//writeLog("topLevelSets: " + topLevelSets.toString());
|
||||
return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, parallel, rekTiefe);
|
||||
return computeCartesianRecursive(new HashSet<>(), new ArrayList<>(topLevelSets), eq, oderConstraintsOutput, fc, parallel, rekTiefe, finalresult);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) {
|
||||
Set<Set<UnifyPair>> unify2(Set<Set<UnifyPair>> setToFlatten, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
|
||||
//Aufruf von computeCartesianRecursive ENDE
|
||||
|
||||
//keine Ahnung woher das kommt
|
||||
@ -656,9 +437,13 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
* Step 5: Substitution
|
||||
*/
|
||||
//writeLog("vor Subst: " + eqPrime);
|
||||
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime);
|
||||
writeLog("vor Subst: " + oderConstraints);
|
||||
String ocString = oderConstraints.toString();
|
||||
List<Set<Set<UnifyPair>>> newOderConstraints = new ArrayList<>(oderConstraints);
|
||||
Optional<Set<UnifyPair>> eqPrimePrime = rules.subst(eqPrime, newOderConstraints);
|
||||
Set<Set<UnifyPair>> unifyres1 = null;
|
||||
Set<Set<UnifyPair>> unifyres2 = null;
|
||||
if (!ocString.equals(newOderConstraints.toString())) writeLog("nach Subst: " + newOderConstraints);
|
||||
//writeLog("nach Subst: " + eqPrimePrime);
|
||||
/*
|
||||
* Step 6 a) Restart (fork) for pairs where subst was applied
|
||||
@ -698,29 +483,20 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
}
|
||||
//}
|
||||
//catch (IOException e) {
|
||||
System.err.println("log-File nicht vorhanden");
|
||||
// System.err.println("log-File nicht vorhanden");
|
||||
//}
|
||||
eqPrimePrimeSet.add(eqPrime);
|
||||
Set<Set<UnifyPair>> eqPrimePrimeSetRet = eqPrimePrimeSet.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(), fc);
|
||||
}
|
||||
else return x; //wenn nichts veraendert wurde wird x zurueckgegeben
|
||||
}).collect(Collectors.toCollection(HashSet::new));
|
||||
urm.notify(eqPrimePrimeSetRet.stream().map((unifyPairs ->
|
||||
new ResultSet(UnifyTypeFactory.convert(unifyPairs, de.dhbwstuttgart.typeinference.constraints.Pair.generateTPHMap(cons))))).collect(Collectors.toList()));
|
||||
if (finalresult) {
|
||||
urm.notify(eqPrimePrimeSet);
|
||||
}
|
||||
}
|
||||
else if(eqPrimePrime.isPresent()) {
|
||||
Set<Set<UnifyPair>> unifyres = unifyres1 = unify(eqPrimePrime.get(), oderConstraints, fc, parallel, rekTiefe);
|
||||
Set<Set<UnifyPair>> unifyres = unifyres1 = unify(eqPrimePrime.get(), newOderConstraints, fc, parallel, rekTiefe, finalresult);
|
||||
|
||||
eqPrimePrimeSet.addAll(unifyres);
|
||||
}
|
||||
else {
|
||||
Set<Set<UnifyPair>> unifyres = unifyres2 = unify(eqPrime, oderConstraints, fc, parallel, rekTiefe);
|
||||
Set<Set<UnifyPair>> unifyres = unifyres2 = unify(eqPrime, newOderConstraints, fc, parallel, rekTiefe, finalresult);
|
||||
|
||||
|
||||
eqPrimePrimeSet.addAll(unifyres);
|
||||
@ -752,7 +528,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
|
||||
|
||||
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe) {
|
||||
Set<Set<UnifyPair>> computeCartesianRecursive(Set<Set<UnifyPair>> fstElems, ArrayList<Set<Set<UnifyPair>>> topLevelSets, Set<UnifyPair> eq, List<Set<Set<UnifyPair>>> oderConstraints, IFiniteClosure fc, boolean parallel, int rekTiefe, Boolean finalresult) {
|
||||
//ArrayList<Set<Set<UnifyPair>>> remainingSets = new ArrayList<>(topLevelSets);
|
||||
fstElems.addAll(topLevelSets.stream()
|
||||
.filter(x -> x.size()==1)
|
||||
@ -762,7 +538,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
.filter(x -> x.size()>1)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
if (remainingSets.isEmpty()) {//Alle Elemente sind 1-elementig
|
||||
Set<Set<UnifyPair>> result = unify2(fstElems, eq, oderConstraints, fc, parallel, rekTiefe);
|
||||
Set<Set<UnifyPair>> result = unify2(fstElems, eq, oderConstraints, fc, parallel, rekTiefe, finalresult);
|
||||
return result;
|
||||
}
|
||||
Set<Set<UnifyPair>> nextSet = remainingSets.remove(0);
|
||||
@ -777,16 +553,35 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
}
|
||||
Set<Set<UnifyPair>> result = new HashSet<>();
|
||||
int variance = 0;
|
||||
Optional<Integer> xi = nextSetasList.stream().map(x -> x.stream().filter(y -> y.getLhsType() instanceof PlaceholderType)
|
||||
.filter(z -> ((PlaceholderType)z.getLhsType()).getVariance() != 0)
|
||||
.map(c -> ((PlaceholderType)c.getLhsType()).getVariance())
|
||||
.reduce((a,b)-> {if (a==b) return a; else return 2; })) //2 kommt insbesondere bei Oder-Constraints vor
|
||||
.filter(d -> d.isPresent())
|
||||
.map(e -> e.get())
|
||||
.findAny();
|
||||
if (xi.isPresent()) {
|
||||
variance = xi.get();
|
||||
}
|
||||
|
||||
ArrayList<UnifyPair> zeroNextElem = new ArrayList<>(nextSetasList.get(0));
|
||||
UnifyPair fstBasePair = zeroNextElem.remove(0).getBasePair();
|
||||
|
||||
if (fstBasePair != null) {
|
||||
Boolean sameBase = true;
|
||||
for (UnifyPair ele : nextSetasList.get(0)) {//check ob a <. ty base oder ob Ueberladung
|
||||
sameBase = sameBase && ele.getBasePair() != null && ele.getBasePair().equals(fstBasePair);
|
||||
}
|
||||
if (sameBase) {
|
||||
Optional<Integer> xi = nextSetasList.stream().map(x -> x.stream().filter(y -> y.getLhsType() instanceof PlaceholderType)
|
||||
.filter(z -> ((PlaceholderType)z.getLhsType()).getVariance() != 0)
|
||||
.map(c -> ((PlaceholderType)c.getLhsType()).getVariance())
|
||||
.reduce((a,b)-> {if (a==b) return a; else return 0; })) //2 kommt insbesondere bei Oder-Constraints vor
|
||||
.filter(d -> d.isPresent())
|
||||
.map(e -> e.get())
|
||||
.findAny();
|
||||
if (xi.isPresent()) {
|
||||
variance = xi.get();
|
||||
}
|
||||
}
|
||||
else {
|
||||
variance = 2;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
variance = 2;
|
||||
}
|
||||
//if (variance == 1 && nextSetasList.size() > 1) {
|
||||
// List<Set<UnifyPair>> al = new ArrayList<>(nextSetasList.size());
|
||||
// for (int ii = 0; ii < nextSetasList.size();ii++) {
|
||||
@ -817,6 +612,42 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
if (nextSetasList.iterator().next().stream().filter(x -> x.getLhsType().getName().equals("D")).findFirst().isPresent() && nextSetasList.size()>1)
|
||||
System.out.print("");
|
||||
writeLog("nextSetasList: " + nextSetasList.toString());
|
||||
Set<UnifyPair> nextSetElem = nextSetasList.get(0);
|
||||
writeLog("BasePair1: " + nextSetElem + " " + nextSetElem.iterator().next().getBasePair());
|
||||
|
||||
/* sameEqSet-Bestimmung: Wenn a = ty \in nextSet dann enthaelt sameEqSet alle Paare a < ty1 oder ty2 < a aus fstElems */
|
||||
Set<UnifyPair> sameEqSet = new HashSet<>();
|
||||
if (variance != 2) {
|
||||
Optional<UnifyPair> optOrigPair = nextSetElem.stream().filter(x -> (
|
||||
//x.getBasePair() != null && ist gegeben wenn variance != 2
|
||||
//x.getBasePair().getPairOp().equals(PairOperator.SMALLERDOT) &&
|
||||
(x.getPairOp().equals(PairOperator.EQUALSDOT)
|
||||
/*
|
||||
(x.getBasePair().getLhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getLhsType()))
|
||||
|| (x.getBasePair().getRhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
*/
|
||||
))).filter(x -> //Sicherstellen, dass bei a = ty a auch wirklich die gesuchte Typvariable ist
|
||||
x.getLhsType().equals(x.getBasePair().getLhsType()) ||
|
||||
x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
).findFirst();
|
||||
writeLog("optOrigPair: " + optOrigPair);
|
||||
if (optOrigPair.isPresent()) {
|
||||
UnifyPair origPair = optOrigPair.get();
|
||||
UnifyType tyVar;
|
||||
if (!((tyVar = origPair.getLhsType()) instanceof PlaceholderType)) {
|
||||
tyVar = origPair.getRhsType();
|
||||
}
|
||||
UnifyType tyVarEF = tyVar;
|
||||
sameEqSet = fstElems.stream().map(xx -> xx.iterator().next())
|
||||
.filter(x -> (((x.getLhsType().equals(tyVarEF) && !(x.getRhsType() instanceof PlaceholderType))
|
||||
|| (x.getRhsType().equals(tyVarEF) && !(x.getLhsType() instanceof PlaceholderType)))))
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
}
|
||||
/* sameEqSet-Bestimmung Ende */
|
||||
|
||||
Set<UnifyPair> a = null;
|
||||
while (nextSetasList.size() > 0) { //(nextSetasList.size() != 0) {
|
||||
Set<UnifyPair> a_last = a;
|
||||
@ -924,6 +755,37 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Set<Set<UnifyPair>> res = new HashSet<>();
|
||||
Set<Set<Set<UnifyPair>>> add_res = new HashSet<>();
|
||||
Set<Set<UnifyPair>> aParDef = new HashSet<>();
|
||||
|
||||
/* PL 2019-03-11 Anfang eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
if (variance != 2 && !sameEqSet.isEmpty()) {
|
||||
Optional<UnifyPair> optAPair = a.stream().filter(x -> (
|
||||
//x.getBasePair() != null && ist gegeben wenn variance != 2
|
||||
//x.getBasePair().getPairOp().equals(PairOperator.SMALLERDOT) &&
|
||||
(x.getPairOp().equals(PairOperator.EQUALSDOT)
|
||||
/*
|
||||
(x.getBasePair().getLhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getLhsType()))
|
||||
|| (x.getBasePair().getRhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
*/
|
||||
))).filter(x -> //Sicherstellen, dass bei a = ty a auch wirklich die gesuchte Typvariable ist
|
||||
x.getLhsType().equals(x.getBasePair().getLhsType()) ||
|
||||
x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
).findFirst();
|
||||
|
||||
if (optAPair.isPresent()) {//basepair ist entweder a <. Ty oder ty <. a
|
||||
UnifyPair aPair = optAPair.get();
|
||||
//writeLog("optOrigPair: " + optOrigPair + " " + "aPair: " + aPair+ " " + "aPair.basePair(): " + aPair.getBasePair());
|
||||
writeLog("variance: " + new Integer(variance).toString() + "sameEqSet:" + sameEqSet);
|
||||
if (!checkA(aPair, sameEqSet, elems, result)) {
|
||||
a = null;
|
||||
noShortendElements++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* PL 2019-03-11 Ende eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
|
||||
if(parallel && (variance == 1) && noOfThread <= MaxNoOfThreads) {
|
||||
Set<TypeUnify2Task> forks = new HashSet<>();
|
||||
Set<UnifyPair> newEqOrig = new HashSet<>(eq);
|
||||
@ -932,7 +794,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
newElemsOrig.add(a);
|
||||
|
||||
/* FORK ANFANG */
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
//forks.add(forkOrig);
|
||||
forkOrig.fork();
|
||||
/* FORK ENDE */
|
||||
@ -946,11 +808,40 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
synchronized (this) { nextSetasList.remove(nSaL);
|
||||
writeLog("1 RM" + nSaL.toString());
|
||||
}
|
||||
|
||||
/* PL 2019-03-13 Anfang eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
Optional<UnifyPair> optAPair = nSaL.stream().filter(x -> (
|
||||
//x.getBasePair() != null && ist gegeben wenn variance != 2
|
||||
//x.getBasePair().getPairOp().equals(PairOperator.SMALLERDOT) &&
|
||||
(x.getPairOp().equals(PairOperator.EQUALSDOT)
|
||||
/*
|
||||
(x.getBasePair().getLhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getLhsType()))
|
||||
|| (x.getBasePair().getRhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
*/
|
||||
))).filter(x -> //Sicherstellen, dass bei a = ty a auch wirklich die gesuchte Typvariable ist
|
||||
x.getLhsType().equals(x.getBasePair().getLhsType()) ||
|
||||
x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
).findFirst();
|
||||
|
||||
if (optAPair.isPresent()) {//basepair ist entweder a <. Ty oder ty <. a
|
||||
UnifyPair aPair = optAPair.get();
|
||||
//writeLog("optOrigPair: " + optOrigPair + " " + "aPair: " + aPair+ " " + "aPair.basePair(): " + aPair.getBasePair());
|
||||
writeLog("variance: " + new Integer(variance).toString() + "sameEqSet:" + sameEqSet);
|
||||
if (!sameEqSet.isEmpty() && !checkA(aPair, sameEqSet, elems, result)) {
|
||||
nSaL = null;
|
||||
noShortendElements++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* PL 2019-03-13 Ende eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
|
||||
Set<UnifyPair> newEq = new HashSet<>(eq);
|
||||
Set<Set<UnifyPair>> newElems = new HashSet<>(elems);
|
||||
List<Set<Set<UnifyPair>>> newOderConstraints = new ArrayList<>(oderConstraints);
|
||||
newElems.add(nSaL);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
forks.add(fork);
|
||||
fork.fork();
|
||||
}
|
||||
@ -958,7 +849,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
/* FORK ANFANG */
|
||||
synchronized (this) {
|
||||
noOfThread--;
|
||||
writeLog("wait "+ forkOrig.thNo);
|
||||
res = forkOrig.join();
|
||||
forkOrig.writeLog("final Orig 1");
|
||||
forkOrig.closeLogFile();
|
||||
//Set<Set<UnifyPair>> fork_res = forkOrig.join();
|
||||
writeLog("JoinOrig " + new Integer(forkOrig.thNo).toString());
|
||||
//noOfThread--; an das Ende von compute verschoben
|
||||
@ -966,6 +861,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
};
|
||||
/* FORK ENDE */
|
||||
|
||||
forks.forEach(x -> writeLog("wait: " + x.thNo));
|
||||
for(TypeUnify2Task fork : forks) {
|
||||
synchronized (this) {
|
||||
Set<Set<UnifyPair>> fork_res = fork.join();
|
||||
@ -977,8 +873,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
if (!isUndefinedPairSetSet(fork_res)) {
|
||||
aParDef.add(fork.getNextSetElement());
|
||||
}
|
||||
fork.writeLog("final 1");
|
||||
fork.closeLogFile();
|
||||
};
|
||||
}
|
||||
}
|
||||
noOfThread++;
|
||||
} else {
|
||||
if(parallel && (variance == -1) && noOfThread <= MaxNoOfThreads) {
|
||||
Set<TypeUnify2Task> forks = new HashSet<>();
|
||||
@ -988,7 +887,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
newElemsOrig.add(a);
|
||||
|
||||
/* FORK ANFANG */
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
//forks.add(forkOrig);
|
||||
forkOrig.fork();
|
||||
/* FORK ENDE */
|
||||
@ -1002,11 +901,40 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
synchronized (this) { nextSetasList.remove(nSaL);
|
||||
writeLog("-1 RM" + nSaL.toString());
|
||||
}
|
||||
|
||||
/* PL 2019-03-13 Anfang eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
Optional<UnifyPair> optAPair = nSaL.stream().filter(x -> (
|
||||
//x.getBasePair() != null && ist gegeben wenn variance != 2
|
||||
//x.getBasePair().getPairOp().equals(PairOperator.SMALLERDOT) &&
|
||||
(x.getPairOp().equals(PairOperator.EQUALSDOT)
|
||||
/*
|
||||
(x.getBasePair().getLhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getLhsType()))
|
||||
|| (x.getBasePair().getRhsType() instanceof PlaceholderType
|
||||
&& x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
*/
|
||||
))).filter(x -> //Sicherstellen, dass bei a = ty a auch wirklich die gesuchte Typvariable ist
|
||||
x.getLhsType().equals(x.getBasePair().getLhsType()) ||
|
||||
x.getLhsType().equals(x.getBasePair().getRhsType())
|
||||
).findFirst();
|
||||
|
||||
if (optAPair.isPresent()) {//basepair ist entweder a <. Ty oder ty <. a
|
||||
UnifyPair aPair = optAPair.get();
|
||||
//writeLog("optOrigPair: " + optOrigPair + " " + "aPair: " + aPair+ " " + "aPair.basePair(): " + aPair.getBasePair());
|
||||
writeLog("variance: " + new Integer(variance).toString() + "sameEqSet:" + sameEqSet);
|
||||
if (!sameEqSet.isEmpty() && !checkA(aPair, sameEqSet, elems, result)) {
|
||||
nSaL = null;
|
||||
noShortendElements++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* PL 2019-03-13 Ende eingefuegt Vergleich mit anderen Paaren ggf. loeschen */
|
||||
|
||||
Set<UnifyPair> newEq = new HashSet<>(eq);
|
||||
Set<Set<UnifyPair>> newElems = new HashSet<>(elems);
|
||||
List<Set<Set<UnifyPair>>> newOderConstraints = new ArrayList<>(oderConstraints);
|
||||
newElems.add(nSaL);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
forks.add(fork);
|
||||
fork.fork();
|
||||
}
|
||||
@ -1014,7 +942,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
/* FORK ANFANG */
|
||||
synchronized (this) {
|
||||
noOfThread--;
|
||||
writeLog("wait "+ forkOrig.thNo);
|
||||
res = forkOrig.join();
|
||||
forkOrig.writeLog("final Orig -1");
|
||||
forkOrig.closeLogFile();
|
||||
//Set<Set<UnifyPair>> fork_res = forkOrig.join();
|
||||
writeLog("JoinOrig " + new Integer(forkOrig.thNo).toString());
|
||||
//noOfThread--; an das Ende von compute verschoben
|
||||
@ -1022,6 +954,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
};
|
||||
/* FORK ENDE */
|
||||
|
||||
forks.forEach(x -> writeLog("wait: " + x.thNo));
|
||||
for(TypeUnify2Task fork : forks) {
|
||||
synchronized (this) {
|
||||
Set<Set<UnifyPair>> fork_res = fork.join();
|
||||
@ -1033,19 +966,22 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
if (!isUndefinedPairSetSet(fork_res)) {
|
||||
aParDef.add(fork.getNextSetElement());
|
||||
}
|
||||
fork.writeLog("final -1");
|
||||
fork.closeLogFile();
|
||||
};
|
||||
}
|
||||
noOfThread++;
|
||||
} else {
|
||||
if(parallel && (variance == 2) && noOfThread <= MaxNoOfThreads) {
|
||||
writeLog("var2einstieg");
|
||||
Set<TypeUnifyTask> forks = new HashSet<>();
|
||||
Set<TypeUnify2Task> forks = new HashSet<>();
|
||||
Set<UnifyPair> newEqOrig = new HashSet<>(eq);
|
||||
Set<Set<UnifyPair>> newElemsOrig = new HashSet<>(elems);
|
||||
List<Set<Set<UnifyPair>>> newOderConstraintsOrig = new ArrayList<>(oderConstraints);
|
||||
newElemsOrig.add(a);
|
||||
|
||||
/* FORK ANFANG */
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task forkOrig = new TypeUnify2Task(newElemsOrig, newEqOrig, newOderConstraintsOrig, a, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
//forks.add(forkOrig);
|
||||
forkOrig.fork();
|
||||
/* FORK ENDE */
|
||||
@ -1061,7 +997,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Set<Set<UnifyPair>> newElems = new HashSet<>(elems);
|
||||
List<Set<Set<UnifyPair>>> newOderConstraints = new ArrayList<>(oderConstraints);
|
||||
newElems.add(nSaL);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm, cons);
|
||||
TypeUnify2Task fork = new TypeUnify2Task(newElems, newEq, newOderConstraints, nSaL, fc, parallel, logFile, log, rekTiefe, urm);
|
||||
forks.add(fork);
|
||||
fork.fork();
|
||||
}
|
||||
@ -1069,26 +1005,33 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
|
||||
/* FORK ANFANG */
|
||||
synchronized (this) {
|
||||
noOfThread--;
|
||||
writeLog("wait "+ forkOrig.thNo);
|
||||
res = forkOrig.join();
|
||||
forkOrig.writeLog("final Orig 2");
|
||||
forkOrig.closeLogFile();
|
||||
//Set<Set<UnifyPair>> fork_res = forkOrig.join();
|
||||
writeLog("JoinOrig " + new Integer(forkOrig.thNo).toString());
|
||||
//noOfThread--; an das Ende von compute verschoben
|
||||
//add_res.add(fork_res); //vermutlich falsch
|
||||
};
|
||||
/* FORK ENDE */
|
||||
|
||||
for(TypeUnifyTask fork : forks) {
|
||||
forks.forEach(x -> writeLog("wait: " + x.thNo));
|
||||
for(TypeUnify2Task fork : forks) {
|
||||
synchronized (this) {
|
||||
Set<Set<UnifyPair>> fork_res = fork.join();
|
||||
writeLog("Join " + new Integer(fork.thNo).toString());
|
||||
//noOfThread--; an das Ende von compute verschoben
|
||||
add_res.add(fork_res);
|
||||
fork.writeLog("final 2");
|
||||
fork.closeLogFile();
|
||||
};
|
||||
}}
|
||||
else {
|
||||
}
|
||||
noOfThread++;
|
||||
} else {
|
||||
//parallel = false; //Wenn MaxNoOfThreads erreicht ist, sequentiell weiterarbeiten
|
||||
elems.add(a); //PL 2019-01-16 muss das wirklich hin steht schon in Zeile 859 ja braucht man siehe Zeile 859
|
||||
res = unify2(elems, eq, oderConstraints, fc, parallel, rekTiefe);
|
||||
res = unify2(elems, eq, oderConstraints, fc, parallel, rekTiefe, finalresult);
|
||||
}}}
|
||||
if (!isUndefinedPairSetSet(res) && isUndefinedPairSetSet(result)) {
|
||||
//wenn korrektes Ergebnis gefunden alle Fehlerfaelle loeschen
|
||||
@ -1124,6 +1067,7 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
//Alle Variablen bestimmen die nicht hinzugefügt wurden in a_last
|
||||
//System.out.println(a_last);
|
||||
a_last.forEach(x -> {writeLog("a_last_elem:" + x + " basepair: " + x.getBasePair());});
|
||||
try {//PL eingefuegt 2019-03-06 da bei map mmer wieder Nullpointer kamen
|
||||
List<PlaceholderType> varsLast_a =
|
||||
a_last.stream().filter(x -> ((x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName())
|
||||
&& (x.getLhsType() instanceof PlaceholderType) && (x.getBasePair().getLhsType() instanceof PlaceholderType))
|
||||
@ -1170,10 +1114,14 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
writeLog("RES var=1 ADD:" + result.toString() + " " + res.toString());
|
||||
result.addAll(res);
|
||||
}}}
|
||||
}
|
||||
catch (NullPointerException e) {
|
||||
writeLog("NullPointerException: " + a_last.toString());
|
||||
}
|
||||
}
|
||||
else {
|
||||
//alle Fehlerfaelle und alle korrekten Ergebnis jeweils adden
|
||||
writeLog("RES Fst:" + result.toString() + " " + res.toString());
|
||||
writeLog("RES Fst: reuslt: " + result.toString() + " res: " + res.toString());
|
||||
result.addAll(res);
|
||||
}
|
||||
}
|
||||
@ -1420,6 +1368,46 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Boolean checkA (UnifyPair aPair, Set<UnifyPair> sameEqSet, Set<Set<UnifyPair>> elems, Set<Set<UnifyPair>> result) {
|
||||
writeLog("checkA: " + aPair + "sameEqSet: " + sameEqSet);
|
||||
for (UnifyPair sameEq : sameEqSet) {
|
||||
if (sameEq.getLhsType() instanceof PlaceholderType) {
|
||||
Set<UnifyPair> localEq = new HashSet<>();
|
||||
Set<UnifyPair> unitedSubst = new HashSet<>(aPair.getAllSubstitutions());
|
||||
unitedSubst.addAll(aPair.getAllBases());
|
||||
unitedSubst.addAll(sameEq.getAllSubstitutions());
|
||||
unitedSubst.addAll(sameEq.getAllBases());
|
||||
localEq.add(new UnifyPair(aPair.getRhsType(), sameEq.getRhsType(), sameEq.getPairOp(), unitedSubst, null));
|
||||
Set<Set<UnifyPair>> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false);
|
||||
if (isUndefinedPairSetSet(localRes)) {
|
||||
if (result.isEmpty() || isUndefinedPairSetSet(result)) {
|
||||
result.addAll(localRes);
|
||||
}
|
||||
//writeLog("FALSE: " + aPair + "sameEqSet: " + sameEqSet);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
Set<UnifyPair> localEq = new HashSet<>();
|
||||
Set<UnifyPair> unitedSubst = new HashSet<>(aPair.getAllSubstitutions());
|
||||
unitedSubst.addAll(aPair.getAllBases());
|
||||
unitedSubst.addAll(sameEq.getAllSubstitutions());
|
||||
unitedSubst.addAll(sameEq.getAllBases());
|
||||
localEq.add(new UnifyPair(sameEq.getLhsType(), aPair.getRhsType(), sameEq.getPairOp(), unitedSubst, null));
|
||||
Set<Set<UnifyPair>> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false);
|
||||
if (isUndefinedPairSetSet(localRes)) {
|
||||
if (result.isEmpty() || isUndefinedPairSetSet(result)) {
|
||||
result.addAll(localRes);
|
||||
}
|
||||
//writeLog("FALSE: " + aPair + "sameEqSet: " + sameEqSet);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//writeLog("TRUE: " + aPair + "sameEqSet: " + sameEqSet);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected boolean couldBecorrect(Set<Pair<Set<UnifyPair>, UnifyPair>> reducedUndefResSubstGroundedBasePair, Set<UnifyPair> nextElem) {
|
||||
return reducedUndefResSubstGroundedBasePair.stream()
|
||||
.map(pair -> {
|
||||
@ -1752,7 +1740,11 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
Bei allen die Abhaengigkeit der Elemente aus eq2sAsList als evtl. als Substitution
|
||||
hinzufuegen
|
||||
*/
|
||||
Set<UnifyPair> consideredElements = new HashSet<>();
|
||||
for(UnifyPair pair : eq2sAsList) {
|
||||
if (consideredElements.contains(pair)) {
|
||||
continue;
|
||||
}
|
||||
PairOperator pairOp = pair.getPairOp();
|
||||
UnifyType lhsType = pair.getLhsType();
|
||||
UnifyType rhsType = pair.getRhsType();
|
||||
@ -1776,7 +1768,70 @@ public class TypeUnifyTask extends RecursiveTask<Set<Set<UnifyPair>>> {
|
||||
remElem.add(new UnifyPair(pair.getLhsType(), new SuperType(pair.getRhsType()), PairOperator.EQUALSDOT));
|
||||
x1.remove(remElem);
|
||||
}
|
||||
/* ZU LOESCHEN ANFANG
|
||||
//System.out.println(x1);
|
||||
Set<UnifyPair> sameEqSet = eq2sAsList.stream()
|
||||
.filter(x -> ((x.getLhsType().equals(lhsType) || x.getRhsType().equals(lhsType)) && !x.equals(pair)))
|
||||
.collect(Collectors.toCollection(HashSet::new));
|
||||
consideredElements.addAll(sameEqSet);
|
||||
Set<Set<UnifyPair>> x2 = x1;
|
||||
Set<Set<UnifyPair>> x1Res = new HashSet<>();
|
||||
writeLog("pair:\n" + pair.toString());
|
||||
writeLog("x1 Start:\n" + x1.toString());
|
||||
writeLog("sameEqSet:\n" + sameEqSet.toString());
|
||||
for (UnifyPair sameEq : sameEqSet) {
|
||||
writeLog("x1 Original:\n" + x1.toString());
|
||||
if (sameEq.getLhsType() instanceof PlaceholderType) {
|
||||
x1 = x1.stream().filter(y -> {
|
||||
UnifyPair type = y.stream().filter(z -> z.getLhsType().equals(lhsType)).findFirst().get();
|
||||
Set<UnifyPair> localEq = new HashSet<>();
|
||||
Set<UnifyPair> unitedSubst = new HashSet<>(type.getSubstitution());
|
||||
unitedSubst.addAll(sameEq.getSubstitution());
|
||||
localEq.add(new UnifyPair(type.getRhsType(), sameEq.getRhsType(), sameEq.getPairOp(), unitedSubst, null));
|
||||
Set<Set<UnifyPair>> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false);
|
||||
Boolean localCorr = !isUndefinedPairSetSet(localRes);
|
||||
if (!localCorr) {
|
||||
collectErr.addAll(localRes);
|
||||
}
|
||||
else {
|
||||
localRes.forEach(z -> z.addAll(y));
|
||||
x1Res.addAll(localRes);
|
||||
}
|
||||
return localCorr;
|
||||
}
|
||||
).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
else {
|
||||
x1 = x1.stream().filter(y -> {
|
||||
UnifyPair type = y.stream().filter(z -> z.getLhsType().equals(lhsType)).findFirst().get();
|
||||
Set<UnifyPair> localEq = new HashSet<>();
|
||||
Set<UnifyPair> unitedSubst = new HashSet<>(type.getSubstitution());
|
||||
unitedSubst.addAll(sameEq.getSubstitution());
|
||||
localEq.add(new UnifyPair(sameEq.getLhsType(), type.getRhsType(), sameEq.getPairOp(), unitedSubst, null));
|
||||
Set<Set<UnifyPair>> localRes = unify(localEq, new ArrayList<>(), fc, false, 0, false);
|
||||
Boolean localCorr = !isUndefinedPairSetSet(localRes);
|
||||
if (!localCorr) {
|
||||
collectErr.addAll(localRes);
|
||||
}
|
||||
else {
|
||||
localRes.forEach(z -> z.addAll(y));
|
||||
x1Res.addAll(localRes);
|
||||
}
|
||||
return localCorr;
|
||||
}
|
||||
).collect(Collectors.toCollection(HashSet::new));
|
||||
}
|
||||
writeLog("x1 nach Loeschung von " + sameEq.toString()+" :\n" + x1.toString());
|
||||
}
|
||||
Set<Set<UnifyPair>> x1ResPrime;
|
||||
if (sameEqSet.isEmpty()) {
|
||||
x1ResPrime = x1;
|
||||
}
|
||||
else {
|
||||
x1ResPrime = x1Res;
|
||||
}
|
||||
result.get(0).add(x1ResPrime);
|
||||
ZU LOESCHEN ENDE */
|
||||
result.get(0).add(x1);
|
||||
if (x1.isEmpty()) {
|
||||
undefined.add(pair); //Theta ist nicht im FC => Abbruch
|
||||
|
@ -1,13 +1,31 @@
|
||||
package de.dhbwstuttgart.typeinference.unify;
|
||||
|
||||
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.syntaxtree.factory.UnifyTypeFactory;
|
||||
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||
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<de.dhbwstuttgart.typeinference.constraints.Pair> cons;
|
||||
|
||||
IFiniteClosure fc;
|
||||
|
||||
public UnifyResultModel(ConstraintSet<de.dhbwstuttgart.typeinference.constraints.Pair> cons,
|
||||
IFiniteClosure fc) {
|
||||
this.cons = cons;
|
||||
this.fc = fc;
|
||||
}
|
||||
|
||||
private List<UnifyResultListener> listeners = new ArrayList<>();
|
||||
|
||||
public void addUnifyResultListener(UnifyResultListener listenerToAdd) {
|
||||
@ -18,7 +36,20 @@ public class UnifyResultModel {
|
||||
listeners.remove(listenerToRemove);
|
||||
}
|
||||
|
||||
public void notify(List<ResultSet> newResult) {
|
||||
public void notify(Set<Set<UnifyPair>> eqPrimePrimeSet) {
|
||||
Set<Set<UnifyPair>> eqPrimePrimeSetRet = eqPrimePrimeSet.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(), fc);
|
||||
}
|
||||
else return x; //wenn nichts veraendert wurde wird x zurueckgegeben
|
||||
}).collect(Collectors.toCollection(HashSet::new));
|
||||
List<ResultSet> newResult = eqPrimePrimeSetRet.stream().map(unifyPairs ->
|
||||
new ResultSet(UnifyTypeFactory.convert(unifyPairs, de.dhbwstuttgart.typeinference.constraints.Pair.generateTPHMap(cons))))
|
||||
.collect(Collectors.toList());
|
||||
UnifyResultEvent evt = new UnifyResultEvent(newResult);
|
||||
|
||||
for (UnifyResultListener listener : listeners) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dhbwstuttgart.typeinference.unify.interfaces;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@ -85,6 +86,13 @@ public interface IRuleSet {
|
||||
public Optional<UnifyPair> adaptExt(UnifyPair pair, IFiniteClosure fc);
|
||||
public Optional<UnifyPair> adaptSup(UnifyPair pair, IFiniteClosure fc);
|
||||
|
||||
/**
|
||||
* Applies the subst-Rule to a set of pairs (usually Eq').
|
||||
* @param pairs The set of pairs where the subst rule should apply.
|
||||
* @return An optional of the modified set, if there were any substitutions. An empty optional if there were no substitutions.
|
||||
*/
|
||||
public Optional<Set<UnifyPair>> subst(Set<UnifyPair> pairs, List<Set<Set<UnifyPair>>> oderConstraints);
|
||||
|
||||
/**
|
||||
* Applies the subst-Rule to a set of pairs (usually Eq').
|
||||
* @param pairs The set of pairs where the subst rule should apply.
|
||||
|
@ -2,6 +2,7 @@ package de.dhbwstuttgart.typeinference.unify.model;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@ -12,6 +13,7 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.common.collect.Ordering;
|
||||
@ -32,7 +34,7 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify;
|
||||
public class FiniteClosure //extends Ordering<UnifyType> //entfernt PL 2018-12-11
|
||||
implements IFiniteClosure {
|
||||
|
||||
FileWriter logFile;
|
||||
Writer logFile;
|
||||
static Boolean log = false;
|
||||
public void setLogTrue() {
|
||||
log = true;
|
||||
@ -66,7 +68,7 @@ implements IFiniteClosure {
|
||||
/**
|
||||
* Creates a new instance using the inheritance tree defined in the pairs.
|
||||
*/
|
||||
public FiniteClosure(Set<UnifyPair> pairs, FileWriter logFile) {
|
||||
public FiniteClosure(Set<UnifyPair> pairs, Writer logFile) {
|
||||
this.logFile = logFile;
|
||||
this.pairs = new HashSet<>(pairs);
|
||||
inheritanceGraph = new HashMap<UnifyType, Node<UnifyType>>();
|
||||
@ -656,11 +658,16 @@ implements IFiniteClosure {
|
||||
if (left.getName().equals("Matrix") || right.getName().equals("Matrix"))
|
||||
System.out.println("");
|
||||
/*
|
||||
pairop = PairOperator.SMALLERDOTWC;
|
||||
List<UnifyType> al = new ArrayList<>();
|
||||
PlaceholderType xx =new PlaceholderType("xx");
|
||||
al.add(xx);
|
||||
left = new ExtendsType(new ReferenceType("Vector", new TypeParams(al)));
|
||||
right = new ReferenceType("Vector", new TypeParams(new ArrayList<>(al)));
|
||||
|
||||
List<UnifyType> alr = new ArrayList<>();
|
||||
UnifyType exx = new ExtendsType(xx);
|
||||
alr.add(exx);
|
||||
right = new ExtendsType(new ReferenceType("Vector", new TypeParams(alr)));
|
||||
*/
|
||||
/*
|
||||
List<UnifyType> al = new ArrayList<>();
|
||||
@ -711,8 +718,13 @@ implements IFiniteClosure {
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.err.println("no LogFile");}}
|
||||
//Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok.
|
||||
long smallerLen = smallerRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)).count();
|
||||
//Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok.
|
||||
Predicate<UnifyPair> delFun = x -> !((x.getLhsType() instanceof PlaceholderType ||
|
||||
x.getRhsType() instanceof PlaceholderType)
|
||||
&& !((x.getLhsType() instanceof WildcardType) && //? extends/super a <.? a
|
||||
((WildcardType)x.getLhsType()).getWildcardedType().equals(x.getRhsType()))
|
||||
);
|
||||
long smallerLen = smallerRes.stream().filter(delFun).count();
|
||||
if (smallerLen == 0) return -1;
|
||||
else {
|
||||
up = new UnifyPair(right, left, pairop);
|
||||
@ -728,7 +740,7 @@ implements IFiniteClosure {
|
||||
catch (IOException e) {
|
||||
System.err.println("no LogFile");}}
|
||||
//Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok.
|
||||
long greaterLen = greaterRes.stream().filter(x -> !(x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType)).count();
|
||||
long greaterLen = greaterRes.stream().filter(delFun).count();
|
||||
if (greaterLen == 0) return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public class FunNType extends UnifyType {
|
||||
* Creates a FunN-Type with the specified TypeParameters.
|
||||
*/
|
||||
protected FunNType(TypeParams p) {
|
||||
super("Fun"+(p.size()-1), p);
|
||||
super("Fun"+(p.size()-1)+"$$", p);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,9 +1,11 @@
|
||||
package de.dhbwstuttgart.typeinference.unify.model;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BinaryOperator;
|
||||
@ -30,14 +32,25 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
|
||||
* in dem compare(Theta, Theta') aufgerufen wird.
|
||||
*/
|
||||
public int compareEq (UnifyPair left, UnifyPair right) {
|
||||
try {
|
||||
//if (left.getRhsType() instanceof WildcardType || right.getRhsType() instanceof WildcardType) {//PL 2019-01-12 ausgetauscht
|
||||
if (((PlaceholderType)left.getLhsType()).isInnerType() && ((PlaceholderType)right.getLhsType()).isInnerType()) {
|
||||
return fc.compare(left.getRhsType(), right.getRhsType(), PairOperator.SMALLERDOTWC);
|
||||
}
|
||||
else {
|
||||
return fc.compare(left.getRhsType(), right.getRhsType(), PairOperator.SMALLERDOT);
|
||||
}}
|
||||
catch (ClassCastException e) {
|
||||
try {
|
||||
((FiniteClosure)fc).logFile.write("ClassCastException: " + left.toString() +"\n\n");
|
||||
((FiniteClosure)fc).logFile.flush();
|
||||
}
|
||||
catch (IOException ie) {
|
||||
}
|
||||
return -99;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public int compareEq (UnifyPair left, UnifyPair right) {
|
||||
if (left == null || right == null)
|
||||
@ -115,6 +128,43 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
|
||||
* @see com.google.common.collect.Ordering#compare(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
public int compare (Set<UnifyPair> left, Set<UnifyPair> right) {
|
||||
/*
|
||||
//pairop = PairOperator.SMALLERDOTWC;
|
||||
List<UnifyType> al = new ArrayList<>();
|
||||
PlaceholderType xx = PlaceholderType.freshPlaceholder();
|
||||
al.add(xx);
|
||||
UnifyType t1 = new ExtendsType(new ReferenceType("Vector", new TypeParams(al)));
|
||||
|
||||
//PlaceholderType yy =new PlaceholderType("yy");
|
||||
List<UnifyType> alr = new ArrayList<>();
|
||||
UnifyType exx = new ExtendsType(xx);
|
||||
alr.add(exx);
|
||||
UnifyType t2 = new ExtendsType(new ReferenceType("Vector", new TypeParams(alr)));
|
||||
|
||||
PlaceholderType a = PlaceholderType.freshPlaceholder();
|
||||
a.setInnerType(true);
|
||||
UnifyPair p1 = new UnifyPair(a, t1, PairOperator.SMALLERDOTWC);
|
||||
PlaceholderType b = PlaceholderType.freshPlaceholder();
|
||||
b.setInnerType(true);
|
||||
UnifyPair p2 = new UnifyPair(b, t2, PairOperator.SMALLERDOTWC);
|
||||
|
||||
List<UnifyType> al3 = new ArrayList<>();
|
||||
al3.add(a);
|
||||
|
||||
List<UnifyType> al4 = new ArrayList<>();
|
||||
al4.add(b);
|
||||
|
||||
UnifyPair p3 = new UnifyPair(new PlaceholderType("c"), new ReferenceType("Vector", new TypeParams(al3)), PairOperator.EQUALSDOT);
|
||||
UnifyPair p4 = new UnifyPair(new PlaceholderType("c"), new ReferenceType("Vector", new TypeParams(al4)), PairOperator.EQUALSDOT);
|
||||
|
||||
right = new HashSet<>();
|
||||
right.add(p1);
|
||||
right.add(p3);
|
||||
left = new HashSet<>();
|
||||
left.add(p2);
|
||||
left.add(p4);
|
||||
*/
|
||||
|
||||
if ((left.size() == 1) && right.size() == 1) {
|
||||
if (left.iterator().next().getLhsType().getName().equals("AFS")) {
|
||||
System.out.println("");
|
||||
@ -198,9 +248,9 @@ public class OrderingUnifyPair extends Ordering<Set<UnifyPair>> {
|
||||
//Set<PlaceholderType> varsleft = lefteq.stream().map(x -> (PlaceholderType)x.getLhsType()).collect(Collectors.toCollection(HashSet::new));
|
||||
//Set<PlaceholderType> varsright = righteq.stream().map(x -> (PlaceholderType)x.getLhsType()).collect(Collectors.toCollection(HashSet::new));
|
||||
//filtern des Paares a = Theta, das durch a <. Thata' generiert wurde (nur im Fall 1 relevant) andere Substitutioen werden rausgefiltert
|
||||
lefteq.removeIf(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName())
|
||||
lefteq.removeIf(x -> (x.getBasePair()!=null) && !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName())
|
||||
||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName())));//removeIf(x -> !varsright.contains(x.getLhsType()));
|
||||
righteq.removeIf(x -> !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName())
|
||||
righteq.removeIf(x -> (x.getBasePair()!=null) && !(x.getLhsType().getName().equals(x.getBasePair().getLhsType().getName())
|
||||
||x.getLhsType().getName().equals(x.getBasePair().getRhsType().getName())));//.removeIf(x -> !varsleft.contains(x.getLhsType()));
|
||||
UnifyPair lseq = lefteq.iterator().next();
|
||||
UnifyPair rseq = righteq.iterator().next();
|
||||
|
@ -177,6 +177,20 @@ public class UnifyPair {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wenn in einem Paar bestehend aus 2 Typvariablen eine nicht wildcardtable ist,
|
||||
* so beide auf nicht wildcardtable setzen
|
||||
*/
|
||||
public void disableCondWildcards() {
|
||||
if (lhs instanceof PlaceholderType && rhs instanceof PlaceholderType
|
||||
&& (!((PlaceholderType)lhs).isWildcardable() || !((PlaceholderType)rhs).isWildcardable()))
|
||||
{
|
||||
((PlaceholderType)lhs).disableWildcardtable();
|
||||
((PlaceholderType)rhs).disableWildcardtable();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Boolean wrongWildcard() {
|
||||
return lhs.wrongWildcard() || rhs.wrongWildcard();
|
||||
}
|
||||
@ -219,10 +233,12 @@ public class UnifyPair {
|
||||
public String toString() {
|
||||
String ret = "";
|
||||
if (lhs instanceof PlaceholderType) {
|
||||
ret = new Integer(((PlaceholderType)lhs).getVariance()).toString() + " " + ((PlaceholderType)lhs).isInnerType();
|
||||
ret = new Integer(((PlaceholderType)lhs).getVariance()).toString() + " " + ((PlaceholderType)lhs).isInnerType()
|
||||
+ " " + ((PlaceholderType)lhs).isWildcardable();
|
||||
}
|
||||
if (rhs instanceof PlaceholderType) {
|
||||
ret = ret + ", " + new Integer(((PlaceholderType)rhs).getVariance()).toString() + " " + ((PlaceholderType)rhs).isInnerType();
|
||||
ret = ret + ", " + new Integer(((PlaceholderType)rhs).getVariance()).toString() + " " + ((PlaceholderType)rhs).isInnerType()
|
||||
+ " " + ((PlaceholderType)rhs).isWildcardable();
|
||||
}
|
||||
return "(" + lhs + " " + pairOp + " " + rhs + ", " + ret + ")"; //+ ", [" + getfBounded().toString()+ "])";
|
||||
}
|
||||
|
@ -32,11 +32,11 @@ public class FieldTphConsMethTest {
|
||||
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("FieldTphConsMeth");
|
||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
instanceOfClass = classToTest.getConstructor(Object.class).newInstance("C");
|
||||
Field a = classToTest.getDeclaredField("a");
|
||||
a.setAccessible(true);
|
||||
|
||||
@ -44,6 +44,7 @@ public class FieldTphConsMethTest {
|
||||
Object result = m.invoke(instanceOfClass, 42);
|
||||
|
||||
assertEquals(42,result);
|
||||
assertEquals("C", a.get(instanceOfClass));
|
||||
}
|
||||
|
||||
}
|
||||
|
66
src/test/java/bytecode/FieldTphMMethTest.java
Normal file
66
src/test/java/bytecode/FieldTphMMethTest.java
Normal file
@ -0,0 +1,66 @@
|
||||
package bytecode;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
|
||||
public class FieldTphMMethTest {
|
||||
|
||||
private static String path;
|
||||
private static File fileToTest;
|
||||
private static JavaTXCompiler compiler;
|
||||
private static ClassLoader loader;
|
||||
private static Class<?> classToTest;
|
||||
private static String pathToClassFile;
|
||||
private static Object instanceOfClass;
|
||||
private static Object instanceOfClass2;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/FieldTphMMeth.jav";
|
||||
fileToTest = new File(path);
|
||||
compiler = new JavaTXCompiler(fileToTest);
|
||||
compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/");
|
||||
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("FieldTphMMeth");
|
||||
instanceOfClass = classToTest.getConstructor(Object.class,Object.class,Boolean.class).newInstance("C",42,true);
|
||||
instanceOfClass2 = classToTest.getConstructor(Object.class,Object.class,Boolean.class).newInstance("C",42,false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testM() throws Exception {
|
||||
|
||||
Method m = classToTest.getDeclaredMethod("m", Object.class,Object.class,Boolean.class);
|
||||
Object result = m.invoke(instanceOfClass, "C",42,false);
|
||||
|
||||
assertEquals(42,result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithTrue() throws Exception {
|
||||
Field a = classToTest.getDeclaredField("a");
|
||||
a.setAccessible(true);
|
||||
|
||||
assertEquals("C", a.get(instanceOfClass));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithFalse() throws Exception {
|
||||
Field a = classToTest.getDeclaredField("a");
|
||||
a.setAccessible(true);
|
||||
|
||||
assertEquals(42, a.get(instanceOfClass2));
|
||||
}
|
||||
|
||||
}
|
@ -6,14 +6,13 @@ import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
|
||||
public class VectorSuperTest {
|
||||
public class InfTest {
|
||||
|
||||
private static String path;
|
||||
private static File fileToTest;
|
||||
@ -23,23 +22,16 @@ public class VectorSuperTest {
|
||||
private static String pathToClassFile;
|
||||
private static Object instanceOfClass;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/VectorSuper.jav";
|
||||
@Test
|
||||
public void generateBC() throws Exception {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inf.jav";
|
||||
fileToTest = new File(path);
|
||||
compiler = new JavaTXCompiler(fileToTest);
|
||||
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||
compiler.generateBytecode(pathToClassFile);
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("VectorSuper");
|
||||
classToTest = loader.loadClass("Inf");
|
||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
Method m = classToTest.getDeclaredMethod("m", Vector.class);
|
||||
//Object result = m.invoke(instanceOfClass, 1);
|
||||
|
||||
//assertEquals(1,result);
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package bytecode;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
@ -12,8 +12,8 @@ import org.junit.Test;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
|
||||
public class FieldTph {
|
||||
|
||||
public class KompTphTest {
|
||||
|
||||
private static String path;
|
||||
private static File fileToTest;
|
||||
private static JavaTXCompiler compiler;
|
||||
@ -22,22 +22,16 @@ public class FieldTph {
|
||||
private static String pathToClassFile;
|
||||
private static Object instanceOfClass;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/FieldTph.jav";
|
||||
@Test
|
||||
public void generateBC() throws Exception {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/KompTph.jav";
|
||||
fileToTest = new File(path);
|
||||
compiler = new JavaTXCompiler(fileToTest);
|
||||
compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/");
|
||||
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
|
||||
compiler.generateBytecode(pathToClassFile);
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("FieldTph");
|
||||
classToTest = loader.loadClass("KompTph");
|
||||
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Field[] fields = classToTest.getFields();
|
||||
assertEquals(1, fields.length);
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
@ -27,7 +28,7 @@ public class MatrixOpTest {
|
||||
private static Object instanceOfClass_m3;
|
||||
|
||||
@Test
|
||||
public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, InstantiationException {
|
||||
public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, IOException, InstantiationException, NoSuchFieldException {
|
||||
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/MatrixOP.jav";
|
||||
fileToTest = new File(path);
|
||||
compiler = new JavaTXCompiler(fileToTest);
|
||||
@ -35,7 +36,7 @@ public class MatrixOpTest {
|
||||
compiler.generateBytecode(pathToClassFile);
|
||||
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
||||
classToTest = loader.loadClass("MatrixOP");
|
||||
/*
|
||||
|
||||
Vector<Vector<Integer>> vv = new Vector<Vector<Integer>>();
|
||||
Vector<Integer> v1 = new Vector<Integer> ();
|
||||
v1.addElement(2);
|
||||
@ -48,6 +49,7 @@ public class MatrixOpTest {
|
||||
//m1.addElement(v2);
|
||||
vv.addElement(v1);
|
||||
vv.addElement(v2);
|
||||
|
||||
instanceOfClass_m1 = classToTest.getDeclaredConstructor(Vector.class).newInstance(vv); //Matrix m1 = new Matrix(vv);
|
||||
|
||||
Vector<Vector<Integer>> vv1 = new Vector<Vector<Integer>>();
|
||||
@ -62,13 +64,23 @@ public class MatrixOpTest {
|
||||
//m2.addElement(v4);
|
||||
vv1.addElement(v3);
|
||||
vv1.addElement(v4);
|
||||
|
||||
instanceOfClass_m2 = classToTest.getDeclaredConstructor(Vector.class).newInstance(vv1);//Matrix m2 = new Matrix(vv1);
|
||||
|
||||
|
||||
|
||||
//Matrix m3 = m1.mul(vv1);
|
||||
Method mul = classToTest.getDeclaredMethod("mul", Vector.class);
|
||||
Object result = mul.invoke(instanceOfClass_m1, instanceOfClass_m2);
|
||||
// Method mul = classToTest.getDeclaredMethod("mul", Vector.class);
|
||||
// Object result = mul.invoke(instanceOfClass_m1, instanceOfClass_m2);
|
||||
Field mul = classToTest.getField("mul");
|
||||
mul.setAccessible(true);
|
||||
|
||||
Class<?> lambda = mul.get(instanceOfClass_m1).getClass();
|
||||
Method apply = lambda.getMethod("apply", Object.class,Object.class);
|
||||
// Damit man auf die Methode zugreifen kann
|
||||
apply.setAccessible(true);
|
||||
|
||||
Object result = apply.invoke(mul.get(instanceOfClass_m1),instanceOfClass_m1, instanceOfClass_m2);
|
||||
System.out.println(instanceOfClass_m1.toString() + " * " + instanceOfClass_m2.toString() + " = " + result.toString());
|
||||
|
||||
Vector<Vector<Integer>> res = new Vector<Vector<Integer>>();
|
||||
@ -85,7 +97,7 @@ public class MatrixOpTest {
|
||||
res.addElement(v6);
|
||||
instanceOfClass_m3 = classToTest.getDeclaredConstructor(Vector.class).newInstance(res);
|
||||
assertEquals(result, instanceOfClass_m3);
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
0
src/test/java/bytecode/MatrixTest.java
Normal file → Executable file
0
src/test/java/bytecode/MatrixTest.java
Normal file → Executable file
64
src/test/java/bytecode/simplifyalgo/FinderTest.java
Normal file
64
src/test/java/bytecode/simplifyalgo/FinderTest.java
Normal file
@ -0,0 +1,64 @@
|
||||
package bytecode.simplifyalgo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import de.dhbwstuttgart.bytecode.TPHExtractor;
|
||||
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
|
||||
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
|
||||
import de.dhbwstuttgart.bytecode.utilities.ConstraintsFinder;
|
||||
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
|
||||
import de.dhbwstuttgart.bytecode.utilities.Simplify;
|
||||
import de.dhbwstuttgart.typedeployment.TypeInsertPlacer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Fayez Abu Alia
|
||||
*
|
||||
*/
|
||||
public class FinderTest {
|
||||
|
||||
@Test
|
||||
public void testM1() {
|
||||
List<TPHConstraint> allCons = new ArrayList<>();
|
||||
// L < M
|
||||
TPHConstraint c1 = new ExtendsConstraint("L", "M", Relation.EXTENDS);
|
||||
// L < N
|
||||
TPHConstraint c2 = new ExtendsConstraint("L", "N", Relation.EXTENDS);
|
||||
// M < O
|
||||
TPHConstraint c3 = new ExtendsConstraint("M", "O", Relation.EXTENDS);
|
||||
// M < P
|
||||
TPHConstraint c4 = new ExtendsConstraint("M", "P", Relation.EXTENDS);
|
||||
|
||||
allCons.add(c1);
|
||||
allCons.add(c2);
|
||||
allCons.add(c3);
|
||||
allCons.add(c4);
|
||||
List<List<TPHConstraint>> res = new ArrayList<>();
|
||||
List<TPHConstraint> l1 = new ArrayList<>();
|
||||
List<TPHConstraint> l2 = new ArrayList<>();
|
||||
|
||||
l1.add(c1);
|
||||
l1.add(c2);
|
||||
|
||||
l2.add(c3);
|
||||
l2.add(c4);
|
||||
|
||||
res.add(l1);
|
||||
res.add(l2);
|
||||
|
||||
ConstraintsFinder finder = new ConstraintsFinder(allCons);
|
||||
|
||||
assertEquals(finder.findConstraints(), res);
|
||||
}
|
||||
|
||||
}
|
5
src/test/java/logFiles/.gitignore
vendored
5
src/test/java/logFiles/.gitignore
vendored
@ -1,5 +0,0 @@
|
||||
# Ignore everything in this directory
|
||||
*
|
||||
# Except this file
|
||||
!.gitignore
|
||||
|
27
src/test/resources/bytecode/javFiles/FieldTphMMeth.jav
Normal file
27
src/test/resources/bytecode/javFiles/FieldTphMMeth.jav
Normal file
@ -0,0 +1,27 @@
|
||||
import java.lang.Boolean;
|
||||
|
||||
public class FieldTphMMeth {
|
||||
a;
|
||||
|
||||
public FieldTphMMeth(c,d,e) {
|
||||
a = m(c,d,e);
|
||||
}
|
||||
|
||||
m(b,d,e) {
|
||||
if(e) {
|
||||
return m3(b);
|
||||
} else{
|
||||
return m3(d);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
m2(b) {
|
||||
a = m3(b);
|
||||
}
|
||||
|
||||
m3(b){
|
||||
return b;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
public class Id {
|
||||
public class Id<FTU extends FTT,FTT> {
|
||||
|
||||
id(b){
|
||||
id(FTU b){
|
||||
return b;
|
||||
}
|
||||
}
|
19
src/test/resources/bytecode/javFiles/Inf.jav
Normal file
19
src/test/resources/bytecode/javFiles/Inf.jav
Normal file
@ -0,0 +1,19 @@
|
||||
public class Inf {
|
||||
m(x,y,a){
|
||||
var z;
|
||||
var v;
|
||||
var w;
|
||||
var b;
|
||||
y=x;
|
||||
z=x;
|
||||
v=y;
|
||||
w=y;
|
||||
y=a;
|
||||
b=a;
|
||||
}
|
||||
}
|
||||
// v w
|
||||
// \ /
|
||||
// z y b
|
||||
// \ / \ /
|
||||
// x a
|
13
src/test/resources/bytecode/javFiles/KompTph.jav
Normal file
13
src/test/resources/bytecode/javFiles/KompTph.jav
Normal file
@ -0,0 +1,13 @@
|
||||
public class KompTph {
|
||||
public m(a, b, c) {
|
||||
var d = a;
|
||||
var e = a;
|
||||
a = b;
|
||||
c = b;
|
||||
m2(a,c);
|
||||
}
|
||||
|
||||
public m2(a,b){
|
||||
m(a,a,b);
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
import java.util.Vector;
|
||||
import java.lang.Integer;
|
||||
//import java.lang.Float;
|
||||
import java.lang.Float;
|
||||
//import java.lang.Byte;
|
||||
//import java.lang.Boolean;
|
||||
|
||||
public class Matrix extends Vector<Vector<Integer>> {
|
||||
public class Matrix extends Vector<Vector<Integer>> {
|
||||
|
||||
Matrix () {
|
||||
}
|
||||
@ -16,10 +16,10 @@ public class Matrix extends Vector<Vector<Integer>> {
|
||||
// Boolean a = this.add(vv.elementAt(i));
|
||||
this.add(vv.elementAt(i));
|
||||
i=i+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mul(m) {
|
||||
mul(java.util.Vector<? extends Vector<? extends java.lang.Integer>> m) {
|
||||
var ret = new Matrix();
|
||||
var i = 0;
|
||||
while(i < size()) {
|
||||
|
@ -3,7 +3,7 @@ import java.lang.Integer;
|
||||
import java.lang.Byte;
|
||||
import java.lang.Boolean;
|
||||
|
||||
public class MatrixOP extends Vector<Vector<Integer>> {
|
||||
public class MatrixOP extends Vector<Vector<Integer>> {
|
||||
|
||||
MatrixOP () {
|
||||
}
|
||||
@ -18,10 +18,10 @@ public class MatrixOP extends Vector<Vector<Integer>> {
|
||||
}
|
||||
}
|
||||
|
||||
mul = (m1, m2) -> {
|
||||
Fun2$$<java.util.Vector<? extends java.util.Vector<? extends java.lang.Byte>>, java.util.Vector<? extends java.util.Vector<? extends java.lang.Byte>>, MatrixOP> mul = (m1, m2) -> {
|
||||
var ret = new MatrixOP();
|
||||
var i = 0;
|
||||
while(i < size()) {
|
||||
while(i < m1.size()) {
|
||||
var v1 = m1.elementAt(i);
|
||||
var v2 = new Vector<Integer>();
|
||||
var j = 0;
|
||||
|
@ -14,10 +14,10 @@ sort(in){
|
||||
return merge(sort(firstHalf), sort(secondHalf));
|
||||
}
|
||||
*/
|
||||
/*
|
||||
|
||||
|
||||
void sort(ArrayList<String> a){
|
||||
void sort(a){
|
||||
a = merge(a,a);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
import java.util.Vector;
|
||||
import java.lang.Integer;
|
||||
|
||||
public class VectorSuper {
|
||||
|
||||
m(x){
|
||||
Integer y = 1;
|
||||
x.addElement(y);
|
||||
//return x;
|
||||
}
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
class Faculty {
|
||||
|
||||
Integer mul(Integer x, Integer y) {
|
||||
//fact;
|
||||
|
||||
Integer mul(Integer x, Integer y) {
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
m () {
|
||||
var fact = (Integer x) -> {
|
||||
var fact = (Integer x) -> {
|
||||
return mul(x, fact.apply(x));
|
||||
};
|
||||
return fact;
|
||||
|
@ -1,13 +1,8 @@
|
||||
class MathStruc <A> {
|
||||
|
||||
class mathStruc<A> {
|
||||
A model;
|
||||
|
||||
innerOp = o -> ms -> new MathStruc<A>(o.apply(this.model, ms.model));
|
||||
|
||||
mathStruc(A a) { }
|
||||
|
||||
A model(){ A a; return a; }
|
||||
|
||||
methode(){
|
||||
var innerOp = o -> ms ->
|
||||
new mathStruc<A>(o.apply(this.model(),ms.model()));
|
||||
return innerOp;
|
||||
}
|
||||
}
|
||||
MathStruc(A m) { model=m; }
|
||||
}
|
Reference in New Issue
Block a user