JSR223 Scripting: SimpleRule does not set rule UID #3580
Comments
@steve-bate Can you elaborate why not setting a rule UID causes issues? Note that this is the normal case: A newly added rule should not have a UID set, but it is the rule engine, which assigns a unique id to the rule. |
@kai Given the Jython rule below... scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
class MyRule(SimpleRule):
def __init__(self):
self.triggers = [
Trigger("MyTrigger", "core.ItemStateUpdateTrigger",
Configuration({ "itemName": "TestString1"}))
]
def execute(self, module, input):
events.postUpdate("TestString2", "some data")
automationManager.addRule(MyRule()) I see the following exception information in the log when the rule is reloaded (after an edit)...
The I see the |
I keep track of the added rules using their UID. If the scriptfile is modified or deleted later on, those rules are removed from the rule engine. If looking at the source of the RuleEngine, I don't see any way to figure the ruleID out, if it is not assigned by my code beforehand. I don't even see where the RuleEngine itself assigns a ruleID. If looking at |
I'm not sure if this is right behaviour here. The rules are imported instead of provided, this means when the rules are added into RuleEngine they stay there, until the rules are explicitly removed by the user (through the RuleRegistry interface). This is the behaiviour of RuleResourceBundleImporter. This is done because the rules are acctually stored into managed rule provider which permit to save changes on a rule done by the user.
The Rule registry assigns a rule uid when the rule is missing. This is done in the RuleRegistry.add(rule) method. |
I think we are all confused by the misleading class names (and the fact that there isn't a clear separation of concerns) - a class diagram as a dev documentation would definitely help here... The |
Ok I agree, I didn't know that this is a separate rule provider.
I agree that uid is better to be set by the provider, but in case of mistake or wrong rule provider the RuleEngine must assign them, otherwise these rules can't be added. |
It can be discussed whether we want to consider them as valid rules at all, because otherwise they should not be added (or better say "provided") anyhow. |
@kaikreuzer The So the resolution as I understood is that the |
"providing a rule" and "adding a rule to the registry" are two very different concepts and I think that should be treated that way in scripts as well. So how about being able to add (or maybe better call it "register"?) rules to the ScriptedRuleProvider within scripts directly?
Let me reformulate it: The ScriptedRuleProvider should provide a set of rules, which all have UIDs, yes. |
Any update on this issue? |
I'm sorry, I am a bit busy these days.
If you want to add a rule permanantly to the RuleRegistry you could already do that by using: ruleRegistry.addPermanent(). We could discuss about the wording here.
I open a PR for that. |
Right, but I was talking about "being able to add (or maybe better call it "register"?) rules to the ScriptedRuleProvider" - which is what we are currently doing when defining rules in scripts. I therefore think that this code should be refactored to have a clean architecture. |
@smerschjohann The
SimpleRule
class does not set the rule UID and this causes issues when the rule event publishers try to use the UID to generate event messages. In my local environment, I solved the problem by adding aSimpleRule
constructor that initialized the UID. I also added a constructor to allow the user to specify a prefix for the UID (to make it more user-friendly when logging).The text was updated successfully, but these errors were encountered: