From 788e830269d5e21c43ae56268135c7822e46f6b2 Mon Sep 17 00:00:00 2001 From: uuqjz Date: Wed, 23 Oct 2024 13:06:05 +0200 Subject: [PATCH] Split DFD and Web Converter --- .../converter/DataFlowDiagramConverter.java | 317 +--------------- .../converter/WebEditorConverter.java | 355 ++++++++++++++++++ .../converter/tests/AnnotationsTest.java | 3 - .../converter/tests/BehaviorTest.java | 2 - .../converter/tests/MicroSecEndTest.java | 7 +- .../converter/tests/PCMTest.java | 1 - .../converter/tests/WebEditorTest.java | 25 +- 7 files changed, 374 insertions(+), 336 deletions(-) create mode 100644 bundles/org.dataflowanalysis.converter/src/org/dataflowanalysis/converter/WebEditorConverter.java diff --git a/bundles/org.dataflowanalysis.converter/src/org/dataflowanalysis/converter/DataFlowDiagramConverter.java b/bundles/org.dataflowanalysis.converter/src/org/dataflowanalysis/converter/DataFlowDiagramConverter.java index 2f18067..c03ee29 100644 --- a/bundles/org.dataflowanalysis.converter/src/org/dataflowanalysis/converter/DataFlowDiagramConverter.java +++ b/bundles/org.dataflowanalysis.converter/src/org/dataflowanalysis/converter/DataFlowDiagramConverter.java @@ -4,22 +4,15 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; - import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.function.Predicate; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; import org.apache.log4j.Logger; import org.dataflowanalysis.analysis.core.AbstractTransposeFlowGraph; import org.dataflowanalysis.analysis.core.AbstractVertex; @@ -46,9 +39,6 @@ public class DataFlowDiagramConverter extends Converter { private Map> inputPinToFlowNamesMap; - private final dataflowdiagramFactory dfdFactory; - private final datadictionaryFactory ddFactory; - private Map idToNodeMap; private final Logger logger = Logger.getLogger(DataFlowDiagramConverter.class); protected final static String DELIMITER_PIN_NAME = "|"; @@ -56,29 +46,6 @@ public class DataFlowDiagramConverter extends Converter { private BehaviorConverter behaviorConverter; - public DataFlowDiagramConverter() { - dfdFactory = dataflowdiagramFactory.eINSTANCE; - ddFactory = datadictionaryFactory.eINSTANCE; - } - - /** - * Converts a Web Editor DFD format file into a DataFlowDiagramAndDictionary object. - * @param inputFile The path of the input file in Web Editor DFD format. - * @return DataFlowDiagramAndDictionary object representing the converted data flow diagram and dictionary. - */ - public DataFlowDiagramAndDictionary webToDfd(String inputFile) { - return webToDfd(loadWeb(inputFile).get()); - } - - /** - * Converts a WebEditorDfd object into a DataFlowDiagramAndDictionary object. - * @param inputFile The WebEditorDfd object to convert. - * @return DataFlowDiagramAndDictionary object representing the converted data flow diagram and dictionary. - */ - public DataFlowDiagramAndDictionary webToDfd(WebEditorDfd inputFile) { - return processWeb(inputFile); - } - /** * Converts Data Flow Diagram and Data Dictionary provided via paths into a WebEditorDfd object, analyzes it and annotates the propagated labels in the WebDFD. * @param inputDataFlowDiagram The path of the data flow diagram. @@ -149,8 +116,6 @@ public WebEditorDfd dfdToWebAndAnalyzeAndAnnotateWithCustomTFGFinder(DataFlowDia return processDfd(complete.dataFlowDiagram(), complete.dataDictionary(), createNodeAnnotationMap(complete, conditions, finderClass)); } - - /** * Stores a WebEditorDfd object into a specified output file. * @param web The WebEditorDfd object to store. @@ -166,25 +131,7 @@ public void storeWeb(WebEditorDfd web, String outputFile) { } catch (IOException e) { logger.error("Could not store web dfd:", e); } - } - - - /** - * Loads a WebEditorDfd object from a specified input file. - * @param inputFile The path of the input file. - * @return Optional containing the loaded WebEditorDfd object; empty if an error occurs. - */ - public Optional loadWeb(String inputFile) { - objectMapper = new ObjectMapper(); - file = new File(inputFile); - try { - WebEditorDfd result = objectMapper.readValue(file, WebEditorDfd.class); - return Optional.ofNullable(result); // This will never be null given readValue's behavior, but it's a safe usage pattern. - } catch (IOException e) { - logger.error("Could not load web dfd:", e); - return Optional.empty(); - } - } + } /** * Loads a data flow diagram and data dictionary from specified input files and returns them as a combined object. @@ -269,169 +216,7 @@ private Map createNodeAnnotationMap (DataFlowDiagramAndDiction return mapNodeToAnnotations; } - private DataFlowDiagramAndDictionary processWeb(WebEditorDfd webdfd) { - idToNodeMap = new HashMap<>(); - Map pinToNodeMap = new HashMap<>(); - Map idToPinMap = new HashMap<>(); - Map idToLabelMap = new HashMap<>(); - Map> nodeOutpinBehaviorMap = new HashMap<>(); - - DataFlowDiagram dataFlowDiagram = dfdFactory.createDataFlowDiagram(); - DataDictionary dataDictionary = ddFactory.createDataDictionary(); - - behaviorConverter = new BehaviorConverter(dataDictionary); - - createWebLabelTypes(webdfd, idToLabelMap, dataDictionary); - - createWebNodes(webdfd, pinToNodeMap, idToPinMap, idToLabelMap, nodeOutpinBehaviorMap, dataFlowDiagram, dataDictionary); - - createWebFlows(webdfd, pinToNodeMap, idToPinMap, dataFlowDiagram); - - List nodesInBehavior = nodeOutpinBehaviorMap.keySet() - .stream() - .collect(Collectors.toList()); - - nodesInBehavior.forEach(node -> { - Map outpinBehaviors = nodeOutpinBehaviorMap.get(node); - outpinBehaviors.forEach((outpin, behavior) -> { - parseBehavior(node, outpin, behavior, dataFlowDiagram, dataDictionary); - }); - }); - - return new DataFlowDiagramAndDictionary(dataFlowDiagram, dataDictionary); - } - - private void createWebNodes(WebEditorDfd webdfd, Map pinToNodeMap, Map pinMap, Map idToLabelMap, - Map> nodeOutpinBehavior, DataFlowDiagram dataFlowDiagram, DataDictionary dataDictionary) { - for (Child child : webdfd.model() - .children()) { - String[] type = child.type() - .split(":"); - String name = child.text(); - - if (type[0].equals("node")) { - Node node = switch (type[1]) { - case "function" -> dfdFactory.createProcess(); - case "storage" -> dfdFactory.createStore(); - case "input-output" -> dfdFactory.createExternal(); - default -> { - logger.error("Unrecognized node type: " + type[1]); - continue; - } - }; - - node.setEntityName(name); - node.setId(child.id()); - - var behaviour = ddFactory.createBehaviour(); - behaviour.setEntityName(name); - node.setBehaviour(behaviour); - dataDictionary.getBehaviour() - .add(behaviour); - - createWebPins(pinToNodeMap, pinMap, nodeOutpinBehavior, child, node); - - List