use of org.apache.jena.graph.Node in project jena by apache.
the class TextIndexLucene method query$.
private List<TextHit> query$(IndexReader indexReader, Node property, String qs, String graphURI, String lang, int limit) throws ParseException, IOException {
String textField = docDef.getField(property);
String textClause;
String langClause = null;
String graphClause = null;
//for language-based search extension
if (getDocDef().getLangField() != null) {
String langField = getDocDef().getLangField();
if (lang != null) {
if (this.isMultilingual && !lang.equals("none")) {
textField = textField + "_" + lang;
}
langClause = !"none".equals(lang) ? langField + ":" + lang : "-" + langField + ":*";
}
}
if (textField != null)
textClause = textField + ":" + qs;
else
textClause = qs;
if (graphURI != null) {
String escaped = QueryParserBase.escape(graphURI);
graphClause = getDocDef().getGraphField() + ":" + escaped;
}
String queryString = textClause;
if (langClause != null)
queryString = "(" + queryString + ") AND " + langClause;
if (graphClause != null)
queryString = "(" + queryString + ") AND " + graphClause;
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Query query = parseQuery(queryString, queryAnalyzer);
if (limit <= 0)
limit = MAX_N;
ScoreDoc[] sDocs = indexSearcher.search(query, limit).scoreDocs;
List<TextHit> results = new ArrayList<>();
for (ScoreDoc sd : sDocs) {
Document doc = indexSearcher.doc(sd.doc);
String[] values = doc.getValues(docDef.getEntityField());
Node literal = null;
String field = (property != null) ? docDef.getField(property) : docDef.getPrimaryField();
String[] lexicals = doc.getValues(field);
if (lexicals.length > 0) {
String lexical = lexicals[0];
String[] langs = doc.getValues(docDef.getLangField());
if (langs.length > 0) {
String doclang = langs[0];
if (doclang.startsWith(DATATYPE_PREFIX)) {
String datatype = doclang.substring(DATATYPE_PREFIX.length());
TypeMapper tmap = TypeMapper.getInstance();
literal = NodeFactory.createLiteral(lexical, tmap.getSafeTypeByName(datatype));
} else {
literal = NodeFactory.createLiteral(lexical, doclang);
}
} else {
literal = NodeFactory.createLiteral(lexical);
}
}
for (String v : values) {
Node n = TextQueryFuncs.stringToNode(v);
TextHit hit = new TextHit(n, sd.score, literal);
results.add(hit);
}
}
return results;
}
use of org.apache.jena.graph.Node in project jena by apache.
the class ParserProfileStd method create.
@Override
public Node create(Node currentGraph, Token token) {
// Dispatches to the underlying ParserFactory operation
long line = token.getLine();
long col = token.getColumn();
String str = token.getImage();
switch(token.getType()) {
case BNODE:
return createBlankNode(currentGraph, str, line, col);
case IRI:
return createURI(str, line, col);
case PREFIXED_NAME:
{
String prefix = str;
String suffix = token.getImage2();
String expansion = expandPrefixedName(prefix, suffix, token);
return createURI(expansion, line, col);
}
case DECIMAL:
return createTypedLiteral(str, XSDDatatype.XSDdecimal, line, col);
case DOUBLE:
return createTypedLiteral(str, XSDDatatype.XSDdouble, line, col);
case INTEGER:
return createTypedLiteral(str, XSDDatatype.XSDinteger, line, col);
case LITERAL_DT:
{
Token tokenDT = token.getSubToken2();
String uriStr;
switch(tokenDT.getType()) {
case IRI:
uriStr = tokenDT.getImage();
break;
case PREFIXED_NAME:
{
String prefix = tokenDT.getImage();
String suffix = tokenDT.getImage2();
uriStr = expandPrefixedName(prefix, suffix, tokenDT);
break;
}
default:
throw new RiotException("Expected IRI for datatype: " + token);
}
uriStr = resolveIRI(uriStr, tokenDT.getLine(), tokenDT.getColumn());
RDFDatatype dt = NodeFactory.getType(uriStr);
return createTypedLiteral(str, dt, line, col);
}
case LITERAL_LANG:
return createLangLiteral(str, token.getImage2(), line, col);
case STRING:
return createStringLiteral(str, line, col);
default:
{
Node x = createNodeFromToken(currentGraph, token, line, col);
if (x != null)
return x;
errorHandler.fatal("Not a valid token for an RDF term: " + token, line, col);
return null;
}
}
}
use of org.apache.jena.graph.Node in project jena by apache.
the class InferenceProcessorRDFS method range.
/*
* [rdfs3: (?p rdfs:range ?c) -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]
* [rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]
*/
private final void range(Node s, Node p, Node o) {
// Mask out literal subjects
if (o.isLiteral())
return;
// Range
List<Node> x = state.rangeList.get(p);
if (x != null) {
for (Node c : x) {
derive(o, rdfType, c);
subClass(o, rdfType, c);
}
}
}
use of org.apache.jena.graph.Node in project jena by apache.
the class InferenceProcessorRDFS method domain.
/*
* [rdfs2: (?p rdfs:domain ?c) -> [(?x rdf:type ?c) <- (?x ?p ?y)] ]
* [rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]
*/
private final void domain(Node s, Node p, Node o) {
List<Node> x = state.domainList.get(p);
if (x != null) {
for (Node c : x) {
derive(s, rdfType, c);
subClass(s, rdfType, c);
}
}
}
use of org.apache.jena.graph.Node in project jena by apache.
the class InferenceSetupRDFS method exec.
private static void exec(String qs, Model model, Map<Node, List<Node>> multimap) {
String preamble = StrUtils.strjoinNL("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>", "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>", "PREFIX owl: <http://www.w3.org/2002/07/owl#>", "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>");
Query query = QueryFactory.create(preamble + "\n" + qs, Syntax.syntaxARQ);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet rs = qexec.execSelect();
for (; rs.hasNext(); ) {
QuerySolution soln = rs.next();
Node x = soln.get("x").asNode();
Node y = soln.get("y").asNode();
if (!multimap.containsKey(x))
multimap.put(x, new ArrayList<Node>());
multimap.get(x).add(y);
}
}
Aggregations