rule_base.py :  » Language-Interface » Python-Knowledge-Engine » pyke-1.1.1 » pyke » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Python Knowledge Engine 
Python Knowledge Engine » pyke 1.1.1 » pyke » rule_base.py
# $Id: rule_base.py 081917d30609 2010-03-05 mtnyogi $
# coding=utf-8
# 
# Copyright  2007-2008 Bruce Frederiksen
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import itertools
from pyke import knowledge_base

class StopProof(Exception): pass

class stopIteratorContext(object):
    def __init__(self, rule_base, iterator_context):
        self.rule_base = rule_base
        self.context = iterator_context

    def __enter__(self):
        return stopIterator(self.rule_base, self.context.__enter__())

    def __exit__(self, type, value, tb):
        self.context.__exit__(type, value, tb)

class stopIterator(object):
    def __init__(self, rule_base, iterator):
        self.rule_base = rule_base
        self.iterator = iter(iterator)

    def __iter__(self): return self

    def next(self):
        if self.iterator:
            try:
                return self.iterator.next()
            except StopProof:
                self.iterator = None
                self.rule_base.num_bc_rule_failures += 1
        raise StopIteration

class chain_context(object):
    def __init__(self, outer_it):
        self.outer_it = outer_iterable(outer_it)

    def __enter__(self):
        return itertools.chain.from_iterable(self.outer_it)

    def __exit__(self, type, value, tb): self.outer_it.close()

class outer_iterable(object):
    def __init__(self, outer_it):
        self.outer_it = iter(outer_it)
        self.inner_it = None

    def __iter__(self): return self

    def close(self):
        if hasattr(self.inner_it, '__exit__'):
            self.inner_it.__exit__(None, None, None)
        elif hasattr(self.inner_it, 'close'): self.inner_it.close()
        if hasattr(self.outer_it, 'close'): self.outer_it.close()

    def next(self):
        ans = self.outer_it.next()
        if hasattr(ans, '__enter__'):
            self.inner_it = ans
            return ans.__enter__()
        ans = iter(ans)
        self.inner_it = ans
        return ans

