60c4d96d929f9cfacceb705c977120ba03cff8eb
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (c) 2021 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.adaptive.concepts;
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.List;
27 import lombok.EqualsAndHashCode;
28 import lombok.ToString;
29
30 /**
31  * The Class AutoLearn is used as a Java context for Adaptive auto-learning of trends towards a fixed value in the
32  * adaptive domain.
33  */
34 @EqualsAndHashCode
35 @ToString
36 public class AutoLearn implements Serializable {
37     private static final long serialVersionUID = 3825970380434170754L;
38
39     private transient List<Double> avDiffs = null;
40
41     private transient List<Long> counts = null;
42
43     /**
44      * Checks if the Autolearn instance is initialized.
45      *
46      * @return true, if the Autolearn instance is initialized
47      */
48     public boolean isInitialized() {
49         return (avDiffs != null && counts != null);
50     }
51
52     /**
53      * initializes the auto learning algorithm with the number of convergent variables to use.
54      *
55      * @param size the number of convergent variables to use
56      */
57     public void init(final int size) {
58         if (avDiffs == null || avDiffs.isEmpty()) {
59             avDiffs = new ArrayList<>(size);
60             for (var i = 0; i < size; i++) {
61                 avDiffs.add(i, Double.NaN);
62             }
63         }
64
65         if (counts == null || counts.isEmpty()) {
66             counts = new ArrayList<>(size);
67             for (var i = 0; i < size; i++) {
68                 counts.add(i, 0L);
69             }
70         }
71     }
72
73     /**
74      * Gets the average difference values of the algorithm.
75      *
76      * @return the average difference values of the algorithm
77      */
78     public List<Double> getAvDiffs() {
79         return avDiffs;
80     }
81
82     /**
83      * Sets the average difference values of the algorithm.
84      *
85      * @param avDiffs the average difference values of the algorithm
86      */
87     public void setAvDiffs(final List<Double> avDiffs) {
88         this.avDiffs = avDiffs;
89     }
90
91     /**
92      * Check if the average difference values of the algorithm are set.
93      *
94      * @return true, if check set av diffs
95      */
96     public boolean checkSetAvDiffs() {
97         return ((avDiffs != null) && (!avDiffs.isEmpty()));
98     }
99
100     /**
101      * Unset the average difference values of the algorithm.
102      */
103     public void unsetAvDiffs() {
104         avDiffs = null;
105     }
106
107     /**
108      * Gets the count values of the algorithm.
109      *
110      * @return the count values of the algorithm
111      */
112     public List<Long> getCounts() {
113         return counts;
114     }
115
116     /**
117      * Sets the count values of the algorithm.
118      *
119      * @param counts the count values of the algorithm
120      */
121     public void setCounts(final List<Long> counts) {
122         this.counts = counts;
123     }
124
125     /**
126      * Check if the count values of the algorithm are set.
127      *
128      * @return true, if the count values of the algorithm are set
129      */
130     public boolean checkSetCounts() {
131         return ((counts != null) && (!counts.isEmpty()));
132     }
133
134     /**
135      * Unset the count values of the algorithm.
136      */
137     public void unsetCounts() {
138         counts = null;
139     }
140
141
142 }