ASP-Generierung: parameterListe

This commit is contained in:
JanUlrich 2017-12-18 14:43:03 +01:00
parent 3a444c0172
commit e8757a179f
17 changed files with 358 additions and 44 deletions

View File

@ -29,7 +29,7 @@ import java.util.stream.Collectors;
public class JavaTXCompiler {
final CompilationEnvironment environment;
final CompilationEnvironment environment;
public final Map<File, SourceFile> sourceFiles = new HashMap<>();
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
@ -38,56 +38,83 @@ public class JavaTXCompiler {
public JavaTXCompiler(List<File> sources) throws IOException, ClassNotFoundException {
environment = new CompilationEnvironment(sources);
for(File s : sources){
sourceFiles.put(s,parse(s));
for (File s : sources) {
sourceFiles.put(s, parse(s));
}
}
public List<ResultSet> typeInference() throws ClassNotFoundException {
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
for(SourceFile sf : sourceFiles.values()){
for (SourceFile sf : sourceFiles.values()) {
allClasses.addAll(sf.getClasses());
}
List<ClassOrInterface> importedClasses = new ArrayList<>();
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for(File forSourceFile : sourceFiles.keySet())
for(JavaClassName name : sourceFiles.get(forSourceFile).getImports()){
//TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet
for (File forSourceFile : sourceFiles.keySet())
for (JavaClassName name : sourceFiles.get(forSourceFile).getImports()) {
//TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet
ClassOrInterface importedClass = ASTFactory.createClass(
ClassLoader.getSystemClassLoader().loadClass(name.toString()));
importedClasses.add(importedClass);
}
allClasses.addAll(importedClasses);
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
final ConstraintSet<Pair> cons = new TYPE(sourceFiles.values(), allClasses).getConstraints();
return new TYPE(sourceFiles.values(), allClasses).getConstraints();
}
public List<ClassOrInterface> getAvailableClasses(SourceFile forSourceFile) throws ClassNotFoundException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
for (SourceFile sf : sourceFiles.values()) {
allClasses.addAll(sf.getClasses());
}
List<ClassOrInterface> importedClasses = new ArrayList<>();
for (JavaClassName name : forSourceFile.getImports()) {
//TODO: Hier werden imports von eigenen (.jav) Klassen nicht beachtet
ClassOrInterface importedClass = ASTFactory.createClass(
ClassLoader.getSystemClassLoader().loadClass(name.toString()));
importedClasses.add(importedClass);
allClasses.addAll(importedClasses);
}
return allClasses;
}
public List<ResultSet> typeInference() throws ClassNotFoundException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
//Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for(SourceFile sf : this.sourceFiles.values()) {
allClasses.addAll(getAvailableClasses(sf));
}
final ConstraintSet<Pair> cons = getConstraints();
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
TypeUnify unify = new TypeUnify();
Set<Set<UnifyPair>> results = new HashSet<>();
for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){
for (List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()) {
Set<UnifyPair> xConsSet = new HashSet<>();
for(Constraint<UnifyPair> constraint : xCons){
for (Constraint<UnifyPair> constraint : xCons) {
xConsSet.addAll(constraint);
}
System.out.println(xConsSet);
//System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
//System.out.println("RESULT: " + result);
results.addAll(result);
}
return results.stream().map((unifyPairs ->
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
}
}
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints){
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints) {
HashMap<String, TypePlaceholder> ret = new HashMap<>();
constraints.map((Pair p)->{
if(p.TA1 instanceof TypePlaceholder){
ret.put(((TypePlaceholder)p.TA1).getName(), (TypePlaceholder) p.TA1);
constraints.map((Pair p) -> {
if (p.TA1 instanceof TypePlaceholder) {
ret.put(((TypePlaceholder) p.TA1).getName(), (TypePlaceholder) p.TA1);
}
if(p.TA2 instanceof TypePlaceholder){
ret.put(((TypePlaceholder)p.TA2).getName(), (TypePlaceholder) p.TA2);
if (p.TA2 instanceof TypePlaceholder) {
ret.put(((TypePlaceholder) p.TA2).getName(), (TypePlaceholder) p.TA2);
}
return null;
});
@ -97,8 +124,8 @@ public class JavaTXCompiler {
private SourceFile parse(File sourceFile) throws IOException, java.lang.ClassNotFoundException {
CompilationUnitContext tree = JavaTXParser.parse(sourceFile);
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(sourceFile), new GenericsRegistry(null));
SourceFile ret = generator.convert(tree, environment.packageCrawler);
SourceFile ret = generator.convert(tree, environment.packageCrawler);
return ret;
}
}
}

View File

@ -43,7 +43,7 @@ public class TypeGenerator {
throw new NotImplementedException();
}else
if(unannTypeContext.unannReferenceType().unannArrayType()!=null){
System.out.println(unannTypeContext.getText());
//System.out.println(unannTypeContext.getText());
throw new NotImplementedException();
}else
if(unannTypeContext.unannReferenceType().unannTypeVariable()!=null){

View File

@ -1,24 +1,49 @@
package de.dhbwstuttgart.sat.asp;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.model.*;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public class ASPGenerator {
public static String toASP(ConstraintSet constraintSet, Collection<ClassOrInterface> fcClasses){
String ret = "";
for(ClassOrInterface cl : fcClasses){
String className = toConstant(cl.getClassName());
String superClassName = toConstant(cl.getSuperClass().getName());
ASPWriter writer = new ASPWriter();
private final String asp;
}
return ret;
public ASPGenerator(ConstraintSet constraints, Collection<ClassOrInterface> fcClasses){
asp = toASP(constraints, fcClasses);
}
public String getASP(){
return asp;
}
public static String toConstant(JavaClassName name){
private String toASP(ConstraintSet constraintSet, Collection<ClassOrInterface> fcClasses){
for(ClassOrInterface cl : fcClasses){
Optional<ClassOrInterface> superClass =
fcClasses.stream().filter(c -> c.getSuperClass().getName().equals(cl.getClassName())).findAny();
//Für den Fall das es keinen Supertyp in den fcClasses gibt, wird die Klasse immer noch als ihr eigener Subtyp angefügt:
ASPPairSmaller fcEntry = new ASPPairSmaller(convert(cl), convert(superClass.orElse(cl)));
writer.add(new ASPStatement(fcEntry.toASP()));
}
return writer.getASPFile();
}
private ASPType convert(ClassOrInterface cl){
List<ASPType> paramList = new ArrayList<>();
for(GenericTypeVar gtv : cl.getGenerics()){
paramList.add(new ASPGenericType(toConstant(gtv.getName())));
}
ASPParameterList params = new ASPParameterList(paramList, writer);
return new ASPRefType(toConstant(cl.getClassName()), params);
}
private String toConstant(JavaClassName name){
return "c" + name.toString().replace(".", "_");
}
}

View File

@ -0,0 +1,24 @@
package de.dhbwstuttgart.sat.asp;
import de.dhbwstuttgart.sat.asp.model.ASPRefType;
import de.dhbwstuttgart.sat.asp.model.ASPStatement;
import de.dhbwstuttgart.sat.asp.model.ASPType;
import java.util.HashSet;
public class ASPWriter {
private HashSet<ASPStatement> content = new HashSet<>();
public void add(ASPStatement stmt){
content.add(stmt);
}
public String getASPFile(){
String ret = "";
for(ASPStatement statement : content){
ret += statement.getASP() + ".\n";
}
return ret;
}
}

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPGenericType implements ASPType{
public ASPGenericType(String name){
}
}

View File

@ -0,0 +1,21 @@
package de.dhbwstuttgart.sat.asp.model;
public abstract class ASPPair {
public final ASPType leftSide;
public final ASPType rightSide;
public ASPPair(ASPType ls, ASPType rs){
this.leftSide = ls;
this.rightSide = rs;
}
public String toASP(){
return this.getRuleName() + "(" + leftSide + ","+ rightSide + ")";
}
public String toString(){
return toASP();
}
protected abstract String getRuleName();
}

View File

@ -0,0 +1,13 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPPairEquals extends ASPPair{
private final static String ASP_PAIR_EQUALS_NAME = "equals";
public ASPPairEquals(ASPType ls, ASPType rs){
super(ls, rs);
}
@Override
protected String getRuleName() {
return ASP_PAIR_EQUALS_NAME;
}
}

View File

@ -0,0 +1,13 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPPairSmaller extends ASPPair{
private final static String ASP_PAIR_SMALLER_NAME = "smaller";
public ASPPairSmaller(ASPType ls, ASPType rs){
super(ls, rs);
}
@Override
protected String getRuleName() {
return ASP_PAIR_SMALLER_NAME;
}
}

View File

@ -0,0 +1,40 @@
package de.dhbwstuttgart.sat.asp.model;
import de.dhbwstuttgart.sat.asp.ASPWriter;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ASPParameterList {
private final static String ASP_PARAMLIST_NAME = "param";
private final static String ASP_PARAMLIST_END_POINTER = "null";
public final String name;
private final List<ASPType> types;
public ASPParameterList(List<ASPType> types, ASPWriter writer){
this.types = types;
if(types.size() == 0){
name = ASP_PARAMLIST_END_POINTER;
}else{
name = NameGenerator.makeNewName();
String nextPointer = name;
Iterator<ASPType> it = types.iterator();
while(it.hasNext()){
ASPType t = it.next();
String param = nextPointer + "," + t.toString() + ",";
nextPointer = NameGenerator.makeNewName();
if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER;
param += nextPointer;
writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")"));
//paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")"));
}
}
}
public String toString(){
return name;
}
}

View File

@ -0,0 +1,20 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPRefType implements ASPType {
public static final String ASP_TYPE = "type";
private final ASPParameterList params;
private final String name;
public ASPRefType(String name, ASPParameterList params){
this.name = name;
this.params = params;
}
public ASPParameterList getParams() {
return params;
}
public String toString(){
return ASP_TYPE + "(" + name +"," + params.name + ")";
}
}

View File

@ -0,0 +1,27 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPStatement {
private final String stmt;
public ASPStatement(String stmt) {
this.stmt = stmt;
}
public String toString(){
return stmt;
}
@Override
public int hashCode() {
return stmt.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof ASPStatement)return stmt.equals(((ASPStatement) obj).stmt);
return false;
}
public String getASP() {
return stmt;
}
}

View File

@ -1,9 +1,4 @@
package de.dhbwstuttgart.sat.asp.model;
import java.util.List;
public class ASPType {
public ASPType(String name, List<ASPType> params){
}
public interface ASPType {
}

View File

@ -0,0 +1,7 @@
package de.dhbwstuttgart.sat.asp.model;
public class ASPTypeVar implements ASPType{
public ASPTypeVar(String name){
}
}

View File

@ -153,8 +153,8 @@ public final class TypeParams implements Iterable<UnifyType>{
return false;
for(int i = 0; i < this.size(); i++){
if(this.get(i) == null)
System.out.print("s");
//if(this.get(i) == null)
//System.out.print("s");
}
for(int i = 0; i < this.size(); i++)
if(!(this.get(i).equals(other.get(i))))

View File

@ -0,0 +1,68 @@
package asp.typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.sat.asp.ASPGenerator;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class ASPTest {
public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final List<File> filesToTest = new ArrayList<>();
protected File fileToTest = null;
public ASPTest(){
}
@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"));
//filesToTest.add(new File(rootDirectory+"fc.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : compiler.sourceFiles.values()) {
//allClasses.addAll(compiler.getAvailableClasses(sf));
}
for(SourceFile sf : compiler.sourceFiles.values()) {
allClasses.addAll(sf.getClasses());
}
final ConstraintSet<Pair> cons = compiler.getConstraints();
ASPGenerator generator = new ASPGenerator(cons, allClasses);
System.out.println(generator.getASP());
}
static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@ -0,0 +1,9 @@
package asp.typeinference;
import java.io.File;
public class VectorTest extends ASPTest {
public VectorTest() {
this.fileToTest = new File(rootDirectory+"Vector.jav");
}
}

View File

@ -1,11 +1,23 @@
package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.sat.asp.ASPGenerator;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import de.dhbwstuttgart.typeinference.typeAlgo.TYPE;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import org.junit.Test;
import java.io.File;
@ -17,8 +29,10 @@ import java.nio.charset.StandardCharsets;
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;
import java.util.stream.Collectors;
public class JavaTXCompilerTest {
@ -28,11 +42,10 @@ public class JavaTXCompilerTest {
public JavaTXCompilerTest(){
}
@Test
public void test() throws IOException, java.lang.ClassNotFoundException {
public void test() throws IOException, ClassNotFoundException {
if(fileToTest != null)filesToTest.add(fileToTest);
else return;
else return;
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
//filesToTest.add(new File(rootDirectory+"test.jav"));
@ -47,6 +60,7 @@ public class JavaTXCompilerTest {
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(fileToTest);
List<ResultSet> results = compiler.typeInference();
for(File f : compiler.sourceFiles.keySet()){
@ -54,14 +68,18 @@ public class JavaTXCompilerTest {
System.out.println(ASTTypePrinter.print(sf));
//List<ResultSet> results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen
assert results.size()>0;
Set<String> insertedTypes = new HashSet<>();
for(ResultSet resultSet : results){
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);
assert result.size()>0;
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
System.out.println(tip.insert(content));
insertedTypes.add(tip.insert(content));
}
}
for(String s : insertedTypes){
System.out.println(s);
}
}
}