use of org.apache.jena.reasoner.rulesys.Functor in project jena by apache.
the class BindingStack method getBinding.
/**
* If the node is a variable then return the current binding (null if not bound)
* otherwise return the node itself.
*/
public Node getBinding(Node node) {
if (node instanceof Node_RuleVariable) {
return environment[((Node_RuleVariable) node).getIndex()];
} else if (node instanceof Node_ANY) {
return null;
} else if (Functor.isFunctor(node)) {
Functor functor = (Functor) node.getLiteralValue();
if (functor.isGround())
return node;
Node[] args = functor.getArgs();
List<Node> boundargs = new ArrayList<>(args.length);
for (Node arg : args) {
Node binding = getBinding(arg);
if (binding == null) {
// Not sufficent bound to instantiate functor yet
return null;
}
boundargs.add(binding);
}
Functor newf = new Functor(functor.getName(), boundargs, functor.getImplementor());
return Functor.makeFunctorNode(newf);
} else {
return node;
}
}
use of org.apache.jena.reasoner.rulesys.Functor in project jena by apache.
the class GenericTripleMatchFrame method functorMatch.
/**
* 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;
}
use of org.apache.jena.reasoner.rulesys.Functor in project jena by apache.
the class TriplePattern method compatibleWith.
/**
* 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);
}
}
use of org.apache.jena.reasoner.rulesys.Functor in project jena by apache.
the class TriplePattern method variantOf.
/**
* 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);
}
}
Aggregations