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 AutoLearnPolicyDecideTaskSelectionLogic.
34 public class AutoLearnPolicyDecideTaskSelectionLogic {
35 // Recurring string constants
36 private static final String AUTO_LEARN_ALBUM = "AutoLearnAlbum";
37 private static final String AUTO_LEARN = "AutoLearn";
39 private static final Random RAND = new Random(System.currentTimeMillis());
40 private static final double WANT = 50.0;
46 * @param executor the executor
49 public boolean getTask(final TaskSelectionExecutionContext executor) {
50 String idString = executor.subject.getId();
51 executor.logger.debug(idString);
53 String inFieldsString = executor.inFields.toString();
54 executor.logger.debug(inFieldsString);
56 final List<String> tasks = executor.subject.getTaskNames();
60 executor.getContextAlbum(AUTO_LEARN_ALBUM).lockForWriting(AUTO_LEARN);
61 } catch (final ContextException e) {
62 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
66 // Get the context object
67 AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum(AUTO_LEARN_ALBUM).get(AUTO_LEARN);
68 if (autoLearn == null) {
69 autoLearn = new AutoLearn();
72 // Check the lists are initialized
73 if (!autoLearn.isInitialized()) {
77 final double now = (Double) (executor.inFields.get("MonitoredValue"));
78 final double diff = now - WANT;
79 final int option = getOption(diff, autoLearn);
80 learn(option, diff, autoLearn);
82 executor.getContextAlbum(AUTO_LEARN_ALBUM).put(AUTO_LEARN_ALBUM, autoLearn);
85 executor.getContextAlbum(AUTO_LEARN_ALBUM).unlockForWriting(AUTO_LEARN);
86 } catch (final ContextException e) {
87 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
91 executor.subject.getTaskKey(tasks.get(option)).copyTo(executor.selectedTask);
98 * @param diff the diff
99 * @param autoLearn the auto learn
102 private int getOption(final double diff, final AutoLearn autoLearn) {
103 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
104 final int r = RAND.nextInt(size);
106 int closestdowni = -1;
107 double closestup = Double.MAX_VALUE;
108 double closestdown = Double.MIN_VALUE;
109 for (int i = 0; i < size; i++) {
110 if (Double.isNaN(avdiffs[i])) {
113 if (avdiffs[i] >= diff && avdiffs[i] <= closestup) {
114 closestup = avdiffs[i];
117 if (avdiffs[i] <= diff && avdiffs[i] >= closestdown) {
118 closestdown = avdiffs[i];
122 if (closestupi == -1 || closestdowni == -1) {
125 if (closestupi == closestdowni) {
128 if (Math.abs(closestdown - diff) > Math.abs(closestup - diff)) {
138 * @param option the option
139 * @param diff the diff
140 * @param autoLearn the auto learn
142 private void learn(final int option, final double diff, final AutoLearn autoLearn) {
143 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
144 final Long[] counts = autoLearn.getCounts().toArray(new Long[autoLearn.getCounts().size()]);
145 if (option < 0 || option >= avdiffs.length) {
146 throw new IllegalArgumentException("Error: option" + option);
149 if (Double.isNaN(avdiffs[option])) {
150 avdiffs[option] = diff;
152 avdiffs[option] = (avdiffs[option] * (counts[option] - 1) + diff) / counts[option];
154 autoLearn.setAvDiffs(Arrays.asList(avdiffs));
155 autoLearn.setCounts(Arrays.asList(counts));