factories, builder and first unit test

This commit is contained in:
Florian Steurer 2015-10-18 15:13:28 +02:00
parent 4fa160529d
commit 9e1f417280
5 changed files with 155 additions and 41 deletions

View File

@ -2,3 +2,10 @@
- Unify löst auch zirkuläre Abhängigkeiten a <. b <. c <. a
- Tests dazu?
- Prüfung im Builder?
- Unterschied Wildcard und FreshWildcard, ExtendsWildcard und FreshExtendsWildcard etc...
- FreshWildcard = TPH für Wildcards?
- Warum ist result von unify = Menge<Menge<Pair>> und nicht Menge<Pair>
- Menge Equals überarbeiten (Momentan Reihenfolgensensitiv)

View File

@ -0,0 +1,35 @@
package unify;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
public class UnifyPairMengenBuilder {
private Menge<Pair> assumptions = new Menge<Pair>();
public void addPair(Type t1, Type t2) {
assumptions.add(new Pair(t1, t2));
}
public void addPair(Type t1, Type t2, PairOperator pairOp) {
assumptions.add(new Pair(t1, t2, pairOp));
}
public Menge<Pair> getPairMenge() {
return assumptions;
}
public Menge<Menge<Pair>> getNestedPairMenge() {
Menge<Menge<Pair>> nested = new Menge<>();
for(Pair p : assumptions) {
Menge<Pair> m = new Menge<Pair>();
m.add(p);
nested.add(m);
}
return nested;
}
}

View File

@ -1,14 +1,14 @@
package unify;
import junit.framework.Assert;
import org.junit.Test;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.unify.FC_TTO;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
import de.dhbwstuttgart.typeinference.unify.Unify;
public class UnifyTest {
@ -16,52 +16,53 @@ public class UnifyTest {
@Test
public void unifyTestSimpleTypes() {
/*
* Input
*/
Type tph1 = TypePlaceholder.backdoorCreate("b");
Type tph2 = TypePlaceholder.backdoorCreate("a");
RefType rt1 = new RefType("java.lang.Boolean", null, 0);
RefType rt2 = new RefType("java.lang.Integer", null, 0);
Menge<Pair> assumptions = new Menge<Pair>();
// b <. Boolean <. a <. Integer
assumptions.add(new Pair(tph1, rt1));
assumptions.add(new Pair(rt1, tph2));
assumptions.add(new Pair(tph2, rt2));
/*
* FC TTO
*/
Menge<Pair> fc = new Menge<Pair>();
// String <. Boolean <. Integer
fc.add(new Pair(new RefType("java.lang.String", null, 0), new RefType("java.lang.Boolean", null, 0)));
fc.add(new Pair(new RefType("java.lang.Boolean", null, 0), new RefType("java.lang.Integer", null, 0)));
Menge tto = (Menge) fc.clone();
Menge<Class> classes = new Menge<Class>();
classes.add(new Class("java.lang.Boolean", 0));
classes.add(new Class("java.lang.String", 0));
classes.add(new Class("java.lang.Integer", 0));
// Was ist tto und classes?
FC_TTO fctto = new FC_TTO(fc, tto, null);
System.out.println(Unify.unify(assumptions, fctto));
}
@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() {
}
protected boolean mengeEquals(Menge<Menge<Pair>> m1, Menge<Menge<Pair>> m2) {
return m1.containsAll(m2) && m2.containsAll(m1);
}
}

View File

@ -0,0 +1,43 @@
package unify;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.ObjectType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.Pair.PairOperator;
public class UnifyTypeFactory {
public RefType GetSimpleType(String name, Type... parameters) {
if(parameters.length == 0)
return new RefType(name, null, 0);
Menge<Type> typeParams = new Menge<Type>();
for(Type t : parameters)
typeParams.add(t);
return new RefType(name, typeParams, null, 0);
}
public ExtendsWildcardType GetExtendsType(ObjectType extendedType) {
return new ExtendsWildcardType(extendedType);
}
public SuperWildcardType GetSuperType(ObjectType superedType) {
return new SuperWildcardType(superedType);
}
public WildcardType GetWildcardType() {
return new WildcardType(null, null, 0);
}
public TypePlaceholder GetTypePlaceholder(String name) {
return TypePlaceholder.backdoorCreate(name);
}
}

View File

@ -0,0 +1,28 @@
package unify;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.Pair;
import de.dhbwstuttgart.typeinference.unify.FC_TTO;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
public class Unify_FC_TTO_Builder {
private Menge<Pair> fc = new Menge<Pair>();
private Menge<Class> classes = new Menge<Class>();
public void AddInheritance(Type t1, Type t2) {
if(t1 instanceof RefType)
classes.add(new Class(t1.get_Name(), t1.getOffset()));
if(t2 instanceof RefType)
classes.add(new Class(t2.get_Name(), t2.getOffset()));
fc.add(new Pair(t1, t2));
}
public FC_TTO Get_FC_TTO() {
return new FC_TTO(fc, (Menge<?>) fc.clone(), classes);
}
}