From 865dd094c03a145dc0588534263d1a62fef809eb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 6 Jul 2018 15:10:45 -0400 Subject: 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 --- .../main/resources/__closedLoopControlName__.drl | 119 +++++++++++++++++---- 1 file changed, 101 insertions(+), 18 deletions(-) (limited to 'controlloop/templates/archetype-cl-amsterdam') 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" @@ -138,23 +148,6 @@ rule "${policyName}.SETUP" logger.info("{}: {} : YAML=[{}]", params.getClosedLoopControlName(), drools.getRule().getName(), params.getControlLoopYaml()); 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 @@ -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 -- cgit 1.2.3-korg