diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index e8d74cb6..8d381e37 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -102,8 +102,8 @@ public class JavaTXCompiler { System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); - System.out.println("RESULT: " + result); - results.addAll(result); + System.out.println("RESULT: " + result.size()); + //results.addAll(result); } return results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 7863c8ed..ea2b17a6 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -11,6 +12,7 @@ import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.model.*; import java.util.*; +import java.util.stream.Collectors; public class FCGenerator { /** @@ -19,12 +21,15 @@ public class FCGenerator { * * @param availableClasses - Alle geparsten Klassen */ - public static Set toFC(Collection availableClasses) throws ClassNotFoundException { - HashSet pairs = new HashSet<>(); + public static Set toUnifyFC(Collection availableClasses) throws ClassNotFoundException { + return toFC(availableClasses).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet()); + } + + public static Set toFC(Collection availableClasses) throws ClassNotFoundException { + HashSet pairs = new HashSet<>(); for(ClassOrInterface cly : availableClasses){ pairs.addAll(getSuperTypes(cly, availableClasses)); } - System.out.println(pairs); return pairs; } @@ -35,20 +40,20 @@ public class FCGenerator { * @param forType * @return */ - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { return getSuperTypes(forType, availableClasses, new HashMap<>()); } //TODO: implements Interface auch als superklassen beachten - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { - List params = new ArrayList<>(); + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { + List params = new ArrayList<>(); //Die GTVs, die in forType hinzukommen: - HashMap newGTVs = new HashMap<>(); + HashMap newGTVs = new HashMap<>(); //Generics mit gleichem Namen müssen den selben TPH bekommen for(GenericTypeVar gtv : forType.getGenerics()){ if(!gtvs.containsKey(gtv.getParsedName())){ - gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); - newGTVs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + gtvs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); + newGTVs.put(gtv.getParsedName(), TypePlaceholder.fresh(new NullToken())); } params.add(gtvs.get(gtv.getParsedName())); } @@ -73,27 +78,26 @@ public class FCGenerator { while(itGenParams.hasNext()){ RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: - UnifyType setSetType = setType.acceptTV(new TypeExchanger(gtvs)); + RefTypeOrTPHOrWildcardOrGeneric setSetType = setType.acceptTV(new TypeExchanger(gtvs)); newGTVs.put(itGenParams.next().getParsedName(), setSetType); } - UnifyType superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); + RefTypeOrTPHOrWildcardOrGeneric superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); - TypeParams paramList = new TypeParams(params); - UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); - UnifyType t2 = superType; + RefTypeOrTPHOrWildcardOrGeneric t1 = new RefType(forType.getClassName(), params, new NullToken()); + RefTypeOrTPHOrWildcardOrGeneric t2 = superType; - UnifyPair ret = UnifyTypeFactory.generateSmallerPair(t1, t2); + Pair ret = new Pair(t1, t2, PairOperator.SMALLER); - List superTypes; + List superTypes; //Rekursiver Aufruf. Abbruchbedingung ist Object als Superklasse: if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ - superTypes = Arrays.asList(UnifyTypeFactory.generateSmallerPair(UnifyTypeFactory.convert(ASTFactory.createObjectType()), UnifyTypeFactory.convert(ASTFactory.createObjectType()))); + superTypes = Arrays.asList(new Pair(ASTFactory.createObjectType(), ASTFactory.createObjectType(), PairOperator.SMALLER)); }else{ superTypes = getSuperTypes(superClass, availableClasses, newGTVs); } - List retList = new ArrayList<>(); + List retList = new ArrayList<>(); retList.add(ret); retList.addAll(superTypes); @@ -103,42 +107,41 @@ public class FCGenerator { /** * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. */ - private static class TypeExchanger implements TypeVisitor{ + private static class TypeExchanger implements TypeVisitor{ - private final HashMap gtvs; + private final HashMap gtvs; - TypeExchanger(HashMap gtvs){ + TypeExchanger(HashMap gtvs){ this.gtvs = gtvs; } @Override - public UnifyType visit(RefType refType) { - List params = new ArrayList<>(); + public RefTypeOrTPHOrWildcardOrGeneric visit(RefType refType) { + List params = new ArrayList<>(); for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ params.add(param.acceptTV(this)); } - TypeParams paramList = new TypeParams(params); - UnifyType ret = new ReferenceType(refType.getName().toString(), paramList); + RefTypeOrTPHOrWildcardOrGeneric ret = new RefType(refType.getName(), params, new NullToken()); return ret; } @Override - public UnifyType visit(SuperWildcardType superWildcardType) { + public RefTypeOrTPHOrWildcardOrGeneric visit(SuperWildcardType superWildcardType) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(TypePlaceholder typePlaceholder) { + public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(ExtendsWildcardType extendsWildcardType) { + public RefTypeOrTPHOrWildcardOrGeneric visit(ExtendsWildcardType extendsWildcardType) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(GenericRefType genericRefType) { + public RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType) { if(! gtvs.containsKey(genericRefType.getParsedName())) throw new DebugException("Dieser Fall darf nicht auftreten"); //TODO: Diesen Dirty-Hack beseitigen. Fehler tritt bei java.lang.invoke.LambdaFormEditor$Transform$Kind auf. diff --git a/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java new file mode 100644 index 00000000..0497b09a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java @@ -0,0 +1,17 @@ +package de.dhbwstuttgart.sat.asp; + +import de.dhbwstuttgart.parser.scope.JavaClassName; + +public class ASPStringConverter { + public static String toConstant(JavaClassName name){ + return toConstant(name.toString().replace(".", "_")); + } + + public static String toConstant(String name){ + return "c" + name.toString().replace(".", "_"); + } + + public static String fromConstant(String s){ + return s.replace("_", ".").substring(1); + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index 85f6dc56..ed1e9f40 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -17,16 +17,20 @@ public class Clingo { private static final List programFiles = new ArrayList<>(); static{ programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/basis.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/finiteclosure.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp")); - programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce1.lp")); - programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce2.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp")); } public Clingo(List inputFiles){ this.input = inputFiles; } + /* + TODO: Clingo per Java Wrapper https://stackoverflow.com/questions/3356200/using-java-to-wrap-over-c + */ public String runClingo() throws IOException, InterruptedException { String pathToClingo = "/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/clingo-5.2.1-linux-x86_64/clingo"; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java index 8d4d73f9..24ed9269 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -9,8 +9,8 @@ public enum ASPRule { ASP_PARAMLISTNUMERATION_NAME("paramNum"), ASP_PARAMLIST_END_POINTER("null"), ASP_TYPE("type"), - ASP_FCTYPE("type") - ; + ASP_FCTYPE("typeFC"), + ASP_TYPE_VAR("typeVar"); private final String text; diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index c7be7dbc..b3534f47 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -1,17 +1,187 @@ package de.dhbwstuttgart.sat.asp.parser; -import de.dhbwstuttgart.typeinference.result.ResultPair; -import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.model.ASPParameterList; +import de.dhbwstuttgart.sat.asp.writer.model.ASPRefType; +import de.dhbwstuttgart.sat.asp.writer.model.ASPType; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.result.*; -import java.util.HashSet; -import java.util.Set; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; +import java.io.StringReader; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +/** + * Ablauf: + * - Erst werden alle benötigten Statements eingelesen. Die Pointer bleiben als Strings erhalten + * - Anschließend müssen diese wieder zu einem Unify Ergebnis zurückgewandelt werden + * - Hier nicht die Typen aus dem unify.model packages verwenden. + * TODO: Überlegen welche Informationen noch nach der Unifizierung gebraucht werden + * -> Eigentlich nur die korrekten Namen der Typen und TPHs + */ public class ASPParser { - ResultSet parse(String result){ - Set ret = new HashSet<>(); - for(String pair : result.split(",")){ + private final Collection originalTPHs; + private ResultSet resultSet; + private Map types = new HashMap<>(); + private Set tphs = new HashSet<>(); + private Map parameterLists = new HashMap<>(); + /** + * Parst clingo output welcher als JSON (option --outf=2) ausgibt + * @param toParse + * @return + */ + public static ResultSet parse(String toParse, Collection oldPlaceholders){ + return new ASPParser(toParse, oldPlaceholders).resultSet; + } + + + private ASPParser(String toParse, Collection oldPlaceholders){ + this.originalTPHs = oldPlaceholders; + JsonObject jsonResult = Json.createReader(new StringReader(toParse)).readObject(); + JsonArray results = jsonResult.getJsonArray("Call").getJsonObject(0). + getJsonArray("Witnesses").getJsonObject(0). + getJsonArray("Value"); + + /* + Diese Funktion läuft im folgenden mehrmals über das Result aus dem ASP Programm. + Dabei werden Schritt für Schritt die Felder dieser Klasse befüllt die am Schluss das ResultSet ergeben + */ + Set ret = new HashSet<>(); + //Zuerst die params und typeVars: + for(int i = 0; i params = new ArrayList<>(); + ParsedType t = types.get(name); + for(String param : t.params){ + params.add(this.getType(param)); + } + return new RefType(new JavaClassName(ASPStringConverter.fromConstant(t.name)), params, new NullToken()); + }else throw new DebugException("Der Typ " + name + " konnte nicht bestimmt werden"); + } + + private static class ParameterListNode{ + final String type; + final String nextNode; + + public ParameterListNode(String type, String next) { + this.type = type; + this.nextNode = next; + } + } + private void parseParameter(String statement){ + Pattern p = Pattern.compile(ASPRule.ASP_PARAMLIST_NAME+"\\(([^,]+),([^,]+),([^,]+)\\)"); + Matcher m = p.matcher(statement); + boolean b = m.matches(); + if(b){ + if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); + String pointer = m.group(1); + String type = m.group(2); + String next = m.group(2); + if(next.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))next = null; + if(this.parameterLists.containsKey(pointer)){ + throw new DebugException("Fehler in Ergebnisparsen"); + } + this.parameterLists.put(pointer,new ParameterListNode(type, next)); + } + } + + private void parseTypeVar(String statement){ + Pattern p = Pattern.compile(ASPRule.ASP_TYPE_VAR+"\\(([^,]+)\\)"); + Matcher m = p.matcher(statement); + boolean b = m.matches(); + if(b){ + if(m.groupCount()<1)throw new DebugException("Fehler in Regex"); + String name = m.group(1); + this.tphs.add(name); + } + } + + private void parseType(String statement){ + Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+)\\)"); + Matcher m = p.matcher(statement); + boolean b = m.matches(); + if(b){ + if(m.groupCount()<2)throw new DebugException("Fehler in Regex"); + String ls = m.group(1); + String rs = m.group(2); + List params = this.getParams(rs); + this.types.put(ls,new ParsedType(ls, params)); + } + } + + private List getParams(String pointer) { + List params = new ArrayList<>(); + if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params; + while(pointer != null){ + if(!parameterLists.containsKey(pointer)) + throw new DebugException("Fehler in Ergebnisparsen"); + ParameterListNode param = parameterLists.get(pointer); + pointer = param.nextNode; + params.add(param.type); + } + return params; + //Optional ret = parameterLists.stream().filter(aspParameterList -> aspParameterList.name.equals(rs)).findAny(); + //return ret.get(); } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java deleted file mode 100644 index 614bddb0..00000000 --- a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.sat.asp.parser.model; - -public class ParsedASPStatement { - public ParsedASPStatement(String statement){ - - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java new file mode 100644 index 00000000..c49ce831 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.sat.asp.parser.model; + +import java.util.List; + +public class ParsedType { + public final String name; + public final List params; + public ParsedType(String name, List params){ + this.name = name; + this.params = params; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index ea162c11..b1483423 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -1,7 +1,9 @@ package de.dhbwstuttgart.sat.asp.writer; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -19,25 +21,24 @@ public class ASPGenerator { ASPWriter writer = new ASPWriter(); private final String asp; - public ASPGenerator(ConstraintSet constraints, Collection fcClasses){ + public ASPGenerator(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException { List> constraints1 = constraints.cartesianProduct().iterator().next(); List constraintPairs = new ArrayList<>(); for(Constraint constraint : constraints1){ - System.out.println(UnifyTypeFactory.convert(constraint)); constraintPairs.addAll(constraint); } - asp = toASP(constraintPairs, fcClasses); + asp = toASP(constraintPairs, FCGenerator.toFC(fcClasses)); } public String getASP(){ return asp; } - private String toASP(List constraintSet, Collection fcClasses){ + private String toASP(List constraintSet, Collection fc){ TypeConverter converter = new TypeConverter(); - for(ClassOrInterface cl : fcClasses){ - ASPRefType superClass = (ASPRefType) cl.getSuperClass().acceptTV(converter); - ASPPairSmaller fcEntry = new ASPPairSmaller(new ASPFCType(convert(cl)), new ASPFCType(superClass), writer); + for(Pair fcp : fc){ + //Wenn dieser Cast fehlschlägt stimmt etwas nicht. Alle Paare in der FC müssen smaller Operatoren haen + ASPPairSmaller fcEntry = (ASPPairSmaller) convert(fcp); writer.add(new ASPStatement(fcEntry.toASP())); } for(Pair cons : constraintSet){ @@ -55,24 +56,19 @@ public class ASPGenerator { return new ASPPairEquals(ls, rs,writer); }else if(pair.OperatorSmallerDot()){ return new ASPPairSmallerDot(ls, rs, writer); + }else if(pair.OperatorSmaller()){ + //Diese Cast müssen auch immer funktionieren, da in smaller Constraints nur RefTypes vorkommen + return new ASPPairSmaller(new ASPFCType((ASPRefType) ls), new ASPFCType((ASPRefType) rs), writer); }else throw new NotImplementedException(); } private ASPRefType convert(ClassOrInterface cl){ List paramList = new ArrayList<>(); for(GenericTypeVar gtv : cl.getGenerics()){ - paramList.add(new ASPGenericType(toConstant(gtv.getName()))); + paramList.add(new ASPGenericType(ASPStringConverter.toConstant(gtv.getName()))); } ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(toConstant(cl.getClassName()), params); - } - - public static String toConstant(JavaClassName name){ - return toConstant(name.toString().replace(".", "_")); - } - - public static String toConstant(String name){ - return "c" + name.toString().replace(".", "_"); + return new ASPRefType(ASPStringConverter.toConstant(cl.getClassName()), params); } private class TypeConverter implements TypeVisitor{ @@ -84,7 +80,7 @@ public class ASPGenerator { paramList.add(gtv.acceptTV(this)); } ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(toConstant(type.getName()), params); + return new ASPRefType(ASPStringConverter.toConstant(type.getName()), params); } @Override @@ -94,7 +90,7 @@ public class ASPGenerator { @Override public ASPType visit(TypePlaceholder typePlaceholder) { - return new ASPTypeVar(toConstant(typePlaceholder.getName())); + return new ASPTypeVar(ASPStringConverter.toConstant(typePlaceholder.getName())); } @Override @@ -104,7 +100,7 @@ public class ASPGenerator { @Override public ASPType visit(GenericRefType genericRefType) { - return new ASPRefType(toConstant(genericRefType.getName()), + return new ASPRefType(ASPStringConverter.toConstant(genericRefType.getName()), new ASPParameterList(new ArrayList<>(), writer)); } } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java index 3c176e49..178bb8fa 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.sat.asp.writer.model; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.sat.asp.writer.ASPWriter; @@ -30,13 +31,12 @@ public class ASPParameterList { writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); paramNum++; - //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } } } private String newName() { - return ASPGenerator.toConstant(NameGenerator.makeNewName()); + return ASPStringConverter.toConstant(NameGenerator.makeNewName()); } public String toString(){ diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java index 8b3c1af6..24b33eaf 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.sat.asp.writer.model; +import de.dhbwstuttgart.sat.asp.model.ASPRule; + public class ASPTypeVar implements ASPType{ private final String name; @@ -9,7 +11,7 @@ public class ASPTypeVar implements ASPType{ @Override public String toString() { - return "typeVar("+ name +")"; + return ASPRule.ASP_TYPE_VAR+"("+ name +")"; } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index a962c2bc..43fcf1b4 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -3,13 +3,11 @@ package de.dhbwstuttgart.syntaxtree.factory; import java.util.*; import java.util.stream.Collectors; -import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.WildcardType; @@ -35,7 +33,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - return new FiniteClosure(FCGenerator.toFC(fromClasses)); + return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses)); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ @@ -129,10 +127,13 @@ public class UnifyTypeFactory { UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1) , UnifyTypeFactory.convert(p.TA2)); return ret; - }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)) { UnifyPair ret = generateEqualDotPair(UnifyTypeFactory.convert(p.TA1) , UnifyTypeFactory.convert(p.TA2)); return ret; + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + return generateSmallerPair(UnifyTypeFactory.convert(p.TA1), + UnifyTypeFactory.convert(p.TA2)); }else throw new NotImplementedException(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index f7db0481..bb631e04 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -13,16 +13,17 @@ public class BinaryExpr extends Expression } public enum Operator{ - ADD, - SUB, - MUL, - AND, - OR, - DIV, - LESSTHAN, - BIGGERTHAN, - LESSEQUAL, - BIGGEREQUAL + ADD, // + + SUB, // - + MUL, // * + MOD, // Modulo Operator % + AND, // && + OR, // || + DIV, // / + LESSTHAN, // < + BIGGERTHAN, // > + LESSEQUAL, // <= + BIGGEREQUAL // >= } public final Operator operation; diff --git a/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java b/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java index 64d0ce0b..c24fe211 100644 --- a/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java +++ b/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java @@ -9,7 +9,7 @@ public class PairTPHEqualTPH extends ResultPair numericAdditionOrStringConcatenation = new HashSet<>(); + Constraint numeric = new Constraint<>(); + //Zuerst der Fall für Numerische AusdrücPairOpnumericeratorke, das sind Mul, Mod und Div immer: + //see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17 + //Expression muss zu Numeric Convertierbar sein. also von Numeric erben + numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); + numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); + //The type of a multiplicative expression is the promoted type of its operands. + numeric.add(new Pair(binary.rexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); + numeric.add(new Pair(binary.lexpr.getType(), binary.getType(), PairOperator.SMALLERDOT)); + numericAdditionOrStringConcatenation.add(numeric); + if(binary.operation.equals(BinaryExpr.Operator.ADD)) { + //Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2) + Constraint stringConcat = new Constraint<>(); + stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT)); + stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT)); + stringConcat.add(new Pair(binary.getType(), string, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(stringConcat); + } + constraintsSet.addOderConstraint(numericAdditionOrStringConcatenation); + }else { + throw new NotImplementedException(); + } + } + @Override public void visit(Literal literal) { - //Nothing to do here. Literale kriegen beim parsen den korrekten Typ. + //Nothing to do here. Literale erzeugen keine Constraints } @Override diff --git a/src/de/dhbwstuttgart/typeinference/unify/Match.java b/src/de/dhbwstuttgart/typeinference/unify/Match.java new file mode 100644 index 00000000..78f7360f --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/Match.java @@ -0,0 +1,89 @@ +package de.dhbwstuttgart.typeinference.unify; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import de.dhbwstuttgart.typeinference.unify.interfaces.IMatch; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; + +/** + * Implementation of match derived from unification algorithm. + * @author Martin Pluemicke + */ +public class Match implements IMatch { + + @Override + public Optional match(ArrayList termsList) { + + // Start with the identity unifier. Substitutions will be added later. + Unifier mgu = Unifier.identity(); + + // Apply rules while possible + int idx = 0; + while(idx < termsList.size()) { + UnifyPair pair = termsList.get(idx); + UnifyType rhsType = pair.getRhsType(); + UnifyType lhsType = pair.getLhsType(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); + + // REDUCE - Rule + if(!(rhsType instanceof PlaceholderType) && !(lhsType instanceof PlaceholderType)) { + Set result = new HashSet<>(); + + // f<...> = g<...> with f != g are not unifiable + if(!rhsType.getName().equals(lhsType.getName())) + return Optional.empty(); // conflict + // f = f are not unifiable + if(rhsTypeParams.size() != lhsTypeParams.size()) + return Optional.empty(); // conflict + // f = g is not unifiable (cannot be f = f because erase rule would have been applied) + //if(rhsTypeParams.size() == 0) + //return Optional.empty(); + + // Unpack the arguments + for(int i = 0; i < rhsTypeParams.size(); i++) + result.add(new UnifyPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); + + termsList.remove(idx); + termsList.addAll(result); + continue; + } + + // DELETE - Rule + if(pair.getRhsType().equals(pair.getLhsType())) { + termsList.remove(idx); + continue; + } + + // SWAP - Rule + if(!(lhsType instanceof PlaceholderType) && (rhsType instanceof PlaceholderType)) { + return Optional.empty(); // conflict + } + + // OCCURS-CHECK + //deleted + + // SUBST - Rule + if(lhsType instanceof PlaceholderType) { + mgu.add((PlaceholderType) lhsType, rhsType); + termsList = termsList.stream().map(mgu::applyleft).collect(Collectors.toCollection(ArrayList::new)); + idx = idx+1 == termsList.size() ? 0 : idx+1; + continue; + } + + idx++; + } + + return Optional.of(mgu); + } +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 2005a387..e51679e2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -34,6 +34,7 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class TypeUnifyTask extends RecursiveTask>> { private static final long serialVersionUID = 1L; + private static int i = 0; /** * The implementation of setOps that will be used during the unification @@ -77,6 +78,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ + //System.out.println("Unifikation: " + eq); Set eq0 = applyTypeUnificationRules(eq, fc); /* @@ -97,6 +99,8 @@ public class TypeUnifyTask extends RecursiveTask>> { // cartesian product of the sets created by pattern matching. List>> topLevelSets = new ArrayList<>(); + System.out.println(eq2s); + if(eq1s.size() != 0) { // Do not add empty sets or the cartesian product will always be empty. Set> wrap = new HashSet<>(); wrap.add(eq1s); @@ -134,12 +138,16 @@ public class TypeUnifyTask extends RecursiveTask>> { // Sub cartesian products of the second level (pattern matched) sets // "the big (x)" for(Set>> secondLevelSet : secondLevelSets) { + //System.out.println("secondLevelSet "+secondLevelSet.size()); List>> secondLevelSetList = new ArrayList<>(secondLevelSet); Set>> cartResult = setOps.cartesianProduct(secondLevelSetList); - + //System.out.println("CardResult: "+cartResult.size()); // Flatten and add to top level sets Set> flat = new HashSet<>(); + int j = 0; for(List> s : cartResult) { + j++; + //System.out.println("s from CardResult: "+cartResult.size() + " " + j); Set flat1 = new HashSet<>(); for(Set s1 : s) flat1.addAll(s1); @@ -164,6 +172,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 5: Substitution */ + System.out.println("vor Subst: " + eqPrime); Optional> eqPrimePrime = rules.subst(eqPrime); /* @@ -176,11 +185,13 @@ public class TypeUnifyTask extends RecursiveTask>> { //(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt. eqPrimePrimeSet.add(eqPrime); else if(eqPrimePrime.isPresent()) { + System.out.println("nextStep: " + eqPrimePrime.get()); TypeUnifyTask fork = new TypeUnifyTask(eqPrimePrime.get(), fc, true); forks.add(fork); fork.fork(); } else { + System.out.println("nextStep: " + eqPrime); TypeUnifyTask fork = new TypeUnifyTask(eqPrime, fc, true); forks.add(fork); fork.fork(); @@ -381,9 +392,12 @@ public class TypeUnifyTask extends RecursiveTask>> { UnifyType rhsType = pair.getRhsType(); // Case 1: (a <. Theta') - if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) - result.get(0).add(unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc)); - + if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { + System.out.println(pair); + Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc); + System.out.println(x1); + result.get(0).add(x1); + } // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType) result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), fc)); @@ -442,13 +456,24 @@ public class TypeUnifyTask extends RecursiveTask>> { break; } - Set cs = fc.getAllTypesByName(thetaPrime.getName()); - cs.add(thetaPrime); + Set cs = fc.getAllTypesByName(thetaPrime.getName());//cs= [java.util.Vector, java.util.Vector>, ????java.util.Vector???] + + //PL 18-02-06 entfernt, kommt durch unify wieder rein + //cs.add(thetaPrime); + //PL 18-02-06 entfernt for(UnifyType c : cs) { - Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); - thetaQs.add(thetaPrime); //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert, - //da children offensichtlich ein echtes kleiner und kein kleinergleich ist + //PL 18-02-05 getChildren durch smaller ersetzt in getChildren werden die Varianlen nicht ersetzt. + Set thetaQs = fc.smaller(c).stream().collect(Collectors.toCollection(HashSet::new)); + //Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); + //thetaQs.add(thetaPrime); //PL 18-02-05 wieder geloescht + //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert, + //da children offensichtlich ein echtes kleiner und kein kleinergleich ist + + //PL 18-02-06: eingefuegt, thetaQs der Form V> <. V'> werden entfernt + thetaQs = thetaQs.stream().filter(ut -> ut.getTypeParams().arePlaceholders()).collect(Collectors.toCollection(HashSet::new)); + //PL 18-02-06: eingefuegt + Set thetaQPrimes = new HashSet<>(); TypeParams cParams = c.getTypeParams(); if(cParams.size() == 0) @@ -464,6 +489,11 @@ public class TypeUnifyTask extends RecursiveTask>> { } for(UnifyType tqp : thetaQPrimes) { + //System.out.println(tqp.toString()); + //i++; + //System.out.println(i); + //if (i == 62) + // System.out.println(tqp.toString()); Optional opt = stdUnify.unify(tqp, thetaPrime); if (!opt.isPresent()) continue; @@ -474,10 +504,11 @@ public class TypeUnifyTask extends RecursiveTask>> { for (Entry sigma : unifier) substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); - List freshTphs = new ArrayList<>(); + //List freshTphs = new ArrayList<>(); PL 18-02-06 in die For-Schleife verschoben for (UnifyType tq : thetaQs) { - Set smaller = fc.smaller(unifier.apply(tq)); + Set smaller = fc.smaller(unifier.apply(tq)); for(UnifyType theta : smaller) { + List freshTphs = new ArrayList<>(); Set resultPrime = new HashSet<>(); for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java new file mode 100644 index 00000000..5e201dd7 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java @@ -0,0 +1,35 @@ +package de.dhbwstuttgart.typeinference.unify.interfaces; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +/** + * Match + * @author Martin Pluemicke + */ +public interface IMatch { + + /** + * Finds the most general unifier sigma of the set {t1,...,tn} so that + * sigma(t1) = sigma(t2) = ... = sigma(tn). + * @param terms The set of terms to be unified + * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. + */ + public Optional match(ArrayList termsList); + + /** + * Finds the most general unifier sigma of the set {t1,...,tn} so that + * sigma(t1) = sigma(t2) = ... = sigma(tn). + * @param terms The set of terms to be unified + * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. + */ + + +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index aba3d3f6..dcffbee6 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -8,7 +8,11 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +//PL 18-02-05 Unifier durch Matcher ersetzt +//mus greater noch erstezt werden import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; + +import de.dhbwstuttgart.typeinference.unify.Match; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; @@ -46,6 +50,13 @@ public class FiniteClosure implements IFiniteClosure { if(pair.getPairOp() != PairOperator.SMALLER) continue; + //VERSUCH PL 18-02-06 + //koennte ggf. die FC reduzieren + //TO DO: Ueberpruefen, ob das sinnvll und korrekt ist + //if (!pair.getLhsType().getTypeParams().arePlaceholders() + // && !pair.getRhsType().getTypeParams().arePlaceholders()) + // continue; +; // Add nodes if not already in the graph if(!inheritanceGraph.containsKey(pair.getLhsType())) inheritanceGraph.put(pair.getLhsType(), new Node(pair.getLhsType())); @@ -93,7 +104,9 @@ public class FiniteClosure implements IFiniteClosure { private Set computeSmaller(Set types) { HashSet result = new HashSet<>(); - IUnify unify = new MartelliMontanariUnify(); + //PL 18-02-05 Unifier durch Matcher ersetzt + //IUnify unify = new MartelliMontanariUnify(); + Match match = new Match(); for(UnifyType t : types) { @@ -112,10 +125,16 @@ public class FiniteClosure implements IFiniteClosure { continue; // if T <* T' then sigma(T) <* sigma(T') - Set> candidates = strInheritanceGraph.get(t.getName()); + Set> candidates = strInheritanceGraph.get(t.getName()); //cadidates= [???Node(java.util.Vector>)??? + // , Node(java.util.Vector) + //] for(Node candidate : candidates) { UnifyType theta2 = candidate.getContent(); - Optional optSigma = unify.unify(theta2, t); + //PL 18-02-05 Unifier durch Matcher ersetzt ANFANG + ArrayList termList= new ArrayList(); + termList.add(new UnifyPair(theta2,t, PairOperator.EQUALSDOT)); + Optional optSigma = match.match(termList); + //PL 18-02-05 Unifier durch Matcher ersetzt ENDE if(!optSigma.isPresent()) continue; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index c6aa9b3e..aa375c09 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -94,6 +94,7 @@ class Node { @Override public String toString() { - return "Node(" + content.toString() + ")\n"; + return "Elem: Node(" + content.toString() + ")\nPrec: " + getContentOfPredecessors().toString() + + "\nDesc: " + getContentOfDescendants().toString() + "\n\n"; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index 482155fc..3d5fc278 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -102,9 +102,9 @@ public final class TypeParams implements Iterable{ if(p.equals(t)) return true; } - else + else { if(p.getTypeParams().occurs(t)) - return true; + return true; } return false; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 2dd7c83f..061ba588 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -66,6 +66,14 @@ public class Unifier implements Function, Iterable getFC() { Set ret = new HashSet<>(); - ret.add(ASTFactory.createObjectClass()); - ret.add(ASTFactory.createClass(java.util.List.class)); return ret; } public ConstraintSet getPairs() { ConstraintSet ret = new ConstraintSet<>(); - ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + ret.addUndConstraint(new Pair(testType, ASTFactory.createObjectType(), PairOperator.EQUALSDOT)); return ret; } } diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java new file mode 100644 index 00000000..9e7c867d --- /dev/null +++ b/test/asp/UnifyWithoutWildcards.java @@ -0,0 +1,29 @@ +package asp; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +public class UnifyWithoutWildcards { + + public Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createObjectClass()); + ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } + + public ConstraintSet getPairs() { + ConstraintSet ret = new ConstraintSet<>(); + ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + return ret; + } +} diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index b41d9405..cea141f9 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -14,7 +14,9 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class ASPTest { @@ -44,9 +46,9 @@ public class ASPTest { //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); - List allClasses = new ArrayList<>(); + Set allClasses = new HashSet<>(); for(SourceFile sf : compiler.sourceFiles.values()) { - //allClasses.addAll(compiler.getAvailableClasses(sf)); + allClasses.addAll(compiler.getAvailableClasses(sf)); } for(SourceFile sf : compiler.sourceFiles.values()) { allClasses.addAll(sf.getClasses()); diff --git a/test/javFiles/Expressions.jav b/test/javFiles/Expressions.jav new file mode 100644 index 00000000..e2e992a1 --- /dev/null +++ b/test/javFiles/Expressions.jav @@ -0,0 +1,8 @@ +class Expressions{ + +void test(){ + var x = 2; + x = x + 2; +} + +} \ No newline at end of file diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e1183535..aa8966c9 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -6,21 +6,21 @@ class Matrix extends Vector> { mul(m) { var ret = new Matrix(); var i = 0; - while(i < size()) { + //while(i < size()) { var v1 = this.elementAt(i); - var v2 = new Vector(); - var j = 0; - while(j < v1.size()) { - var erg = 0; - var k = 0; - while(k < v1.size()) { - erg = erg + v1.elementAt(k) - * m.elementAt(k).elementAt(j); - k++; } - v2.addElement(new Integer(erg)); - j++; } - ret.addElement(v2); - i++; } + //var v2 = new Vector(); + //var j = 0; + //while(j < v1.size()) { + //var erg = 0; + //var k = 0; + //while(k < v1.size()) { + //erg = erg + v1.elementAt(k) + // * m.elementAt(k).elementAt(j); + //k++; } + //v2.addElement(new Integer(erg)); + //j++; } + //ret.addElement(v2); + //i++; } return ret; } } diff --git a/test/typeinference/FacultyTest.java b/test/typeinference/FacultyTest.java deleted file mode 100644 index aadaf6e7..00000000 --- a/test/typeinference/FacultyTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class FacultyTest extends JavaTXCompilerTest{ - public FacultyTest() { - this.fileToTest = new File(rootDirectory+"Faculty.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/FiniteClosureTest.java b/test/typeinference/FiniteClosureTest.java deleted file mode 100644 index 8b62e4e5..00000000 --- a/test/typeinference/FiniteClosureTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class FiniteClosureTest extends JavaTXCompilerTest{ - public FiniteClosureTest() { -// this.fileToTest = new File(rootDirectory+"fc.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/GenericsTest.java b/test/typeinference/GenericsTest.java deleted file mode 100644 index abcef013..00000000 --- a/test/typeinference/GenericsTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package typeinference; - -import java.io.File; - -//TODO: Hier gibt es einen Fehler. Das erstellte ConstraintSet stimmt nicht -public class GenericsTest extends JavaTXCompilerTest{ - public GenericsTest() { - this.fileToTest = new File(rootDirectory+"Generics.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 03452018..3fe7c17d 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -23,19 +23,57 @@ import java.util.Set; public class JavaTXCompilerTest { public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; - private static final List filesToTest = new ArrayList<>(); - protected File fileToTest = null; - public JavaTXCompilerTest(){ + @Test + public void finiteClosure() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"fc.jav")); } @Test - public void test() throws IOException, ClassNotFoundException { - if(fileToTest != null)filesToTest.add(fileToTest); - else return; - //filesToTest.add(new File(rootDirectory+"Faculty.jav")); - //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - //filesToTest.add(new File(rootDirectory+"test.jav")); - filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); + public void lambda() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda.jav")); + } + @Test + public void lambda2() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda2.jav")); + } + @Test + public void lambda3() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda3.jav")); + } + @Test + public void mathStruc() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"mathStruc.jav")); + } + @Test + public void generics() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Generics.jav")); + } + @Test + public void faculty() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Faculty.jav")); + } + @Test + public void matrix() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Matrix.jav")); + } + @Test + public void vector() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Vector.jav")); + } + @Test + public void lambdaRunnable() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"LambdaRunnable.jav")); + } + @Test + public void expressions() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Expressions.jav")); + } + + private static class TestResultSet{ + + } + + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { //filesToTest.add(new File(rootDirectory+"fc.jav")); //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); @@ -45,6 +83,9 @@ public class JavaTXCompilerTest { //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); + // //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + // //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + // //filesToTest.add(new File(rootDirectory+"test.jav")); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); List results = compiler.typeInference(); @@ -68,6 +109,7 @@ public class JavaTXCompilerTest { System.out.println(s); } } + return new TestResultSet(); } static String readFile(String path, Charset encoding) diff --git a/test/typeinference/Lambda2Test.java b/test/typeinference/Lambda2Test.java deleted file mode 100644 index 84867cd9..00000000 --- a/test/typeinference/Lambda2Test.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class Lambda2Test extends JavaTXCompilerTest{ - public Lambda2Test() { - this.fileToTest = new File(rootDirectory+"Lambda2.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/Lambda3Test.java b/test/typeinference/Lambda3Test.java deleted file mode 100644 index 843eee10..00000000 --- a/test/typeinference/Lambda3Test.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class Lambda3Test extends JavaTXCompilerTest{ - public Lambda3Test() { - this.fileToTest = new File(rootDirectory+"Lambda3.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/LambdaTest.java b/test/typeinference/LambdaTest.java deleted file mode 100644 index abdaa140..00000000 --- a/test/typeinference/LambdaTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class LambdaTest extends JavaTXCompilerTest{ - public LambdaTest() { - this.fileToTest = new File(rootDirectory+"Lambda.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/MatrixTest.java b/test/typeinference/MatrixTest.java deleted file mode 100644 index 8cc587c9..00000000 --- a/test/typeinference/MatrixTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class MatrixTest extends JavaTXCompilerTest{ - public MatrixTest() { - this.fileToTest = new File(rootDirectory+"Matrix.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/RunnableTest.java b/test/typeinference/RunnableTest.java deleted file mode 100644 index bc0c52ad..00000000 --- a/test/typeinference/RunnableTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class RunnableTest extends JavaTXCompilerTest{ - public RunnableTest() { - this.fileToTest = new File(rootDirectory+"LambdaRunnable.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/VectorTest.java b/test/typeinference/VectorTest.java deleted file mode 100644 index 864535b3..00000000 --- a/test/typeinference/VectorTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class VectorTest extends JavaTXCompilerTest{ - public VectorTest() { - this.fileToTest = new File(rootDirectory+"Vector.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/mathStrucTest.java b/test/typeinference/mathStrucTest.java deleted file mode 100644 index 0181be72..00000000 --- a/test/typeinference/mathStrucTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class mathStrucTest extends JavaTXCompilerTest{ - public mathStrucTest() { - this.fileToTest = new File(rootDirectory+"mathStruc.jav"); - } -} \ No newline at end of file