use of org.apache.jena.reasoner.rulesys.BuiltinException in project jena by apache.
the class Regex method bodyCall.
/**
* 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) {
if (length < 2)
throw new BuiltinException(this, context, "Must have at least 2 arguments to " + getName());
String text = getString(getArg(0, args, context), context);
String pattern = getString(getArg(1, args, context), context);
Matcher m = Pattern.compile(pattern).matcher(text);
if (!m.matches())
return false;
if (length > 2) {
// bind any capture groups
BindingEnvironment env = context.getEnv();
for (int i = 0; i < Math.min(length - 2, m.groupCount()); i++) {
String gm = m.group(i + 1);
Node match = (gm != null) ? NodeFactory.createLiteral(gm) : NodeFactory.createLiteral("");
if (!env.bind(args[i + 2], match))
return false;
}
}
return true;
}
use of org.apache.jena.reasoner.rulesys.BuiltinException in project jena by apache.
the class StrConcat method bodyCall.
/**
* 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) {
if (length < 1)
throw new BuiltinException(this, context, "Must have at least 1 argument to " + getName());
StringBuilder buff = new StringBuilder();
for (int i = 0; i < length - 1; i++) {
buff.append(lex(getArg(i, args, context), context));
}
Node result = NodeFactory.createLiteral(buff.toString());
return context.getEnv().bind(args[length - 1], result);
}
use of org.apache.jena.reasoner.rulesys.BuiltinException in project jena by apache.
the class UriConcat method bodyCall.
/**
* 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) {
if (length < 1)
throw new BuiltinException(this, context, "Must have at least 1 argument to " + getName());
StringBuilder buff = new StringBuilder();
for (int i = 0; i < length - 1; i++) {
buff.append(lex(getArg(i, args, context), context));
}
Node result = NodeFactory.createURI(buff.toString());
return context.getEnv().bind(args[length - 1], result);
}
use of org.apache.jena.reasoner.rulesys.BuiltinException in project jena by apache.
the class Drop method headAction.
/**
* This method is invoked when the builtin is called in a rule head.
* Such a use is only valid in a forward rule.
* @param args the array of argument values for the builtin, this is an array
* of Nodes.
* @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
*/
@Override
public void headAction(Node[] args, int length, RuleContext context) {
InfGraph inf = context.getGraph();
Graph raw = inf.getRawGraph();
Graph deductions = inf.getDeductionsGraph();
for (int i = 0; i < length; i++) {
Node clauseN = getArg(i, args, context);
if (Util.isNumeric(clauseN)) {
int clauseIndex = Util.getIntValue(clauseN);
Object clause = context.getRule().getBodyElement(clauseIndex);
if (clause instanceof TriplePattern) {
Triple t = context.getEnv().instantiate((TriplePattern) clause);
raw.delete(t);
deductions.delete(t);
} else {
throw new BuiltinException(this, context, "illegal triple to remove non-triple clause");
}
} else {
throw new BuiltinException(this, context, "illegal arg to remove (" + clauseN + "), must be an integer");
}
}
}
Aggregations