Author: rezan
Date: Wed Jul 29 23:34:53 2015
New Revision: 1693360
URL: http://svn.apache.org/r1693360
Log:
patterns
Added:
devicemap/trunk/clients/2.0/reference/src/Pattern.java
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
devicemap/trunk/clients/2.0/reference/src/Transformer.java
devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.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=1693360&r1=1693359&r2=1693360&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java Wed Jul 29
23:34:53 2015
@@ -18,17 +18,16 @@
*/
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.map.ObjectMapper;
public class DeviceMapClient {
public final static String VERSION = "2.0";
- private static JsonNode nullNode = null;
-
private String domain;
private String domainVersion;
@@ -36,6 +35,8 @@ public class DeviceMapClient {
private List<String> tokenSeperators;
private int ngramConcatSize;
+ private Map<String, List<Pattern>> patterns;
+
private String defaultId;
public DeviceMapClient() {
@@ -46,37 +47,39 @@ public class DeviceMapClient {
tokenSeperators = new ArrayList<>();
ngramConcatSize = 1;
+ patterns = new HashMap<>();
+
defaultId = null;
}
- public void loadPatterns(JsonFile patterns) throws Exception {
- if(!patterns.getType().equals("pattern")) {
- throw new Exception("Unknown pattern file type: " + patterns.getType());
+ public void loadPatterns(JsonFile patternFile) throws Exception {
+ if(!patternFile.getType().equals("pattern")) {
+ throw new Exception("Unknown pattern file type: " +
patternFile.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());
+ domain = patternFile.getDomain();
+ } else if(!domain.equals(patternFile.getDomain())) {
+ throw new Exception("Domains do not match: " + domain + " != " +
patternFile.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());
+ domainVersion = patternFile.getDomainVersion();
+ } else if(!domainVersion.equals(patternFile.getDomainVersion())) {
+ throw new Exception("DomainVersions do not match: " + domainVersion + "
!= " + patternFile.getDomainVersion());
}
- Main.log("Loading pattern domain: " + patterns.getDomain() + ", version: "
+
- patterns.getDomainVersion() + (patch ? ", patch" : ""), 1);
+ Main.log("Loading pattern domain: " + patternFile.getDomain() + ",
version: " +
+ patternFile.getDomainVersion() + (patch ? ", patch" : ""), 1);
- if(get(patterns.getJsonNode(), "inputParser").isObject()) {
- JsonNode inputParser = patterns.getJsonNode().get("inputParser");
+ if(JsonFile.get(patternFile.getJsonNode(), "inputParser").isObject()) {
+ JsonNode inputParser = patternFile.getJsonNode().get("inputParser");
- if(get(inputParser, "transformers").isArray()) {
+ if(JsonFile.get(inputParser, "transformers").isArray()) {
if(patch) {
transformers = new ArrayList<>();
}
@@ -94,7 +97,7 @@ public class DeviceMapClient {
Main.log("Found " + transformers.size() + " transformer(s)", 1);
- if(get(inputParser, "tokenSeperators").isArray()) {
+ if(JsonFile.get(inputParser, "tokenSeperators").isArray()) {
if(patch) {
tokenSeperators = new ArrayList<>();
}
@@ -116,41 +119,61 @@ public class DeviceMapClient {
Main.log("Found " + tokenSeperators.size() + " tokenSeperator(s)", 1);
- if(get(inputParser, "ngramConcatSize").asInt(0) > 0) {
- ngramConcatSize = inputParser.get("ngramConcatSize").asInt(0);
+ if(inputParser.get("ngramConcatSize") != null) {
+ String ngramConcatSizeStr =
inputParser.get("ngramConcatSize").asText();
+ ngramConcatSize = Integer.parseInt(ngramConcatSizeStr);
+
+ if(ngramConcatSize < 1) {
+ throw new Exception("Invalid value of ngramConcatSize: " +
ngramConcatSize);
+ }
Main.log("Found ngramConcatSize: " + ngramConcatSize, 2);
}
}
- if(get(patterns.getJsonNode(), "patternSet").isObject()) {
- JsonNode patternSet = patterns.getJsonNode().get("patternSet");
+ if(JsonFile.get(patternFile.getJsonNode(), "patternSet").isObject()) {
+ JsonNode patternSet = patternFile.getJsonNode().get("patternSet");
- if(get(patternSet, "defaultId").isTextual()) {
+ if(JsonFile.get(patternSet, "defaultId").isTextual()) {
defaultId = patternSet.get("defaultId").asText();
Main.log("Found defaultId: " + defaultId, 2);
}
- int patternCount = 0;
+ if(patternSet.get("simpleHashCount") != null) {
+ String simpleHashCountStr = patternSet.get("simpleHashCount").asText();
+ int simpleHashCount = Integer.parseInt(simpleHashCountStr);
- if(get(patternSet, "patterns").isArray()) {
+ if(simpleHashCount < 1) {
+ throw new Exception("Invalid value of simpleHashCount: " +
simpleHashCount);
+ }
+
+ patterns = new HashMap<>(simpleHashCount);
+ }
+
+ if(JsonFile.get(patternSet, "patterns").isArray()) {
for(Iterator<JsonNode> i = patternSet.get("patterns").iterator();
i.hasNext();) {
- JsonNode pattern = i.next();
+ JsonNode patternNode = i.next();
- if(get(pattern, "patternId").asText().isEmpty()) {
- throw new Exception("Bad patternId found, position: " +
patternCount);
- }
+ Pattern pattern = new Pattern(patternNode);
- String patternId = pattern.get("patternId").asText();
+ for(String patternToken : pattern.getPatternTokens()) {
+ List<Pattern> tokenPatterns = patterns.get(patternToken);
- Main.log("patternId: " + patternId, 3);
+ if(tokenPatterns == null) {
+ tokenPatterns = new ArrayList<>();
+ }
- patternCount++;
+ tokenPatterns.add(pattern);
+
+ patterns.put(patternToken, tokenPatterns);
+ }
+
+ Main.log(pattern.toString(), 3);
}
}
- Main.log("Found " + patternCount + " pattern(s)", 1);
+ Main.log("Found " + patterns.size() + " pattern(s)", 1);
}
}
@@ -171,11 +194,11 @@ public class DeviceMapClient {
int attributeCount = 0;
- if(get(attributes.getJsonNode(), "attributes").isArray()) {
+ if(JsonFile.get(attributes.getJsonNode(), "attributes").isArray()) {
for(Iterator<JsonNode> i =
attributes.getJsonNode().get("attributes").iterator(); i.hasNext();) {
JsonNode attribute = i.next();
- if(get(attribute, "patternId").asText().isEmpty()) {
+ if(JsonFile.get(attribute, "patternId").asText().isEmpty()) {
throw new Exception("Bad attribute patternId found, position: " +
attributeCount);
}
@@ -183,24 +206,24 @@ public class DeviceMapClient {
Main.log("Attribute patternId: " + patternId, 3);
- if(get(attribute, "attributes").isObject()) {
+ if(JsonFile.get(attribute, "attributes").isObject()) {
for(Iterator<String> j =
attribute.get("attributes").getFieldNames(); j.hasNext();) {
String key = j.next();
String value = attribute.get("attributes").get(key).asText();
}
}
- if(get(attribute, "attributeTransformers").isArray()) {
+ if(JsonFile.get(attribute, "attributeTransformers").isArray()) {
for(Iterator<JsonNode> j =
attribute.get("attributeTransformers").iterator(); j.hasNext();) {
JsonNode attributeTransformer = j.next();
- if(get(attributeTransformer, "name").asText().isEmpty()) {
+ if(JsonFile.get(attributeTransformer,
"name").asText().isEmpty()) {
throw new Exception("Bad attributeTransformer name, position:
" + attributeCount);
}
String name = attributeTransformer.get("name").asText();
- if(get(attributeTransformer, "transformers").isArray()) {
+ if(JsonFile.get(attributeTransformer, "transformers").isArray())
{
for(Iterator<JsonNode> k =
attributeTransformer.get("transformers").iterator(); k.hasNext();) {
JsonNode transformer = k.next();
}
@@ -215,24 +238,9 @@ public class DeviceMapClient {
Main.log("Found " + attributeCount + " attributes(s)", 1);
}
- public static JsonNode get(JsonNode node, String name) throws Exception {
- JsonNode ret = node.get(name);
-
- if(ret != null) {
- return ret;
- }
-
- if(nullNode == null) {
- ObjectMapper mapper = new ObjectMapper();
- nullNode = mapper.readTree("null");
- }
-
- return nullNode;
- }
-
public static Transformer getTransformer(JsonNode transformer) throws
Exception {
- String type = get(transformer, "type").asText();
- JsonNode parameters = get(transformer, "parameters");
+ String type = JsonFile.get(transformer, "type").asText();
+ JsonNode parameters = JsonFile.get(transformer, "parameters");
if(type.equals("LowerCase")) {
return new TransformerLowerCase();
@@ -245,7 +253,7 @@ public class DeviceMapClient {
throw new Exception("Transformer not found: " + type);
}
- public String classify(String text) {
+ public String classify(String text) throws Exception {
if(text == null) {
text = "";
}
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=1693360&r1=1693359&r2=1693360&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/JsonFile.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/JsonFile.java Wed Jul 29 23:34:53
2015
@@ -22,6 +22,8 @@ import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonFile {
+
+ private static JsonNode nullNode = null;
private final JsonNode json;
@@ -58,6 +60,21 @@ public class JsonFile {
domainVersion = json.get("domainVersion").asText();
}
+ public static JsonNode get(JsonNode node, String name) throws Exception {
+ JsonNode ret = node.get(name);
+
+ if(ret != null) {
+ return ret;
+ }
+
+ if(nullNode == null) {
+ ObjectMapper mapper = new ObjectMapper();
+ nullNode = mapper.readTree("null");
+ }
+
+ return nullNode;
+ }
+
public String getType() {
return type;
}
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=1693360&r1=1693359&r2=1693360&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Main.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/Main.java Wed Jul 29 23:34:53 2015
@@ -71,22 +71,22 @@ public class Main {
}
for(String pattern : patterns) {
- log("Pattern file: '" + pattern + "'", 1);
+ log("Pattern file: '" + pattern + "'", 0);
client.loadPatterns(new JsonFile(pattern));
}
for(String attribute : attributes) {
- log("Attribute file: '" + attribute + "'", 1);
+ log("Attribute file: '" + attribute + "'", 0);
client.loadAttributes(new JsonFile(attribute));
}
for(String test : tests) {
- log("Test file: '" + test + "'", 1);
+ log("Test file: '" + test + "'", 0);
failure |= test(client, new JsonFile(test));
}
if(testString != null) {
- log("Test string: '" + testString + "'", 1);
+ log("Test string: '" + testString + "'", 0);
String result = client.classify(testString);
}
@@ -145,22 +145,22 @@ public class Main {
int testCount = 0;
int passCount = 0;
- if(DeviceMapClient.get(tests.getJsonNode(), "tests").isArray()) {
+ if(JsonFile.get(tests.getJsonNode(), "tests").isArray()) {
for(Iterator<JsonNode> i =
tests.getJsonNode().get("tests").iterator(); i.hasNext();) {
JsonNode test = i.next();
- if(DeviceMapClient.get(test, "input").asText().isEmpty()) {
+ if(JsonFile.get(test, "input").asText().isEmpty()) {
throw new Exception("Bad test input found, position: " +
testCount);
}
String input = test.get("input").asText();
String resultPatternId = null;
- if(!DeviceMapClient.get(test, "resultPatternId").isNull()) {
- resultPatternId = DeviceMapClient.get(test,
"resultPatternId").asText();
+ if(!JsonFile.get(test, "resultPatternId").isNull()) {
+ resultPatternId = JsonFile.get(test, "resultPatternId").asText();
}
- if(DeviceMapClient.get(test, "resultAttributes").isObject()) {
+ if(JsonFile.get(test, "resultAttributes").isObject()) {
for(Iterator<String> j =
test.get("resultAttributes").getFieldNames(); j.hasNext();) {
String key = j.next();
String value = test.get("resultAttributes").get(key).asText();
Added: devicemap/trunk/clients/2.0/reference/src/Pattern.java
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Pattern.java?rev=1693360&view=auto
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Pattern.java (added)
+++ devicemap/trunk/clients/2.0/reference/src/Pattern.java Wed Jul 29 23:34:53
2015
@@ -0,0 +1,88 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.codehaus.jackson.JsonNode;
+
+public class Pattern {
+ private final String patternId;
+ private final String patternType;
+ private final List<String> patternTokens;
+
+ private final String rankType;
+ private final int rankValue;
+
+ public Pattern(JsonNode json) throws Exception {
+ if(json.get("patternId") == null ||
json.get("patternId").asText().isEmpty()) {
+ throw new Exception("patternId not found");
+ }
+
+ patternId = json.get("patternId").asText();
+
+ if(json.get("patternType") == null ||
json.get("patternType").asText().isEmpty()) {
+ throw new Exception("patternType not found in " + patternId);
+ }
+
+ if(!JsonFile.get(json, "patternTokens").isArray()) {
+ throw new Exception("patternTokens array not found in " + patternId);
+ }
+
+ if(json.get("rankType") == null ||
json.get("rankType").asText().isEmpty()) {
+ throw new Exception("rankType not found in " + patternId);
+ }
+
+ patternType = json.get("patternType").asText();
+ rankType = json.get("rankType").asText();
+
+ String rankValueStr = (json.get("rankValue") != null ?
json.get("rankValue").asText() : "0");
+ rankValue = Integer.parseInt(rankValueStr);
+
+ if(rankValue > 1000 || rankValue < -1000) {
+ throw new Exception("Invalid rankValue in " + patternId + ": " +
rankValue);
+ }
+
+ patternTokens = new ArrayList<>();
+
+ for(Iterator<JsonNode> i = json.get("patternTokens").iterator();
i.hasNext();) {
+ JsonNode patternToken = i.next();
+
+ if(patternToken.asText().isEmpty()) {
+ throw new Exception("Empty patternToken in " + patternId);
+ }
+
+ patternTokens.add(patternToken.asText());
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "patternId: " + patternId + ", patternType: " + patternType +
+ ", patternTokens: " + patternTokens.size() + ", rankType: " +
rankType + ", rankValue: " + rankValue;
+ }
+
+ public String getPatternId() {
+ return patternId;
+ }
+
+ public List<String> getPatternTokens() {
+ return patternTokens;
+ }
+}
Modified: devicemap/trunk/clients/2.0/reference/src/Transformer.java
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Transformer.java?rev=1693360&r1=1693359&r2=1693360&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Transformer.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/Transformer.java Wed Jul 29
23:34:53 2015
@@ -18,5 +18,5 @@
*/
public interface Transformer {
- public String transform(String input);
+ public String transform(String input) throws Exception;
}
Modified: devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java
URL:
http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java?rev=1693360&r1=1693359&r2=1693360&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java
(original)
+++ devicemap/trunk/clients/2.0/reference/src/TransformerReplaceAll.java Wed
Jul 29 23:34:53 2015
@@ -23,17 +23,17 @@ public class TransformerReplaceAll imple
private final String find;
private final String replaceWith;
- public TransformerReplaceAll(JsonNode transformer) throws Exception {
- if(transformer.get("find") == null ||
transformer.get("find").asText().isEmpty()) {
+ public TransformerReplaceAll(JsonNode json) throws Exception {
+ if(json.get("find") == null || json.get("find").asText().isEmpty()) {
throw new Exception("ReplaceAll find not defined");
}
- if(transformer.get("replaceWith") == null) {
+ if(json.get("replaceWith") == null) {
throw new Exception("ReplaceAll replaceWith not defined");
}
- find = transformer.get("find").asText();
- replaceWith = transformer.get("replaceWith").asText();
+ find = json.get("find").asText();
+ replaceWith = json.get("replaceWith").asText();
}
@Override