Java Code Examples for com.hp.hpl.jena.rdf.model.RDFNode#isLiteral()

The following examples show how to use com.hp.hpl.jena.rdf.model.RDFNode#isLiteral() . 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: SparqlQueryBase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
public DataEntry getDataEntryFromRS(ResultSet rs) {
	DataEntry dataEntry = new DataEntry();
	QuerySolution soln = rs.nextSolution();
	String colName, value;
	boolean useColumnNumbers = this.isUsingColumnNumbers();
	/* for each column get the colName and colValue and add to the data entry */
	for (int i = 0; i < rs.getResultVars().size(); i++) {
		colName = rs.getResultVars().get(i);
		RDFNode node = soln.get(colName) ;  			
		if (node.isLiteral()) {
			value = convertRSToString(soln, colName);
		} else {
			value = soln.getResource(colName).getURI();
		}			
		dataEntry.addValue(useColumnNumbers ? Integer.toString(i + 1) : 
			colName, new ParamValue(value));
	}
	return dataEntry;
}
 
Example 2
Source File: SPARQLQuery.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private DataEntry getDataEntryFromRS(ResultSet rs) {
	DataEntry dataEntry = new DataEntry();
	QuerySolution soln = rs.nextSolution();
	String colName, value;
	boolean useColumnNumbers = this.isUsingColumnNumbers();
	/* for each column get the colName and colValue and add to the data entry */
	for (int i = 0; i < rs.getResultVars().size(); i++) {
		colName = rs.getResultVars().get(i);
		RDFNode node = soln.get(colName) ;  			
		if (node.isLiteral()) {
			value = convertRSToString(soln, colName);
		} else {
			value = soln.getResource(colName).getURI();
		}			
		dataEntry.addValue(useColumnNumbers ? Integer.toString(i + 1) : 
			colName, new ParamValue(value));
	}
	return dataEntry;
}
 
Example 3
Source File: PrettyTurtleWriter.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
private String toTurtle(RDFNode r) {
	if (r.isURIResource()) {
		return PrettyPrinter.qNameOrURI(relativize(r.asResource().getURI()), prefixes);
	} else if (r.isLiteral()) {
		StringBuffer result = new StringBuffer(quote(r.asLiteral().getLexicalForm()));
		if (!"".equals(r.asLiteral().getLanguage())) {
			result.append("@");
			result.append(r.asLiteral().getLanguage());
		} else if (r.asLiteral().getDatatype() != null) {
			result.append("^^");
			result.append(toTurtle(ResourceFactory.createResource(r.asLiteral().getDatatypeURI())));
		}
		return result.toString();
	} else {
		if (!blankNodeMap.containsKey(r)) {
			blankNodeMap.put(r.asResource(), "_:b" + blankNodeCounter++);
		}
		return blankNodeMap.get(r);
	}
}
 
Example 4
Source File: RDFComparator.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
public int compare(RDFNode n1, RDFNode n2) {
	if (n1.isURIResource()) {
		if (!n2.isURIResource()) return -1;
		return n1.asResource().getURI().compareTo(n2.asResource().getURI());
	}
	if (n1.isAnon()) {
		if (n2.isURIResource()) return 1;
		if (n2.isLiteral()) return -1;
		return n1.asResource().getId().getLabelString().compareTo(n2.asResource().getId().getLabelString());
	}
	if (!n2.isLiteral()) return 1;
	int cmpLex = n1.asLiteral().getLexicalForm().compareTo(n2.asLiteral().getLexicalForm());
	if (cmpLex != 0) return cmpLex;
	if (n1.asLiteral().getDatatypeURI() == null) {
		if (n2.asLiteral().getDatatypeURI() != null) return -1;
		return n1.asLiteral().getLanguage().compareTo(n2.asLiteral().getLanguage());
	}
	if (n2.asLiteral().getDatatypeURI() == null) return 1;
	return n1.asLiteral().getDatatypeURI().compareTo(n2.asLiteral().getDatatypeURI()); 
}
 
Example 5
Source File: RDFReader.java    From marklogic-contentpump with Apache License 2.0 5 votes vote down vote up
private String object(RDFNode node) {
    if (node.isLiteral()) {
        Literal lit = node.asLiteral();
        String text = lit.getString();
        String lang = lit.getLanguage();
        String type = lit.getDatatypeURI();

        if (lang == null || "".equals(lang)) {
            lang = "";
        } else {
            lang = " xml:lang='" + escapeXml(lang) + "'";
        }

        if ("".equals(lang)) {
            if (type == null) {
                type = "http://www.w3.org/2001/XMLSchema#string";
            }
            type = " datatype='" + escapeXml(type) + "'";
        } else {
            type = "";
        }

        return "<sem:object" + type + lang + ">" + escapeXml(text) + "</sem:object>";
    } else if (node.isAnon()) {
        return "<sem:object>http://marklogic.com/semantics/blank/" + Long.toHexString(
                hash64(fuse(scramble(milliSecs),randomValue), node.toString()))
                +"</sem:object>";
    } else {
        return "<sem:object>" + escapeXml(node.toString()) + "</sem:object>";
    }
}
 
Example 6
Source File: R2RMLReader.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public boolean isTypeOf(RDFNode node) {
	if (!node.isLiteral()) return false;
	Literal l = node.asLiteral();
	return XSD.xstring.getURI().equals(l.getDatatypeURI())
			|| (l.getDatatypeURI() == null && "".equals(l.getLanguage()));
}
 
Example 7
Source File: R2RMLReader.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public boolean isTypeOf(RDFNode node) {
	return (node.isURIResource() || node.isLiteral());
}
 
Example 8
Source File: R2RMLReader.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public RDFNode coerce(RDFNode in) {
	return in.isLiteral() ? ResourceFactory.createResource() : in;
}
 
Example 9
Source File: DBPediaCandidateType.java    From uncc2014watsonsim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Find the possible lexical types of a name.
 * tag("New York") for example might be:
 *  {"populated place", "place", "municipality"}..
 */
public List<String> viaDBPedia(String text) {
	/*
	 * ABOUT THE QUERY
	 * ===============
	 * Most of these results are not really excellent.
	 * The recall is pretty high but the precision is low because it
	 * matches every name that _contains_ the candidate answer.
	 * 
	 * So, we should probably trim the results to the most popular names.
	 * 
	 * BUT many queries have thousands of names so this will probably be
	 * slow. Meaning we probably need to compromise or develop our own
	 * solution.
	 * 
	 * ABOUT THE RESULTS
	 * =================
	 * A lot of the results are generic, like "place". And "city" is also
	 * a place, so it may just be inadequately tagged. We probably need
	 * some graph algorithm to help with this.
	 * Some results have synonyms. "country" is a real tag, but "nation" is
	 * not. WordNet can help with this.
	 * 
	 */

	rdf.begin(ReadWrite.READ);
	List<String> types = new ArrayList<>();
	try (QueryExecution qe = QueryExecutionFactory.create(getQuery(text), 
			rdf.getDefaultModel())) {
		ResultSet rs = qe.execSelect();
		while (rs.hasNext()) {
			QuerySolution s = rs.next();
			RDFNode node = s.get("?kind");
			if (node == null) {}
			else if (node.isLiteral())
				types.add(node.asLiteral().getLexicalForm().toLowerCase());
			else if (node.isResource())
				types.add(node.asResource().getLocalName().toLowerCase());
		}
	} finally {
		rdf.end();
	}

	return types;
}