Skip to content

Commit

Permalink
[automation] Avoid disappearing rules by correctly checking the model…
Browse files Browse the repository at this point in the history
… and its type (openhab#2077)

Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer committed Jan 11, 2021
1 parent 044bd40 commit 66766d5
Showing 1 changed file with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,42 +129,45 @@ public void removeProviderChangeListener(ProviderChangeListener<Rule> listener)

@Override
public void modelChanged(String modelFileName, EventType type) {
String ruleModelName = modelFileName.substring(0, modelFileName.lastIndexOf("."));
switch (type) {
case ADDED:
EObject model = modelRepository.getModel(modelFileName);
if (model instanceof RuleModel) {
RuleModel ruleModel = (RuleModel) model;
int index = 1;
for (org.openhab.core.model.rule.rules.Rule rule : ruleModel.getRules()) {
addRule(toRule(ruleModelName, rule, index));
xExpressions.put(ruleModelName + "-" + index, rule.getScript());
index++;
String ruleModelType = modelFileName.substring(modelFileName.lastIndexOf(".") + 1);
if ("rules".equalsIgnoreCase(ruleModelType)) {
String ruleModelName = modelFileName.substring(0, modelFileName.lastIndexOf("."));
switch (type) {
case ADDED:
EObject model = modelRepository.getModel(modelFileName);
if (model instanceof RuleModel) {
RuleModel ruleModel = (RuleModel) model;
int index = 1;
for (org.openhab.core.model.rule.rules.Rule rule : ruleModel.getRules()) {
addRule(toRule(ruleModelName, rule, index));
xExpressions.put(ruleModelName + "-" + index, rule.getScript());
index++;
}
handleVarDeclarations(ruleModelName, ruleModel);
}
handleVarDeclarations(ruleModelName, ruleModel);
}
break;
case MODIFIED:
removeRuleModel(ruleModelName);
EObject modifiedModel = modelRepository.getModel(modelFileName);
if (modifiedModel instanceof RuleModel) {
RuleModel ruleModel = (RuleModel) modifiedModel;
int index = 1;
for (org.openhab.core.model.rule.rules.Rule rule : ruleModel.getRules()) {
Rule newRule = toRule(ruleModelName, rule, index);
Rule oldRule = rules.get(ruleModelName);
updateRule(oldRule, newRule);
xExpressions.put(ruleModelName + "-" + index, rule.getScript());
index++;
break;
case MODIFIED:
removeRuleModel(ruleModelName);
EObject modifiedModel = modelRepository.getModel(modelFileName);
if (modifiedModel instanceof RuleModel) {
RuleModel ruleModel = (RuleModel) modifiedModel;
int index = 1;
for (org.openhab.core.model.rule.rules.Rule rule : ruleModel.getRules()) {
Rule newRule = toRule(ruleModelName, rule, index);
Rule oldRule = rules.get(ruleModelName);
updateRule(oldRule, newRule);
xExpressions.put(ruleModelName + "-" + index, rule.getScript());
index++;
}
handleVarDeclarations(ruleModelName, ruleModel);
}
handleVarDeclarations(ruleModelName, ruleModel);
}
break;
case REMOVED:
removeRuleModel(ruleModelName);
break;
default:
logger.debug("Unknown event type.");
break;
case REMOVED:
removeRuleModel(ruleModelName);
break;
default:
logger.debug("Unknown event type.");
}
}
}

Expand Down Expand Up @@ -209,21 +212,30 @@ private void removeRuleModel(String modelName) {
Iterator<Entry<String, Rule>> it = rules.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Rule> entry = it.next();
if (entry.getKey().startsWith(modelName + "-")) {
if (belongsToModel(entry.getKey(), modelName)) {
removeRule(entry.getValue());
it.remove();
}
}
Iterator<Entry<String, XExpression>> it2 = xExpressions.entrySet().iterator();
while (it2.hasNext()) {
Entry<String, XExpression> entry = it2.next();
if (entry.getKey().startsWith(modelName + "-")) {
if (belongsToModel(entry.getKey(), modelName)) {
it2.remove();
}
}
contexts.remove(modelName);
}

private boolean belongsToModel(String id, String modelName) {
int idx = id.lastIndexOf("-");
if (idx >= 0) {
String prefix = id.substring(0, idx);
return prefix.equals(modelName);
}
return false;
}

private void removeRule(Rule rule) {
for (ProviderChangeListener<Rule> providerChangeListener : listeners) {
providerChangeListener.removed(this, rule);
Expand Down

0 comments on commit 66766d5

Please sign in to comment.