Explorar el Código

Add a JSON Formatter

Fabio Battaglia hace 5 años
padre
commit
a609a7723b

+ 7 - 1
pom.xml

@@ -6,7 +6,7 @@
 
   <groupId>info.hkzlab.dupal.analyzer</groupId>
   <artifactId>dupal-analyzer</artifactId>
-  <version>0.1.0</version>
+  <version>0.1.1</version>
 
   <name>dupal-analyzer</name>
   <url>https://github.com/DuPAL-PAL-DUmper</url>
@@ -24,6 +24,12 @@
       <version>2.9.2</version>
     </dependency>
 
+    <dependency>
+      <groupId>org.json</groupId>
+      <artifactId>json</artifactId>
+      <version>20200518</version>
+    </dependency>
+
     <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>

+ 11 - 18
src/main/java/info/hkzlab/dupal/analyzer/board/boardio/DuPALAnalyzer.java

@@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
 import info.hkzlab.dupal.analyzer.exceptions.*;
 import info.hkzlab.dupal.analyzer.palanalisys.explorers.OSExplorer;
 import info.hkzlab.dupal.analyzer.palanalisys.explorers.SimpleExplorer;
-import info.hkzlab.dupal.analyzer.palanalisys.formatter.EspressoFormatter;
+import info.hkzlab.dupal.analyzer.palanalisys.formatter.JSONFormatter;
 import info.hkzlab.dupal.analyzer.palanalisys.graph.OutState;
 import info.hkzlab.dupal.analyzer.palanalisys.simple.SimpleState;
 import info.hkzlab.dupal.analyzer.utilities.BitUtils;
