private static boolean matchNode(Item node, Item item, Match details) { if (isAny(item)) { details.anyMatches++; return true; } if (isAnyVar(item)) { details.varMatches++; return true; } if (node.isSymbol()) { // TERM in the thing to be matched means something concrete will be there. if (node.equals(TERM)) { if (item.equals(TERM)) { details.termMatches++; return true; } // Does not match LITERAL, URI, BNODE and VAR/ANY were done above. return false; } throw new ARQException("StatsMatcher: unexpected slot type: " + node); } if (!node.isNode()) return false; Node n = node.getNode(); if (n.isConcrete()) { if (item.isNode() && item.getNode().equals(n)) { details.exactMatches++; return true; } if (isAnyTerm(item)) { details.termMatches++; return true; } if (isAnyURI(item) && n.isURI()) { details.termMatches++; return true; } if (isAnyLiteral(item) && n.isLiteral()) { details.termMatches++; return true; } if (isAnyBNode(item) && n.isBlank()) { details.termMatches++; return true; } } return false; }
protected Object extractBinding(Map<String, Node> binds, int nodeIndex) { Node node = this.nodes[nodeIndex]; if (node.isVariable()) { Node boundNode = binds.get(this.nodes[nodeIndex].getName()); if (boundNode == null) { throw new UnsupportedOperationException("Unbound variable"); } node = boundNode; } if (node.isConcrete()) { Node_Concrete lit = (Node_Concrete) node; if (lit.isLiteral()) return lit.getLiteralValue(); else if (lit.isURI()) return lit.getURI(); else if (lit.isBlank()) return lit.getBlankNodeLabel(); } throw new UnsupportedOperationException("Incorrect node type for comparison: " + node); }
/** * 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; }