public static List<String> multiValueURI(Resource r, Property p) { List<RDFNode> nodes = multiValue(r, p) ; List<String> values = new ArrayList<String>() ; for ( Iterator<RDFNode> iter= nodes.iterator() ; iter.hasNext() ; ) { RDFNode n = iter.next(); if ( n.isURIResource() ) values.add(((Resource)n).getURI()) ; } return values ; }
public static List<String> multiValueURI(Resource r, Property p) { List<RDFNode> nodes = multiValue(r, p) ; List<String> values = new ArrayList<String>() ; for ( Iterator<RDFNode> iter= nodes.iterator() ; iter.hasNext() ; ) { RDFNode n = iter.next(); if ( n.isURIResource() ) values.add(((Resource)n).getURI()) ; } return values ; }
if (n.isURIResource()) sb.append( "\n " ).append("<").append( n.asNode().getURI() ).append(">"); sb.append("\n }");
private Map<URI, MatchResult> search(String textquery, Query sparqlQuery) { logger.debug("Executing SPARQL query: {}", sparqlQuery); QueryExecution qexec = QueryExecutionFactory.sparqlService(queryEndpoint.toString(), sparqlQuery); ResultSet resultSet = qexec.execSelect(); Map<URI, MatchResult> r = Maps.newLinkedHashMap(); while (resultSet.hasNext()) { QuerySolution solution = resultSet.next(); RDFNode s = solution.get("s"); if (s.isURIResource()) { try { String resource = s.asResource().getURI(); FreeTextMatchResult result = new FreeTextMatchResult(new URI(searchProperty + "?q=" + textquery), new URI(resource)); r.put(new URI(resource), result); } catch (URISyntaxException e) { e.printStackTrace(); } } } return r; }
private void countObjectReferencesIn(Model m) { for (StmtIterator statements = m.listStatements(); statements.hasNext();) { Statement statement = statements.nextStatement(); RDFNode O = statement.getObject(); if (O.isURIResource()) { JSONLDComposer.Int count = refCount.get(O); if (count == null) refCount.put(O.asResource(), count = new Int()); count.inc(); } } }
/** * Return the lexical form of a node. This is the lexical form of a * literal, the URI of a URIResource or the annonID of a bNode. */ public static String getLexicalForm(RDFNode value) { if (value.isLiteral()) { return ((Literal)value).getLexicalForm(); } else if (value.isURIResource()) { return ((Resource)value).getURI(); } else { return value.toString(); } }
/** * Return the lexical form of a node. This is the lexical form of a * literal, the URI of a URIResource or the annonID of a bNode. */ public static String getLexicalForm(RDFNode value) { if (value.isLiteral()) { return ((Literal)value).getLexicalForm(); } else if (value.isURIResource()) { return ((Resource)value).getURI(); } else { return value.toString(); } }
protected String spelling( RDFNode n ) { if (n.isURIResource()) return resourceSpelling( (Resource) n ); if (n.isLiteral()) return ((Literal) n).getLexicalForm(); String id = ((Resource) n).getId().toString(); return id; }
public void toInteractions(ResultSet results) throws StudyImporterException { final InteractionListener listener = new InteractionListenerImpl(nodeFactory, getGeoNamesService(), getLogger()); while (results.hasNext()) { QuerySolution next = results.next(); Iterator<String> nameIter = next.varNames(); Map<String, String> props = new TreeMap<>(); while (nameIter.hasNext()) { String key = nameIter.next(); RDFNode rdfNode = next.get(key); if (rdfNode.isURIResource()) { props.put(key, next.getResource(key).getURI()); } else { props.put(key, next.getLiteral(key).getString()); } } props.put(StudyImporterForTSV.STUDY_SOURCE_CITATION, getDataset().getCitation()); listener.newLink(props); } }
private void add(Collection<Attribute> attributes, String attr_type, String prefix, String name, RDFNode node) throws StatefulIngestServiceException { String value = null; if (attr_type.equals(AttributeValueType.DATETIME)) { // Eventually could deal with the typed DateTime literal } if (node.isLiteral()) { value = node.asLiteral().getString(); } else if (node.isURIResource()) { value = node.asResource().getURI(); } else { throw new StatefulIngestServiceException("Expected resource to be literal or uri: " + node); } add(attributes, attr_type, prefix, name, value); }
/** * Extracts a List filled with the binding of selectElement variable for each * query solution, turned into a string (URIs or lexical forms). * Exhausts the result set. Create a rewindable one to use multiple times. * @see com.hp.hpl.jena.query.ResultSetFactory * */ public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) { List<String> items = new ArrayList<String>() ; while (rs.hasNext()) { QuerySolution qs = rs.nextSolution() ; RDFNode rn = qs.get(selectElement) ; if ( rn.isLiteral() ) items.add( ((Literal)rn).getLexicalForm() ) ; else if ( rn.isURIResource() ) items.add( ((Resource)rn).getURI() ) ; else if ( rn.isAnon() ) { items.add( ((Resource)rn).getId().getLabelString() ) ; } else throw new ARQException("Unknow thing in results : "+rn) ; } return items ; } }
/** * Extracts a List filled with the binding of selectElement variable for each * query solution, turned into a string (URIs or lexical forms). * Exhausts the result set. Create a rewindable one to use multiple times. * @see com.hp.hpl.jena.query.ResultSetFactory */ public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) { // feature suggested by James Howison List<String> items = new ArrayList<String>() ; while (rs.hasNext()) { QuerySolution qs = rs.nextSolution() ; RDFNode rn = qs.get(selectElement) ; if ( rn.isLiteral() ) items.add( ((Literal)rn).getLexicalForm() ) ; else if ( rn.isURIResource() ) items.add( ((Resource)rn).getURI() ) ; else if ( rn.isAnon() ) { items.add( ((Resource)rn).getId().getLabelString() ) ; } else throw new ARQException("Unknow thing in results : "+rn) ; } return items ; }
protected static void addSubclassesFrom( Model result, Model schema ) { for (StmtIterator it = schema.listStatements( ANY, RDFS.subClassOf, ANY ); it.hasNext();) { Statement s = it.nextStatement(); if (s.getSubject().isURIResource() && s.getObject().isURIResource()) result.add( s ); } }
private boolean getBoolean( Resource root, Property p, boolean ifAbsent ) { RDFNode r = getUnique( root, p ); return r == null ? ifAbsent : r.isLiteral() ? booleanSpelling( r.asNode().getLiteralLexicalForm() ) : r.isURIResource() ? booleanSpelling( r.asNode().getLocalName() ) : false ; }
/** * Test method for * {@link net.sf.taverna.t2.reference.sadi.StreamToRDFConverter#renderFrom(java.io.InputStream)} * . */ @Test public void testRenderFrom() { RDFNode rdfNode = streamToRDFConverter.renderFrom(new ByteArrayInputStream(rdfResourceString.getBytes())); assertTrue(rdfNode.isURIResource()); assertEquals("http://sadiframework.org/examples/input/erminej1", rdfNode.asNode().getURI()); rdfNode = streamToRDFConverter.renderFrom(new ByteArrayInputStream(rdfURIResourceString.getBytes())); assertTrue(rdfNode.isURIResource()); assertEquals("http://purl.uniprot.org/uniprot/P12345", rdfNode.asNode().getURI()); }
/** * Test method for * {@link net.sf.taverna.t2.reference.sadi.StreamToRDFConverter#convert(java.io.InputStream)} * . */ @Test public void testConvert() { RDFNode rdfNode = StreamToRDFConverter.convert(new ByteArrayInputStream(rdfResourceString.getBytes())); assertTrue(rdfNode.isURIResource()); assertEquals("http://sadiframework.org/examples/input/erminej1", rdfNode.asNode().getURI()); rdfNode = StreamToRDFConverter.convert(new ByteArrayInputStream(rdfURIResourceString.getBytes())); assertTrue(rdfNode.isURIResource()); assertEquals("http://purl.uniprot.org/uniprot/P12345", rdfNode.asNode().getURI()); }
/** * @param rs * The reified statement to look for object. * @param type * The type of the object. * @return The object (literal value, id or uri) or null if an error occurs */ protected String getReifiedStatementObject(final ReifiedStatement rs, final RDFNodeTypes type) { String toRet = null; final RDFNode obj = rs.getStatement().getObject(); if (type.equals(RDFNodeTypes.ANON_R) && obj.isAnon()) { toRet = ((Resource) obj).getId().getLabelString(); } else if (type.equals(RDFNodeTypes.LITERAL) && obj.isLiteral()) { toRet = ((Literal) obj).getLexicalForm(); } else if (type.equals(RDFNodeTypes.NAMED_R) && obj.isURIResource()) { toRet = ((Resource) obj).getURI(); } return toRet; }
private void populateBindingValue(BindingBuilder builder, RDFNode node) { LOGGER.trace("- Value {}",node); if(node.isLiteral()) { Literal literal=node.asLiteral(); builder. withValue( ProtocolFactory. newLiteral(). withLexicalForm(literal.getLexicalForm()). withDatatype(literal.getDatatypeURI()). withLanguage(literal.getLanguage())); } else if(node.isURIResource()) { builder.withValue(ProtocolFactory.newResource(node.asResource().getURI())); } else { builder.withValue(ProtocolFactory.newVariable(node.asResource().getId().getLabelString())); } }
/** For sorting values, every node should have a label. That's easy for literals and URI nodes, but bnode IDs are not stable, and for testing purposes if nothing else, we want the order of values to be the same every time. So for a bnode we give it the spelling of the lexical form of its literal label, if it has one. We try api:label then rdfs:label and otherwise fall back to the bnode ID and cross our fingers. */ private String objectTagOf( RDFNode r ) { if (r.isAnon()) { Statement labelling = r.asResource().getProperty( API.label ); if (labelling == null) labelling = r.asResource().getProperty( RDFS.label ); if (labelling != null) { RDFNode label = labelling.getObject(); if (label.isLiteral()) return label.asLiteral().getLexicalForm(); } return r.asNode().getBlankNodeLabel(); } // if (r.isURIResource()) { Resource r1 = (Resource) r; String shorter = nameMap.get( r1.getURI() ); return shorter == null ? r1.getURI() : shorter; } if (r.isLiteral()) { return ((Literal) r).getLexicalForm(); } throw new BrokenException("node " + r + " neither blank, literal, or URI."); }
if (type == null && value != null && value.getObject().isURIResource()) type = RDFS.Resource.getURI(); if (type == null) {