From bb8821635720574be3c4c7146657fc8082c7d54b Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 5 Jul 2018 14:24:36 -0400 Subject: Clean up objects for deleted rules When rules for a closed loop are deleted, associated working memory elements may be left around. Modified the code to retract the various elements. Re-added code to unlock when retracting TargetLock. Add junit tests for new methods. Forgot the junit test for request id. Change-Id: Iec2805adbdb147a643f6163badee05ea353ada88 Issue-ID: POLICY-872 Signed-off-by: Jim Hahn --- .../main/resources/__closedLoopControlName__.drl | 107 +++++++++++++-------- 1 file changed, 66 insertions(+), 41 deletions(-) (limited to 'controlloop/templates') 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 1274d6c16..290a22270 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 @@ -121,7 +121,9 @@ end * */ rule "${policyName}.SETUP" + salience 1 when + not( Params( getClosedLoopControlName() == "${closedLoopControlName}", getControlLoopYaml() == "${controlLoopYaml}" ) ) then Params params = new Params(); @@ -136,6 +138,23 @@ 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 @@ -1173,26 +1192,14 @@ rule "${policyName}.EVENT.MANAGER.TIMEOUT" $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName() ) $manager : ControlLoopEventManager( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId() ) $clTimer : ControlLoopTimer ( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString(), $to : getDelay() ) - $operations : LinkedList() - from collect( ControlLoopOperationManager( onset.closedLoopControlName == $event.getClosedLoopControlName(), onset.getRequestId() == $event.getRequestId() ) ) - $opTimers : LinkedList() - from collect( OperationTimer( closedLoopControlName == $event.getClosedLoopControlName(), requestID == $event.getRequestId().toString() ) ) - $locks : LinkedList() - from collect( TargetLock (requestID == $event.getRequestId()) ) then Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); logger.info("{}: {}", $params.getClosedLoopControlName(), drools.getRule().getName()); - if ($operations == null) { - logger.debug("{}: {}: event={} manager={} clTimer={} operations=0", - $params.getClosedLoopControlName(), drools.getRule().getName(), - $event, $manager, $clTimer); - } else { - logger.debug("{}: {}: event={} manager={} clTimer={} operations={}", - $params.getClosedLoopControlName(), drools.getRule().getName(), - $event, $manager, $clTimer, $operations.size()); - } + logger.debug("{}: {}: event={}", + $params.getClosedLoopControlName(), drools.getRule().getName(), + $event); // // Tell the Event Manager it has timed out // @@ -1208,38 +1215,56 @@ rule "${policyName}.EVENT.MANAGER.TIMEOUT" PolicyEngine.manager.deliver("POLICY-CL-MGT", notification); } // - // Retract EVERYTHING + // Retract the event // retract($event); +end + +/* +* +* This rule cleans up the manager and other objects after an event has +* been retracted. +* +*/ +rule "${policyName}.EVENT.MANAGER.CLEANUP" + when + $manager : ControlLoopEventManager( $clName : getClosedLoopControlName(), $requestId : getRequestID() ) + $clTimer : ControlLoopTimer ( closedLoopControlName == $clName, requestID == $requestId.toString() ) + $operations : LinkedList() + from collect( ControlLoopOperationManager( onset.closedLoopControlName == $clName, onset.getRequestId() == $requestId ) ) + $opTimers : LinkedList() + from collect( OperationTimer( closedLoopControlName == $clName, requestID == $requestId.toString() ) ) + $locks : LinkedList() + from collect( TargetLock (requestID == $requestId) ) + not( VirtualControlLoopEvent( closedLoopControlName == $clName, requestId == $requestId ) ) + then + + Logger logger = LoggerFactory.getLogger(drools.getRule().getPackage()); + logger.info("{}: {}", $clName, drools.getRule().getName()); + + logger.debug("{}: {}: manager={} clTimer={} operations={}", + $clName, drools.getRule().getName(), + $manager, $clTimer, $operations.size()); + + // + // Retract EVERYTHING + // retract($manager); retract($clTimer); - if ($operations != null && $operations.size() > 0) { - Iterator iter = $operations.iterator(); - while (iter.hasNext()) { - ControlLoopOperationManager manager = iter.next(); - retract(manager); - } + + for(Object manager: $operations) { + retract((ControlLoopOperationManager) manager); } - if ($opTimers != null && $opTimers.size() > 0) { - Iterator iter = $opTimers.iterator(); - while (iter.hasNext()) { - OperationTimer opTimer = iter.next(); - retract(opTimer); - } + for(Object opTimer: $opTimers) { + retract((OperationTimer) opTimer); } - if ($locks != null && $locks.size() > 0) { - Iterator iter = $locks.iterator(); - while (iter.hasNext()) { - TargetLock lock = iter.next(); - // - // Ensure we release the lock - // - PolicyGuard.unlockTarget(lock); - // - // - // - retract(lock); - } + for(Object lock: $locks) { + TargetLock tgt = (TargetLock) lock; + // + // Ensure we release the lock + // + PolicyGuard.unlockTarget(tgt); + retract(tgt); } end -- cgit 1.2.3-korg