Package com.hp.hpl.jena.reasoner.rulesys

Examples of com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment


    /**
     * Execute a single rule firing.
     */
    public static void execute(RETERuleContext context, boolean isAdd) {
        Rule rule = context.getRule();
        BindingEnvironment env = context.getEnv();
        ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI)context.getGraph();
        if (infGraph.shouldTrace()) {
            logger.info("Fired rule: " + rule.toShortString());
        }
        RETEEngine engine = context.getEngine();
        engine.incRuleCount();
        List<Triple> matchList = null;
        if (infGraph.shouldLogDerivations() && isAdd) {
            // Create derivation record
            matchList = new ArrayList<>(rule.bodyLength());
            for (int i = 0; i < rule.bodyLength(); i++) {
                Object clause = rule.getBodyElement(i);
                if (clause instanceof TriplePattern) {
                    matchList.add(env.instantiate((TriplePattern)clause));
                }
            }
        }
        for (int i = 0; i < rule.headLength(); i++) {
            Object hClause = rule.getHeadElement(i);
            if (hClause instanceof TriplePattern) {
                Triple t = env.instantiate((TriplePattern) hClause);
                // Used to filter out triples with literal subjects
                // but this is not necessary
                // if (!t.getSubject().isLiteral()) {
                    // Only add the result if it is legal at the RDF level.
                    // E.g. RDFS rules can create assertions about literals
View Full Code Here


    /**
     * Execute a single rule firing.
     */
    public static void execute(RETERuleContext context, boolean isAdd) {
        Rule rule = context.getRule();
        BindingEnvironment env = context.getEnv();
        ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI)context.getGraph();
        if (infGraph.shouldTrace()) {
            logger.info("Fired rule: " + rule.toShortString());
        }
        RETEEngine engine = context.getEngine();
        engine.incRuleCount();
        List<Triple> matchList = null;
        if (infGraph.shouldLogDerivations() && isAdd) {
            // Create derivation record
            matchList = new ArrayList<Triple>(rule.bodyLength());
            for (int i = 0; i < rule.bodyLength(); i++) {
                Object clause = rule.getBodyElement(i);
                if (clause instanceof TriplePattern) {
                    matchList.add(env.instantiate((TriplePattern)clause));
                }
            }
        }
        for (int i = 0; i < rule.headLength(); i++) {
            Object hClause = rule.getHeadElement(i);
            if (hClause instanceof TriplePattern) {
                Triple t = env.instantiate((TriplePattern) hClause);
                // Used to filter out triples with literal subjects
                // but this is not necessary
                // if (!t.getSubject().isLiteral()) {
                    // Only add the result if it is legal at the RDF level.
                    // E.g. RDFS rules can create assertions about literals
View Full Code Here

   
    static class DummyRuleContext implements RuleContext {

        @Override
        public BindingEnvironment getEnv() {
            return new BindingEnvironment() {
               
                @Override
                public Triple instantiate(TriplePattern pattern) {
                    // TODO Auto-generated method stub
                    return null;
View Full Code Here

     * the current environment
     */
    @Override
    public boolean bodyCall(Node[] args, int length, RuleContext context) {
        checkArgs(length, context);
        BindingEnvironment env = context.getEnv();
        Node n1 = getArg(0, args, context);
        Node n2 = getArg(1, args, context);
        if (n1.isLiteral() && n2.isLiteral()) {
            Object v1 = n1.getLiteralValue();
            Object v2 = n2.getLiteralValue();
            Node sum = null;
            if (v1 instanceof Number && v2 instanceof Number) {
                Number nv1 = (Number)v1;
                Number nv2 = (Number)v2;
                if (v1 instanceof Float || v1 instanceof Double
                ||  v2 instanceof Float || v2 instanceof Double) {
                    sum = Util.makeDoubleNode(nv1.doubleValue() / nv2.doubleValue());
                } else {
                    sum = Util.makeLongNode(nv1.longValue() / nv2.longValue());
                }
                return env.bind(args[2], sum);
            }
        }
        // Doesn't (yet) handle partially bound cases
        return false;
    }
View Full Code Here

        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) ? Node.createLiteral( gm ) : Node.createLiteral("");
                if ( !env.bind(args[i+2], match) ) return false;
            }
        }
        return true;
    }
View Full Code Here

     * the current environment
     */
    @Override
    public boolean bodyCall(Node[] args, int length, RuleContext context) {
        checkArgs(length, context);
        BindingEnvironment env = context.getEnv();
        Node now = Node.createLiteral( LiteralLabelFactory.create(new XSDDateTime(Calendar.getInstance())) );
        return env.bind(args[0], now);
    }
View Full Code Here

   
    static class DummyRuleContext implements RuleContext {

        @Override
        public BindingEnvironment getEnv() {
            return new BindingEnvironment() {
               
                @Override
                public Triple instantiate(TriplePattern pattern) {
                    // TODO Auto-generated method stub
                    return null;
View Full Code Here

     * the current environment
     */
    @Override
    public boolean bodyCall(Node[] args, int length, RuleContext context) {
        checkArgs(length, context);
        BindingEnvironment env = context.getEnv();
        Node now = Node.createLiteral( LiteralLabelFactory.create(new XSDDateTime(Calendar.getInstance())) );
        return env.bind(args[0], now);
    }
View Full Code Here

TOP

Related Classes of com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.