From a02e5a16a84b37a0c66c1f9970fe3d440c1d3f8f Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 31 Oct 2018 16:07:37 +0100 Subject: [PATCH] Transivitaet-Algo version 1 --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 165 ++++++++++++++++-- test/bytecode/javFiles/Tph.jav | 2 +- 2 files changed, 154 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 62fb86ce..641db6dd 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import de.dhbwstuttgart.exceptions.NotImplementedException; @@ -315,8 +316,11 @@ public class BytecodeGen implements ASTVisitor { // * substitute L with R in all constraint // b)no => go to next step // 2. check the result of step 1 if there are any equal-constraints like L=R, M=R .. - // a) yes + // a) yes => put all such TPhs in a map and define "key-Cons" + // -- key-Cons = TPH < Object -- + // put this Constraint and the // b) no + // 3. is ArrayList consToRemove = new ArrayList<>(); // step 1: for(TPHConstraint c : allCons) { @@ -338,10 +342,15 @@ public class BytecodeGen implements ASTVisitor { allCons.forEach(c->System.out.println(c.toString())); System.out.println("----------------"); allCons.removeAll(consToRemove); + consToRemove = new ArrayList<>(); + + int size = allCons.size(); + System.out.println("AFTER DELETE ALL CONST: " + allCons.size()); allCons.forEach(c->System.out.println(c.toString())); System.out.println("----------------"); HashMap> result = new HashMap<>(); + for(TPHConstraint c : allCons) { if(c.getRel()==Relation.EQUAL) { HashSet equalTPHs = getEqualsTPHs(result, c); @@ -349,6 +358,8 @@ public class BytecodeGen implements ASTVisitor { equalTPHs.add(c.getLeft()); equalTPHs.add(c.getRight()); result.put(constraint, equalTPHs); + consToRemove.add(c); + size--; } } System.out.println("Step 2 Result: "); @@ -360,9 +371,150 @@ public class BytecodeGen implements ASTVisitor { System.out.println(); }); System.out.println("----------------"); + allCons.removeAll(consToRemove); + allCons.addAll(result.keySet()); + + if(allCons.size()<2) { + + if(!result.containsKey(allCons.get(0))) + result.put(allCons.get(0), null); + + return result; + } + + size += result.keySet().size(); + + for(TPHConstraint c : allCons) { + if(c.getRight().equals(Type.getInternalName(Object.class))) + size--; + } + + ArrayList methodTphs = new ArrayList<>(); + for(MethodAndTPH m : tphExtractor.ListOfMethodsAndTph) { + if(m.getName().equals(name)) { + methodTphs = m.getTphs(); + break; + } + } + + HashMap subAndSuper = new HashMap<>(); + for(TPHConstraint c : allCons) { + if(subAndSuper.containsKey(c.getLeft())) { + LinkedList all = new LinkedList<>(); + all.add(c.getLeft()); + String sup =c.getRight(); + all.add(sup); + HashMap ss = new HashMap<>(); + for(TPHConstraint constr : allCons) { + ss.put(constr.getLeft(), constr.getRight()); + } + while(ss.containsKey(sup)) { + sup = ss.get(sup); + all.add(sup); + } + if(!containTPH(methodTphs, all.getLast())) + continue; + } + subAndSuper.put(c.getLeft(), c.getRight()); + } + + int numOfVisitedPairs = 0; + for(String sub : subAndSuper.keySet()) { + if(isTPHInConstraint(result,sub)) + continue; + + if(!containTPH(methodTphs,sub)) + continue; + + if(numOfVisitedPairs>=size) + break; + LinkedList tphInRel = new LinkedList<>(); + tphInRel.add(sub); + String superT = subAndSuper.get(sub); + tphInRel.add(superT); + + numOfVisitedPairs++; + boolean isCycle = false; + while(subAndSuper.containsKey(superT)) { + superT = subAndSuper.get(superT); + if(tphInRel.contains(superT)) { + isCycle = true; + break; + } + tphInRel.add(superT); + numOfVisitedPairs++; + } + + // Subtype + String subTphRes = tphInRel.getFirst(); + // Die größte Supertype + String superTphRes = tphInRel.getLast(); + + while(subAndSuper.containsValue(subTphRes)) { + for(String tph : subAndSuper.keySet()) { + if(containTPH(methodTphs,tph) && subAndSuper.get(tph).equals(subTphRes)) { + subTphRes = tph; + break; + } + } + if(subTphRes.equals(tphInRel.getFirst())) { + break; + } + tphInRel.addFirst(subTphRes); + numOfVisitedPairs++; + } + + subTphRes = tphInRel.getFirst(); + + int i = 2; + while(!containTPH(methodTphs,superTphRes) && (tphInRel.size()-i) >0) { + superTphRes = tphInRel.get(tphInRel.size()-i); + i++; + } + + if(!containTPH(methodTphs, superTphRes)) { + result.put(new ExtendsConstraint(subTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), null); + } else { + result.put(new ExtendsConstraint(subTphRes, superTphRes, Relation.EXTENDS), null); + result.put(new ExtendsConstraint(superTphRes, Type.getInternalName(Object.class), Relation.EXTENDS), null); + } + } + System.out.println("ZwischenResult: "); + result.forEach((c,hs)->{ + if(c!=null) { + System.out.print(c.toString() + " -> "); + if(hs == null) { + System.out.print(" [] "); + }else { + hs.forEach(s->{ + System.out.print(s + ", "); + }); + } + } + + + System.out.println(); + }); + System.out.println("----------------"); return result; } + private boolean isTPHInConstraint(HashMap> result, String sub) { + for(TPHConstraint c : result.keySet()) { + if(c.getLeft().equals(sub)) + return true; + } + return false; + } + + private boolean containTPH(ArrayList methodTphs, String sub) { + for(TypePlaceholder tph : methodTphs) { + if(tph.getName().equals(sub)) + return true; + } + return false; + } + private TPHConstraint getKeyConstraint(HashMap> result, TPHConstraint toFind) { for(TPHConstraint c : result.keySet()) { if(c.containTPH(toFind.getLeft()) || c.containTPH(toFind.getRight())) @@ -379,16 +531,6 @@ public class BytecodeGen implements ASTVisitor { return new HashSet<>(); } - private String getBound(ArrayList allCons, String right) { - String bound = Type.getInternalName(Object.class); - for(TPHConstraint c: allCons) { - if(c.getRel() == Relation.EXTENDS && c.getLeft().equals(right)) { - return c.getRight(); - } - } - return bound; - } - private ArrayList simplifyPairs(String methodName, ArrayList allPairs, ArrayList allCons) { allPairs.forEach(p->System.out.print(p.TA1 + " < "+ p.TA2+ " ; ")); @@ -638,7 +780,6 @@ public class BytecodeGen implements ASTVisitor { fv.visitEnd(); } - // access flages?? modifiers @Override public void visit(Field field) { System.out.println("In Field ---"); diff --git a/test/bytecode/javFiles/Tph.jav b/test/bytecode/javFiles/Tph.jav index 1160e1a5..9faa570c 100644 --- a/test/bytecode/javFiles/Tph.jav +++ b/test/bytecode/javFiles/Tph.jav @@ -2,7 +2,7 @@ public class Tph { m(a,b){ var c = m2(b); - return c; + return a; // return m2(b); }