This file is indexed.

/usr/share/pyshared/pyke/fact_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# $Id: fact_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.

'''
    A fact_base is one of the kinds of knowledge_bases (see also, rule_base
    and special).

        >>> from pyke import knowledge_engine
        >>> engine = knowledge_engine.engine()
        >>> fb = fact_base(engine, 'fb_name')
        >>> fb
        <fact_base fb_name>
        >>> fb.dump_universal_facts()
        >>> fb.dump_specific_facts()

    A fact_base is nothing more than a list of facts.  Each fact has a name
    and a tuple of arguments.  These arguments are python data (not
    patterns).

    Fact_bases support two kinds of facts: universal facts (universally
    true) and case specific facts (only true in a specific situation).

        >>> fb.add_universal_fact('some_universal_fact', ('a', 2))
        >>> fb.add_case_specific_fact('some_specific_fact', ('b', ('hi', 32)))
        >>> fb.dump_universal_facts()
        some_universal_fact('a', 2)
        >>> fb.dump_specific_facts()
        some_specific_fact('b', ('hi', 32))

    The 'reset' method deletes all case specific facts, but leaves the
    universal facts.

        >>> fb.reset()
        >>> fb.dump_universal_facts()
        some_universal_fact('a', 2)
        >>> fb.dump_specific_facts()

    Normally, universal facts are established once at program
    initialization time and case specific facts are established both just
    prior to each invocation of the expert system as well as by assertions
    in forward chaining rules.

        >>> fb.assert_('some_fact', ('a', 2, ('hi', 'mom')))
        >>> fb.dump_universal_facts()
        some_universal_fact('a', 2)
        >>> fb.dump_specific_facts()
        some_fact('a', 2, ('hi', 'mom'))
        >>> fb.assert_('some_fact', ('a', 3, ('hi', 'mom')))
        >>> fb.dump_specific_facts()
        some_fact('a', 2, ('hi', 'mom'))
        some_fact('a', 3, ('hi', 'mom'))
        >>> fb.assert_('some_other_fact', ())
        >>> fb.dump_specific_facts()
        some_fact('a', 2, ('hi', 'mom'))
        some_fact('a', 3, ('hi', 'mom'))
        some_other_fact()
    
    Duplicate facts are not allowed and trying to assert a duplicate fact is
    silently ignored.

        >>> fb.assert_('some_fact', ('a', 2, ('hi', 'mom')))
        >>> fb.dump_specific_facts()
        some_fact('a', 2, ('hi', 'mom'))
        some_fact('a', 3, ('hi', 'mom'))
        some_other_fact()

'''

import itertools
import contextlib
from pyke import knowledge_base, contexts

class fact_base(knowledge_base.knowledge_base):
    ''' Not much to fact_bases.  The real work is done in fact_list! '''
    def __init__(self, engine, name, register = True):
        super(fact_base, self).__init__(engine, name, fact_list, register)

    def dump_universal_facts(self):
        for fl_name in sorted(self.entity_lists.iterkeys()):
            self.entity_lists[fl_name].dump_universal_facts()

    def dump_specific_facts(self):
        for fl_name in sorted(self.entity_lists.iterkeys()):
            self.entity_lists[fl_name].dump_specific_facts()

    def add_universal_fact(self, fact_name, args):
        self.get_entity_list(fact_name).add_universal_fact(args)

    def add_case_specific_fact(self, fact_name, args):
        self.get_entity_list(fact_name).add_case_specific_fact(args)

    def assert_(self, fact_name, args):
        self.add_case_specific_fact(fact_name, args)

    def get_stats(self):
        num_fact_lists = num_universal = num_case_specific = 0
        for fact_list in self.entity_lists.itervalues():
            universal, case_specific = fact_list.get_stats()
            num_universal += universal
            num_case_specific += case_specific
            num_fact_lists += 1
        return num_fact_lists, num_universal, num_case_specific

    def print_stats(self, f):
        num_fact_lists, num_universal, num_case_specific = self.get_stats()
        f.write("%s: %d fact names, %d universal facts, "
                "%d case_specific facts\n" %
                (self.name, num_fact_lists, num_universal, num_case_specific))

