Ejemplo n.º 1
0
def collect_diagnosis(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('patient', 'feels', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        diseases = []
        with engine.lookup('choroby', 'symptom_of', context, \
                           rule.foreach_patterns(1)) \
          as gen_1:
          for dummy in gen_1:
            diseases.append(context.lookup_data('disease'))
        mark4 = context.mark(True)
        if rule.pattern(0).match_data(context, context,
                tuple(diseases)):
          context.end_save_all_undo()
          engine.assert_('patient', 'suffers_from',
                         (rule.pattern(1).as_data(context),
                          rule.pattern(0).as_data(context),)),
          rule.rule_base.num_fc_rules_triggered += 1
        else: context.end_save_all_undo()
        context.undo_to_mark(mark4)
  finally:
    context.done()
Ejemplo n.º 2
0
def run(rule_bases_to_activate,
        default_rb = None, init_fn = None, fn_to_run_plan = eval_plan,
        plan_globals = {}):
    if not Did_init: init()

    if not isinstance(rule_bases_to_activate, (tuple, list)):
        rule_bases_to_activate = (rule_bases_to_activate,)

    if default_rb is None: default_rb = rule_bases_to_activate[0]

    while True:
        print
        goal_str = raw_input("goal: ")
        if not goal_str: break
        goal, args_str = parse(goal_str)
        if goal == "trace":
            args = args_str.split()
            if len(args) == 1:
                Engine.trace(default_rb, args[0])
            else:
                Engine.trace(*args)
            continue
        if goal == "untrace":
            args = args_str.split()
            if len(args) == 1:
                Engine.untrace(default_rb, args[0])
            else:
                Engine.untrace(*args)
            continue
        args_str = args_str.strip()
        rb_name = default_rb
        if args_str[0] == '.':
            rb_name = goal
            goal, args_str = parse(args_str[1:])
        args = parse(args_str)[0]
        print "proving: %s.%s%s" % (rb_name, goal, args)
        goal_args = tuple(as_pattern(arg) for arg in args)
        Engine.reset()
        if init_fn: init_fn(Engine)
        context = contexts.simple_context()
        try:
            Engine.activate(*rule_bases_to_activate)
            with Engine.prove(rb_name, goal, context, goal_args) as it:
                for prototype_plan in it:
                    final = {}
                    print "got: %s%s" % \
                          (goal, tuple(arg.as_data(context, True, final)
                                       for arg in goal_args))
                    if not prototype_plan:
                        print "no plan returned"
                    else:
                        plan = prototype_plan.create_plan(final)
                        fn_to_run_plan(plan_globals, locals())
        except:
            krb_traceback.print_exc(100)
def monitor_no_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'monitor_answer', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        from GUI.BitmapDialog import BitmapDialog
        BitmapDialog("Please switch it on and see the results.", 'images/monitor.jpg').ShowModal()
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def ram_yes_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'beep_answer', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        from GUI.BitmapDialog import BitmapDialog
        BitmapDialog("Check the memory, you may have purchased the wrong memory, installed it incorrectly, or damaged the memory module by handling it improperly.", 'images/ram.jpg').ShowModal()
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def init_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_questions', 'beep_question', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        engine.assert_('off_state_facts', 'beep_answer',
                       (rule.pattern(0).as_data(context),)),
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def speaker_no_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'speaker_answer', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        import wx
        wx.MessageBox("Please make sure the speaker is there if not please install it in your case and rerun the program", 'Solution')
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def monitor_no_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'monitor_answer', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        import wx
        wx.MessageBox("Please switched on and see the results.", 'Solution')
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 8
0
def open(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('chest', 'closed', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        engine.assert_('chest', 'closed',
                       (rule.pattern(0).as_data(context),)),
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def if_problem_solved_yes_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'problem_solved', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        import wx
        wx.MessageBox("Thank you for your cooperation", 'Solution')
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
def speaker_no_rule(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('off_state_facts', 'speaker_answer', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        from GUI.BitmapDialog import BitmapDialog
        BitmapDialog("Please make sure the speaker is there if not please install it in your case and rerun the program", 'images/speaker.jpg').ShowModal()
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 11
0
 def new_fact(self, fact_args, n):
     if self.ran:
         arg_patterns = self.foreach_facts[n][2]
         if len(fact_args) == len(arg_patterns):
             context = contexts.simple_context()
             if all(itertools.imap(lambda pat, arg:
                                       pat.match_data(context, context, arg),
                                   arg_patterns,
                                   fact_args)):
                 self.rule_base.num_fc_rules_rerun += 1
                 if self.foreach_facts[n][3]:
                     self.rule_fn(self)
                 else:
                     self.rule_fn(self, context, n)
             context.done()
Ejemplo n.º 12
0
 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()
Ejemplo n.º 13
0
def has_antenna(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('animalia', 'has_antenna', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        engine.assert_('animalia', 'animal_kind',
                       (rule.pattern(0).as_data(context),
                        rule.pattern(1).as_data(context),
                        rule.pattern(2).as_data(context),
                        rule.pattern(3).as_data(context),)),
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 14
0
def parent_and_child(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('family', 'child_parent', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        engine.assert_('family', 'child_parent',
                       (rule.pattern(0).as_data(context),
                        rule.pattern(1).as_data(context),
                        rule.pattern(2).as_data(context),
                        rule.pattern(3).as_data(context),
                        rule.pattern(4).as_data(context),)),
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 15
0
def hf(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('pcb', 'is_hf', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        engine.assert_('pcb', 'needs_hf_mat',
                       (rule.pattern(0).as_data(context),)),
        engine.assert_('pcb', 'needs_ctrl_z',
                       (rule.pattern(0).as_data(context),)),
        engine.assert_('pcb', 'needs_gnd_layer',
                       (rule.pattern(0).as_data(context),)),
        rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 16
0
def princess(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('famous', 'princess', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                engine.assert_('famous', 'gender', (
                    rule.pattern(0).as_data(context),
                    rule.pattern(1).as_data(context),
                )),
                engine.assert_('famous', 'disney',
                               (rule.pattern(0).as_data(context), )),
                rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 17
0
def freely_elected(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('famous', 'rules', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                with knowledge_base.Gen_once if index == 1 \
                         else engine.lookup('famous', 'free_elections', context,
                                            rule.foreach_patterns(1)) \
                  as gen_1:
                    for dummy in gen_1:
                        engine.assert_('famous', 'freely_elected',
                                       (rule.pattern(0).as_data(context), )),
                        rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 18
0
def facts_for_bc_rules(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    engine.assert_('family', 'as_au',
                   (rule.pattern(0).as_data(context),
                    rule.pattern(1).as_data(context),)),
    engine.assert_('family', 'as_au',
                   (rule.pattern(2).as_data(context),
                    rule.pattern(3).as_data(context),)),
    engine.assert_('family', 'as_nn',
                   (rule.pattern(4).as_data(context),
                    rule.pattern(5).as_data(context),)),
    engine.assert_('family', 'as_nn',
                   (rule.pattern(6).as_data(context),
                    rule.pattern(7).as_data(context),)),
    rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 19
0
def take(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('chest', 'closed', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                with knowledge_base.Gen_once if index == 1 \
                         else engine.lookup('key', 'inChest', context,
                                            rule.foreach_patterns(1)) \
                  as gen_1:
                    for dummy in gen_1:
                        engine.assert_('key', 'inChest',
                                       (rule.pattern(0).as_data(context), )),
                        rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 20
0
def recommend__material_3(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('pcb', 'needs_no_hf_mat', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        with knowledge_base.Gen_once if index == 1 \
                 else engine.lookup('pcb', 'is_not_hf_mat', context,
                                    rule.foreach_patterns(1)) \
          as gen_1:
          for dummy in gen_1:
            engine.assert_('pcb', 'good_material',
                           (rule.pattern(0).as_data(context),
                            rule.pattern(1).as_data(context),)),
            rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 21
0
def inc_num_features(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('fb_checkin', 'inc_num_features', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                engine.assert_('fb_checkin', 'feature', (
                    rule.pattern(0).as_data(context),
                    rule.pattern(1).as_data(context),
                )),
                engine.assert_('fb_checkin', 'update_inc_num_features', (
                    rule.pattern(0).as_data(context),
                    rule.pattern(1).as_data(context),
                    rule.pattern(2).as_data(context),
                )),
                rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 22
0
def how_related_cousins(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('family', 'cousins', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        mark1 = context.mark(True)
        if rule.pattern(0).match_data(context, context,
                nth(context.lookup_data('n'))):
          context.end_save_all_undo()
          engine.assert_('family', 'how_related',
                         (rule.pattern(1).as_data(context),
                          rule.pattern(2).as_data(context),
                          rule.pattern(3).as_data(context),)),
          rule.rule_base.num_fc_rules_triggered += 1
        else: context.end_save_all_undo()
        context.undo_to_mark(mark1)
  finally:
    context.done()
Ejemplo n.º 23
0
def niece_or_nephew_and_aunt_or_uncle(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('family', 'child_parent', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        with knowledge_base.Gen_once if index == 1 \
                 else engine.lookup('family', 'siblings', context,
                                    rule.foreach_patterns(1)) \
          as gen_1:
          for dummy in gen_1:
            with knowledge_base.Gen_once if index == 2 \
                     else engine.lookup('family', 'as_au', context,
                                        rule.foreach_patterns(2)) \
              as gen_2:
              for dummy in gen_2:
                with knowledge_base.Gen_once if index == 3 \
                         else engine.lookup('family', 'as_nn', context,
                                            rule.foreach_patterns(3)) \
                  as gen_3:
                  for dummy in gen_3:
                    mark4 = context.mark(True)
                    if rule.pattern(0).match_data(context, context,
                            ('great',) * len(context.lookup_data('depth'))):
                      context.end_save_all_undo()
                      engine.assert_('family', 'nn_au',
                                     (rule.pattern(1).as_data(context),
                                      rule.pattern(2).as_data(context),
                                      rule.pattern(0).as_data(context),
                                      rule.pattern(3).as_data(context),
                                      rule.pattern(4).as_data(context),)),
                      rule.rule_base.num_fc_rules_triggered += 1
                    else: context.end_save_all_undo()
                    context.undo_to_mark(mark4)
  finally:
    context.done()
def move_01(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('coil_area', 'coil_information', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                with knowledge_base.Gen_once if index == 1 \
                         else engine.lookup('coil_area', 'coil_area_ruler', context,
                                            rule.foreach_patterns(1)) \
                  as gen_1:
                    for dummy in gen_1:
                        if context.lookup_data('status') == 1:
                            engine.assert_('coil_area', 'move_area', (
                                rule.pattern(0).as_data(context),
                                rule.pattern(1).as_data(context),
                                rule.pattern(2).as_data(context),
                            )),
                            rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 25
0
def sisters(rule, context = None, index = None):
  engine = rule.rule_base.engine
  if context is None: context = contexts.simple_context()
  try:
    with knowledge_base.Gen_once if index == 0 \
             else engine.lookup('family', 'daughter_of', context,
                                rule.foreach_patterns(0)) \
      as gen_0:
      for dummy in gen_0:
        with knowledge_base.Gen_once if index == 1 \
                 else engine.lookup('family', 'daughter_of', context,
                                    rule.foreach_patterns(1)) \
          as gen_1:
          for dummy in gen_1:
            if context.lookup_data('sister1') != context.lookup_data('sister2'):
              engine.assert_('family', 'siblings',
                             (rule.pattern(0).as_data(context),
                              rule.pattern(1).as_data(context),
                              rule.pattern(2).as_data(context),
                              rule.pattern(2).as_data(context),)),
              rule.rule_base.num_fc_rules_triggered += 1
  finally:
    context.done()
Ejemplo n.º 26
0
def president(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('famous', 'rules', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                notany36_worked = True
                with engine.lookup('famous', 'title', context, \
                                   rule.foreach_patterns(1)) \
                  as gen_1:
                    for dummy in gen_1:
                        notany36_worked = False
                        if not notany36_worked: break
                if notany36_worked:
                    engine.assert_('famous', 'title', (
                        rule.pattern(0).as_data(context),
                        rule.pattern(1).as_data(context),
                    )),
                    rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 27
0
def dictator(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('famous', 'rules', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                notany20_worked = True
                with engine.lookup('famous', 'free_elections', context, \
                                   rule.foreach_patterns(1)) \
                  as gen_1:
                    for dummy in gen_1:
                        if context.lookup_data(
                                'nation') == context.lookup_data('nation'):
                            notany20_worked = False
                        if not notany20_worked: break
                if notany20_worked:
                    engine.assert_('famous', 'dictator',
                                   (rule.pattern(0).as_data(context), )),
                    rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 28
0
def reasonable(rule, context=None, index=None):
    engine = rule.rule_base.engine
    if context is None: context = contexts.simple_context()
    try:
        with knowledge_base.Gen_once if index == 0 \
                 else engine.lookup('uber', 'labels', context,
                                    rule.foreach_patterns(0)) \
          as gen_0:
            for dummy in gen_0:
                engine.assert_('uber', 'IsA', (
                    rule.pattern(0).as_data(context),
                    rule.pattern(1).as_data(context),
                )),
                engine.assert_('uber', 'IsA', (
                    rule.pattern(2).as_data(context),
                    rule.pattern(1).as_data(context),
                )),
                engine.assert_('uber', 'IsA', (
                    rule.pattern(3).as_data(context),
                    rule.pattern(1).as_data(context),
                )),
                rule.rule_base.num_fc_rules_triggered += 1
    finally:
        context.done()
Ejemplo n.º 29
0
 def prove(self, engine, **args):
     context = contexts.simple_context()
     for var, value in args.iteritems():
         context.bind(var, context, value)
     return producer(engine, self.rb_name, self.goal_name, self.patterns,
                     context, self.pattern_vars)
Ejemplo n.º 30
0
Archivo: goal.py Proyecto: e-loue/pyke
 def prove(self, engine, **args):
     context = contexts.simple_context()
     for var, value in args.iteritems():
         context.bind(var, context, value)
     return producer(engine, self.rb_name, self.goal_name, self.patterns, context, self.pattern_vars)