From e2749ec7e1c4407f4843177e72eb0b73ae8a4481 Mon Sep 17 00:00:00 2001 From: Nil Geisweiller Date: Tue, 15 Feb 2022 07:17:07 +0200 Subject: [PATCH 1/2] Fix type annotation and improve comment of get_context --- rocca/agents/utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rocca/agents/utils.py b/rocca/agents/utils.py index 1d11463..6177625 100644 --- a/rocca/agents/utils.py +++ b/rocca/agents/utils.py @@ -423,9 +423,14 @@ def is_virtual(clause: Atom) -> bool: return is_a(clause.type, types.VirtualLink) -def get_context(cogscm: Atom) -> tuple[Atom, Atom]: +def get_context(cogscm: Atom) -> tuple[list[Atom], list[Atom]]: """Extract the context of a cognitive schematic. + Since the context can be multiple clauses, virtual and + non-virtual, it outputs a pair of two lists + + (present-clauses, virtual-clauses) + For instance given a cognitive schematic of that format BackPredictiveImplicationScope @@ -436,7 +441,7 @@ def get_context(cogscm: Atom) -> tuple[Atom, Atom]: - return . + return ([], []). Another example, given a cognitive schematic of that format @@ -450,12 +455,7 @@ def get_context(cogscm: Atom) -> tuple[Atom, Atom]: ... - return - - Since the context can be multiple clauses, virtual and - non-virtual, it outputs a pair of two lists - - (present-clauses, virtual-clauses) + return ([], []) """ @@ -826,7 +826,7 @@ def get_context_actual_truth(atomspace: AtomSpace, cogscm: Atom, i: int) -> Trut BackPredictiveImplicationScope - + And (or SimultaneousAnd?) Execution From 7686a42650c2c1df755d1fcf747d7d8fe6684033 Mon Sep 17 00:00:00 2001 From: Nil Geisweiller Date: Tue, 15 Feb 2022 07:17:33 +0200 Subject: [PATCH 2/2] Add unit test for get_context --- tests/agents/test_utils.py | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/agents/test_utils.py b/tests/agents/test_utils.py index 16247ae..f8cd92e 100644 --- a/tests/agents/test_utils.py +++ b/tests/agents/test_utils.py @@ -33,6 +33,7 @@ differential_entropy, get_uniq_atoms, to_human_readable_str, + get_context, ) # Set main atomspace @@ -242,3 +243,92 @@ def test_to_human_readable_str(): expected_3b = "PoleAngle($angle) ∧ -0.01 > $angle ∧ do(GoLeft) ↝ Reward(1)" assert cogscm_hrs_3 == expected_3a or cogscm_hrs_3 == expected_3b + + +def test_get_context(): + # 1. Monoaction plan: + # outside(self, house) ∧ do(go_to_key) ↝ hold(self, key) + cogscm_1 = BackPredictiveImplicationScopeLink( + VariableSet(), + SLink(ZLink()), + AndLink( + EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ), + ExecutionLink(SchemaNode("go_to_key")), + ), + EvaluationLink( + PredicateNode("hold"), ListLink(ConceptNode("self"), ConceptNode("key")) + ), + ) + + context_1, _ = get_context(cogscm_1) + expected_1 = EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ) + + assert context_1[0] == expected_1 + + # 2. Dioaction plan: + # outside(self, house) ∧ do(go_to_key) ≺ do(go_to_house) ↝ inside(self, house) + cogscm_2 = BackPredictiveImplicationScopeLink( + VariableSet(), + SLink(ZLink()), + BackSequentialAndLink( + SLink(ZLink()), + AndLink( + EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ), + ExecutionLink(SchemaNode("go_to_key")), + ), + ExecutionLink(SchemaNode("go_to_house")), + ), + EvaluationLink( + PredicateNode("inside"), ListLink(ConceptNode("self"), ConceptNode("house")) + ), + ) + + context_2, _ = get_context(cogscm_2) + expected_2 = EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ) + + assert context_2[0] == expected_2 + + # 3. Triaction plan + # outside(self, house) ∧ do(go_to_key) ≺ do(go_to_house) ≺ do(go_to_diamonds) ↝ Reward(1) + cogscm_3 = BackPredictiveImplicationScopeLink( + VariableSet(), + SLink(ZLink()), + BackSequentialAndLink( + SLink(ZLink()), + BackSequentialAndLink( + SLink(ZLink()), + AndLink( + EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ), + ExecutionLink(SchemaNode("go_to_key")), + ), + ExecutionLink(SchemaNode("go_to_house")), + ), + ExecutionLink(SchemaNode("go_to_house")), + ), + EvaluationLink( + PredicateNode("inside"), ListLink(ConceptNode("self"), ConceptNode("house")) + ), + ) + + context_3, _ = get_context(cogscm_3) + expected_3 = EvaluationLink( + PredicateNode("outside"), + ListLink(ConceptNode("self"), ConceptNode("house")), + ) + + assert context_3[0] == expected_3