use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.
the class RuleTriggerManager method removeRules.
private void removeRules(TriggerTypes type, Collection<? extends Collection<Rule>> ruleSets, RuleModel model) {
for (Collection<Rule> ruleSet : ruleSets) {
Set<Rule> clonedSet = new HashSet<Rule>(ruleSet);
// first remove all rules of the model, if not null (=non-existent)
if (model != null) {
for (Rule newRule : model.getRules()) {
for (Rule oldRule : clonedSet) {
if (newRule.getName().equals(oldRule.getName())) {
ruleSet.remove(oldRule);
if (type == TIMER) {
removeTimerRule(oldRule);
}
}
}
}
}
// now also remove all proxified rules from the set
clonedSet = new HashSet<Rule>(ruleSet);
for (Rule rule : clonedSet) {
if (rule.eResource() == null) {
ruleSet.remove(rule);
if (type == TIMER) {
removeTimerRule(rule);
}
}
}
}
}
use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.
the class RuleTriggerManager method internalGetChangeRules.
private void internalGetChangeRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes, State newState, State oldState, List<Rule> result) {
final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
for (Rule rule : getAllRules(CHANGE, mapName)) {
for (EventTrigger t : rule.getEventtrigger()) {
String triggerOldStateString = null;
String triggerNewStateString = null;
if ((!isGroup) && (t instanceof ChangedEventTrigger)) {
final ChangedEventTrigger ct = (ChangedEventTrigger) t;
if (ct.getItem().equals(name)) {
triggerOldStateString = ct.getOldState();
triggerNewStateString = ct.getNewState();
} else {
continue;
}
} else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
if (gmct.getGroup().equals(name)) {
triggerOldStateString = gmct.getOldState();
triggerNewStateString = gmct.getNewState();
} else {
continue;
}
} else {
continue;
}
if (triggerOldStateString != null) {
final State triggerOldState = TypeParser.parseState(acceptedDataTypes, triggerOldStateString);
if (!oldState.equals(triggerOldState)) {
continue;
}
}
if (triggerNewStateString != null) {
final State triggerNewState = TypeParser.parseState(acceptedDataTypes, triggerNewStateString);
if (!newState.equals(triggerNewState)) {
continue;
}
}
result.add(rule);
}
}
}
use of org.eclipse.smarthome.model.rule.rules.Rule in project smarthome by eclipse.
the class ExecuteRuleJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
String modelName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULEMODEL);
String ruleName = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_RULENAME);
if (modelRepository != null && scriptEngine != null) {
EObject model = modelRepository.getModel(modelName);
if (model instanceof RuleModel) {
RuleModel ruleModel = (RuleModel) model;
Rule rule = getRule(ruleModel, ruleName);
if (rule != null) {
Script script = scriptEngine.newScriptFromXExpression(rule.getScript());
logger.debug("Executing scheduled rule '{}'", rule.getName());
try {
script.execute(RuleContextHelper.getContext(rule, injector));
} catch (ScriptExecutionException e) {
logger.error("Error during the execution of rule '{}': {}", rule.getName(), e.getMessage());
}
} else {
logger.debug("Scheduled rule '{}' does not exist", ruleName);
}
} else {
logger.debug("Rule file '{}' does not exist", modelName);
}
}
}
Aggregations