class fact_list(knowledge_base.knowledge_entity_list):
    def __init__(self, name):
        super(fact_list, self).__init__(name)
        self.universal_facts = []               # [(arg...)...]
        self.case_specific_facts = []           # [(arg...)...]
        self.hashes = {}        # (len, (index...)): (other_indices,
                                #       {(arg...): [other_args_from_factn...]})
        self.fc_rule_refs = []  # (fc_rule, foreach_index)

    def reset(self):
        self.case_specific_facts = []
        self.hashes.clear()
        self.fc_rule_refs = []

    def dump_universal_facts(self):
        for args in self.universal_facts:
            print '%s%s' % (self.name, args)

    def dump_specific_facts(self):
        for args in self.case_specific_facts:
            print '%s%s' % (self.name, args)

    def add_fc_rule_ref(self, fc_rule, foreach_index):
        self.fc_rule_refs.append((fc_rule, foreach_index))

    def get_affected_fc_rules(self):
        return (fc_rule for fc_rule, foreach_index in self.fc_rule_refs)

    def lookup(self, bindings, pat_context, patterns):
        """ Returns a context manager for a generator that binds patterns to
            successive facts, yielding None for each successful match.
            Undoes bindings upon continuation, so that no bindings remain at
            StopIteration.
        """
        indices = tuple(enum for enum in enumerate(patterns)
                             if enum[1].is_data(pat_context))
        other_indices, other_arg_lists = \
            self._get_hashed(len(patterns),
                             tuple(index[0] for index in indices),
                             tuple(index[1].as_data(pat_context)
                                   for index in indices))
        def gen():
            if other_arg_lists:
                for args in other_arg_lists:
                    mark = bindings.mark(True)
                    end_done = False
                    try:
                        if all(itertools.imap(
                                   lambda i, arg:
                                       patterns[i].match_data(bindings,
                                                              pat_context,
                                                              arg),
                                   other_indices,
                                   args)):
                            bindings.end_save_all_undo()
                            end_done = True
                            yield
                    finally:
                        if not end_done: bindings.end_save_all_undo()
                        bindings.undo_to_mark(mark)
        return contextlib.closing(gen())

    def _get_hashed(self, len, indices, args):
        ans = self.hashes.get((len, indices))
        if ans is None: ans = self._hash(len, indices)
        other_indices, arg_map = ans
        return other_indices, arg_map.get(args, ())

    def _hash(self, length, indices):
        args_hash = {}
        new_entry = (tuple(i for i in range(length) if i not in indices),
                     args_hash)
        self.hashes[length, indices] = new_entry
        for args in itertools.chain(self.universal_facts,
                                    self.case_specific_facts):
            if len(args) == length:
                selected_args = tuple(arg for i, arg in enumerate(args)
                                          if i in indices)
                args_hash.setdefault(selected_args, []) \
                         .append(tuple(arg for i, arg in enumerate(args)
                                           if i not in indices))
        return new_entry

    def add_universal_fact(self, args):
        assert args not in self.case_specific_facts, \
               "add_universal_fact: fact already present as specific fact"
        if args not in self.universal_facts:
            self.universal_facts.append(args)
            self.add_args(args)

    def add_case_specific_fact(self, args):
        if args not in self.universal_facts and \
           args not in self.case_specific_facts:
            self.case_specific_facts.append(args)
            self.add_args(args)
            for fc_rule, foreach_index in self.fc_rule_refs:
                fc_rule.new_fact(args, foreach_index)

    def add_args(self, args):
        for (length, indices), (other_indices, arg_map) \
         in self.hashes.iteritems():
            if length == len(args):
                selected_args = tuple(arg for i, arg in enumerate(args)
                                          if i in indices)
                arg_map.setdefault(selected_args, []) \
                       .append(tuple(arg for i, arg in enumerate(args)
                                         if i not in indices))

    def get_stats(self):
        return len(self.universal_facts), len(self.case_specific_facts)