/** * Bind the ith variable in the current envionment to the given value. * Checks that the new binding is compatible with any current binding. * @return false if the binding fails */ public boolean bind(int i, Node value) { Node node = environment[i]; if (node == null) { environment[i] = value; return true; } else { return node.sameValueAs(value); } }
/** Equality override */ @Override public boolean equals(Object o) { // Pass 1 - just check basic shape if (! (o instanceof BindingVector) ) return false; Node[] other = ((BindingVector)o).environment; if (environment.length != other.length) return false; for (int i = 0; i < environment.length; i++) { Node n = environment[i]; Node no = other[i]; if (n == null) { if (no != null) return false; } else { if (! n.sameValueAs(no)) return false; } } return true; }
/** * Equality is based on structural comparison */ @Override public boolean equals(Object obj) { if (obj instanceof Functor) { Functor f2 = (Functor)obj; if (name.equals(f2.name) && args.length == f2.args.length) { for (int i = 0; i < args.length; i++) { if (!args[i].sameValueAs(f2.args[i])) return false; } return true; } } return false; }
/** * Test if one node is a variant of another give a table of variable matches. */ private boolean variantOf(Node n, Node p, Map<Node, Node> vmap) { if (n instanceof Node_RuleVariable) { if (p instanceof Node_RuleVariable) { Object nMatch = vmap.get(n); if (nMatch == null) { // First match of these pairs vmap.put(n, p); return true; } else { return nMatch == p; } } else { return false; } } else { return n.sameValueAs(p); } }
private static QueryIterator groundedPath(Binding binding, Graph graph, Node subject, Path path, Node object, ExecutionContext execCxt) { Iterator<Node> iter = PathEval.eval(graph, subject, path) ; // Now count the number of matches. int count = 0 ; for ( ; iter.hasNext() ; ) { Node n = iter.next() ; if ( n.sameValueAs(object) ) count++ ; } return new QueryIterYieldN(count, binding) ; }
private static QueryIterator groundedPath(Binding binding, Graph graph, Node subject, Path path, Node object, ExecutionContext execCxt) { Iterator<Node> iter = PathEval.eval(graph, subject, path) ; // Now count the number of matches. int count = 0 ; for ( ; iter.hasNext() ; ) { Node n = iter.next() ; if ( n.sameValueAs(object) ) count++ ; } return new QueryIterYieldN(count, binding) ; }
/** * Test that two literals are semantically equivalent. * In some cases this may be the sames as equals, in others * equals is stricter. For example, two xsd:int literals with * the same value but different language tag are semantically * equivalent but distinguished by the java equality function * in order to support round tripping. */ @Override public boolean sameValueAs(Literal other) { return asNode().sameValueAs(other.asNode()); }
/** * Compare two nodes, taking into account variable indices. */ public static boolean sameNodeAs(Node n, Node m) { if (n instanceof Node_RuleVariable) { if (m instanceof Node_RuleVariable) { return ((Node_RuleVariable)n).getIndex() == ((Node_RuleVariable)m).getIndex(); } else { return false; } } else { return n.sameValueAs(m); } }
/** * Bind a variable in the current envionment to the given value. * Checks that the new binding is compatible with any current binding. * @param var a Node_RuleVariable defining the variable to bind * @param value the value to bind * @return false if the binding fails */ @Override public boolean bind(Node var, Node value) { if (var instanceof Node_RuleVariable) { return bind(((Node_RuleVariable)var).getIndex(), value); } else { return var.sameValueAs(value); } }
/** * Bind a variable in the current envionment to the given value. * Checks that the new binding is compatible with any current binding. * @param var a Node_RuleVariable defining the variable to bind * @param value the value to bind * @return false if the binding fails */ @Override public boolean bind(Node var, Node value) { if (var instanceof Node_RuleVariable) { return bind(((Node_RuleVariable)var).getIndex(), value); } else { return var.sameValueAs(value); } }
/** * Bind a variable in the current envionment to the given value. * Checks that the new binding is compatible with any current binding. * @param var a Node_RuleVariable defining the variable to bind * @param value the value to bind * @return false if the binding fails */ @Override public boolean bind(Node var, Node value) { Node dvar = var; if (dvar instanceof Node_RuleVariable) dvar = ((Node_RuleVariable)dvar).deref(); if (dvar instanceof Node_RuleVariable) { interpreter.bind(dvar, value); return true; } else { return var.sameValueAs(value); } }
/** * Bind the ith variable in the current envionment to the given value. * Checks that the new binding is compatible with any current binding. * Handles aliased variables. * @return false if the binding fails */ public boolean bind(int i, Node value) { Node node = environment[i]; if (node == null) { environment[i] = value; return true; } else if (node instanceof Node_RuleVariable) { environment[i] = value; return bind(((Node_RuleVariable)node).getIndex(), value); } else { return node.sameValueAs(value); } }
/** * Return true if the first argument is a list which contains * the second argument. */ protected static boolean listContains(Node list, Node element, RuleContext context ) { if (list == null || list.equals(RDF.Nodes.nil)) { return false; } else { Node elt = Util.getPropValue(list, RDF.Nodes.first, context); if (elt.sameValueAs(element)) { return true; } else { Node next = Util.getPropValue(list, RDF.Nodes.rest, context); return listContains(next, element, context); } } } }
/** * 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 */ @Override public boolean bodyCall(Node[] args, int length, RuleContext context) { checkArgs(length, context); Node n1 = getArg(0, args, context); Node n2 = getArg(1, args, context); if (Util.comparable(n1, n2)) { return Util.compareTypedLiterals(n1, n2) == 0; } else { return n1.sameValueAs(n2); } }
/** * 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 */ @Override public boolean bodyCall(Node[] args, int length, RuleContext context) { checkArgs(length, context); Node n1 = getArg(0, args, context); Node n2 = getArg(1, args, context); if (Util.comparable(n1, n2)) { return Util.compareTypedLiterals(n1, n2) != 0; } else { return ! n1.sameValueAs(n2); } }
@Override public QueryIterator execEvaluated(Binding binding, Node subject, Node predicate, Node object, ExecutionContext execCxt) { if ( Var.isVar(subject) ) throw new ExprEvalException("bnode: subject is an unbound variable") ; if ( ! subject.isBlank() ) return IterLib.noResults(execCxt) ; String str = subject.getBlankNodeLabel() ; Node obj = Node.createLiteral(str) ; if ( Var.isVar(object) ) return IterLib.oneResult(binding, Var.alloc(object), obj, execCxt) ; // Subject and object are concrete if ( object.sameValueAs(obj) ) return IterLib.result(binding, execCxt) ; return IterLib.noResults(execCxt) ; } }
@Override public QueryIterator execEvaluated(Binding binding, Node subject, Node predicate, Node object, ExecutionContext execCxt) { if ( Var.isVar(subject) ) throw new ExprEvalException("bnode: subject is an unbound variable") ; if ( ! subject.isBlank() ) return IterLib.noResults(execCxt) ; String str = subject.getBlankNodeLabel() ; Node obj = NodeFactory.createLiteral(str) ; if ( Var.isVar(object) ) return IterLib.oneResult(binding, Var.alloc(object), obj, execCxt) ; // Subject and object are concrete if ( object.sameValueAs(obj) ) return IterLib.result(binding, execCxt) ; return IterLib.noResults(execCxt) ; } }
@Override public QueryIterator execEvaluated(Binding binding, Node subject, Node predicate, Node object, ExecutionContext execCxt) { if ( subject.isVariable() && object.isVariable() ) throw new QueryExecException("Both subject and object are unbound variables: "+FmtUtils.stringForNode(predicate)) ; if ( subject.isVariable() ) // Object not a variable or already bound return IterLib.oneResult(binding, Var.alloc(subject), object, execCxt) ; if ( object.isVariable() ) // Subjects not a variable or already bound return IterLib.oneResult(binding, Var.alloc(object), subject, execCxt) ; // Both bound. Must be the same. if ( subject.sameValueAs(object) ) return IterLib.result(binding, execCxt) ; // different return IterLib.noResults(execCxt) ; }
@Override public QueryIterator execEvaluated(Binding binding, Node subject, Node predicate, Node object, ExecutionContext execCxt) { if ( subject.isVariable() && object.isVariable() ) throw new QueryExecException("Both subject and object are unbound variables: "+FmtUtils.stringForNode(predicate)) ; if ( subject.isVariable() ) // Object not a variable or already bound return IterLib.oneResult(binding, Var.alloc(subject), object, execCxt) ; if ( object.isVariable() ) // Subjects not a variable or already bound return IterLib.oneResult(binding, Var.alloc(object), subject, execCxt) ; // Both bound. Must be the same. if ( subject.sameValueAs(object) ) return IterLib.result(binding, execCxt) ; // different return IterLib.noResults(execCxt) ; }
private void test(String x, Node correct) { Node n = NodeFactory.parseNode(x) ; NodeId nodeId = NodeId.inline(n) ; if ( correct == null ) { assertNull(nodeId) ; return ; } Node n2 = NodeId.extract(nodeId) ; assertNotNull(n2) ; String s = "("+correct.getLiteralLexicalForm()+","+n2.getLiteralLexicalForm()+")" ; assertTrue("Not same value: "+s, correct.sameValueAs(n2)) ; // Term equality. assertEquals("Not same term", correct, n2) ; } }