View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.commons.rdf.jena;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  import java.nio.file.StandardCopyOption;
28  
29  import org.apache.commons.rdf.api.BlankNode;
30  import org.apache.commons.rdf.api.BlankNodeOrIRI;
31  import org.apache.commons.rdf.api.Graph;
32  import org.apache.commons.rdf.api.RDFTerm;
33  import org.apache.commons.rdf.api.Triple;
34  import org.apache.commons.rdf.simple.Types;
35  import org.apache.jena.riot.Lang;
36  import org.apache.jena.riot.RDFDataMgr;
37  import org.apache.jena.sparql.graph.GraphFactory;
38  import org.junit.After;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  /** Adapt a Jena Graph after parsing data into it */
43  public class TestJenaGraphToCommonsRDFGraph {
44      private static final boolean DEBUG = false;
45      private Path turtleFile;
46  
47      @Before
48      public void preparePath() throws IOException {
49          turtleFile = Files.createTempFile("commonsrdf", "test.ttl");
50          Files.copy(getClass().getResourceAsStream("/D.ttl"), turtleFile, StandardCopyOption.REPLACE_EXISTING);
51      }
52  
53      @After
54      public void deletePath() throws IOException {
55          if (turtleFile != null) {
56              Files.deleteIfExists(turtleFile);
57          }
58      }
59  
60      @Test
61      public void jenaToCommonsRDF() throws Exception {
62          final org.apache.jena.graph.Graph jGraph = GraphFactory.createGraphMem();
63          RDFDataMgr.read(jGraph, turtleFile.toUri().toString());
64  
65          final JenaRDF factory = new JenaRDF();
66  
67          // "graph" is a CommonsRDF graph
68          try (final Graph graph = factory.asGraph(jGraph)) {
69  
70              // The below check expected statements from D.ttl
71  
72              final JenaIRI p = factory.createIRI("http://example.com/p");
73              final JenaIRI s = factory.createIRI("http://example.com/s");
74              final JenaLiteral literal123 = factory.createLiteral("123", Types.XSD_INTEGER);
75              assertTrue(graph.contains(s, p, literal123));
76  
77              final JenaIRI p1 = factory.createIRI("http://example.com/p1");
78              // Let's look up the BlankNode
79              final BlankNodeOrIRI bnode1 = graph.stream(null, p1, null).findFirst().map(Triple::getSubject).get();
80              assertTrue(bnode1 instanceof BlankNode);
81  
82              // Verify we can use BlankNode in query again
83              final RDFTerm obj = graph.stream(bnode1, p1, null).findFirst().map(Triple::getObject).get();
84  
85              // Let's look up also that nested blank node
86              assertTrue(obj instanceof BlankNode);
87              final BlankNode bnode2 = (BlankNode) obj;
88  
89              final JenaIRI q = factory.createIRI("http://example.com/q");
90              final JenaLiteral literalR = factory.createLiteral("r", "en");
91              assertTrue(graph.contains(bnode2, q, literalR));
92  
93              // Can we add the same triple again as s/p/o
94              // without affecting graph size?
95              // Just to be evil we add a blanknode-iri-blanknode statement
96              assertEquals(3, graph.size());
97              graph.add(bnode1, p1, bnode2);
98              assertEquals(3, graph.size());
99  
100             // Add the same Triple again
101             graph.stream(bnode2, null, null).findFirst().ifPresent(graph::add);
102             assertEquals(3, graph.size());
103 
104             // Add to CommonsRDF Graph
105             final JenaIRI s2 = factory.createIRI("http://example/s2");
106             final JenaIRI p2 = factory.createIRI("http://example/p2");
107             final JenaLiteral foo = factory.createLiteral("foo");
108             graph.add(s2, p2, foo);
109             assertEquals(4, graph.size());
110             assertTrue(graph.contains(s2, p2, foo));
111 
112             // Verify the corresponding Jena Nodes are in Jena graph
113             assertTrue(jGraph.contains(s2.asJenaNode(), p2.asJenaNode(), foo.asJenaNode()));
114 
115             if (DEBUG) {
116                 System.out.println("==== Write CommonsRDF graph\n");
117                 graph.stream().forEach(System.out::println);
118                 // And its in the Jena graph
119                 System.out.println("\n==== Write Jena graph directly\n");
120                 RDFDataMgr.write(System.out, jGraph, Lang.TTL);
121             }
122         }
123     }
124 }