adaptExt + tests

This commit is contained in:
Florian Steurer 2015-11-15 15:06:34 +01:00
parent e447e1cd46
commit 480c7dca61
2 changed files with 106 additions and 3 deletions

View File

@ -348,8 +348,46 @@ public class RuleSet implements IRuleSet{
@Override
public Optional<MPair> adaptExt(MPair pair) {
// TODO Auto-generated method stub
return null;
if(pair.getPairOp() != PairOperator.SMALLERDOTWC)
return Optional.empty();
Type lhsType = pair.getLhsType();
if(!(lhsType instanceof SimpleType) && !(lhsType instanceof ExtendsType))
return Optional.empty();
Type rhsType = pair.getRhsType();
if(!(rhsType instanceof ExtendsType))
return Optional.empty();
if(lhsType.getTypeParams().size() == 0 || rhsType.getTypeParams().size() == 0)
return Optional.empty();
Type lhsFromFc;
if(lhsType instanceof SimpleType)
lhsFromFc = finiteClosure.getType(lhsType.getName());
else
lhsFromFc = new ExtendsType(finiteClosure.getType(((ExtendsType) lhsType).getExtendedType().getName()));
if(lhsFromFc == null)
return Optional.empty();
Set<Type> grArg = finiteClosure.grArg(lhsFromFc);
Optional<Type> opt = grArg.stream().filter(x -> x.getName().equals(rhsType.getName())).findAny();
if(!opt.isPresent())
return Optional.empty();
Type newLhs = ((ExtendsType) opt.get()).getExtendedType();
TypeParams lhsTypeParams = lhsType.getTypeParams();
TypeParams lhsFromFcTypeParams = lhsFromFc.getTypeParams();
Unifier unif = new Unifier(lhsFromFcTypeParams.get(0), lhsTypeParams.get(0));
for(int i = 1; i < lhsTypeParams.size(); i++)
unif.andThen(new Unifier(lhsFromFcTypeParams.get(i), lhsTypeParams.get(i)));
return Optional.of(new MPair(newLhs.apply(unif), rhsType, PairOperator.SMALLERDOTWC));
}
@Override

View File

@ -530,15 +530,26 @@ public class RuleSetTest {
IRuleSet rules = new RuleSet(fcb.getFiniteClosure());
/*
* Positive Tests
*/
SimpleType c1 = tf.getSimpleType("Type1", "String", "Double");
SimpleType c2 = tf.getSimpleType("Type2", "Object");
SimpleType c3 = tf.getSimpleType("Type3", "Object", "Number");
MPair pair1 = new MPair(c1, c2, PairOperator.SMALLERDOT);
MPair pair2 = new MPair(c1, c3, PairOperator.SMALLERDOT);
MPair pair2 = new MPair(c2, c3, PairOperator.SMALLERDOT);
MPair pair3 = new MPair(c1, c3, PairOperator.SMALLERDOT);
System.out.println("------ Adapt ------");
System.out.println(rules.adapt(pair1));
System.out.println(rules.adapt(pair2));
System.out.println(rules.adapt(pair3));
/*
* Negative Tests
*/
MPair noAdapt1 = new MPair(c2, c1, PairOperator.SMALLERDOT);
MPair noAdapt2 = new MPair(c1, c1, PairOperator.SMALLERDOT);
@ -551,7 +562,61 @@ public class RuleSetTest {
@Test
public void testAdaptExt() {
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
SimpleType t1 = tf.getSimpleType("Type1", "T", "U");
SimpleType t2 = tf.getSimpleType("Type2", "T");
SimpleType t3 = tf.getSimpleType("Type3", tf.getPlaceholderType("T"), tf.getSimpleType("Integer"));
fcb.add(t1, t2);
fcb.add(t2, t3);
IRuleSet rules = new RuleSet(fcb.getFiniteClosure());
/*
* Positive Tests
*/
SimpleType c1 = tf.getSimpleType("Type1", "String", "Double");
SimpleType c2 = tf.getSimpleType("Type2", "Object");
SimpleType c3 = tf.getSimpleType("Type3", "Object", "Number");
ExtendsType extc1 = new ExtendsType(c1);
ExtendsType extc2 = new ExtendsType(c2);
ExtendsType extc3 = new ExtendsType(c3);
MPair pair1 = new MPair(c1, extc2, PairOperator.SMALLERDOTWC);
MPair pair2 = new MPair(c2, extc3, PairOperator.SMALLERDOTWC);
MPair pair3 = new MPair(c1, extc3, PairOperator.SMALLERDOTWC);
MPair pair4 = new MPair(extc1, extc2, PairOperator.SMALLERDOTWC);
MPair pair5 = new MPair(extc2, extc3, PairOperator.SMALLERDOTWC);
MPair pair6 = new MPair(extc1, extc3, PairOperator.SMALLERDOTWC);
MPair pair7 = new MPair(extc1, extc1, PairOperator.SMALLERDOTWC);
System.out.println("------ AdaptExt ------");
System.out.println(rules.adaptExt(pair1));
System.out.println(rules.adaptExt(pair2));
System.out.println(rules.adaptExt(pair3));
System.out.println(rules.adaptExt(pair4));
System.out.println(rules.adaptExt(pair5));
System.out.println(rules.adaptExt(pair6));
System.out.println(rules.adaptExt(pair7));
/*
* Negative Tests
*/
MPair noAdapt1 = new MPair(extc2, extc1, PairOperator.SMALLERDOTWC);
MPair noAdapt2 = new MPair(extc1, c2, PairOperator.SMALLERDOTWC);
MPair noAdapt3 = new MPair(tf.getSuperType(c1), extc2, PairOperator.SMALLERDOTWC);
MPair noAdapt4 = new MPair(extc3, extc1, PairOperator.SMALLERDOTWC);
MPair noAdapt5 = new MPair(c1, extc2, PairOperator.SMALLERDOT);
Assert.assertFalse(rules.adaptExt(noAdapt1).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt2).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt3).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt4).isPresent());
Assert.assertFalse(rules.adaptExt(noAdapt5).isPresent());
}
@Test