Skip to content

Commit

Permalink
avniproject/avni-webapp#1345 | Make conditions optional for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Oct 9, 2024
1 parent 7110974 commit fce1569
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/rules/declarative/DeclarativeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ class DeclarativeRule {
}

isEmpty() {
return _.chain(this)
.get('conditions[0].compoundRule.rules[0].lhs.type')
.isEmpty()
.value();
return false;
}

addNewAction() {
Expand Down Expand Up @@ -107,13 +104,17 @@ class DeclarativeRule {
let actionConditions = '';
_.forEach(this.conditions, (condition, index) => {
const {compoundRule, conjunction} = condition;
const conditionName = `condition${index + 1}${conditionAppender}`;
const operator = (index + 1) < _.size(this.conditions) ? DeclarativeRule.conjunctionToBooleanOperator[conjunction] : '';
matchesConditions.push(`${conditionName} ${operator}`);
const rhsScopeCode = condition.getRHSScopeCode(entityName);
const ruleCondition = baseRuleCondition.replace('$RULE_CONDITION', compoundRule.getRuleCondition());
ruleConditions += `${rhsScopeCode}\n `;
ruleConditions += `const ${conditionName} = ${ruleCondition};\n `;
if (_.size(compoundRule.rules) === 1 && _.isNil(compoundRule.rules[0].lhs.type)) {
matchesConditions.push('true')
} else {
const conditionName = `condition${index + 1}${conditionAppender}`;
const operator = (index + 1) < _.size(this.conditions) ? DeclarativeRule.conjunctionToBooleanOperator[conjunction] : '';
matchesConditions.push(`${conditionName} ${operator}`);
const rhsScopeCode = condition.getRHSScopeCode(entityName);
const ruleCondition = baseRuleCondition.replace('$RULE_CONDITION', compoundRule.getRuleCondition());
ruleConditions += `${rhsScopeCode}\n `;
ruleConditions += `const ${conditionName} = ${ruleCondition};\n `;
}
});
_.forEach(this.actions, (action) => {
const actionTypes = Action.actionTypes;
Expand Down
1 change: 0 additions & 1 deletion src/rules/declarative/LHS.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class LHS {
}

validate() {
assertTrue(!_.isNil(this.type), "Type cannot be empty");
if (_.isEqual(this.type, LHS.types.Concept)) {
assertTrue(!_.isNil(this.conceptName), "Concept cannot be empty");
assertTrue(!_.isNil(this.scope), "Scope cannot be empty");
Expand Down
4 changes: 2 additions & 2 deletions src/rules/declarative/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Rule {
}

getRuleSummary() {
return `${this.lhs.getRuleSummary()} ${_.lowerCase(this.operator)} ${this.rhs.getRuleSummary()}`
return _.isNil(this.lhs.type) ? 'Always' : `${this.lhs.getRuleSummary()} ${_.lowerCase(this.operator)} ${this.rhs.getRuleSummary()}`
}

clone() {
Expand All @@ -104,7 +104,7 @@ class Rule {

validate() {
this.lhs.validate();
assertTrue(!_.isNil(this.operator), "Operator cannot be empty");
if(!_.isNil(this.lhs.type)) assertTrue(!_.isNil(this.operator), "Operator cannot be empty");
this.isRhsRequired() && this.rhs.validate();
}
}
Expand Down

0 comments on commit fce1569

Please sign in to comment.