Example #1
0
def populate(engine):
    This_rule_base = engine.get_create('pcb')

    fc_rule.fc_rule('needs_hifreq', This_rule_base, needs_hifreq,
                    (('pcb', 'is_hf',
                      (contexts.variable('circuit'), ), False), ),
                    (contexts.variable('circuit'), ))
def populate(engine):
  This_rule_base = engine.get_create('krbparse_test')
  
  fc_rule.fc_rule('name1', This_rule_base, name1,
    (('a', 'b',
      (pattern.pattern_literal('x'),
       contexts.variable('b'),),
      False),),
    (contexts.variable('b'),))
def populate(engine):
    This_rule_base = engine.get_create('remedyget')

    bc_rule.bc_rule('get', This_rule_base, 'get', get, None, (
        contexts.variable('fault'),
        contexts.variable('remedy'),
    ), (), (
        contexts.variable('fault'),
        contexts.variable('remedy'),
    ))
def populate(engine):
    This_rule_base = engine.get_create('regras')

    bc_rule.bc_rule('febre', This_rule_base, 'febre', febre, None, (
        contexts.variable('temperatura'),
        contexts.variable('dia'),
    ), (), (
        contexts.variable('fahrenheit'),
        contexts.variable('unit'),
        contexts.variable('celsius'),
    ))
def populate(engine):
  This_rule_base = engine.get_create('krbparse_test')
  
  bc_rule.bc_rule('name2', This_rule_base, 'x',
                  name2, krbparse_test_plans.name2,
                  (pattern.pattern_literal(1),
                   contexts.variable('c'),
                   pattern.pattern_literal((1, 'b',)),
                   pattern.pattern_tuple((pattern.pattern_literal(1), pattern.pattern_literal('b'), contexts.variable('c'),), None),),
                  ('plan', 'd',),
                  (pattern.pattern_literal('x'),
                   contexts.variable('c'),
                   contexts.variable('plan#1'),
                   pattern.pattern_literal(1),
                   pattern.pattern_literal(2),
                   pattern.pattern_literal(3),
                   contexts.variable('plan#2'),))
  
  bc_rule.bc_rule('name3', This_rule_base, 'x',
                  name3, krbparse_test_plans.name3,
                  (pattern.pattern_literal(1),
                   contexts.variable('c'),
                   pattern.pattern_literal((1, 'b',)),
                   pattern.pattern_tuple((pattern.pattern_literal(1), pattern.pattern_literal('b'), contexts.variable('c'),), None),),
                  ('d',),
                  (pattern.pattern_literal('x'),
                   contexts.variable('c'),
                   contexts.variable('plan#1'),
                   pattern.pattern_literal(1),
                   pattern.pattern_literal(2),
                   pattern.pattern_literal(3),
                   contexts.variable('foo_fn'),))
Example #6
0
def as_pattern(data):
    if isinstance(data, tuple) and is_pattern(data):
        if is_rest_var(data[-1]):
            name = data[-1][2:]
            if name[0] == '_':
                rest_var = contexts.anonymous(name)
            else:
                rest_var = contexts.variable(name)
            return pattern.pattern_tuple(
                tuple(as_pattern(element) for element in data[:-1]), rest_var)
        return pattern.pattern_tuple(
            tuple(as_pattern(element) for element in data))
    if isinstance(data, (str, )) and is_pattern(data):
        name = data[1:]
        if name[0] == '_': return contexts.anonymous(name)
        return contexts.variable(name)
    return pattern.pattern_literal(data)
def make_pattern(x):
    if isinstance(x, str):
        if x[0] == '$': return contexts.variable(x[1:])
        return pattern.pattern_literal(x)
    if isinstance(x, (tuple, list)):
        return pattern.pattern_tuple(
            tuple(make_pattern(element) for element in x))
    return pattern.pattern_literal(x)
Example #8
0
def make_pattern(x):
    if isinstance(x, types.StringTypes):
        if x[0] == "$":
            return contexts.variable(x[1:])
        return pattern.pattern_literal(x)
    if isinstance(x, (tuple, list)):
        return pattern.pattern_tuple(tuple(make_pattern(element) for element in x))
    return pattern.pattern_literal(x)
Example #9
0
def as_pattern(data):
    if isinstance(data, tuple) and is_pattern(data):
        if is_rest_var(data[-1]):
            name = data[-1][2:]
            if name[0] == '_':
                rest_var = contexts.anonymous(name)
            else:
                rest_var = contexts.variable(name)
            return pattern.pattern_tuple(tuple(as_pattern(element)
                                               for element in data[:-1]),
                                         rest_var)
        return pattern.pattern_tuple(tuple(as_pattern(element)
                                           for element in data))
    if isinstance(data, types.StringTypes) and is_pattern(data):
        name = data[1:]
        if name[0] == '_': return contexts.anonymous(name)
        return contexts.variable(name)
    return pattern.pattern_literal(data)
Example #10
0
def p_pattern_var(p):
    ''' variable : PATTERN_VAR_TOK
    '''
    global pattern_vars
    if goal_mode:
        pattern_vars.append(p[1])
        p[0] = contexts.variable(p[1])
    else:
        p[0] = "contexts.variable(%s)" % p[1]
Example #11
0
def p_pattern_var(p):
    ''' variable : PATTERN_VAR_TOK
    '''
    global pattern_vars
    if goal_mode:
        pattern_vars.append(p[1])
        p[0] = contexts.variable(p[1])
    else:
        p[0] = "contexts.variable(%s)" % p[1]
def populate(engine):
  This_rule_base = engine.get_create('off_state_rules')
  
  fc_rule.fc_rule('init_rule', This_rule_base, init_rule,
    (('off_state_questions', 'beep_question',
      (contexts.variable('ans'),),
      False),),
    (contexts.variable('ans'),))
  
  fc_rule.fc_rule('ram_yes_rule', This_rule_base, ram_yes_rule,
    (('off_state_facts', 'beep_answer',
      (pattern.pattern_literal(True),),
      False),),
    ())
  
  fc_rule.fc_rule('ram_no_rule', This_rule_base, ram_no_rule,
    (('off_state_facts', 'beep_answer',
      (pattern.pattern_literal(False),),
      False),),
    (pattern.pattern_literal('speaker'),))
  
  fc_rule.fc_rule('speaker_rule', This_rule_base, speaker_rule,
    (('off_state_facts', 'problem_from',
      (pattern.pattern_literal('speaker'),),
      False),
     ('off_state_questions', 'speaker_question',
      (contexts.variable('ans'),),
      False),),
    (contexts.variable('ans'),))
  
  fc_rule.fc_rule('speaker_yes_rule', This_rule_base, speaker_yes_rule,
    (('off_state_facts', 'speaker_answer',
      (pattern.pattern_literal(True),),
      False),
     ('off_state_questions', 'monitor_check_question',
      (contexts.variable('ans'),),
      False),),
    (contexts.variable('ans'),))
  
  fc_rule.fc_rule('speaker_no_rule', This_rule_base, speaker_no_rule,
    (('off_state_facts', 'speaker_answer',
      (pattern.pattern_literal(False),),
      False),),
    ())
  
  fc_rule.fc_rule('monitor_yes_rule', This_rule_base, monitor_yes_rule,
    (('off_state_facts', 'monitor_answer',
      (pattern.pattern_literal(True),),
      False),),
    ())
  
  fc_rule.fc_rule('monitor_no_rule', This_rule_base, monitor_no_rule,
    (('off_state_facts', 'monitor_answer',
      (pattern.pattern_literal(False),),
      False),),
    ())
Example #13
0
def populate(engine):
  This_rule_base = engine.get_create('deseases')
  
  bc_rule.bc_rule('dengue_package', This_rule_base, 'desease',
                  dengue_package, None,
                  (pattern.pattern_literal('dengue'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('dengue_not_chikungunha', This_rule_base, 'desease',
                  dengue_not_chikungunha, None,
                  (pattern.pattern_literal('dengue'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('zika_package', This_rule_base, 'desease',
                  zika_package, None,
                  (pattern.pattern_literal('zika'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('zika_not_chikungunha', This_rule_base, 'desease',
                  zika_not_chikungunha, None,
                  (pattern.pattern_literal('zika'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('zika_itching', This_rule_base, 'desease',
                  zika_itching, None,
                  (pattern.pattern_literal('zika'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('chikungunha_package', This_rule_base, 'desease',
                  chikungunha_package, None,
                  (pattern.pattern_literal('chikungunha'),
                   contexts.variable('u'),),
                  (),
                  ())
  
  bc_rule.bc_rule('chikungunha_not_dengue', This_rule_base, 'desease',
                  chikungunha_not_dengue, None,
                  (pattern.pattern_literal('chikungunha'),
                   contexts.variable('u'),),
                  (),
                  ())
def populate(engine):
    This_rule_base = engine.get_create('reasonable')

    fc_rule.fc_rule('reasonable', This_rule_base, reasonable,
                    (('uber', 'labels', (
                        contexts.variable('concept1'),
                        contexts.variable('concept2'),
                        contexts.variable('concept3'),
                    ), False), ), (
                        contexts.variable('concept1'),
                        contexts.variable('a'),
                        contexts.variable('concept2'),
                        contexts.variable('concept3'),
                    ))
Example #15
0
def populate(engine):
    This_rule_base = engine.get_create('example')

    bc_rule.bc_rule('half_siblings_same_father', This_rule_base,
                    'half_sibling', half_siblings_same_father, None, (
                        contexts.variable('af'),
                        contexts.variable('bf'),
                    ), (), (
                        contexts.variable('af'),
                        contexts.variable('father'),
                        contexts.variable('mother1'),
                        contexts.variable('bf'),
                        contexts.variable('mother2'),
                    ))
Example #16
0
def populate(engine):
  This_rule_base = engine.get_create('diagnose')
  
  fc_rule.fc_rule('collect_diagnosis', This_rule_base, collect_diagnosis,
    (('patient', 'feels',
      (contexts.variable('patient'),
       contexts.variable('symptom'),),
      False),
     ('choroby', 'symptom_of',
      (contexts.variable('symptom'),
       contexts.variable('disease'),),
      True),),
    (contexts.variable('diseases'),
     contexts.variable('patient'),))
def populate(engine):
    This_rule_base = engine.get_create('fc_area_recommend')

    fc_rule.fc_rule('move_01', This_rule_base, move_01, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p0'),
            contexts.variable('p1'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p1'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_02', This_rule_base, move_02, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p1'),
            contexts.variable('p2'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p2'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_03', This_rule_base, move_03, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p2'),
            contexts.variable('p3'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p3'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_04', This_rule_base, move_04, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p3'),
            contexts.variable('p4'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p4'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_05', This_rule_base, move_05, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p4'),
            contexts.variable('p5'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p5'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_06', This_rule_base, move_06, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p5'),
            contexts.variable('p6'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p6'),
        contexts.variable('status'),
    ))

    fc_rule.fc_rule('move_07', This_rule_base, move_07, (
        ('coil_area', 'coil_information', (
            contexts.variable('steel_kind'),
            contexts.variable('coil_kind'),
            contexts.variable('size'),
            contexts.variable('next_unit'),
        ), False),
        ('coil_area', 'coil_area_ruler', (
            contexts.variable('coil_kind'),
            contexts.variable('p6'),
            contexts.variable('p7'),
            contexts.variable('status'),
        ), False),
    ), (
        contexts.variable('coil_kind'),
        contexts.variable('p7'),
        contexts.variable('status'),
    ))
Example #18
0
class engine(object):
    _Variables = tuple(contexts.variable('ans_%d' % i) for i in range(100))

    def __init__(self, *search_paths, **kws):
        r'''All search_paths are relative to reference_path.

        Each search_path may be:

            path        -- a path relative to reference_path to search for
                           source files, placing the compiled knowledge bases
                           in '.compiled_krb'.
            module      -- the module's __file__ is taken as the path.
            (None|path|module, target_package)
                        -- use target_package rather than '.compiled_krb'.
                           This is a package name in Python dotted name
                           notation relative to path.  Use None to use the
                           compiled knowledge bases in the target_package
                           without scanning for source files.

        kws can be: load_fc, load_bc, load_fb and load_qb.  They are all
        boolean valued and default to True.
        '''

        # import this stuff here to avoid import cycles...
        global condensedPrint, pattern, fact_base, goal, rule_base, special, \
               target_pkg
        from pyke import (condensedPrint, pattern, fact_base, goal, rule_base,
                          special, target_pkg)

        for keyword in kws.keys():
            if keyword not in ('load_fc', 'load_bc', 'load_fb', 'load_qb'):
                raise TypeError("engine.__init__() got an unexpected keyword "
                                "argument %r" % keyword)
        self.knowledge_bases = {}
        self.rule_bases = {}
        special.create_for(self)

        if len(search_paths) == 1 and isinstance(search_paths[0], tuple) and \
           search_paths[0][0] == '*direct*' and \
           isinstance(search_paths[0][1], types.ModuleType):
            # secret hook for the compiler to initialize itself (so the
            # compiled python module can be in an egg).
            search_paths[0][1].populate(self)
        else:
            target_pkgs = {}  # {target_package_name: target_pkg}
            for path in search_paths:
                self._create_target_pkg(path, target_pkgs)
            for target_package in target_pkgs.values():
                if debug:
                    print("target_package:", target_package, file=sys.stderr)
                target_package.compile(self)
                target_package.write()
                target_package.load(self, **kws)
        for kb in self.knowledge_bases.values():
            kb.init2()
        for rb in self.rule_bases.values():
            rb.init2()

    def _create_target_pkg(self, path, target_pkgs):
        # Does target_pkg.add_source_package.

        if debug: print("engine._create_target_pkg:", path, file=sys.stderr)

        # First, figure out source_package_name, source_package_dir
        #               and target_package_name:
        target_package_name = '.compiled_krb'  # default
        if isinstance(path, (tuple, list)):
            path, target_package_name = path
        if isinstance(path, types.ModuleType):
            path = path.__file__
        if not isinstance(path, ((str, ), type(None))):
            raise ValueError("illegal path argument: string expected, got " + \
                               str(type(path)))

        if debug:
            print("_create_target_pkg path:", \
                                 repr(path), file=sys.stderr)
            print("_create_target_pkg target_package_name:", \
                                 repr(target_package_name), file=sys.stderr)

        # Handle the case where there are no source files (for a distributed
        # app that wants to hide its knowledge bases):
        if path is None:
            assert target_package_name[0] != '.', \
                   "engine: relative target, %s, illegal " \
                   "with no source package" % \
                       target_package_name
            if target_package_name not in target_pkgs:
                # This import must succeed!
                tp = _get_target_pkg(target_package_name +
                                     '.compiled_pyke_files')
                if tp is None:
                    raise AssertionError("%s: compiled with different version "
                                         "of Pyke" % target_package_name)
                tp.reset(check_sources=False)
                target_pkgs[target_package_name] = tp
            return

        path = os.path.normpath(os.path.abspath(path))

        path_to_package, source_package_name, remainder_path, zip_file_flag = \
          _pythonify_path(path)

        if debug:
            print("_create_target_pkg path to " \
                                   "_pythonify_path:", \
                                 repr(path), file=sys.stderr)
            print("    path_to_package:",
                  repr(path_to_package),
                  file=sys.stderr)
            print("    source_package_name:", \
                                 repr(source_package_name), file=sys.stderr)
            print("    remainder_path:", repr(remainder_path), file=sys.stderr)
            print("    zip_file_flag:", zip_file_flag, file=sys.stderr)

        target_filename = None

        # Convert relative target_package_name (if specified) to absolute form:
        if target_package_name[0] == '.':
            num_dots = \
                len(target_package_name) - len(target_package_name.lstrip('.'))
            if debug:
                print("_create_target_pkg num_dots:",
                      num_dots,
                      file=sys.stderr)
            if num_dots == 1:
                base_package = source_package_name
            else:
                base_package = \
                    '.'.join(source_package_name.split('.')[:-(num_dots - 1)])
            if base_package:
                target_package_name = \
                    base_package + '.' + target_package_name[num_dots:]
            else:
                target_package_name = target_package_name[num_dots:]

            target_filename = \
              os.path.join(path_to_package,
                           os.path.join(*target_package_name.split('.')),
                           'compiled_pyke_files.py')

            if debug:
                print("_create_target_pkg " \
                                       "absolute target_package_name:", \
                                     target_package_name, file=sys.stderr)

        if target_package_name in target_pkgs:
            tp = target_pkgs[target_package_name]
        else:
            target_name = target_package_name + '.compiled_pyke_files'
            if debug:
                print("_create_target_pkg target_name:", \
                                     target_name, file=sys.stderr)
            tp = None
            try:
                # See if compiled_pyke_files already exists.
                tp = _get_target_pkg(target_name)
            except ImportError:
                pass
            if tp is None:
                if debug:
                    print("_create_target_pkg: no target module",
                          file=sys.stderr)
                tp = target_pkg.target_pkg(target_name, target_filename)
            tp.reset()
            target_pkgs[target_package_name] = tp

        source_package_dir = \
          os.path.join(path_to_package,
                       os.path.join(*source_package_name.split('.')))
        if not os.path.isdir(source_package_dir):
            source_package_dir = os.path.dirname(source_package_dir)
            remainder_path = os.path.dirname(remainder_path)
        tp.add_source_package(source_package_name, remainder_path,
                              source_package_dir)

    def get_ask_module(self):
        if not hasattr(self, 'ask_module'):
            from pyke import ask_tty
            self.ask_module = ask_tty
        return self.ask_module

    def reset(self):
        r'''Erases all case-specific facts and deactivates all rule bases.
        '''
        for rb in self.rule_bases.values():
            rb.reset()
        for kb in self.knowledge_bases.values():
            kb.reset()

    def get_kb(self, kb_name, _new_class=None):
        ans = self.knowledge_bases.get(kb_name)
        if ans is None:
            if _new_class: ans = _new_class(self, kb_name)
            else: raise KeyError("knowledge_base %s not found" % kb_name)
        return ans

    def get_rb(self, rb_name):
        ans = self.rule_bases.get(rb_name)
        if ans is None: raise KeyError("rule_base %s not found" % rb_name)
        return ans

    def get_create(self, rb_name, parent=None, exclude_list=()):
        ans = self.rule_bases.get(rb_name)
        if ans is None:
            ans = rule_base.rule_base(self, rb_name, parent, exclude_list)
        elif ans.parent != parent or ans.exclude_set != frozenset(
                exclude_list):
            raise AssertionError("duplicate rule_base: %s" % rb_name)
        return ans

    def get_ke(self, kb_name, entity_name):
        return self.get_kb(kb_name).get_entity_list(entity_name)

    def add_universal_fact(self, kb_name, fact_name, args):
        r'''Universal facts are not deleted by engine.reset.
        '''
        if isinstance(args, (str, )):
            raise TypeError("engine.add_universal_fact: "
                            "illegal args type, %s" % type(args))
        args = tuple(args)
        return self.get_kb(kb_name, fact_base.fact_base) \
                   .add_universal_fact(fact_name, args)

    def add_case_specific_fact(self, kb_name, fact_name, args):
        r'''Case specific facts are deleted by engine.reset.
        '''
        if isinstance(args, (str, )):
            raise TypeError("engine.add_case_specific_fact: "
                            "illegal args type, %s" % type(args))
        args = tuple(args)
        return self.get_kb(kb_name, fact_base.fact_base) \
                   .add_case_specific_fact(fact_name, args)

    def assert_(self, kb_name, entity_name, args):
        if isinstance(args, (str, )):
            raise TypeError("engine.assert_: "
                            "illegal args type, %s" % type(args))
        args = tuple(args)
        return self.get_kb(kb_name, fact_base.fact_base) \
                   .assertTrue(entity_name, args)

    def activate(self, *rb_names):
        r'''Activate rule bases.

        This runs all forward-chaining rules in the activated rule bases, so
        add your facts before doing this!
        '''
        for rb_name in rb_names:
            self.get_rb(rb_name).activate()

    def lookup(self, kb_name, entity_name, pat_context, patterns):
        return self.get_kb(kb_name).lookup(pat_context, pat_context,
                                           entity_name, patterns)

    def prove_goal(self, goal_str, **args):
        r'''Proves goal_str with logic variables set to args.

        This returns a context manager that you use in a with statement:

            Ugly setup to use the family_relations example.  You can ignore
            this... :-(

            >>> source_dir = os.path.dirname(os.path.dirname(__file__))
            >>> family_relations_dir = \
            ...   os.path.join(source_dir, 'examples/family_relations')
            >>> sys.path.insert(0, family_relations_dir)
            >>> from pyke import knowledge_engine
            >>> my_engine = knowledge_engine.engine(family_relations_dir)

            >>> my_engine.activate('bc_example')

            OK, here's the example!

            >>> with my_engine.prove_goal(
            ...        'family.how_related($person1, $person2, $how_related)',
            ...        person1='bruce') as it:
            ...     for vars, plan in it:
            ...         print "bruce is related to", vars['person2'], "as", \
            ...               vars['how_related']

        vars is a dictionary of all of the logic variables in the goal
        (without the '$') and their values.  The plan is a callable python
        function.

        If you only want the first answer, see engine.prove_1_goal.
        '''
        return goal.compile(goal_str).prove(self, **args)

    def prove_1_goal(self, goal_str, **args):
        r'''Proves goal_str with logic variables set to args.

        Returns the vars and plan for the first solution found.  Raises
        knowledge_engine.CanNotProve if no solutions are found.

            Ugly setup to use the family_relations example.  You can ignore
            this... :-(

            >>> source_dir = os.path.dirname(os.path.dirname(__file__))
            >>> family_relations_dir = \
            ...   os.path.join(source_dir, 'examples/family_relations')
            >>> sys.path.insert(0, family_relations_dir)
            >>> from pyke import knowledge_engine
            >>> my_engine = knowledge_engine.engine(family_relations_dir)

            >>> my_engine.activate('bc_example')

            OK, here's the example!

            >>> vars, plan = \
            ...   my_engine.prove_1_goal(
            ...     'bc_example.how_related($person1, $person2, $how_related)',
            ...     person1='bruce',
            ...     person2='m_thomas')
            >>> print "bruce is related to m_thomas as", vars['how_related']
            bruce is related to m_thomas as ('father', 'son')

        If you want more than one answer, see engine.prove_goal.
        '''
        return goal.compile(goal_str).prove_1(self, **args)

    def prove(self, kb_name, entity_name, pat_context, patterns):
        r'''Deprecated.  Use engine.prove_goal.
        '''
        return self.get_kb(kb_name).prove(pat_context, pat_context,
                                          entity_name, patterns)

    def prove_n(self, kb_name, entity_name, fixed_args=(), num_returns=0):
        '''Returns a context manager for a generator of:
               a tuple of len == num_returns, and a plan (or None).

        Deprecated.  Use engine.prove_goal.
        '''
        if isinstance(fixed_args, (str, )):
            raise TypeError("engine.prove_n: fixed_args must not be a string, "
                            "did you forget a , (%(arg)s) => (%(arg)s,)?" %
                            {'arg': repr(fixed_args)})

        def gen():
            context = contexts.simple_context()
            vars = self._Variables[:num_returns]
            try:
                with self.prove(kb_name, entity_name, context,
                                tuple(pattern.pattern_literal(arg)
                                      for arg in fixed_args) + vars) \
                  as it:
                    for plan in it:
                        final = {}
                        ans = tuple(
                            context.lookup_data(var.name, final=final)
                            for var in vars)
                        if plan: plan = plan.create_plan(final)
                        yield ans, plan
            finally:
                context.done()

        return contextlib.closing(gen())

    def prove_1(self, kb_name, entity_name, fixed_args=(), num_returns=0):
        r'''Returns a tuple of len == num_returns, and a plan (or None).

        Deprecated.  Use engine.prove_1_goal.
        '''
        try:
            # All we need is the first one!
            with self.prove_n(kb_name, entity_name, fixed_args, num_returns) \
              as it:
                return next(iter(it))
        except StopIteration:
            raise CanNotProve(
                "Can not prove %s.%s%s" %
                (kb_name, entity_name,
                 condensedPrint.cprint(fixed_args +
                                       self._Variables[:num_returns])))

    def print_stats(self, f=sys.stdout):
        for kb \
         in sorted(iter(self.knowledge_bases.values()), key=lambda kb: kb.name):
            kb.print_stats(f)

    def trace(self, rb_name, rule_name):
        self.get_rb(rb_name).trace(rule_name)

    def untrace(self, rb_name, rule_name):
        self.get_rb(rb_name).untrace(rule_name)
Example #19
0
def populate(engine):
    This_rule_base = engine.get_create('fb_checkin_fc')

    fc_rule.fc_rule('prior', This_rule_base, prior, (('fb_checkin', 'prior', (
        contexts.variable('node'),
        contexts.variable('value'),
    ), False), ), (
        contexts.variable('node'),
        pattern.pattern_literal(0),
    ))

    fc_rule.fc_rule('inc_num_samples', This_rule_base, inc_num_samples,
                    (('fb_checkin', 'inc_num_samples', (
                        contexts.variable('node'),
                        contexts.variable('value'),
                        contexts.variable('prob'),
                    ), False), ), (
                        contexts.variable('node'),
                        contexts.variable('value'),
                        pattern.pattern_literal(0),
                    ))

    fc_rule.fc_rule('inc_num_features', This_rule_base, inc_num_features,
                    (('fb_checkin', 'inc_num_features', (
                        contexts.variable('node'),
                        contexts.variable('value'),
                        contexts.variable('prob'),
                    ), False), ), (
                        contexts.variable('node'),
                        contexts.variable('value'),
                        pattern.pattern_literal(0),
                    ))

    fc_rule.fc_rule('accuracy', This_rule_base, accuracy,
                    (('fb_checkin', 'accuracy',
                      (contexts.variable('new_value'), ), False), ),
                    (contexts.variable('new_value'), ))

    fc_rule.fc_rule('sample_size', This_rule_base, sample_size,
                    (('fb_checkin', 'sample_size', (
                        contexts.variable('node'),
                        contexts.variable('value'),
                    ), False), ), (
                        contexts.variable('node'),
                        contexts.variable('value'),
                    ))

    fc_rule.fc_rule('feature', This_rule_base, feature,
                    (('fb_checkin', 'feature', (
                        contexts.variable('node'),
                        contexts.variable('value'),
                    ), False), ), (
                        contexts.variable('node'),
                        contexts.variable('value'),
                    ))

    fc_rule.fc_rule('threshold', This_rule_base, threshold,
                    (('fb_checkin', 'threshold',
                      (contexts.variable('value'), ), False), ),
                    (contexts.variable('value'), ))
Example #20
0
def populate(engine):
    This_rule_base = engine.get_create('example')

    fc_rule.fc_rule('son_of', This_rule_base, son_of, (('family', 'son_of', (
        contexts.variable('child'),
        contexts.variable('father'),
        contexts.variable('mother'),
    ), False), ), (
        contexts.variable('child'),
        contexts.variable('father'),
        pattern.pattern_literal('father'),
        pattern.pattern_literal('son'),
        contexts.variable('mother'),
        pattern.pattern_literal('mother'),
    ))

    fc_rule.fc_rule('daughter_of', This_rule_base, daughter_of,
                    (('family', 'daughter_of', (
                        contexts.variable('child'),
                        contexts.variable('father'),
                        contexts.variable('mother'),
                    ), False), ), (
                        contexts.variable('child'),
                        contexts.variable('father'),
                        pattern.pattern_literal('father'),
                        pattern.pattern_literal('daughter'),
                        contexts.variable('mother'),
                        pattern.pattern_literal('mother'),
                    ))

    fc_rule.fc_rule('brothers', This_rule_base, brothers, (
        ('family', 'son_of', (
            contexts.variable('brother1'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
        ('family', 'son_of', (
            contexts.variable('brother2'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
    ), (
        contexts.variable('brother1'),
        contexts.variable('brother2'),
        pattern.pattern_literal('brother'),
    ))

    fc_rule.fc_rule('sisters', This_rule_base, sisters, (
        ('family', 'daughter_of', (
            contexts.variable('sister1'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
        ('family', 'daughter_of', (
            contexts.variable('sister2'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
    ), (
        contexts.variable('sister1'),
        contexts.variable('sister2'),
        pattern.pattern_literal('sister'),
    ))

    fc_rule.fc_rule('brother_and_sister', This_rule_base, brother_and_sister, (
        ('family', 'son_of', (
            contexts.variable('brother'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
        ('family', 'daughter_of', (
            contexts.variable('sister'),
            contexts.variable('father'),
            contexts.variable('mother'),
        ), False),
    ), (
        contexts.variable('brother'),
        contexts.variable('sister'),
        pattern.pattern_literal('sister'),
        pattern.pattern_literal('brother'),
    ))

    fc_rule.fc_rule('facts_for_bc_rules', This_rule_base, facts_for_bc_rules,
                    (), (
                        pattern.pattern_literal('brother'),
                        pattern.pattern_literal('uncle'),
                        pattern.pattern_literal('sister'),
                        pattern.pattern_literal('aunt'),
                        pattern.pattern_literal('son'),
                        pattern.pattern_literal('nephew'),
                        pattern.pattern_literal('daughter'),
                        pattern.pattern_literal('niece'),
                    ))
Example #21
0
def populate(engine):
    This_rule_base = engine.get_create('fc_rules')

    fc_rule.fc_rule('gender', This_rule_base, gender, (
        ('famous', 'knownFor', (
            contexts.variable('person'),
            contexts.variable('status'),
        ), False),
        ('famous', 'gender', (
            contexts.variable('person'),
            contexts.variable('gender'),
        ), True),
    ), (
        contexts.variable('person'),
        pattern.pattern_literal('male'),
    ))

    fc_rule.fc_rule('dictator', This_rule_base, dictator, (
        ('famous', 'rules', (
            contexts.variable('person'),
            contexts.variable('nation'),
        ), False),
        ('famous', 'free_elections', (contexts.variable('nation'), ), True),
    ), (contexts.variable('person'), ))

    fc_rule.fc_rule('freely_elected', This_rule_base, freely_elected, (
        ('famous', 'rules', (
            contexts.variable('person'),
            contexts.variable('nation'),
        ), False),
        ('famous', 'free_elections', (contexts.variable('nation'), ), False),
    ), (contexts.variable('person'), ))

    fc_rule.fc_rule('president', This_rule_base, president, (
        ('famous', 'rules', (
            contexts.variable('person'),
            contexts.variable('nation'),
        ), False),
        ('famous', 'title', (
            contexts.variable('person'),
            contexts.variable('title'),
        ), True),
    ), (
        contexts.variable('person'),
        pattern.pattern_literal('president'),
    ))

    fc_rule.fc_rule('allies', This_rule_base, allies, (
        ('famous', 'allies', (
            contexts.variable('nation1'),
            contexts.variable('nation2'),
        ), False),
        ('famous', 'rules', (
            contexts.variable('person1'),
            contexts.variable('nation1'),
        ), False),
        ('famous', 'rules', (
            contexts.variable('person2'),
            contexts.variable('nation2'),
        ), False),
    ), (
        contexts.variable('person1'),
        contexts.variable('nation2'),
        contexts.variable('person2'),
        contexts.variable('nation1'),
    ))

    fc_rule.fc_rule('children', This_rule_base, children, (
        ('famous', 'famousFor', (
            contexts.variable('person'),
            contexts.variable('skill'),
        ), False),
        ('famous', 'children', (
            contexts.variable('person'),
            contexts.variable('yesno'),
        ), True),
    ), (
        contexts.variable('person'),
        pattern.pattern_literal('No'),
    ))

    fc_rule.fc_rule('race', This_rule_base, race, (
        ('famous', 'famousFor', (
            contexts.variable('person'),
            contexts.variable('skill'),
        ), False),
        ('famous', 'race', (
            contexts.variable('person'),
            contexts.variable('race'),
        ), True),
    ), (
        contexts.variable('person'),
        pattern.pattern_literal('Caucasian'),
    ))

    fc_rule.fc_rule('princess', This_rule_base, princess,
                    (('famous', 'princess',
                      (contexts.variable('person'), ), False), ), (
                          contexts.variable('person'),
                          pattern.pattern_literal('female'),
                      ))
Example #22
0
def populate(engine):
  This_rule_base = engine.get_create('fc_example')
  
  fc_rule.fc_rule('son_of', This_rule_base, son_of,
    (('family', 'son_of',
      (contexts.variable('child'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),),
    (contexts.variable('child'),
     contexts.variable('father'),
     pattern.pattern_literal('father'),
     pattern.pattern_literal('son'),
     contexts.variable('mother'),
     pattern.pattern_literal('mother'),))
  
  fc_rule.fc_rule('daughter_of', This_rule_base, daughter_of,
    (('family', 'daughter_of',
      (contexts.variable('child'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),),
    (contexts.variable('child'),
     contexts.variable('father'),
     pattern.pattern_literal('father'),
     pattern.pattern_literal('daughter'),
     contexts.variable('mother'),
     pattern.pattern_literal('mother'),))
  
  fc_rule.fc_rule('brothers', This_rule_base, brothers,
    (('family', 'son_of',
      (contexts.variable('brother1'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),
     ('family', 'son_of',
      (contexts.variable('brother2'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),),
    (contexts.variable('brother1'),
     contexts.variable('brother2'),
     pattern.pattern_literal('brother'),))
  
  fc_rule.fc_rule('sisters', This_rule_base, sisters,
    (('family', 'daughter_of',
      (contexts.variable('sister1'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),
     ('family', 'daughter_of',
      (contexts.variable('sister2'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),),
    (contexts.variable('sister1'),
     contexts.variable('sister2'),
     pattern.pattern_literal('sister'),))
  
  fc_rule.fc_rule('brother_and_sister', This_rule_base, brother_and_sister,
    (('family', 'son_of',
      (contexts.variable('brother'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),
     ('family', 'daughter_of',
      (contexts.variable('sister'),
       contexts.variable('father'),
       contexts.variable('mother'),),
      False),),
    (contexts.variable('brother'),
     contexts.variable('sister'),
     pattern.pattern_literal('sister'),
     pattern.pattern_literal('brother'),))
  
  fc_rule.fc_rule('facts_for_bc_rules', This_rule_base, facts_for_bc_rules,
    (),
    (pattern.pattern_literal('brother'),
     pattern.pattern_literal('uncle'),
     pattern.pattern_literal('sister'),
     pattern.pattern_literal('aunt'),
     pattern.pattern_literal('son'),
     pattern.pattern_literal('nephew'),
     pattern.pattern_literal('daughter'),
     pattern.pattern_literal('niece'),))
  
  fc_rule.fc_rule('niece_or_nephew_and_aunt_or_uncle', This_rule_base, niece_or_nephew_and_aunt_or_uncle,
    (('family', 'child_parent',
      (contexts.variable('nn'),
       contexts.variable('parent'),
       contexts.variable('depth'),
       contexts.anonymous('_'),
       contexts.variable('child_type'),),
      False),
     ('family', 'siblings',
      (contexts.variable('parent'),
       contexts.variable('au'),
       contexts.variable('sibling_type'),
       contexts.anonymous('_'),),
      False),
     ('family', 'as_au',
      (contexts.variable('sibling_type'),
       contexts.variable('au_type'),),
      False),
     ('family', 'as_nn',
      (contexts.variable('child_type'),
       contexts.variable('nn_type'),),
      False),),
    (contexts.variable('greats'),
     contexts.variable('nn'),
     contexts.variable('au'),
     contexts.variable('au_type'),
     contexts.variable('nn_type'),))
  
  fc_rule.fc_rule('parent_and_child', This_rule_base, parent_and_child,
    (('family', 'child_parent',
      (contexts.variable('child'),
       contexts.variable('parent'),
       contexts.variable('parent_type'),
       contexts.variable('child_type'),),
      False),),
    (contexts.variable('child'),
     contexts.variable('parent'),
     pattern.pattern_literal(()),
     contexts.variable('parent_type'),
     contexts.variable('child_type'),))
  
  fc_rule.fc_rule('grand_parent_and_child', This_rule_base, grand_parent_and_child,
    (('family', 'child_parent',
      (contexts.variable('child'),
       contexts.variable('parent'),
       contexts.anonymous('_'),
       contexts.variable('child_type'),),
      False),
     ('family', 'child_parent',
      (contexts.variable('parent'),
       contexts.variable('grand_parent'),
       contexts.variable('parent_type'),
       contexts.anonymous('_'),),
      False),),
    (contexts.variable('child'),
     contexts.variable('grand_parent'),
     pattern.pattern_literal(('grand',)),
     contexts.variable('parent_type'),
     contexts.variable('child_type'),))
  
  fc_rule.fc_rule('great_grand_parent_and_child', This_rule_base, great_grand_parent_and_child,
    (('family', 'child_parent',
      (contexts.variable('child'),
       contexts.variable('grand_child'),
       contexts.anonymous('_'),
       contexts.variable('child_type'),),
      False),
     ('family', 'child_parent',
      (contexts.variable('grand_child'),
       contexts.variable('grand_parent'),
       pattern.pattern_tuple((contexts.variable('a'),), contexts.variable('b')),
       contexts.variable('parent_type'),
       contexts.anonymous('_'),),
      False),),
    (contexts.variable('child'),
     contexts.variable('grand_parent'),
     pattern.pattern_tuple((pattern.pattern_literal('great'), contexts.variable('a'),), contexts.variable('b')),
     contexts.variable('parent_type'),
     contexts.variable('child_type'),))
  
  fc_rule.fc_rule('first_cousins', This_rule_base, first_cousins,
    (('family', 'child_parent',
      (contexts.variable('cousin1'),
       contexts.variable('sibling1'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),
     ('family', 'siblings',
      (contexts.variable('sibling1'),
       contexts.variable('sibling2'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),
     ('family', 'child_parent',
      (contexts.variable('cousin2'),
       contexts.variable('sibling2'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),),
    (contexts.variable('cousin1'),
     contexts.variable('cousin2'),
     pattern.pattern_literal(1),))
  
  fc_rule.fc_rule('nth_cousins', This_rule_base, nth_cousins,
    (('family', 'child_parent',
      (contexts.variable('next_cousin1'),
       contexts.variable('cousin1'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),
     ('family', 'cousins',
      (contexts.variable('cousin1'),
       contexts.variable('cousin2'),
       contexts.variable('n'),),
      False),
     ('family', 'child_parent',
      (contexts.variable('next_cousin2'),
       contexts.variable('cousin2'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),),
    (contexts.variable('next_n'),
     contexts.variable('next_cousin1'),
     contexts.variable('next_cousin2'),))
  
  fc_rule.fc_rule('how_related_child_parent', This_rule_base, how_related_child_parent,
    (('family', 'child_parent',
      (contexts.variable('person1'),
       contexts.variable('person2'),
       contexts.variable('prefix'),
       contexts.variable('p2_type'),
       contexts.variable('p1_type'),),
      False),),
    (contexts.variable('relationship'),
     contexts.variable('person1'),
     contexts.variable('person2'),))
  
  fc_rule.fc_rule('how_related_parent_child', This_rule_base, how_related_parent_child,
    (('family', 'child_parent',
      (contexts.variable('person2'),
       contexts.variable('person1'),
       contexts.variable('prefix'),
       contexts.variable('p1_type'),
       contexts.variable('p2_type'),),
      False),),
    (contexts.variable('relationship'),
     contexts.variable('person1'),
     contexts.variable('person2'),))
  
  fc_rule.fc_rule('how_related_siblings', This_rule_base, how_related_siblings,
    (('family', 'siblings',
      (contexts.variable('person1'),
       contexts.variable('person2'),
       contexts.variable('p2_type'),
       contexts.variable('p1_type'),),
      False),),
    (contexts.variable('person1'),
     contexts.variable('person2'),
     pattern.pattern_tuple((contexts.variable('p1_type'), contexts.variable('p2_type'),), None),))
  
  fc_rule.fc_rule('how_related_nn_au', This_rule_base, how_related_nn_au,
    (('family', 'nn_au',
      (contexts.variable('person1'),
       contexts.variable('person2'),
       contexts.variable('prefix'),
       contexts.variable('p2_type'),
       contexts.variable('p1_type'),),
      False),),
    (contexts.variable('relationship'),
     contexts.variable('person1'),
     contexts.variable('person2'),))
  
  fc_rule.fc_rule('how_related_au_nn', This_rule_base, how_related_au_nn,
    (('family', 'nn_au',
      (contexts.variable('person2'),
       contexts.variable('person1'),
       contexts.variable('prefix'),
       contexts.variable('p1_type'),
       contexts.variable('p2_type'),),
      False),),
    (contexts.variable('relationship'),
     contexts.variable('person1'),
     contexts.variable('person2'),))
  
  fc_rule.fc_rule('how_related_cousins', This_rule_base, how_related_cousins,
    (('family', 'cousins',
      (contexts.variable('cousin1'),
       contexts.variable('cousin2'),
       contexts.variable('n'),),
      False),),
    (contexts.variable('nth'),
     contexts.variable('cousin1'),
     contexts.variable('cousin2'),
     pattern.pattern_tuple((contexts.variable('nth'), pattern.pattern_literal('cousins'),), None),))
  
  fc_rule.fc_rule('how_related_removed_cousins', This_rule_base, how_related_removed_cousins,
    (('family', 'child_parent',
      (contexts.variable('removed_cousin1'),
       contexts.variable('cousin1'),
       contexts.variable('grand'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),
     ('family', 'cousins',
      (contexts.variable('cousin1'),
       contexts.variable('cousin2'),
       contexts.variable('n'),),
      False),),
    (contexts.variable('nth'),
     contexts.variable('r1'),
     contexts.variable('removed_cousin1'),
     contexts.variable('cousin2'),
     pattern.pattern_tuple((contexts.variable('nth'), pattern.pattern_literal('cousins'), contexts.variable('r1'), pattern.pattern_literal('removed'),), None),))
  
  fc_rule.fc_rule('how_related_cousins_removed', This_rule_base, how_related_cousins_removed,
    (('family', 'cousins',
      (contexts.variable('cousin1'),
       contexts.variable('cousin2'),
       contexts.variable('n'),),
      False),
     ('family', 'child_parent',
      (contexts.variable('removed_cousin2'),
       contexts.variable('cousin2'),
       contexts.variable('grand'),
       contexts.anonymous('_'),
       contexts.anonymous('_'),),
      False),),
    (contexts.variable('nth'),
     contexts.variable('r1'),
     contexts.variable('cousin1'),
     contexts.variable('removed_cousin2'),
     pattern.pattern_tuple((contexts.variable('nth'), pattern.pattern_literal('cousins'), contexts.variable('r1'), pattern.pattern_literal('removed'),), None),))
Example #23
0
def populate(engine):
  This_rule_base = engine.get_create('subway_rules')
  
  bc_rule.bc_rule('at_station', This_rule_base, 'at_station',
                  at_station, None,
                  (contexts.variable('station'),),
                  (),
                  (contexts.variable('from'),
                   contexts.variable('line'),
                   contexts.variable('station'),
                   pattern.pattern_tuple((contexts.variable('line'),), None),
                   contexts.anonymous('_'),))
  
  bc_rule.bc_rule('travel_on_same_line', This_rule_base, 'take_line',
                  travel_on_same_line, None,
                  (contexts.variable('from'),
                   contexts.variable('line'),
                   contexts.variable('to'),),
                  (),
                  (contexts.variable('from'),
                   pattern.pattern_tuple((contexts.variable('line'),), None),
                   contexts.anonymous('_'),
                   contexts.variable('to'),))
  
  bc_rule.bc_rule('connected_stations', This_rule_base, 'connected',
                  connected_stations, None,
                  (contexts.variable('from'),
                   contexts.variable('to'),),
                  (),
                  (contexts.variable('from'),
                   contexts.variable('line'),
                   contexts.anonymous('_'),
                   contexts.variable('to'),))
Example #24
0
def populate(engine):
    This_rule_base = engine.get_create('Remedyrule')

    bc_rule.bc_rule('get', This_rule_base, 'get', get, None, (
        contexts.variable('fault'),
        contexts.variable('actualfault'),
        contexts.variable('remedy'),
    ), (), (
        contexts.variable('responsefault'),
        contexts.variable('fault'),
        contexts.variable('VMfault'),
        contexts.variable('PMfault'),
        contexts.variable('finalfault'),
        contexts.variable('actualfault'),
        contexts.variable('remedy'),
    ))

    bc_rule.bc_rule('arr', This_rule_base, 'ar', arr, None, (
        contexts.variable('responsefault'),
        contexts.variable('fault'),
    ), (), (
        contexts.variable('resp'),
        contexts.variable('responsefault'),
    ))

    bc_rule.bc_rule('VM', This_rule_base, 'VM', VM, None, (
        contexts.variable('VMfault'),
        contexts.variable('fault'),
    ), (), (
        contexts.variable('ft'),
        contexts.variable('VMfault'),
    ))

    bc_rule.bc_rule('PM', This_rule_base, 'PM', PM, None, (
        contexts.variable('PMfault'),
        contexts.variable('fault'),
    ), (), (
        contexts.variable('pt'),
        contexts.variable('PMfault'),
    ))
def populate(engine):
  This_rule_base = engine.get_create('category_rules')
  
  bc_rule.bc_rule('ordered_rule0', This_rule_base, 'top2',
                  ordered_rule0, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('d'),
                   pattern.pattern_literal(2),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(4),
                   pattern.pattern_literal(11),))
  
  bc_rule.bc_rule('ordered_rule1', This_rule_base, 'top2',
                  ordered_rule1, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('d'),
                   pattern.pattern_literal(4),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(6),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule2', This_rule_base, 'top2',
                  ordered_rule2, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('dining'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('d'),
                   pattern.pattern_literal(5),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(9),
                   pattern.pattern_literal('a'),))
  
  bc_rule.bc_rule('ordered_rule3', This_rule_base, 'top2',
                  ordered_rule3, None,
                  (pattern.pattern_literal('relaxation'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(6),))
  
  bc_rule.bc_rule('ordered_rule4', This_rule_base, 'top2',
                  ordered_rule4, None,
                  (pattern.pattern_literal('outdoor'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   contexts.variable('num'),
                   pattern.pattern_literal(8),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(13),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule5', This_rule_base, 'top2',
                  ordered_rule5, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('dining'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(4),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(11),))
  
  bc_rule.bc_rule('ordered_rule6', This_rule_base, 'top2',
                  ordered_rule6, None,
                  (pattern.pattern_literal('relaxation'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   contexts.variable('num'),
                   pattern.pattern_literal(10),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule7', This_rule_base, 'top2',
                  ordered_rule7, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('dining'),),
                  (),
                  (pattern.pattern_literal(6),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(7),
                   pattern.pattern_literal(9),
                   pattern.pattern_literal('a'),))
  
  bc_rule.bc_rule('ordered_rule8', This_rule_base, 'top2',
                  ordered_rule8, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('cultural'),),
                  (),
                  (pattern.pattern_literal(1),
                   contexts.variable('num'),
                   pattern.pattern_literal(2),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(3),))
  
  bc_rule.bc_rule('ordered_rule9', This_rule_base, 'top2',
                  ordered_rule9, None,
                  (pattern.pattern_literal('outdoor'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(2),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(3),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(6),))
  
  bc_rule.bc_rule('ordered_rule10', This_rule_base, 'top2',
                  ordered_rule10, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(7),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(13),))
  
  bc_rule.bc_rule('ordered_rule11', This_rule_base, 'top2',
                  ordered_rule11, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('relaxation'),),
                  (),
                  (pattern.pattern_literal(4),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(6),
                   pattern.pattern_literal(12),))
  
  bc_rule.bc_rule('ordered_rule12', This_rule_base, 'top2',
                  ordered_rule12, None,
                  (pattern.pattern_literal('dining'),
                   pattern.pattern_literal('relaxation'),),
                  (),
                  (pattern.pattern_literal(5),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(10),))
  
  bc_rule.bc_rule('ordered_rule13', This_rule_base, 'top2',
                  ordered_rule13, None,
                  (pattern.pattern_literal('outdoor'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(6),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule14', This_rule_base, 'top2',
                  ordered_rule14, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(1),
                   contexts.variable('num'),
                   pattern.pattern_literal(3),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(7),
                   pattern.pattern_literal('a'),))
  
  bc_rule.bc_rule('ordered_rule15', This_rule_base, 'top2',
                  ordered_rule15, None,
                  (pattern.pattern_literal('dining'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(7),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(10),))
  
  bc_rule.bc_rule('ordered_rule16', This_rule_base, 'top2',
                  ordered_rule16, None,
                  (pattern.pattern_literal('relaxation'),
                   pattern.pattern_literal('nature'),),
                  (),
                  (pattern.pattern_literal(4),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(13),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule17', This_rule_base, 'top2',
                  ordered_rule17, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('relaxation'),),
                  (),
                  (pattern.pattern_literal(1),
                   contexts.variable('num'),
                   pattern.pattern_literal(7),
                   pattern.pattern_literal('b'),
                   pattern.pattern_literal(13),))
  
  bc_rule.bc_rule('ordered_rule18', This_rule_base, 'top2',
                  ordered_rule18, None,
                  (pattern.pattern_literal('dining'),
                   pattern.pattern_literal('relaxation'),),
                  (),
                  (pattern.pattern_literal(2),
                   pattern.pattern_literal('a'),
                   pattern.pattern_literal(9),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule19', This_rule_base, 'top2',
                  ordered_rule19, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('outdoor'),),
                  (),
                  (pattern.pattern_literal(2),
                   pattern.pattern_literal('b'),))
  
  bc_rule.bc_rule('ordered_rule20', This_rule_base, 'top2',
                  ordered_rule20, None,
                  (pattern.pattern_literal('dining'),
                   pattern.pattern_literal('relaxation'),),
                  (),
                  (pattern.pattern_literal(1),
                   pattern.pattern_literal('a'),))
  
  bc_rule.bc_rule('ordered_rule21', This_rule_base, 'top2',
                  ordered_rule21, None,
                  (pattern.pattern_literal('sights'),
                   pattern.pattern_literal('nature'),),
                  (),
                  ())
Example #26
0
def populate(engine):
  This_rule_base = engine.get_create('fc_pcb')
  
  fc_rule.fc_rule('hf', This_rule_base, hf,
    (('pcb', 'is_hf',
      (contexts.variable('net'),),
      False),),
    (contexts.variable('net'),))
  
  fc_rule.fc_rule('needs_no_hf', This_rule_base, needs_no_hf,
    (('pcb', 'is_not_hf',
      (contexts.variable('net'),),
      False),),
    (contexts.variable('net'),))
  
  fc_rule.fc_rule('large_budget_1', This_rule_base, large_budget_1,
    (('pcb', 'is_short_timeline',
      (contexts.variable('circuit'),),
      False),),
    (contexts.variable('circuit'),))
  
  fc_rule.fc_rule('large_budget_2', This_rule_base, large_budget_2,
    (('pcb', 'is_critical',
      (contexts.variable('circuit'),),
      False),),
    (contexts.variable('circuit'),))
  
  fc_rule.fc_rule('recommend__material_1', This_rule_base, recommend__material_1,
    (('pcb', 'needs_hf_mat',
      (contexts.variable('net'),),
      False),
     ('pcb', 'needs_large_budget',
      (contexts.variable('circuit'),),
      False),
     ('pcb', 'is_hf_mat',
      (contexts.variable('material'),),
      False),
     ('pcb', 'is_expensive_mat',
      (contexts.variable('material'),),
      False),),
    (contexts.variable('net'),
     contexts.variable('material'),))
  
  fc_rule.fc_rule('recommend__material_2', This_rule_base, recommend__material_2,
    (('pcb', 'needs_hf_mat',
      (contexts.variable('net'),),
      False),
     ('pcb', 'is_hf_mat',
      (contexts.variable('material'),),
      False),),
    (contexts.variable('net'),
     contexts.variable('material'),))
  
  fc_rule.fc_rule('recommend__material_3', This_rule_base, recommend__material_3,
    (('pcb', 'needs_no_hf_mat',
      (contexts.variable('net'),),
      False),
     ('pcb', 'is_not_hf_mat',
      (contexts.variable('material'),),
      False),),
    (contexts.variable('net'),
     contexts.variable('material'),))
  
  fc_rule.fc_rule('gnd_separation', This_rule_base, gnd_separation,
    (('pcb', 'requires_isolation',
      (contexts.variable('net1'),
       contexts.variable('net2'),),
      False),),
    (contexts.variable('net1'),))
Example #27
0
def populate(engine):
    This_rule_base = engine.get_create('rules')

    bc_rule.bc_rule('diagnostico', This_rule_base, 'diagnostico', diagnostico,
                    None, (contexts.variable('resultado'), ), (), (
                        contexts.variable('result1'),
                        contexts.variable('hipertrofiaganglionar'),
                        contexts.variable('discrasia'),
                        contexts.variable('resultado'),
                    ))

    bc_rule.bc_rule('diagnostico1', This_rule_base, 'diagnostico1',
                    diagnostico1, None, (contexts.variable('result1'), ), (), (
                        contexts.variable('febre2'),
                        contexts.variable('mancha2'),
                        contexts.variable('dormuscular'),
                        contexts.variable('articular2'),
                        contexts.variable('conjuntivite'),
                        contexts.variable('edemartic'),
                        contexts.variable('dordecabeca'),
                        contexts.variable('acometimentoneurologico'),
                        contexts.variable('coceiraa'),
                        contexts.variable('result1'),
                    ))

    bc_rule.bc_rule('int_febre', This_rule_base, 'int_febre', int_febre, None,
                    (contexts.variable('intfebre'), ), (), (
                        contexts.variable('ans_febre2'),
                        contexts.variable('intfebre'),
                    ))

    bc_rule.bc_rule('int_mancha', This_rule_base, 'int_mancha', int_mancha,
                    None, (contexts.variable('intmancha'), ), (), (
                        contexts.variable('ans_mancha2'),
                        contexts.variable('intmancha'),
                    ))

    bc_rule.bc_rule('int_dormuscular', This_rule_base, 'int_dormuscular',
                    int_dormuscular, None,
                    (contexts.variable('intdormuscular'), ), (), (
                        contexts.variable('ans_dor_musc'),
                        contexts.variable('intdormuscular'),
                    ))

    bc_rule.bc_rule('freq_dorarticular', This_rule_base, 'freq_dorarticular',
                    freq_dorarticular, None,
                    (contexts.variable('intdorarticular'), ), (), (
                        contexts.variable('ans_dor_art2'),
                        contexts.variable('intdorarticular'),
                    ))

    bc_rule.bc_rule('tem_conjuntivite', This_rule_base, 'tem_conjuntivite',
                    tem_conjuntivite, None,
                    (contexts.variable('temconjuntivite'), ), (), (
                        contexts.variable('ans_conjuntivite'),
                        contexts.variable('temconjuntivite'),
                    ))

    bc_rule.bc_rule('int_edemaart', This_rule_base, 'int_edemaart',
                    int_edemaart, None, (contexts.variable('intedemart'), ),
                    (), (
                        contexts.variable('ans_edemart'),
                        contexts.variable('intedemart'),
                    ))

    bc_rule.bc_rule('int_dorcabeca', This_rule_base, 'int_dorcabeca',
                    int_dorcabeca, None, (contexts.variable('intdorcabeca'), ),
                    (), (
                        contexts.variable('ans_dorcabeca'),
                        contexts.variable('intdorcabeca'),
                    ))

    bc_rule.bc_rule('int_coceira', This_rule_base, 'int_coceira', int_coceira,
                    None, (contexts.variable('intcoceira'), ), (), (
                        contexts.variable('ans_coceira'),
                        contexts.variable('intcoceira'),
                    ))

    bc_rule.bc_rule('int_acrometimentoNeurologico', This_rule_base,
                    'int_acrometimentoNeurologico',
                    int_acrometimentoNeurologico, None,
                    (contexts.variable('intacrometimentoNeurologico'), ), (), (
                        contexts.variable('ans_acrometimentoNeurologico'),
                        contexts.variable('intacrometimentoNeurologico'),
                    ))

    bc_rule.bc_rule('int_hipertrofiaganglionar', This_rule_base,
                    'int_hipertrofiaganglionar', int_hipertrofiaganglionar,
                    None, (contexts.variable('inthipertrofiaganglionar'), ),
                    (), (
                        contexts.variable('ans_hipertrofiaganglionar'),
                        contexts.variable('inthipertrofiaganglionar'),
                    ))

    bc_rule.bc_rule('int_discrasiahemorragica', This_rule_base,
                    'int_discrasiahemorragica', int_discrasiahemorragica, None,
                    (contexts.variable('intdiscrasiahemorragica'), ), (), (
                        contexts.variable('ans_discrasiahemorragica'),
                        contexts.variable('intdiscrasiahemorragica'),
                    ))
Example #28
0
def populate(engine):
  This_rule_base = engine.get_create('animalia_fc')
  
  fc_rule.fc_rule('gives_milk', This_rule_base, gives_milk,
    (('animalia', 'gives_milk',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('mammal'),
     pattern.pattern_literal('gives_milk'),))
  
  fc_rule.fc_rule('have_feathers', This_rule_base, have_feathers,
    (('animalia', 'have_feathers',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('bird'),
     pattern.pattern_literal('have_feathers'),))
  
  fc_rule.fc_rule('eats_meat', This_rule_base, eats_meat,
    (('animalia', 'eats_meat',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('carnivore'),
     pattern.pattern_literal('eats_meat'),))
  
  fc_rule.fc_rule('can_swim', This_rule_base, can_swim,
    (('animalia', 'can_swim',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('aquatic'),
     pattern.pattern_literal('can_swim'),))
  
  fc_rule.fc_rule('lays_eggs', This_rule_base, lays_eggs,
    (('animalia', 'lays_eggs',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('bird'),
     pattern.pattern_literal('lays_eggs'),))
  
  fc_rule.fc_rule('has_hair', This_rule_base, has_hair,
    (('animalia', 'has_hair',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('mammal'),
     pattern.pattern_literal('has_hair'),))
  
  fc_rule.fc_rule('have_scales', This_rule_base, have_scales,
    (('animalia', 'have_scales',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('aquatic'),
     pattern.pattern_literal('have_scales'),))
  
  fc_rule.fc_rule('has_antenna', This_rule_base, has_antenna,
    (('animalia', 'has_antenna',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('insect'),
     pattern.pattern_literal('has_antenna'),))
  
  fc_rule.fc_rule('have_webbed_feet', This_rule_base, have_webbed_feet,
    (('animalia', 'have_webbed_feet',
      (contexts.variable('animal'),
       contexts.variable('type'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('type'),
     pattern.pattern_literal('amphibian'),
     pattern.pattern_literal('have_webbed_feet'),))
  
  fc_rule.fc_rule('find_animal_feature', This_rule_base, find_animal_feature,
    (('animalia', 'animal_kind',
      (contexts.variable('animal'),
       contexts.variable('type'),
       contexts.variable('kind'),
       contexts.variable('feature'),),
      False),),
    (contexts.variable('animal'),
     contexts.variable('feature'),
     contexts.variable('kind'),))
Example #29
0
def populate(engine):
  This_rule_base = engine.get_create('bc_regras')
  
  bc_rule.bc_rule('diagnostico_1', This_rule_base, 'diagnostico_1',
                  diagnostico_1, None,
                  (contexts.variable('febre'),
                   contexts.variable('dia_febre'),
                   contexts.variable('dia_manchas'),
                   contexts.variable('dorMuscular'),
                   contexts.variable('doenca'),),
                  (),
                  (contexts.variable('febre'),
                   contexts.variable('dia_febre'),
                   contexts.variable('doenca1'),
                   contexts.variable('dia_manchas'),
                   contexts.variable('doenca2'),
                   contexts.variable('dorMuscular'),
                   contexts.variable('doenca3'),
                   contexts.variable('doenca'),))
  
  bc_rule.bc_rule('diagnostico_2', This_rule_base, 'diagnostico_2',
                  diagnostico_2, None,
                  (contexts.variable('dorArticulacao'),
                   contexts.variable('intesiDorArtic'),
                   contexts.variable('edema'),
                   contexts.variable('doenca'),),
                  (),
                  (contexts.variable('dorArticulacao'),
                   contexts.variable('doenca4'),
                   contexts.variable('intesiDorArtic'),
                   contexts.variable('doenca5'),
                   contexts.variable('edema'),
                   contexts.variable('doenca6'),
                   contexts.variable('doenca'),))
  
  bc_rule.bc_rule('diagnostico_3', This_rule_base, 'diagnostico_3',
                  diagnostico_3, None,
                  (contexts.variable('conjuntivite'),
                   contexts.variable('dorCabeca'),
                   contexts.variable('coceira'),
                   contexts.variable('doenca'),),
                  (),
                  (contexts.variable('conjuntivite'),
                   contexts.variable('probabilidade'),
                   contexts.variable('doenca7'),
                   contexts.variable('dorCabeca'),
                   contexts.variable('doenca8'),
                   contexts.variable('coceira'),
                   contexts.variable('doenca9'),
                   contexts.variable('doenca'),))
  
  bc_rule.bc_rule('diagnostico_4', This_rule_base, 'diagnostico_4',
                  diagnostico_4, None,
                  (contexts.variable('hipertrofia'),
                   contexts.variable('discrasia'),
                   contexts.variable('acometimento'),
                   contexts.variable('neonato'),
                   contexts.variable('doenca'),),
                  (),
                  (contexts.variable('hipertrofia'),
                   contexts.variable('doenca10'),
                   contexts.variable('discrasia'),
                   contexts.variable('doenca11'),
                   contexts.variable('acometimento'),
                   contexts.variable('frequencia'),
                   contexts.variable('neonato'),
                   contexts.variable('doenca12'),
                   contexts.variable('doenca'),))
  
  bc_rule.bc_rule('resulta_diagnostico', This_rule_base, 'diagnostico_resultado',
                  resulta_diagnostico, None,
                  (contexts.variable('febre'),
                   contexts.variable('dia_febre'),
                   contexts.variable('dia_manchas'),
                   contexts.variable('dorMuscular'),
                   contexts.variable('dorArticulacao'),
                   contexts.variable('intesiDorArtic'),
                   contexts.variable('edema'),
                   contexts.variable('conjuntivite'),
                   contexts.variable('dorCabeca'),
                   contexts.variable('coceira'),
                   contexts.variable('hipertrofia'),
                   contexts.variable('discrasia'),
                   contexts.variable('acometimento'),
                   contexts.variable('neonato'),
                   contexts.variable('doenca'),),
                  (),
                  (contexts.variable('febre'),
                   contexts.variable('dia_febre'),
                   contexts.variable('dia_manchas'),
                   contexts.variable('dorMuscular'),
                   contexts.variable('doenca51'),
                   contexts.variable('dorArticulacao'),
                   contexts.variable('intesiDorArtic'),
                   contexts.variable('edema'),
                   contexts.variable('doenca52'),
                   contexts.variable('conjuntivite'),
                   contexts.variable('dorCabeca'),
                   contexts.variable('coceira'),
                   contexts.variable('doenca53'),
                   contexts.variable('hipertrofia'),
                   contexts.variable('discrasia'),
                   contexts.variable('acometimento'),
                   contexts.variable('neonato'),
                   contexts.variable('doenca54'),
                   contexts.variable('doenca'),))