I’ve just set up the JSR addon but when I try to execute the rule I always got an exception:
2016-04-30 00:49:08.313 [INFO ] [o.c.j.i.e.scriptmanager.Script] - Loading Script testrule.py
2016-04-30 00:49:08.314 [INFO ] [o.c.j.i.e.scriptmanager.Script] - EngineName: jython
2016-04-30 00:49:08.330 [INFO ] [o.o.c.j.i.e.s.ScriptManager ] - Engine found for File: testrule.py
2016-04-30 00:49:46.500 [ERROR] [.c.j.i.e.RuleExecutionRunnable] - Error while executing rule: org.python.proxies.__builtin__$TestRule$2@2538208f
org.python.core.PyException: null
at org.python.core.Py.AttributeError(Py.java:205) ~[jython.jar:na]
at org.python.core.PyObject.noAttributeError(PyObject.java:1013) ~[jython.jar:na]
at org.python.core.PyObject.__getattr__(PyObject.java:1008) ~[jython.jar:na]
at org.python.pycode._pyx2.execute$3(<script>:8) ~[na:na]
at org.python.pycode._pyx2.call_function(<script>) ~[na:na]
at org.python.core.PyTableCode.call(PyTableCode.java:167) ~[jython.jar:na]
at org.python.core.PyBaseCode.call(PyBaseCode.java:307) ~[jython.jar:na]
at org.python.core.PyBaseCode.call(PyBaseCode.java:198) ~[jython.jar:na]
at org.python.core.PyFunction.__call__(PyFunction.java:482) ~[jython.jar:na]
at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237) ~[jython.jar:na]
at org.python.core.PyMethod.__call__(PyMethod.java:228) ~[jython.jar:na]
at org.python.core.PyMethod.__call__(PyMethod.java:218) ~[jython.jar:na]
at org.python.core.PyMethod.__call__(PyMethod.java:213) ~[jython.jar:na]
at org.python.core.PyObject._jcallexc(PyObject.java:3626) ~[jython.jar:na]
at org.python.core.PyObject._jcall(PyObject.java:3658) ~[jython.jar:na]
at org.python.proxies.__builtin__$TestRule$2.execute(Unknown Source) ~[na:na]
at org.openhab.core.jsr223.internal.engine.RuleExecutionRunnable.run(RuleExecutionRunnable.java:36) ~[na:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
My rule is quite simple so I don’t really know what the problem is:
If I use the self.getName() call instead, I still got the same error. Do you have an idea for this as well (I would expect the getName() method to be defined somewhere in the class hierachie but I’m completly new to jython…
To get the name of a rule you can use standard Python techniques for getting the class name.
rule_name = type(self).__name__
So a getName function could be implemented in a rule class as
def getName(self):
return type(self).__name__
If you wanted to reuse that functionality across rules, you could create your own rule base class.
class MyRule(Rule):
def getName(self):
return type(self).__name__
and then derive your rules from that class instead of directly from Rule.
class ExampleRule(MyRule):
def getEventTrigger(self):
oh.logInfo(self.getName(), "getEventTrigger called")
return [ StartupTrigger() ]
# add execute function, etc. ...
You probably know this already, but the first argument to the log functions can be any string. The value is used as the LogBack category name in the log file. It isn’t required to be the rule name although that’s a convenient string to use.