Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automation] Avoid disappearing rules by correctly checking the model and its type #2077

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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