private Literal getOptionalObjectLiteral(Model model, Resource subject, URI predicate, String lang) { Set<Value> objects = GraphUtil.getObjects(model, subject, predicate); Literal result = null; for (Value nextValue : objects) { if (nextValue instanceof Literal) { final Literal literal = (Literal) nextValue; if (result == null || (lang != null && lang.equals(literal.getLanguage()))) { result = literal; } } } return result; }
/** * Gets the objects of the statements with the specified subject, predicate * and (optionally) contexts from the supplied graph. Calling this method is * equivalent to calling <tt>graph.match(subj, pred, null, contexts)</tt> and * adding the objects of the matching statements to a set. See * {@link Graph#match(Resource, URI, Value, Resource[])} for a description of * the parameter values. */ public static Set<Value> getObjects(Graph graph, Resource subj, URI pred, Resource... contexts) { Iterator<Value> iter = getObjectIterator(graph, subj, pred, contexts); return Iterators.addAll(iter, new LinkedHashSet<Value>()); }
/** * Gets the subject of the statement(s) with the specified predicate and * object from the specified contexts in the supplied graph. If the * combination of predicate, object and contexts matches one or more * statements, all these statements should have the same subject. A * {@link GraphUtilException} is thrown if this is not the case. See * {@link Graph#match(Resource, URI, Value, Resource[])} for a description of * the parameter values. * * @return The subject of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If the statements matched by the specified parameters have more * than one unique subject. */ public static Resource getOptionalSubject(Graph graph, URI pred, Value obj, Resource... contexts) throws GraphUtilException { Set<Resource> subjects = getSubjects(graph, pred, obj, contexts); if (subjects.isEmpty()) { return null; } else if (subjects.size() == 1) { return subjects.iterator().next(); } else { throw new GraphUtilException("Multiple " + pred + " properties found"); } }
private Binding getBinding(Resource bindingNode) throws GraphUtilException { Literal name = GraphUtil.getUniqueObjectLiteral(graph, bindingNode, VARIABLE); Value value = GraphUtil.getUniqueObject(graph, bindingNode, VALUE); return new SimpleBinding(name.getLabel(), value); } }
final Literal userLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, USER); if (userLit != null) { setUser(userLit.getLabel()); final Literal pwdLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, PASSWORD); if (pwdLit != null) { setPassword(pwdLit.getLabel()); final Literal instLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, INSTANCE); if (instLit != null) { setInstance(instLit.getLabel()); final Literal zooLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, ZOOKEEPERS); if (zooLit != null) { setZookeepers(zooLit.getLabel()); final Literal mockLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, IS_MOCK); if (mockLit != null) { setMock(Boolean.parseBoolean(mockLit.getLabel()));
GraphUtil.getOptionalObjectLiteral(model, nextRecord, OasConstants.DC_TERMS_MODIFIED); final Literal message = GraphUtil.getOptionalObjectLiteral(model, nextRecord, RDFS.COMMENT); final Literal action = GraphUtil.getOptionalObjectLiteral(model, nextRecord, OasConstants.URN_TAG_LOGACTION); final URI method = GraphUtil.getOptionalObjectURI(model, nextRecord, OasConstants.URN_TAG_LOGOPERATION_VERB); final Literal user = GraphUtil.getOptionalObjectLiteral(model, nextRecord, OasConstants.FOAF_MAKER); final Literal reference = GraphUtil.getOptionalObjectLiteral(model, nextRecord, OasConstants.URN_TAG_AUDITLOG_REF); final Literal ipaddress = GraphUtil.getOptionalObjectLiteral(model, nextRecord, OasConstants.URN_TAG_USERSIPADDRESS);
/** * Utility method that casts the return value of * {@link #getUniqueObject(Graph, Resource, URI, Resource[])} to a Literal, * or throws a GraphUtilException if that value is not a Literal. * * @return The object of the matched statement(s). * @throws GraphUtilException * If such an exception is thrown by * {@link #getUniqueObject(Graph, Resource, URI, Resource[])} or if * its return value is not a Literal. */ public static Literal getUniqueObjectLiteral(Graph graph, Resource subj, URI pred) throws GraphUtilException { Value obj = getUniqueObject(graph, subj, pred); if (obj instanceof Literal) { return (Literal)obj; } else { throw new GraphUtilException("Expected literal for property " + pred); } }
@Override public void parse(Graph graph, Resource implNode) throws RepositoryConfigException { super.parse(graph, implNode); try { URI uri = GraphUtil.getOptionalObjectURI(graph, implNode, ENDPOINT); if (uri != null) { setURL(uri.stringValue()); } Set<Value> set = GraphUtil.getObjects(graph, implNode, SUBJECT_SPACE); subjects.clear(); for (Value space : set) { subjects.add(space.stringValue()); } } catch (GraphUtilException e) { throw new RepositoryConfigException(e.getMessage(), e); } } }
@Override public void endRDF() throws RDFHandlerException { try { Resource resultSetNode = GraphUtil.getUniqueSubject(graph, RDF.TYPE, RESULTSET); List<String> bindingNames = getBindingNames(resultSetNode); tqrHandler.startQueryResult(bindingNames); Iterator<Value> solIter = GraphUtil.getObjectIterator(graph, resultSetNode, SOLUTION); while (solIter.hasNext()) { Value solutionNode = solIter.next(); if (solutionNode instanceof Resource) { reportSolution((Resource)solutionNode, bindingNames); } else { throw new RDFHandlerException("Value for " + SOLUTION + " is not a resource: " + solutionNode); } } tqrHandler.endQueryResult(); } catch (GraphUtilException e) { throw new RDFHandlerException(e.getMessage(), e); } catch (TupleQueryResultHandlerException e) { throw new RDFHandlerException(e.getMessage(), e); } }
/** * Utility method that casts the return value of * {@link #getOptionalObject(Graph, Resource, URI, Resource[])} to a * Resource, or throws a GraphUtilException if that value is not a Resource. * * @return The object of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If such an exception is thrown by * {@link #getOptionalObject(Graph, Resource, URI, Resource[])} or if * its return value is not a Resource. */ public static Resource getOptionalObjectResource(Graph graph, Resource subj, URI pred) throws GraphUtilException { Value obj = getOptionalObject(graph, subj, pred); if (obj == null || obj instanceof Resource) { return (Resource)obj; } else { throw new GraphUtilException("Expected URI or blank node for property " + pred); } }
@Override public void endRDF() throws RDFHandlerException { try { Resource resultSetNode = GraphUtil.getUniqueSubject(graph, RDF.TYPE, RESULTSET); Literal booleanLit = GraphUtil.getUniqueObjectLiteral(graph, resultSetNode, BOOLEAN); if (booleanLit.equals(DAWGTestResultSetSchema.TRUE)) { value = true; } else if (booleanLit.equals(DAWGTestResultSetSchema.FALSE)) { value = false; } else { throw new RDFHandlerException("Invalid boolean value: " + booleanLit); } } catch (GraphUtilException e) { throw new RDFHandlerException(e.getMessage(), e); } } }
/** * Utility method that casts the return value of * {@link #getUniqueSubject(Graph, URI, Value, Resource[])} to a URI, or * throws a GraphUtilException if that value is not a URI. * * @return The subject of the matched statement(s). * @throws GraphUtilException * If such an exception is thrown by * {@link #getUniqueSubject(Graph, URI, Value, Resource[])} or if its * return value is not a URI. */ public static URI getUniqueSubjectURI(Graph graph, URI pred, Value obj, Resource... contexts) throws GraphUtilException { Resource subject = getUniqueSubject(graph, pred, obj, contexts); if (subject instanceof URI) { return (URI)subject; } else { throw new GraphUtilException("Expected URI for subject " + subject); } }
/** * Utility method that casts the return value of * {@link #getOptionalSubject(Graph, URI, Value, Resource[])} to a URI, or * throws a GraphUtilException if that value is not a URI. * * @return The subject of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If such an exception is thrown by * {@link #getOptionalSubject(Graph, URI, Value, Resource[])} or if * its return value is not a URI. */ public static URI getOptionalSubjectURI(Graph graph, URI pred, Value obj, Resource... contexts) throws GraphUtilException { Resource subject = getOptionalSubject(graph, pred, obj, contexts); if (subject instanceof URI) { return (URI)subject; } else { throw new GraphUtilException("Expected URI for subject " + subject); } }
Literal typeLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, REPOSITORYTYPE); if (typeLit != null) { setType(typeLit.getLabel()); Literal qtypeLit = GraphUtil.getOptionalObjectLiteral(graph, implNode, QUEST_TYPE); if (qtypeLit != null) { setQuestType(qtypeLit.getLabel()); Literal name = GraphUtil.getOptionalObjectLiteral(graph, implNode, NAME); if (name != null) { setName(name.getLabel()); Literal owlfile = GraphUtil.getOptionalObjectLiteral(graph, implNode, OWLFILE); if (owlfile != null) { setOwlFile(owlfile.getLabel()); Literal obdafile = GraphUtil.getOptionalObjectLiteral(graph, implNode, OBDAFILE); if (obdafile != null) { setObdaFile(obdafile.getLabel()); Literal existl = GraphUtil.getOptionalObjectLiteral(graph, implNode, EXISTENTIAL); if (existl != null) { setExistential(existl.booleanValue()); Literal rewr = GraphUtil.getOptionalObjectLiteral(graph, implNode, REWRITING); if (rewr != null) { setRewriting(rewr.getLabel());
/** * Utility method that casts the return value of * {@link #getUniqueObject(Graph, Resource, URI, Resource[])} to a Resource, * or throws a GraphUtilException if that value is not a Resource. * * @return The object of the matched statement(s). * @throws GraphUtilException * If such an exception is thrown by * {@link #getUniqueObject(Graph, Resource, URI, Resource[])} or if * its return value is not a Resource. */ public static Resource getUniqueObjectResource(Graph graph, Resource subj, URI pred) throws GraphUtilException { Value obj = getUniqueObject(graph, subj, pred); if (obj instanceof Resource) { return (Resource)obj; } else { throw new GraphUtilException("Expected URI or blank node for property " + pred); } }
/** * Utility method that casts the return value of * {@link #getOptionalObject(Graph, Resource, URI, Resource[])} to a URI, or * throws a GraphUtilException if that value is not a URI. * * @return The object of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If such an exception is thrown by * {@link #getOptionalObject(Graph, Resource, URI, Resource[])} or if * its return value is not a URI. */ public static URI getOptionalObjectURI(Graph graph, Resource subj, URI pred) throws GraphUtilException { Value obj = getOptionalObject(graph, subj, pred); if (obj == null || obj instanceof URI) { return (URI)obj; } else { throw new GraphUtilException("Expected URI for property " + pred); } }
/** * Utility method that casts the return value of * {@link #getUniqueSubject(Graph, IRI, Value, Resource[])} to a URI, or * throws a GraphUtilException if that value is not a URI. * * @return The subject of the matched statement(s). * @throws GraphUtilException * If such an exception is thrown by * {@link #getUniqueSubject(Graph, IRI, Value, Resource[])} or if its * return value is not a URI. * @deprecated since 2.8.0. Use * {@link Model#filter(Resource, IRI, Value, Resource...)} and * {@link Model#subjectIRI()} instead. */ @Deprecated public static IRI getUniqueSubjectURI(Graph graph, IRI pred, Value obj, Resource... contexts) throws GraphUtilException { Resource subject = getUniqueSubject(graph, pred, obj, contexts); if (subject instanceof IRI) { return (IRI)subject; } else { throw new GraphUtilException("Expected URI for subject " + subject); } }
/** * Utility method that casts the return value of * {@link #getOptionalSubject(Graph, IRI, Value, Resource[])} to a URI, or * throws a GraphUtilException if that value is not a URI. * * @return The subject of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If such an exception is thrown by * {@link #getOptionalSubject(Graph, IRI, Value, Resource[])} or if * its return value is not a URI. * @deprecated since 2.8.0. Use * {@link Model#filter(Resource, IRI, Value, Resource...)} and * {@link Model#subjectIRI()} instead. */ @Deprecated public static IRI getOptionalSubjectURI(Graph graph, IRI pred, Value obj, Resource... contexts) throws GraphUtilException { Resource subject = getOptionalSubject(graph, pred, obj, contexts); if (subject == null || subject instanceof IRI) { return (IRI)subject; } else { throw new GraphUtilException("Expected URI for subject " + subject); } }
@Override public void parse(Graph graph, Resource subj) throws RepositoryConfigException { super.parse(graph, subj); try { Literal lit = GraphUtil.getOptionalObjectLiteral(graph, subj, SNAPSHOT); if (lit != null) { setSnapshot(lit.booleanValue()); } lit = GraphUtil.getOptionalObjectLiteral(graph, subj, SERIALIZABLE); if (lit != null) { setSerializable(lit.booleanValue()); } } catch (GraphUtilException e) { throw new RepositoryConfigException(e.toString(), e); } }
/** * Gets the object of the statement(s) with the specified subject and * predicate from the specified contexts in the supplied graph. If the * combination of subject, predicate and contexts matches one or more * statements, all these statements should have the same object. A * {@link GraphUtilException} is thrown if this is not the case. See * {@link Graph#match(Resource, URI, Value, Resource[])} for a description of * the parameter values. * * @return The object of the matched statement(s), or <tt>null</tt> if no * matching statements were found. * @throws GraphUtilException * If the statements matched by the specified parameters have more * than one unique object. */ public static Value getOptionalObject(Graph graph, Resource subj, URI pred, Resource... contexts) throws GraphUtilException { Set<Value> objects = getObjects(graph, subj, pred, contexts); if (objects.isEmpty()) { return null; } else if (objects.size() == 1) { return objects.iterator().next(); } else { throw new GraphUtilException("Multiple " + pred + " properties found"); } }