Search in sources :

Example 36 with PrefixMappingImpl

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);
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) Graph(org.apache.jena.graph.Graph) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) Test(org.junit.Test)

Example 37 with PrefixMappingImpl

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;
}
Also used : Triple(org.apache.jena.graph.Triple) PrefixMapping(org.apache.jena.shared.PrefixMapping) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl)

Example 38 with PrefixMappingImpl

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;
}
Also used : Triple(org.apache.jena.graph.Triple) PrefixMapping(org.apache.jena.shared.PrefixMapping) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) Trie(org.apache.jena.atlas.lib.Trie)

Example 39 with PrefixMappingImpl

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);
}
Also used : Path(org.apache.jena.sparql.path.Path) PrefixMapping(org.apache.jena.shared.PrefixMapping) Dataset(org.apache.jena.query.Dataset) WonNodeInfo(won.protocol.service.WonNodeInfo) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl)

Aggregations

PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)39 PrefixMapping (org.apache.jena.shared.PrefixMapping)34 Test (org.junit.Test)13 Path (org.apache.jena.sparql.path.Path)11 Graph (org.apache.jena.graph.Graph)8 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)4 TriplePath (org.apache.jena.sparql.core.TriplePath)4 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)4 BeforeClass (org.junit.BeforeClass)4 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)3 ContractTest (org.xenei.junit.contract.ContractTest)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Triple (org.apache.jena.graph.Triple)2 Dataset (org.apache.jena.query.Dataset)2 Date (java.util.Date)1 Trie (org.apache.jena.atlas.lib.Trie)1 SecuredPrefixMapping (org.apache.jena.permissions.graph.SecuredPrefixMapping)1 Query (org.apache.jena.query.Query)1 UpdateDeniedException (org.apache.jena.shared.UpdateDeniedException)1