diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Rule.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Rule.java
index 5fcd43e4b38..31f3fc70a5e 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Rule.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Rule.java
@@ -8,32 +8,41 @@
package org.eclipse.smarthome.automation;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.UUID;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.smarthome.automation.template.RuleTemplate;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.common.registry.Identifiable;
/**
- * Rule is built from {@link Module}s and consists of three sections:
- * ON/IF/THEN.
+ * An automation Rule is built from {@link Module}s and consists of three parts:
*
- *
ON - contains {@link Trigger} modules. The triggers defines what fires the the Rule.
- *
IF - contains {@link Condition} modules which determinate if the Rule is satisfied or not. When all conditions
- * are satisfied then the Rule can proceed with execution of THEN part.
- *
THEN - contains actions which have to be executed by the {@link Rule}
+ *
Triggers: a list of {@link Trigger} modules. Each {@link Trigger} from this list
+ * can start the evaluation of the Rule. A Rule with an empty list of {@link Trigger}s can
+ * only be triggered through the {@link RuleRegistry#runNow(String, boolean, java.util.Map)} method,
+ * or directly executed with the {@link RuleRegistry#runNow(String)} method.
+ *
Conditions: a list of {@link Condition} modules. When a Rule is triggered, the
+ * evaluation of the Rule's {@link Condition}s will determine if the Rule will be executed.
+ * A Rule will be executed only when all it's {@link Condition}s are satisfied. If the {@link Condition}s
+ * list is empty, the Rule is considered satisfied.
+ *
Actions: a list of {@link Action} modules. These modules determine the actions that
+ * will be performed when a Rule is executed.
*
- * Rules can have tags - non-hierarchical keywords or terms for
- * describing them. They help for classifying the items and allow them to be
- * found.
+ * Additionally, Rules can have tags - non-hierarchical keywords or terms for describing them.
+ * They can help the user to classify or label the Rules, and to filter and search them.
*
* @author Yordan Mihaylov - Initial Contribution
* @author Ana Dimova - Initial Contribution
* @author Vasil Ilchev - Initial Contribution
*/
+@NonNullByDefault
public class Rule implements Identifiable {
protected List triggers;
@@ -41,61 +50,65 @@ public class Rule implements Identifiable {
protected List actions;
protected Configuration configuration;
protected List configDescriptions;
+ @Nullable
protected String templateUID;
protected String uid;
+ @Nullable
protected String name;
protected Set tags;
protected Visibility visibility;
+ @Nullable
protected String description;
/**
- * Constructor creates an empty rule. The rule has ruleUID set by the rule engine.
- */
- public Rule() {
- }
-
- /**
- * Constructor creates an empty rule with specified rule uid
+ * Constructor for creating an empty {@link Rule} with a specified rule identifier.
+ * When {@code null} is passed for the {@code uid} parameter, the {@link Rule}'s identifier will
+ * be randomly generated.
*
- * @param uid is the unique identifier of created rule.
+ * @param uid the rule's identifier, or {@code null} if a random identifier should be generated.
*/
- public Rule(String uid) {
- this.uid = uid;
+ public Rule(@Nullable String uid) {
+ this(uid, null, null, null, null, null, null, null);
}
/**
- * Utility constructor which creates a rule from modules or template.
+ * Utility constructor for creating a {@link Rule} from a set of modules, or from a template.
+ * When {@code null} is passed for the {@code uid} parameter, the {@link Rule}'s identifier will be randomly
+ * generated.
*
- * @param uid is the unique identifier of the rule.
- * @param triggers trigger modules
- * @param conditions condition modules
- * @param actions action modules
- * @param configurations are values of rule template. It is available when the rule is created from template and the
- * template is not resolved.
- * @param templateUID the unique identifier of RuleTemplate. It is available when the rule is created from template
- * and the template is not resolved.
- * @param visibility visibility of rule
+ * @param uid the {@link Rule}'s identifier, or {@code null} if a random identifier should be generated.
+ * @param triggers the {@link Rule}'s triggers list, or {@code null} if the {@link Rule} should have no triggers or
+ * will be created from a template.
+ * @param conditions the {@link Rule}'s conditions list, or {@code null} if the {@link Rule} should have no
+ * conditions, or will be created from a template.
+ * @param actions the {@link Rule}'s actions list, or {@code null} if the {@link Rule} should have no
+ * actions, or will be created from a template.
+ * @param configDescriptions metadata describing the configuration of the {@link Rule}.
+ * @param configuration the values that will configure the modules of the {@link Rule}.
+ * @param templateUID the {@link RuleTemplate} identifier of the template that will be used by the
+ * {@link RuleRegistry} to validate the {@link Rule}'s configuration, as well as to create and configure
+ * the {@link Rule}'s modules, or null if the {@link Rule} should not be created from a template.
+ * @param visibility the {@link Rule}'s visibility
*/
- public Rule(String uid, List triggers, //
- List conditions, //
- List actions, //
- List configDescriptions, //
- Configuration configurations, String templateUID, Visibility visibility) {
- this.uid = uid;
- setTriggers(triggers);
- setConditions(conditions);
- setActions(actions);
- setConfigurationDescriptions(configDescriptions);
- setConfiguration(configurations);
+ public Rule(@Nullable String uid, @Nullable List triggers, @Nullable List conditions,
+ @Nullable List actions, @Nullable List configDescriptions,
+ @Nullable Configuration configuration, @Nullable String templateUID, @Nullable Visibility visibility) {
+ this.uid = uid == null ? UUID.randomUUID().toString() : uid;
+ this.triggers = triggers == null ? new ArrayList<>() : triggers;
+ this.conditions = conditions == null ? new ArrayList<>() : conditions;
+ this.actions = actions == null ? new ArrayList<>() : actions;
+ this.configDescriptions = configDescriptions == null ? new ArrayList<>() : configDescriptions;
+ this.configuration = configuration == null ? new Configuration() : configuration;
setTemplateUID(templateUID);
- setVisibility(visibility);
+ this.visibility = visibility == null ? Visibility.VISIBLE : visibility;
+ tags = new HashSet<>();
}
/**
- * This method is used for getting the unique identifier of the Rule. This property is set by the RuleEngine when
- * the {@link Rule} is added. It's optional property.
+ * This method is used to obtain the identifier of the Rule. It can be specified by the {@link Rule}'s creator, or
+ * randomly generated.
*
- * @return unique id of this {@link Rule}
+ * @return an identifier of this {@link Rule}. Can't be {@code null}.
*/
@Override
public String getUID() {
@@ -103,252 +116,253 @@ public String getUID() {
}
/**
- * This method is used for getting the unique identifier of the RuleTemplate. This property is set by the RuleEngine
- * when the {@link Rule} is added and it is created from template. It's optional property.
+ * This method is used to obtain the {@link RuleTemplate} identifier of the template the {@link Rule} was created
+ * from. It will be used by the {@link RuleRegistry} to resolve the {@link Rule}: to validate the {@link Rule}'s
+ * configuration, as well as to create and configure the {@link Rule}'s modules. If a {@link Rule} has not been
+ * created from a template, or has been successfully resolved by the {@link RuleRegistry}, this method will return
+ * {@code null}.
*
- * @return unique id of this {@link Rule}
+ * @return the identifier of the {@link Rule}'s {@link RuleTemplate}, or {@code null} if the {@link Rule} has not
+ * been created from a template, or has been successfully resolved by the {@link RuleRegistry}.
*/
- public String getTemplateUID() {
+ public @Nullable String getTemplateUID() {
return templateUID;
}
- public void setTemplateUID(String templateUID) {
+ /**
+ * This method is used to specify the {@link RuleTemplate} identifier of the template that will be used to
+ * by the {@link RuleRegistry} to resolve the {@link Rule}: to validate the {@link Rule}'s configuration, as well as
+ * to create and configure the {@link Rule}'s modules.
+ */
+ public void setTemplateUID(@Nullable String templateUID) {
this.templateUID = templateUID;
}
/**
- * This method is used for getting the user friendly name of the {@link Rule}. It's optional property.
+ * This method is used to obtain the {@link Rule}'s human-readable name.
*
- * @return the name of rule or null.
+ * @return the {@link Rule}'s human-readable name, or {@code null}.
*/
- public String getName() {
+ public @Nullable String getName() {
return name;
}
/**
- * This method is used for setting a friendly name of the Rule. This property
- * can be changed only when the Rule is not in active state.
+ * This method is used to specify the {@link Rule}'s human-readable name.
*
- * @param ruleName a new name.
- * @throws IllegalStateException when the rule is in active state
+ * @param ruleName the {@link Rule}'s human-readable name, or {@code null}.
*/
- public void setName(String ruleName) throws IllegalStateException {
+ public void setName(@Nullable String ruleName) {
name = ruleName;
}
/**
- * Rules can have
- *
- *
tags - non-hierarchical keywords or terms for describing them. This method is
- * used for getting the tags assign to this Rule. The tags are used to filter the rules.
- *
+ * This method is used to obtain the {@link Rule}'s assigned tags.
*
- * @return a {@link Set} of tags
+ * @return the {@link Rule}'s assigned tags.
*/
public Set getTags() {
- return tags = tags != null ? tags : Collections. emptySet();
+ return tags;
}
/**
- * Rules can have
- *
- *
tags - non-hierarchical keywords or terms for describing them. This method is
- * used for setting the tags to this rule. This property can be changed only when the Rule is not in active state.
- * The tags are used to filter the rules.
- *
+ * This method is used to specify the {@link Rule}'s assigned tags.
*
- * @param ruleTags list of tags assign to this Rule.
- * @throws IllegalStateException when the rule is in active state.
+ * @param ruleTags the {@link Rule}'s assigned tags.
*/
- public void setTags(Set ruleTags) throws IllegalStateException {
- tags = ruleTags != null ? ruleTags : Collections. emptySet();
+ @SuppressWarnings("null")
+ public void setTags(Set ruleTags) {
+ tags = ruleTags != null ? ruleTags : new HashSet<>();
}
/**
- * This method is used for getting the description of the Rule. The
- * description is a long, user friendly description of the Rule defined by
- * this descriptor.
+ * This method is used to obtain the human-readable description of the purpose and consequences of the
+ * {@link Rule}'s execution.
*
- * @return the description of the Rule.
+ * @return the {@link Rule}'s human-readable description, or {@code null}.
*/
- public String getDescription() {
+ public @Nullable String getDescription() {
return description;
}
/**
- * This method is used for setting the description of the Rule. The
- * description is a long, user friendly description of the Rule defined by
- * this descriptor.
+ * This method is used to specify human-readable description of the purpose and consequences of the
+ * {@link Rule}'s execution.
*
- * @param ruleDescription of the Rule.
+ * @param ruleDescription the {@link Rule}'s human-readable description, or {@code null}.
*/
- public void setDescription(String ruleDescription) {
+ public void setDescription(@Nullable String ruleDescription) {
description = ruleDescription;
}
/**
- * This method is used to show visibility of the Rule
+ * This method is used to obtain the {@link Rule}'s {@link Visibility}.
*
- * @return visibility of rule
+ * @return the {@link Rule}'s {@link Visibility} value.
*/
public Visibility getVisibility() {
- if (visibility == null) {
- return Visibility.VISIBLE;
- }
return visibility;
}
+ /**
+ * This method is used to specify the {@link Rule}'s {@link Visibility}.
+ *
+ * @param visibility the {@link Rule}'s {@link Visibility} value.
+ */
+ @SuppressWarnings("null")
public void setVisibility(Visibility visibility) {
- this.visibility = visibility;
+ this.visibility = visibility == null ? Visibility.VISIBLE : visibility;
}
/**
- * This method is used for getting Map with configuration values of the {@link Rule} Key -id of the
- * {@link ConfigDescriptionParameter} Value - the
- * value of the corresponding property
+ * This method is used to obtain the {@link Rule}'s {@link Configuration}.
*
- * @return current configuration values
+ * @return current configuration values, or an empty {@link Configuration}.
*/
public Configuration getConfiguration() {
- if (configuration == null) {
- configuration = new Configuration();
- }
return configuration;
}
/**
- * This method is used for setting the Map with configuration values of the {@link Rule}. Key - id of the
- * {@link ConfigDescriptionParameter} Value -
- * the value of the corresponding property
+ * This method is used to specify the {@link Rule}'s {@link Configuration}.
*
- * @param ruleConfiguration new configuration values.
+ * @param ruleConfiguration the new configuration values.
*/
+ @SuppressWarnings("null")
public void setConfiguration(Configuration ruleConfiguration) {
- this.configuration = ruleConfiguration;
+ this.configuration = ruleConfiguration == null ? new Configuration() : ruleConfiguration;
}
/**
- * This method is used for getting the {@link List} with {@link ConfigDescriptionParameter}s
- * defining meta info for configuration properties of the Rule.
+ * This method is used to obtain the {@link List} with {@link ConfigDescriptionParameter}s
+ * defining meta info for configuration properties of the {@link Rule}.
*
- * @return a {@link Set} of {@link ConfigDescriptionParameter}s.
+ * @return a {@link List} of {@link ConfigDescriptionParameter}s.
*/
public List getConfigurationDescriptions() {
- if (configDescriptions == null) {
- configDescriptions = new ArrayList(3);
- }
return configDescriptions;
}
+ /**
+ * This method is used to describe with {@link ConfigDescriptionParameter}s
+ * the meta info for configuration properties of the {@link Rule}.
+ */
+ @SuppressWarnings("null")
public void setConfigurationDescriptions(List configDescriptions) {
- this.configDescriptions = (configDescriptions == null) ? new ArrayList(3)
- : configDescriptions;
+ this.configDescriptions = configDescriptions == null ? new ArrayList<>() : configDescriptions;
}
+ /**
+ * This method is used to get the conditions participating in {@link Rule}.
+ *
+ * @return a list with the conditions that belong to this {@link Rule}.
+ */
public List getConditions() {
- if (conditions == null) {
- conditions = new ArrayList(3);
- }
return conditions;
}
+ /**
+ * This method is used to specify the conditions participating in {@link Rule}.
+ *
+ * @param conditions a list with the conditions that should belong to this {@link Rule}.
+ */
+ @SuppressWarnings("null")
public void setConditions(List conditions) {
- this.conditions = (conditions == null) ? new ArrayList(3) : conditions;
+ this.conditions = conditions == null ? new ArrayList<>() : conditions;
}
+ /**
+ * This method is used to get the actions participating in {@link Rule}.
+ *
+ * @return a list with the actions that belong to this {@link Rule}.
+ */
public List getActions() {
- if (actions == null) {
- actions = new ArrayList(3);
- }
return actions;
}
+ /**
+ * This method is used to specify the actions participating in {@link Rule}
+ *
+ * @param actions a list with the actions that should belong to this {@link Rule}.
+ */
+ @SuppressWarnings("null")
public void setActions(List actions) {
- this.actions = (actions == null) ? new ArrayList(3) : actions;
+ this.actions = actions == null ? new ArrayList<>() : actions;
}
+ /**
+ * This method is used to get the triggers participating in {@link Rule}
+ *
+ * @return a list with the triggers that belong to this {@link Rule}.
+ */
public List getTriggers() {
- if (triggers == null) {
- triggers = new ArrayList(3);
- }
return triggers;
}
+ /**
+ * This method is used to specify the triggers participating in {@link Rule}
+ *
+ * @param triggers a list with the triggers that should belong to this {@link Rule}.
+ */
+ @SuppressWarnings("null")
public void setTriggers(List triggers) {
- this.triggers = (triggers == null) ? new ArrayList(3) : triggers;
+ this.triggers = triggers == null ? new ArrayList<>() : triggers;
}
/**
- * This method is used to get a module participating in Rule
+ * This method is used to get a {@link Module} participating in {@link Rule}
*
- * @param moduleId unique id of the module in this rule.
- * @return module with specified id or null when it does not exist.
+ * @param moduleId specifies the id of a module belonging to this {@link Rule}.
+ * @return module with specified id or {@code null} if it does not belong to this {@link Rule}.
*/
- public Module getModule(String moduleId) {
- Module module = getModule(moduleId, triggers);
- if (module != null) {
- return module;
- }
-
- module = getModule(moduleId, conditions);
- if (module != null) {
- return module;
- }
-
- module = getModule(moduleId, actions);
- if (module != null) {
- return module;
- }
- return null;
- }
-
- private T getModule(String moduleUID, List modules) {
- if (modules != null) {
- for (T module : modules) {
- if (module.getId().equals(moduleUID)) {
- return module;
- }
+ public @Nullable Module getModule(String moduleId) {
+ for (Module module : getModules(Module.class)) {
+ if (module.getId().equals(moduleId)) {
+ return module;
}
}
return null;
}
/**
- * This method is used to return the module of this rule.
+ * This method is used to obtain the modules of the {@link Rule}, corresponding to the specified class.
*
- * @param moduleClazz optional parameter defining type looking modules. The
- * types are {@link Trigger}, {@link Condition} or {@link Action}
- * @return list of modules of defined type or all modules when the type is not
- * specified.
+ * @param moduleClazz defines the class of the looking modules. It can be {@link Module}, {@link Trigger},
+ * {@link Condition} or {@link Action}.
+ * @return the modules of defined type or empty list if the {@link Rule} has no modules that belong to the specified
+ * type.
*/
@SuppressWarnings("unchecked")
public List getModules(Class moduleClazz) {
- List result = null;
- if (moduleClazz == null) {
- result = new ArrayList();
- result.addAll((Collection extends T>) getTriggers());
- result.addAll((Collection extends T>) getConditions());
- result.addAll((Collection extends T>) getActions());
+ final List result;
+ if (Module.class == moduleClazz) {
+ List modules = new ArrayList();
+ modules.addAll(triggers);
+ modules.addAll(conditions);
+ modules.addAll(actions);
+ result = (List) Collections.unmodifiableList(modules);
} else if (Trigger.class == moduleClazz) {
- result = (List) getTriggers();
+ result = (List) triggers;
} else if (Condition.class == moduleClazz) {
- result = (List) getConditions();
+ result = (List) conditions;
} else if (Action.class == moduleClazz) {
- result = (List) getActions();
+ result = (List) actions;
+ } else {
+ result = Collections.emptyList();
}
- return result != null ? result : Collections. emptyList();
+ return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((uid == null) ? 0 : uid.hashCode());
+ result = prime * result + uid.hashCode();
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
@@ -359,11 +373,7 @@ public boolean equals(Object obj) {
return false;
}
Rule other = (Rule) obj;
- if (uid == null) {
- if (other.uid != null) {
- return false;
- }
- } else if (!uid.equals(other.uid)) {
+ if (!uid.equals(other.uid)) {
return false;
}
return true;
diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/RulePredicates.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/RulePredicates.java
index a84157a6fc5..741c3f8f4d5 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/RulePredicates.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/RulePredicates.java
@@ -47,16 +47,12 @@ public class RulePredicates {
public static String getPrefix(Rule rule) {
if (null != rule) {
final String uid = rule.getUID();
- if (null != uid) {
- final int index = uid.indexOf(PREFIX_SEPARATOR);
-
- // only when a delimiter was found and the prefix is not empty
- if (0 < index) {
- return uid.substring(0, index);
- }
+ final int index = uid.indexOf(PREFIX_SEPARATOR);
+ // only when a delimiter was found and the prefix is not empty
+ if (0 < index) {
+ return uid.substring(0, index);
}
}
-
return null;
}
diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Visibility.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Visibility.java
index 1faeb71a89a..9f0851ebea6 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Visibility.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/Visibility.java
@@ -11,24 +11,24 @@
import org.eclipse.smarthome.automation.type.ModuleType;
/**
- * Defines visibility values of {@link ModuleType}s and {@link Template}s
+ * Defines visibility values of {@link Rule}s, {@link ModuleType}s and {@link Template}s
*
* @author Yordan Mihaylov - Initial Contribution
*
*/
public enum Visibility {
/**
- * The UI has to show this object.
+ * The UI has always to show an object with such visibility.
*/
VISIBLE,
/**
- * The UI has to hide this object.
+ * The UI has always to hide an object with such visibility.
*/
HIDDEN,
/**
- * The UI has to show this object only to experts.
+ * The UI has to show an object with such visibility only to experts.
*/
EXPERT
diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/RuleTemplate.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/RuleTemplate.java
index d3d253d1c10..d8f0b1f5c5b 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/RuleTemplate.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/RuleTemplate.java
@@ -8,12 +8,13 @@
package org.eclipse.smarthome.automation.template;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.Collections;
-import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.UUID;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.automation.Action;
import org.eclipse.smarthome.automation.Condition;
import org.eclipse.smarthome.automation.Module;
@@ -23,12 +24,11 @@
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
/**
- * The templates define types of shared, ready to use rule definitions, which
- * can be instantiated and configured to produce {@link Rule} instances . Each
- * Template has a unique id.
+ * The {@link RuleTemplate} defines a shared, ready to use - rule definition, which can be configured to produce
+ * {@link Rule} instances.
*
- * The {@link RuleTemplate}s can be used by any creator of Rules, but they can be modified only by its creator. The
- * template modification is done by updating the {@link RuleTemplate} through {@link TemplateRegistry}
+ * The {@link RuleTemplate}s can be used by any creator of Rules, but they can be modified only by its creator.
+ * The template modification is done by updating the {@link RuleTemplate}.
*
* Templates can have tags - non-hierarchical keywords or terms for describing them.
*
@@ -37,97 +37,95 @@
* @author Vasil Ilchev - Initial Contribution
* @author Markus Rathgeb - Add default constructor for deserialization
*/
+@NonNullByDefault
public class RuleTemplate implements Template {
/**
- * This field holds an unique identifier of the {@link RuleTemplate} instance.
+ * This field holds the {@link RuleTemplate}'s identifier, specified by its creator or randomly generated.
*/
- private String uid;
+ private final String uid;
/**
- * This field holds a list with the unique {@link Trigger}s participating in the {@link Rule} and starting its
- * execution.
+ * This field holds a list with the {@link Trigger}s participating in the {@link RuleTemplate}.
*/
- private List triggers;
+ private final List triggers;
/**
- * This field holds a list with the unique {@link Condition}s participating in the {@link Rule} and determine the
- * completion of the execution.
+ * This field holds a list with the {@link Condition}s participating in the {@link RuleTemplate}.
*/
- private List conditions;
+ private final List conditions;
/**
- * This field holds a list with the unique {@link Action}s participating in the {@link Rule} and are the real work
- * that will be done by the rule.
+ * This field holds a list with the {@link Action}s participating in the {@link RuleTemplate}.
*/
- private List actions;
+ private final List actions;
/**
* This field holds a set of non-hierarchical keywords or terms for describing the {@link RuleTemplate}.
*/
- private Set tags;
+ private final Set tags;
/**
- * This field holds the short, user friendly name of the Template.
+ * This field holds the short, human-readable label of the {@link RuleTemplate}.
*/
- private String label;
+ @Nullable
+ private final String label;
/**
- * This field describes the usage of the {@link Rule} and its benefits.
+ * This field describes the usage of the {@link RuleTemplate} and its benefits.
*/
- private String description;
+ @Nullable
+ private final String description;
/**
- * This field determines {@link Visibility} of the template.
+ * This field determines {@link Visibility} of the {@link RuleTemplate}.
*/
- private Visibility visibility;
+ private final Visibility visibility;
/**
- * This field defines a set of configuration properties of the {@link Rule}.
+ * This field defines a set of configuration properties of the future {@link Rule} instances.
*/
- private List configDescriptions;
+ private final List configDescriptions;
/**
- * Default constructor for deserialization e.g. by Gson.
- */
- protected RuleTemplate() {
- }
-
- /**
- * This constructor creates a {@link RuleTemplate} instance.
+ * This constructor creates a {@link RuleTemplate} instance that will be used for creating {@link Rule}s from a set
+ * of modules, belong to the template. When {@code null} is passed for the {@code uid} parameter, the
+ * {@link RuleTemplate}'s identifier will be randomly generated.
*
- * @param UID is an unique identifier of the {@link RuleTemplate} instance
- * @param label short human readable description
- * @param description a detailed human readable description
- * @param tags a set of tags
- * @param triggers list of unique {@link Trigger}s participating in the {@link Rule}
- * @param conditions list of unique {@link Condition}s participating in the {@link Rule}
- * @param actions list of unique {@link Action}s participating in the {@link Rule}
- * @param configDescriptions set of configuration properties of the {@link Rule}
- * @param visibility defines if the template can be public or private
+ * @param uid the {@link RuleTemplate}'s identifier, or {@code null} if a random identifier should be generated.
+ * @param label the short human-readable {@link RuleTemplate}'s label.
+ * @param description a detailed human-readable {@link RuleTemplate}'s description.
+ * @param tags the {@link RuleTemplate}'s assigned tags.
+ * @param triggers the {@link RuleTemplate}'s triggers list, or {@code null} if the {@link RuleTemplate} should have
+ * no triggers.
+ * @param conditions the {@link RuleTemplate}'s conditions list, or {@code null} if the {@link RuleTemplate} should
+ * have no conditions.
+ * @param actions the {@link RuleTemplate}'s actions list, or {@code null} if the {@link RuleTemplate} should have
+ * no actions.
+ * @param configDescriptions describing metadata for the configuration of the future {@link Rule} instances.
+ * @param visibility the {@link RuleTemplate}'s visibility.
*/
- public RuleTemplate(String UID, String label, String description, Set tags, List triggers,
- List conditions, List actions, List configDescriptions,
- Visibility visibility) {
-
- this.uid = UID;
+ public RuleTemplate(@Nullable String UID, @Nullable String label, @Nullable String description,
+ @Nullable Set tags, @Nullable List triggers, @Nullable List conditions,
+ @Nullable List actions, @Nullable List configDescriptions,
+ @Nullable Visibility visibility) {
+ this.uid = UID == null ? UUID.randomUUID().toString() : UID;
this.label = label;
this.description = description;
- this.triggers = triggers;
- this.conditions = conditions;
- this.actions = actions;
- this.configDescriptions = configDescriptions;
- this.visibility = visibility;
- if (tags == null || tags.isEmpty()) {
- return;
- }
- this.tags = new HashSet(tags);
+ this.triggers = triggers == null ? Collections.emptyList() : Collections.unmodifiableList(triggers);
+ this.conditions = conditions == null ? Collections.emptyList() : Collections.unmodifiableList(conditions);
+ this.actions = actions == null ? Collections.emptyList() : Collections.unmodifiableList(actions);
+ this.configDescriptions = configDescriptions == null ? Collections.emptyList()
+ : Collections.unmodifiableList(configDescriptions);
+ this.visibility = visibility == null ? Visibility.VISIBLE : visibility;
+ this.tags = tags == null ? Collections.emptySet() : Collections.unmodifiableSet(tags);
}
/**
- * This method is used for getting the type of Template. It is unique in scope of RuleEngine.
+ * This method is used to obtain the identifier of the {@link RuleTemplate}. It can be specified by the
+ * {@link RuleTemplate}'s creator, or randomly generated.
*
- * @return the unique id of Template.
+ * @return an identifier of this {@link RuleTemplate}. Can't be {@code null}.
*/
@Override
public String getUID() {
@@ -135,122 +133,136 @@ public String getUID() {
}
/**
- * This method is used for getting the assigned tags to this Template. The tags are
- * non-hierarchical keywords or terms that are used for describing the template and to filter the templates.
+ * This method is used to obtain the {@link RuleTemplate}'s assigned tags.
*
- * @return tags that is a set of non-hierarchical keywords or terms, describing the template.
+ * @return the {@link RuleTemplate}'s assigned tags.
*/
@Override
public Set getTags() {
- return tags != null ? tags : Collections. emptySet();
+ return tags;
}
/**
- * This method is used for getting the label of the Template.
+ * This method is used to obtain the {@link RuleTemplate}'s human-readable label.
*
- * @return the short, user friendly name of the Template.
+ * @return the {@link RuleTemplate}'s human-readable label, or {@code null}.
*/
@Override
- public String getLabel() {
+ public @Nullable String getLabel() {
return label;
}
/**
- * This method is used for getting the description of the Template. The
- * description is a long, user friendly description of the Rule defined by
- * this Template.
+ * This method is used to obtain the human-readable description of the purpose of the {@link RuleTemplate}.
*
- * @return the description of the Template.
+ * @return the {@link RuleTemplate}'s human-readable description, or {@code null}.
*/
@Override
- public String getDescription() {
+ public @Nullable String getDescription() {
return description;
}
/**
- * This method is used to show visibility of the template
+ * This method is used to obtain the {@link RuleTemplate}'s {@link Visibility}.
*
- * @return visibility of template
+ * @return the {@link RuleTemplate}'s {@link Visibility} value.
*/
@Override
public Visibility getVisibility() {
- if (visibility == null) {
- return Visibility.VISIBLE;
- }
return visibility;
}
/**
- * This method is used for getting the Set with {@link ConfigDescriptionParameter}s defining meta info for
- * configuration properties of the Rule.
+ * This method is used to obtain the {@link List} with {@link ConfigDescriptionParameter}s
+ * defining meta info for configuration properties of the future {@link Rule} instances.
*
- * @return a {@link Set} of {@link ConfigDescriptionParameter}s.
+ * @return a {@link List} of {@link ConfigDescriptionParameter}s.
*/
public List getConfigurationDescriptions() {
- return configDescriptions != null ? configDescriptions : Collections. emptyList();
+ return configDescriptions;
}
/**
- * This method is used to get a {@link Module} participating in Rule
+ * This method is used to get a {@link Module} participating in {@link RuleTemplate}
*
- * @param id unique id of the module in this rule.
- * @param the type of the module
+ * @param moduleId unique id of the module in this {@link RuleTemplate}.
* @return module with specified id or null when it does not exist.
*/
- public T getModule(String id) {
+ public @Nullable Module getModule(String moduleId) {
+ for (Module module : getModules(Module.class)) {
+ if (module.getId().equals(moduleId)) {
+ return module;
+ }
+ }
return null;
}
/**
- * This method is used to return a group of {@link Module}s of this rule
+ * This method is used to obtain the modules of the {@link RuleTemplate}, corresponding to the specified class.
*
- * @param moduleClazz optional parameter defining type looking modules. The types
- * are {@link Trigger}, {@link Condition} or {@link Action}
- * @param the type of the module that is required
- * @return list of modules of defined type or all modules when the type is not
- * specified.
+ * @param moduleClazz defines the class of the looking modules. It can be {@link Module}, {@link Trigger},
+ * {@link Condition} or {@link Action}.
+ * @return the modules of defined type or empty list if the {@link RuleTemplate} has no modules that belong to the
+ * specified type.
*/
@SuppressWarnings("unchecked")
- @Deprecated
public List getModules(Class moduleClazz) {
- List result = null;
- if (moduleClazz == null) {
- result = new ArrayList();
- result.addAll((Collection extends T>) triggers);
- result.addAll((Collection extends T>) conditions);
- result.addAll((Collection extends T>) actions);
+ final List result;
+ if (Module.class == moduleClazz) {
+ List modules = new ArrayList();
+ modules.addAll(triggers);
+ modules.addAll(conditions);
+ modules.addAll(actions);
+ result = (List) Collections.unmodifiableList(modules);
} else if (Trigger.class == moduleClazz) {
result = (List) triggers;
} else if (Condition.class == moduleClazz) {
result = (List) conditions;
} else if (Action.class == moduleClazz) {
result = (List) actions;
+ } else {
+ result = Collections.emptyList();
}
- return result != null ? result : Collections. emptyList();
+ return result;
}
+ /**
+ * This method is used to get the triggers participating in {@link RuleTemplate}.
+ *
+ * @return a list with the triggers that belong to this {@link RuleTemplate}.
+ */
public List getTriggers() {
- return triggers != null ? triggers : Collections. emptyList();
+ return triggers;
}
+ /**
+ * This method is used to get the conditions participating in {@link RuleTemplate}.
+ *
+ * @return a list with the conditions that belong to this {@link RuleTemplate}.
+ */
public List getConditions() {
- return conditions != null ? conditions : Collections. emptyList();
+ return conditions;
}
+ /**
+ * This method is used to get the actions participating in {@link RuleTemplate}.
+ *
+ * @return a list with the actions that belong to this {@link RuleTemplate}.
+ */
public List getActions() {
- return actions != null ? actions : Collections. emptyList();
+ return actions;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((uid == null) ? 0 : uid.hashCode());
+ result = prime * result + uid.hashCode();
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
@@ -261,11 +273,7 @@ public boolean equals(Object obj) {
return false;
}
RuleTemplate other = (RuleTemplate) obj;
- if (uid == null) {
- if (other.uid != null) {
- return false;
- }
- } else if (!uid.equals(other.uid)) {
+ if (!uid.equals(other.uid)) {
return false;
}
return true;
diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/Template.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/Template.java
index e2b38f7b0e5..52edd9c95c6 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/Template.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/template/Template.java
@@ -9,6 +9,8 @@
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.automation.Rule;
import org.eclipse.smarthome.automation.Visibility;
import org.eclipse.smarthome.core.common.registry.Identifiable;
@@ -27,6 +29,7 @@
* @author Ana Dimova - Initial Contribution
* @author Vasil Ilchev - Initial Contribution
*/
+@NonNullByDefault
public interface Template extends Identifiable {
/**
@@ -55,7 +58,7 @@ public interface Template extends Identifiable {
*
* @return the label of the Template.
*/
- public String getLabel();
+ public @Nullable String getLabel();
/**
* This method is used for getting the description of the Template. The
@@ -64,7 +67,7 @@ public interface Template extends Identifiable {
*
* @return the description of the Template.
*/
- public String getDescription();
+ public @Nullable String getDescription();
/**
* This method is used to show visibility of the template
diff --git a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/type/ActionType.java b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/type/ActionType.java
index 87f39b436c1..263f4b10f76 100644
--- a/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/type/ActionType.java
+++ b/bundles/automation/org.eclipse.smarthome.automation.api/src/main/java/org/eclipse/smarthome/automation/type/ActionType.java
@@ -11,17 +11,17 @@
import java.util.List;
import java.util.Set;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.automation.Action;
import org.eclipse.smarthome.automation.Module;
import org.eclipse.smarthome.automation.Visibility;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
/**
- * This class provides common functionality for creating {@link Action} instances by supplying types with their
- * meta-information. The {@link Action}s are part of "THEN" section of the Rule. Each {@link ActionType} is defined by
- * unique id in scope of the RuleEngine and defines {@link ConfigDescriptionParameter}s that are meta-information for
- * configuration and meta-information for {@link Input}s and {@link Output}s used for creation of {@link Action}
- * instances.
+ * This class provides common functionality for creating {@link Action} instances by supplying their meta-information.
+ * Each {@link ActionType} is uniquely identifiable in scope of the {@link ModuleTypeRegistry} and defines
+ * {@link ConfigDescriptionParameter}s that are meta-information for configuration of the future {@link Action}
+ * instances and meta-information for {@link Input}s and {@link Output}s used from these {@link Action} instances.
*
* @author Yordan Mihaylov - Initial Contribution
* @author Ana Dimova - Initial Contribution
@@ -33,30 +33,25 @@ public class ActionType extends ModuleType {
* This field contains meta-information describing the incoming connections of the {@link Action} module to the
* other {@link Module}s.
*/
- private List inputs;
+ private final List inputs;
/**
* This field contains meta-information describing the outgoing connections of the {@link Action} module to the
* other {@link Action}s.
*/
- private List