9e8af9afb30481c3d1d979885495fc0a3e8950d5
[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 static final long serialVersionUID = 1L;
37
38     private final transient WorkingMemory workMem;
39     private transient FactHandle factHandle;
40
41     /**
42      * Constructs the object.
43      *
44      * @param params control loop parameters
45      * @param event event to be managed by this object
46      * @param workMem working memory to update if this changes
47      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
48      *         be created
49      */
50     public ControlLoopEventManager2Drools(ControlLoopParams params, VirtualControlLoopEvent event,
51         WorkingMemory workMem) throws ControlLoopException {
52
53         super(params, event);
54         this.workMem = workMem;
55     }
56
57     /**
58      * This is a hook added to 'ControlLoopEventManager2.start()' --
59      * here, we add an additional check.
60      */
61     @Override
62     protected void startHook() {
63         if ((factHandle = workMem.getFactHandle(this)) == null) {
64             throw new IllegalStateException("manager is not in working memory");
65         }
66     }
67
68     /**
69      * This is a hook added to 'ControlLoopEventManager2.updated(...)' --
70      * here, we mark it as updated in Drools memory.
71      */
72     @Override
73     protected void notifyUpdate() {
74         workMem.update(factHandle, this);
75     }
76 }