Search in sources :

Example 1 with Trie

use of org.apache.jena.atlas.lib.Trie in project jena by apache.

the class PrefixMappingUtils method fullMethod.

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

Example 2 with Trie

use of org.apache.jena.atlas.lib.Trie 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)

Aggregations

Trie (org.apache.jena.atlas.lib.Trie)2 Triple (org.apache.jena.graph.Triple)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)1 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)1