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;
}
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;
}
}
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;
}
Aggregations