package tests; import static org.junit.jupiter.api.Assertions.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.HeadlessException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.JFrame; import org.junit.jupiter.api.Test; import org.ntlab.deltaExtractor.Alias; import org.ntlab.deltaExtractor.DeltaExtractorJSON; import org.ntlab.deltaExtractor.ExtractedStructure; import org.ntlab.deltaExtractor.IAliasCollector; import org.ntlab.deltaExtractor.IAliasTracker; import org.ntlab.deltaViewer.CollaborationAliasCollector; import org.ntlab.deltaViewer.CollaborationLayout; import org.ntlab.deltaViewer.CollaborationObjectCallGraph; import org.ntlab.deltaViewer.CollaborationViewer; import org.ntlab.deltaViewer.DeltaAliasCollector; import org.ntlab.trace.MethodExecution; import org.ntlab.trace.ObjectReference; import org.ntlab.trace.Reference; import org.ntlab.trace.ThreadInstance; import org.ntlab.trace.TraceJSON; import org.ntlab.trace.TracePoint; class MagnetRONFrameTest extends JFrame { // Delta Extract Type protected static final String CONTAINER_COMPONENT = "Container-Component"; protected static final String CONTAINER_COMPONENT_COLLECTION = "Container-Component(Collection)"; private static Dimension DEFAULT_SIZE = new Dimension(1300, 700); private static String WINDOW_TITLE = "Delta Viewer"; private CollaborationViewer viewer = null; private boolean visible = false; public MagnetRONFrameTest() { } public MagnetRONFrameTest(boolean visible) throws HeadlessException { super(WINDOW_TITLE); setSize(DEFAULT_SIZE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); this.viewer = new CollaborationViewer(); getContentPane().add(viewer, BorderLayout.CENTER); pack(); this.visible = true; } public void startAnimation() { if (visible) { // Change Here! // String key = "getterOverlap"; String key = "setterOverlap"; Map<ExtractedStructure, IAliasCollector> extractedPluralDelta = extractPluralDelta(key); List<ExtractedStructure> eList = new ArrayList<>(extractedPluralDelta.keySet()); List<IAliasCollector> dacList = new ArrayList<>(extractedPluralDelta.values()); CollaborationObjectCallGraph cocg = null; CollaborationAliasCollector cac = null; for (ExtractedStructure e: eList) { if (cocg == null) { cocg = new CollaborationObjectCallGraph(e); } else { cocg.merge(e); } } for (IAliasCollector dac: dacList) { if (cac == null) { cac = new CollaborationAliasCollector(dac); } else { cac.merge(dac); } } // new Thread() { // public void run() { startCollectionViewer(cocg, cac); // } // }.start(); } } public Map<ExtractedStructure, IAliasCollector> extractPluralDelta(String key) { Map<ExtractedStructure, IAliasCollector> extractedPluralDelta = new HashMap<>(); Map<String, String[]> argsMap = new HashMap<>(); setArgmentsForDeltaExtract(argsMap); Map<String, List<String>> argsKeysMap = new HashMap<>(); setArgsKeysForPluralDeltaExtract(argsKeysMap, argsMap); List<String> argsKeys = argsKeysMap.get(key); TraceJSON trace = new TraceJSON(argsMap.get(argsKeys.get(0))[4]); DeltaExtractorJSON s = new DeltaExtractorJSON(trace); for (String argsKey: argsKeys) { List<ExtractedStructure> eList = new ArrayList<>(); List<IAliasCollector> dacList = new ArrayList<>(); if (argsMap.get(argsKey)[5] == CONTAINER_COMPONENT || argsMap.get(argsKey)[5] == CONTAINER_COMPONENT_COLLECTION) { HashMap<String, ThreadInstance> threads = trace.getAllThreads(); if (threads.values().size() == 1) { ThreadInstance thread = threads.values().iterator().next(); // 最後のスレッドを見ているだけ… TracePoint tp = thread.getRoot().get(thread.getRoot().size() - 1).getExitPoint(); Reference reference = new Reference(argsMap.get(argsKey)[0], argsMap.get(argsKey)[1], argsMap.get(argsKey)[2], argsMap.get(argsKey)[3]); IAliasTracker dac = new DeltaAliasCollector(); ExtractedStructure e = s.extract(reference, tp, dac); eList.add(e); dacList.add(dac); } else { for (ThreadInstance thread: threads.values()) { TracePoint tp = thread.getRoot().get(thread.getRoot().size() - 1).getExitPoint(); Reference reference = new Reference(argsMap.get(argsKey)[0], argsMap.get(argsKey)[1], argsMap.get(argsKey)[2], argsMap.get(argsKey)[3]); if (argsMap.get(argsKey)[5] == CONTAINER_COMPONENT_COLLECTION) { reference.setCollection(true); } // reference.setArray(true); // reference.setFinalLocal(true); // reference.setCreation(true); IAliasTracker dac = new DeltaAliasCollector(); ExtractedStructure e = s.extract(reference, tp, dac); System.out.println(e); if (e != null) { eList.add(e); dacList.add(dac); System.out.println("add" + eList.size() + ", " + dacList.size()); } System.out.println("---------------------------"); } } } else { IAliasTracker dac = new DeltaAliasCollector(); MethodExecution me = trace.getLastMethodExecution(argsMap.get(argsKey)[2]); Map<ObjectReference, TracePoint> refs = me.getObjectReferences(argsMap.get(argsKey)[3]); ObjectReference ref = refs.keySet().iterator().next(); ExtractedStructure e = s.extract(refs.get(ref), ref, dac); eList.add(e); dacList.add(dac); } extractedPluralDelta.put(eList.get(0), dacList.get(0)); } return extractedPluralDelta; } public void startCollectionViewer(CollaborationObjectCallGraph cocg, IAliasCollector cac) { List<Alias> aliasList = new ArrayList<>(cac.getAliasList()); if(cocg != null) { List<TracePoint> relatedPoints = cocg.getRelatedPoints(); TracePoint relatedPoint = relatedPoints.get(relatedPoints.size()-1); // if (srcSide.size() >= 1 && dstSide.size() >= 1) { // WINDOW_TITLE = "extract delta of:" + e.getDelta().getSrcSide().get(0).getDstClassName() + "(" + e.getDelta().getSrcSide().get(0).getDstObjectId() + ")" + " -> " + e.getDelta().getDstSide().get(0).getDstClassName() + "(" + e.getDelta().getDstSide().get(0).getDstObjectId() + ")"; // setTitle(WINDOW_TITLE); // } } viewer.init(cocg, cac, new CollaborationLayout()); viewer.initAnimation(); for (int i = 0; i <= aliasList.size(); i++) { viewer.stepToAnimation(i); } } private void setArgmentsForDeltaExtract(Map<String, String[]> map){ // Samples of Plural Delta String[] getterOverlap1 = {null, null, "getterOverlap.F","getterOverlap.D", "traces/getterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("getterOverlap1", getterOverlap1); String[] getterOverlap2 = {null, null, "getterOverlap.G","getterOverlap.D", "traces/getterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("getterOverlap2", getterOverlap2); String[] setterOverlap1 = {null, null, "setterOverlap.F","setterOverlap.C", "traces/setterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("setterOverlap1", setterOverlap1); String[] setterOverlap2 = {null, null, "setterOverlap.G","setterOverlap.C", "traces/setterOverlap.txt", CONTAINER_COMPONENT_COLLECTION}; map.put("setterOverlap2", setterOverlap2); } private void setArgsKeysForPluralDeltaExtract(Map<String, List<String>> argsKeysMap, Map<String, String[]> argsMap){ // Samples of Plural Delta argsKeysMap.put("getterOverlap", new ArrayList<String>(Arrays.asList("getterOverlap1", "getterOverlap2"))); argsKeysMap.put("setterOverlap", new ArrayList<String>(Arrays.asList("setterOverlap1", "setterOverlap2"))); } }