/usr/share/pyshared/pyke/knowledge_base.py is in python-pyke 1.1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # $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__ or after loading from a
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 the pickle. We do this so that we don't end up pickling the
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
|