forked from i21017/JavaTypeUnify
Change super to subtype in ASP generation
This commit is contained in:
parent
cd017ba665
commit
2827098a27
@ -15,6 +15,16 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ConstraintParser {
|
||||
|
||||
public static NamedType replaceGenerics(NamedType in, Set<String> genericNames){
|
||||
List<Type> superParams = in.params().stream().map(p -> {
|
||||
if(genericNames.contains(p.name())){
|
||||
return (Type) new TypeParameter(p.name());
|
||||
}else{
|
||||
return replaceGenerics((NamedType) p, genericNames);
|
||||
}
|
||||
}).toList();
|
||||
return new NamedType(in.name(), superParams);
|
||||
}
|
||||
public static List<ExtendsRelation> parseExtendsRelations(String cons){
|
||||
CharStream input = CharStreams.fromString(cons);
|
||||
de.dhbwstuttgart.input.parser.ConstraintSetLexer lexer = new ConstraintSetLexer(input);
|
||||
@ -24,7 +34,11 @@ public class ConstraintParser {
|
||||
List<ExtendsRelation> ret = new ArrayList<>();
|
||||
for(var ext : conSet.extendsRelation()){
|
||||
if(ext.typeParams()!=null){
|
||||
ret.add(new ExtendsRelation(ext.IDENTIFIER().getText(), ext.typeParams().IDENTIFIER().stream().map(x -> new TypeParameter(x.getText())).toList(), (NamedType) parseType(ext.type())));
|
||||
var typeParams = ext.typeParams().IDENTIFIER().stream().map(x -> new TypeParameter(x.getText())).toList();
|
||||
var typeParamNames = typeParams.stream().map(tp -> tp.name()).collect(Collectors.toSet());
|
||||
NamedType parsedSuperType = (NamedType) parseType(ext.type());
|
||||
parsedSuperType = replaceGenerics(parsedSuperType, typeParamNames);
|
||||
ret.add(new ExtendsRelation(ext.IDENTIFIER().getText(), typeParams, parsedSuperType));
|
||||
}else{
|
||||
ret.add(new ExtendsRelation(ext.IDENTIFIER().getText(), List.of(), (NamedType) parseType(ext.type())));
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ public class ASPGenerator {
|
||||
String ret = "";
|
||||
for(var x : extendsRelations){
|
||||
NamedType subtype = new NamedType(x.subtypeName(), x.subtypeParams().stream().map(i -> (Type)i).toList());
|
||||
ret += "super("+subtype.toASP()+","+x.superType().toASP()+"):-super("+subtype.toASP()+").";
|
||||
ret += "subtype("+subtype.toASP()+","+x.superType().toASP()+"):-subtype("+subtype.toASP()+").";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dhbwstuttgart.sat.asp;
|
||||
|
||||
public interface Type {
|
||||
String name();
|
||||
String toASP();
|
||||
}
|
||||
|
@ -46,11 +46,11 @@ public class UnifyTest {
|
||||
"Vector<X> < List<X>," +
|
||||
"MyPair<X,Y> < Pair<X,X>," +
|
||||
"Pair<X,Y> < Object," +
|
||||
"List<X> < Object," +
|
||||
"Integer < Object, String < Object, " +
|
||||
"{List<Integer> <. _a | List<String> <. _a}{List<_c> <. _b | List<_c> <. _b} {List<Integer> <. _b | List<String> <. _b}"+
|
||||
"{List<Integer> <. _aa | List<String> <. _aa}{List<_c> <. _ba | List<_c> <. _ba} {List<Integer> <. _ba | List<String> <. _ba}"+
|
||||
"{List<Integer> <. _a | List<String> <. _aaa}{List<_c> <. _baa | List<_c> <. _baa} {List<Integer> <. _baa | List<String> <. _baa}";
|
||||
"List<X> < Object," +
|
||||
"Integer < Object, String < Object, " +
|
||||
"{List<Integer> <. _a | List<String> <. _a}{List<_c> <. _b, String <. Object | List<_c> <. _b} {Vector<Integer> <. _b | Vector<String> <. _b}"+
|
||||
"{List<Integer> <. _aa | List<String> <. _aa}{List<_c> <. _ba | List<_c> <. _ba} {Vector<Integer> <. _ba | Vector<String> <. _ba}"+
|
||||
"{List<Integer> <. _aaa | List<String> <. _aaa}{List<_c> <. _baa | List<_c> <. _baa} {Vector<Integer> <. _baa | Vector<String> <. _baa}";
|
||||
|
||||
System.out.println(ASPGenerator.generateASP(ConstraintParser.parse(input)));
|
||||
System.out.println(ASPGenerator.generateExtendsRelations(ConstraintParser.parseExtendsRelations(input)));
|
||||
|
Loading…
Reference in New Issue
Block a user