Java Code Examples for com.hp.hpl.jena.graph.Node#isURI()

The following examples show how to use com.hp.hpl.jena.graph.Node#isURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrettyPrinter.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
/**
 * Pretty-prints an RDF node and shortens URIs into QNames according to a
 * {@link PrefixMapping}.
 * @param n An RDF node
 * @return An N-Triples style textual representation with URIs shortened to QNames
 */
public static String toString(Node n, PrefixMapping prefixes) {
	if (n.isURI()) {
		return qNameOrURI(n.getURI(), prefixes);
	}
	if (n.isBlank()) {
		return "_:" + n.getBlankNodeLabel();
	}
	if (n.isVariable()) {
		return "?" + n.getName();
	}
	if (Node.ANY.equals(n)) {
		return "?ANY";
	}
	// Literal
	String s = "\"" + n.getLiteralLexicalForm() + "\"";
	if (!"".equals(n.getLiteralLanguage())) {
		s += "@" + n.getLiteralLanguage();
	}
	if (n.getLiteralDatatype() != null) {
		s += "^^" + qNameOrURI(n.getLiteralDatatypeURI(), prefixes);
	}
	return s;
}
 
Example 2
Source File: TypeConversion.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Transforms a Jena node into a java object. Possible node types: uri,
 * variable, literal (datatype, languageTag), blank node
 * 
 * @param n The node to transform
 * @return A specific java object
 * @throws ModelRuntimeException from the underlying model
 */
public static org.ontoware.rdf2go.model.node.Node toRDF2Go(Node n) throws ModelRuntimeException {
	// A return of null indicates that the variable is not present in this
	// solution.
	if(n == null)
		return null;
	
	if(n.isURI())
		return new URIImpl(n.getURI());
	
	if(n.isVariable())
		throw new RuntimeException("Cannot convert a Jena variable to an RDF2Go node");
	
	if(n.isLiteral()) {
		LiteralLabel lit = n.getLiteral();
		// datatype
		if(lit.getDatatypeURI() != null) {
			return new DatatypeLiteralImpl(lit.getLexicalForm(), new URIImpl(
			        lit.getDatatypeURI()));
		}
		
		// language tagged
		if(lit.language() != null && !lit.language().equals(""))
			return new LanguageTagLiteralImpl(lit.getLexicalForm(), lit.language());
		
		// plain
		return new PlainLiteralImpl(lit.getLexicalForm());
	}
	
	if(n.isBlank())
		return new JenaBlankNode(n);
	
	// none of the above - don't know how to transform that
	throw new RuntimeException("no transformation defined from " + n + " to java");
}
 
Example 3
Source File: NTriples.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link String} representation of the given {@link Node}.
 * 
 * @param node the node.
 * @return a {@link String} representation of the given {@link Node}.
 */
public static String asNt(final Node node) {
	if (node.isURI()) {
		return asNtURI(node);
	} else if (node.isBlank()) {
		return asNtBlankNode(node);		
	} else if (node.isLiteral()) {
		return asNtLiteral(node);
	}
	throw new IllegalArgumentException(node.getClass().getName());
}
 
Example 4
Source File: NeoGraph.java    From neo4jena with Apache License 2.0 4 votes vote down vote up
/**
 * Find the given triple(s) from the graph. 
 */
