isInSolvedForm(), Unit-Tests, removed PairOp.Equals

This commit is contained in:
Florian Steurer 2016-03-31 22:09:46 +02:00
parent 304a5693a6
commit d89d06797e
7 changed files with 443 additions and 306 deletions

View File

@ -6,7 +6,6 @@ public class MPair {
SMALLER, SMALLER,
SMALLERDOT, SMALLERDOT,
SMALLERDOTWC, SMALLERDOTWC,
EQUALS,
EQUALSDOT; EQUALSDOT;
@Override @Override
@ -18,8 +17,6 @@ public class MPair {
return "<."; return "<.";
case SMALLERDOTWC: case SMALLERDOTWC:
return "<.?"; return "<.?";
case EQUALS:
return "=";
default: default:
return "=."; return "=.";
} }

View File

@ -56,32 +56,27 @@ public class Mapping {
return result.size() == pairs.size() ? Optional.of(result) : Optional.empty(); return result.size() == pairs.size() ? Optional.of(result) : Optional.empty();
} }
private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) { private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) {
/*
* TODO
* Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt?
*/
switch(op) { switch(op) {
case Equal: case Equal:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALSDOT;
case Smaller: case Smaller:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER;
case SmallerExtends:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT; return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT;
default: case SmallerExtends:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOTWC;
default:
return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALSDOT;
} }
} }
private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) { private de.dhbwstuttgart.typeinference.Pair.PairOperator unmapOp(de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator op) {
switch(op) { switch(op) {
case EQUALS:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
case SMALLER: case SMALLER:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller; return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
case SMALLERDOT: case SMALLERDOT:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends; return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller;
case SMALLERDOTWC:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.SmallerExtends;
default: default:
return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal; return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal;
} }

View File

@ -33,7 +33,7 @@ public class MartelliMontanariUnify implements IUnify {
Type prev = iter.next(); Type prev = iter.next();
while(iter.hasNext()) { while(iter.hasNext()) {
Type next = iter.next(); Type next = iter.next();
termsQ.add(new MPair(prev, next, PairOperator.EQUALS)); termsQ.add(new MPair(prev, next, PairOperator.EQUALSDOT));
prev = next; prev = next;
} }

View File

@ -542,12 +542,12 @@ public class RuleSet implements IRuleSet{
typeMap.put(t2, typeMap.get(t2)+1); typeMap.put(t2, typeMap.get(t2)+1);
} }
ArrayList<MPair> result = new ArrayList<MPair>(pairs); Queue<MPair> result1 = new LinkedList<MPair>(pairs);
ArrayList<MPair> result = new ArrayList<MPair>();
boolean applied = false; boolean applied = false;
for(int i = 0; i < result.size(); i++) { while(!result1.isEmpty()) {
MPair pair = result.get(i); MPair pair = result1.poll();
PlaceholderType lhsType = null; PlaceholderType lhsType = null;
Type rhsType; Type rhsType;
@ -560,8 +560,11 @@ public class RuleSet implements IRuleSet{
&& !rhsType.getTypeParams().occurs(lhsType)) { && !rhsType.getTypeParams().occurs(lhsType)) {
Unifier uni = new Unifier(lhsType, rhsType); Unifier uni = new Unifier(lhsType, rhsType);
result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new)); result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new));
result1 = result1.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new));
applied = true; applied = true;
} }
result.add(pair);
} }
return applied ? Optional.of(new HashSet<>(result)) : Optional.empty(); return applied ? Optional.of(new HashSet<>(result)) : Optional.empty();

View File

