You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by re...@apache.org on 2015/07/29 05:17:10 UTC

svn commit: r1693179 - in /devicemap/trunk/clients/2.0/reference/src: DeviceMapClient.java JsonFile.java Main.java

Author: rezan
Date: Wed Jul 29 03:17:10 2015
New Revision: 1693179

URL: http://svn.apache.org/r1693179
Log:
pattern file parsing

Modified:
    devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
    devicemap/trunk/clients/2.0/reference/src/JsonFile.java
    devicemap/trunk/clients/2.0/reference/src/Main.java

Modified: devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java?rev=1693179&r1=1693178&r2=1693179&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java Wed Jul 29 03:17:10 2015
@@ -17,14 +17,101 @@
  under the License.
  */
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.codehaus.jackson.JsonNode;
+
 public class DeviceMapClient {
   
   public final static String VERSION = "2.0";
 
+  private String domain;
+  private String domainVersion;
+
+  private List<String> transformers;
+  private List<String> tokenSeperators;
+  private int ngramConcatSize;
+
+  private String defaultId;
+
   public DeviceMapClient() {
+    domain = null;
+    domainVersion = null;
+
+    transformers = new ArrayList<String>();
+    tokenSeperators = new ArrayList<String>();
+    ngramConcatSize = 1;
+
+    defaultId = null;
   }
 
-  public void loadPatterns(JsonFile patterns) {
+  public void loadPatterns(JsonFile patterns) throws Exception {
+    if(!patterns.getType().equals("pattern")) {
+      throw new Exception("Unknown pattern file type: " + patterns.getType());
+    }
+    
+    boolean patch = false;
+
+    if(domain == null) {
+      domain = patterns.getDomain();
+    } else if(!domain.equals(patterns.getDomain())) {
+      throw new Exception("Domains do not match: " + domain + " != " + patterns.getDomain());
+    } else {
+      patch = true;
+    }
+
+    if(domainVersion == null) {
+      domainVersion = patterns.getDomainVersion();
+    } else if(!domainVersion.equals(patterns.getDomainVersion())) {
+      throw new Exception("DomainVersions do not match: " + domainVersion + " != " + patterns.getDomainVersion());
+    }
+
+    System.out.println("  Loading pattern domain: " + patterns.getDomain() + ", version: " +
+        patterns.getDomainVersion() + (patch ? ", patch" : ""));
+
+    if(patterns.getJsonNode().get("inputParser").isObject()) {
+      JsonNode inputParser = patterns.getJsonNode().get("inputParser");
+
+      if(inputParser.get("transformers").isArray()) {
+        for(Iterator<JsonNode> i = inputParser.get("transformers").iterator(); i.hasNext();) {
+          JsonNode transformer = i.next();
+          transformers.add(transformer.get("type").asText());
+          System.out.println("  Found transformer: " + transformer.get("type"));
+        }
+      }
+
+      if(inputParser.get("tokenSeperators").isArray()) {
+        for(Iterator<JsonNode> i = inputParser.get("tokenSeperators").iterator(); i.hasNext();) {
+          JsonNode tokenSeperator = i.next();
+          tokenSeperators.add(tokenSeperator.asText());
+          System.out.println("  Found tokenSeperator: '" + tokenSeperator.asText() + "'");
+        }
+      }
+
+      if(inputParser.get("ngramConcatSize").asInt(0) > 0) {
+        ngramConcatSize = inputParser.get("ngramConcatSize").asInt(0);
+        System.out.println("  Found ngramConcatSize: " + ngramConcatSize);
+      }
+    }
+
+    if(patterns.getJsonNode().get("patternSet").isObject()) {
+      JsonNode patternSet = patterns.getJsonNode().get("patternSet");
+
+      if(patternSet.get("defaultId").isTextual()) {
+        defaultId = patternSet.get("defaultId").asText();
+        System.out.println("  Found defaultId: " + defaultId);
+      }
+
+      int patternCount = 0;
+      if(patternSet.get("patterns").isArray()) {
+        for(Iterator<JsonNode> i = patternSet.get("patterns").iterator(); i.hasNext();) {
+          JsonNode tokenSeperator = i.next();
+          patternCount++;
+        }
+        System.out.println("  Found " + patternCount + " pattern(s)");
+      }
+    }
   }
 
   public void loadAttributes(JsonFile attributes) {

Modified: devicemap/trunk/clients/2.0/reference/src/JsonFile.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/JsonFile.java?rev=1693179&r1=1693178&r2=1693179&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/JsonFile.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/JsonFile.java Wed Jul 29 03:17:10 2015
@@ -18,7 +18,6 @@
  */
 
 import java.io.File;
-import java.io.IOException;
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
 
@@ -26,8 +25,52 @@ public class JsonFile {
   
   private JsonNode json;
 
-  public JsonFile(String path) throws IOException {
+  private String type;
+  private String domain;
+  private String domainVersion;
+
+  public JsonFile(String path) throws Exception {
     ObjectMapper mapper = new ObjectMapper();
     json = mapper.readTree(new File(path));
+
+    if(!json.isObject()) {
+      throw new Exception("JsonFile is not an object");
+    }
+
+    if(json.get("specVersion").asDouble(0d) != 2.0d) {
+      throw new Exception("Bad specVersion found: " + json.get("specVersion").asDouble(0d));
+    }
+
+    if(json.get("type") == null || json.get("type").asText().isEmpty()) {
+      throw new Exception("type not defined");
+    }
+
+    if(json.get("domain") == null || json.get("domain").asText().isEmpty()) {
+      throw new Exception("domain not defined");
+    }
+
+    if(json.get("domainVersion") == null || json.get("domainVersion").asText().isEmpty()) {
+      throw new Exception("domainVersion not defined");
+    }
+
+    type = json.get("type").asText();
+    domain = json.get("domain").asText();
+    domainVersion = json.get("domainVersion").asText();
+  }
+
+  public String getType() {
+    return type;
+  }
+
+  public String getDomain() {
+    return domain;
+  }
+
+  public String getDomainVersion() {
+    return domainVersion;
+  }
+
+  public JsonNode getJsonNode() {
+    return json;
   }
 }

Modified: devicemap/trunk/clients/2.0/reference/src/Main.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Main.java?rev=1693179&r1=1693178&r2=1693179&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Main.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/Main.java Wed Jul 29 03:17:10 2015
@@ -49,13 +49,13 @@ public class Main {
         testString = option;
       } else {
         printHelp();
-        throw new RuntimeException("unknown option: " + option);
+        throw new Exception("unknown option: " + option);
       }
     }
 
     if(patterns.isEmpty()) {
       printHelp();
-      throw new RuntimeException("Pattern file required");
+      throw new Exception("Pattern file required");
     }
 
     for(String pattern : patterns) {
@@ -88,11 +88,11 @@ public class Main {
     System.out.println("");
   }
 
-  private static String getParam(String args[], int pos, String error) {
+  private static String getParam(String args[], int pos, String error) throws Exception {
     if(pos >= args.length) {
-      throw new RuntimeException(error);
+      throw new Exception(error);
     } else if(args[pos].startsWith("-") || args[pos].isEmpty()) {
-      throw new RuntimeException(error);
+      throw new Exception(error);
     } else {
       return args[pos];
     }