CLEREZZA-990: Using clerezza commons-rdf
diff --git a/rdf.rdfjson/README.md b/rdf.rdfjson/README.md
new file mode 100644
index 0000000..e3ace1c
--- /dev/null
+++ b/rdf.rdfjson/README.md
@@ -0,0 +1,4 @@
+# Apache Clerezza rdf/json Parser and Serializer
+
+This provides support for the RDF 1.1 JSON Alternate Serialization (RDF/JSON),
+see: http://www.w3.org/TR/rdf-json/
diff --git a/rdf.rdfjson/pom.xml b/rdf.rdfjson/pom.xml
index adbd0d6..280b682 100644
--- a/rdf.rdfjson/pom.xml
+++ b/rdf.rdfjson/pom.xml
@@ -18,13 +18,13 @@
specific language governing permissions and limitations
under the License.
--->
+ -->
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>clerezza</artifactId>
<groupId>org.apache.clerezza</groupId>
- <version>0.5</version>
+ <version>7</version>
<relativePath>../parent</relativePath>
</parent>
<groupId>org.apache.clerezza</groupId>
@@ -43,12 +43,12 @@
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.ontologies</artifactId>
<scope>test</scope>
- <version>0.12</version>
+ <version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
- <version>0.14</version>
+ <version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza.ext</groupId>
@@ -61,7 +61,7 @@
</dependency>
</dependencies>
<build>
- <plugins>
+ <plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
diff --git a/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java b/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
index 105566e..9ad302c 100644
--- a/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
+++ b/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParsingProvider.java
@@ -28,36 +28,39 @@
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.Language;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
-import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
+import org.apache.clerezza.commons.rdf.BlankNode;
+import org.apache.clerezza.commons.rdf.Language;
+import org.apache.clerezza.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.BlankNodeOrIRI;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.impl.utils.LiteralImpl;
+import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl;
+import org.apache.clerezza.commons.rdf.impl.utils.TripleImpl;
+import org.apache.clerezza.commons.rdf.impl.utils.TypedLiteralImpl;
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
- *
+ * A {@link org.apache.clerezza.rdf.core.serializedform.ParsingProvider} for
+ * rdf/json
+ *
* @author tio, hasan
- *
+ *
*/
-@Component(immediate=true)
+@Component(immediate = true)
@Service(ParsingProvider.class)
@SupportedFormat(SupportedFormat.RDF_JSON)
public class RdfJsonParsingProvider implements ParsingProvider {
private final Logger logger = LoggerFactory.getLogger(getClass());
+ private static final IRI XSD_STRING = new IRI("http://www.w3.org/2001/XMLSchema#string");
@Override
- public void parse(MGraph target, InputStream serializedGraph, String formatIdentifier, UriRef baseUri) {
+ public void parse(Graph target, InputStream serializedGraph, String formatIdentifier, IRI baseUri) {
- BNodeManager bNodeMgr = new BNodeManager();
+ BlankNodeManager bNodeMgr = new BlankNodeManager();
JSONParser parser = new JSONParser();
InputStreamReader reader;
@@ -72,13 +75,13 @@
try {
JSONObject root = (JSONObject) parser.parse(reader);
- NonLiteral nonLiteral = null;
+ BlankNodeOrIRI nonLiteral = null;
for (Object key : root.keySet()) {
String keyString = (String) key;
if (keyString.startsWith("_:")) {
- nonLiteral = bNodeMgr.getBNode(keyString);
+ nonLiteral = bNodeMgr.getBlankNode(keyString);
} else {
- nonLiteral = new UriRef(keyString);
+ nonLiteral = new IRI(keyString);
}
JSONObject predicates = (JSONObject) root.get(keyString);
addTriplesToGraph(nonLiteral, bNodeMgr, predicates, target);
@@ -92,45 +95,48 @@
}
}
- private class BNodeManager {
- private Map<String, BNode> bNodeMap = new HashMap<String, BNode>();
+ private class BlankNodeManager {
- public BNode getBNode(String id) {
- BNode bNode = bNodeMap.get(id);
+ private Map<String, BlankNode> bNodeMap = new HashMap<String, BlankNode>();
+
+ public BlankNode getBlankNode(String id) {
+ BlankNode bNode = bNodeMap.get(id);
if (bNode == null) {
- bNode = new BNode();
+ bNode = new BlankNode();
bNodeMap.put(id, bNode);
}
return bNode;
}
}
- private void addTriplesToGraph(NonLiteral subject, BNodeManager bNodeMgr, JSONObject predicates, MGraph mGraph) {
+ private void addTriplesToGraph(BlankNodeOrIRI subject, BlankNodeManager bNodeMgr, JSONObject predicates, Graph mGraph) {
for (Object predicate : predicates.keySet()) {
JSONArray objects = (JSONArray) predicates.get(predicate);
for (Object object : objects) {
JSONObject values = (JSONObject) object;
String value = (String) values.get("value");
if (values.get("type").equals("literal")) {
- Object dataType = values.get("datatype");
- if (dataType != null &&
- !dataType.toString().isEmpty()) {
- mGraph.add(new TripleImpl(subject, new UriRef((String) predicate),
- //LiteralFactory.getInstance().createTypedLiteral(value)));
- new TypedLiteralImpl(value.toString(),new UriRef(dataType.toString()))));
- } else if (values.containsKey("lang")
+ IRI dataType;
+ Object dataTypeValue = values.get("datatype");
+ if (dataTypeValue == null
+ || dataTypeValue.toString().isEmpty()) {
+ dataType = XSD_STRING;
+ } else {
+ dataType = new IRI(dataTypeValue.toString());
+ }
+ Language language = null;
+ if (values.containsKey("lang")
&& !values.get("lang").equals("")
&& values.get("lang") != null) {
- mGraph.add(new TripleImpl(subject, new UriRef((String) predicate),
- new PlainLiteralImpl(value, new Language((String) values.get("lang")))));
- } else {
- mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), new PlainLiteralImpl(value)));
+ language = new Language((String) values.get("lang"));
}
+ mGraph.add(new TripleImpl(subject, new IRI((String) predicate),
+ new LiteralImpl(value.toString(), dataType, language)));
} else if (values.get("type").equals("uri")) {
- mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), new UriRef(value)));
+ mGraph.add(new TripleImpl(subject, new IRI((String) predicate), new IRI(value)));
} else if (values.get("type").equals("bnode")) {
- NonLiteral bNode = bNodeMgr.getBNode(value);
- mGraph.add(new TripleImpl(subject, new UriRef((String) predicate), bNode));
+ BlankNodeOrIRI bNode = bNodeMgr.getBlankNode(value);
+ mGraph.add(new TripleImpl(subject, new IRI((String) predicate), bNode));
}
}
}
diff --git a/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializingProvider.java b/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializingProvider.java
index c635588..b0b8991 100644
--- a/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializingProvider.java
+++ b/rdf.rdfjson/src/main/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializingProvider.java
@@ -27,14 +27,13 @@
import java.util.Map;
import java.util.Map.Entry;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.PlainLiteral;
-import org.apache.clerezza.rdf.core.Resource;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.TripleCollection;
-import org.apache.clerezza.rdf.core.TypedLiteral;
-import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.commons.rdf.BlankNode;
+import org.apache.clerezza.commons.rdf.BlankNodeOrIRI;
+import org.apache.clerezza.commons.rdf.RDFTerm;
+import org.apache.clerezza.commons.rdf.Triple;
+import org.apache.clerezza.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.Literal;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
import org.apache.felix.scr.annotations.Component;
@@ -47,7 +46,7 @@
* rdf/json.
*
* This implementation is based on first sorting the triples within the parsed
- * {@link TripleCollection} based on the {@link #SUBJECT_COMPARATOR subject}.
+ * {@link Graph} based on the {@link #SUBJECT_COMPARATOR subject}.
* <p>
* The serialization is done on a subject scope. Meaning that all triples for a
* subject are serialized and instantly written to the provided
@@ -64,7 +63,7 @@
@SuppressWarnings("unchecked")
@Override
- public void serialize(OutputStream serializedGraph, TripleCollection tc,
+ public void serialize(OutputStream serializedGraph, Graph tc,
String formatIdentifier) {
if (tc.isEmpty()) { // ensure writing an empty element in case of an
// empty collection
@@ -77,7 +76,7 @@
}
return;
}
- BNodeManager bNodeMgr = new BNodeManager();
+ BlankNodeManager bNodeMgr = new BlankNodeManager();
BufferedWriter out;
try {
out = new BufferedWriter(new OutputStreamWriter(serializedGraph,
@@ -89,10 +88,10 @@
Triple[] sortedTriples = tc.toArray(new Triple[tc.size()]);
Arrays.sort(sortedTriples, SUBJECT_COMPARATOR);
Triple triple;
- NonLiteral subject = null;
+ BlankNodeOrIRI subject = null;
String subjectStr = null;
- UriRef predicate = null;
- Map<UriRef, JSONArray> predicateValues = new HashMap<UriRef, JSONArray>();
+ IRI predicate = null;
+ Map<IRI, JSONArray> predicateValues = new HashMap<IRI, JSONArray>();
JSONObject jSubject = new JSONObject();
try {
out.write("{"); // start the root object
@@ -102,7 +101,7 @@
if (subjectChange) {
if (subject != null) {
// write the predicate values
- for (Entry<UriRef, JSONArray> predicates : predicateValues
+ for (Entry<IRI, JSONArray> predicates : predicateValues
.entrySet()) {
jSubject.put(
predicates.getKey().getUnicodeString(),
@@ -116,10 +115,10 @@
}
// init next subject
subject = triple.getSubject();
- if (subject instanceof BNode) {
- subjectStr = bNodeMgr.getBNodeId((BNode) subject);
- } else { // if (subject instanceof UriRef)
- subjectStr = ((UriRef) subject).getUnicodeString();
+ if (subject instanceof BlankNode) {
+ subjectStr = bNodeMgr.getBlankNodeId((BlankNode) subject);
+ } else { // if (subject instanceof IRI)
+ subjectStr = ((IRI) subject).getUnicodeString();
}
}
predicate = triple.getPredicate();
@@ -131,7 +130,7 @@
values.add(writeObject(bNodeMgr, triple.getObject()));
}
if (subjectStr != null) {
- for (Entry<UriRef, JSONArray> predicates : predicateValues
+ for (Entry<IRI, JSONArray> predicates : predicateValues
.entrySet()) {
jSubject.put(predicates.getKey().getUnicodeString(),
predicates.getValue());
@@ -146,50 +145,46 @@
}
}
- private class BNodeManager {
- private Map<BNode, String> bNodeMap = new HashMap<BNode, String>();
+ private class BlankNodeManager {
+ private Map<BlankNode, String> bNodeMap = new HashMap<BlankNode, String>();
private int counter = 0;
- public String getBNodeId(BNode node) {
+ public String getBlankNodeId(BlankNode node) {
String bNodeId = bNodeMap.get(node);
if (bNodeId == null) {
bNodeId = "_:b" + ++counter;
- bNodeMap.put((BNode) node, bNodeId);
+ bNodeMap.put((BlankNode) node, bNodeId);
}
return bNodeId;
}
}
/**
- * Converts the {@link Resource object} of an triple to JSON
+ * Converts the {@link RDFTerm object} of an triple to JSON
*
* @param bNodeMgr
- * used to lookup {@link BNode} instances
+ * used to lookup {@link BlankNode} instances
* @param object
* the object of the triple
* @return the JSON representation of parsed object
*/
@SuppressWarnings("unchecked")
- private JSONObject writeObject(BNodeManager bNodeMgr, Resource object) {
+ private JSONObject writeObject(BlankNodeManager bNodeMgr, RDFTerm object) {
JSONObject jObject = new JSONObject();
- if (object instanceof PlainLiteral) {
- PlainLiteral plainLiteral = (PlainLiteral) object;
- jObject.put("value", plainLiteral.getLexicalForm());
- jObject.put("type", "literal");
- if (plainLiteral.getLanguage() != null) {
- jObject.put("lang", plainLiteral.getLanguage().toString());
- }
- } else if (object instanceof TypedLiteral) {
- TypedLiteral literal = (TypedLiteral) object;
+ if (object instanceof Literal) {
+ Literal literal = (Literal) object;
jObject.put("value", literal.getLexicalForm());
jObject.put("type", "literal");
jObject.put("datatype", literal.getDataType().getUnicodeString());
- } else if (object instanceof UriRef) {
- UriRef uriRef = (UriRef) object;
+ if (literal.getLanguage() != null) {
+ jObject.put("lang", literal.getLanguage().toString());
+ }
+ } else if (object instanceof IRI) {
+ IRI uriRef = (IRI) object;
jObject.put("value", uriRef.getUnicodeString());
jObject.put("type", "uri");
- } else if (object instanceof BNode) {
- String bNodeId = bNodeMgr.getBNodeId((BNode) object);
+ } else if (object instanceof BlankNode) {
+ String bNodeId = bNodeMgr.getBlankNodeId((BlankNode) object);
jObject.put("value", bNodeId);
jObject.put("type", "bnode");
}
@@ -209,7 +204,7 @@
return compare(a.getSubject(), b.getSubject());
}
- private int compare(NonLiteral a, NonLiteral b) {
+ private int compare(BlankNodeOrIRI a, BlankNodeOrIRI b) {
int hashA = a.hashCode();
int hashB = b.hashCode();
if (hashA != hashB) {
diff --git a/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java b/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
index c1115a9..200a253 100644
--- a/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
+++ b/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/parser/RdfJsonParserProviderTest.java
@@ -18,15 +18,15 @@
import java.io.InputStream;
import java.util.Iterator;
-import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.commons.rdf.BlankNode;
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.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.BlankNodeOrIRI;
+import org.apache.clerezza.commons.rdf.Triple;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph;
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
/**
@@ -35,38 +35,38 @@
public class RdfJsonParserProviderTest {
@Test
- public void testParsingOfObjectBNode() {
+ public void testParsingOfObjectBlankNode() {
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);
+ Graph parsedGraph = new SimpleGraph();
+ provider.parse(parsedGraph, jsonIn, "application/rdf+json", null);
+ Assert.assertEquals(parsedGraph.size(), 1);
+ Iterator<Triple> triples = parsedGraph.filter(new IRI("http://example.org/node1"),
+ new IRI("http://example.org/prop1"), null);
Assert.assertTrue(triples.hasNext());
- Assert.assertTrue(triples.next().getObject() instanceof BNode);
+ Assert.assertTrue(triples.next().getObject() instanceof BlankNode);
}
@Test
- public void testParsingOfSubjectBNode() {
+ public void testParsingOfSubjectBlankNode() {
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"));
+ Graph parsedGraph = new SimpleGraph();
+ provider.parse(parsedGraph, jsonIn, "application/rdf+json", null);
+ Assert.assertEquals(3, parsedGraph.size());
+ Iterator<Triple> triples = parsedGraph.filter(null, new IRI("http://example.org/prop1"),
+ new IRI("http://example.org/node1"));
Assert.assertTrue(triples.hasNext());
- NonLiteral subject = triples.next().getSubject();
- Assert.assertTrue(subject instanceof BNode);
+ BlankNodeOrIRI subject = triples.next().getSubject();
+ Assert.assertTrue(subject instanceof BlankNode);
- triples = parsedMGraph.filter(null, new UriRef("http://example.org/prop2"),
- new UriRef("http://example.org/node2"));
+ triples = parsedGraph.filter(null, new IRI("http://example.org/prop2"),
+ new IRI("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"));
+ triples = parsedGraph.filter(null, new IRI("http://example.org/prop3"),
+ new IRI("http://example.org/node3"));
Assert.assertTrue(triples.hasNext());
Assert.assertFalse(subject.equals(triples.next().getSubject()));
}
@@ -75,12 +75,12 @@
public void testParser() {
ParsingProvider provider = new RdfJsonParsingProvider();
InputStream jsonIn = getClass().getResourceAsStream("test.json");
- MGraph deserializedMGraph = new SimpleMGraph();
- provider.parse(deserializedMGraph, jsonIn, "application/rdf+json", null);
- Assert.assertEquals(deserializedMGraph.size(), 6);
- Iterator<Triple> triples = deserializedMGraph.filter(new UriRef("http://base/child1"), null, null);
+ Graph deserializedGraph = new SimpleGraph();
+ provider.parse(deserializedGraph, jsonIn, "application/rdf+json", null);
+ Assert.assertEquals(deserializedGraph.size(), 6);
+ Iterator<Triple> triples = deserializedGraph.filter(new IRI("http://base/child1"), null, null);
while (triples.hasNext()) {
- UriRef uri = triples.next().getPredicate();
+ IRI uri = triples.next().getPredicate();
Assert.assertEquals(uri.getUnicodeString(), "http://base/propertyB");
}
}
diff --git a/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializerProviderTest.java b/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializerProviderTest.java
index 2273fdc..f997e97 100644
--- a/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializerProviderTest.java
+++ b/rdf.rdfjson/src/test/java/org/apache/clerezza/rdf/rdfjson/serializer/RdfJsonSerializerProviderTest.java
@@ -28,18 +28,17 @@
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
-import org.apache.clerezza.rdf.core.BNode;
-import org.apache.clerezza.rdf.core.Language;
+import org.apache.clerezza.commons.rdf.BlankNode;
+import org.apache.clerezza.commons.rdf.Language;
+import org.apache.clerezza.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.BlankNodeOrIRI;
+import org.apache.clerezza.commons.rdf.Triple;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.commons.rdf.Literal;
+import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl;
+import org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph;
+import org.apache.clerezza.commons.rdf.impl.utils.TripleImpl;
import org.apache.clerezza.rdf.core.LiteralFactory;
-import org.apache.clerezza.rdf.core.MGraph;
-import org.apache.clerezza.rdf.core.NonLiteral;
-import org.apache.clerezza.rdf.core.PlainLiteral;
-import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.TypedLiteral;
-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.ParsingProvider;
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.ontologies.FOAF;
@@ -58,23 +57,23 @@
public class RdfJsonSerializerProviderTest {
private final static LiteralFactory lf = LiteralFactory.getInstance();
- private final static UriRef RDF_NIL = new UriRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");
- private final static UriRef node1 = new UriRef("http://example.org/node1");
- private final static UriRef node2 = new UriRef("http://example.org/node2");
- private final static UriRef prop1 = new UriRef("http://example.org/prop1");
- private final static UriRef prop2 = new UriRef("http://example.org/prop2");
- private final static UriRef prop3 = new UriRef("http://example.org/prop3");
- private final static UriRef prop4 = new UriRef("http://example.org/prop4");
- private final static UriRef prop5 = new UriRef("http://example.org/prop5");
- private final static UriRef prop6 = new UriRef("http://example.org/prop6");
- private final static BNode blank1 = new BNode();
- private final static BNode blank2 = new BNode();
+ private final static IRI RDF_NIL = new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");
+ private final static IRI node1 = new IRI("http://example.org/node1");
+ private final static IRI node2 = new IRI("http://example.org/node2");
+ private final static IRI prop1 = new IRI("http://example.org/prop1");
+ private final static IRI prop2 = new IRI("http://example.org/prop2");
+ private final static IRI prop3 = new IRI("http://example.org/prop3");
+ private final static IRI prop4 = new IRI("http://example.org/prop4");
+ private final static IRI prop5 = new IRI("http://example.org/prop5");
+ private final static IRI prop6 = new IRI("http://example.org/prop6");
+ private final static BlankNode blank1 = new BlankNode();
+ private final static BlankNode blank2 = new BlankNode();
private final static PlainLiteralImpl plainLiteralA = new PlainLiteralImpl("A");
private final static PlainLiteralImpl plainLiteralB = new PlainLiteralImpl("B");
private final static PlainLiteralImpl plainLiteralC = new PlainLiteralImpl("C");
- private final static TypedLiteral typedLiteralA = lf.createTypedLiteral("A");
+ private final static Literal typedLiteralA = lf.createTypedLiteral("A");
- private MGraph mGraph;
+ private Graph mGraph;
@BeforeClass
public static void setUpClass() throws Exception {
@@ -86,7 +85,7 @@
@Before
public void setUp() {
- mGraph = new SimpleMGraph();
+ mGraph = new SimpleGraph();
}
@After
@@ -94,7 +93,7 @@
}
@Test
- public void testSerializationOfBNode() {
+ public void testSerializationOfBlankNode() {
mGraph.add(new TripleImpl(node1, prop1, blank1));
SerializingProvider provider = new RdfJsonSerializingProvider();
ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
@@ -118,10 +117,10 @@
ParsingProvider parsingProvider = new RdfJsonParsingProvider();
ByteArrayInputStream jsonIn = new ByteArrayInputStream(serializedGraph.toByteArray());
- MGraph parsedMGraph = new SimpleMGraph();
- parsingProvider.parse(parsedMGraph, jsonIn, "application/rdf+json", null);
+ Graph parsedGraph = new SimpleGraph();
+ parsingProvider.parse(parsedGraph, jsonIn, "application/rdf+json", null);
- Assert.assertEquals(mGraph.getGraph(), parsedMGraph.getGraph());
+ Assert.assertEquals(mGraph.getImmutableGraph(), parsedGraph.getImmutableGraph());
}
/*
@@ -135,18 +134,15 @@
mGraph.add(new TripleImpl(blank1, prop4, plainLiteralC));
mGraph.add(new TripleImpl(blank1, prop5, typedLiteralA));
mGraph.add(new TripleImpl(node1, prop6, blank1));
-
- SerializingProvider provider = new RdfJsonSerializingProvider();
- ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
- provider.serialize(serializedGraph, mGraph, "application/rdf+json");
-// System.out.println(serializedGraph.toString());
- ParsingProvider parsingProvider = new RdfJsonParsingProvider();
- ByteArrayInputStream jsonIn = new ByteArrayInputStream(serializedGraph.toByteArray());
- MGraph parsedMGraph = new SimpleMGraph();
- parsingProvider.parse(parsedMGraph, jsonIn, "application/rdf+json", null);
-
- Assert.assertEquals(6, parsedMGraph.size());
- Assert.assertEquals(mGraph.getGraph(), parsedMGraph.getGraph());
+ serializeDeserialize(mGraph);
+ }
+
+ @Test
+ public void literalWithLanguage() {
+ Literal l = new PlainLiteralImpl("Ein deutsches Literal.",
+ new Language("de"));
+ mGraph.add(new TripleImpl(blank1, RDF.first, l));
+ serializeDeserialize(mGraph);
}
/**
@@ -154,7 +150,7 @@
*/
@Test
public void testBigGraph() throws Exception {
- //reduced Graph size to 5000 to allow equals test between the
+ //reduced ImmutableGraph size to 5000 to allow equals test between the
//serialised and parsed RDF graphs. Equals tests on bigger graphs
//would take to much time
int NUM_TRIPLES = 5000;
@@ -165,9 +161,9 @@
double b = 2.0;//bNode
double nb = b - (l * 2 / 3); //create new bNode
double random;
- NonLiteral subject = null;
- UriRef predicate = null;
- List<UriRef> predicateList = new ArrayList<UriRef>();
+ BlankNodeOrIRI subject = null;
+ IRI predicate = null;
+ List<IRI> predicateList = new ArrayList<IRI>();
predicateList.add(RDF.first);
predicateList.add(RDF.rest);
predicateList.add(RDF.type);
@@ -183,14 +179,14 @@
String URI_PREFIX = "http://www.test.org/bigGraph/ref";
Language DE = new Language("de");
Language EN = new Language("en");
- Iterator<UriRef> predicates = predicateList.iterator();
- List<BNode> bNodes = new ArrayList<BNode>();
- bNodes.add(new BNode());
+ Iterator<IRI> predicates = predicateList.iterator();
+ List<BlankNode> bNodes = new ArrayList<BlankNode>();
+ bNodes.add(new BlankNode());
for (int count = 0; mGraph.size() < NUM_TRIPLES; count++) {
random = Math.random() * 3;
if (random >= 2.5 || count == 0) {
if (random <= 2.75) {
- subject = new UriRef(URI_PREFIX + count);
+ subject = new IRI(URI_PREFIX + count);
} else {
int rndIndex = (int) ((random - 2.75) * bNodes.size() / (3.0 - 2.75));
subject = bNodes.get(rndIndex);
@@ -209,7 +205,7 @@
} else if (random <= d) {
mGraph.add(new TripleImpl(subject, predicate, lf.createTypedLiteral(random)));
} else {
- PlainLiteral text;
+ Literal text;
if (random <= i) {
text = new PlainLiteralImpl("Literal for " + count);
} else if (random <= d) {
@@ -220,26 +216,26 @@
mGraph.add(new TripleImpl(subject, predicate, text));
}
} else if (random <= b) { //bnode
- BNode bnode;
+ BlankNode bnode;
if (random <= nb) {
- bnode = new BNode();
+ bnode = new BlankNode();
bNodes.add(bnode);
} else { //>nb <b
int rndIndex = (int) ((random - nb) * bNodes.size() / (b - nb));
bnode = bNodes.get(rndIndex);
}
mGraph.add(new TripleImpl(subject, predicate, bnode));
- } else { //UriRef
+ } else { //IRI
mGraph.add(new TripleImpl(subject, predicate,
- new UriRef(URI_PREFIX + (int) count * random)));
+ new IRI(URI_PREFIX + (int) count * random)));
}
}
//Asserts the correct sorting of the triples in the graph by the
//Comparator used by the JSON serializer
- Set<NonLiteral> subjects = new HashSet<NonLiteral>();
+ Set<BlankNodeOrIRI> subjects = new HashSet<BlankNodeOrIRI>();
Triple[] sortedTriples = mGraph.toArray(new Triple[mGraph.size()]);
Arrays.sort(sortedTriples, RdfJsonSerializingProvider.SUBJECT_COMPARATOR);
- NonLiteral current = sortedTriples[0].getSubject();
+ BlankNodeOrIRI current = sortedTriples[0].getSubject();
for(Triple triple : sortedTriples){
if(!triple.getSubject().equals(current)){
subjects.add(current);
@@ -259,11 +255,23 @@
System.out.println("Serialized " + mGraph.size() + " Triples in " + (System.currentTimeMillis() - start) + " ms");
ParsingProvider parsingProvider = new RdfJsonParsingProvider();
ByteArrayInputStream jsonIn = new ByteArrayInputStream(serializedGraph.toByteArray());
- MGraph parsedMGraph = new SimpleMGraph();
- parsingProvider.parse(parsedMGraph, jsonIn, "application/rdf+json", null);
- Assert.assertEquals(originalSize, parsedMGraph.size());
- sortedTriples = parsedMGraph.toArray(new Triple[parsedMGraph.size()]);
+ Graph parsedGraph = new SimpleGraph();
+ parsingProvider.parse(parsedGraph, jsonIn, "application/rdf+json", null);
+ Assert.assertEquals(originalSize, parsedGraph.size());
+ sortedTriples = parsedGraph.toArray(new Triple[parsedGraph.size()]);
Arrays.sort(sortedTriples, RdfJsonSerializingProvider.SUBJECT_COMPARATOR);
- Assert.assertEquals(mGraph.getGraph(), parsedMGraph.getGraph());
+ Assert.assertEquals(mGraph.getImmutableGraph(), parsedGraph.getImmutableGraph());
+ }
+
+ private void serializeDeserialize(Graph mGraph) {
+ SerializingProvider provider = new RdfJsonSerializingProvider();
+ ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
+ provider.serialize(serializedGraph, mGraph, "application/rdf+json");
+ ParsingProvider parsingProvider = new RdfJsonParsingProvider();
+ ByteArrayInputStream jsonIn = new ByteArrayInputStream(serializedGraph.toByteArray());
+ Graph parsedGraph = new SimpleGraph();
+ parsingProvider.parse(parsedGraph, jsonIn, "application/rdf+json", null);
+ Assert.assertEquals(mGraph.size(), parsedGraph.size());
+ Assert.assertEquals(mGraph.getImmutableGraph(), parsedGraph.getImmutableGraph());
}
}