use of org.apache.jena.shared.impl.PrefixMappingImpl in project jena by apache.
the class TestPrefixMappingUtils method prefixesTTL2.
@Test
public void prefixesTTL2() {
// 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.calcInUsePrefixMappingTTL(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);
}
use of org.apache.jena.shared.impl.PrefixMappingImpl in project jena by apache.
the class PrefixMappingUtils method calcInUsePrefixMappingTTL.
/**
* Analyse the graph to see which prefixes of the given {@link PrefixMapping} are used
* by the graph triples.
* <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, PrefixMapping)}.
* <p>
* This function does not calculate new prefixes.
*
* @see #calcInUsePrefixMapping(Graph, PrefixMapping)
*/
public static PrefixMapping calcInUsePrefixMappingTTL(Graph graph, PrefixMapping prefixMapping) {
/* Method:
*
* For each URI, split in in the usual place, after "/" or "#" for http URIs, and
* after the last ":" for URNs, then see if that is a declared prefix.
*
* Exit early if every prefix is accounted for.
*/
// Map prefix -> URI.
Map<String, String> pmap = prefixMapping.getNsPrefixMap();
// All URIs used as prefixes in the prefix mapping.
Set<String> prefixURIs = new HashSet<>(pmap.values());
// Prefixes used.
Set<String> inUse = new HashSet<>();
Iterator<Triple> iter = graph.find(null, null, null);
while (iter.hasNext()) {
Triple triple = iter.next();
processTTL(triple, inUse, prefixMapping);
if (inUse.size() == prefixURIs.size())
// Fast exit.
break;
}
if (pmap.size() == inUse.size())
return prefixMapping;
// Build result.
PrefixMapping pmap2 = new PrefixMappingImpl();
inUse.forEach((prefix) -> pmap2.setNsPrefix(prefix, prefixMapping.getNsPrefixURI(prefix)));
return pmap2;
}
use of org.apache.jena.shared.impl.PrefixMappingImpl in project jena by apache.
the class PrefixMappingUtils method calcInUsePrefixMapping.
/**
* Analyse the graph to see which prefixes of the given {@link PrefixMapping} 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, PrefixMapping)
*/
public static PrefixMapping calcInUsePrefixMapping(Graph graph, PrefixMapping prefixMapping) {
/* Method:
*
* For each URI in the data, look it up in the trie.
* to see if has a declared prefix.
*
* Exit early if every prefix is accounted for.
*/
// Map prefix to URI.
Map<String, String> pmap = prefixMapping.getNsPrefixMap();
// Map URI to prefix, with partial lookup (all uri keys that partly match the URI)
Trie<String> trie = new Trie<>();
// Change this to "add(uri, uri)" to calculate the uris.
pmap.forEach((prefix, uri) -> trie.add(uri, prefix));
// Prefixes in use.
// (URIs if "add(uri, uri)")
Set<String> inUse = new HashSet<>();
ExtendedIterator<Triple> iter = graph.find(null, null, null);
try {
while (iter.hasNext()) {
Triple triple = iter.next();
process(triple, inUse, trie);
if (pmap.size() == inUse.size())
break;
}
} finally {
iter.close();
}
if (pmap.size() == inUse.size())
return prefixMapping;
// Build result.
PrefixMapping pmap2 = new PrefixMappingImpl();
inUse.forEach((prefix) -> pmap2.setNsPrefix(prefix, prefixMapping.getNsPrefixURI(prefix)));
return pmap2;
}
use of org.apache.jena.shared.impl.PrefixMappingImpl in project webofneeds by researchstudio-sat.
the class WonLinkedDataUtils method findWonNode.
public static Optional<WonNodeInfo> findWonNode(URI someURI, Optional<URI> requesterWebID, LinkedDataSource linkedDataSource) {
assert linkedDataSource != null : "linkedDataSource must not be null";
int depth = 5;
int maxRequests = 1000;
List<Path> propertyPaths = new ArrayList<>();
PrefixMapping pmap = new PrefixMappingImpl();
pmap.withDefaultMappings(PrefixMapping.Standard);
pmap.setNsPrefix("won", WON.getURI());
pmap.setNsPrefix("msg", WONMSG.getURI());
pmap.setNsPrefix("rdfs", RDFS.getURI());
propertyPaths.add(PathParser.parse("^rdfs:member / ^won:messageContainer / ^won:wonNode", pmap));
propertyPaths.add(PathParser.parse("^won:messageContainer / ^won:wonNode", pmap));
propertyPaths.add(PathParser.parse("^won:wonNode", pmap));
propertyPaths.add(PathParser.parse("rdfs:member / ^won:wonNode", pmap));
Dataset ds = linkedDataSource.getDataForResourceWithPropertyPath(someURI, requesterWebID, propertyPaths, maxRequests, depth);
WonNodeInfo info = WonRdfUtils.WonNodeUtils.getWonNodeInfo(ds);
return Optional.ofNullable(info);
}
Aggregations