diff --git a/src/main/org/opennars/middle/operatorreflection/MethodInvocationOperator.java b/src/main/org/opennars/middle/operatorreflection/MethodInvocationOperator.java new file mode 100644 index 0000000000000000000000000000000000000000..4e017e6af47caef89e27e5298f7c7888e8fe1eff --- /dev/null +++ b/src/main/org/opennars/middle/operatorreflection/MethodInvocationOperator.java @@ -0,0 +1,44 @@ +package org.opennars.middle.operatorreflection; + +import org.opennars.entity.Task; +import org.opennars.interfaces.Pluggable; +import org.opennars.interfaces.Timable; +import org.opennars.language.Term; +import org.opennars.operator.Operation; +import org.opennars.operator.Operator; +import org.opennars.storage.Memory; + +/** + * Exposes a method of a object as a callable operator + */ + public class MethodInvocationOperator extends Operator { + private Method method; + private Object object; + + /** + * @param narsOperatorName name of the operator as called by NARS + * @param object object of method call + * @param method called method + */ + public MethodInvocationOperator(String narsOperatorName, Object object, Method method) { + super(narsOperatorName); + + this.object = object; + this.method = method; + } + + @Override + protected List<Task> execute(Operation operation, Term[] args, Memory memory, Timable time) { + try { + method.invoke(object); + } + catch (IllegalAccessException e) { + // ignore + } + catch (InvocationTargetException e) { + // ignore + } + + return null; + } +} diff --git a/src/main/org/opennars/middle/operatorreflection/OperatorReflection.java b/src/main/org/opennars/middle/operatorreflection/OperatorReflection.java new file mode 100644 index 0000000000000000000000000000000000000000..6be1e71d0c4f06df112d97b45f2e65b1aabb5b04 --- /dev/null +++ b/src/main/org/opennars/middle/operatorreflection/OperatorReflection.java @@ -0,0 +1,45 @@ +package org.opennars.middle.operatorreflection; + +import org.opennars.entity.Task; +import org.opennars.interfaces.Pluggable; +import org.opennars.interfaces.Timable; +import org.opennars.language.Term; +import org.opennars.operator.Operation; +import org.opennars.operator.Operator; +import org.opennars.storage.Memory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Manages the exposed methods which are callable by Operators from NARS + */ +public class OperatorReflection { + public Map<String, MethodInvocationOperator> methodOperators = new HashMap<>(); + + /** + * adds and registers a method without arguments + * + * @param pluggable used to register the operator + * @param narsOperatorName name of the operator as called by NARS + * @param object object of method call + * @param methodName name of the called method + */ + public void appendAndRegisterMethodWithoutArguments(Pluggable pluggable, String narsOperatorName, Object object, String methodName) throws NoSuchMethodException { + Method calledMethod = retMethodByName(object, methodName); + + MethodInvocationOperator operator = new MethodInvocationOperator(narsOperatorName, object, calledMethod); + + // register operator + pluggable.addPlugin(operator); + + methodOperators.put(narsOperatorName, operator); + } + + protected Method retMethodByName(Object object, String methodName) throws NoSuchMethodException { + return object.getClass().getMethod(methodName); + } +}