use of org.openrdf.model.Value in project stanbol by apache.
the class SesameYard method findRepresentation.
@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
if (parsedQuery == null) {
throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
}
final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
RepositoryConnection con = null;
TupleQueryResult results = null;
try {
con = repository.getConnection();
con.begin();
//execute the query
int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
results = executeSparqlFieldQuery(con, query, limit, false);
//parse the results and generate the Representations
//create an own valueFactors so that all the data of the query results
//are added to the same Sesame Model
Model model = new TreeModel();
RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
while (results.hasNext()) {
BindingSet result = results.next();
Value value = result.getValue(query.getRootVariableName());
if (value instanceof URI) {
//copy all data to the model and create the representation
RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
//link the result with the query result
model.add(queryRoot, queryResult, value);
representations.add(rep);
}
//ignore non URI results
}
con.commit();
return new SesameQueryResultList(model, query, representations);
} catch (RepositoryException e) {
throw new YardException("Unable to execute findReferences query", e);
} catch (QueryEvaluationException e) {
throw new YardException("Unable to execute findReferences query", e);
} finally {
if (results != null) {
//close the result if present
try {
results.close();
} catch (QueryEvaluationException ignore) {
/* ignore */
}
}
if (con != null) {
try {
con.close();
} catch (RepositoryException ignore) {
/* ignore */
}
}
}
}
use of org.openrdf.model.Value in project stanbol by apache.
the class RdfRepresentation method remove.
@Override
public void remove(String field, Object parsedValue) {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
if (parsedValue == null) {
log.warn("NULL parsed as value in remove method for symbol " + getId() + " and field " + field + " -> call ignored");
return;
}
URI property = sesameFactory.createURI(field);
Collection<Object> values = new ArrayList<Object>();
ModelUtils.checkValues(factory, parsedValue, values);
for (Object value : values) {
if (value instanceof Value) {
//native support for Sesame types!
removeValue(property, (Value) value);
} else if (value instanceof RdfWrapper) {
//for Sesame RDF wrapper we can directly use the Value
removeValue(property, ((RdfWrapper) value).getValue());
} else if (value instanceof Reference) {
removeValue(property, sesameFactory.createURI(((Reference) value).getReference()));
} else if (value instanceof Text) {
removeValue(property, sesameFactory.createLiteral(((Text) value).getText(), ((Text) value).getLanguage()));
} else {
//else add an typed Literal!
removeValue(property, createTypedLiteral(value));
}
}
}
use of org.openrdf.model.Value in project stanbol by apache.
the class RdfRepresentation method get.
@Override
@SuppressWarnings("unchecked")
public <T> Iterator<T> get(String field, final Class<T> type) throws UnsupportedTypeException {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
URI property = sesameFactory.createURI(field);
//filter for values that are compatible with the parsed type
Iterator<?> iterator = IteratorUtils.filteredIterator(IteratorUtils.transformedIterator(model.filter(subject, property, null).iterator(), // get the object from the statement
objectTransFormer), new ValueTypeFilter<T>(type));
if (!Value.class.isAssignableFrom(type)) {
//if the requested type is not a Sesame value, we need also to
//transform results
iterator = IteratorUtils.transformedIterator(// the already filtered values
iterator, // need to be transformed
org.apache.stanbol.entityhub.model.sesame.ModelUtils.VALUE_TRANSFORMER);
}
return (Iterator<T>) iterator;
}
use of org.openrdf.model.Value in project vcell by virtualcell.
the class Model2JDOM method createElement.
protected Element createElement(Graph model, Resource resource) {
Element element = null;
Iterator<Statement> stmtIter = model.match(resource, RDF.TYPE, null);
while (stmtIter.hasNext()) {
Value object = stmtIter.next().getObject();
if (object instanceof URI) {
URI type = (URI) object;
String nameSpaceURI = type.getNamespace();
String localName = type.getLocalName();
if (localName != null && localName.length() > 0 && nameSpaceURI != null && nameSpaceURI.length() > 0) {
NameSpace ns = nsMap.provideNamesSpace(nameSpaceURI);
element = new Element(localName, Namespace.getNamespace(ns.prefix, ns.uri));
break;
}
}
}
if (element == null) {
element = new Element(TYPELESS_NODE_NAME, nsRDF);
}
if (resource instanceof URI) {
element.setAttribute("about", resource.stringValue(), nsRDF);
} else {
element.setAttribute("nodeID", blankNodeID(resource), nsRDF);
}
return element;
}
use of org.openrdf.model.Value in project vcell by virtualcell.
the class RefGroup method refs.
public Set<MIRIAMRef> refs(Graph graph) {
Iterator<Statement> stmtIter = graph.match(getResource(), null, null);
Set<MIRIAMRef> refs = new HashSet<MIRIAMRef>();
while (stmtIter.hasNext()) {
Statement statement = stmtIter.next();
if (RDFBagUtil.isRDFContainerMembershipProperty(statement.getPredicate())) {
Value object = statement.getObject();
if (object instanceof URI) {
URI resourceRef = (URI) object;
try {
refs.add(MIRIAMRef.createFromURN(resourceRef.stringValue()));
} catch (URNParseFailureException e) {
e.printStackTrace();
}
}
}
}
return refs;
}
Aggregations