Skip to content

Commit

Permalink
wrap kanren code in a KanrenEngine class; parse rules on init
Browse files Browse the repository at this point in the history
  • Loading branch information
maxeeem committed Sep 16, 2023
1 parent 64e32eb commit cd822c4
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 188 deletions.
18 changes: 17 additions & 1 deletion Tests/test_NAL/test_NAL1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import Tests.utils_for_test as utils_for_test
from Tests.utils_for_test import *

import logging

# utils_for_test.engine = RuleMap()

class TEST_NAL1(unittest.TestCase):
Expand Down Expand Up @@ -60,10 +62,18 @@ def test_deduction(self):
'Robin is a type of animal.
''outputMustContain('<robin --> animal>. %1.00;0.81%')
'''

rules, task, belief, concept, task_link, term_link, result1, result2 = rule_map_two_premises(
'<bird --> animal>. %1.00;0.90%',
'<robin --> bird>. %1.00;0.90%',
'bird.')

# Set log level
logging.basicConfig()
log = logging.getLogger("LOG")
results = kanren.inference(task.sentence, belief.sentence)
log.warning(results)

tasks_derived = [rule(task, belief, task_link, term_link) for rule in rules]
self.assertTrue(
output_contains(tasks_derived, '<robin --> animal>. %1.00;0.81%')
Expand Down Expand Up @@ -161,7 +171,13 @@ def test_exemplification(self):
self.assertTrue(
output_contains(tasks_derived, '<animal --> robin>. %1.00;0.45%')
)


# Set log level
logging.basicConfig()
log = logging.getLogger("LOG")
results = kanren.inference(task.sentence, belief.sentence)
log.warning(results)

pass


Expand Down
4 changes: 3 additions & 1 deletion Tests/utils_for_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from pynars.NAL.MentalOperation import execute
from pynars.Narsese import Sentence, Judgement, Quest, Question, Goal

from pynars.NARS.InferenceEngine.Logic2 import KanrenEngine

nars = Reasoner(100, 100)
engine: GeneralEngine = nars.inference

kanren: KanrenEngine = KanrenEngine()

def rule_map_two_premises(premise1: str, premise2: str, term_common: str, inverse: bool=False, is_belief_term: bool=False, index_task=None, index_belief=None) -> Tuple[List[RuleCallable], Task, Belief, Concept, TaskLink, TermLink, Tuple[Task, Task, Task, Task]]:
''''''
Expand Down
3 changes: 3 additions & 0 deletions pynars/NARS/InferenceEngine/GeneralEngine/GeneralEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ def step(self, concept: Concept):
term_belief = concept_target.term
# if belief is None: continue

# if belief is not None:
# print(">>", task.term, belief.term)

# before matching and applying the rules, do variable-related processes (i.e. unification, and substitution/introduction/elimination)
subst, elimn, intro = GeneralEngine.unify(task.term, belief.term if belief is not None else None, concept.term, task_link_valid, term_link)
task_subst, task_elimn, task_intro = GeneralEngine.substitute(subst, elimn, intro, task)
Expand Down
Loading

0 comments on commit cd822c4

Please sign in to comment.