eclipse-archived / smarthome

Eclipse SmartHome™ project
https://www.eclipse.org/smarthome/
Eclipse Public License 2.0
862 stars 783 forks source link

Duplicate Rule import from Bundle-Resources #2304

Open chrisschauer opened 7 years ago

chrisschauer commented 7 years ago

Hi,

i can't found any specification how rules from bundle resources are imported. So I decided to start with this issue a discussion.

Problem I have a binding which includes a rule (maybe for rule-templates it's the same) in the bundle resources under /ESH-INF/automation/rules as JSON. The first rule import works perfect. But on every further restart of the bundle the importer tries to re-add the rule and this exception occurs:

2016-10-12 12:13:00.348 [DEBUG] [ationResourceBundlesEventQueue:371  ] - Process bundle event 1, for automation bundle 'xxxxxxx.smarthome.binding.alexa' 
2016-10-12 12:13:00.359 [DEBUG] [c.p.RuleResourceBundleImporter:172  ] - Parse rules from bundle 'xxxxxxx.smarthome.binding.alexa' 
2016-10-12 12:13:00.364 [TRACE] [e.s.storage.mapdb.MapDbStorage:154  ] - deserialized value 'org.eclipse.smarthome.automation.Rule@f048ca21' from MapDB
2016-10-12 12:13:00.367 [DEBUG] [.c.thing.internal.ThingManager:1002 ] - Thing handler factory 'AlexaHandlerFactory' added
2016-10-12 12:13:00.372 [DEBUG] [c.p.RuleResourceBundleImporter:242  ] - Not importing rule 'alexa.command' because: Cannot add element, because an element with same UID (alexa.command) already exists.java.lang.IllegalArgumentException: Cannot add element, because an element with same UID (alexa.command) already exists.
    at org.eclipse.smarthome.core.common.registry.AbstractManagedProvider.add(AbstractManagedProvider.java:56)
    at org.eclipse.smarthome.core.common.registry.AbstractRegistry.add(AbstractRegistry.java:177)
    at org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl.add(RuleRegistryImpl.java:201)
    at org.eclipse.smarthome.automation.core.internal.RuleRegistryImpl.add(RuleRegistryImpl.java:1)
    at org.eclipse.smarthome.automation.internal.core.provider.RuleResourceBundleImporter.importData(RuleResourceBundleImporter.java:240)
    at org.eclipse.smarthome.automation.internal.core.provider.RuleResourceBundleImporter.processAutomationProvider(RuleResourceBundleImporter.java:207)
    at org.eclipse.smarthome.automation.internal.core.provider.AutomationResourceBundlesEventQueue.processBundleDefault(AutomationResourceBundlesEventQueue.java:443)
    at org.eclipse.smarthome.automation.internal.core.provider.AutomationResourceBundlesEventQueue.processBundleChanged(AutomationResourceBundlesEventQueue.java:416)
    at org.eclipse.smarthome.automation.internal.core.provider.AutomationResourceBundlesEventQueue.run(AutomationResourceBundlesEventQueue.java:165)
    at java.lang.Thread.run(Thread.java:745)

Expected behavior The importer reacts like this...

maggu2810 commented 7 years ago

Is it expected behaviour that the rule is still available if the provided bundle is removed?

chrisschauer commented 7 years ago

In my use case it does not make sense to keep the rule if the bundle isn't installed. In general I would also suggest a way, where the rule is removed/or disabled if the bundle is stopped.

sjsf commented 7 years ago

the RuleResourceBundleImporter is not a RuleProvider but actually does (or better: tries to do) an import, therefore saving the rule in the ManagedRuleProvider. So it rather looks like purpose and not like a bug. That way of implementing it IMHO is (at least) surprising, considering the way how it works for all the other entities within ESH...

danchom commented 7 years ago

@chrisschauer you can't overwrite existing rule, because all the changes made on that rule will be lost on restart of the system. This is expected behavior. If you want to remove a rule you have to delete it explicitly. Of course you can do such read only rule provider which will provide read only rules from bundle. These rules can be enabled/disabled by the RuleEngine, but they can't be changed (updated) or removed on anyway. @maggu2810 expected behavior is to remove the rules from provider when the provider is disappeared. These means the imported rules have to disappeared when the managed provider is removed (not bundle from which they was imported). Unfortunately it does not work for rules coming from managed rule provider but it is fixred in PR #2194 @sjka you are right the bundle resource importer is not a provider itself, it is importer which imports rules through the rule registry into rule managed provider.