jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [1/9] jena git commit: JENA-1430: Read quads for ja:data by filename
Date Thu, 07 Dec 2017 14:27:17 GMT
Repository: jena
Updated Branches:
  refs/heads/master e512f51b1 -> f987d61f3


JENA-1430: Read quads for ja:data by filename


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/d174ec04
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/d174ec04
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/d174ec04

Branch: refs/heads/master
Commit: d174ec04dccb205de96e63c775e01f948380f8cc
Parents: bd58437
Author: Andy Seaborne <andy@apache.org>
Authored: Mon Nov 20 10:57:01 2017 +0000
Committer: Andy Seaborne <andy@apache.org>
Committed: Mon Nov 20 17:31:47 2017 +0000

----------------------------------------------------------------------
 .../core/assembler/InMemDatasetAssembler.java   | 35 +++++++++-----------
 .../jena/sparql/util/graph/GraphUtils.java      | 18 ++++++++++
 .../assembler/TestInMemDatasetAssembler.java    | 32 +++++++++++++++---
 jena-fuseki2/examples/fuseki-in-mem-txn.ttl     | 24 ++++++++++++++
 4 files changed, 86 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/d174ec04/jena-arq/src/main/java/org/apache/jena/sparql/core/assembler/InMemDatasetAssembler.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/core/assembler/InMemDatasetAssembler.java
b/jena-arq/src/main/java/org/apache/jena/sparql/core/assembler/InMemDatasetAssembler.java
index a79779f..b70be2a 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/core/assembler/InMemDatasetAssembler.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/core/assembler/InMemDatasetAssembler.java
@@ -20,12 +20,12 @@ package org.apache.jena.sparql.core.assembler;
 
 import static org.apache.jena.assembler.JA.data;
 import static org.apache.jena.query.DatasetFactory.createTxnMem;
-import static org.apache.jena.query.ReadWrite.WRITE;
 import static org.apache.jena.riot.RDFDataMgr.read;
 import static org.apache.jena.sparql.core.assembler.AssemblerUtils.setContext;
 import static org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab.pGraphName;
 import static org.apache.jena.sparql.core.assembler.DatasetAssemblerVocab.pNamedGraph;
 import static org.apache.jena.sparql.util.graph.GraphUtils.getAsStringValue;
+import static org.apache.jena.sparql.util.graph.GraphUtils.multiValueAsString;
 import static org.apache.jena.sparql.util.graph.GraphUtils.multiValueResource;
 
 import org.apache.jena.assembler.Assembler;
@@ -33,6 +33,7 @@ import org.apache.jena.assembler.Mode;
 import org.apache.jena.assembler.assemblers.AssemblerBase;
 import org.apache.jena.query.Dataset;
 import org.apache.jena.rdf.model.Resource;