@Override
public ExtendedIterator<Triple> find(Node subject, Node predicate, Node object) {
	//System.out.println("NeoGraph#find");
	try(Transaction tx = graphdb.beginTx()) {
		StringBuffer query = new StringBuffer("MATCH triple=");
		
		query.append("(subject");
		if(subject.equals(Node.ANY)) {
			query.append(":"+ LABEL_URI);
			//System.out.println("NeoGraph#find#Any:"+subject);
		} else if(subject.isURI()){
			query.append(":"+LABEL_URI+" {uri:'");
			query.append(subject.getURI());
			query.append("'}");
			//System.out.println("NeoGraph#find#URI:"+subject+subject.getURI());
		} else {
			query.append(":" + LABEL_BNODE);
			//System.out.println("NeoGraph#find#"+subject);
		}
		query.append(")-[predicate]->(object");
		
		//query.append("]->(object");
		if(object.equals(Node.ANY)) {
			//query.append(" ");
		} else if(object.isURI()){
			query.append(":"+LABEL_URI+" {uri:'");
			query.append(object.getURI());
			query.append("'}");
		} else {
			query.append(":"+LABEL_LITERAL+" {value:'");
			query.append(object.getLiteralValue());
			query.append("'}");
		}
		query.append(")");
		
		//System.out.println("Predicate in query: " +getPrefixMapping().shortForm(predicate.getURI()));
		if(predicate.isURI()) {
			query.append("WHERE type(predicate)='");
			query.append(getPrefixMapping().shortForm(predicate.getURI()));
			query.append("'");
		}
		
		query.append("\nRETURN subject, type(predicate), object");
		//System.out.println(query.toString());
		ExecutionEngine engine = new ExecutionEngine(graphdb);
		ExecutionResult results = engine.execute(query.toString());
		//System.out.println(results.dumpToString());
		//System.out.println("NeoGraph#find#"+predicate);
		//System.out.println("NeoGraph#find#"+object);
		//System.out.println("NeoGraph#find#DONE");
		return new ExecutionResultIterator(results, graphdb);
	}
}
 
Example 5
Source File: SPARQLEndPoint.java    From LodView with MIT License 4 votes vote down vote up
private List<TripleBean> moreThenOneQuery(QueryExecution qe, List<TripleBean> results, int retry, String overrideProperty) throws Exception {

		try {
			ResultSet rs = qe.execSelect();
			while (rs.hasNext()) {
				TripleBean rb = new TripleBean();
				QuerySolution qs = rs.next();
				String property = "";
				if (overrideProperty != null) {
					property = overrideProperty;
				} else if (qs.get("p") != null) {
					property = qs.get("p").asNode().toString();
				}

				try {
					if (qs.get("s") != null && !qs.get("s").asNode().toString().startsWith("http://")) { // probably
																											// a
																											// bn
						rb.setIRI(qs.get("s").asNode().toString());
						rb.setNsIRI("_:" + rb.getIRI());
					} else if (qs.get("s") != null && qs.get("s").asNode().toString().startsWith("http://")) {
						rb.setIRI(qs.get("s").asNode().toString());
						rb.setNsIRI(Misc.toNsResource(rb.getIRI(), conf));
						rb.setUrl(Misc.toBrowsableUrl(rb.getIRI(), conf));
					}

					PropertyBean p = new PropertyBean();
					p.setNsProperty(Misc.toNsResource(property, conf));
					p.setProperty(property);
					if (ontoBean != null) {
						p.setLabel(ontoBean.getEscapedValue("label", locale, property));
						p.setComment(ontoBean.getEscapedValue("comment", locale, property));
					}
					p.setPropertyUrl(Misc.toBrowsableUrl(property, conf));
					rb.setProperty(p);
					if (qs.get("o") != null) {
						Node object = qs.get("o").asNode();
						if (object.isURI()) {
							rb.setType("iri");
							rb.setValue(object.toString(false));
						} else if (object.isLiteral()) {
							rb.setType("literal");
							rb.setDataType(object.getLiteralDatatypeURI());
							rb.setNsDataType(Misc.toNsResource(object.getLiteralDatatypeURI(), conf));
							rb.setLang(object.getLiteralLanguage());
							rb.setValue(object.getLiteralLexicalForm());
						} else if (object.isBlank()) {
							rb.setType("bnode");
							rb.setValue(object.toString(false));
						}
					} else {
						rb.setType("literal");
						rb.setValue("");
					}
					results.add(rb);
				} catch (Exception e) {
					System.err.println("error? " + e.getMessage());
					// e.printStackTrace();
				}
			}
		} catch (Exception ez) {
			if (retry < 3) {
				retry++;
				// System.out.println("query failed (" + ez.getMessage() +
				// "), I'm giving another chance (" + retry + "/3)");
				return moreThenOneQuery(qe, results, retry, overrideProperty);
			}
			ez.printStackTrace();
			throw new Exception("connection refused");
		}

		return results;
	}
 
Example 6
Source File: TypedNodeMaker.java    From GeoTriples with Apache License 2.0 votes vote down vote up
public boolean matches(Node node) { return node.isURI(); }