You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2012/04/25 06:24:35 UTC

svn commit: r1330094 - in /incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey: parsers/FieldQueryReader.java writers/QueryResultsToRDF.java

Author: rwesten
Date: Wed Apr 25 04:24:35 2012
New Revision: 1330094

URL: http://svn.apache.org/viewvc?rev=1330094&view=rev
Log:
STANBOL-589: Added support for parsing/writing SimilarityConstraints from/to JSON formatted FieldQueries

Modified:
    incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java
    incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultsToRDF.java

Modified: incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java?rev=1330094&r1=1330093&r2=1330094&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java (original)
+++ incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/parsers/FieldQueryReader.java Wed Apr 25 04:24:35 2012
@@ -48,6 +48,7 @@ import org.apache.stanbol.entityhub.serv
 import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
 import org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint;
 import org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint;
+import org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint;
 import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
 import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint.PatternType;
 import org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint;
@@ -148,6 +149,7 @@ public class FieldQueryReader implements
             JSONObject jConstraint = constraints.getJSONObject(i);
             if(jConstraint.has("field")){
                 String field = jConstraint.getString("field");
+                field = NamespaceEnum.getFullName(field);
                 //check if there is already a constraint for that field
                 if(field == null || field.isEmpty()){
 //                    log.warn("The value of the key \"field\" MUST NOT be NULL nor emtpy!");
@@ -206,6 +208,7 @@ public class FieldQueryReader implements
         if(selected != null){
             for(int i=0;i<selected.length();i++){
                 String selectedField = selected.getString(i);
+                selectedField = NamespaceEnum.getFullName(selectedField);
                 if(selectedField != null && !selectedField.isEmpty()){
                     query.addSelectedField(selectedField);
                 }
@@ -255,6 +258,8 @@ public class FieldQueryReader implements
                 return parseTextConstraint(jConstraint);
             } else if (type.equals(ConstraintType.range.name())){
                 return parseRangeConstraint(jConstraint);
+            } else if(type.equals(ConstraintType.similarity.name())){
+                return parseSimilarityConstraint(jConstraint);
             } else {
                 log.warn(String.format("Unknown Constraint Type %s. Supported values are %s",               
                     Arrays.asList("reference",ConstraintType.values())));
@@ -281,6 +286,27 @@ public class FieldQueryReader implements
         }
     }
 
+    private static Constraint parseSimilarityConstraint(JSONObject jConstraint) throws JSONException {
+        String context = jConstraint.optString("context");
+        if(context == null){
+            throw new IllegalArgumentException("SimilarityConstraints MUST define a \"context\": \n "+jConstraint.toString(4));
+        }
+        JSONArray addFields = jConstraint.optJSONArray("addFields");
+        final List<String> fields;
+        if(addFields != null && addFields.length() > 0){
+            fields = new ArrayList<String>(addFields.length());
+            for(int i=0;i<addFields.length();i++){
+                String field = addFields.optString(i);
+                if(field != null && !field.isEmpty()){
+                    fields.add(NamespaceEnum.getFullName(field));
+                }
+            }
+        } else {
+            fields = null;
+        }
+        return new SimilarityConstraint(context,fields);
+    }
+
     /**
      * @param jConstraint
      * @return

Modified: incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultsToRDF.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultsToRDF.java?rev=1330094&r1=1330093&r2=1330094&view=diff
==============================================================================
--- incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultsToRDF.java (original)
+++ incubator/stanbol/trunk/entityhub/jersey/src/main/java/org/apache/stanbol/entityhub/jersey/writers/QueryResultsToRDF.java Wed Apr 25 04:24:35 2012
@@ -16,14 +16,20 @@
  */
 package org.apache.stanbol.entityhub.jersey.writers;
 
+import java.io.ByteArrayOutputStream;
+import java.io.StringWriter;
+import java.nio.charset.Charset;
 import java.util.Iterator;
 
 import org.apache.clerezza.rdf.core.LiteralFactory;
 import org.apache.clerezza.rdf.core.MGraph;
 import org.apache.clerezza.rdf.core.Triple;
 import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.clerezza.rdf.jena.serializer.JenaSerializerProvider;
 import org.apache.stanbol.commons.indexedgraph.IndexedMGraph;
 import org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList;
 import org.apache.stanbol.entityhub.servicesapi.model.Entity;
@@ -115,4 +121,12 @@ final class QueryResultsToRDF {
         return resultGraph;
     }
 
+    public static void main(String[] args) throws Exception {
+        JenaSerializerProvider p = new JenaSerializerProvider();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        MGraph g = new SimpleMGraph();
+        g.add(new TripleImpl(new UriRef("urn:test"),new UriRef("http://test.org/test"),new PlainLiteralImpl("test")));
+        p.serialize(out, g, SupportedFormat.N_TRIPLE);
+        System.out.println(new String(out.toByteArray(),Charset.forName("UTF-8")));
+    }
 }