private void writeNode(RDFNode node) throws IOException { Node n = node.asNode(); if (n.isURI()) { write(" <uri>" + escape(n.getURI()) + "</uri>\n"); return; } if (n.isBlank()) { write(" <id>" + escape(n.getBlankNodeId().toString()) + "</id>\n"); return; } if (!n.isLiteral()) { throw new JenaException("Don't know how to serialize node " + n); } if (n.getLiteral().getDatatypeURI() != null) { write( " <typedLiteral datatype=\"" + escape(n.getLiteral().getDatatypeURI()) + "\">" + escape(n.getLiteral().getLexicalForm()) + "</typedLiteral>\n"); return; } if (n.getLiteral().language() == null || "".equals(n.getLiteral().language())) { write(" <plainLiteral>" + escape(n.getLiteral().getLexicalForm()) + "</plainLiteral>\n"); return; } write( " <plainLiteral xml:lang=\"" + n.getLiteral().language() + "\">" + escape(n.getLiteral().getLexicalForm()) + "</plainLiteral>\n"); }
/* * See http://www.w3.org/TR/rdf-sparql-query paragraph 11.4.4 * * "(STR) Returns the lexical form of a literal; returns the codepoint representation of an IRI." * * @see http://www.w3.org/TR/rdf-sparql-query */ private void convertStr(E_Str expr) { logger.debug("convertStr " + expr.toString()); expr.getArg().visit(this); Expression arg = expression.pop(); if (arg instanceof AttributeExprEx) { // make a new AttributeExprEx with changed NodeMaker, which returns plain literal // TODO this seems to work, but needs more testing. AttributeExprEx attribute = (AttributeExprEx) arg; TypedNodeMaker nodeMaker = (TypedNodeMaker) attribute.getNodeMaker(); TypedNodeMaker newNodeMaker = new TypedNodeMaker( TypedNodeMaker.PLAIN_LITERAL, nodeMaker.valueMaker(), nodeMaker.isUnique()); logger.debug("changing nodemaker " + nodeMaker + " to " + newNodeMaker); expression.push( new AttributeExprEx((Attribute) attribute.attributes().iterator().next(), newNodeMaker)); } else if (arg instanceof ConstantEx) { ConstantEx constant = (ConstantEx) arg; Node node = constant.getNode(); String lexicalForm = node.getLiteral().getLexicalForm(); node = Node.createLiteral(lexicalForm); ConstantEx constantEx = new ConstantEx(NodeValue.makeNode(node).asString(), node); logger.debug("pushing " + constantEx); expression.push(constantEx); } else { conversionFailed(expr); } }
public static String str(Node node) { if (node.isLiteral()) return node.getLiteral().getLexicalForm(); if (node.isURI()) return node.getURI(); // if ( node.isBlank() ) return node.getBlankNodeId().getLabelString() ; // if ( node.isBlank() ) return "" ; if (node.isBlank()) NodeValue.raise(new ExprTypeException("Blank node: " + node)); NodeValue.raise(new ExprEvalException("Not a string: " + node)); return "[undef]"; }
/** * Gets NXRelations node instance given Jena node. * * @param jenaNodeInst * @return NXRelations node instance */ private Node getNXRelationsNode(com.hp.hpl.jena.graph.Node jenaNodeInst) { if (jenaNodeInst == null) { return null; } Node nuxNode = null; if (jenaNodeInst.isBlank()) { AnonId anonId = jenaNodeInst.getBlankNodeId(); String id = anonId.getLabelString(); nuxNode = NodeFactory.createBlank(id); } else if (jenaNodeInst.isLiteral()) { LiteralLabel label = jenaNodeInst.getLiteral(); String value = label.getLexicalForm(); String type = jenaNodeInst.getLiteralDatatypeURI(); String language = jenaNodeInst.getLiteralLanguage(); if (type != "") { nuxNode = NodeFactory.createTypedLiteral(value, type); } else if (language != "") { nuxNode = NodeFactory.createLiteral(value, language); } else { nuxNode = NodeFactory.createLiteral(value); } } else if (jenaNodeInst.isURI()) { String uri = jenaNodeInst.getURI(); // try to find corresponding prefix // TODO AT: maybe take namespaces from relation service? for (Map.Entry<String, String> ns : namespaces.entrySet()) { String base = ns.getValue(); if (uri.startsWith(base)) { String localName = uri.substring(base.length()); nuxNode = NodeFactory.createQNameResource(base, localName); break; } } if (nuxNode == null) { // default to resource nuxNode = NodeFactory.createResource(uri); } } else { throw new IllegalArgumentException( "Cannot translate non concrete Jena node into NXRelations node"); } return nuxNode; }
/** * Builds up statements like [] rr:template "http://data.example.com/department/{DEPTNO}" or [] * rr:class ex:Department and returns them as a Statement List. * * @param r2rml the target com.hp.hpl.jena.rdf.model.Model * @param mappingData the target that should be mapped to relational structures (subject, * predicate or object) * @param varDefs the construction definition of the target value based in the actual database * value and some additional data like prefix strings or certain functions like uri( ... ) * @return a List<Statement> containing all the subject map statements */ private List<Statement> buildMapStatements(Model r2rml, Node mappingData, VarDefinition varDefs) { List<Statement> results = new ArrayList<Statement>(); // a blank node [] Resource mapSubject = ResourceFactory.createResource(); // rr:template or rr:column or rr:constant Property mapPredicate; // a literal like "http://data.example.com/department/{DEPTNO}" or // simply "DEPTNO" (column name) or a constant "Foo bar!!" // (or in rare cases a URI, which is handled separately) Literal mapObject; // template or column or constant if (mappingData.isVariable()) { Collection<RestrictedExpr> restrictions = varDefs.getDefinitions((Var) mappingData); List<PredicateAndObject> mapPredicateAndObjects = processRestrictions(restrictions); for (PredicateAndObject result : mapPredicateAndObjects) { mapPredicate = result.getPrediacte(); Statement resultStatement; RDFNode rawObject = result.getRawObject(); // object is literal if (rawObject.isLiteral()) { mapObject = rawObject.asLiteral(); resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject); // object is blank node } else if (rawObject.isAnon()) { Resource mapResObject = rawObject.asResource(); resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapResObject); // object is resource } else { Resource mapResObject = rawObject.asResource(); resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapResObject); } results.add(resultStatement); } // everything that is not a variable is handled as a constant } else if (mappingData.isConcrete()) { // URIs and Literals have to be handled separately since the methods // to retrieve the respective value are different Statement resultStatement; // URI if (mappingData.isURI()) { /* * This case is somewhat special since the mapObject is not a * Literal. So, this needs some special handling: * - the Literal mapObject will not be used * - a special mapObject_uri Resource will be created * - the result will be created, appended to the List and * returned to not go through any further ordinary processing */ Resource mapObject_uri = ResourceFactory.createResource(mappingData.getURI()); mapPredicate = ResourceFactory.createProperty(rrNamespace, "constant"); resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject_uri); results.add(resultStatement); return results; // Literal } else if (mappingData.isLiteral()) { mapObject = ResourceFactory.createPlainLiteral(mappingData.getLiteral().toString(false)); // else (e.g. blank node) } else { // mapSubject.isBlank() == true /* * Hmm... I think this violates the standard. So lean back and * enjoy the trace... */ mapObject = null; } mapPredicate = ResourceFactory.createProperty(rrPrefix, "constant"); resultStatement = r2rml.createStatement(mapSubject, mapPredicate, mapObject); results.add(resultStatement); } return results; }
/** Deconstruct the node or list object argument and make a SpatialMatch */ @Override protected SpatialMatch objectToStruct(PropFuncArg argObject) { if (argObject.isNode()) { log.warn("Object not a List: " + argObject); return null; } List<Node> list = argObject.getArgList(); if (list.size() < 4 || list.size() > 5) throw new SpatialIndexException("Change in object list size"); int idx = 0; Node x = list.get(idx); if (!x.isLiteral()) { log.warn("Latitude 1 is not a literal " + list); return null; } if (!SpatialValueUtil.isDecimal(x.getLiteral())) { log.warn("Latitude 1 is not a decimal " + list); return null; } Double latitude1 = Double.parseDouble(x.getLiteralLexicalForm()); idx++; x = list.get(idx); if (!x.isLiteral()) { log.warn("Longitude 1 is not a literal " + list); return null; } if (!SpatialValueUtil.isDecimal(x.getLiteral())) { log.warn("Longitude 1 is not a decimal " + list); return null; } Double longtitude1 = Double.parseDouble(x.getLiteralLexicalForm()); idx++; x = list.get(idx); if (!x.isLiteral()) { log.warn("Latitude 2 is not a literal " + list); return null; } if (!SpatialValueUtil.isDecimal(x.getLiteral())) { log.warn("Latitude 2 is not a decimal " + list); return null; } Double latitude2 = Double.parseDouble(x.getLiteralLexicalForm()); idx++; x = list.get(idx); if (!x.isLiteral()) { log.warn("Longitude 2 is not a literal " + list); return null; } if (!SpatialValueUtil.isDecimal(x.getLiteral())) { log.warn("Longitude 2 is not a decimal " + list); return null; } Double longtitude2 = Double.parseDouble(x.getLiteralLexicalForm()); idx++; int limit = -1; if (idx < list.size()) { x = list.get(idx); if (!x.isLiteral()) { log.warn("Limit is not a literal " + list); return null; } LiteralLabel lit = x.getLiteral(); if (!XSDDatatype.XSDinteger.isValidLiteral(lit)) { log.warn("Limit is not an integer " + list); return null; } int v = NodeFactoryExtra.nodeToInt(x); limit = (v < 0) ? -1 : v; idx++; if (idx < list.size()) { log.warn("Limit is not the last parameter " + list); return null; } } SpatialMatch match = new SpatialMatch( latitude1, longtitude1, latitude2, longtitude2, limit, getSpatialOperation()); if (log.isDebugEnabled()) log.debug("Trying SpatialMatch: " + match.toString()); return match; }