3af9defc7423041403bd048a2497bebd6c1ae0f8
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
23 import org.drools.core.WorkingMemory;
24 import org.kie.api.runtime.rule.FactHandle;
25 import org.onap.policy.controlloop.ControlLoopException;
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
28
29 /**
30  * Manager for a single control loop event. Once this has been created, the event can be
31  * retracted from working memory. Once this has been created, {@link #start()} should be
32  * invoked, and then {@link #nextStep()} should be invoked continually until
33  * {@link #isActive()} returns {@code false}, indicating that all steps have completed.
34  */
35 public class ControlLoopEventManager2Drools extends ControlLoopEventManager2 {
36     private final transient WorkingMemory workMem;
37     private transient FactHandle factHandle;
38
39     /**
40      * Constructs the object.
41      *
42      * @param params control loop parameters
43      * @param event event to be managed by this object
44      * @param workMem working memory to update if this changes
45      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
46      *         be created
47      */
48     public ControlLoopEventManager2Drools(ControlLoopParams params, VirtualControlLoopEvent event,
49         WorkingMemory workMem) throws ControlLoopException {
50
51         super(params, event);
52         this.workMem = workMem;
53     }
54
55     /**
56      * This is a hook added to 'ControlLoopEventManager2.start()' --
57      * here, we add an additional check.
58      */
59     @Override
60     protected void startHook() {
61         if ((factHandle = workMem.getFactHandle(this)) == null) {
62             throw new IllegalStateException("manager is not in working memory");
63         }
64     }
65
66     /**
67      * This is a hook added to 'ControlLoopEventManager2.updated(...)' --
68      * here, we mark it as updated in Drools memory.
69      */
70     @Override
71     protected void notifyUpdate() {
72         workMem.update(factHandle, this);
73     }
74 }