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

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