finite closure greater smaller funktioniert jetzt auch mit wildcards

(muss noch getestet werden)
This commit is contained in:
Florian Steurer 2016-03-16 23:27:45 +01:00
parent 1762101330
commit d19a79bd63
2 changed files with 66 additions and 13 deletions

View File

@ -1,5 +1,7 @@
package de.dhbwstuttgart.typeinference.unify.model; package de.dhbwstuttgart.typeinference.unify.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
@ -57,12 +59,30 @@ public class FiniteClosure implements IFiniteClosure {
*/ */
@Override @Override
public Set<Type> smaller(Type type) { public Set<Type> smaller(Type type) {
if(!inheritanceGraph.containsKey(type)) Set<Type> result = inheritanceGraph.containsKey(type) ? inheritanceGraph.get(type).getContentOfDescendants() : new HashSet<>();
return new HashSet<>();
Set<Type> result = inheritanceGraph.get(type).getContentOfDescendants();
result.add(type); result.add(type);
if(type.getTypeParams().size() == 0)
return result;
ArrayList<Set<Type>> paramCandidates = new ArrayList<>();
for(Type param : type.getTypeParams()) {
if(param instanceof ExtendsType || param instanceof SuperType) {
Set<Type> pc = param.smArg(this);
paramCandidates.add(pc);
} else {
HashSet<Type> pc = new HashSet<>();
pc.add(param);
paramCandidates.add(pc);
}
}
Set<TypeParams> permResult = new HashSet<>();
permuteParams(paramCandidates, 0, permResult, new Type[paramCandidates.size()]);
for(TypeParams newParams : permResult)
result.add(type.setTypeParams(newParams));
return result; return result;
} }
@ -72,11 +92,30 @@ public class FiniteClosure implements IFiniteClosure {
*/ */
@Override @Override
public Set<Type> greater(Type type) { public Set<Type> greater(Type type) {
if(!inheritanceGraph.containsKey(type)) Set<Type> result = inheritanceGraph.containsKey(type) ? inheritanceGraph.get(type).getContentOfPredecessors() : new HashSet<>();
return new HashSet<>();
Set<Type> result = inheritanceGraph.get(type).getContentOfPredecessors();
result.add(type); result.add(type);
if(type.getTypeParams().size() == 0)
return result;
ArrayList<Set<Type>> paramCandidates = new ArrayList<>();
for(Type param : type.getTypeParams()) {
if(param instanceof ExtendsType || param instanceof SuperType) {
Set<Type> pc = param.grArg(this);
paramCandidates.add(pc);
} else {
HashSet<Type> pc = new HashSet<>();
pc.add(param);
paramCandidates.add(pc);
}
}
Set<TypeParams> permResult = new HashSet<>();
permuteParams(paramCandidates, 0, permResult, new Type[paramCandidates.size()]);
for(TypeParams newParams : permResult)
result.add(type.setTypeParams(newParams));
return result; return result;
} }
@ -213,4 +252,18 @@ public class FiniteClosure implements IFiniteClosure {
return new HashSet<>(); return new HashSet<>();
return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new)); return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new));
} }
protected void permuteParams(ArrayList<Set<Type>> candidates, int idx, Set<TypeParams> result, Type[] current) {
if(candidates.size() == idx) {
result.add(new TypeParams(Arrays.copyOf(current, current.length)));
return;
}
Set<Type> localCandidates = candidates.get(idx);
for(Type t : localCandidates) {
current[idx] = t;
permuteParams(candidates, idx+1, result, current);
}
}
} }

View File

@ -1,15 +1,11 @@
package unify; package unify;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test; import org.junit.Test;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.MPair; import de.dhbwstuttgart.typeinference.unify.model.MPair;
import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator; import de.dhbwstuttgart.typeinference.unify.model.MPair.PairOperator;
import de.dhbwstuttgart.typeinference.unifynew.TypeFactory; import de.dhbwstuttgart.typeinference.unifynew.TypeFactory;
import de.dhbwstuttgart.typeinference.unify.model.Type;
public class FiniteClosureTest { public class FiniteClosureTest {
@ -37,10 +33,14 @@ public class FiniteClosureTest {
@Test @Test
public void testSmaller() { public void testSmaller() {
IFiniteClosure fc = new FiniteClosureBuilder().getCollectionExample(); FiniteClosureBuilder fcb = new FiniteClosureBuilder();
TypeFactory tf = new TypeFactory(); TypeFactory tf = new TypeFactory();
fcb.add(tf.getSimpleType("Integer"), tf.getSimpleType("Number"));
IFiniteClosure fc = fcb.getCollectionExample();
System.out.println("\n\n----- Smaller Test -----"); System.out.println("\n\n----- Smaller Test -----");
System.out.println("Smaller(List<? extends Number>) = " + fc.smaller(tf.getSimpleType("List", tf.getExtendsType(tf.getSimpleType("Number")))));
System.out.println("Smaller(List<T>) = " + fc.smaller(tf.getSimpleType("List", "T"))); System.out.println("Smaller(List<T>) = " + fc.smaller(tf.getSimpleType("List", "T")));
System.out.println("Smaller(TreeSet<T>) = " + fc.smaller(tf.getSimpleType("TreeSet", "T"))); System.out.println("Smaller(TreeSet<T>) = " + fc.smaller(tf.getSimpleType("TreeSet", "T")));
System.out.println("Smaller(Collection) = " + fc.smaller(tf.getSimpleType("Collection"))); System.out.println("Smaller(Collection) = " + fc.smaller(tf.getSimpleType("Collection")));