Esempio n. 1
0
 def test_lst_execution(self):
     atom1 = ConceptNode("atom1")
     exec_link = ExecutionOutputLink(GroundedSchemaNode("py:ret_lst"),
                                     ListLink(atom1, atom1, atom1))
     try:
         execute_atom(self.space, exec_link)
         self.assertFalse("call should fail")
     except RuntimeError as e:
         # Use `nosetests3 --nocapture` to see this print...
         print("The exception message is " + str(e))
         self.assertTrue("did not return Atomese" in str(e))
Esempio n. 2
0
 def test_add_atom_from_grounded_schema_node(self):
     test_as = create_child_atomspace(self.atomspace)
     execute_atom(
         test_as,
         ExecutionOutputLink(GroundedSchemaNode("py:add_new_link"),
                             ListLink()))
     self.assertTrue(
         test_as.is_link_in_atomspace(types.InheritanceLink, [
             test_as.add_node(types.ConceptNode, "cat"),
             test_as.add_node(types.ConceptNode, "animal")
         ]))
Esempio n. 3
0
def get_context_actual_truth(atomspace: AtomSpace, cogscm: Atom,
                             i: int) -> TruthValue:
    """Calculate tv of the context of cognitive schematic cogscm at time i.

    Given a cognitive schematic of that format

    BackPredictiveImplicationScope <tv>
      <vardecl>
      <expiry>
      And (or SimultaneousAnd?)
        <context>
        Execution
          <action>
          <input> [optional]
          <output> [optional]
      <goal>

    calculate the truth value of <context> at time i, where the
    variables have been properly instantiated.

    For now the calculation is crisp, either a context is completely
    true (stv 1 1) or completely false (stv 0 1).

    """

    # Build and run a query to check if the context is true
    vardecl = get_vardecl(cogscm)
    present_clauses, virtual_clauses = get_context(cogscm)
    stamped_present_clauses = [timestamp(pc, i) for pc in present_clauses]
    body = AndLink(PresentLink(*stamped_present_clauses),
                   IsClosedLink(*stamped_present_clauses),
                   IsTrueLink(*stamped_present_clauses), *virtual_clauses)
    query = SatisfactionLink(vardecl, body)
    tv = execute_atom(atomspace, query)
    return tv
Esempio n. 4
0
 def _get_atoms_by_type(self, type):
     type_name = get_type_name(type)
     return execute_atom(
         self.space,
         GetLink(
             VariableList(
                 TypedVariableLink(VariableNode("X"), TypeNode(type_name))),
             AndLink(VariableNode("X"))))
Esempio n. 5
0
 def test_too_many_args(self):
     atom1 = ConceptNode("atom1")
     exec_link = ExecutionOutputLink(GroundedSchemaNode("py:return_concept"),
                                     ListLink(atom1, atom1))
     try:
        result = execute_atom(self.space, exec_link)
        self.assertFalse("call should fail")
     except RuntimeError as e:
        self.assertTrue("but 2 were given" in str(e))
Esempio n. 6
0
 def test_too_few_args(self):
     atom1 = ConceptNode("atom1")
     exec_link = ExecutionOutputLink(GroundedSchemaNode("py:return_concept"),
                                     ListLink())
     try:
        result = execute_atom(self.space, exec_link)
        self.assertFalse("call should fail")
     except RuntimeError as e:
        self.assertTrue("missing 1 required positional argument" in str(e))
Esempio n. 7
0
    def test_tmp_atomspace(self):
        ListLink(ConceptNode("foo"), ConceptNode("bar"))

        get = GetLink(VariableNode("x"),
                AndLink(
                    PresentLink(ListLink (ConceptNode("foo"),
                                          (VariableNode("x")))),
                    EvaluationLink(GroundedPredicateNode("py: test_functions.func_one"),
                        ListLink(VariableNode("x"))),
                   EvaluationLink(GroundedPredicateNode( "py: test_functions.func_two"),
                   ListLink (VariableNode ("x")))))
        result = execute_atom(self.atomspace, get)
        self.assertFalse(result.out)
        self.assertFalse(self.atomspace.is_node_in_atomspace(types.ConceptNode, 'barleycorn'))
        test_functions.func_one_result = TruthValue(1,1)
        result = execute_atom(self.atomspace, get)
        self.assertTrue(result.out)
        # still should not be in the current namespace
        self.assertFalse(self.atomspace.is_node_in_atomspace(types.ConceptNode, 'barleycorn'))
Esempio n. 8
0
    def test_good_execution(self):
        atom1 = ConceptNode("atom1")
        exec_link = ExecutionOutputLink(GroundedSchemaNode("py:good_tv"),
                                        ListLink(atom1, atom1, atom1))
        okay = execute_atom(self.space, exec_link)

        # Use `nosetests3 --nocapture` to see this print...
        print("The good TV is " + str(okay))
        expect = TruthValue(0.5, 0.5)
        self.assertTrue(okay == expect)
