/** * Analyse the graph to see which prefixes of the graph are in use. * <p> * In the case of overlapping prefixes (where one prefix declaration is has an initial * URI string which matches another prefix declaration), all are included, though * they may not be used when printing (that depends on the output process). In effect, * this process has "false positives". * <p> * This function does not calculate new prefixes. * * @see #calcInUsePrefixMappingTTL(Graph) */ public static PrefixMapping calcInUsePrefixMapping(Graph graph) { PrefixMapping prefixMapping = graph.getPrefixMapping() ; if ( prefixMapping == null ) return null ; return calcInUsePrefixMapping(graph, prefixMapping) ; }
/** * Analyse the graph to see which prefixes of the graph are in use. * <p> * In the case of overlapping prefixes (where one prefix declaration is has an initial * URI string which matches another prefix declaration), all are included, though * they may not be used when printing (that depends on the output process). In effect, * this process has "false positives". * <p> * This function does not calculate new prefixes. * * @see #calcInUsePrefixMappingTTL(Graph) */ public static PrefixMapping calcInUsePrefixMapping(Graph graph) { PrefixMapping prefixMapping = graph.getPrefixMapping() ; if ( prefixMapping == null ) return null ; return calcInUsePrefixMapping(graph, prefixMapping) ; }
/** * Return a read-only graph that has the same data (RDF triples) as the one given, but has a * prefix mapping that only includes "in use" prefixes as calculated by * {@link #calcInUsePrefixMapping(Graph, PrefixMapping)}. * <p> * The prefix mappings of the two graphs are not connected. * Later changes to the prefix mapping of the original graph are not reflected in the returned graph. * Modifications to the triples contained in the underlying graph are reflected. */ public static Graph graphInUsePrefixMapping(Graph graph) { final PrefixMapping prefixMapping = calcInUsePrefixMapping(graph) ; prefixMapping.lock() ; Graph graph2 = new WrappedGraph(graph) { @Override public void performAdd(Triple triple) { throw new UnsupportedOperationException() ; } @Override public void performDelete(Triple triple) { throw new UnsupportedOperationException() ; } @Override public PrefixMapping getPrefixMapping() { return prefixMapping ; } } ; return graph2 ; }
/** * Return a read-only graph that has the same data (RDF triples) as the one given, but has a * prefix mapping that only includes "in use" prefixes as calculated by * {@link #calcInUsePrefixMapping(Graph, PrefixMapping)}. * <p> * The prefix mappings of the two graphs are not connected. * Later changes to the prefix mapping of the original graph are not reflected in the returned graph. * Modifications to the triples contained in the underlying graph are reflected. */ public static Graph graphInUsePrefixMapping(Graph graph) { final PrefixMapping prefixMapping = calcInUsePrefixMapping(graph) ; prefixMapping.lock() ; Graph graph2 = new WrappedGraph(graph) { @Override public void performAdd(Triple triple) { throw new UnsupportedOperationException() ; } @Override public void performDelete(Triple triple) { throw new UnsupportedOperationException() ; } @Override public PrefixMapping getPrefixMapping() { return prefixMapping ; } } ; return graph2 ; }
@Test public void prefixes1() { // All prefixes used. String data1 = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "@prefix ex: <http://example/ex#> ." , "" , ":s1 :p :x1 ." , ":s1 ex:p :x1 ." ) ; Graph graph1 = create(data1) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = graph1.getPrefixMapping() ; Assert.assertEquals(2, size(pmap)) ; Assert.assertEquals(pmapExpected, pmap) ; }
@Test public void prefixes3() { // Some URIs without prefixes. String data = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "" , "<http://other/s1> :p :x1 ." ) ; Graph graph1 = create(data) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; }
@Test public void prefixes3() { // Some URIs without prefixes. String data = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "" , "<http://other/s1> :p :x1 ." ) ; Graph graph1 = create(data) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; }
@Test public void prefixes4() { // No prefixes. String data = StrUtils.strjoinNL ( "<http://other/s1> <http://example/p> 123 ." ) ; Graph graph1 = create(data) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; Assert.assertEquals(0, size(pmap)) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; }
@Test public void prefixes4() { // No prefixes. String data = StrUtils.strjoinNL ( "<http://other/s1> <http://example/p> 123 ." ) ; Graph graph1 = create(data) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; Assert.assertEquals(0, size(pmap)) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; }
@Test public void prefixes1() { // All prefixes used. String data1 = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "@prefix ex: <http://example/ex#> ." , "" , ":s1 :p :x1 ." , ":s1 ex:p :x1 ." ) ; Graph graph1 = create(data1) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = graph1.getPrefixMapping() ; Assert.assertEquals(2, size(pmap)) ; Assert.assertEquals(pmapExpected, pmap) ; }
@Test public void prefixes2() { // Some prefixes used String data2 = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "@prefix ex: <http://example/ex#> ." , "@prefix notinuse: <http://example/whatever/> ." , "" , ":s1 :p :x1 ." , ":s1 ex:p :x1 ." ) ; Graph graph1 = create(data2) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ; pmapExpected.setNsPrefix("ex", "http://example/ex#") ; Assert.assertEquals(2, size(pmap)) ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; Assert.assertTrue(pmap.getNsPrefixURI("notinuse") == null) ; }
@Test public void prefixes2() { // Some prefixes used String data2 = StrUtils.strjoinNL ("@prefix : <http://example/> ." , "@prefix ex: <http://example/ex#> ." , "@prefix notinuse: <http://example/whatever/> ." , "" , ":s1 :p :x1 ." , ":s1 ex:p :x1 ." ) ; Graph graph1 = create(data2) ; PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph1) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ; pmapExpected.setNsPrefix("ex", "http://example/ex#") ; Assert.assertEquals(2, size(pmap)) ; Assert.assertTrue(sameMapping(pmapExpected, pmap)) ; Assert.assertTrue(pmap.getNsPrefixURI("notinuse") == null) ; }
PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ;
PrefixMapping pmap = PrefixMappingUtils.calcInUsePrefixMapping(graph) ; PrefixMapping pmapExpected = new PrefixMappingImpl() ; pmapExpected.setNsPrefix("", "http://example/") ;