All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aksw.jena_sparql_api.utils.GraphUtils Maven / Gradle / Ivy

There is a newer version: 3.17.0-1
Show newest version
package org.aksw.jena_sparql_api.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.util.iterator.ExtendedIterator;

public class GraphUtils {

    public static Map indexBySubject(Graph graph) {
        ExtendedIterator it = graph.find(Node.ANY, Node.ANY, Node.ANY);
        Map result;
        try {
            result = indexBySubject(it);
        } finally {
            it.close();
        }
        return result;
    }

    public static Map indexBySubject(Iterable triples) {
        Map result = indexBySubject(triples.iterator());
        return result;
    }

    public static Map indexBySubject(Iterator it) {
        Map result = new HashMap();
        while(it.hasNext()) {
            Triple triple = it.next();
            Node s = triple.getSubject();

            Graph graph = result.get(s);
            if(graph == null) {
                graph = GraphFactory.createGraphMem();
                result.put(s,  graph);
            }
            graph.add(triple);
        }

        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy