Search in sources :

Example 1 with BuiltinException

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;
}
Also used : BuiltinException(org.apache.jena.reasoner.rulesys.BuiltinException) Matcher(java.util.regex.Matcher) Node(org.apache.jena.graph.Node) BindingEnvironment(org.apache.jena.reasoner.rulesys.BindingEnvironment)

Example 2 with BuiltinException

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);
}
Also used : BuiltinException(org.apache.jena.reasoner.rulesys.BuiltinException) Node(org.apache.jena.graph.Node)

Example 3 with BuiltinException

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);
}
Also used : BuiltinException(org.apache.jena.reasoner.rulesys.BuiltinException) Node(org.apache.jena.graph.Node)

Example 4 with BuiltinException

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");
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) BuiltinException(org.apache.jena.reasoner.rulesys.BuiltinException) InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) TriplePattern(org.apache.jena.reasoner.TriplePattern)

Aggregations

Node (org.apache.jena.graph.Node)4 BuiltinException (org.apache.jena.reasoner.rulesys.BuiltinException)4 Matcher (java.util.regex.Matcher)1 Graph (org.apache.jena.graph.Graph)1 Triple (org.apache.jena.graph.Triple)1 InfGraph (org.apache.jena.reasoner.InfGraph)1 TriplePattern (org.apache.jena.reasoner.TriplePattern)1 BindingEnvironment (org.apache.jena.reasoner.rulesys.BindingEnvironment)1