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  package org.apache.commons.rdf.simple;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.rdf.api.Graph;
31  import org.apache.commons.rdf.api.IRI;
32  import org.apache.commons.rdf.api.RDF;
33  import org.apache.commons.rdf.api.Triple;
34  import org.junit.AfterClass;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  
38  /**
39   * Test writing graph
40   */
41  public class TestWritingGraph {
42  
43      /*
44       * 200k triples should do - about 7 MB on disk. Override with
45       * -Dtriples=20000000 to exercise your memory banks.
46       */
47      private static final long TRIPLES = Long.getLong("triples", 200000L);
48  
49      /**
50       * Run tests with -Dkeepfiles=true to inspect /tmp files *
51       */
52      private static boolean KEEP_FILES = Boolean.getBoolean("keepfiles");
53  
54      private static Graph graph;
55  
56      private static RDF factory;
57  
58      @BeforeClass
59      public static void createGraph() throws Exception {
60          factory = new SimpleRDF();
61          graph = factory.createGraph();
62          final IRI subject = factory.createIRI("subj");
63          final IRI predicate = factory.createIRI("pred");
64          final List<IRI> types = new ArrayList<>(Types.values());
65          // Ensure we don't try to create a literal with rdf:langString but
66          // without a language tag
67          types.remove(Types.RDF_LANGSTRING);
68          Collections.shuffle(types);
69          for (int i = 0; i < TRIPLES; i++) {
70              if (i % 11 == 0) {
71                  graph.add(subject, predicate, factory.createBlankNode("Example " + i));
72              } else if (i % 5 == 0) {
73                  graph.add(subject, predicate, factory.createLiteral("Example " + i, "en"));
74              } else if (i % 3 == 0) {
75                  graph.add(subject, predicate, factory.createLiteral("Example " + i, types.get(i % types.size())));
76              } else {
77                  graph.add(subject, predicate, factory.createLiteral("Example " + i));
78              }
79          }
80      }
81  
82      @AfterClass
83      public static void tearDownClass() throws Exception {
84          graph.clear();
85          graph = null;
86      }
87  
88      @Test
89      public void createGraphTiming() throws Exception {
90          createGraph();
91      }
92  
93      @Test
94      public void countQuery() {
95          final IRI subject = factory.createIRI("subj");
96          final IRI predicate = factory.createIRI("pred");
97          final long count = graph.stream(subject, predicate, null).unordered().parallel().count();
98          // System.out.println("Counted - " + count);
99          assertEquals(count, TRIPLES);
100     }
101 
102     public static String tripleAsString(final Triple t) {
103         return t.getSubject().ntriplesString() + " " + t.getPredicate().ntriplesString() + " "
104                 + t.getObject().ntriplesString() + " .";
105     }
106 
107     @Test
108     public void writeGraphFromStream() throws Exception {
109         final Path graphFile = Files.createTempFile("graph", ".nt");
110         if (KEEP_FILES) {
111             System.out.println("From stream: " + graphFile);
112         } else {
113             graphFile.toFile().deleteOnExit();
114         }
115 
116         final Stream<CharSequence> stream = graph.stream().map(TestWritingGraph::tripleAsString);
117         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
118     }
119 
120     @Test
121     public void writeGraphFromStreamFiltered() throws Exception {
122         final Path graphFile = Files.createTempFile("graph", ".nt");
123         if (KEEP_FILES) {
124             System.out.println("Filtered stream: " + graphFile);
125         } else {
126             graphFile.toFile().deleteOnExit();
127         }
128 
129         final IRI subject = factory.createIRI("subj");
130         final IRI predicate = factory.createIRI("pred");
131         final Stream<CharSequence> stream = graph.stream(subject, predicate, null).map(TestWritingGraph::tripleAsString);
132         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
133 
134     }
135 
136     @Test
137     public void writeGraphFromStreamFilteredNoMatches() throws Exception {
138         final Path graphFile = Files.createTempFile("graph-empty-", ".nt");
139         if (KEEP_FILES) {
140             System.out.println("Filtered stream: " + graphFile);
141         } else {
142             graphFile.toFile().deleteOnExit();
143         }
144 
145         final IRI subject = factory.createIRI("nonexistent");
146         final IRI predicate = factory.createIRI("pred");
147         final Stream<CharSequence> stream = graph.stream(subject, predicate, null).map(Object::toString);
148         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
149 
150     }
151 
152 }