From c7ffabe0f091cae85fd62cc7a0c5c56806915563 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 14 Jul 2015 18:43:54 +0200 Subject: [PATCH] Im Unify wird nur noch bei SUBST deepCopy angewendet. Timer implementiert zum ermitteln der Zeitaufwendungen der einzelnen Schritte im Algorithmus --- src/de/dhbwstuttgart/logger/Timestamp.java | 50 +++++++++++++ src/de/dhbwstuttgart/logger/Timewatch.java | 74 +++++++++++++++++++ src/de/dhbwstuttgart/typeinference/Pair.java | 25 ++++++- .../typeinference/unify/Unify.java | 31 ++++++-- .../MultipleTypesInsertTester.java | 20 +++-- 5 files changed, 186 insertions(+), 14 deletions(-) create mode 100644 src/de/dhbwstuttgart/logger/Timestamp.java create mode 100644 src/de/dhbwstuttgart/logger/Timewatch.java diff --git a/src/de/dhbwstuttgart/logger/Timestamp.java b/src/de/dhbwstuttgart/logger/Timestamp.java new file mode 100644 index 00000000..829ccbd8 --- /dev/null +++ b/src/de/dhbwstuttgart/logger/Timestamp.java @@ -0,0 +1,50 @@ +package de.dhbwstuttgart.logger; + +import java.util.Stack; + + +public class Timestamp{ + String name; + + Stack timestamps = new Stack<>(); + Long accumulatedTime = 0L; + + Timestamp(String name){ + this.name = name; + } + + public void start(){ + timestamps.push(System.currentTimeMillis()); + } + + public Long stop(){ + if(timestamps.isEmpty())return 0L; + Long timeStart = timestamps.pop(); + Long currentTime = getTime(); + Long difference = currentTime-timeStart; + accumulatedTime += difference; + return difference; + } + + public Long currentTime(){ + if(timestamps.isEmpty())return 0L; + Long timeStart = timestamps.peek(); + Long currentTime = getTime(); + return currentTime-timeStart; + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof Timestamp))return false; + return this.name.equals(((Timestamp)obj).name); + } + + public String toString(){ + String ret = "Zeit für Aktivität "+this.name+": "+this.accumulatedTime; + return ret; + } + + private Long getTime(){ + return System.currentTimeMillis(); + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/logger/Timewatch.java b/src/de/dhbwstuttgart/logger/Timewatch.java new file mode 100644 index 00000000..26916fea --- /dev/null +++ b/src/de/dhbwstuttgart/logger/Timewatch.java @@ -0,0 +1,74 @@ +package de.dhbwstuttgart.logger; + +import java.util.HashMap; +import java.util.Stack; + +import de.dhbwstuttgart.typeinference.Menge; +import de.dhbwstuttgart.typeinference.exceptions.DebugException; + +public class Timewatch { + + private static final Timewatch time = new Timewatch(); + private final Timestamp startTime; + + private Timers timers = new Timers(); + + private Timewatch(){ + //Privater Konstruktor + startTime = new Timestamp("Timewatch Global"); + startTime.start(); + } + + public static Timewatch getTimewatch() { + return time; + } + + public String dumpTimeData(){ + String ret = "Momentan insgesamt verstrichene Zeit: "+this.startTime.currentTime() +"\n"; + ret += timers.toString(); + return ret; + } + + public Timestamp start(String name){ + if(!timers.contains(name)){ + Timestamp ret = new Timestamp(name); + timers.add(ret); + ret.start(); + return ret; + }else{ + //Timer läuft noch... einfach neue Instanz starten: + Timestamp ret = timers.get(name); + ret.start(); + return ret; + } + } + +} + +class Timers{ + private Menge timers = new Menge<>(); + + public Timestamp get(String name) { + for(Timestamp timer:timers){ + if(timer.name.equals(name))return timer; + } + throw new DebugException("Fehler in Timewatch: Der gesuchte Timer "+name+" ist nicht vorhanden."); + } + + public void add(Timestamp timer) { + timers.add(timer); + } + + public boolean contains(String name) { + return timers.contains(new Timestamp(name)); + } + + public String toString(){ + String ret =""; + for(Timestamp timer : timers){ + ret += timer.toString() + "\n"; + } + return ret; + } +} + diff --git a/src/de/dhbwstuttgart/typeinference/Pair.java b/src/de/dhbwstuttgart/typeinference/Pair.java index 863b3623..21f0e989 100755 --- a/src/de/dhbwstuttgart/typeinference/Pair.java +++ b/src/de/dhbwstuttgart/typeinference/Pair.java @@ -2,7 +2,14 @@ package de.dhbwstuttgart.typeinference; // ino.end // ino.module.Pair.8673.import +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.Hashtable; + import de.dhbwstuttgart.typeinference.Menge; // ino.end @@ -24,7 +31,7 @@ import de.dhbwstuttgart.syntaxtree.type.WildcardType; // Klasse, die ein Paar in der Menge Eq speichern kann // ino.end // ino.class.Pair.26540.declaration -public class Pair +public class Pair implements Serializable // ino.end // ino.class.Pair.26540.body { @@ -405,5 +412,21 @@ public class Pair ret.add(this.TA2); return ret; } + + public Pair deepClone() { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(this); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + return (Pair) ois.readObject(); + } catch (IOException e) { + return null; + } catch (ClassNotFoundException e) { + return null; + } + } } // ino.end diff --git a/src/de/dhbwstuttgart/typeinference/unify/Unify.java b/src/de/dhbwstuttgart/typeinference/unify/Unify.java index bd10be77..b7f21c23 100755 --- a/src/de/dhbwstuttgart/typeinference/unify/Unify.java +++ b/src/de/dhbwstuttgart/typeinference/unify/Unify.java @@ -20,6 +20,7 @@ import com.rits.cloning.Cloner; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.SectionLogger; +import de.dhbwstuttgart.logger.Timewatch; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.MatchException; @@ -664,14 +665,16 @@ public class Unify SectionLogger log = Logger.getSectionLogger(Unify.class.getName(), Section.UNIFY); if(filter){ - Cloner cloner = new Cloner(); + //Cloner cloner = new Cloner(); Menge>> temp = new Menge<>(); //hier werden gefilterte Constraints gesammelt Menge undMenge = new Menge(); //Die Menge von Pairs, welche in jedem Kartesischen Produkt enthalten sind. - undMenge.addAll(cloner.deepClone(Eq1)); + //undMenge.addAll(cloner.deepClone(Eq1)); + undMenge.addAll(Eq1); Menge>> oderConstraints = new Menge<>();//Die zu filternden Constraints for (Menge> vecvecpair : cartProduktSets){ if(vecvecpair.size() == 1){//gibt es nur eine UndMenge in diesem Set, dann kommt diese in jedem Karthesischen Produkt vor: - undMenge.addAll(cloner.deepClone(vecvecpair.firstElement())); + //undMenge.addAll(cloner.deepClone(vecvecpair.firstElement())); + undMenge.addAll(vecvecpair.firstElement()); temp.add(vecvecpair); }else{//gibt es mehrere Mengen, kann gefiltert werden: oderConstraints.add(vecvecpair); //die Menge zu den zu filternden OderConstraints anfügen @@ -682,8 +685,10 @@ public class Unify Menge> filteredOCons = new Menge<>(); //diese Menge sammelt nur Cons for(Menge pairs : oderConstraint){ Menge testMenge = new Menge(); - testMenge.addAll(cloner.deepClone(undMenge)); - testMenge.addAll(cloner.deepClone(pairs)); + //testMenge.addAll(cloner.deepClone(undMenge)); + //testMenge.addAll(cloner.deepClone(pairs)); + testMenge.addAll(undMenge); + testMenge.addAll(pairs); Menge> test = Unify.unifyFiltered(testMenge, fc_tto, false); if(test.size()>0){ filteredOCons.add(pairs); @@ -696,7 +701,7 @@ public class Unify } SetView>> difference = Sets.difference(cartProduktSets, temp); log.debug("Ausgelöschte Constraints: "+difference.toString()); - //cartProduktSets = temp; + cartProduktSets = temp; /* Unifier filterUnify = (pairs)->{ String pairsString = pairs.toString(); @@ -2462,6 +2467,18 @@ throws MatchException inferencelog.debug("TV: " + a.getName()); inferencelog.debug("Bedingung: " + bMitVorbedingung); + Cloner cloner = new Cloner(); + cloner.setDumpClonedClasses(true); + + SectionLogger log = Logger.getSectionLogger("Subst-Methode", Section.UNIFY); + Timewatch timer = Timewatch.getTimewatch(); + de.dhbwstuttgart.logger.Timestamp timestamp = timer.start("Unify-Subst"); + P = cloner.deepClone(P); + a = cloner.deepClone(a); + o = cloner.deepClone(o); + long time = timestamp.stop(); + log.debug("Benötigte Zeit für DeepClone: "+time); + // richtiger Typ aus Pair raussuchen Type T = null; if( nTypnrInPair == 1 ) @@ -2483,7 +2500,7 @@ throws MatchException { // Parameterliste durchgehen Menge vTemp = ((RefType)T).get_ParaList(); - Boolean ret = true; //EINGEFUEGT PL 14-01-16: Return darf erst am Ende zurückgegeben werden und nicht in den ifs + boolean ret = true; //EINGEFUEGT PL 14-01-16: Return darf erst am Ende zurückgegeben werden und nicht in den ifs //sonst werden nicht alle Elemente der Forschleife durchlaufen for( int i = 0; i < vTemp.size(); i++ ) { diff --git a/test/plugindevelopment/TypeInsertTests/MultipleTypesInsertTester.java b/test/plugindevelopment/TypeInsertTests/MultipleTypesInsertTester.java index 1dca7ed6..13e54315 100755 --- a/test/plugindevelopment/TypeInsertTests/MultipleTypesInsertTester.java +++ b/test/plugindevelopment/TypeInsertTests/MultipleTypesInsertTester.java @@ -12,6 +12,7 @@ import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.LoggerConfiguration; import de.dhbwstuttgart.logger.Section; import de.dhbwstuttgart.logger.SectionLogger; +import de.dhbwstuttgart.logger.Timewatch; import de.dhbwstuttgart.parser.JavaParser.yyException; import de.dhbwstuttgart.typeinference.TypeinferenceResultSet; import de.dhbwstuttgart.typeinference.typedeployment.TypeInsertPoint; @@ -69,12 +70,7 @@ public class MultipleTypesInsertTester extends TypeInsertTester{ for(String containString : mustContain){ TestCase.assertTrue("\""+containString+"\" muss in den inferierten Lösungen vorkommen",gesamterSrc.contains(containString)); } - try { - Files.write(Logger.getWholeLog().getBytes(),new File(rootDirectory+sourceFileToInfere+".log")); - } catch (IOException e) { - e.printStackTrace(); - TestCase.fail(); - } + writeLog(sourceFileToInfere+".log"); } public static void testSingleInsert(String sourceFileToInfere, Menge mustContain){ @@ -106,5 +102,17 @@ public class MultipleTypesInsertTester extends TypeInsertTester{ for(String containString : mustContain){ TestCase.assertTrue("\""+containString+"\" muss in den inferierten Lösungen vorkommen",gesamterSrc.contains(containString)); } + writeLog(sourceFileToInfere+".log"); + } + + private static void writeLog(String toFile){ + String log = Logger.getWholeLog()+"\n"; + log+=Timewatch.getTimewatch().dumpTimeData(); + try { + Files.write(log.getBytes(),new File(rootDirectory+toFile)); + } catch (IOException e) { + e.printStackTrace(); + TestCase.fail(); + } } }