knowledge_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 » knowledge_base.py
# $Id: knowledge_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.

class gen_tuple(object):
    def __init__(self, tup): self.tup = tup

    def __enter__(self): return self.tup

    def __exit__(self, type, value, tb): pass

Gen_empty = gen_tuple(())
Gen_once = gen_tuple((None,))

class knowledge_base(object):
    ''' This object is a master repository for knowledge entities of different
        names.  These knowledge entities could be facts or rules.  The
        cumulative information maintained in a knowledge_base represents all
        knowledge within a specific domain.
        
        In the syntax: "name1.name2(arg_pattern...)", the knowledge_base name
        is "name1".
    '''
    def __init__(self, engine, name, entity_list_type = None, register = True):
        self.name = name
        self.entity_lists = {}          # {name: entity_list}
        self.entity_list_type = entity_list_type
        self.initialized = False        # used by self.init2
        if register: self.register(engine)
        else: self.engine = engine

    def register(self, engine):
        r'''
            Called at most once either from __init__afterloadinga import 
            pickle.
        '''
        self.engine = engine
        name = self.name
        if name in engine.knowledge_bases:
            raise AssertionError("knowledge_base %s already exists" % name)
        if name in engine.rule_bases:
            raise AssertionError("name clash between %s '%s' and "
                                 "rule_base '%s'" %
                                     (self.__class__.__name__, name, name))
        engine.knowledge_bases[name] = self

    def __getstate__(self):
        r'''
            User must call 'register' on the new instance after loading it
            from thepickle.Wedothissothatwedontenduppicklingthe import 
            whole engine!
        '''
        ans = vars(self).copy()
        del ans['engine']
        return ans

    def init2(self):
        ''' overridden by subclasses. '''
        pass

    def reset(self):
        for entity in self.entity_lists.itervalues(): entity.reset()

    def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.name)

    def get_entity_list(self, entity_name):
        ans = self.entity_lists.get(entity_name)
        if ans is None:
            if self.entity_list_type:
                ans = self.entity_lists[entity_name] \
                    = self.entity_list_type(entity_name)
            else:
                raise KeyError("%s not found in knowledge_base %s" %
                               (entity_name, self.name))
        return ans

    def lookup(self, bindings, pat_context, entity_name, patterns):
        entity = self.entity_lists.get(entity_name)
        if entity is None: return Gen_empty
        return entity.lookup(bindings, pat_context, patterns)

    def prove(self, bindings, pat_context, entity_name, patterns):
        entity = self.entity_lists.get(entity_name)
        if entity is None: return Gen_empty
        return entity.prove(bindings, pat_context, patterns)

    def add_fc_rule_ref(self, entity_name, fc_rule, foreach_index):
        self.get_entity_list(entity_name) \
            .add_fc_rule_ref(fc_rule, foreach_index)

class knowledge_entity_list(object):
    ''' This object keeps track of all of the knowledge entities sharing the
        same name.  For example, these knowledge entities could be all the
        facts of the same name or all of the rules of the same name.
        Generally, all of the entities in this list may come to bear on
        looking up or proving a single fact or goal.

        In the syntax: "name1.name2(arg_pattern...)", the knowledge entity
        name is "name2".
    '''
    def __init__(self, name):
        self.name = name

    def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.name)

    def reset(self):
        pass

    def prove(self, bindings, pat_context, patterns):
        return self.lookup(bindings, pat_context, patterns)

    def add_fc_rule_ref(self, fc_rule, foreach_index):
        pass

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