diff --git a/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java b/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java index daa5fbe4f2e..a1b17515efb 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core.test/src/test/java/org/eclipse/smarthome/automation/core/internal/RuleEngineTest.java @@ -147,6 +147,39 @@ public void testRuleTags() { Assert.assertTrue("Missing tag in rule", rule2GetTags.contains("tag2")); } + /** + * test get rules by tags + */ + @Test + public void testGetRuleByTags() { + RuleEngine ruleEngine = createRuleEngine(); + + Rule rule1 = new Rule("rule1", null, null, null, null, null); + Set ruleTags = new LinkedHashSet(); + ruleTags.add("tag1"); + rule1.setTags(ruleTags); + ruleEngine.addRule(rule1, true); + + Rule rule2 = new Rule("rule2", null, null, null, null, null); + Set ruleTags2 = new LinkedHashSet(); + ruleTags2.add("tag1"); + ruleTags2.add("tag2"); + rule2.setTags(ruleTags2); + ruleEngine.addRule(rule2, true); + + Collection rules = ruleEngine.getRulesByTags(null); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size()); + + rules = ruleEngine.getRulesByTags(ruleTags); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules doesn't contains both rules.", 2, rules.size()); + + rules = ruleEngine.getRulesByTags(ruleTags2); + Assert.assertNotNull("Cannot find rule by set of tags: ", rules); + Assert.assertEquals("Collection of rules has to contain only rule2.", 1, rules.size()); + } + /** * test rule configurations with null */ diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java index ac2be331b47..d58eb518310 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/RuleEngine.java @@ -765,13 +765,9 @@ public synchronized Collection getRulesByTags(Set tags) { RuntimeRule r = it.next(); if (tags != null) { Set rTags = r.getTags(); - if (tags != null) { - for (Iterator i = rTags.iterator(); i.hasNext();) { - String tag = i.next(); - if (tags.contains(tag)) { - result.add(r.getRuleCopy()); - break; - } + if (rTags != null) { + if (rTags.containsAll(tags)) { + result.add(r.getRuleCopy()); } } } else { @@ -779,6 +775,7 @@ public synchronized Collection getRulesByTags(Set tags) { } } return result; + } /** diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java index d5a15b4037e..e5ff64865c4 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/template/TemplateManager.java @@ -99,14 +99,10 @@ public Collection getByTags(Set tags, Locale loc for (Iterator it = templates.iterator(); it.hasNext();) { T t = it.next(); if (tags != null) { - Collection rTags = t.getTags(); - if (rTags != null) { - for (Iterator itt = rTags.iterator(); itt.hasNext();) { - String tag = itt.next(); - if (tags.contains(tag)) { - result.add(t); - break; - } + Collection tTags = t.getTags(); + if (tTags != null) { + if (tTags.containsAll(tags)) { + result.add(t); } } } else { diff --git a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java index 50774ed45ce..7d673991d65 100644 --- a/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java +++ b/bundles/automation/org.eclipse.smarthome.automation.core/src/main/java/org/eclipse/smarthome/automation/core/internal/type/ModuleTypeManager.java @@ -116,13 +116,9 @@ public Collection getByTags(Set tags, Locale l for (Iterator it = moduleTypes.iterator(); it.hasNext();) { ModuleType mt = it.next(); if (tags != null) { - Collection rTags = mt.getTags(); - for (Iterator itt = rTags.iterator(); itt.hasNext();) { - String tag = itt.next(); - if (tags.contains(tag)) { - result.add((T) createCopy(mt)); - break; - } + Collection mtTags = mt.getTags(); + if (mtTags.containsAll(tags)) { + result.add((T) createCopy(mt)); } } else { result.add((T) createCopy(mt));