From d89d06797e17a7a95f007fe24e9b6133b3b13179 Mon Sep 17 00:00:00 2001 From: Florian Steurer Date: Thu, 31 Mar 2016 22:09:46 +0200 Subject: [PATCH] isInSolvedForm(), Unit-Tests, removed PairOp.Equals --- .../typeinference/unify/model/MPair.java | 3 - .../typeinference/unifynew/Mapping.java | 23 +- .../unifynew/MartelliMontanariUnify.java | 2 +- .../typeinference/unifynew/RuleSet.java | 11 +- .../typeinference/unifynew/Unify.java | 30 +- test/unify/UnifyOldTest.java | 585 ++++++++++-------- test/unify/UnifyTest.java | 95 ++- 7 files changed, 443 insertions(+), 306 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java index 6b740c46..60a3ff0d 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/MPair.java @@ -6,7 +6,6 @@ public class MPair { SMALLER, SMALLERDOT, SMALLERDOTWC, - EQUALS, EQUALSDOT; @Override @@ -18,8 +17,6 @@ public class MPair { return "<."; case SMALLERDOTWC: return "<.?"; - case EQUALS: - return "="; default: return "=."; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java index c8996dc8..9a80f75d 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Mapping.java @@ -56,32 +56,27 @@ public class Mapping { 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) { - /* - * TODO - * Warum hat der PairOp nur drei Werte? Wie wird SMALLERDOTWC etc im anderen Pair geregelt? - */ - + private de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator mapOp(de.dhbwstuttgart.typeinference.Pair.PairOperator op) { switch(op) { case Equal: - return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; + return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALSDOT; case Smaller: - return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLER; - case SmallerExtends: return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.SMALLERDOT; - default: - return de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator.EQUALS; + case SmallerExtends: + 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) { switch(op) { - case EQUALS: - return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal; case SMALLER: return de.dhbwstuttgart.typeinference.Pair.PairOperator.Smaller; 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: return de.dhbwstuttgart.typeinference.Pair.PairOperator.Equal; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java index e3ae2322..f6bc2ca5 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/MartelliMontanariUnify.java @@ -33,7 +33,7 @@ public class MartelliMontanariUnify implements IUnify { Type prev = iter.next(); while(iter.hasNext()) { Type next = iter.next(); - termsQ.add(new MPair(prev, next, PairOperator.EQUALS)); + termsQ.add(new MPair(prev, next, PairOperator.EQUALSDOT)); prev = next; } diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java index fde25609..d3f89104 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/RuleSet.java @@ -542,12 +542,12 @@ public class RuleSet implements IRuleSet{ typeMap.put(t2, typeMap.get(t2)+1); } - ArrayList result = new ArrayList(pairs); - + Queue result1 = new LinkedList(pairs); + ArrayList result = new ArrayList(); boolean applied = false; - for(int i = 0; i < result.size(); i++) { - MPair pair = result.get(i); + while(!result1.isEmpty()) { + MPair pair = result1.poll(); PlaceholderType lhsType = null; Type rhsType; @@ -560,8 +560,11 @@ public class RuleSet implements IRuleSet{ && !rhsType.getTypeParams().occurs(lhsType)) { Unifier uni = new Unifier(lhsType, rhsType); result = result.stream().map(uni::apply).collect(Collectors.toCollection(ArrayList::new)); + result1 = result1.stream().map(uni::apply).collect(Collectors.toCollection(LinkedList::new)); applied = true; } + + result.add(pair); } return applied ? Optional.of(new HashSet<>(result)) : Optional.empty(); diff --git a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java index 26a74729..5d2040c3 100644 --- a/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unifynew/Unify.java @@ -12,9 +12,6 @@ import java.util.Optional; import java.util.Set; 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.IRuleSet; import de.dhbwstuttgart.typeinference.unify.interfaces.ISetOperations; @@ -68,7 +65,7 @@ public class Unify { } // Add the set of [a =. Theta | (a=. Theta) in Eq2'] - Set bufferSet = eq2s.stream() + Set bufferSet = eq2s.stream() .filter(x -> x.getPairOp() == PairOperator.EQUALSDOT && x.getLhsType() instanceof PlaceholderType) .collect(Collectors.toSet()); @@ -133,17 +130,31 @@ public class Unify { * b) Build the union over everything */ - for(Set eqss : changed) { + for(Set eqss : changed) eqPrimePrimeSet.addAll(this.unify(eqss, fc)); - } - + /* * 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 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 applyTypeUnificationRules(Set eq, IFiniteClosure fc) { /* @@ -288,7 +299,8 @@ public class Unify { 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 unifyCase1(PlaceholderType a, Type thetaPrime, IFiniteClosure fc) { diff --git a/test/unify/UnifyOldTest.java b/test/unify/UnifyOldTest.java index 85f5ffd5..b93ba610 100644 --- a/test/unify/UnifyOldTest.java +++ b/test/unify/UnifyOldTest.java @@ -7,7 +7,9 @@ import org.junit.Test; import de.dhbwstuttgart.syntaxtree.factory.UnifyPairMengenBuilder; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; 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.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.typeinference.Pair; @@ -17,287 +19,322 @@ import de.dhbwstuttgart.typeinference.unify.Unify; public class UnifyOldTest { @Test - public void unifyTestSimpleTypes() { + public void unifyTest1() { // 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 <. Boolean - */ - - // Init Types - RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean"); - TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); - - // Expected Result - resultBuilder.clear(); - resultBuilder.addPair(aTph, boolT, PairOperator.Equal); - resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT), - PairOperator.Equal); - Menge> expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(aTph, boolT); - Menge> 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 buffer = resultBuilder.getPairMenge(); - expectedResult = new Menge>(); - 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)); + TypePlaceholder a = typeFactory.GetTypePlaceholder("a"); + 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); + 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 - 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 - */ - - 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> expectedResult = resultBuilder.getNestedPairMenge(); - - // Actual Result - assumptionBuilder.clear(); - assumptionBuilder.addPair(aTph, myType); - Menge> 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 - */ - - 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 <. List> - */ - } - - @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> 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> m1, - Menge> m2) { - if (m1.size() != m2.size()) - return false; - - return containsAll(m1, m2) && containsAll(m2, m1); - } - - private static boolean containsAll(Menge> m1, - Menge> m2) { - for (Menge elem : m2) - if (!contains(m1, elem)) - return false; - return true; - } - - private static boolean contains(Menge> m1, Menge m2) { - for (Menge elem : m1) - if (mengePairEquals(elem, m2)) - return true; - return false; - } - - private static boolean mengePairEquals(Menge m1, Menge m2) { - if (m1.size() != m2.size()) - return false; - - return containsAllPair(m1, m2) && containsAllPair(m2, m1); - } - - private static boolean containsAllPair(Menge m1, Menge m2) { - for (Pair elem : m1) - if (contains(m2, elem)) - return true; - return false; - } - - private static boolean contains(Menge 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)); - } +// +// @Test +// public void unifyTestSimpleTypes() { +// // 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 <. Boolean +// */ +// +// // Init Types +// RefType boolT = typeFactory.GetSimpleType("java.lang.Boolean"); +// TypePlaceholder aTph = typeFactory.GetTypePlaceholder("a"); +// +// // Expected Result +// resultBuilder.clear(); +// resultBuilder.addPair(aTph, boolT, PairOperator.Equal); +// resultBuilder.addPair(aTph, typeFactory.GetExtendsType(boolT), +// PairOperator.Equal); +// Menge> expectedResult = resultBuilder.getNestedPairMenge(); +// +// // Actual Result +// assumptionBuilder.clear(); +// assumptionBuilder.addPair(aTph, boolT); +// Menge> 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 buffer = resultBuilder.getPairMenge(); +// expectedResult = new Menge>(); +// 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 +// */ +// +// 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> expectedResult = resultBuilder.getNestedPairMenge(); +// +// // Actual Result +// assumptionBuilder.clear(); +// assumptionBuilder.addPair(aTph, myType); +// Menge> 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 +// */ +// +// 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 <. List> +// */ +// } +// +// @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> 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> m1, +// Menge> m2) { +// if (m1.size() != m2.size()) +// return false; +// +// return containsAll(m1, m2) && containsAll(m2, m1); +// } +// +// private static boolean containsAll(Menge> m1, +// Menge> m2) { +// for (Menge elem : m2) +// if (!contains(m1, elem)) +// return false; +// return true; +// } +// +// private static boolean contains(Menge> m1, Menge m2) { +// for (Menge elem : m1) +// if (mengePairEquals(elem, m2)) +// return true; +// return false; +// } +// +// private static boolean mengePairEquals(Menge m1, Menge m2) { +// if (m1.size() != m2.size()) +// return false; +// +// return containsAllPair(m1, m2) && containsAllPair(m2, m1); +// } +// +// private static boolean containsAllPair(Menge m1, Menge m2) { +// for (Pair elem : m1) +// if (contains(m2, elem)) +// return true; +// return false; +// } +// +// private static boolean contains(Menge 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)); +// } } diff --git a/test/unify/UnifyTest.java b/test/unify/UnifyTest.java index 2d2faad8..3c5792a3 100644 --- a/test/unify/UnifyTest.java +++ b/test/unify/UnifyTest.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import java.util.TreeSet; import java.util.stream.Collectors; import org.junit.Test; @@ -242,6 +241,100 @@ public class UnifyTest extends Unify { @Test 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 <. Vector) + * (List <. List) + */ + + Type tphA = tf.getPlaceholderType("a"); + Type tphB = tf.getPlaceholderType("b"); + Type extB = tf.getExtendsType(tphB); + Type extNum = tf.getExtendsType(number); + + Set eq = new HashSet(); + 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> expected = new HashSet<>(); + Set> actual = unify(eq, fc); + + System.out.println(actual); + //Assert.assertEquals(actual, expected); + + /* + * Test 2: + * + * Vector <. List + * + */ + + Type extA = tf.getExtendsType(tphA); + + eq = new HashSet(); + 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 <. List + * + */ + + eq = new HashSet(); + 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 <. List + * List <. AbstractList + * ? extends Number <.? b + */ + + eq = new HashSet(); + 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); + }