use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.
the class RuleTemplateRegistry method getByTags.
@Override
public Collection<RuleTemplate> getByTags(Locale locale, String... tags) {
Set<String> tagSet = tags != null ? new HashSet<String>(Arrays.asList(tags)) : null;
Collection<RuleTemplate> result = new ArrayList<RuleTemplate>(20);
for (Provider<RuleTemplate> provider : elementMap.keySet()) {
for (RuleTemplate resultTemplate : elementMap.get(provider)) {
Collection<String> tTags = resultTemplate.getTags();
RuleTemplate t = locale == null ? resultTemplate : ((RuleTemplateProvider) provider).getTemplate(resultTemplate.getUID(), locale);
if (tagSet == null) {
result.add(t);
} else if (tTags != null && tTags.containsAll(tagSet)) {
result.add(t);
}
}
}
return result;
}
use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.
the class RuleRegistryImpl method resolveRuleByTemplate.
/**
* The method checks if the rule has to be resolved by template or not. If the rule does not contain tempateUID it
* returns same rule, otherwise it tries to resolve the rule created from template. If the template is available
* the method creates a new rule based on triggers, conditions and actions from template. If the template is not
* available returns the same rule.
*
* @param rule a rule defined by template.
* @return the resolved rule(containing modules defined by the template) or not resolved rule, if the template is
* missing.
*/
private Rule resolveRuleByTemplate(Rule rule) {
String templateUID = rule.getTemplateUID();
if (templateUID != null) {
RuleTemplate template = templateRegistry.get(templateUID);
if (template == null) {
logger.debug("Rule template {} does not exist.", templateUID);
return rule;
} else {
Rule resolvedRule = new Rule(rule.getUID(), RuleUtils.getTriggersCopy(template.getTriggers()), RuleUtils.getConditionsCopy(template.getConditions()), RuleUtils.getActionsCopy(template.getActions()), template.getConfigurationDescriptions(), rule.getConfiguration(), null, rule.getVisibility());
String name = rule.getName();
if (name != null) {
resolvedRule.setName(name);
}
resolvedRule.setTags(rule.getTags());
String description = rule.getDescription();
if (description != null) {
resolvedRule.setDescription(description);
}
ruleEngine.resolveConfiguration(resolvedRule);
return resolvedRule;
}
}
return rule;
}
use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.
the class TemplateGSONParser method parse.
@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
JsonReader jr = new JsonReader(reader);
try {
if (jr.hasNext()) {
JsonToken token = jr.peek();
Set<Template> templates = new HashSet<>();
if (JsonToken.BEGIN_ARRAY.equals(token)) {
templates.addAll(gson.fromJson(jr, new TypeToken<List<RuleTemplate>>() {
}.getType()));
} else {
Template template = gson.fromJson(jr, RuleTemplate.class);
templates.add(template);
}
return templates;
}
} catch (Exception e) {
throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
} finally {
try {
jr.close();
} catch (IOException e) {
}
}
return Collections.emptySet();
}
use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.
the class AutomationCommandList method getTemplateByFilter.
/**
* This method reduces the list of {@link Template}s so that their unique identifier or part of it to match the
* {@link #id} or
* the index in the <tt>list</tt> to match the {@link #id}.
*
* @param list is the list of {@link Template}s for reducing.
* @return a collection of {@link Template}s that match the filter.
*/
private Collection<RuleTemplate> getTemplateByFilter(Map<String, String> list) {
Collection<RuleTemplate> templates = new ArrayList<RuleTemplate>();
RuleTemplate t = null;
String uid = list.get(id);
if (uid != null) {
t = autoCommands.getTemplate(uid, locale);
if (t != null) {
templates.add(t);
return templates;
}
} else {
t = autoCommands.getTemplate(id, locale);
if (t != null) {
templates.add(t);
return templates;
} else {
for (String templateUID : list.keySet()) {
if (templateUID.indexOf(id) != -1) {
templates.add(autoCommands.getTemplate(templateUID, locale));
}
}
}
}
return templates;
}
use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.
the class AutomationCommandList method listTemplates.
/**
* This method is responsible for execution of command {@link AutomationCommands#LIST_TEMPLATES}.
*
* @return a string representing understandable for the user message containing information on the outcome of the
* command {@link AutomationCommands#LIST_TEMPLATES}.
*/
private String listTemplates() {
Collection<RuleTemplate> collection = autoCommands.getTemplates(locale);
Map<String, Template> templates = new Hashtable<String, Template>();
Map<String, String> listTemplates = null;
if (collection != null && !collection.isEmpty()) {
addCollection(collection, templates);
String[] uids = new String[templates.size()];
Utils.quickSort(templates.keySet().toArray(uids), 0, templates.size());
listTemplates = Utils.putInHastable(uids);
}
if (listTemplates != null && !listTemplates.isEmpty()) {
if (id != null) {
collection = getTemplateByFilter(listTemplates);
if (collection.size() == 1) {
Template t = (Template) collection.toArray()[0];
if (t != null) {
return Printer.printTemplate(t);
} else {
return String.format("Nonexistent ID: %s", id);
}
} else if (collection.isEmpty()) {
return String.format("Nonexistent ID: %s", id);
} else {
if (!templates.isEmpty()) {
templates.clear();
}
addCollection(collection, templates);
listTemplates = Utils.filterList(templates, listTemplates);
}
}
if (listTemplates != null && !listTemplates.isEmpty()) {
return Printer.printTemplates(listTemplates);
}
}
return "There are no Templates available!";
}
Aggregations