Search in sources :

Example 1 with Triple

use of com.hp.hpl.jena.graph.Triple in project stanbol by apache.

the class RdfIndexingSource method listObjects.

/* ----------------------------------------------------------------------
     *     RDF Backend implementation
     * ----------------------------------------------------------------------
     */
@Override
public Collection<Node> listObjects(Node subject, Node property) {
    Collection<Node> nodes = new ArrayList<Node>();
    if (bnodePrefix != null && subject.isURI() && subject.getURI().startsWith(bnodePrefix)) {
        subject = NodeFactory.createAnon(new AnonId(subject.getURI().substring(bnodePrefix.length())));
    }
    ExtendedIterator<Triple> it = indexingDataset.getDefaultGraph().find(subject, property, null);
    while (it.hasNext()) {
        //STANBOL-765: we need also to transform bnodes to URIs for the
        //RDFBackend implementation
        Node object = it.next().getObject();
        if (bnodePrefix != null && object.isBlank()) {
            StringBuilder sb = new StringBuilder(bnodePrefix);
            sb.append(object.getBlankNodeId().getLabelString());
            object = NodeFactory.createURI(sb.toString());
        }
        nodes.add(object);
    }
    it.close();
    return nodes;
}
Also used : Triple(com.hp.hpl.jena.graph.Triple) Node(com.hp.hpl.jena.graph.Node) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) ArrayList(java.util.ArrayList) AnonId(com.hp.hpl.jena.rdf.model.AnonId)

Example 2 with Triple

use of com.hp.hpl.jena.graph.Triple in project stanbol by apache.

the class RdfIndexingSource method getEntityData.

@Override
public Representation getEntityData(String id) {
    final Node resource;
    //STANBOL-765: check if the parsed id represents an bnode
    if (bnodePrefix != null && id.startsWith(bnodePrefix)) {
        resource = NodeFactory.createAnon(AnonId.create(id.substring(bnodePrefix.length())));
    } else {
        resource = NodeFactory.createURI(id);
    }
    Representation source = vf.createRepresentation(id);
    boolean found;
    ExtendedIterator<Triple> outgoing = null;
    try {
        // There may still be exceptions while reading triples
        outgoing = indexingDataset.getDefaultGraph().find(resource, null, null);
        found = outgoing.hasNext();
        while (outgoing.hasNext()) {
            //iterate over the statements for that resource
            Triple statement = outgoing.next();
            Node predicate = statement.getPredicate();
            if (predicate == null || !predicate.isURI()) {
                log.warn("Ignore field {} for resource {} because it is null or not an URI!", predicate, resource);
            } else {
                String field = predicate.getURI();
                Node value = statement.getObject();
                processValue(value, source, field);
            }
        //end else predicate != null
        }
    //end iteration over resource triple
    } catch (Exception e) {
        log.warn("Unable to retrieve entity data for Entity '" + id + "'", e);
        found = false;
        try {
            if (outgoing != null) {
                outgoing.close();
            }
        } catch (Exception e1) {
        /* ignore */
        }
    }
    if (found) {
        if (log.isTraceEnabled()) {
            log.info("RDFTerm: \n{}", ModelUtils.getRepresentationInfo(source));
        }
        return source;
    } else {
        log.debug("No Statements found for id {} (Node: {})!", id, resource);
        return null;
    }
}
Also used : Triple(com.hp.hpl.jena.graph.Triple) Node(com.hp.hpl.jena.graph.Node) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) URISyntaxException(java.net.URISyntaxException) NoSuchElementException(java.util.NoSuchElementException) DatatypeFormatException(com.hp.hpl.jena.datatypes.DatatypeFormatException)

Example 3 with Triple

use of com.hp.hpl.jena.graph.Triple in project stanbol by apache.

the class RdfIndexingSource method listSubjects.

@Override
public Collection<Node> listSubjects(Node property, Node object) {
    Collection<Node> nodes = new ArrayList<Node>();
    if (bnodePrefix != null && object.isURI() && object.getURI().startsWith(bnodePrefix)) {
        object = NodeFactory.createAnon(new AnonId(object.getURI().substring(bnodePrefix.length())));
    }
    ExtendedIterator<Triple> it = indexingDataset.getDefaultGraph().find(null, property, object);
    while (it.hasNext()) {
        Node subject = it.next().getSubject();
        //RDFBackend implementation
        if (bnodePrefix != null && subject.isBlank()) {
            StringBuilder sb = new StringBuilder(bnodePrefix);
            sb.append(subject.getBlankNodeId().getLabelString());
            subject = NodeFactory.createURI(sb.toString());
        }
        nodes.add(subject);
    }
    it.close();
    return nodes;
}
Also used : Triple(com.hp.hpl.jena.graph.Triple) Node(com.hp.hpl.jena.graph.Node) RDFNode(com.hp.hpl.jena.rdf.model.RDFNode) ArrayList(java.util.ArrayList) AnonId(com.hp.hpl.jena.rdf.model.AnonId)

Aggregations

Node (com.hp.hpl.jena.graph.Node)3 Triple (com.hp.hpl.jena.graph.Triple)3 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)3 AnonId (com.hp.hpl.jena.rdf.model.AnonId)2 ArrayList (java.util.ArrayList)2 DatatypeFormatException (com.hp.hpl.jena.datatypes.DatatypeFormatException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchElementException (java.util.NoSuchElementException)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1