clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject svn commit: r1073581 - in /incubator/clerezza/trunk/parent/rdf.rdfjson/src: main/java/org/apache/clerezza/rdf/rdfjson/parser/ test/java/org/apache/clerezza/rdf/rdfjson/parser/ test/resources/org/apache/clerezza/rdf/rdfjson/parser/
Date Wed, 23 Feb 2011 03:29:48 GMT
Author: hasan
Date: Wed Feb 23 03:29:48 2011
New Revision: 1073581

URL: http://svn.apache.org/viewvc?rev=1073581&view=rev
Log:
CLEREZZA-415: refactored parser code and fixed parser bug regarding bnodes as objects which
do not appear as subjects, added tests

Added:
    incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-object-bnode.json
    incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-subject-bnode.json
Modified:
    incubator/clerezza/trunk/parent/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
    incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
    incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test.json

Modified: incubator/clerezza/trunk/parent/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java?rev=1073581&r1=1073580&r2=1073581&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
(original)
+++ incubator/clerezza/trunk/parent/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
Wed Feb 23 03:29:48 2011
@@ -37,15 +37,17 @@ import org.apache.clerezza.rdf.core.impl
 import org.apache.clerezza.rdf.core.impl.TripleImpl;
 import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
 import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
 
 /**
  * A {@link org.apache.clerezza.rdf.core.serializedform.ParsingProvider} for rdf/json
  * 
- * @author tio *
- * @scr.component immediate="true"
- * @scr.service interface="org.apache.clerezza.rdf.core.serializedform.ParsingProvider"
+ * @author tio, hasan
  * 
  */
+@Component(immediate=true)
+@Service(ParsingProvider.class)
 @SupportedFormat(SupportedFormat.RDF_JSON)
 public class RdfJsonParsingProvider implements ParsingProvider {
 
@@ -56,13 +58,22 @@ public class RdfJsonParsingProvider impl
 
 		JSONParser parser = new JSONParser();
 		InputStreamReader reader = new InputStreamReader(serializedGraph);
+		Map<String, NonLiteral> bNodeMap = new HashMap<String, NonLiteral>();
+
 		try {
 			JSONObject root = (JSONObject) parser.parse(reader);
-			Map<String, NonLiteral> subjects = createSubjectsFromJSONObjects(root);
-			for (String keyString : subjects.keySet()) {
-				NonLiteral key = subjects.get(keyString);
+
+			NonLiteral nonLiteral = null;
+			for (Object key : root.keySet()) {
+				String keyString = (String) key;
+				if (keyString.startsWith("_:")) {
+					nonLiteral = new BNode();
+					bNodeMap.put(keyString, nonLiteral);
+				} else {
+					nonLiteral = new UriRef(keyString);
+				}
 				JSONObject predicates = (JSONObject) root.get(keyString);
-				addValuesToGraph(key, subjects, predicates, target);
+				addTriplesToGraph(nonLiteral, bNodeMap, predicates, target);
 			}
 		} catch (IOException ioe) {
 			logger.error(ioe.getMessage());
@@ -73,23 +84,7 @@ public class RdfJsonParsingProvider impl
 		}
 	}
 
-	private Map<String, NonLiteral> createSubjectsFromJSONObjects(JSONObject root) {
-		Map<String, NonLiteral> subjectsAsJSONObjects = new HashMap<String, NonLiteral>();
-
-		for (Object key : root.keySet()) {
-			String keyString = (String) key;
-			if (keyString.startsWith("_:")) {
-				BNode bNode = new BNode();
-				subjectsAsJSONObjects.put(keyString, bNode);
-			} else {
-				UriRef uri = new UriRef(keyString);
-				subjectsAsJSONObjects.put(keyString, uri);
-			}
-		}
-		return subjectsAsJSONObjects;
-	}
-
-	private void addValuesToGraph(NonLiteral key, Map<String, NonLiteral> subjects, JSONObject
predicates,
+	private void addTriplesToGraph(NonLiteral subject, Map<String, NonLiteral> bNodeMap,
JSONObject predicates,
 			MGraph mGraph) {
 		for (Object predicate : predicates.keySet()) {
 			JSONArray objects = (JSONArray) predicates.get(predicate);
@@ -100,20 +95,25 @@ public class RdfJsonParsingProvider impl
 					if (values.containsKey("datatype")
 							&& !values.get("datatype").equals("")
 							&& values.get("datatype") != null) {
-						mGraph.add(new TripleImpl(key, new UriRef((String) predicate),
+						mGraph.add(new TripleImpl(subject, new UriRef((String) predicate),
 								LiteralFactory.getInstance().createTypedLiteral(value)));
 					} else if (values.containsKey("lang")
 							&& !values.get("lang").equals("")
 							&& values.get("lang") != null) {
-						mGraph.add(new TripleImpl(key, new UriRef((String) predicate),
+						mGraph.add(new TripleImpl(subject, new UriRef((String) predicate),
 								new PlainLiteralImpl(value, new Language((String) values.get("lang")))));
 					} else {
-						mGraph.add(new TripleImpl(key, new UriRef((String) predicate), new PlainLiteralImpl(value)));
+						mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), new PlainLiteralImpl(value)));
 					}
 				} else if (values.get("type").equals("uri")) {
-					mGraph.add(new TripleImpl(key, new UriRef((String) predicate), new UriRef(value)));
+					mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), new UriRef(value)));
 				} else if (values.get("type").equals("bnode")) {
-					mGraph.add(new TripleImpl(key, new UriRef((String) predicate), subjects.get(value)));
+					NonLiteral bNode = bNodeMap.get(value);
+					if (bNode == null) {
+						bNode = new BNode();
+						bNodeMap.put(value, bNode);
+					}
+					mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), bNode));
 				}
 			}
 		}

Modified: incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java?rev=1073581&r1=1073580&r2=1073581&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
(original)
+++ incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
Wed Feb 23 03:29:48 2011
@@ -18,21 +18,60 @@ package org.apache.clerezza.rdf.rdfjson.
 
 import java.io.InputStream;
 import java.util.Iterator;
+import org.apache.clerezza.rdf.core.BNode;
 
 import org.junit.Assert;
 import org.junit.Test;
 import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
 import org.apache.clerezza.rdf.core.Triple;
 import org.apache.clerezza.rdf.core.UriRef;
 import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
 import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
 
 /**
- * @author tio
+ * @author tio, hasan
  */
 public class RdfJsonParserProviderTest {
 
 	@Test
+	public void testParsingOfObjectBNode() {
+		ParsingProvider provider = new RdfJsonParsingProvider();
+		InputStream jsonIn = getClass().getResourceAsStream("test-object-bnode.json");
+		MGraph parsedMGraph = new SimpleMGraph();
+		provider.parse(parsedMGraph, jsonIn, "application/rdf+json", null);
+		Assert.assertEquals(parsedMGraph.size(), 1);
+		Iterator<Triple> triples = parsedMGraph.filter(new UriRef("http://example.org/node1"),
+				new UriRef("http://example.org/prop1"), null);
+		Assert.assertTrue(triples.hasNext());
+		Assert.assertTrue(triples.next().getObject() instanceof BNode);
+	}
+
+	@Test
+	public void testParsingOfSubjectBNode() {
+		ParsingProvider provider = new RdfJsonParsingProvider();
+		InputStream jsonIn = getClass().getResourceAsStream("test-subject-bnode.json");
+		MGraph parsedMGraph = new SimpleMGraph();
+		provider.parse(parsedMGraph, jsonIn, "application/rdf+json", null);
+		Assert.assertEquals(3, parsedMGraph.size());
+		Iterator<Triple> triples = parsedMGraph.filter(null, new UriRef("http://example.org/prop1"),
+				new UriRef("http://example.org/node1"));
+		Assert.assertTrue(triples.hasNext());
+		NonLiteral subject = triples.next().getSubject();
+		Assert.assertTrue(subject instanceof BNode);
+
+		triples = parsedMGraph.filter(null, new UriRef("http://example.org/prop2"),
+				new UriRef("http://example.org/node2"));
+		Assert.assertTrue(triples.hasNext());
+		Assert.assertTrue(subject.equals(triples.next().getSubject()));
+
+		triples = parsedMGraph.filter(null, new UriRef("http://example.org/prop3"),
+				new UriRef("http://example.org/node3"));
+		Assert.assertTrue(triples.hasNext());
+		Assert.assertFalse(subject.equals(triples.next().getSubject()));
+	}
+
+	@Test
 	public void testParser() {
 		ParsingProvider provider = new RdfJsonParsingProvider();
 		InputStream jsonIn = getClass().getResourceAsStream("test.json");

Added: incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-object-bnode.json
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-object-bnode.json?rev=1073581&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-object-bnode.json
(added)
+++ incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-object-bnode.json
Wed Feb 23 03:29:48 2011
@@ -0,0 +1,5 @@
+{	
+	"http://example.org/node1":	{
+		"http://example.org/prop1": [{"value":"_:bnode1","type":"bnode"}]
+	}
+}

Added: incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-subject-bnode.json
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-subject-bnode.json?rev=1073581&view=auto
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-subject-bnode.json
(added)
+++ incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test-subject-bnode.json
Wed Feb 23 03:29:48 2011
@@ -0,0 +1,10 @@
+{	
+	"_:bnode1":	{
+		"http://example.org/prop1": [{"value":"http://example.org/node1","type":"uri"}],
+		"http://example.org/prop2": [{"value":"http://example.org/node2","type":"uri"}]
+	},
+
+	"_:bnode2":	{
+		"http://example.org/prop3": [{"value":"http://example.org/node3","type":"uri"}]
+	}
+}

Modified: incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test.json
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test.json?rev=1073581&r1=1073580&r2=1073581&view=diff
==============================================================================
--- incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test.json
(original)
+++ incubator/clerezza/trunk/parent/rdf.rdfjson/src/test/resources/org/apache/clerezza/rdf/rdfjson/parser/test.json
Wed Feb 23 03:29:48 2011
@@ -1,16 +1,16 @@
-{	
-	"http://base/child1":	{
+{
+	"http://base/child1": {
 		"http://base/propertyB":[{"value":"B","type":"literal"}]
 	},
 	
-	"_1":	{
-		"http://base/propertyC":[{"value":"C","type":"literal","lang":"en"}]
+	"_:b1": {
+		"http://base/propertyC":[{"value":"C","type":"literal","lang":"en"}],
 		"http://base/propertyE":[{"value":"E","type":"literal","datatype":"http://www.w3.org/2001/XMLSchema#string"}]
 	},
 
-	"http://base/root":	{
+	"http://base/root": {
 		"http://base/propertyA":[{"value":"A","type":"literal"}],
 		"http://base/resourcePropertyB":[{"value":"http://base/child1","type":"uri"}],
-		"http://base/resourcePropertyD":[{"value":"_1","type":"bnode"}]
+		"http://base/resourcePropertyD":[{"value":"_:b1","type":"bnode"}]
 	}
-}
\ No newline at end of file
+}



Mime
View raw message