diff options
author | Jim Hahn <jrh3@att.com> | 2018-07-06 15:10:45 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-07-06 15:12:44 -0400 |
commit | 865dd094c03a145dc0588534263d1a62fef809eb (patch) | |
tree | 746812a6f882c7068a3f928cb0e61ab6740d919c | |
parent | bb8821635720574be3c4c7146657fc8082c7d54b (diff) |
Remove Params for deleted rules
If rules are deleted, then no rule is left to retract the
associated Params object. However, we can create more general
rules that will fire whenever rules are added, thus causing
clean-up of old Params at that time.
Implemented the above approach.
Change-Id: I207d493cc6924a4b34a8d0fa4915ed499ebc4a6e
Issue-ID: POLICY-872
Signed-off-by: Jim Hahn <jrh3@att.com>
-rw-r--r-- | controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl | 119 |
1 files changed, 101 insertions, 18 deletions
diff --git a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl index 290a22270..888d2b6e5 100644 --- a/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl +++ b/controlloop/templates/archetype-cl-amsterdam/src/main/resources/archetype-resources/src/main/resources/__closedLoopControlName__.drl @@ -96,6 +96,14 @@ declare Params controlLoopYaml : String end +/* + * Used to clean up Params that no longer have associated rules. + */ +declare ParamsCleaner + closedLoopControlName : String + controlLoopYaml : String +end + /* * Operation Timer @@ -118,6 +126,8 @@ end /* * * Called once and only once to insert the parameters into working memory for this Closed Loop policy. +* This has a higher salience so we can ensure that the Params is created before we have a chance to +* discard any events. * */ rule "${policyName}.SETUP" @@ -140,23 +150,6 @@ end /* * -* This rule removes an old Params object that has the wrong YAML. -* -*/ -rule "${policyName}.PARAMS.CEANUP" - salience 1 - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() != "${controlLoopYaml}" ) - then - - Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); - logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml()); - - retract($params); -end - -/* -* * This rule responds to DCAE Events where there is no manager yet. Either it is * the first ONSET, or a subsequent badly formed Event (i.e. Syntax error, or is-closed-loop-disabled) * @@ -322,7 +315,7 @@ rule "${policyName}.EVENT.MANAGER" // cleanup and avoid the other rules being fired for this event. // if (eventStatus != NEW_EVENT_STATUS.FIRST_ONSET) { - logger.warn("{}: {}: no first onset", + logger.warn("{}: {}: not first onset", $params.getClosedLoopControlName(), drools.getRule().getName()); retract($event); } @@ -1287,3 +1280,93 @@ rule "${policyName}.EVENT.CLEANUP" retract($event); end + +/* +* +* When rules are deleted, the associated Params (and its subordinate objects) +* remain in working memory, because there are no longer any rules to clean +* them up. However, ANY time new rules are loaded, this rule will trigger +* a clean-up of ALL Params, regardless of their name & yaml, thus removing +* any that no longer have associated rules. +* This has a higher salience so that we immediately check Params when the +* rules change, before processing any events. +* +*/ +rule "${policyName}.PARAMS.CHECKUP" + salience 2 + when + Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {} : YAML=[{}]", $clName, drools.getRule().getName(), $yaml); + + ParamsCleaner cleaner = new ParamsCleaner(); + cleaner.setClosedLoopControlName($clName); + cleaner.setControlLoopYaml($yaml); + + insert(cleaner); +end + +/* +* +* This rule removes "cleaner" objects for rules that are still active, thus +* preventing the associated Params objects from being removed. Any cleaners +* that are left after this rule has fired will cause their corresponding Params +* to be removed. +* This has a higher salience so that we discard the cleaner before it has +* a chance to force the removal of the associated Params. +* +*/ +rule "${policyName}.CLEANER.ACTIVE" + salience 2 + when + $cleaner: ParamsCleaner( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() == "${controlLoopYaml}" ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml()); + + retract($cleaner); +end + +/* +* +* This rule removes Params objects that no longer have associated rules; if a +* Params still had associated rules, then the cleaner would have been removed +* by those rules and thus this rule would not fire. +* This has a higher salience so that we remove old Params before it causes any +* events to be processed. +* +*/ +rule "${policyName}.PARAMS.CLEANUP" + salience 1 + when + $params: Params( $clName: closedLoopControlName, $yaml: controlLoopYaml ) + ParamsCleaner( getClosedLoopControlName() == $clName, getControlLoopYaml() == $yaml ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {} : YAML=[{}]", $params.getClosedLoopControlName(), drools.getRule().getName(), $params.getControlLoopYaml()); + + retract($params); + + // Note: the cleaner may be needed for cleaning additional params, thus + // we do not retract it here - we'll leave that to another rule +end + +/* +* +* This rule removes "cleaner" objects when they're no longer needed. +* +*/ +rule "${policyName}.CLEANER.CLEANUP" + when + $cleaner: ParamsCleaner( ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {} : YAML=[{}]", $cleaner.getClosedLoopControlName(), drools.getRule().getName(), $cleaner.getControlLoopYaml()); + + retract($cleaner); +end |