From 1141417c0be3e520100e80d8f86896567fbbd538 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 9 Oct 2014 17:38:10 +0200 Subject: [PATCH] Weitere Aussonderung von Constraints durch Unify --- bin/.gitignore | 2 + .../dhbwstuttgart/core/ConsoleInterface.java | 4 +- src/de/dhbwstuttgart/core/MyCompiler.java | 10 +++-- src/de/dhbwstuttgart/logger/Logger.java | 44 ++++++++++++++----- src/de/dhbwstuttgart/syntaxtree/Class.java | 18 ++++---- .../dhbwstuttgart/syntaxtree/SourceFile.java | 17 +++---- .../syntaxtree/type/RefType.java | 4 +- .../syntaxtree/type/TypePlaceholder.java | 4 +- .../typeinference/ConstraintsSet.java | 20 +++++++-- .../typeinference/OderConstraint.java | 6 ++- src/de/dhbwstuttgart/typeinference/Pair.java | 2 +- .../assumptions/TypeAssumptions.java | 10 ++--- test/plugindevelopment/TypeInsertTester.java | 2 +- 13 files changed, 92 insertions(+), 51 deletions(-) diff --git a/bin/.gitignore b/bin/.gitignore index f1735d3a6..8ac1c637b 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1,5 @@ /de/ /mycompiler/ /plugindevelopment/ +/bytecode/ +/parser/ diff --git a/src/de/dhbwstuttgart/core/ConsoleInterface.java b/src/de/dhbwstuttgart/core/ConsoleInterface.java index 742f0fe61..e453b0b1c 100755 --- a/src/de/dhbwstuttgart/core/ConsoleInterface.java +++ b/src/de/dhbwstuttgart/core/ConsoleInterface.java @@ -11,7 +11,7 @@ import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException; public class ConsoleInterface { private static final String directory = System.getProperty("user.dir"); - + private static final Logger log = Logger.getLogger( ConsoleInterface.class.getName() ); /** * @param args */ @@ -20,7 +20,7 @@ public class ConsoleInterface { for(String file : args){ filenames.add(file); } - Logger.setOutput(null); // sämtliches Logging unterdrücken + Logger.setStandardOutput(null); // sämtliches Logging unterdrücken run(filenames); } diff --git a/src/de/dhbwstuttgart/core/MyCompiler.java b/src/de/dhbwstuttgart/core/MyCompiler.java index 7ec12f3b9..375cd684b 100755 --- a/src/de/dhbwstuttgart/core/MyCompiler.java +++ b/src/de/dhbwstuttgart/core/MyCompiler.java @@ -66,13 +66,13 @@ public class MyCompiler implements MyCompilerAPI // Logger // ino.end // ino.attribute.codegenlog.21265.declaration - protected static Logger codegenlog = Logger.getLogger("codegen"); + //protected static Logger codegenlog = Logger.getLogger("codegen"); // ino.end // ino.attribute.inferencelog.21268.declaration - protected static Logger inferencelog = Logger.getLogger("inference"); + protected static Logger inferencelog = Logger.getLogger(MyCompiler.class.getName()); // ino.end // ino.attribute.parserlog.21271.declaration - protected static Logger parserlog = Logger.getLogger("parser"); + //protected static Logger parserlog = Logger.getLogger("parser"); // ino.end // ino.attribute.OutputDir.21274.declaration @@ -180,7 +180,7 @@ public class MyCompiler implements MyCompilerAPI } else { - parserlog.error( "SEMANTIK-CHECK-FEHLER: Parameter " + TempParameter.getName() + " muss weitere Parameter besitzen (laut Klassendefinition)" ); + //parserlog.error( "SEMANTIK-CHECK-FEHLER: Parameter " + TempParameter.getName() + " muss weitere Parameter besitzen (laut Klassendefinition)" ); //FIXME Throw exception instead of simple exit System.exit( 1 ); } @@ -512,6 +512,8 @@ public class MyCompiler implements MyCompilerAPI // ino.end // ino.method.typeReconstruction.21304.body { + Logger.setStandardOutput(System.out); //TODO: Hier noch das Log-Level richtig setzen (je nachdem ob debugt wird oder nicht) + if(m_AbstractSyntaxTree==null){ throw new NullPointerException("Es wurde noch kein Abstrakter Syntaxbaum erstellt!"); } diff --git a/src/de/dhbwstuttgart/logger/Logger.java b/src/de/dhbwstuttgart/logger/Logger.java index a640ae551..3003da8ea 100644 --- a/src/de/dhbwstuttgart/logger/Logger.java +++ b/src/de/dhbwstuttgart/logger/Logger.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.logger; import java.io.PrintStream; import java.util.HashMap; +import java.util.logging.Level; public class Logger { @@ -9,17 +10,35 @@ public class Logger { private static final HashMap LOGGER_DIRECTORY = new HashMap<>(); private String name; - private PrintStream output; - + private final java.util.logging.Logger log; + private Logger(String name, PrintStream output) { this.name = name; - this.output = output; + this.log = java.util.logging.Logger.getLogger( name ); + if(output != null)log.addHandler(new OutputHandler(output)); + log.setLevel(Level.FINE); } - public void debug(String message){ - output(message); + /** + * Logt eine Debug Message, welche zusätzlich einer bestimmten Section zugewiesen wird. + * Dadurch lässt sich die DEBUG ausgabe übersichtlicher gestalten. + * @param message + * @param section + */ + public void debug(String message, Section section){ + output(message, Level.FINE); } + public void debug(String message){ + //output(message, Level.FINE); + } + + /** + * Liefert den Logger mit dem angegebenen Namen. + * Üblicherweise wird diese Methode mit dem Namen der Klasse aufgerufen, in welcher der Logger tätig ist. + * @param name - Name der Klasse ( Ermittelbar mittels .class.getName() ) + * @return + */ public static Logger getLogger(String name) { Logger ret; if(LOGGER_DIRECTORY.containsKey(name)){ @@ -31,22 +50,23 @@ public class Logger { return ret; } - private void output(String msg){ + private void output(String msg , Level logLevel){ + log.log(logLevel, msg); + /* if(output != null){ output.println(msg); }else if(standardOutput != null){ standardOutput.println(msg); } + */ } - public void info(String string) { - // TODO Auto-generated method stub - + public void info(String message) { + output(message, Level.INFO); } - public void error(String string) { - // TODO Auto-generated method stub - + public void error(String message) { + output(message, Level.WARNING); } /** diff --git a/src/de/dhbwstuttgart/syntaxtree/Class.java b/src/de/dhbwstuttgart/syntaxtree/Class.java index 65f07318f..a23e235ae 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Class.java +++ b/src/de/dhbwstuttgart/syntaxtree/Class.java @@ -10,7 +10,7 @@ import java.util.Iterator; import java.util.Vector; import de.dhbwstuttgart.logger.Logger; - +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.core.AClassOrInterface; import de.dhbwstuttgart.core.IItemWithOffset; import de.dhbwstuttgart.parser.JavaClassName; @@ -44,8 +44,8 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit * Log4j - Loggerinstanzen */ protected static Logger inferencelog = Logger.getLogger("inference"); - protected static Logger codegenlog = Logger.getLogger("codegen"); - protected static Logger parserlog = Logger.getLogger("parser"); + //protected static Logger codegenlog = Logger.getLogger("codegen"); + //protected static Logger parserlog = Logger.getLogger("parser"); protected UsedId pkgName; protected Modifiers modifiers; protected String name; @@ -137,9 +137,9 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit private TypeAssumptions typeAssumptions = null;//muss mit null Initialisiert werden. Darf nur über getTypeAssumptions abgerufen werden. // ino.attribute.parserlog.23038.declaration - protected Logger parselog = Logger.getLogger("parser"); + //protected Logger parselog = Logger.getLogger("parser"); // ino.end - protected Logger typinferenzLog = Logger.getLogger("Typeinference"); + protected Logger typinferenzLog = Logger.getLogger(Class.class.getName()); private SyntaxTreeNode parent; private Vector fielddecl = new Vector(); private GenericDeclarationList genericClassParameters; @@ -267,7 +267,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit } - parserlog.debug("Neue Klasse: " + name); + //parserlog.debug("Neue Klasse: " + name); } // ino.end @@ -658,7 +658,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit ////////////////////////////// inferencelog.info("Rufe TRStart()..."); - typinferenzLog.debug("Erstellte FiniteClosure: "+supportData); + typinferenzLog.debug("Erstellte FiniteClosure: "+supportData, Section.TYPEINFERENCE); ////////////////////////////// // Ab hier ... // @author A10023 - Andreas Stadelmeier: @@ -677,7 +677,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit if(gparam instanceof GenericTypeVar)oderConstraints.add(((GenericTypeVar)gparam).TYPE(assumptions)); //Constraints für die Generischen Variablen erstellen und diese dem AssumptionsSet hinzufügen } - typinferenzLog.debug("Erstellte Assumptions: "+assumptions); + typinferenzLog.debug("Erstellte Assumptions: "+assumptions, Section.TYPEINFERENCE); /* //Generiere Liste mit Expressions, welche zur Initialisierung von Feldern verwendet werden. Vector fieldInitializers = new Vector(); @@ -696,7 +696,7 @@ public class Class extends SyntaxTreeNode implements AClassOrInterface, IItemWit for(Field f:this.getFields()){ oderConstraints.add(f.TYPE(assumptions)); } - typinferenzLog.debug("Erstellte Constraints: "+oderConstraints); + typinferenzLog.debug("Erstellte Constraints: "+oderConstraints, Section.TYPEINFERENCE); return oderConstraints; diff --git a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java index d1f45761c..594825103 100755 --- a/src/de/dhbwstuttgart/syntaxtree/SourceFile.java +++ b/src/de/dhbwstuttgart/syntaxtree/SourceFile.java @@ -11,6 +11,7 @@ import java.util.Iterator; import java.util.Vector; import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.bytecode.ClassFile; import de.dhbwstuttgart.core.AClassOrInterface; import de.dhbwstuttgart.core.MyCompiler; @@ -673,12 +674,12 @@ public class SourceFile //Assumptions der importierten Klassen sammeln: TypeAssumptions importAssumptions = this.makeBasicAssumptionsFromJRE(imports, true); globalAssumptions.add(importAssumptions); - typinferenzLog.debug("Von JRE erstellte Assumptions: "+importAssumptions); + typinferenzLog.debug("Von JRE erstellte Assumptions: "+importAssumptions, Section.TYPEINFERENCE); //FiniteClosure generieren: FC_TTO finiteClosure = this.makeFC(globalAssumptions); - typinferenzLog.debug("FiniteClosure: \n"+finiteClosure); + typinferenzLog.debug("FiniteClosure: \n"+finiteClosure, Section.TYPEINFERENCE); ConstraintsSet oderConstraints = new ConstraintsSet(); //Alle Constraints der in dieser SourceFile enthaltenen Klassen sammeln: @@ -697,7 +698,7 @@ public class SourceFile return retValue;}; oderConstraints.filterWrongConstraints(unifier); oderConstraints.unifyUndConstraints(unifier); - typinferenzLog.debug("Übriggebliebene Konstraints:\n"+oderConstraints+"\n"); + typinferenzLog.debug("Übriggebliebene Konstraints:\n"+oderConstraints+"\n", Section.TYPEINFERENCE); //Die Constraints in Pair's umwandeln (Karthesisches Produkt bilden): Vector> xConstraints = new Vector>();// = oderConstraints.getConstraints(); for(Vector uC : oderConstraints.getConstraints()){ //mit dem getConstraints-Aufruf wird das Karthesische Produkt erzeugt. @@ -707,7 +708,7 @@ public class SourceFile } xConstraints.add(cons); } - typinferenzLog.debug("Karthesisches Produkt der Constraints: "+xConstraints); + typinferenzLog.debug("Karthesisches Produkt der Constraints: "+xConstraints, Section.TYPEINFERENCE); finiteClosure.generateFullyNamedTypes(globalAssumptions); @@ -781,7 +782,7 @@ public class SourceFile result.addAll(unifyResult); // Debugoutput:Vector> - typinferenzLog.debug("Unifiziertes Ergebnis: "+result); + typinferenzLog.debug("Unifiziertes Ergebnis: "+result, Section.TYPEINFERENCE); /* // Prüfe ob eindeutige Lösung: @@ -803,7 +804,7 @@ public class SourceFile //typinferenzLog.debug(supportData.getFiniteClosure()); //typinferenzLog.debug("Typinformationen: \n"+this.getTypeInformation(this.getMethodList(), fieldInitializers)); - typinferenzLog.debug("\nJavaFiles:\n"); + typinferenzLog.debug("\nJavaFiles:\n", Section.TYPEINFERENCE); //typinferenzLog.debug(this.printJavaCode(new ResultSet(new Vector()))); @@ -818,8 +819,8 @@ public class SourceFile ret.add(reconstructionResult); //ResultSet res = new ResultSet(resultSet); - typinferenzLog.debug("JavaFile für ResultSet "+reconstructionResult+"\n"); - typinferenzLog.debug(klasse.printJavaCode(reconstructionResult)); + typinferenzLog.debug("JavaFile für ResultSet "+reconstructionResult+"\n", Section.TYPEINFERENCE); + typinferenzLog.debug(klasse.printJavaCode(reconstructionResult), Section.TYPEINFERENCE); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 85e4ba2fb..af50be029 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -56,10 +56,10 @@ public class RefType extends Type implements IMatchable private boolean primitiveFlag=false; // ino.end // ino.attribute.parserlog.26628.declaration - protected static Logger parserlog = Logger.getLogger("parser"); + //protected static Logger parserlog = Logger.getLogger("parser"); // ino.end // ino.attribute.codegenlog.26631.declaration - protected static Logger codegenlog = Logger.getLogger("codegen"); + //protected static Logger codegenlog = Logger.getLogger("codegen"); // ino.end diff --git a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java index b8b20c44b..6f5fb6b3e 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/TypePlaceholder.java @@ -6,7 +6,7 @@ package de.dhbwstuttgart.syntaxtree.type; import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; -import java.util.logging.Logger; +import de.dhbwstuttgart.logger.*; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.parser.JavaClassName; @@ -31,6 +31,7 @@ public class TypePlaceholder extends Type // ino.end // ino.class.TypePlaceholder.26780.body { + private static final Logger log = Logger.getLogger(TypePlaceholder.class.getName()); // ino.attribute.strNextName.26785.declaration private static String strNextName = "A"; // ino.end @@ -55,6 +56,7 @@ public class TypePlaceholder extends Type { super(parent, -1); this.name = new JavaClassName(typeName); + if(parent != null)log.debug("Erstelle TPH "+typeName+" für SyntaxTreeNode: "+parent, Section.TYPEINFERENCE); } // ino.end diff --git a/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java b/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java index c7f052ca4..f67fb6d04 100755 --- a/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java +++ b/src/de/dhbwstuttgart/typeinference/ConstraintsSet.java @@ -2,10 +2,12 @@ package de.dhbwstuttgart.typeinference; import java.util.Iterator; import java.util.Vector; - +import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.*; import de.dhbwstuttgart.typeinference.unify.Unifier; public class ConstraintsSet implements Iterable{ + private static final Logger log = Logger.getLogger(ConstraintsSet.class.getName()); private Vector constraintsSet; public ConstraintsSet(){ @@ -58,14 +60,26 @@ public class ConstraintsSet implements Iterable{ } } + /** + * Nimmt alle UndConstraints und filtert mithilfe dieser die falschen Constraints aus den OderConstraints + * @param unifier + */ public void unifyUndConstraints(Unifier unifier) { Vector uCons = this.filterUndConstraints(); Vector alleUndConstraints = new Vector<>(); for(UndConstraint undConstraint : uCons){ alleUndConstraints.addAll(undConstraint.getConstraintPairs()); } - Vector> unifyResult = unifier.apply(alleUndConstraints); - + this.filterWrongConstraints( + (pairs)->{ + Vector undConstraintsUndPairs = new Vector<>(); + undConstraintsUndPairs.addAll(pairs); + undConstraintsUndPairs.addAll(alleUndConstraints); + log.debug("Versuche Pairs auszusondern:\n"+pairs, Section.TYPEINFERENCE); + log.debug("Unifiziere:\n"+undConstraintsUndPairs, Section.TYPEINFERENCE); + Vector> unifyResult = unifier.apply(undConstraintsUndPairs); + return unifyResult; + }); } /** diff --git a/src/de/dhbwstuttgart/typeinference/OderConstraint.java b/src/de/dhbwstuttgart/typeinference/OderConstraint.java index 84f59d6a4..1f2762d4f 100755 --- a/src/de/dhbwstuttgart/typeinference/OderConstraint.java +++ b/src/de/dhbwstuttgart/typeinference/OderConstraint.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference; import java.util.Vector; import de.dhbwstuttgart.logger.Logger; +import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.Type; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -90,13 +91,16 @@ public class OderConstraint{ if(!unifierResult.isEmpty()){ filteredConstraints.add(cons); }else{ - logger.debug("Ausgesondertes Constraint: "+cons); + logger.debug("Ausgesondertes Constraint: "+cons, Section.TYPEINFERENCE); } } this.oderConstraintPairs = filteredConstraints; } UndConstraint filterUndConstraints() { + if(this.oderConstraintPairs.size()==1){ + return this.oderConstraintPairs.firstElement(); + } return null; } diff --git a/src/de/dhbwstuttgart/typeinference/Pair.java b/src/de/dhbwstuttgart/typeinference/Pair.java index b3126aa46..114bc97ea 100755 --- a/src/de/dhbwstuttgart/typeinference/Pair.java +++ b/src/de/dhbwstuttgart/typeinference/Pair.java @@ -121,7 +121,7 @@ public class Pair if(OperatorSmallerExtends()) Operator = "