class rule_base(knowledge_base.knowledge_base):
    def __init__(self, engine, name, parent = None, exclude_list = ()):
        super(rule_base, self).__init__(engine, name, rule_list, False)
        if name in engine.rule_bases:
            raise AssertionError("rule_base %s already exists" % name)
        if name in engine.knowledge_bases:
            raise AssertionError("name clash between rule_base '%s' and "
                                 "fact_base '%s'" % (name, name))
        engine.rule_bases[name] = self
        self.fc_rules = []
        self.parent = parent
        self.exclude_set = frozenset(exclude_list)
        self.rules = {}         # {name: rule}

    def add_fc_rule(self, fc_rule):
        if fc_rule.name in self.rules:
            raise AssertionError("%s rule_base: duplicate rule name: %s" %
                                 (self.name, fc_rule.name))
        self.rules[fc_rule.name] = fc_rule
        self.fc_rules.append(fc_rule)

    def add_bc_rule(self, bc_rule):
        if bc_rule.name in self.rules:
            raise AssertionError("%s rule_base: duplicate rule name: %s" %
                                 (self.name, bc_rule.name))
        self.rules[bc_rule.name] = bc_rule
        self.get_entity_list(bc_rule.goal_name).add_bc_rule(bc_rule)

    def init2(self):
        if not self.initialized:
            self.initialized = True
            if self.parent:
                parent = self.engine.rule_bases.get(self.parent)
                if parent is None:
                    raise KeyError("rule_base %s: parent %s not found" % \
                                   (self.name, self.parent))
                self.parent = parent
                self.parent.init2()
                self.root_name = self.parent.root_name
            else:
                self.root_name = self.name
            self.reset()

    def derived_from(self, rb):
        parent = self.parent
        while parent:
            if parent == rb: return True
            parent = parent.parent
        return False

    def register_fc_rules(self, stop_at_rb):
        rb = self
        while rb is not stop_at_rb:
            for fc_rule in rb.fc_rules: fc_rule.register_rule()
            if not rb.parent: break
            rb = rb.parent

    def run_fc_rules(self, stop_at_rb):
        rb = self
        while rb is not stop_at_rb:
            for fc_rule in rb.fc_rules: fc_rule.run()
            if not rb.parent: break
            rb = rb.parent

    def activate(self):
        current_rb = self.engine.knowledge_bases.get(self.root_name)
        if current_rb:
            assert self.derived_from(current_rb), \
                   "%s.activate(): not derived from current rule_base, %s" % \
                   (self.name, current_rb.name)
        self.engine.knowledge_bases[self.root_name] = self
        self.register_fc_rules(current_rb)
        self.run_fc_rules(current_rb)

    def reset(self):
        if self.root_name in self.engine.knowledge_bases:
            del self.engine.knowledge_bases[self.root_name]
        for fc_rule in self.fc_rules: fc_rule.reset()
        self.num_fc_rules_triggered = 0
        self.num_fc_rules_rerun = 0
        self.num_prove_calls = 0
        self.num_bc_rules_matched = 0
        self.num_bc_rule_successes = 0
        self.num_bc_rule_failures = 0

    def gen_rule_lists_for(self, goal_name):
        rule_base = self
        while True:
            rl = rule_base.entity_lists.get(goal_name)
            if rl: yield rl
            if rule_base.parent and goal_name not in rule_base.exclude_set:
                rule_base = rule_base.parent
            else:
                break

    def prove(self, bindings, pat_context, goal_name, patterns):
        self.num_prove_calls += 1
        return stopIteratorContext(self,
                   chain_context(
                       rl.prove(bindings, pat_context, patterns)
                       for rl in self.gen_rule_lists_for(goal_name)))

    def print_stats(self, f):
        f.write("%s: %d fc_rules, %d triggered, %d rerun\n" %
                (self.name, len(self.fc_rules), self.num_fc_rules_triggered,
                 self.num_fc_rules_rerun))
        num_bc_rules = sum(rule_list.num_bc_rules()
                             for rule_list in self.entity_lists.itervalues())
        f.write("%s: %d bc_rules, %d goals, %d rules matched\n" %
                (self.name, num_bc_rules, self.num_prove_calls,
                 self.num_bc_rules_matched))
        f.write("%s  %d successes, %d failures\n" %
                (' ' * len(self.name), self.num_bc_rule_successes,
                 self.num_bc_rule_failures))
        if self.parent: self.parent.print_stats(f)

    def trace(self, rule_name):
        for rule_list in self.entity_lists.itervalues():
            if rule_list.trace(rule_name): return
        raise KeyError("trace: rule %s not found" % rule_name)

    def untrace(self, rule_name):
        for rule_list in self.entity_lists.itervalues():
            if rule_list.untrace(rule_name): return
        raise KeyError("untrace: rule %s not found" % rule_name)

class rule_list(knowledge_base.knowledge_entity_list):
    def __init__(self, name):
        self.name = name
        self.bc_rules = []

    def add_bc_rule(self, bc_rule):
        self.bc_rules.append(bc_rule)

    def prove(self, bindings, pat_context, patterns):
        """ Returns a context manager for a generator that binds patterns to
            successively proven goals, yielding the plan (or None, if no plan)
            for each successful match.  Undoes bindings upon continuation,
            so that no bindings remain at StopIteration.
        """
        return chain_context(
                   bc_rule.bc_fn(bc_rule, patterns, pat_context)
                   for bc_rule in self.bc_rules)

    def num_bc_rules(self):
        return len(self.bc_rules)

    def trace(self, rule_name):
        for bc_rule in self.bc_rules:
            if bc_rule.name == rule_name:
                bc_rule.trace()
                return True
        return False

    def untrace(self, rule_name):
        for bc_rule in self.bc_rules:
            if bc_rule.name == rule_name:
                bc_rule.untrace()
                return True
        return False

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.