Esempio n. 9
0
    def test_do_execute_value(self):
        key = PredicateNode("key")
        atom = ConceptNode("atom")
        atom.set_value(key, FloatValue([1, 2, 3]))

        value_of_link = ValueOfLink(atom, key)

        value = execute_atom(self.atomspace, value_of_link)
        self.assertEqual(FloatValue([1, 2, 3]), value)
        self.assertEqual([1, 2, 3], value.to_list())
Esempio n. 10
0
    def test_do_execute_atom(self):
        (DefineLink(
            DefinedSchemaNode('add'),
            LambdaLink(VariableList(VariableNode('$X'), VariableNode('$Y')),
                       PlusLink(VariableNode('$X'), VariableNode('$Y')))))

        res = execute_atom(
            self.atomspace,
            ExecutionOutputLink(DefinedSchemaNode('add'),
                                ListLink(NumberNode("3"), NumberNode("4"))))
        self.assertEqual(NumberNode("7"), res)
Esempio n. 11
0
 def test_grounded_cond(self):
     grounded_cond = CondLink(
         EvaluationLink(GroundedPredicateNode("py:grounded_cond1"),
                        ListLink()), NumberNode('1'),
         EvaluationLink(GroundedPredicateNode("py:grounded_cond2"),
                        ListLink()), NumberNode('2'))
     result = execute_atom(self.space, grounded_cond)
     baz = NumberNode("2")
     print("got %s", result)
     print("expected %s\n", baz)
     self.assertTrue(result == baz)
Esempio n. 12
0
 def test_execute_atom(self):
     result = execute_atom(self.atomspace,
             ExecutionOutputLink(
                 GroundedSchemaNode("py: test_functions.add_link"),
                 ListLink(
                     ConceptNode("one"),
                     ConceptNode("two")
                 )
             )
         )
     list_link = ListLink(
             ConceptNode("one"),
             ConceptNode("two")
         )
     self.assertEquals(result, list_link)
Esempio n. 13
0
    def test_run_openpsi(self):
        openpsi = OpenPsi(atomspace)

        goal = openpsi.create_goal("goal-run")

        context = [
            InheritanceLink(
                VariableNode("$APPLE"),
                ConceptNode("apple")),
            AbsentLink(
                InheritanceLink(
                    VariableNode("$APPLE"),
                    ConceptNode("handled")))
        ]

        action = ExecutionOutputLink(
            GroundedSchemaNode("py: eat_apple"),
            ListLink(
                VariableNode("$APPLE")))

        component = openpsi.create_component("test-component-run")

        openpsi.add_rule(context, action, goal, TruthValue(1.0, 1.0), component)

        openpsi.run(component)

        # Apples are handled by OpenPsi loop
        InheritanceLink(ConceptNode("apple-1"), ConceptNode("apple"))
        InheritanceLink(ConceptNode("apple-2"), ConceptNode("apple"))

        delay = 5
        time.sleep(delay)
        openpsi.halt(component)

        handled_apples = GetLink(
            InheritanceLink(
                VariableNode("$APPLE"),
                ConceptNode("handled")))

        result_set = execute_atom(atomspace, handled_apples)
        result1 = result_set.out[0]
        result2 = result_set.out[1]
        self.assertEqual(result1, ConceptNode("apple-1"))
        self.assertEqual(result2, ConceptNode("apple-2"))
Esempio n. 14
0
 def test_bindlink(self):
     atom = execute_atom(self.atomspace, self.bindlink_atom)
     print("Bindlink found: " + str(atom))
     self._check_result_setlink(atom, 3)
Esempio n. 15
0
        )
    )
    '''
scheme_eval(atomspace, execute_code)

print ("execute: cog-execute")
if (executed):
    print ("add_link - executed successfully")
else:
    print ("add_link - did NOT execute")

executed = False
execute_atom( atomspace,
    ExecutionOutputLink(
        GroundedSchemaNode("py: add_link"),
        ListLink(
            ConceptNode("one"),
            ConceptNode("two")
        )
    )
)
print ("execute: execute_atom")
if (executed):
    print ("add_link - executed successfully")
else:
    print ("add_link - did NOT execute")


finalize_opencog()
del atomspace
Esempio n. 16
0
 def test_satisfying_set(self):
     atom = execute_atom(self.atomspace, self.getlink_atom)
     self._check_result_setlink(atom, 3)
Esempio n. 17
0
 def test_execute_atom_no_return_value(self):
     result = execute_atom(self.atomspace,
             PutLink(DeleteLink(VariableNode("X")),
                     ConceptNode("deleteme")))
     self.assertEquals(result, None)
Esempio n. 18
0
 def test_correct_argcount(self):
     atom1 = ConceptNode("atom1")
     exec_link = ExecutionOutputLink(GroundedSchemaNode("py:return_concept"),
                                     ListLink(atom1))
     result = execute_atom(self.space, exec_link)
     self.assertTrue(result.name == "test")