+import org.apache.jena.system.Txn;
 
 /**
  * An {@link Assembler} that creates in-memory {@link Dataset}s.
@@ -49,25 +50,21 @@ public class InMemDatasetAssembler extends AssemblerBase implements Assembler
{
 		final Dataset dataset = createTxnMem();
 		setContext(root, dataset.getContext());
 
-		dataset.begin(WRITE);
-
-		// load data into the default graph
-		if (root.hasProperty(data)) {
-		    multiValueResource(root, data)
-				.forEach(defaultGraphDocument -> read(dataset, defaultGraphDocument.getURI()));
-		}
-
-		// load data into named graphs
-		multiValueResource(root, pNamedGraph).forEach(namedGraphResource -> {
-			final String graphName = getAsStringValue(namedGraphResource, pGraphName);
-			if (namedGraphResource.hasProperty(data)) {
-			    multiValueResource(namedGraphResource, data)
-			        .forEach(namedGraphData -> read(dataset.getNamedModel(graphName), namedGraphData.getURI()));
-			}
+		Txn.executeWrite(dataset, ()->{ 
+    		// Load data into the default graph
+		    // This also loads quads into the dataset.
+    		multiValueAsString(root, data)
+    		    .forEach(dataURI -> read(dataset, dataURI));
+    
+    		// load data into named graphs
+    		multiValueResource(root, pNamedGraph).forEach(namedGraphResource -> {
+    			final String graphName = getAsStringValue(namedGraphResource, pGraphName);
+    			if (namedGraphResource.hasProperty(data)) {
+    			    multiValueAsString(namedGraphResource, data)
+    			        .forEach(namedGraphData -> read(dataset.getNamedModel(graphName), namedGraphData));
+    			}
+    		});
 		});
-
-		dataset.commit();
-		dataset.end();
 		return dataset;
 	}
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/d174ec04/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
index 719fe21..3d907bd 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/graph/GraphUtils.java
@@ -66,6 +66,24 @@ public class GraphUtils {
         }
         return values ;
     }
+    
+    /** Get a list of the URIs (as strings) and strings
+     *  @see #getAsStringValue
+     */
+    public static List<String> multiValueAsString(Resource r, Property p) {
+        List<RDFNode> nodes = multiValue(r, p) ;
+        List<String> values = new ArrayList<>() ;
+
+        for ( RDFNode n : nodes ) {
+            if ( n.isLiteral() ) {
+                values.add(((Literal)n).getString()) ;
+            }
+            if ( n.isURIResource() ) {
+                values.add(((Resource)n).getURI());
+            }
+        }
+        return values ;
+    }
 
     public static List<RDFNode> multiValue(Resource r, Property p) {
         List<RDFNode> values = new ArrayList<>() ;

http://git-wip-us.apache.org/repos/asf/jena/blob/d174ec04/jena-arq/src/test/java/org/apache/jena/sparql/core/assembler/TestInMemDatasetAssembler.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/core/assembler/TestInMemDatasetAssembler.java
b/jena-arq/src/test/java/org/apache/jena/sparql/core/assembler/TestInMemDatasetAssembler.java
index 29644b6..6d88214 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/core/assembler/TestInMemDatasetAssembler.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/core/assembler/TestInMemDatasetAssembler.java
@@ -22,7 +22,6 @@ import static java.nio.file.Files.createTempFile;
 import static org.apache.jena.assembler.JA.MemoryModel ;
 import static org.apache.jena.assembler.JA.data ;
 import static org.apache.jena.assembler.Mode.DEFAULT;
-import static org.apache.jena.query.DatasetFactory.createTxnMem;
 import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
 import static org.apache.jena.riot.Lang.NQUADS;
 import static org.apache.jena.riot.RDFDataMgr.write;
@@ -45,7 +44,10 @@ import org.apache.jena.rdf.model.Model;
 import org.apache.jena.rdf.model.Resource;
 import org.apache.jena.rdf.model.Statement;
 import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.DatasetGraphFactory;
 import org.apache.jena.sparql.core.Quad;
+import org.apache.jena.sparql.sse.SSE;
+import org.apache.jena.sparql.util.IsoMatcher;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -113,7 +115,7 @@ public class TestInMemDatasetAssembler extends Assert {
 	}
 
 	@Test
-	public void directDataLinkToQuads() throws IOException {
+	public void directDataQuadsURI() throws IOException {
 		// first make a file of quads to load later
 		final Model model = createDefaultModel();
 		final Path quads = createTempFile("quadExample", ".nq");
@@ -122,7 +124,7 @@ public class TestInMemDatasetAssembler extends Assert {
 		simpleExample.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem);
 		simpleExample.addProperty(data, quadsURI);
 
-		final DatasetGraph dsg = createTxnMem().asDatasetGraph();
+		final DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
 		model.listStatements().mapWith(Statement::asTriple).mapWith(t -> new Quad(quadsURI.asNode(),
t))
 				.forEachRemaining(dsg::add);
 		try (OutputStream out = new FileOutputStream(quads.toFile())) {
@@ -136,7 +138,29 @@ public class TestInMemDatasetAssembler extends Assert {
 		assertTrue(assembledNamedModel.contains(assembledNamedModel.createStatement(simpleExample,
data, quadsURI)));
 	}
 
-	@Test(expected = CannotConstructException.class)
+    @Test
+    public void directDataQuadsString() throws IOException {
+        // first make a file of quads to load later
+        DatasetGraph dsgData = DatasetGraphFactory.createTxnMem();
+        dsgData.add(SSE.parseQuad("(_ :s1 :p1 :o1)"));
+        dsgData.add(SSE.parseQuad("(:gn :s2 :p2 :o2)"));
+        Path quads = createTempFile("quadExample2", ".nq");
+        String dataFileName = quads.toFile().toURI().toString();
+        try (OutputStream out = new FileOutputStream(quads.toFile())) {
+            write(out, dsgData, NQUADS);
+        }
+        
+        Model assemblerModel = createDefaultModel();
+        Resource simpleExample2 = assemblerModel.createResource("test:simpleExample2");
+        simpleExample2.addProperty(type, DatasetAssemblerVocab.tDatasetTxnMem) ;
+        simpleExample2.addProperty(data, dataFileName);
+        
+        final Dataset dataset = assemble(simpleExample2);
+        
+        assertTrue(IsoMatcher.isomorphic(dsgData, dataset.asDatasetGraph()));
+    }
+
+    @Test(expected = CannotConstructException.class)
 	public void wrongKindOfAssemblerDefinition() {
 		final Model model = createDefaultModel();
 		final Resource badExample = model.createResource("test:badExample");

http://git-wip-us.apache.org/repos/asf/jena/blob/d174ec04/jena-fuseki2/examples/fuseki-in-mem-txn.ttl
----------------------------------------------------------------------
diff --git a/jena-fuseki2/examples/fuseki-in-mem-txn.ttl b/jena-fuseki2/examples/fuseki-in-mem-txn.ttl
new file mode 100644
index 0000000..340d53f
--- /dev/null
+++ b/jena-fuseki2/examples/fuseki-in-mem-txn.ttl
@@ -0,0 +1,24 @@
+## Licensed under the terms of http://www.apache.org/licenses/LICENSE-2.0
+
+@prefix :        <#> .
+@prefix fuseki:  <http://jena.apache.org/fuseki#> .
+@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix ja:      <http://jena.hpl.hp.com/2005/11/Assembler#> .
+@prefix tdb:     <http://jena.hpl.hp.com/2008/tdb#> .
+
+<#serviceInMemory> rdf:type fuseki:Service;
+    rdfs:label                   "In-memory, trasnactioal dataset.";
+    fuseki:name                  "ds";
+    fuseki:serviceQuery          "query";
+    fuseki:serviceQuery          "sparql";
+    fuseki:serviceUpdate         "update";
+    fuseki:serviceUpload         "upload" ;
+    fuseki:serviceReadGraphStore "data" ;
+    fuseki:serviceReadGraphStore "get" ;
+    fuseki:dataset <#dataset> ;
+.
+
+<#dataset> rdf:type ja:DatasetTxnMem;
+   ja:data <file:data.trig>;
+.


Mime
View raw message