Skip to content

Commit

Permalink
feat: ✨ Optimized operation registration mechanism
Browse files Browse the repository at this point in the history
Operation registration is now allowed without typing eval/exec and will pre-detect "registered" before registration
  • Loading branch information
ARCJ137442 committed Oct 30, 2023
1 parent 99ef061 commit fdc2c68
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
28 changes: 18 additions & 10 deletions pynars/ConsolePlus.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,37 +301,45 @@ def eval_code(*args: List[str]) -> None:

@cmd_register(('register-operation', 'register'))
def register_operation(*args: List[str]) -> None:
'''Format: register-operation <Operation(Term) Name> <'eval'/'exec'> <Python Code>
'''Format: register-operation <Operation(Term) Name> [<'eval'/'exec'> <Python Code>]
Register an operation to NARS interface.
function signature:
execution_F(arguments: Iterable[Term], task: Task=None, memory: Memory=None) -> Union[Task,None]
default fallback of execution_F when code='' is equivalent to:
default fallback of execution_F when only 1 argument is provided:
print(f'executed: arguments={arguments}, task={task}, memory={memory}. the "task" will be returned')
! Unsupported: register mental operations
'''
name = args[0]
"The operator's name without `^` as prefix"
eType = args[1]
code = " ".join(args[2:])
if code == '':
if len(args) == 1:
def execution_F(arguments: Iterable[Term], task: Task=None, memory: Memory=None) -> Union[Task,None]:
print(f'executed: arguments={arguments}, task={task}, memory={memory}. the "task" will be returned')
return task
execution_F.__doc__ = f'''
The execution is auto generated from operator {name} without code
'''.strip()
else:
eType = args[1]
code = " ".join(args[2:])
if eType =='exec':
def execution_F(arguments: Iterable[Term], task: Task=None, memory: Memory=None) -> Union[Task,None]:
return exec(code)
else:
def execution_F(arguments: Iterable[Term], task: Task=None, memory: Memory=None) -> Union[Task,None]:
return eval(code)
execution_F.__doc__ = f'''
The execution is auto generated from operator {name} in {eType} mode with code={code}
'''
current_NARS_interface.reasoner.register_operation(name, execution_F)
print(f'Operation {name} was successfully registered in mode "{eType}" with code={code}')
execution_F.__doc__ = f'''
The execution is auto generated from operator {name} in {eType} mode with code={code}
'''.strip()
if current_NARS_interface.reasoner.register_operation(name, execution_F):
print(f'Operation {name} was successfully registered ' + (
'without code'
if len(args)
else f'in mode "{eType}" with code={code}'))
else:
print(f'The operation {name} was already registered!')


@cmd_register(('simplify-parse', 'parse'))
Expand Down
14 changes: 9 additions & 5 deletions pynars/NARS/Control/Reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ def mental_operation(self, task: Task, concept: Concept, answers_question: Task,

return task_operation_return, task_executed, belief_awared

def register_operation(self, name_operation: str, callback: Callable):
''''''
from pynars.Narsese import Operation as Op
op = Op(name_operation)
Operation.register(op, callback)
def register_operation(self, name_operation: str, callback: Callable) -> bool:
'''register an operation and return whether the registration is successful'''
'return the status of whether the registration is successful'
if not Operation.isRegisteredByName(name_operation):
from pynars.Narsese import Operation as Op
op = Op(name_operation)
Operation.register(op, callback)
return True
return False

10 changes: 9 additions & 1 deletion pynars/NARS/Operation/Register.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
def register(operation: Operation, callable: Callable):
''''''
global registered_operations
registered_operations[operation] = callable
registered_operations[operation] = callable

def isRegisteredByName(word) -> bool:
''''''
global registered_operations
for registered_operation in registered_operations:
if registered_operation.word == word:
return True
return False

0 comments on commit fdc2c68

Please sign in to comment.