1
0

Change super to subtype in ASP generation

This commit is contained in:
JanUlrich 2024-06-27 21:52:36 +02:00
parent cd017ba665
commit 2827098a27
4 changed files with 22 additions and 7 deletions

View File

@ -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())));
}

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.sat.asp;
public interface Type {
String name();
String toASP();
}

View File

@ -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)));