/** * This method is invoked when the builtin is called in a rule body. * * @param args the array of argument values for the builtin, this is an array of Nodes, some of * which may be Node_RuleVariables. * @param length the length of the argument list, may be less than the length of the args array * for some rule engines * @param context an execution context giving access to other relevant data * @return return true if the buildin predicate is deemed to have succeeded in the current * environment */ public boolean bodyCall(Node[] args, int length, RuleContext context) { if (length != 3) { throw new BuiltinException( this, context, "builtin " + getName() + " requires 3 arguments but saw " + length); } Node obj = getArg(2, args, context); Node subj = getArg(0, args, context); // Allow variables in subject position to correspond to a wild card if (subj.isVariable()) { subj = null; } // Allow variables in the predicate position to correspond to a wild card Node pred = getArg(1, args, context); if (pred.isVariable()) { pred = null; } boolean bContainsObj = context.contains(subj, pred, obj); if (!bContainsObj) { return false; } // does it contain anything else? ClosableIterator<Triple> citr = context.find((Node) null, pred, obj); boolean otherSubject = false; while (citr.hasNext()) { Object o = citr.next(); Node tmpSubj = ((Triple) o).getSubject(); if (!tmpSubj.equals(subj)) { otherSubject = true; break; } } citr.close(); return !otherSubject; }
/** Encoding of a node so it can be reconstructed */ public static String serialize(Node n, String base, PrefixMap prefixMap) { // See also Nodec. // See also OutputLangUtils - merge and this is a buffering call. if (n == null) return "<<null>>"; if (n.isBlank()) { String str = n.getBlankNodeLabel(); // c.f. OutputLangUtils if (onlySafeBNodeLabels) str = safeBNodeLabel(str); return "_:" + str; } if (n.isLiteral()) return FmtUtils.stringForLiteral((Node_Literal) n, null); if (n.isURI()) { String uri = n.getURI(); return stringForURI(uri, base, prefixMap); } // Safe name? if (n.isVariable()) return "?" + n.getName(); // // if ( n.equals(Node.ANY) ) // return "ANY" ; throw new TDBException("Failed to turn a node into a string: " + n); // return null ; }
/** * Return an equivalent triple pattern with standardized variable names (?s, ?p, ?o). This * method is needed to ensure that functionally equivalent triple patterns do not get assigned * to different entries in the cache. * * @param triplePattern * @return a Triple that is equivalent to triplePattern, but with standard variable names */ protected Triple standardizeVarNames(Triple triplePattern) { Node s = triplePattern.getSubject(); Node p = triplePattern.getPredicate(); Node o = triplePattern.getObject(); // make the key consistent, regardless of user's chosen variable names if (s.isVariable()) { s = NodeCreateUtils.create("?s"); } if (p.isVariable()) { p = NodeCreateUtils.create("?p"); } if (o.isVariable()) { o = NodeCreateUtils.create("?o"); } return new Triple(s, p, o); }
protected ElementGroup getWhereClauseWithBlankNodeFilter(Triple triplePattern) { Node s = triplePattern.getSubject(); Node o = triplePattern.getObject(); ElementGroup whereClause = new ElementGroup(); whereClause.addTriplePattern(triplePattern); if (s.isVariable()) { whereClause.addElementFilter( new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(s))))); } if (o.isVariable()) { whereClause.addElementFilter( new ElementFilter(new E_LogicalNot(new E_IsBlank(new ExprVar(o))))); } return whereClause; }
private SWRLIndividualObject makeIndividalObject(final Node node) throws URISyntaxException { if (node.isVariable()) { final SWRLIndividualVariable var = swrlFactory.createIndividualVariable(new URI(varNS + node.getName())); vars.put(node.toString(), var); return var; } final OWLIndividual ind = owlModel.createIndividual(null, new URI(node.getURI())); return swrlFactory.wrapIndividual(ind); }
/** * this nodeToElement is pretty much identical to that of * graph.query.patternstagecompiler.compile. */ private Element nodeToElement(Node X, Mapping map) { if (X.equals(Query.ANY)) return Element.ANY; if (X.isVariable()) { if (map.hasBound(X)) return new Bound(map.indexOf(X)); else { freeVarCnt++; return new Free(X); } } return new Fixed(X); }
// Replaced by getVarsMentioned @Deprecated public static Set<Node> getVariables(Iterable<Node> nodes) { Set<Node> result = new HashSet<Node>(); for (Node node : nodes) { if (node.isVariable()) { result.add(node); } } return result; }
private SWRLDataObject makeDataObject(final Node node) throws URISyntaxException { if (node.isVariable()) { final SWRLDataVariable var = swrlFactory.createDataVariable(new URI(varNS + node.getName())); vars.put(node.toString(), var); return var; } final OWLDataValue value = new OWLDataValueImpl(new LiteralImpl(node, (EnhGraph) owlModel.getImplementation())); return swrlFactory.wrapDataValue(value); }
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); }
protected static String sparqlNode(Node node, String varName) { if (node == null || node.isVariable()) { return varName; } else if (node.isBlank()) { return "<fake:blank>"; // or throw exception? } else if (node.isURI()) { StringBuffer uriBuff = new StringBuffer(); return uriBuff.append("<").append(node.getURI()).append(">").toString(); } else if (node.isLiteral()) { StringBuffer literalBuff = new StringBuffer(); literalBuff.append("\""); pyString(literalBuff, node.getLiteralLexicalForm()); literalBuff.append("\""); if (node.getLiteralDatatypeURI() != null) { literalBuff.append("^^<").append(node.getLiteralDatatypeURI()).append(">"); } else if (node.getLiteralLanguage() != null && node.getLiteralLanguage().length() > 0) { literalBuff.append("@").append(node.getLiteralLanguage()); } return literalBuff.toString(); } else { return varName; } }
public static void addVar(Collection<Var> acc, Node n) { if (n == null) return; if (n.isVariable()) acc.add(Var.alloc(n)); }
public void addIfVariable(Node possibleVariable, NodeMaker nodeMaker, AliasMap aliases) { if (!possibleVariable.isVariable()) return; add(possibleVariable.getName(), nodeMaker, aliases); }
/** * 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; }
public List<AlphaResultUnion> calculateAlphaSTG( Collection<Triple> triples, AbstractConceptMapping cm) throws Exception { List<AlphaResultUnion> alphaResultUnionList = new Vector<AlphaResultUnion>(); Triple firstTriple = triples.iterator().next(); Node tpSubject = firstTriple.getSubject(); SQLLogicalTable alphaSubject = this.calculateAlphaSubject(tpSubject, cm); // String logicalTableAlias = cm.getLogicalTableAlias(); String logicalTableAlias = alphaSubject.getAlias(); // if(logicalTableAlias == null || logicalTableAlias.equals("")) { // cm.setLogicalTableAlias(alphaSubject.getAlias()); // } // mapping projection of corresponding predicates for (Triple tp : triples) { Node tpPredicate = tp.getPredicate(); List<SQLJoinTable> alphaPredicateObjects = new Vector<SQLJoinTable>(); List<SQLLogicalTable> alphaPredicateObjects2 = new Vector<SQLLogicalTable>(); if (tpPredicate.isURI()) { String tpPredicateURI = tpPredicate.getURI(); Collection<String> mappedClassURIs = cm.getMappedClassURIs(); boolean processableTriplePattern = true; if (tp.getObject().isURI()) { String objectURI = tp.getObject().getURI(); if (RDF.type.getURI().equals(tpPredicateURI) && mappedClassURIs.contains(objectURI)) { processableTriplePattern = false; } } if (processableTriplePattern) { List<SQLJoinTable> alphaPredicateObjectAux = calculateAlphaPredicateObjectSTG(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux != null) { alphaPredicateObjects.addAll(alphaPredicateObjectAux); } List<SQLLogicalTable> alphaPredicateObjectAux2 = calculateAlphaPredicateObjectSTG2(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux2 != null) { alphaPredicateObjects2.addAll(alphaPredicateObjectAux2); } AlphaResult alphaResult = new AlphaResult(alphaSubject, alphaPredicateObjects, tpPredicateURI); // alphaResult.setAlphaPredicateObjects2(alphaPredicateObjectAux2); AlphaResultUnion alphaTP = new AlphaResultUnion(alphaResult); alphaResultUnionList.add(alphaTP); } } else if (tpPredicate.isVariable()) { Collection<AbstractPropertyMapping> pms = cm.getPropertyMappings(); AlphaResultUnion alphaTP = new AlphaResultUnion(); for (AbstractPropertyMapping pm : pms) { String tpPredicateURI = pm.getMappedPredicateName(); List<SQLJoinTable> alphaPredicateObjectAux = calculateAlphaPredicateObjectSTG(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux != null) { alphaPredicateObjects.addAll(alphaPredicateObjectAux); } List<SQLLogicalTable> alphaPredicateObjectAux2 = calculateAlphaPredicateObjectSTG2(tp, cm, tpPredicateURI, logicalTableAlias); if (alphaPredicateObjectAux2 != null) { alphaPredicateObjects2.addAll(alphaPredicateObjectAux2); } AlphaResult alphaResult = new AlphaResult(alphaSubject, alphaPredicateObjects, tpPredicateURI); // alphaResult.setAlphaPredicateObjects2(alphaPredicateObjectAux2); alphaTP.add(alphaResult); } if (alphaTP != null) { alphaResultUnionList.add(alphaTP); } } else { String errorMessage = "Predicate has to be either an URI or a variable"; throw new QueryTranslationException(errorMessage); } } return alphaResultUnionList; }
private boolean isVar(Node node) { return (node == null || node.isVariable() || node == Node.ANY); }
private static Node generateCountableNode(int count, Node node1, Node node2) { if (node2 == node1 && node2.isVariable()) { node2 = Var.alloc(node2.getName() + "_" + count); } return node2; }