/** * Check that the object of a triple match corresponds to the given functor pattern. * Side effects the variable bindings. */ public boolean functorMatch(Triple t, LPInterpreter interpreter) { Node o = t.getObject(); if (!Functor.isFunctor(o)) return false; Functor f = (Functor)o.getLiteralValue(); if ( ! f.getName().equals(objectFunctor.getName())) return false; if ( f.getArgLength() != objectFunctor.getArgLength()) return false; Node[] fargs = f.getArgs(); Node[] oFargs = objectFunctor.getArgs(); for (int i = 0; i < fargs.length; i++) { if (!interpreter.unify(oFargs[i], fargs[i])) return false; } return true; }
/** * Execute the given built in as a body clause. * @param context an execution context giving access to other relevant data * @return true if the functor has an implementation and that implementation returns true when evaluated */ public boolean evalAsBodyClause(RuleContext context) { if (getImplementor() == null) { logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); return false; } return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); }
/** * Check that the object of a triple match corresponds to the given functor pattern. * Side effects the variable bindings. */ public boolean functorMatch(Triple t, LPInterpreter interpreter) { Node o = t.getObject(); if (!Functor.isFunctor(o)) return false; Functor f = (Functor)o.getLiteralValue(); if ( ! f.getName().equals(objectFunctor.getName())) return false; if ( f.getArgLength() != objectFunctor.getArgLength()) return false; Node[] fargs = f.getArgs(); Node[] oFargs = objectFunctor.getArgs(); for (int i = 0; i < fargs.length; i++) { if (!interpreter.unify(oFargs[i], fargs[i])) return false; } return true; }
/** * Execute the given built in as a body clause. * @param context an execution context giving access to other relevant data * @return true if the functor has an implementation and that implementation returns true when evaluated */ public boolean evalAsBodyClause(RuleContext context) { if (getImplementor() == null) { logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); return false; } return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); }
private boolean allMonotonic(ClauseEntry[] elts) { for ( ClauseEntry elt : elts ) { if ( elt instanceof Functor ) { Builtin b = ( (Functor) elt ).getImplementor(); if ( b != null ) { if ( !b.isMonotonic() ) { return false; } } else { throw new ReasonerException( "(allMonotonic) Undefined Functor " + ( (Functor) elt ).getName() + " in " + toShortString() ); } } } return true; }
private boolean allMonotonic(ClauseEntry[] elts) { for ( ClauseEntry elt : elts ) { if ( elt instanceof Functor ) { Builtin b = ( (Functor) elt ).getImplementor(); if ( b != null ) { if ( !b.isMonotonic() ) { return false; } } else { throw new ReasonerException( "(allMonotonic) Undefined Functor " + ( (Functor) elt ).getName() + " in " + toShortString() ); } } } return true; }
/** * Emit code for a call to a built-in predicate (functor). * @param functor the built-in to be invoked. */ void emitBody(Functor functor) { Node[] fargs = functor.getArgs(); Builtin builtin = functor.getImplementor(); if (builtin == null) { throw new LPRuleSyntaxException("Unknown builtin operation " + functor.getName(), rule); } if (builtin.getArgLength() != 0 && builtin.getArgLength() != fargs.length) { throw new LPRuleSyntaxException("Wrong number of arguments to functor " + functor.getName() + " : got " + functor.getArgLength() + " : expected " + builtin.getArgLength(), rule); } for (int i = 0; i < fargs.length; i++) { Node node = fargs[i]; // We optionally force an eager dereference of variables here. // We used to force this but the current builtin implementations // now robust against it (the do a deref themselves anyway). emitBodyPut(node, i, true); } code[p++] = CALL_BUILTIN; code[p++] = (byte)fargs.length; args.add(builtin); }
/** * Emit code for a call to a built-in predicate (functor). * @param functor the built-in to be invoked. */ void emitBody(Functor functor) { Node[] fargs = functor.getArgs(); Builtin builtin = functor.getImplementor(); if (builtin == null) { throw new LPRuleSyntaxException("Unknown builtin operation " + functor.getName(), rule); } if (builtin.getArgLength() != 0 && builtin.getArgLength() != fargs.length) { throw new LPRuleSyntaxException("Wrong number of arguments to functor " + functor.getName() + " : got " + functor.getArgLength() + " : expected " + builtin.getArgLength(), rule); } for (int i = 0; i < fargs.length; i++) { Node node = fargs[i]; // We optionally force an eager dereference of variables here. // We used to force this but the current builtin implementations // now robust against it (the do a deref themselves anyway). emitBodyPut(node, i, true); } code[p++] = CALL_BUILTIN; code[p++] = (byte)fargs.length; args.add(builtin); }
/** * Execute the given built in as a body clause, only if it is side-effect-free. * @param context an execution context giving access to other relevant data * @return true if the functor has an implementation and that implementation returns true when evaluated */ public boolean safeEvalAsBodyClause(RuleContext context) { if (getImplementor() == null) { logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); return false; } if (implementor.isSafe()) { return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); } else { return false; } }
/** * Execute the given built in as a body clause, only if it is side-effect-free. * @param context an execution context giving access to other relevant data * @return true if the functor has an implementation and that implementation returns true when evaluated */ public boolean safeEvalAsBodyClause(RuleContext context) { if (getImplementor() == null) { logger.warn("Invoking undefined functor " + getName() + " in " + context.getRule().toShortString()); return false; } if (implementor.isSafe()) { return implementor.bodyCall(getBoundArgs(context.getEnv()), args.length, context); } else { return false; } }
/** * Clone a functor, cloning any embedded variables. */ private Functor cloneFunctor(Functor f, Map<Node_RuleVariable, Node> vmap, BindingEnvironment env) { Node[] args = f.getArgs(); Node[] cargs = new Node[args.length]; for (int i = 0; i < args.length; i++) { cargs[i] = cloneNode(args[i], vmap, env); } Functor fn = new Functor(f.getName(), cargs, f.getImplementor()); fn.setImplementor(f.getImplementor()); return fn; }
/** * Clone a functor, cloning any embedded variables. */ private Functor cloneFunctor(Functor f, Map<Node_RuleVariable, Node> vmap, BindingEnvironment env) { Node[] args = f.getArgs(); Node[] cargs = new Node[args.length]; for (int i = 0; i < args.length; i++) { cargs[i] = cloneNode(args[i], vmap, env); } Functor fn = new Functor(f.getName(), cargs, f.getImplementor()); fn.setImplementor(f.getImplementor()); return fn; }
/** * Test if a pattern is just a variant of this pattern. I.e. it is the same * up to variable renaming. This takes into account multiple occurances * of the same variable. */ public boolean variantOf(TriplePattern pattern) { Map<Node, Node> vmap = CollectionFactory.createHashedMap(); if ( ! variantOf(subject, pattern.subject, vmap) ) return false; if ( ! variantOf(predicate, pattern.predicate, vmap) ) return false; if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { Functor functor = (Functor)object.getLiteralValue(); Functor pFunctor = (Functor)pattern.object.getLiteralValue(); if ( ! functor.getName().equals(pFunctor.getName()) ) return false; Node[] args = functor.getArgs(); Node[] pargs = pFunctor.getArgs(); if ( args.length != pargs.length ) return false; for (int i = 0; i < args.length; i++) { if ( ! variantOf(args[i], pargs[i], vmap) ) return false; } return true; } else { return variantOf(object, pattern.object, vmap); } }
/** * Test if a pattern is just a variant of this pattern. I.e. it is the same * up to variable renaming. This takes into account multiple occurances * of the same variable. */ public boolean variantOf(TriplePattern pattern) { Map<Node, Node> vmap = CollectionFactory.createHashedMap(); if ( ! variantOf(subject, pattern.subject, vmap) ) return false; if ( ! variantOf(predicate, pattern.predicate, vmap) ) return false; if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { Functor functor = (Functor)object.getLiteralValue(); Functor pFunctor = (Functor)pattern.object.getLiteralValue(); if ( ! functor.getName().equals(pFunctor.getName()) ) return false; Node[] args = functor.getArgs(); Node[] pargs = pFunctor.getArgs(); if ( args.length != pargs.length ) return false; for (int i = 0; i < args.length; i++) { if ( ! variantOf(args[i], pargs[i], vmap) ) return false; } return true; } else { return variantOf(object, pattern.object, vmap); } }
Functor newf = new Functor(functor.getName(), boundargs, functor.getImplementor()); return Functor.makeFunctorNode( newf ); } else {
Functor newf = new Functor(functor.getName(), boundargs, functor.getImplementor()); return Functor.makeFunctorNode( newf ); } else {
/** * Compare two patterns for compatibility - i.e. potentially unifiable. * Two patterns are "compatible" in the sense we mean here * if all their ground terms match. A variable in either pattern * can match a ground term or a variable in the other. We are not, * currently, checking for multiple occurances of the same variable. * Functor-valued object literals are treated as a special case which * are only checked for name/arity matching. */ public boolean compatibleWith(TriplePattern pattern) { boolean ok = subject.isVariable() || pattern.subject.isVariable() || subject.equals(pattern.subject); if (!ok) return false; ok = predicate.isVariable() || pattern.predicate.isVariable() || predicate.equals(pattern.predicate); if (!ok) return false; if (object.isVariable() || pattern.object.isVariable()) return true; // Left with checking compatibility of ground literals if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { Functor functor = (Functor)object.getLiteralValue(); Functor pFunctor = (Functor)pattern.object.getLiteralValue(); return (functor.getName().equals(pFunctor.getName()) && functor.getArgs().length == pFunctor.getArgs().length); } else { return object.sameValueAs(pattern.object); } }
/** * Compare two patterns for compatibility - i.e. potentially unifiable. * Two patterns are "compatible" in the sense we mean here * if all their ground terms match. A variable in either pattern * can match a ground term or a variable in the other. We are not, * currently, checking for multiple occurances of the same variable. * Functor-valued object literals are treated as a special case which * are only checked for name/arity matching. */ public boolean compatibleWith(TriplePattern pattern) { boolean ok = subject.isVariable() || pattern.subject.isVariable() || subject.equals(pattern.subject); if (!ok) return false; ok = predicate.isVariable() || pattern.predicate.isVariable() || predicate.equals(pattern.predicate); if (!ok) return false; if (object.isVariable() || pattern.object.isVariable()) return true; // Left with checking compatibility of ground literals if (Functor.isFunctor(object) && Functor.isFunctor(pattern.object)) { Functor functor = (Functor)object.getLiteralValue(); Functor pFunctor = (Functor)pattern.object.getLiteralValue(); return (functor.getName().equals(pFunctor.getName()) && functor.getArgs().length == pFunctor.getArgs().length); } else { return object.sameValueAs(pattern.object); } }
"Invoking undefined Functor " + f.getName() + " in " + r.toShortString() );