⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpbruleengine.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                ((LPTopGoalIterator)i.next()).close();
            }
        }
    }
    
    
//  =======================================================================
//  Interface for tabled operations

    /**
     * Register an RDF predicate as one whose presence in a goal should force
     * the goal to be tabled.
     */
    public synchronized void tablePredicate(Node predicate) {
        ruleStore.tablePredicate(predicate);
    }
    
    /**
     * Return a generator for the given goal (assumes that the caller knows that
     * the goal should be tabled).
     * @param goal the goal whose results are to be generated
     * @param clauses the precomputed set of code blocks used to implement the goal
     */
    public synchronized Generator generatorFor(TriplePattern goal, List clauses) {
        Generator generator = (Generator) tabledGoals.get(goal);
        if (generator == null) {
            LPInterpreter interpreter = new LPInterpreter(this, goal, clauses, false);
            activeInterpreters.add(interpreter);
            generator = new Generator(interpreter, goal);
            schedule(generator);
            tabledGoals.put(goal, generator);
        }
        return generator;
    }
        
    /**
     * Return a generator for the given goal (assumes that the caller knows that
     * the goal should be tabled).
     * @param goal the goal whose results are to be generated
     */
    public synchronized Generator generatorFor(TriplePattern goal) {
        Generator generator = (Generator) tabledGoals.get(goal);
        if (generator == null) {
            LPInterpreter interpreter = new LPInterpreter(this, goal, false);
            activeInterpreters.add(interpreter);
            generator = new Generator(interpreter, goal);
            schedule(generator);
            tabledGoals.put(goal, generator);
        }
        return generator;
    }
    
    /**
     * Register that a generator or specific generator state (Consumer choice point)
     * is now ready to run.
     */
    public void schedule(LPAgendaEntry state) {
        agenda.add(state);
    }
    
    /**
     * Run the scheduled generators until the given generator is ready to run.
     */
    public synchronized void pump(LPInterpreterContext gen) {
//        System.out.println("Pump agenda on engine " + this + ", size = " + agenda.size());
        Collection batch = null;
        if (CYCLES_BETWEEN_COMPLETION_CHECK > 0) {
            batch = new ArrayList(CYCLES_BETWEEN_COMPLETION_CHECK);
        }
        int count = 0; 
        while(!gen.isReady()) {
            if (agenda.isEmpty()) {
//                System.out.println("Cycled " + this + ", " + count);
                return;
            } 
//            Iterator ai = agenda.iterator();
//            LPAgendaEntry next = (LPAgendaEntry) ai.next();
//            ai.remove();
            int chosen = agenda.size() - 1;
            LPAgendaEntry next = (LPAgendaEntry) agenda.get(chosen);
            agenda.remove(chosen);
//            System.out.println("  pumping entry " + next);
            next.pump();
            count ++;
            if (CYCLES_BETWEEN_COMPLETION_CHECK > 0) {
                batch.add(next.getGenerator());
                if (count % CYCLES_BETWEEN_COMPLETION_CHECK == 0) {
                    Generator.checkForCompletions(batch);
                    batch.clear();
                }
            }
        }
        if (CYCLES_BETWEEN_COMPLETION_CHECK > 0 && !batch.isEmpty()) {
            Generator.checkForCompletions(batch);
        }
        
//        System.out.println("Cycled " + this + ", " + count);
    }
     
    /**
     * Check all known interpeter contexts to see if any are complete.
     */
    public void checkForCompletions() {
        ArrayList contexts = new ArrayList(activeInterpreters.size());
        for (Iterator i = activeInterpreters.iterator(); i.hasNext(); ) {
            LPInterpreter interpreter = (LPInterpreter)i.next();
            if (interpreter.getContext() instanceof Generator) {
                contexts.add(interpreter.getContext());     
            }
        }
        Generator.checkForCompletions(contexts);
    }
    
//  =======================================================================
//  Profiling support
   
    /**
     * Record a rule invocation in the profile count.
     */
    public void incrementProfile(RuleClauseCode clause) {
        if (profile != null) {
            String index = clause.toString();
            Count count = (Count)profile.get(index);
            if (count == null) {
                profile.put(index, new Count(clause).inc());
            } else {
                count.inc();
            }
        }
    }
    
    /**
     * Reset the profile.
     * @param enable it true then profiling will continue with a new empty profile table,
     * if false profiling will stop all current data lost.
     */
    public void resetProfile(boolean enable) {
        profile = enable ? new HashMap() : null;
    }
    
    /**
     * Print a profile of rules used since the last reset.
     */
    public void printProfile() {
        if (profile == null) {
            System.out.println("No profile collected");
        } else {
            ArrayList counts = new ArrayList();
            counts.addAll(profile.values());
            Collections.sort(counts);
            System.out.println("LP engine rule profile");
            for (Iterator i = counts.iterator(); i.hasNext(); ) {
                System.out.println(i.next());
            }
        }
    }
    
    /**
     * Record count of number of rule invocations, used in profile structure only.
     */
    static class Count implements Comparable {
        protected int count = 0;
        protected RuleClauseCode clause;

        /** Constructor */
        public Count(RuleClauseCode clause) {
            this.clause = clause;
        }
        
        /** return the count value */
        public int getCount() {
            return count;        
        }
        
        /** increment the count value, return the count object */
        public Count inc() {
            count++;
            return this;
        }
        
        /** Ordering */
        public int compareTo(Object other) {
            Count otherCount = (Count) other;
            return (count < otherCount.count) ? -1 : ( (count == otherCount.count) ? 0 : +1);
        }
        
        /** Printable form */
        public String toString() {
            return " " + count + "\t - " + clause;
        }
        
    }
}



/*
    (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. The name of the author may not be used to endorse or promote products
       derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -