2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.adaptive.model.java;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Random;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext;
29 import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn;
32 * The Class AutoLearnPolicy_Decide_TaskSelectionLogic.
34 // CHECKSTYLE:OFF: checkstyle:typeName
35 public class AutoLearnPolicy_Decide_TaskSelectionLogic {
36 // CHECKSTYLE:ON: checkstyle:typeName
37 private static final Random RAND = new Random(System.currentTimeMillis());
38 private static final double WANT = 50.0;
44 * @param executor the executor
47 public boolean getTask(final TaskSelectionExecutionContext executor) {
48 executor.logger.debug(executor.subject.getId());
49 executor.logger.debug(executor.inFields.toString());
50 final List<String> tasks = executor.subject.getTaskNames();
54 executor.getContextAlbum("AutoLearnAlbum").lockForWriting("AutoLearn");
55 } catch (final ContextException e) {
56 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
60 // Get the context object
61 AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum("AutoLearnAlbum").get("AutoLearn");
62 if (autoLearn == null) {
63 autoLearn = new AutoLearn();
66 // Check the lists are initialized
67 if (!autoLearn.isInitialized()) {
71 final double now = (Double) (executor.inFields.get("MonitoredValue"));
72 final double diff = now - WANT;
73 final int option = getOption(diff, autoLearn);
74 learn(option, diff, autoLearn);
76 executor.getContextAlbum("AutoLearnAlbum").put("AutoLearnAlbum", autoLearn);
79 executor.getContextAlbum("AutoLearnAlbum").unlockForWriting("AutoLearn");
80 } catch (final ContextException e) {
81 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
85 executor.subject.getTaskKey(tasks.get(option)).copyTo(executor.selectedTask);
92 * @param diff the diff
93 * @param autoLearn the auto learn
96 private int getOption(final double diff, final AutoLearn autoLearn) {
97 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
98 final int r = RAND.nextInt(size);
100 int closestdowni = -1;
101 double closestup = Double.MAX_VALUE;
102 double closestdown = Double.MIN_VALUE;
103 for (int i = 0; i < size; i++) {
104 if (Double.isNaN(avdiffs[i])) {
107 if (avdiffs[i] >= diff && avdiffs[i] <= closestup) {
108 closestup = avdiffs[i];
111 if (avdiffs[i] <= diff && avdiffs[i] >= closestdown) {
112 closestdown = avdiffs[i];
116 if (closestupi == -1 || closestdowni == -1) {
119 if (closestupi == closestdowni) {
122 if (Math.abs(closestdown - diff) > Math.abs(closestup - diff)) {
132 * @param option the option
133 * @param diff the diff
134 * @param autoLearn the auto learn
136 private void learn(final int option, final double diff, final AutoLearn autoLearn) {
137 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
138 final Long[] counts = autoLearn.getCounts().toArray(new Long[autoLearn.getCounts().size()]);
139 if (option < 0 || option >= avdiffs.length) {
140 throw new IllegalArgumentException("Error: option" + option);
143 if (Double.isNaN(avdiffs[option])) {
144 avdiffs[option] = diff;
146 avdiffs[option] = (avdiffs[option] * (counts[option] - 1) + diff) / counts[option];
148 autoLearn.setAvDiffs(Arrays.asList(avdiffs));
149 autoLearn.setCounts(Arrays.asList(counts));