JavaTXCompilerInJavaTX/test/strucType/TestPaperExample.java
2018-05-17 21:35:14 +02:00

90 lines
4.0 KiB
Java

package strucType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.strucTypes.ClassOrInterfaceWithConstraints;
import de.dhbwstuttgart.strucTypes.Construct;
import de.dhbwstuttgart.strucTypes.InferredTypes;
import de.dhbwstuttgart.strucTypes.Solve;
import de.dhbwstuttgart.strucTypes.StrucTYPE;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException;
import de.dhbwstuttgart.strucTypes.exception.InconsistentConstraintsException;
import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints;
import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes;
import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
public class TestPaperExample {
public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/";
public final PrintConstraints printConstraints = new PrintConstraints();
@org.junit.Test
public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException {
ArrayList<File> files = new ArrayList<>();
files.add(new File(rootDirectory + "testPaperExample.jav"));
trans(files);
files.clear();
files.add(new File(rootDirectory + "testMain.jav"));
trans(files);
}
private void trans(ArrayList<File> files)
throws IOException, ClassNotFoundException, ImpossibleSubTypeException, InconsistentConstraintsException {
JavaTXCompiler compiler = new JavaTXCompiler(files);
for (File f : compiler.sourceFiles.keySet()) {
String name = f.getName();
System.out.println("Filename: " + name);
SourceFile sourceFile = compiler.sourceFiles.get(f);
// Print SourceFile Infos
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
sourceFile.accept(syntaxtreeprinter);
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
final ConstraintsSet constraints = strucTYPE.getConstraints();
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
System.out.println("\n--StrucTYPE--");
printConstraints.print(constraints);
PrintInferredTypes.print(inferredTypesType);
Construct construct = new Construct(constraints, inferredTypesType);
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
final Set<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
System.out.println("\n--Construct--");
System.out.println("\nConstructed Interfaces:");
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
printConstraints.printSubTypeConstraints(subTypeConstraints);
PrintInferredTypes.print(inferredTypesConstruct);
System.out.println("\n--Solve--");
// Alle Klassen aus allen SourceFiles
List<ClassOrInterface> availableClasses = compiler.getAvailableClasses(sourceFile);
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses);
System.out.println("\nFinite Closure:");
System.out.println(finiteClosure);
Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct);
ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass();
System.out.println("\nSolved Class:");
solvedClass.accept(syntaxtreeprinter);
System.out.println("\nRemaining Constraints:");
printConstraints.printSubTypeConstraints(solvedClass.getConstraints());
System.out.println("____________________________________________________________________________");
}
}
}