@@ -79,6 +79,7 @@ public class DuPALAnalyzer {
     public void startAnalisys(boolean padTable) throws Exception {
         int board_revision = dpci.getBoardVersion();
         DuPALCmdInterface.DuPAL_LED led;
+        String formatterOutput = null;
        
         logger.info("startAnalisys() -> Espresso table results will"+(padTable?"":" not")+" be padded.");
         
@@ -94,19 +95,13 @@ public class DuPALAnalyzer {
 
         if(board_revision >= 2) dpci.setLED(led, true);
 
-        String header = null;
-        String[] table = null;
-        String footer = null;
-
-        footer = EspressoFormatter.formatEspressoFooter();
-
         try {
             if((dpci.palSpecs.getPinCount_IO() == 0) && (dpci.palSpecs.getPinCount_RO() == 0)) { // Purely combinatorial and no feedbacks, we can perform simple bruteforcing
                 SimpleState[] ssArray = SimpleExplorer.exploreStates(dpci);
-                table = EspressoFormatter.formatEspressoTable(dpci.palSpecs, ssArray);
-                header = EspressoFormatter.formatEspressoTableHeader(dpci.palSpecs, 0);
                 
                 logger.info("Got " + ssArray.length + " output states!");
+
+                formatterOutput = JSONFormatter.formatJSON(dpci.palSpecs, ssArray);
             } else { // Either registered, or with feedbacks
                 if(ioAsOutMask < 0) {
                     ioAsOutMask = detectIOTypeMask(dpci);
@@ -114,13 +109,12 @@ public class DuPALAnalyzer {
                 }
                 
                 OutState[] osArray = OSExplorer.exploreOutStates(dpci, ioAsOutMask);
-                header = EspressoFormatter.formatEspressoTableHeader(dpci.palSpecs, ioAsOutMask);
-                table = EspressoFormatter.formatEspressoTable(dpci.palSpecs, ioAsOutMask, osArray, padTable);
 
                 logger.info("Got " + osArray.length + " output states!");
+                formatterOutput = JSONFormatter.formatJSON(dpci.palSpecs, ioAsOutMask, osArray);
             }
 
-            saveTableToFile(outFile, header, table, footer);
+            saveOutputToFile(outFile, formatterOutput);
         } catch(Exception e) {
             throw e;
         } finally {
@@ -128,18 +122,17 @@ public class DuPALAnalyzer {
         }
     }
 
-    private void saveTableToFile(String destination, String header, String[] rows, String footer) throws IOException {
+    private void saveOutputToFile(String destination, String out) throws IOException {
         FileOutputStream fout = null;
         
-        logger.info("saveTableToFile() -> Saving to " + destination);
+        logger.info("saveOutputToFile() -> Saving to " + destination);
 
         try {
             fout = new FileOutputStream(outFile);
 
-            fout.write(header.getBytes(StandardCharsets.US_ASCII));
-            for(String row : rows) fout.write(row.getBytes(StandardCharsets.US_ASCII));
-            fout.write(footer.getBytes(StandardCharsets.US_ASCII));
-
+            fout.write(out.getBytes(StandardCharsets.US_ASCII));
+            
+            fout.flush();
             fout.close();
         } catch(IOException e) {
             logger.error("Error printing out the registered outputs table (not including outputs).");

+ 111 - 0
src/main/java/info/hkzlab/dupal/analyzer/palanalisys/formatter/JSONFormatter.java

@@ -0,0 +1,111 @@
+package info.hkzlab.dupal.analyzer.palanalisys.formatter;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import info.hkzlab.dupal.analyzer.devices.PALSpecs;
+import info.hkzlab.dupal.analyzer.palanalisys.graph.*;
+import info.hkzlab.dupal.analyzer.palanalisys.simple.SimpleState;
+
+public class JSONFormatter {
+    public static final int JSON_FORMAT_REVISION = 1;
+
+    private JSONFormatter() {};
+   
+    public static String formatJSON(PALSpecs pSpecs, SimpleState[] states) {
+        JSONObject rootObject = new JSONObject();
+        
+        // Fill in the header and insert it into the root object
+        rootObject.put("header", buildHeader(pSpecs));
+        JSONArray ssArray = new JSONArray();
+
+        for(SimpleState ss : states) {
+            ssArray.put(buildObjectFromSimpleState(ss));
+        }
+
+        rootObject.put("states", ssArray);
+
+        return rootObject.toString(4);
+    }
+
+    public static String formatJSON(PALSpecs pSpecs, int ioAsOutMask, OutState[] states) {
+        JSONObject rootObject = new JSONObject();
+        JSONArray osLinks = new JSONArray();
+        JSONArray regLinks = new JSONArray();
+
+        // Fill in the header and insert it into the root object
+        rootObject.put("header", buildHeader(pSpecs, ioAsOutMask));
+
+        for(OutState os : states) {
+            OutLink[] oLinks = os.getOutLinks();
+            RegLink[] rLinks = os.getRegLinks();
+
+            for(OutLink ol : oLinks) osLinks.put(buildObjectFromOutLink(ol));
+            for(RegLink rl : rLinks) osLinks.put(buildObjectFromRegLink(rl));
+        }
+
+        rootObject.put("oLinks", osLinks);
+        rootObject.put("rLinks", regLinks);
+
+        return rootObject.toString(4);
+    }
+
+    private static JSONObject buildHeader(PALSpecs pSpecs) {
+        return buildHeader(pSpecs, 0);
+    }
+
+    private static JSONObject buildHeader(PALSpecs pSpecs, int ioAsOutMask) {
+        JSONObject header = new JSONObject();
+        JSONObject palDetails = new JSONObject();
+
+        palDetails.put("type", pSpecs.toString());
+        if(pSpecs.getPinCount_IO() > 0) palDetails.put("IOsAsOUT", ioAsOutMask);
+        
+        header.put("revision", JSON_FORMAT_REVISION);
+        header.put("analyzerProgram", "DuPAL Analyzer");
+        header.put("analyzerVersion", JSONFormatter.class.getPackage().getImplementationVersion());
+        header.put("PAL", palDetails);
+
+        return header;
+    }
+
+    private static JSONObject buildObjectFromSimpleState(SimpleState ss) {
+        JSONObject ssObject = new JSONObject();
+
+        ssObject.put("inputs", ss.input);
+        ssObject.put("outputs", ss.output);
+        ssObject.put("hiz", ss.hiz);
+
+        return ssObject;
+    }
+
+    private static JSONObject buildObjectFromPINs(OutStatePins pins) {
+        JSONObject pinsObject = new JSONObject();
+
+        pinsObject.put("outputs", pins.out);
+        pinsObject.put("hiz", pins.hiz);
+
+        return pinsObject;
+    }
+
+    private static JSONObject buildObjectFromOutLink(OutLink ol) {
+        JSONObject linkObject = new JSONObject();
+
+        linkObject.put("inputs", ol.getLinkInputs());
+        linkObject.put("source", buildObjectFromPINs(ol.src.pins));
+        linkObject.put("destination", buildObjectFromPINs(ol.dest.pins));
+
+        return linkObject;
+    }
+
+    private static JSONObject buildObjectFromRegLink(RegLink rl) {
+        JSONObject linkObject = new JSONObject();
+
+        linkObject.put("inputs", rl.getLinkInputs());
+        linkObject.put("source", buildObjectFromPINs(rl.src.pins));
+        linkObject.put("middle", buildObjectFromPINs(rl.middle.pins));
+        linkObject.put("destination", buildObjectFromPINs(rl.dest.pins));
+
+        return linkObject;
+    }
+}