/** * 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> * This function attempts to process each URI in the graph as if it were to be printed * in Turtle. Only prefixes that lead to valid output strings are returned. This is * more expensive than {@link #calcInUsePrefixMapping(Graph)}. * <p> * This function does not calculate new prefixes. * * @see #calcInUsePrefixMappingTTL(Graph) */ public static PrefixMapping calcInUsePrefixMappingTTL(Graph graph) { PrefixMapping prefixMapping = graph.getPrefixMapping() ; if ( prefixMapping == null ) return null ; return calcInUsePrefixMappingTTL(graph, prefixMapping) ; }
while(iter.hasNext()) { Triple triple = iter.next() ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getSubject()) ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getPredicate()) ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getObject()) ; if ( inUsePrefixURIs.size() == prefixURIs.size() )
private static void print(Set<String> inUsePrefixURIs, PrefixMapping prefixMapping) { // Convert to prefixes. Set<String> inUsePrefixes = urisToPrefixes(prefixMapping, inUsePrefixURIs) ; // ----- Analysis System.out.println("In use: "+inUsePrefixURIs) ; System.out.println("In use: "+inUsePrefixes) ; inUsePrefixURIs.forEach((u)->System.out.printf(" %s: -> <%s>\n", prefixMapping.getNsURIPrefix(u), u)) ; // Calc not needed to be efficient. Map<String, String> pmap = prefixMapping.getNsPrefixMap() ; Set<String> prefixURIs = new HashSet<>(pmap.values()) ; Set<String> notInUseURIs = SetUtils.difference(prefixURIs, inUsePrefixURIs) ; Set<String> notInUsePrefixes = SetUtils.difference(pmap.keySet(), inUsePrefixes) ; System.out.println("Not in use: "+notInUseURIs) ; System.out.println("Not in use: "+notInUsePrefixes) ; notInUseURIs.forEach((u)->System.out.printf(" %s: -> <%s>\n", prefixMapping.getNsURIPrefix(u), u)) ; }
/** Check every URI as a possible use of a prefix */ private static Set<String> fullMethod(Model m) { /* Method: Covers prefixes not based on "/", "#" or final ":" splitting. * * Build a trie to use as a partial lookup matcher. * For each URI in the data, look it up as a partial match in the trie * to get all URIs in the prefix map that apply. */ // Map prefix to URI. Map<String, String> pmap = m.getNsPrefixMap() ; // Map URI to prefix, with partial lookup (all uri keys that partly match the URI) Trie<String> trie = new Trie<>() ; // change to add(uri, prefix) to get prefixes. pmap.forEach((prefix,uri)-> trie.add(uri, uri)) ; Iterator<Triple> iter = m.getGraph().find(null, null, null) ; // Prefix URIs in use. Set<String> inUseURIs = new HashSet<>() ; while(iter.hasNext()) { Triple triple = iter.next() ; processFull(trie, inUseURIs, triple.getSubject()) ; processFull(trie, inUseURIs, triple.getPredicate()) ; processFull(trie, inUseURIs, triple.getObject()) ; if ( pmap.size() == inUseURIs.size() ) break ; } return inUseURIs ; }
private static void print(Set<String> inUsePrefixURIs, PrefixMapping prefixMapping) { // Convert to prefixes. Set<String> inUsePrefixes = urisToPrefixes(prefixMapping, inUsePrefixURIs) ; // ----- Analysis System.out.println("In use: "+inUsePrefixURIs) ; System.out.println("In use: "+inUsePrefixes) ; inUsePrefixURIs.forEach((u)->System.out.printf(" %s: -> <%s>\n", prefixMapping.getNsURIPrefix(u), u)) ; // Calc not needed to be efficient. Map<String, String> pmap = prefixMapping.getNsPrefixMap() ; Set<String> prefixURIs = new HashSet<>(pmap.values()) ; Set<String> notInUseURIs = SetUtils.difference(prefixURIs, inUsePrefixURIs) ; Set<String> notInUsePrefixes = SetUtils.difference(pmap.keySet(), inUsePrefixes) ; System.out.println("Not in use: "+notInUseURIs) ; System.out.println("Not in use: "+notInUsePrefixes) ; notInUseURIs.forEach((u)->System.out.printf(" %s: -> <%s>\n", prefixMapping.getNsURIPrefix(u), u)) ; }
/** Check every URI as a possible use of a prefix */ private static Set<String> fullMethod(Model m) { /* Method: Covers prefixes not based on "/", "#" or final ":" splitting. * * Build a trie to use as a partial lookup matcher. * For each URI in the data, look it up as a partial match in the trie * to get all URIs in the prefix map that apply. */ // Map prefix to URI. Map<String, String> pmap = m.getNsPrefixMap() ; // Map URI to prefix, with partial lookup (all uri keys that partly match the URI) Trie<String> trie = new Trie<>() ; // change to add(uri, prefix) to get prefixes. pmap.forEach((prefix,uri)-> trie.add(uri, uri)) ; Iterator<Triple> iter = m.getGraph().find(null, null, null) ; // Prefix URIs in use. Set<String> inUseURIs = new HashSet<>() ; while(iter.hasNext()) { Triple triple = iter.next() ; processFull(trie, inUseURIs, triple.getSubject()) ; processFull(trie, inUseURIs, triple.getPredicate()) ; processFull(trie, inUseURIs, triple.getObject()) ; if ( pmap.size() == inUseURIs.size() ) break ; } return inUseURIs ; }
/** * 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> * This function attempts to process each URI in the graph as if it were to be printed * in Turtle. Only prefixes that lead to valid output strings are returned. This is * more expensive than {@link #calcInUsePrefixMapping(Graph)}. * <p> * This function does not calculate new prefixes. * * @see #calcInUsePrefixMappingTTL(Graph) */ public static PrefixMapping calcInUsePrefixMappingTTL(Graph graph) { PrefixMapping prefixMapping = graph.getPrefixMapping() ; if ( prefixMapping == null ) return null ; return calcInUsePrefixMappingTTL(graph, prefixMapping) ; }
while(iter.hasNext()) { Triple triple = iter.next() ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getSubject()) ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getPredicate()) ; processBySplit(prefixURIs, inUsePrefixURIs, triple.getObject()) ; if ( inUsePrefixURIs.size() == prefixURIs.size() )
/** * 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 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 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 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 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 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) ; }