@ -12,9 +12,6 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.IRuleSet;
import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations;
@ -68,7 +65,7 @@ public class Unify {
} }
// Add the set of [a =. Theta | (a=. Theta) in Eq2'] // Add the set of [a =. Theta | (a=. Theta) in Eq2']
Set<MPair> bufferSet = eq2s.stream() Set<MPair> bufferSet = eq2s.stream()
.filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType) .filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
@ -133,17 +130,31 @@ public class Unify {
* b) Build the union over everything * b) Build the union over everything
*/ */
for(Set<MPair> eqss : changed) { for(Set<MPair> eqss : changed)
eqPrimePrimeSet.addAll(this.unify(eqss, fc)); eqPrimePrimeSet.addAll(this.unify(eqss, fc));
}
/* /*
* Step 7: Filter empty sets; * Step 7: Filter empty sets;
*/ */
return eqPrimePrimeSet.stream().filter(x -> !x.isEmpty()).collect(Collectors.toCollection(HashSet::new)); return eqPrimePrimeSet.stream().filter(x -> isSolvedForm(x)).collect(Collectors.toCollection(HashSet::new));
} }
protected boolean isSolvedForm(Set<MPair> eqPrimePrime) {
for(MPair pair : eqPrimePrime) {
Type lhsType = pair.getLhsType();
Type rhsType = pair.getRhsType();
if(!(lhsType instanceof PlaceholderType))
return false;
if(pair.getPairOp() != PairOperator.EQUALSDOT && !(rhsType instanceof PlaceholderType))
return false;
}
return true;
}
protected Set<MPair> applyTypeUnificationRules(Set<MPair> eq, IFiniteClosure fc) { protected Set<MPair> applyTypeUnificationRules(Set<MPair> eq, IFiniteClosure fc) {
/* /*
@ -288,7 +299,8 @@ public class Unify {
result.get(7).add(unifyCase8(lhsType, (PlaceholderType) rhsType, fc)); result.get(7).add(unifyCase8(lhsType, (PlaceholderType) rhsType, fc));
} }
return result.stream().filter(x -> x.size() > 0).collect(Collectors.toCollection(HashSet::new)); 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));
} }
protected Set<MPair> unifyCase1(PlaceholderType a, Type thetaPrime, IFiniteClosure fc) { protected Set<MPair> unifyCase1(PlaceholderType a, Type thetaPrime, IFiniteClosure fc) {

View File

@ -7,7 +7,9 @@ import org.junit.Test;
import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder; import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder; import de.dhbwstuttgart.syntaxtree.factory.Unify_FC_TTO_Builder;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair; import de.dhbwstuttgart.typeinference.Pair;
@ -17,287 +19,322 @@ import de.dhbwstuttgart.typeinference.unify.Unify;
public class UnifyOldTest { public class UnifyOldTest {
@Test @Test
public void unifyTestSimpleTypes() { public void unifyTest1() {
// Init Factories and Builders // Init Factories and Builders
UnifyTypeFactory typeFactory = new UnifyTypeFactory(); UnifyTypeFactory typeFactory = new UnifyTypeFactory();
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
/* TypePlaceholder a = typeFactory.GetTypePlaceholder("a");
* Test a <. Boolean ExtendsWildcardType extA = typeFactory.GetExtendsType(a);
*/ TypePlaceholder b = typeFactory.GetTypePlaceholder("b");
ExtendsWildcardType extB = typeFactory.GetExtendsType(b);
// Init Types RefType integer = typeFactory.GetSimpleType("Integer");
RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean"); SuperWildcardType supInt = typeFactory.GetSuperType(integer);
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); RefType listsupint = typeFactory.GetSimpleType("List", supInt);
RefType number = typeFactory.GetSimpleType("Number");
// Expected Result RefType object = typeFactory.GetSimpleType("XObjectX");
resultBuilder.clear(); ExtendsWildcardType extNum = typeFactory.GetExtendsType(number);
resultBuilder.addPair(aTph, boolT, PairOperator.Equal); RefType intlist = typeFactory.GetSimpleType("List", integer);
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT), RefType alist = typeFactory.GetSimpleType("List", a);
PairOperator.Equal); RefType extBlist = typeFactory.GetSimpleType("List", extB);
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge(); RefType blist = typeFactory.GetSimpleType("List", b);
RefType extNumlist = typeFactory.GetSimpleType("List", extNum);
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(aTph, boolT);
Menge<Menge<Pair>> actualResult = Unify.unify(
assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
// System.out.println(expectedResult);
// System.out.println(actualResult);
Assert.assertTrue(mengeEquals(expectedResult, actualResult));
/*
* Test b <. a, a <. Boolean
*/
// Init Types
boolT = typeFactory.GetSimpleType("java.lang.Boolean");
aTph = typeFactory.GetTypePlaceholder("a");
TypePlaceholder bTph = typeFactory.GetTypePlaceholder("b");
// Expected Result
resultBuilder.clear();
resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT),
PairOperator.Equal);
resultBuilder.addPair(bTph, boolT, PairOperator.Equal);
resultBuilder.addPair(bTph, typeFactory.GetExtendsType(boolT),
PairOperator.Equal);
expectedResult = resultBuilder.getNestedPairMenge();
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(bTph, aTph);
assumptionBuilder.addPair(aTph, boolT);
actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
fcBuilder.Get_FC_TTO());
// System.out.println(expectedResult);
// System.out.println(actualResult);
// NOTE: Elemente im actualResult sind nicht unique
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
/*
* Test b <. a, a <. b
*/
aTph = typeFactory.GetTypePlaceholder("a");
bTph = typeFactory.GetTypePlaceholder("b");
// Expected Result
resultBuilder.clear();
resultBuilder.addPair(bTph, aTph);
resultBuilder.addPair(aTph, bTph);
Menge<Pair> buffer = resultBuilder.getPairMenge();
expectedResult = new Menge<Menge<Pair>>();
expectedResult.add(buffer);
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(bTph, aTph);
assumptionBuilder.addPair(aTph, bTph);
actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
fcBuilder.Get_FC_TTO());
// System.out.println(expectedResult);
// System.out.println(actualResult);
Assert.assertTrue(mengeEquals(expectedResult, actualResult));
/*
* Test Integer <. a, a <. Boolean
*/
RefType intT = typeFactory.GetSimpleType("java.lang.Integer");
boolT = typeFactory.GetSimpleType("java.lang.Boolean");
aTph = typeFactory.GetTypePlaceholder("a");
bTph = typeFactory.GetTypePlaceholder("b");
// Expected Result
resultBuilder.clear();
expectedResult = resultBuilder.getNestedPairMenge();
// Actual Result
assumptionBuilder.clear();
assumptionBuilder.addPair(intT, aTph);
assumptionBuilder.addPair(aTph, boolT);
actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
fcBuilder.Get_FC_TTO());
// System.out.println(expectedResult);
// System.out.println(actualResult);
Assert.assertTrue(mengeEquals(expectedResult, actualResult));
fcBuilder.AddInheritance(number, object);
fcBuilder.AddInheritance(integer, number);
assumptionBuilder.addPair(alist, extBlist, PairOperator.Smaller);
assumptionBuilder.addPair(blist, extNumlist, PairOperator.Smaller);
System.out.println(Unify.unify(assumptionBuilder.getPairMenge(),
fcBuilder.Get_FC_TTO()));
} }
//
@Test // @Test
public void unifyTestGenerics() { // public void unifyTestSimpleTypes() {
// // Init Factories and Builders
// Init Factories and Builders // UnifyTypeFactory typeFactory = new UnifyTypeFactory();
UnifyTypeFactory typeFactory = new UnifyTypeFactory(); // Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); // UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); // UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); //
// /*
/* // * Test a <. Boolean
* Test a <. MyClass<T, F> // */
*/ //
// // Init Types
TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); // RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean");
RefType myType = typeFactory.GetSimpleType("MyClass", // TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
typeFactory.GetTypePlaceholder("T"), //
typeFactory.GetTypePlaceholder("F")); // // Expected Result
// resultBuilder.clear();
// Expected Result // resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
resultBuilder.clear(); // resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT),
resultBuilder.addPair(aTph, myType, PairOperator.Equal); // PairOperator.Equal);
resultBuilder.addPair(aTph, typeFactory.GetExtendsType(myType)); // Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge(); //
// // Actual Result
// Actual Result // assumptionBuilder.clear();
assumptionBuilder.clear(); // assumptionBuilder.addPair(aTph, boolT);
assumptionBuilder.addPair(aTph, myType); // Menge<Menge<Pair>> actualResult = Unify.unify(
Menge<Menge<Pair>> actualResult = Unify.unify( // assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); //
// // System.out.println(expectedResult);
// System.out.println(expectedResult); // // System.out.println(actualResult);
// System.out.println(actualResult); //
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
Assert.assertTrue(mengeEquals(expectedResult, actualResult)); //
// /*
/* // * Test b <. a, a <. Boolean
* Test List<List<T>> <. List<T> // */
*/ //
// // Init Types
TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T"); // boolT = typeFactory.GetSimpleType("java.lang.Boolean");
RefType list = typeFactory.GetSimpleType("List", tTph); // aTph = typeFactory.GetTypePlaceholder("a");
RefType listlist = typeFactory.GetSimpleType("List", list); // TypePlaceholder bTph = typeFactory.GetTypePlaceholder("b");
//
// Expected Result // // Expected Result
resultBuilder.clear(); // resultBuilder.clear();
resultBuilder.addPair(typeFactory.GetExtendsType(list), tTph, // resultBuilder.addPair(aTph, boolT, PairOperator.Equal);
PairOperator.Equal); // resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT),
expectedResult = resultBuilder.getNestedPairMenge(); // PairOperator.Equal);
// resultBuilder.addPair(bTph, boolT, PairOperator.Equal);
// Actual Result // resultBuilder.addPair(bTph, typeFactory.GetExtendsType(boolT),
assumptionBuilder.clear(); // PairOperator.Equal);
assumptionBuilder.addPair(listlist, list); // expectedResult = resultBuilder.getNestedPairMenge();
actualResult = Unify.unify(assumptionBuilder.getPairMenge(), //
fcBuilder.Get_FC_TTO()); // // Actual Result
// assumptionBuilder.clear();
System.out.println(expectedResult); // assumptionBuilder.addPair(bTph, aTph);
System.out.println(actualResult); // assumptionBuilder.addPair(aTph, boolT);
// actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
Assert.assertTrue(mengeEquals(expectedResult, actualResult)); // fcBuilder.Get_FC_TTO());
//
/* // // System.out.println(expectedResult);
* Test List<T> <. List<List<T>> // // System.out.println(actualResult);
*/ //
} // // NOTE: Elemente im actualResult sind nicht unique
// // Assert.assertTrue(mengeEquals(expectedResult, actualResult));
@Test //
public void unifyTestInheritance() { // /*
// * Test b <. a, a <. b
// Init Factories and Builders // */
UnifyTypeFactory typeFactory = new UnifyTypeFactory(); //
Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder(); // aTph = typeFactory.GetTypePlaceholder("a");
UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder(); // bTph = typeFactory.GetTypePlaceholder("b");
UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder(); //
// // Expected Result
// Init Types // resultBuilder.clear();
RefType tBool = typeFactory.GetSimpleType("java.lang.Boolean"); // resultBuilder.addPair(bTph, aTph);
RefType tString = typeFactory.GetSimpleType("java.lang.String"); // resultBuilder.addPair(aTph, bTph);
RefType tInt = typeFactory.GetSimpleType("java.lang.Integer"); //
TypePlaceholder tphA = typeFactory.GetTypePlaceholder("a"); // Menge<Pair> buffer = resultBuilder.getPairMenge();
// expectedResult = new Menge<Menge<Pair>>();
// Build inheritance hierachy // expectedResult.add(buffer);
// Bool <. String <. Int //
fcBuilder.AddInheritance(tBool, tString); // // Actual Result
fcBuilder.AddInheritance(tString, tInt); // assumptionBuilder.clear();
// assumptionBuilder.addPair(bTph, aTph);
// Build Assumptions // assumptionBuilder.addPair(aTph, bTph);
assumptionBuilder.addPair(tphA, tString); // actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
// fcBuilder.Get_FC_TTO());
// Build expected result //
resultBuilder.addPair(tphA, tBool, PairOperator.Equal); // // System.out.println(expectedResult);
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tBool), // // System.out.println(actualResult);
PairOperator.Equal); //
resultBuilder.addPair(tphA, tString, PairOperator.Equal); // Assert.assertTrue(mengeEquals(expectedResult, actualResult));
resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString), //
PairOperator.Equal); // /*
// * Test Integer <. a, a <. Boolean
// Assert // */
Menge<Menge<Pair>> actualResult = Unify.unify( //
assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO()); // RefType intT = typeFactory.GetSimpleType("java.lang.Integer");
// boolT = typeFactory.GetSimpleType("java.lang.Boolean");
// System.out.println(actualResult); // aTph = typeFactory.GetTypePlaceholder("a");
// System.out.println("-------------------"); // bTph = typeFactory.GetTypePlaceholder("b");
// System.out.println(resultBuilder.getNestedPairMenge()); //
// // Expected Result
Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(), // resultBuilder.clear();
actualResult)); // expectedResult = resultBuilder.getNestedPairMenge();
} //
// // Actual Result
@Test // assumptionBuilder.clear();
public void unifyTestWildcards() { // assumptionBuilder.addPair(intT, aTph);
// assumptionBuilder.addPair(aTph, boolT);
} // actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
// fcBuilder.Get_FC_TTO());
private static boolean mengeEquals(Menge<Menge<Pair>> m1, //
Menge<Menge<Pair>> m2) { // // System.out.println(expectedResult);
if (m1.size() != m2.size()) // // System.out.println(actualResult);
return false; //
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
return containsAll(m1, m2) && containsAll(m2, m1); //
} // }
//
private static boolean containsAll(Menge<Menge<Pair>> m1, // @Test
Menge<Menge<Pair>> m2) { // public void unifyTestGenerics() {
for (Menge<Pair> elem : m2) //
if (!contains(m1, elem)) // // Init Factories and Builders
return false; // UnifyTypeFactory typeFactory = new UnifyTypeFactory();
return true; // Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
} // UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
// UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
private static boolean contains(Menge<Menge<Pair>> m1, Menge<Pair> m2) { //
for (Menge<Pair> elem : m1) // /*
if (mengePairEquals(elem, m2)) // * Test a <. MyClass<T, F>
return true; // */
return false; //
} // TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a");
// RefType myType = typeFactory.GetSimpleType("MyClass",
private static boolean mengePairEquals(Menge<Pair> m1, Menge<Pair> m2) { // typeFactory.GetTypePlaceholder("T"),
if (m1.size() != m2.size()) // typeFactory.GetTypePlaceholder("F"));
return false; //
// // Expected Result
return containsAllPair(m1, m2) && containsAllPair(m2, m1); // resultBuilder.clear();
} // resultBuilder.addPair(aTph, myType, PairOperator.Equal);
// resultBuilder.addPair(aTph, typeFactory.GetExtendsType(myType));
private static boolean containsAllPair(Menge<Pair> m1, Menge<Pair> m2) { // Menge<Menge<Pair>> expectedResult = resultBuilder.getNestedPairMenge();
for (Pair elem : m1) //
if (contains(m2, elem)) // // Actual Result
return true; // assumptionBuilder.clear();
return false; // assumptionBuilder.addPair(aTph, myType);
} // Menge<Menge<Pair>> actualResult = Unify.unify(
// assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
private static boolean contains(Menge<Pair> m, Pair p) { //
for (Pair elem : m) // // System.out.println(expectedResult);
if (pairEquals(elem, p)) // // System.out.println(actualResult);
return true; //
return false; // Assert.assertTrue(mengeEquals(expectedResult, actualResult));
//
} // /*
// * Test List<List<T>> <. List<T>
private static boolean pairEquals(Pair p1, Pair p2) { // */
return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2)) //
|| (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1)); // TypePlaceholder tTph = typeFactory.GetTypePlaceholder("T");
} // RefType list = typeFactory.GetSimpleType("List", tTph);
// RefType listlist = typeFactory.GetSimpleType("List", list);
//
// // Expected Result
// resultBuilder.clear();
// resultBuilder.addPair(typeFactory.GetExtendsType(list), tTph,
// PairOperator.Equal);
// expectedResult = resultBuilder.getNestedPairMenge();
//
// // Actual Result
// assumptionBuilder.clear();
// assumptionBuilder.addPair(listlist, list);
// actualResult = Unify.unify(assumptionBuilder.getPairMenge(),
// fcBuilder.Get_FC_TTO());
//
// System.out.println(expectedResult);
// System.out.println(actualResult);
//
// Assert.assertTrue(mengeEquals(expectedResult, actualResult));
//
// /*
// * Test List<T> <. List<List<T>>
// */
// }
//
// @Test
// public void unifyTestInheritance() {
//
// // Init Factories and Builders
// UnifyTypeFactory typeFactory = new UnifyTypeFactory();
// Unify_FC_TTO_Builder fcBuilder = new Unify_FC_TTO_Builder();
// UnifyPairMengenBuilder assumptionBuilder = new UnifyPairMengenBuilder();
// UnifyPairMengenBuilder resultBuilder = new UnifyPairMengenBuilder();
//
// // Init Types
// RefType tBool = typeFactory.GetSimpleType("java.lang.Boolean");
// RefType tString = typeFactory.GetSimpleType("java.lang.String");
// RefType tInt = typeFactory.GetSimpleType("java.lang.Integer");
// TypePlaceholder tphA = typeFactory.GetTypePlaceholder("a");
//
// // Build inheritance hierachy
// // Bool <. String <. Int
// fcBuilder.AddInheritance(tBool, tString);
// fcBuilder.AddInheritance(tString, tInt);
//
// // Build Assumptions
// assumptionBuilder.addPair(tphA, tString);
//
// // Build expected result
// resultBuilder.addPair(tphA, tBool, PairOperator.Equal);
// resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tBool),
// PairOperator.Equal);
// resultBuilder.addPair(tphA, tString, PairOperator.Equal);
// resultBuilder.addPair(tphA, typeFactory.GetExtendsType(tString),
// PairOperator.Equal);
//
// // Assert
// Menge<Menge<Pair>> actualResult = Unify.unify(
// assumptionBuilder.getPairMenge(), fcBuilder.Get_FC_TTO());
//
// // System.out.println(actualResult);
// // System.out.println("-------------------");
// // System.out.println(resultBuilder.getNestedPairMenge());
//
// Assert.assertTrue(mengeEquals(resultBuilder.getNestedPairMenge(),
// actualResult));
// }
//
// @Test
// public void unifyTestWildcards() {
//
// }
//
// private static boolean mengeEquals(Menge<Menge<Pair>> m1,
// Menge<Menge<Pair>> m2) {
// if (m1.size() != m2.size())
// return false;
//
// return containsAll(m1, m2) && containsAll(m2, m1);
// }
//
// private static boolean containsAll(Menge<Menge<Pair>> m1,
// Menge<Menge<Pair>> m2) {
// for (Menge<Pair> elem : m2)
// if (!contains(m1, elem))
// return false;
// return true;
// }
//
// private static boolean contains(Menge<Menge<Pair>> m1, Menge<Pair> m2) {
// for (Menge<Pair> elem : m1)
// if (mengePairEquals(elem, m2))
// return true;
// return false;
// }
//
// private static boolean mengePairEquals(Menge<Pair> m1, Menge<Pair> m2) {
// if (m1.size() != m2.size())
// return false;
//
// return containsAllPair(m1, m2) && containsAllPair(m2, m1);
// }
//
// private static boolean containsAllPair(Menge<Pair> m1, Menge<Pair> m2) {
// for (Pair elem : m1)
// if (contains(m2, elem))
// return true;
// return false;
// }
//
// private static boolean contains(Menge<Pair> m, Pair p) {
// for (Pair elem : m)
// if (pairEquals(elem, p))
// return true;
// return false;
//
// }
//
// private static boolean pairEquals(Pair p1, Pair p2) {
// return (p1.TA1.equals(p2.TA1) && p1.TA2.equals(p2.TA2))
// || (p1.TA1.equals(p2.TA2) && p1.TA2.equals(p2.TA1));
// }
} }

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
@ -242,6 +241,100 @@ public class UnifyTest extends Unify {
@Test @Test
public void unifyTestSimple() { public void unifyTestSimple() {
/*
* INIT
*/
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
Type number = tf.getSimpleType("Number");
Type object = tf.getSimpleType("Object");
Type integer = tf.getSimpleType("Integer");
Type doubl = tf.getSimpleType("Double");
fcb.add(number, object);
fcb.add(integer, number);
fcb.add(doubl, number);
IFiniteClosure fc = fcb.getCollectionExample();
/*
* Test 1:
*
* (Vector<a> <. Vector<? extends b>)
* (List<b> <. List<? extends Number>)
*/
Type tphA = tf.getPlaceholderType("a");
Type tphB = tf.getPlaceholderType("b");
Type extB = tf.getExtendsType(tphB);
Type extNum = tf.getExtendsType(number);
Set<MPair> eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", tphA), tf.getSimpleType("Vector", extB), PairOperator.SMALLERDOT));
eq.add(new MPair(tf.getSimpleType("List", tphB), tf.getSimpleType("List", extNum), PairOperator.SMALLERDOT));
Set<Set<MPair>> expected = new HashSet<>();
Set<Set<MPair>> actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 2:
*
* Vector<? extends a> <. List<? extends Number>
*
*/
Type extA = tf.getExtendsType(tphA);
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", extA), tf.getSimpleType("Vector", extNum), PairOperator.SMALLERDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 3:
*
* Vector<? extends Number> <. List<? extends a>
*
*/
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("Vector", extNum), tf.getSimpleType("Vector", extA), PairOperator.SMALLERDOT));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
/*
* Test 4:
*
* LinkedList <. Deque <. Queue <. Collection
*
* Vector<Number> <. List<a>
* List<a> <. AbstractList<b>
* ? extends Number <.? b
*/
eq = new HashSet<MPair>();
eq.add(new MPair(tf.getSimpleType("LinkedList", number), tf.getSimpleType("Deque", tphA), PairOperator.SMALLERDOT));
eq.add(new MPair(tf.getSimpleType("Deque", tphA), tf.getSimpleType("Queue", tphB), PairOperator.SMALLERDOT));
eq.add(new MPair(extNum, tphB, PairOperator.SMALLERDOTWC));
expected = new HashSet<>();
actual = unify(eq, fc);
System.out.println(actual);
//Assert.assertEquals(actual, expected);
} }