From cd68fc9bae7d6258f77ff59c1431e4f925f61a4c Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 13 Sep 2018 23:48:50 +0100 Subject: Address sonar/Checkstyle Issues Sweep through Apex codebase to fix most ceheckstyle and straightforward sonar issues. Issue-ID: POLICY-1034 Change-Id: I149d9a94ad893affc93573e8de5e3304b6bdde2d Signed-off-by: liamfallon --- .../adaptive/concepts/AnomalyDetection.java | 2 +- .../apex/examples/adaptive/concepts/AutoLearn.java | 9 +- .../adaptive/model/AdaptiveDomainModelFactory.java | 526 +++++++++++---------- .../adaptive/model/AdaptiveDomainModelSaver.java | 15 +- ...alyDetectionPolicyDecideTaskSelectionLogic.java | 410 ++++++++++++++++ ...yDetectionPolicy_Decide_TaskSelectionLogic.java | 406 ---------------- .../AutoLearnPolicyDecideTaskSelectionLogic.java | 157 ++++++ .../AutoLearnPolicy_Decide_TaskSelectionLogic.java | 151 ------ .../model/mvel/AutoLearnDecideTask0TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask0_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask1TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask1_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask2TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask2_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask3TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask3_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask4TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask4_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask5TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask5_TaskLogic.mvel | 40 -- .../model/mvel/AutoLearnDecideTask6TaskLogic.mvel | 40 ++ .../model/mvel/AutoLearnDecideTask6_TaskLogic.mvel | 40 -- 22 files changed, 1144 insertions(+), 1092 deletions(-) create mode 100644 examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java delete mode 100644 examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicy_Decide_TaskSelectionLogic.java create mode 100644 examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java delete mode 100644 examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicy_Decide_TaskSelectionLogic.java create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask0TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask0_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask1TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask1_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask2TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask2_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask3TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask3_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask4TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask4_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask5TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask5_TaskLogic.mvel create mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask6TaskLogic.mvel delete mode 100644 examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask6_TaskLogic.mvel (limited to 'examples/examples-adaptive/src') diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java index f574c74ec..c70c9feb6 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AnomalyDetection.java @@ -120,7 +120,7 @@ public class AnomalyDetection implements Serializable { * * @param anomalyScores the anomaly score values of the algorithm */ - public void setAnomalyScores(final LinkedList anomalyScores) { + public void setAnomalyScores(final List anomalyScores) { this.anomalyScores = anomalyScores; } diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java index f2e27725b..d935af5f0 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/concepts/AutoLearn.java @@ -35,11 +35,6 @@ public class AutoLearn implements Serializable { private List counts = null; - /** - * The Constructor creates an AutoLearn concept. - */ - public AutoLearn() {} - /** * Checks if the Autolearn instance is initialized. * @@ -55,14 +50,14 @@ public class AutoLearn implements Serializable { * @param size the number of convergent variables to use */ public void init(final int size) { - if (avDiffs == null || avDiffs.size() == 0) { + if (avDiffs == null || avDiffs.isEmpty()) { avDiffs = new ArrayList<>(size); for (int i = 0; i < size; i++) { avDiffs.add(i, Double.NaN); } } - if (counts == null || counts.size() == 0) { + if (counts == null || counts.isEmpty()) { counts = new ArrayList<>(size); for (int i = 0; i < size; i++) { counts.add(i, 0L); diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelFactory.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelFactory.java index 6b71468d9..d13f93099 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelFactory.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelFactory.java @@ -50,6 +50,19 @@ import org.onap.policy.apex.model.policymodel.handling.PolicyLogicReader; * The Class AdaptiveDomainModelFactory. */ public class AdaptiveDomainModelFactory { + // Recurring string constants + private static final String LAST_MONITORED_VALUE = "LastMonitoredValue"; + private static final String TASK_SELECTION_LOGIC = "TaskSelectionLogic"; + private static final String DEFAULT_STATE_LOGIC = "DefaultState_Logic"; + private static final String TASK_LOGIC = "TaskLogic"; + private static final String DECIDE = "Decide"; + private static final String ESTABLISH = "Establish"; + private static final String MATCH = "Match"; + private static final String EXTERNAL = "External"; + private static final String DEFAULT_NAMESPACE = "org.onap.policy.apex.examples.adaptive.events"; + private static final String ITERATION2 = "Iteration"; + private static final String DEFAULT_VERSION = "0.0.1"; + private static final String MONITORED_VALUE = "MonitoredValue"; /** * Gets the anomaly detection policy model. @@ -60,130 +73,142 @@ public class AdaptiveDomainModelFactory { public AxPolicyModel getAnomalyDetectionPolicyModel() { // CHECKSTYLE:ON: checkstyle:maximumMethodLength // Data types for event parameters - final AxContextSchema monitoredValue = - new AxContextSchema(new AxArtifactKey("MonitoredValue", "0.0.1"), "Java", "java.lang.Double"); - final AxContextSchema iteration = - new AxContextSchema(new AxArtifactKey("Iteration", "0.0.1"), "Java", "java.lang.Integer"); + final AxContextSchema monitoredValue = new AxContextSchema(new AxArtifactKey(MONITORED_VALUE, DEFAULT_VERSION), + "Java", "java.lang.Double"); + final AxContextSchema iteration = new AxContextSchema(new AxArtifactKey(ITERATION2, DEFAULT_VERSION), "Java", + "java.lang.Integer"); - final AxContextSchemas adContextSchemas = new AxContextSchemas(new AxArtifactKey("AADMDatatypes", "0.0.1")); + final AxContextSchemas adContextSchemas = new AxContextSchemas( + new AxArtifactKey("AADMDatatypes", DEFAULT_VERSION)); adContextSchemas.getSchemasMap().put(monitoredValue.getKey(), monitoredValue); adContextSchemas.getSchemasMap().put(iteration.getKey(), iteration); - final AxEvent anomalyDetectionTriggerEvent = - new AxEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - anomalyDetectionTriggerEvent.setSource("External"); - anomalyDetectionTriggerEvent.setTarget("Match"); - anomalyDetectionTriggerEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - anomalyDetectionTriggerEvent.getParameterMap().put("Iteration", new AxField( - new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), "Iteration"), iteration.getKey())); - - final AxEvent anomalyDetectionMatchEvent = new AxEvent(new AxArtifactKey("AnomalyDetectionMatchEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - anomalyDetectionMatchEvent.setSource("Match"); - anomalyDetectionMatchEvent.setTarget("Establish"); - anomalyDetectionMatchEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - anomalyDetectionMatchEvent.getParameterMap().put("Iteration", - new AxField(new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), "Iteration"), iteration.getKey())); - - final AxEvent anomalyDetectionEstablishEvent = - new AxEvent(new AxArtifactKey("AnomalyDetectionEstablishEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - anomalyDetectionEstablishEvent.setSource("Establish"); - anomalyDetectionEstablishEvent.setTarget("Decide"); - anomalyDetectionEstablishEvent.getParameterMap().put("MonitoredValue", - new AxField(new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), "MonitoredValue"), - monitoredValue.getKey())); - anomalyDetectionEstablishEvent.getParameterMap().put("Iteration", new AxField( - new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), "Iteration"), iteration.getKey())); - - final AxEvent anomalyDetectionDecideEvent = - new AxEvent(new AxArtifactKey("AnomalyDetectionDecideEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - anomalyDetectionDecideEvent.setSource("Decide"); + final AxEvent anomalyDetectionTriggerEvent = new AxEvent( + new AxArtifactKey("AnomalyDetectionTriggerEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + anomalyDetectionTriggerEvent.setSource(EXTERNAL); + anomalyDetectionTriggerEvent.setTarget(MATCH); + anomalyDetectionTriggerEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + anomalyDetectionTriggerEvent.getParameterMap().put(ITERATION2, new AxField( + new AxReferenceKey(anomalyDetectionTriggerEvent.getKey(), ITERATION2), iteration.getKey())); + + final AxEvent anomalyDetectionMatchEvent = new AxEvent( + new AxArtifactKey("AnomalyDetectionMatchEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + anomalyDetectionMatchEvent.setSource(MATCH); + anomalyDetectionMatchEvent.setTarget(ESTABLISH); + anomalyDetectionMatchEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + anomalyDetectionMatchEvent.getParameterMap().put(ITERATION2, new AxField( + new AxReferenceKey(anomalyDetectionMatchEvent.getKey(), ITERATION2), iteration.getKey())); + + final AxEvent anomalyDetectionEstablishEvent = new AxEvent( + new AxArtifactKey("AnomalyDetectionEstablishEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + anomalyDetectionEstablishEvent.setSource(ESTABLISH); + anomalyDetectionEstablishEvent.setTarget(DECIDE); + anomalyDetectionEstablishEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + anomalyDetectionEstablishEvent.getParameterMap().put(ITERATION2, new AxField( + new AxReferenceKey(anomalyDetectionEstablishEvent.getKey(), ITERATION2), iteration.getKey())); + + final AxEvent anomalyDetectionDecideEvent = new AxEvent( + new AxArtifactKey("AnomalyDetectionDecideEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + anomalyDetectionDecideEvent.setSource(DECIDE); anomalyDetectionDecideEvent.setTarget("Act"); - anomalyDetectionDecideEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - anomalyDetectionDecideEvent.getParameterMap().put("Iteration", - new AxField(new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), "Iteration"), iteration.getKey())); - - final AxEvent anomalyDetectionActEvent = new AxEvent(new AxArtifactKey("AnomalyDetectionActEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); + anomalyDetectionDecideEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + anomalyDetectionDecideEvent.getParameterMap().put(ITERATION2, new AxField( + new AxReferenceKey(anomalyDetectionDecideEvent.getKey(), ITERATION2), iteration.getKey())); + + final AxEvent anomalyDetectionActEvent = new AxEvent( + new AxArtifactKey("AnomalyDetectionActEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); anomalyDetectionActEvent.setSource("Act"); - anomalyDetectionActEvent.setTarget("External"); - anomalyDetectionActEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(anomalyDetectionActEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - anomalyDetectionActEvent.getParameterMap().put("Iteration", - new AxField(new AxReferenceKey(anomalyDetectionActEvent.getKey(), "Iteration"), iteration.getKey())); - - final AxEvents anomalyDetectionEvents = new AxEvents(new AxArtifactKey("AnomalyDetectionEvents", "0.0.1")); + anomalyDetectionActEvent.setTarget(EXTERNAL); + anomalyDetectionActEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(anomalyDetectionActEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + anomalyDetectionActEvent.getParameterMap().put(ITERATION2, new AxField( + new AxReferenceKey(anomalyDetectionActEvent.getKey(), ITERATION2), iteration.getKey())); + + final AxEvents anomalyDetectionEvents = new AxEvents( + new AxArtifactKey("AnomalyDetectionEvents", DEFAULT_VERSION)); anomalyDetectionEvents.getEventMap().put(anomalyDetectionTriggerEvent.getKey(), anomalyDetectionTriggerEvent); anomalyDetectionEvents.getEventMap().put(anomalyDetectionMatchEvent.getKey(), anomalyDetectionMatchEvent); anomalyDetectionEvents.getEventMap().put(anomalyDetectionEstablishEvent.getKey(), - anomalyDetectionEstablishEvent); + anomalyDetectionEstablishEvent); anomalyDetectionEvents.getEventMap().put(anomalyDetectionDecideEvent.getKey(), anomalyDetectionDecideEvent); anomalyDetectionEvents.getEventMap().put(anomalyDetectionActEvent.getKey(), anomalyDetectionActEvent); // Data types for context - final AxContextSchema anomalyDetection = new AxContextSchema(new AxArtifactKey("AnomalyDetection", "0.0.1"), - "Java", "org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection"); + final AxContextSchema anomalyDetection = new AxContextSchema( + new AxArtifactKey("AnomalyDetection", DEFAULT_VERSION), "Java", + "org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection"); adContextSchemas.getSchemasMap().put(anomalyDetection.getKey(), anomalyDetection); // One context map final AxContextAlbum anomalyDetectionAlbum = new AxContextAlbum( - new AxArtifactKey("AnomalyDetectionAlbum", "0.0.1"), "APPLICATION", true, anomalyDetection.getKey()); - final AxContextAlbums anomalyDetectionAlbums = - new AxContextAlbums(new AxArtifactKey("AnomalyDetectionAlbums", "0.0.1")); + new AxArtifactKey("AnomalyDetectionAlbum", DEFAULT_VERSION), "APPLICATION", true, + anomalyDetection.getKey()); + final AxContextAlbums anomalyDetectionAlbums = new AxContextAlbums( + new AxArtifactKey("AnomalyDetectionAlbums", DEFAULT_VERSION)); anomalyDetectionAlbums.getAlbumsMap().put(anomalyDetectionAlbum.getKey(), anomalyDetectionAlbum); // Tasks - final AxLogicReader logicReader = - new PolicyLogicReader().setLogicPackage(this.getClass().getPackage().getName()) + final AxLogicReader logicReader = new PolicyLogicReader() + .setLogicPackage(this.getClass().getPackage().getName()) .setDefaultLogic("DefaultAnomalyDetectionTask_Logic"); - final AxTask anomalyDetectionMatchTask = new AxTask(new AxArtifactKey("AnomalyDetectionMatchTask", "0.0.1")); + final AxTask anomalyDetectionMatchTask = new AxTask( + new AxArtifactKey("AnomalyDetectionMatchTask", DEFAULT_VERSION)); anomalyDetectionMatchTask.duplicateInputFields(anomalyDetectionTriggerEvent.getParameterMap()); anomalyDetectionMatchTask.duplicateOutputFields(anomalyDetectionMatchEvent.getParameterMap()); - anomalyDetectionMatchTask - .setTaskLogic(new AxTaskLogic(anomalyDetectionMatchTask.getKey(), "TaskLogic", "MVEL", logicReader)); + anomalyDetectionMatchTask.setTaskLogic( + new AxTaskLogic(anomalyDetectionMatchTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask anomalyDetectionEstablishTask = - new AxTask(new AxArtifactKey("AnomalyDetectionEstablishTask", "0.0.1")); + final AxTask anomalyDetectionEstablishTask = new AxTask( + new AxArtifactKey("AnomalyDetectionEstablishTask", DEFAULT_VERSION)); anomalyDetectionEstablishTask.duplicateInputFields(anomalyDetectionMatchEvent.getParameterMap()); anomalyDetectionEstablishTask.duplicateOutputFields(anomalyDetectionEstablishEvent.getParameterMap()); anomalyDetectionEstablishTask.setTaskLogic( - new AxTaskLogic(anomalyDetectionEstablishTask.getKey(), "TaskLogic", "MVEL", logicReader)); + new AxTaskLogic(anomalyDetectionEstablishTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask anomalyDetectionDecideTask0 = - new AxTask(new AxArtifactKey("AnomalyDetectionDecideTask0", "0.0.1")); + final AxTask anomalyDetectionDecideTask0 = new AxTask( + new AxArtifactKey("AnomalyDetectionDecideTask0", DEFAULT_VERSION)); anomalyDetectionDecideTask0.duplicateInputFields(anomalyDetectionEstablishEvent.getParameterMap()); anomalyDetectionDecideTask0.duplicateOutputFields(anomalyDetectionDecideEvent.getParameterMap()); - anomalyDetectionDecideTask0 - .setTaskLogic(new AxTaskLogic(anomalyDetectionDecideTask0.getKey(), "TaskLogic", "MVEL", logicReader)); + anomalyDetectionDecideTask0.setTaskLogic( + new AxTaskLogic(anomalyDetectionDecideTask0.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask anomalyDetectionDecideTask1 = - new AxTask(new AxArtifactKey("AnomalyDetectionDecideTask1", "0.0.1")); + final AxTask anomalyDetectionDecideTask1 = new AxTask( + new AxArtifactKey("AnomalyDetectionDecideTask1", DEFAULT_VERSION)); anomalyDetectionDecideTask1.duplicateInputFields(anomalyDetectionEstablishEvent.getParameterMap()); anomalyDetectionDecideTask1.duplicateOutputFields(anomalyDetectionDecideEvent.getParameterMap()); - anomalyDetectionDecideTask1 - .setTaskLogic(new AxTaskLogic(anomalyDetectionDecideTask1.getKey(), "TaskLogic", "MVEL", logicReader)); + anomalyDetectionDecideTask1.setTaskLogic( + new AxTaskLogic(anomalyDetectionDecideTask1.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask anomalyDetectionDecideTask2 = - new AxTask(new AxArtifactKey("AnomalyDetectionDecideTask2", "0.0.1")); + final AxTask anomalyDetectionDecideTask2 = new AxTask( + new AxArtifactKey("AnomalyDetectionDecideTask2", DEFAULT_VERSION)); anomalyDetectionDecideTask2.duplicateInputFields(anomalyDetectionEstablishEvent.getParameterMap()); anomalyDetectionDecideTask2.duplicateOutputFields(anomalyDetectionDecideEvent.getParameterMap()); - anomalyDetectionDecideTask2 - .setTaskLogic(new AxTaskLogic(anomalyDetectionDecideTask2.getKey(), "TaskLogic", "MVEL", logicReader)); + anomalyDetectionDecideTask2.setTaskLogic( + new AxTaskLogic(anomalyDetectionDecideTask2.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask anomalyDetectionActTask = new AxTask(new AxArtifactKey("AnomalyDetectionActTask", "0.0.1")); + final AxTask anomalyDetectionActTask = new AxTask( + new AxArtifactKey("AnomalyDetectionActTask", DEFAULT_VERSION)); anomalyDetectionActTask.duplicateInputFields(anomalyDetectionDecideEvent.getParameterMap()); anomalyDetectionActTask.duplicateOutputFields(anomalyDetectionActEvent.getParameterMap()); - anomalyDetectionActTask - .setTaskLogic(new AxTaskLogic(anomalyDetectionActTask.getKey(), "TaskLogic", "MVEL", logicReader)); + anomalyDetectionActTask.setTaskLogic( + new AxTaskLogic(anomalyDetectionActTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTasks anomalyDetectionTasks = new AxTasks(new AxArtifactKey("AnomalyDetectionTasks", "0.0.1")); + final AxTasks anomalyDetectionTasks = new AxTasks(new AxArtifactKey("AnomalyDetectionTasks", DEFAULT_VERSION)); anomalyDetectionTasks.getTaskMap().put(anomalyDetectionMatchTask.getKey(), anomalyDetectionMatchTask); anomalyDetectionTasks.getTaskMap().put(anomalyDetectionEstablishTask.getKey(), anomalyDetectionEstablishTask); anomalyDetectionTasks.getTaskMap().put(anomalyDetectionDecideTask0.getKey(), anomalyDetectionDecideTask0); @@ -192,92 +217,98 @@ public class AdaptiveDomainModelFactory { anomalyDetectionTasks.getTaskMap().put(anomalyDetectionActTask.getKey(), anomalyDetectionActTask); // Policies - logicReader.setDefaultLogic("DefaultState_Logic"); + logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC); - final AxPolicy anomalyDetectionPolicy = new AxPolicy(new AxArtifactKey("AnomalyDetectionPolicy", "0.0.1")); + final AxPolicy anomalyDetectionPolicy = new AxPolicy( + new AxArtifactKey("AnomalyDetectionPolicy", DEFAULT_VERSION)); anomalyDetectionPolicy.setTemplate("MEDA"); - final AxState anomalyDetectionActState = - new AxState(new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Act")); + final AxState anomalyDetectionActState = new AxState( + new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Act")); anomalyDetectionActState.setTrigger(anomalyDetectionDecideEvent.getKey()); final AxStateOutput adAct2Out = new AxStateOutput(anomalyDetectionActState.getKey(), - AxReferenceKey.getNullKey(), anomalyDetectionActEvent.getKey()); + AxReferenceKey.getNullKey(), anomalyDetectionActEvent.getKey()); anomalyDetectionActState.getStateOutputs().put(adAct2Out.getKey().getLocalName(), adAct2Out); - anomalyDetectionActState.setTaskSelectionLogic( - new AxTaskSelectionLogic(anomalyDetectionActState.getKey(), "TaskSelectionLogic", "MVEL", logicReader)); + anomalyDetectionActState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionActState.getKey(), + TASK_SELECTION_LOGIC, "MVEL", logicReader)); anomalyDetectionActState.setDefaultTask(anomalyDetectionActTask.getKey()); anomalyDetectionActState.getTaskReferences().put(anomalyDetectionActTask.getKey(), - new AxStateTaskReference(anomalyDetectionActState.getKey(), anomalyDetectionActTask.getKey(), - AxStateTaskOutputType.DIRECT, adAct2Out.getKey())); + new AxStateTaskReference(anomalyDetectionActState.getKey(), anomalyDetectionActTask.getKey(), + AxStateTaskOutputType.DIRECT, adAct2Out.getKey())); logicReader.setDefaultLogic(null); - final AxState anomalyDetectionDecideState = - new AxState(new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Decide")); + final AxState anomalyDetectionDecideState = new AxState( + new AxReferenceKey(anomalyDetectionPolicy.getKey(), DECIDE)); anomalyDetectionDecideState.setTrigger(anomalyDetectionEstablishEvent.getKey()); final AxStateOutput adDec2Act = new AxStateOutput(anomalyDetectionDecideState.getKey(), - anomalyDetectionActState.getKey(), anomalyDetectionDecideEvent.getKey()); + anomalyDetectionActState.getKey(), anomalyDetectionDecideEvent.getKey()); anomalyDetectionDecideState.getStateOutputs().put(adDec2Act.getKey().getLocalName(), adDec2Act); anomalyDetectionDecideState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionDecideState.getKey(), - "TaskSelectionLogic", "JAVA", logicReader)); + TASK_SELECTION_LOGIC, "JAVA", logicReader)); anomalyDetectionDecideState.setDefaultTask(anomalyDetectionDecideTask0.getKey()); anomalyDetectionDecideState.getContextAlbumReferences().add(anomalyDetectionAlbum.getKey()); anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask0.getKey(), - new AxStateTaskReference(anomalyDetectionDecideState.getKey(), anomalyDetectionDecideTask0.getKey(), - AxStateTaskOutputType.DIRECT, adDec2Act.getKey())); + new AxStateTaskReference(anomalyDetectionDecideState.getKey(), + anomalyDetectionDecideTask0.getKey(), AxStateTaskOutputType.DIRECT, + adDec2Act.getKey())); anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask1.getKey(), - new AxStateTaskReference(anomalyDetectionDecideState.getKey(), anomalyDetectionDecideTask1.getKey(), - AxStateTaskOutputType.DIRECT, adDec2Act.getKey())); + new AxStateTaskReference(anomalyDetectionDecideState.getKey(), + anomalyDetectionDecideTask1.getKey(), AxStateTaskOutputType.DIRECT, + adDec2Act.getKey())); anomalyDetectionDecideState.getTaskReferences().put(anomalyDetectionDecideTask2.getKey(), - new AxStateTaskReference(anomalyDetectionDecideState.getKey(), anomalyDetectionDecideTask2.getKey(), - AxStateTaskOutputType.DIRECT, adDec2Act.getKey())); + new AxStateTaskReference(anomalyDetectionDecideState.getKey(), + anomalyDetectionDecideTask2.getKey(), AxStateTaskOutputType.DIRECT, + adDec2Act.getKey())); - logicReader.setDefaultLogic("DefaultState_Logic"); + logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC); - final AxState anomalyDetectionEstablishState = - new AxState(new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Establish")); + final AxState anomalyDetectionEstablishState = new AxState( + new AxReferenceKey(anomalyDetectionPolicy.getKey(), ESTABLISH)); anomalyDetectionEstablishState.setTrigger(anomalyDetectionMatchEvent.getKey()); final AxStateOutput adEst2Dec = new AxStateOutput(anomalyDetectionEstablishState.getKey(), - anomalyDetectionDecideState.getKey(), anomalyDetectionEstablishEvent.getKey()); + anomalyDetectionDecideState.getKey(), anomalyDetectionEstablishEvent.getKey()); anomalyDetectionEstablishState.getStateOutputs().put(adEst2Dec.getKey().getLocalName(), adEst2Dec); anomalyDetectionEstablishState.setTaskSelectionLogic(new AxTaskSelectionLogic( - anomalyDetectionEstablishState.getKey(), "TaskSelectionLogic", "MVEL", logicReader)); + anomalyDetectionEstablishState.getKey(), TASK_SELECTION_LOGIC, "MVEL", logicReader)); anomalyDetectionEstablishState.setDefaultTask(anomalyDetectionEstablishTask.getKey()); anomalyDetectionEstablishState.getTaskReferences().put(anomalyDetectionEstablishTask.getKey(), - new AxStateTaskReference(anomalyDetectionEstablishState.getKey(), - anomalyDetectionEstablishTask.getKey(), AxStateTaskOutputType.DIRECT, adEst2Dec.getKey())); + new AxStateTaskReference(anomalyDetectionEstablishState.getKey(), + anomalyDetectionEstablishTask.getKey(), AxStateTaskOutputType.DIRECT, + adEst2Dec.getKey())); - final AxState anomalyDetectionMatchState = - new AxState(new AxReferenceKey(anomalyDetectionPolicy.getKey(), "Match")); + final AxState anomalyDetectionMatchState = new AxState( + new AxReferenceKey(anomalyDetectionPolicy.getKey(), MATCH)); anomalyDetectionMatchState.setTrigger(anomalyDetectionTriggerEvent.getKey()); final AxStateOutput adMat2Est = new AxStateOutput(anomalyDetectionMatchState.getKey(), - anomalyDetectionEstablishState.getKey(), anomalyDetectionMatchEvent.getKey()); + anomalyDetectionEstablishState.getKey(), anomalyDetectionMatchEvent.getKey()); anomalyDetectionMatchState.getStateOutputs().put(adMat2Est.getKey().getLocalName(), adMat2Est); anomalyDetectionMatchState.setTaskSelectionLogic(new AxTaskSelectionLogic(anomalyDetectionMatchState.getKey(), - "TaskSelectionLogic", "MVEL", logicReader)); + TASK_SELECTION_LOGIC, "MVEL", logicReader)); anomalyDetectionMatchState.setDefaultTask(anomalyDetectionMatchTask.getKey()); anomalyDetectionMatchState.getTaskReferences().put(anomalyDetectionMatchTask.getKey(), - new AxStateTaskReference(anomalyDetectionMatchState.getKey(), anomalyDetectionMatchTask.getKey(), - AxStateTaskOutputType.DIRECT, adMat2Est.getKey())); + new AxStateTaskReference(anomalyDetectionMatchState.getKey(), + anomalyDetectionMatchTask.getKey(), AxStateTaskOutputType.DIRECT, + adMat2Est.getKey())); anomalyDetectionPolicy.setFirstState(anomalyDetectionMatchState.getKey().getLocalName()); anomalyDetectionPolicy.getStateMap().put(anomalyDetectionMatchState.getKey().getLocalName(), - anomalyDetectionMatchState); + anomalyDetectionMatchState); anomalyDetectionPolicy.getStateMap().put(anomalyDetectionEstablishState.getKey().getLocalName(), - anomalyDetectionEstablishState); + anomalyDetectionEstablishState); anomalyDetectionPolicy.getStateMap().put(anomalyDetectionDecideState.getKey().getLocalName(), - anomalyDetectionDecideState); + anomalyDetectionDecideState); anomalyDetectionPolicy.getStateMap().put(anomalyDetectionActState.getKey().getLocalName(), - anomalyDetectionActState); + anomalyDetectionActState); - final AxPolicies anomalyDetectionPolicies = - new AxPolicies(new AxArtifactKey("AnomalyDetectionPolicies", "0.0.1")); + final AxPolicies anomalyDetectionPolicies = new AxPolicies( + new AxArtifactKey("AnomalyDetectionPolicies", DEFAULT_VERSION)); anomalyDetectionPolicies.getPolicyMap().put(anomalyDetectionPolicy.getKey(), anomalyDetectionPolicy); - final AxKeyInformation keyInformation = - new AxKeyInformation(new AxArtifactKey("AnomalyDetectionKeyInformation", "0.0.1")); - final AxPolicyModel anomalyDetectionPolicyModel = - new AxPolicyModel(new AxArtifactKey("AnomalyDetectionPolicyModel", "0.0.1")); + final AxKeyInformation keyInformation = new AxKeyInformation( + new AxArtifactKey("AnomalyDetectionKeyInformation", DEFAULT_VERSION)); + final AxPolicyModel anomalyDetectionPolicyModel = new AxPolicyModel( + new AxArtifactKey("AnomalyDetectionPolicyModel", DEFAULT_VERSION)); anomalyDetectionPolicyModel.setPolicies(anomalyDetectionPolicies); anomalyDetectionPolicyModel.setEvents(anomalyDetectionEvents); anomalyDetectionPolicyModel.setTasks(anomalyDetectionTasks); @@ -302,58 +333,65 @@ public class AdaptiveDomainModelFactory { public AxPolicyModel getAutoLearnPolicyModel() { // CHECKSTYLE:ON: checkstyle:maximumMethodLength // Data types for event parameters - final AxContextSchema monitoredValue = - new AxContextSchema(new AxArtifactKey("MonitoredValue", "0.0.1"), "Java", "java.lang.Double"); + final AxContextSchema monitoredValue = new AxContextSchema(new AxArtifactKey(MONITORED_VALUE, DEFAULT_VERSION), + "Java", "java.lang.Double"); - final AxContextSchemas alContextSchemas = new AxContextSchemas(new AxArtifactKey("ALDatatypes", "0.0.1")); + final AxContextSchemas alContextSchemas = new AxContextSchemas( + new AxArtifactKey("ALDatatypes", DEFAULT_VERSION)); alContextSchemas.getSchemasMap().put(monitoredValue.getKey(), monitoredValue); - final AxEvent autoLearnTriggerEvent = new AxEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - autoLearnTriggerEvent.setSource("External"); - autoLearnTriggerEvent.setTarget("Match"); - autoLearnTriggerEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(autoLearnTriggerEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - autoLearnTriggerEvent.getParameterMap().put("LastMonitoredValue", new AxField( - new AxReferenceKey(autoLearnTriggerEvent.getKey(), "LastMonitoredValue"), monitoredValue.getKey())); - - final AxEvent autoLearnMatchEvent = new AxEvent(new AxArtifactKey("AutoLearnMatchEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - autoLearnMatchEvent.setSource("Match"); - autoLearnMatchEvent.setTarget("Establish"); - autoLearnMatchEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(autoLearnMatchEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - autoLearnMatchEvent.getParameterMap().put("LastMonitoredValue", new AxField( - new AxReferenceKey(autoLearnMatchEvent.getKey(), "LastMonitoredValue"), monitoredValue.getKey())); - - final AxEvent autoLearnEstablishEvent = new AxEvent(new AxArtifactKey("AutoLearnEstablishEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - autoLearnEstablishEvent.setSource("Establish"); - autoLearnEstablishEvent.setTarget("Decide"); - autoLearnEstablishEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(autoLearnEstablishEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - autoLearnEstablishEvent.getParameterMap().put("LastMonitoredValue", new AxField( - new AxReferenceKey(autoLearnEstablishEvent.getKey(), "LastMonitoredValue"), monitoredValue.getKey())); - - final AxEvent autoLearnDecideEvent = new AxEvent(new AxArtifactKey("AutoLearnDecideEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); - autoLearnDecideEvent.setSource("Decide"); + final AxEvent autoLearnTriggerEvent = new AxEvent(new AxArtifactKey("AutoLearnTriggerEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + autoLearnTriggerEvent.setSource(EXTERNAL); + autoLearnTriggerEvent.setTarget(MATCH); + autoLearnTriggerEvent.getParameterMap().put(MONITORED_VALUE, new AxField( + new AxReferenceKey(autoLearnTriggerEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey())); + autoLearnTriggerEvent.getParameterMap().put(LAST_MONITORED_VALUE, + new AxField(new AxReferenceKey(autoLearnTriggerEvent.getKey(), LAST_MONITORED_VALUE), + monitoredValue.getKey())); + + final AxEvent autoLearnMatchEvent = new AxEvent(new AxArtifactKey("AutoLearnMatchEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + autoLearnMatchEvent.setSource(MATCH); + autoLearnMatchEvent.setTarget(ESTABLISH); + autoLearnMatchEvent.getParameterMap().put(MONITORED_VALUE, new AxField( + new AxReferenceKey(autoLearnMatchEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey())); + autoLearnMatchEvent.getParameterMap().put(LAST_MONITORED_VALUE, + new AxField(new AxReferenceKey(autoLearnMatchEvent.getKey(), LAST_MONITORED_VALUE), + monitoredValue.getKey())); + + final AxEvent autoLearnEstablishEvent = new AxEvent( + new AxArtifactKey("AutoLearnEstablishEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + autoLearnEstablishEvent.setSource(ESTABLISH); + autoLearnEstablishEvent.setTarget(DECIDE); + autoLearnEstablishEvent.getParameterMap().put(MONITORED_VALUE, + new AxField(new AxReferenceKey(autoLearnEstablishEvent.getKey(), MONITORED_VALUE), + monitoredValue.getKey())); + autoLearnEstablishEvent.getParameterMap().put(LAST_MONITORED_VALUE, + new AxField(new AxReferenceKey(autoLearnEstablishEvent.getKey(), LAST_MONITORED_VALUE), + monitoredValue.getKey())); + + final AxEvent autoLearnDecideEvent = new AxEvent(new AxArtifactKey("AutoLearnDecideEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); + autoLearnDecideEvent.setSource(DECIDE); autoLearnDecideEvent.setTarget("Act"); - autoLearnDecideEvent.getParameterMap().put("MonitoredValue", new AxField( - new AxReferenceKey(autoLearnDecideEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - autoLearnDecideEvent.getParameterMap().put("LastMonitoredValue", new AxField( - new AxReferenceKey(autoLearnDecideEvent.getKey(), "LastMonitoredValue"), monitoredValue.getKey())); - - final AxEvent autoLearnActEvent = new AxEvent(new AxArtifactKey("AutoLearnActEvent", "0.0.1"), - "org.onap.policy.apex.examples.adaptive.events"); + autoLearnDecideEvent.getParameterMap().put(MONITORED_VALUE, new AxField( + new AxReferenceKey(autoLearnDecideEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey())); + autoLearnDecideEvent.getParameterMap().put(LAST_MONITORED_VALUE, + new AxField(new AxReferenceKey(autoLearnDecideEvent.getKey(), LAST_MONITORED_VALUE), + monitoredValue.getKey())); + + final AxEvent autoLearnActEvent = new AxEvent(new AxArtifactKey("AutoLearnActEvent", DEFAULT_VERSION), + DEFAULT_NAMESPACE); autoLearnActEvent.setSource("Act"); - autoLearnActEvent.setTarget("External"); - autoLearnActEvent.getParameterMap().put("MonitoredValue", - new AxField(new AxReferenceKey(autoLearnActEvent.getKey(), "MonitoredValue"), monitoredValue.getKey())); - autoLearnActEvent.getParameterMap().put("LastMonitoredValue", new AxField( - new AxReferenceKey(autoLearnActEvent.getKey(), "LastMonitoredValue"), monitoredValue.getKey())); + autoLearnActEvent.setTarget(EXTERNAL); + autoLearnActEvent.getParameterMap().put(MONITORED_VALUE, new AxField( + new AxReferenceKey(autoLearnActEvent.getKey(), MONITORED_VALUE), monitoredValue.getKey())); + autoLearnActEvent.getParameterMap().put(LAST_MONITORED_VALUE, new AxField( + new AxReferenceKey(autoLearnActEvent.getKey(), LAST_MONITORED_VALUE), monitoredValue.getKey())); - final AxEvents autoLearnEvents = new AxEvents(new AxArtifactKey("AutoLearnEvents", "0.0.1")); + final AxEvents autoLearnEvents = new AxEvents(new AxArtifactKey("AutoLearnEvents", DEFAULT_VERSION)); autoLearnEvents.getEventMap().put(autoLearnTriggerEvent.getKey(), autoLearnTriggerEvent); autoLearnEvents.getEventMap().put(autoLearnMatchEvent.getKey(), autoLearnMatchEvent); autoLearnEvents.getEventMap().put(autoLearnEstablishEvent.getKey(), autoLearnEstablishEvent); @@ -361,84 +399,86 @@ public class AdaptiveDomainModelFactory { autoLearnEvents.getEventMap().put(autoLearnActEvent.getKey(), autoLearnActEvent); // Data types for context - final AxContextSchema autoLearn = new AxContextSchema(new AxArtifactKey("AutoLearn", "0.0.1"), "Java", - "org.onap.policy.apex.examples.adaptive.concepts.AutoLearn"); + final AxContextSchema autoLearn = new AxContextSchema(new AxArtifactKey("AutoLearn", DEFAULT_VERSION), "Java", + "org.onap.policy.apex.examples.adaptive.concepts.AutoLearn"); alContextSchemas.getSchemasMap().put(autoLearn.getKey(), autoLearn); // One context map - final AxContextAlbum autoLearnAlbum = new AxContextAlbum(new AxArtifactKey("AutoLearnAlbum", "0.0.1"), - "APPLICATION", true, autoLearn.getKey()); + final AxContextAlbum autoLearnAlbum = new AxContextAlbum(new AxArtifactKey("AutoLearnAlbum", DEFAULT_VERSION), + "APPLICATION", true, autoLearn.getKey()); - final AxContextAlbums autoLearnAlbums = new AxContextAlbums(new AxArtifactKey("AutoLearnContext", "0.0.1")); + final AxContextAlbums autoLearnAlbums = new AxContextAlbums( + new AxArtifactKey("AutoLearnContext", DEFAULT_VERSION)); autoLearnAlbums.getAlbumsMap().put(autoLearnAlbum.getKey(), autoLearnAlbum); // Tasks final AxLogicReader logicReader = new PolicyLogicReader() - .setLogicPackage(this.getClass().getPackage().getName()).setDefaultLogic("DefaultAutoLearnTask_Logic"); + .setLogicPackage(this.getClass().getPackage().getName()) + .setDefaultLogic("DefaultAutoLearnTask_Logic"); - final AxTask autoLearnMatchTask = new AxTask(new AxArtifactKey("AutoLearnMatchTask", "0.0.1")); + final AxTask autoLearnMatchTask = new AxTask(new AxArtifactKey("AutoLearnMatchTask", DEFAULT_VERSION)); autoLearnMatchTask.duplicateInputFields(autoLearnTriggerEvent.getParameterMap()); autoLearnMatchTask.duplicateOutputFields(autoLearnMatchEvent.getParameterMap()); - autoLearnMatchTask.setTaskLogic(new AxTaskLogic(autoLearnMatchTask.getKey(), "TaskLogic", "MVEL", logicReader)); + autoLearnMatchTask.setTaskLogic(new AxTaskLogic(autoLearnMatchTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnEstablishTask = new AxTask(new AxArtifactKey("AutoLearnEstablishTask", "0.0.1")); + final AxTask autoLearnEstablishTask = new AxTask(new AxArtifactKey("AutoLearnEstablishTask", DEFAULT_VERSION)); autoLearnEstablishTask.duplicateInputFields(autoLearnMatchEvent.getParameterMap()); autoLearnEstablishTask.duplicateOutputFields(autoLearnEstablishEvent.getParameterMap()); - autoLearnEstablishTask - .setTaskLogic(new AxTaskLogic(autoLearnEstablishTask.getKey(), "TaskLogic", "MVEL", logicReader)); + autoLearnEstablishTask.setTaskLogic( + new AxTaskLogic(autoLearnEstablishTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); logicReader.setDefaultLogic(null); - final AxTask autoLearnDecideTask0 = new AxTask(new AxArtifactKey("AutoLearnDecideTask0", "0.0.1")); + final AxTask autoLearnDecideTask0 = new AxTask(new AxArtifactKey("AutoLearnDecideTask0", DEFAULT_VERSION)); autoLearnDecideTask0.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask0.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask0 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask0.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask0.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask1 = new AxTask(new AxArtifactKey("AutoLearnDecideTask1", "0.0.1")); + final AxTask autoLearnDecideTask1 = new AxTask(new AxArtifactKey("AutoLearnDecideTask1", DEFAULT_VERSION)); autoLearnDecideTask1.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask1.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask1 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask1.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask1.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask2 = new AxTask(new AxArtifactKey("AutoLearnDecideTask2", "0.0.1")); + final AxTask autoLearnDecideTask2 = new AxTask(new AxArtifactKey("AutoLearnDecideTask2", DEFAULT_VERSION)); autoLearnDecideTask2.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask2.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask2 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask2.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask2.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask3 = new AxTask(new AxArtifactKey("AutoLearnDecideTask3", "0.0.1")); + final AxTask autoLearnDecideTask3 = new AxTask(new AxArtifactKey("AutoLearnDecideTask3", DEFAULT_VERSION)); autoLearnDecideTask3.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask3.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask3 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask3.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask3.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask4 = new AxTask(new AxArtifactKey("AutoLearnDecideTask4", "0.0.1")); + final AxTask autoLearnDecideTask4 = new AxTask(new AxArtifactKey("AutoLearnDecideTask4", DEFAULT_VERSION)); autoLearnDecideTask4.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask4.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask4 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask4.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask4.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask5 = new AxTask(new AxArtifactKey("AutoLearnDecideTask5", "0.0.1")); + final AxTask autoLearnDecideTask5 = new AxTask(new AxArtifactKey("AutoLearnDecideTask5", DEFAULT_VERSION)); autoLearnDecideTask5.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask5.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask5 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask5.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask5.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTask autoLearnDecideTask6 = new AxTask(new AxArtifactKey("AutoLearnDecideTask6", "0.0.1")); + final AxTask autoLearnDecideTask6 = new AxTask(new AxArtifactKey("AutoLearnDecideTask6", DEFAULT_VERSION)); autoLearnDecideTask6.duplicateInputFields(autoLearnEstablishEvent.getParameterMap()); autoLearnDecideTask6.duplicateOutputFields(autoLearnDecideEvent.getParameterMap()); autoLearnDecideTask6 - .setTaskLogic(new AxTaskLogic(autoLearnDecideTask6.getKey(), "TaskLogic", "MVEL", logicReader)); + .setTaskLogic(new AxTaskLogic(autoLearnDecideTask6.getKey(), TASK_LOGIC, "MVEL", logicReader)); logicReader.setDefaultLogic("DefaultAutoLearnTask_Logic"); - final AxTask autoLearnActTask = new AxTask(new AxArtifactKey("AutoLearnActTask", "0.0.1")); + final AxTask autoLearnActTask = new AxTask(new AxArtifactKey("AutoLearnActTask", DEFAULT_VERSION)); autoLearnActTask.duplicateInputFields(autoLearnDecideEvent.getParameterMap()); autoLearnActTask.duplicateOutputFields(autoLearnActEvent.getParameterMap()); - autoLearnActTask.setTaskLogic(new AxTaskLogic(autoLearnActTask.getKey(), "TaskLogic", "MVEL", logicReader)); + autoLearnActTask.setTaskLogic(new AxTaskLogic(autoLearnActTask.getKey(), TASK_LOGIC, "MVEL", logicReader)); - final AxTasks autoLearnTasks = new AxTasks(new AxArtifactKey("AutoLearnTasks", "0.0.1")); + final AxTasks autoLearnTasks = new AxTasks(new AxArtifactKey("AutoLearnTasks", DEFAULT_VERSION)); autoLearnTasks.getTaskMap().put(autoLearnMatchTask.getKey(), autoLearnMatchTask); autoLearnTasks.getTaskMap().put(autoLearnEstablishTask.getKey(), autoLearnEstablishTask); autoLearnTasks.getTaskMap().put(autoLearnDecideTask0.getKey(), autoLearnDecideTask0); @@ -451,81 +491,81 @@ public class AdaptiveDomainModelFactory { autoLearnTasks.getTaskMap().put(autoLearnActTask.getKey(), autoLearnActTask); // Policies - logicReader.setDefaultLogic("DefaultState_Logic"); + logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC); - final AxPolicy autoLearnPolicy = new AxPolicy(new AxArtifactKey("AutoLearnPolicy", "0.0.1")); + final AxPolicy autoLearnPolicy = new AxPolicy(new AxArtifactKey("AutoLearnPolicy", DEFAULT_VERSION)); autoLearnPolicy.setTemplate("MEDA"); final AxState autoLearnActState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), "Act")); autoLearnActState.setTrigger(autoLearnDecideEvent.getKey()); - final AxStateOutput alAct2Out = - new AxStateOutput(autoLearnActState.getKey(), AxReferenceKey.getNullKey(), autoLearnActEvent.getKey()); + final AxStateOutput alAct2Out = new AxStateOutput(autoLearnActState.getKey(), AxReferenceKey.getNullKey(), + autoLearnActEvent.getKey()); autoLearnActState.getStateOutputs().put(alAct2Out.getKey().getLocalName(), alAct2Out); - autoLearnActState.setTaskSelectionLogic( - new AxTaskSelectionLogic(autoLearnActState.getKey(), "TaskSelectionLogic", "MVEL", logicReader)); + autoLearnActState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnActState.getKey(), + TASK_SELECTION_LOGIC, "MVEL", logicReader)); autoLearnActState.setDefaultTask(autoLearnActTask.getKey()); autoLearnActState.getTaskReferences().put(autoLearnActTask.getKey(), - new AxStateTaskReference(autoLearnActState.getKey(), autoLearnActTask.getKey(), - AxStateTaskOutputType.DIRECT, alAct2Out.getKey())); + new AxStateTaskReference(autoLearnActState.getKey(), autoLearnActTask.getKey(), + AxStateTaskOutputType.DIRECT, alAct2Out.getKey())); logicReader.setDefaultLogic(null); - final AxState autoLearnDecideState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), "Decide")); + final AxState autoLearnDecideState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), DECIDE)); autoLearnDecideState.setTrigger(autoLearnEstablishEvent.getKey()); final AxStateOutput alDec2Act = new AxStateOutput(autoLearnDecideState.getKey(), autoLearnActState.getKey(), - autoLearnDecideEvent.getKey()); + autoLearnDecideEvent.getKey()); autoLearnDecideState.getStateOutputs().put(alDec2Act.getKey().getLocalName(), alDec2Act); autoLearnDecideState.getContextAlbumReferences().add(autoLearnAlbum.getKey()); - autoLearnDecideState.setTaskSelectionLogic( - new AxTaskSelectionLogic(autoLearnDecideState.getKey(), "TaskSelectionLogic", "JAVA", logicReader)); + autoLearnDecideState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnDecideState.getKey(), + TASK_SELECTION_LOGIC, "JAVA", logicReader)); autoLearnDecideState.setDefaultTask(autoLearnDecideTask0.getKey()); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask0.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask0.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask0.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask1.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask1.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask1.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask2.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask2.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask2.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask3.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask3.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask3.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask4.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask4.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask4.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask5.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask5.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask5.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); autoLearnDecideState.getTaskReferences().put(autoLearnDecideTask6.getKey(), - new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask6.getKey(), - AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); + new AxStateTaskReference(autoLearnDecideState.getKey(), autoLearnDecideTask6.getKey(), + AxStateTaskOutputType.DIRECT, alDec2Act.getKey())); - logicReader.setDefaultLogic("DefaultState_Logic"); + logicReader.setDefaultLogic(DEFAULT_STATE_LOGIC); - final AxState autoLearnEstablishState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), "Establish")); + final AxState autoLearnEstablishState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), ESTABLISH)); autoLearnEstablishState.setTrigger(autoLearnMatchEvent.getKey()); final AxStateOutput alEst2Dec = new AxStateOutput(autoLearnEstablishState.getKey(), - autoLearnDecideState.getKey(), autoLearnEstablishEvent.getKey()); + autoLearnDecideState.getKey(), autoLearnEstablishEvent.getKey()); autoLearnEstablishState.getStateOutputs().put(alEst2Dec.getKey().getLocalName(), alEst2Dec); - autoLearnEstablishState.setTaskSelectionLogic( - new AxTaskSelectionLogic(autoLearnEstablishState.getKey(), "TaskSelectionLogic", "MVEL", logicReader)); + autoLearnEstablishState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnEstablishState.getKey(), + TASK_SELECTION_LOGIC, "MVEL", logicReader)); autoLearnEstablishState.setDefaultTask(autoLearnEstablishTask.getKey()); autoLearnEstablishState.getTaskReferences().put(autoLearnEstablishTask.getKey(), - new AxStateTaskReference(autoLearnEstablishState.getKey(), autoLearnEstablishTask.getKey(), - AxStateTaskOutputType.DIRECT, alEst2Dec.getKey())); + new AxStateTaskReference(autoLearnEstablishState.getKey(), autoLearnEstablishTask.getKey(), + AxStateTaskOutputType.DIRECT, alEst2Dec.getKey())); - final AxState autoLearnMatchState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), "Match")); + final AxState autoLearnMatchState = new AxState(new AxReferenceKey(autoLearnPolicy.getKey(), MATCH)); autoLearnMatchState.setTrigger(autoLearnTriggerEvent.getKey()); final AxStateOutput alMat2Est = new AxStateOutput(autoLearnMatchState.getKey(), - autoLearnEstablishState.getKey(), autoLearnMatchEvent.getKey()); + autoLearnEstablishState.getKey(), autoLearnMatchEvent.getKey()); autoLearnMatchState.getStateOutputs().put(alMat2Est.getKey().getLocalName(), alMat2Est); - autoLearnMatchState.setTaskSelectionLogic( - new AxTaskSelectionLogic(autoLearnMatchState.getKey(), "TaskSelectionLogic", "MVEL", logicReader)); + autoLearnMatchState.setTaskSelectionLogic(new AxTaskSelectionLogic(autoLearnMatchState.getKey(), + TASK_SELECTION_LOGIC, "MVEL", logicReader)); autoLearnMatchState.setDefaultTask(autoLearnMatchTask.getKey()); autoLearnMatchState.getTaskReferences().put(autoLearnMatchTask.getKey(), - new AxStateTaskReference(autoLearnMatchState.getKey(), autoLearnMatchTask.getKey(), - AxStateTaskOutputType.DIRECT, alMat2Est.getKey())); + new AxStateTaskReference(autoLearnMatchState.getKey(), autoLearnMatchTask.getKey(), + AxStateTaskOutputType.DIRECT, alMat2Est.getKey())); autoLearnPolicy.setFirstState(autoLearnMatchState.getKey().getLocalName()); autoLearnPolicy.getStateMap().put(autoLearnMatchState.getKey().getLocalName(), autoLearnMatchState); @@ -533,13 +573,13 @@ public class AdaptiveDomainModelFactory { autoLearnPolicy.getStateMap().put(autoLearnDecideState.getKey().getLocalName(), autoLearnDecideState); autoLearnPolicy.getStateMap().put(autoLearnActState.getKey().getLocalName(), autoLearnActState); - final AxPolicies autoLearnPolicies = new AxPolicies(new AxArtifactKey("AutoLearnPolicies", "0.0.1")); + final AxPolicies autoLearnPolicies = new AxPolicies(new AxArtifactKey("AutoLearnPolicies", DEFAULT_VERSION)); autoLearnPolicies.getPolicyMap().put(autoLearnPolicy.getKey(), autoLearnPolicy); - final AxKeyInformation keyInformation = - new AxKeyInformation(new AxArtifactKey("AutoLearnKeyInformation", "0.0.1")); - final AxPolicyModel autoLearnPolicyModel = - new AxPolicyModel(new AxArtifactKey("AutoLearnPolicyModel", "0.0.1")); + final AxKeyInformation keyInformation = new AxKeyInformation( + new AxArtifactKey("AutoLearnKeyInformation", DEFAULT_VERSION)); + final AxPolicyModel autoLearnPolicyModel = new AxPolicyModel( + new AxArtifactKey("AutoLearnPolicyModel", DEFAULT_VERSION)); autoLearnPolicyModel.setPolicies(autoLearnPolicies); autoLearnPolicyModel.setEvents(autoLearnEvents); autoLearnPolicyModel.setTasks(autoLearnTasks); diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelSaver.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelSaver.java index 9c860e3c8..4949edd30 100644 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelSaver.java +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/AdaptiveDomainModelSaver.java @@ -23,6 +23,8 @@ package org.onap.policy.apex.examples.adaptive.model; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.handling.ApexModelSaver; import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; /** * This class saves sample domain models to disk in XML and JSON format. @@ -30,10 +32,15 @@ import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; * @author Liam Fallon (liam.fallon@ericsson.com) */ public final class AdaptiveDomainModelSaver { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AdaptiveDomainModelSaver.class); + /** * Private default constructor to prevent subclassing. */ - private AdaptiveDomainModelSaver() {} + private AdaptiveDomainModelSaver() { + // Prevent subclassing + } /** * Write the AADM model to args[0]. @@ -43,21 +50,21 @@ public final class AdaptiveDomainModelSaver { */ public static void main(final String[] args) throws ApexException { if (args.length != 1) { - System.err.println("usage: " + AdaptiveDomainModelSaver.class.getCanonicalName() + " modelDirectory"); + LOGGER.error("usage: " + AdaptiveDomainModelSaver.class.getCanonicalName() + " modelDirectory"); return; } // Save Anomaly Detection model final AxPolicyModel adPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); final ApexModelSaver adModelSaver = - new ApexModelSaver<>(AxPolicyModel.class, adPolicyModel, args[0]); + new ApexModelSaver<>(AxPolicyModel.class, adPolicyModel, args[0]); adModelSaver.apexModelWriteJson(); adModelSaver.apexModelWriteXml(); // Save Auto Learn model final AxPolicyModel alPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); final ApexModelSaver alModelSaver = - new ApexModelSaver<>(AxPolicyModel.class, alPolicyModel, args[0]); + new ApexModelSaver<>(AxPolicyModel.class, alPolicyModel, args[0]); alModelSaver.apexModelWriteJson(); alModelSaver.apexModelWriteXml(); } diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java new file mode 100644 index 000000000..5ee114e0e --- /dev/null +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicyDecideTaskSelectionLogic.java @@ -0,0 +1,410 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.examples.adaptive.model.java; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.math3.distribution.TDistribution; +import org.apache.commons.math3.util.FastMath; +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; +import org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.slf4j.Logger; + +/** + * The Class AnomalyDetectionPolicyDecideTaskSelectionLogic. + */ +public class AnomalyDetectionPolicyDecideTaskSelectionLogic { + private Logger logger; + + // Recurring string constants + private static final String ANOMALY_DETECTION_ALBUM = "AnomalyDetectionAlbum"; + private static final String ANOMALY_DETECTION = "AnomalyDetection"; + + // configuration + private static final double ANOMALY_SENSITIVITY = 0.05; + private static final int FREQUENCY = 360; + + /** + * A map to hold the Anomaly degree/levels/probabilities required for each task.
+ * If there is no task defined for a calculated anomaly-degree, then the default task is + * used.
+ * The map use (LinkedHashMap) is an insertion-ordered map, so the first interval matching a + * query is used. + */ + // CHECKSTYLE:OFF: checkstyle:magicNumber + private static final Map TASK_INTERVALS = new LinkedHashMap<>(); + + static { + TASK_INTERVALS.put(new double[] {0.0, 0.1}, null); // null will mean default task + TASK_INTERVALS.put(new double[] {0.25, 0.5}, "AnomalyDetectionDecideTask1"); + TASK_INTERVALS.put(new double[] {0.5, 1.01}, "AnomalyDetectionDecideTask2"); + } + // CHECKSTYLE:ON: checkstyle:magicNumber + + private volatile TaskSelectionExecutionContext executionContext; + + /** + * Gets the task. + * + * @param executor the executor + * @return the task + */ + public boolean getTask(final TaskSelectionExecutionContext executor) { + executionContext = executor; + logger = executionContext.logger; + String id = executor.subject.getId(); + logger.debug(id); + String inFields = executor.inFields.toString(); + logger.debug(inFields); + final double now = (Double) (executor.inFields.get("MonitoredValue")); + final Integer iteration = (Integer) (executor.inFields.get("Iteration")); + // get the double[forecastedValue, AnomalyScore, AnomalyProbability] + final double[] vals = this.forecastingAndAnomaly(now); + final double anomalyness = vals[2]; + String task = null; + for (final Map.Entry i : TASK_INTERVALS.entrySet()) { + if (checkInterval(anomalyness, i.getKey())) { + task = i.getValue(); + break; + } + } + if (task == null) { + executionContext.subject.getDefaultTaskKey().copyTo(executionContext.selectedTask); + } else { + executionContext.subject.getTaskKey(task).copyTo(executionContext.selectedTask); + } + if (logger.isDebugEnabled()) { + logger.debug( + "TestAnomalyDetectionTSLPolicy0000DecideStateTaskSelectionLogic.getTask():\t************\t\t\t\t" + + "Iteration:\t" + iteration + "\tValue:\t" + now + "\tForecast:\t" + vals[0] + + "\tAnomalyScore:\t" + vals[1] + "\tAnomalyProbability:\t" + vals[2] + "\tInvoking Task:\t" + + executionContext.selectedTask); + } + return true; + } + + /** + * Anomaly detection and forecast. + * + * @param value The current value + * @return Null if the function can not be executed correctly, otherwise double[forecastedValue, + * AnomalyScore, AnomalyProbability] + */ + public double[] forecastingAndAnomaly(final double value) { + try { + executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).lockForWriting(ANOMALY_DETECTION); + } catch (final ApexException e) { + logger.error("Failed to acquire write lock on \"AnomalyDetection\" context", e); + return new double[0]; + } + + // Get the context object + AnomalyDetection anomalyDetection = + (AnomalyDetection) executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).get(ANOMALY_DETECTION); + if (anomalyDetection == null) { + anomalyDetection = new AnomalyDetection(); + executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).put(ANOMALY_DETECTION, anomalyDetection); + } + + // Check the lists are initialized + if (!anomalyDetection.isInitialized()) { + anomalyDetection.init(FREQUENCY); + } + + boolean unsetfirstround = false; + + int frequency = anomalyDetection.getFrequency(); + frequency = frequency + 1; + + // reset frequency counter + if (frequency >= FREQUENCY) { + unsetfirstround = true; + frequency = 0; + } + anomalyDetection.setFrequency(frequency); + + if (unsetfirstround && anomalyDetection.getFirstRound()) { + anomalyDetection.setFirstRound(false); + } + + // --------- calculate the forecasted value - simple version + final Double lastForecast = anomalyDetection.getFrequencyForecasted().get(frequency); + + // get forecast for current value + final double forecastedValue = lastForecast == null ? value : expMovingAverage(value, lastForecast); + + // --------- calculate the anomalyScore + final double anomalyScore = lastForecast == null ? 0.0 : FastMath.abs(lastForecast - value); + + anomalyDetection.getFrequencyForecasted().set(frequency, forecastedValue); + + // anomaly score is ignored in the first frequency period + if (!anomalyDetection.getFirstRound()) { + ((LinkedList) anomalyDetection.getAnomalyScores()).addLast(anomalyScore); + } + + // CHECKSTYLE:OFF: checkstyle:magicNumber + // max FREQUENCY*4 anomaly scores history + listSizeControl(anomalyDetection.getAnomalyScores(), FREQUENCY * 4); + + // ---------- calculate the anomaly probability + double anomalyProbability = 0.0; + if (anomalyDetection.getAnomalyScores().size() > 30) { + // 0.5 + anomalyProbability = getStatsTest(anomalyDetection.getAnomalyScores(), ANOMALY_SENSITIVITY); + } + // CHECKSTYLE:ON: checkstyle:magicNumber + + try { + executionContext.getContextAlbum(ANOMALY_DETECTION_ALBUM).unlockForWriting(ANOMALY_DETECTION); + } catch (final ApexException e) { + logger.error("Failed to release write lock on \"AnomalyDetection\" context", e); + return new double[0]; + } + + return new double[] {forecastedValue, anomalyScore, anomalyProbability}; + } + + /** + * Is the passed value inside the interval, i.e. (value < interval[1] && value>=interval[0]). + * + * @param value The value to check + * @param interval A 2 element double array describing an interval + * @return true if the value is between interval[0] (inclusive) and interval[1] (exclusive), + * i.e. (value < interval[1] && value>=interval[0]). Otherwise false; + */ + private static boolean checkInterval(final double value, final double[] interval) { + if (interval == null || interval.length != 2) { + throw new IllegalArgumentException("something other than an interval passed to checkInterval"); + } + final double min = interval[0]; + final double max = interval[1]; + return (value < max && value >= min); + } + + /** + * calculate the anomaly probability using statistical test. + * + * @param values the values + * @param significanceLevel the significance level + * @return the anomaly probability + */ + private static double getStatsTest(final List values, final double significanceLevel) { + if (isAllEqual(values)) { + return 0.0; + } + // the targeted value or the last value + final double currentV = values.get(values.size() - 1); + Double[] lvaluesCopy = values.toArray(new Double[values.size()]); + Arrays.sort(lvaluesCopy); // takes ~40% of method time + // get mean + double mean = getMean(lvaluesCopy); + // get the test value: val + double val = getV(lvaluesCopy, mean, true); + // get the p value for the test value + double pvalue = getPValue(lvaluesCopy, val, mean); // takes approx 25% of method time + + // check the critical level + while (pvalue < significanceLevel) { // takes approx 20% of method time + // the score value as the anomaly probability + final double score = (significanceLevel - pvalue) / significanceLevel; + if (Double.compare(val, currentV) == 0) { + return score; + } + // do the critical check again for the left values + lvaluesCopy = removevalue(lvaluesCopy, val); + if (isAllEqual(lvaluesCopy)) { + return 0.0; + } + + mean = getMean(lvaluesCopy); + val = getV(lvaluesCopy, mean, true); + pvalue = getPValue(lvaluesCopy, val, mean); + } + return 0.0; + } + + /** + * Get the test value based on mean from sorted values. + * + * @param lvalues the l values + * @param mean the mean + * @param maxValueOnly : only the max extreme value will be tested + * @return the value to be tested + */ + private static double getV(final Double[] lvalues, final double mean, final boolean maxValueOnly) { + double val = lvalues[lvalues.length - 1]; + // max value as the extreme value + if (maxValueOnly) { + return val; + } + // check the extreme side + if ((val - mean) < (mean - lvalues[0])) { + val = lvalues[0]; + } + return val; + } + + /** + * calculate the P value for the t distribution. + * + * @param lvalues the l values + * @param val the value + * @param mean the mean + * @return the p value + */ + private static double getPValue(final Double[] lvalues, final double val, final double mean) { + // calculate z value + final double z = FastMath.abs(val - mean) / getStdDev(lvalues, mean); + // calculate T + final double n = lvalues.length; + final double s = (z * z * n * (2.0 - n)) / (z * z * n - (n - 1.0) * (n - 1.0)); + final double t = FastMath.sqrt(s); + // default p value = 0 + double pvalue = 0.0; + if (!Double.isNaN(t)) { + // t distribution with n-2 degrees of freedom + final TDistribution tDist = new TDistribution(n - 2); + pvalue = n * (1.0 - tDist.cumulativeProbability(t)); + // set max pvalue = 1 + pvalue = pvalue > 1.0 ? 1.0 : pvalue; + } + return pvalue; + } + + /* + * Some utility methods + */ + // exponential = 2(n+1) + private static final double EMA_EXPONENT = 2.0 / (7.0 + 1.0); + private static final double EMA_EXPONENT_1 = (1.0 - EMA_EXPONENT); + + /** + * exponential moving average. + * + * @param value the value + * @param lastForecast the last forecast + * @return the double + */ + private static double expMovingAverage(final double value, final double lastForecast) { + return (value * EMA_EXPONENT) + (lastForecast * EMA_EXPONENT_1); + } + + /** + * Remove the first occurrence of the value val from the array. + * + * @param lvalues the l values + * @param val the value + * @return the double[] + */ + private static Double[] removevalue(final Double[] lvalues, final double val) { + for (int i = 0; i < lvalues.length; i++) { + if (Double.compare(lvalues[i], val) == 0) { + final Double[] ret = new Double[lvalues.length - 1]; + System.arraycopy(lvalues, 0, ret, 0, i); + System.arraycopy(lvalues, i + 1, ret, i, lvalues.length - i - 1); + return ret; + } + } + return lvalues; + } + + /** + * get mean value of double list. + * + * @param lvalues the l values + * @return the mean + */ + private static double getMean(final Double[] lvalues) { + double sum = 0.0; + for (final double d : lvalues) { + + sum += d; + } + return sum / lvalues.length; + } + + /** + * get standard deviation of double list. + * + * @param lvalues the l values + * @param mean the mean + * @return stddev + */ + private static double getStdDev(final Double[] lvalues, final double mean) { + double temp = 0.0; + for (final double d : lvalues) { + temp += (mean - d) * (mean - d); + } + return FastMath.sqrt(temp / lvalues.length); + } + + /** + * Chop head off list to make it length max . + * + * @param list the list to chop + * @param max the max size + */ + private static void listSizeControl(final List list, final int max) { + final int k = list.size(); + if (k > max) { + // Chop the head off the list. + list.subList(0, k - max).clear(); + } + } + + /** + * return true if all values are equal. + * + * @param lvalues the l values + * @return true, if checks if is all equal + */ + private static boolean isAllEqual(final List lvalues) { + final double first = lvalues.get(0); + for (final Double d : lvalues) { + if (Double.compare(d, first) != 0) { + return false; + } + } + return true; + } + + /** + * return true if all values are equal. + * + * @param lvalues the l values + * @return true, if checks if is all equal + */ + private static boolean isAllEqual(final Double[] lvalues) { + final double first = lvalues[0]; + for (final Double d : lvalues) { + if (Double.compare(d, first) != 0) { + return false; + } + } + return true; + } +} diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicy_Decide_TaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicy_Decide_TaskSelectionLogic.java deleted file mode 100644 index 2a654c38e..000000000 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AnomalyDetectionPolicy_Decide_TaskSelectionLogic.java +++ /dev/null @@ -1,406 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.examples.adaptive.model.java; - -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import org.apache.commons.math3.distribution.TDistribution; -import org.apache.commons.math3.util.FastMath; -import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; -import org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.slf4j.Logger; - -/** - * The Class AnomalyDetectionPolicy_Decide_TaskSelectionLogic. - */ -// CHECKSTYLE:OFF: checkstyle:className -public class AnomalyDetectionPolicy_Decide_TaskSelectionLogic { - // CHECKSTYLE:ON: checkstyle:className - - private Logger logger; - // configuration - private static final double ANOMALY_SENSITIVITY = 0.05; - private static final int FREQUENCY = 360; - - /** - * A map to hold the Anomaly degree/levels/probabilities required for each task.
- * If there is no task defined for a calculated anomaly-degree, then the default task is - * used.
- * The map use (LinkedHashMap) is an insertion-ordered map, so the first interval matching a - * query is used. - */ - // CHECKSTYLE:OFF: checkstyle:magicNumber - private static final Map TASK_INTERVALS = new LinkedHashMap<>(); - - static { - TASK_INTERVALS.put(new double[] {0.0, 0.1}, null); // null will mean default task - TASK_INTERVALS.put(new double[] {0.25, 0.5}, "AnomalyDetectionDecideTask1"); - TASK_INTERVALS.put(new double[] {0.5, 1.01}, "AnomalyDetectionDecideTask2"); - } - // CHECKSTYLE:ON: checkstyle:magicNumber - - private volatile TaskSelectionExecutionContext executionContext; - - /** - * Gets the task. - * - * @param executor the executor - * @return the task - */ - public boolean getTask(final TaskSelectionExecutionContext executor) { - executionContext = executor; - logger = executionContext.logger; - logger.debug(executor.subject.getId()); - logger.debug(executor.inFields.toString()); - final double now = (Double) (executor.inFields.get("MonitoredValue")); - final Integer iteration = (Integer) (executor.inFields.get("Iteration")); - // get the double[forecastedValue, AnomalyScore, AnomalyProbability] - final double[] vals = this.forecastingAndAnomaly(now); - final double anomalyness = vals[2]; - String task = null; - for (final Map.Entry i : TASK_INTERVALS.entrySet()) { - if (checkInterval(anomalyness, i.getKey())) { - task = i.getValue(); - break; - } - } - if (task == null) { - executionContext.subject.getDefaultTaskKey().copyTo(executionContext.selectedTask); - } else { - executionContext.subject.getTaskKey(task).copyTo(executionContext.selectedTask); - } - if (logger.isDebugEnabled()) { - logger.debug( - "TestAnomalyDetectionTSLPolicy0000DecideStateTaskSelectionLogic.getTask():\t************\t\t\t\t" - + "Iteration:\t" + iteration + "\tValue:\t" + now + "\tForecast:\t" + vals[0] - + "\tAnomalyScore:\t" + vals[1] + "\tAnomalyProbability:\t" + vals[2] + "\tInvoking Task:\t" - + executionContext.selectedTask); - } - return true; - } - - /** - * Anomaly detection and forecast. - * - * @param value The current value - * @return Null if the function can not be executed correctly, otherwise double[forecastedValue, - * AnomalyScore, AnomalyProbability] - */ - public double[] forecastingAndAnomaly(final double value) { - try { - executionContext.getContextAlbum("AnomalyDetectionAlbum").lockForWriting("AnomalyDetection"); - } catch (final ApexException e) { - logger.error("Failed to acquire write lock on \"AnomalyDetection\" context", e); - return null; - } - - // Get the context object - AnomalyDetection anomalyDetection = - (AnomalyDetection) executionContext.getContextAlbum("AnomalyDetectionAlbum").get("AnomalyDetection"); - if (anomalyDetection == null) { - anomalyDetection = new AnomalyDetection(); - executionContext.getContextAlbum("AnomalyDetectionAlbum").put("AnomalyDetection", anomalyDetection); - } - - // Check the lists are initialized - if (!anomalyDetection.isInitialized()) { - anomalyDetection.init(FREQUENCY); - } - - boolean unsetfirstround = false; - - int frequency = anomalyDetection.getFrequency(); - frequency = frequency + 1; - - // reset frequency counter - if (frequency >= FREQUENCY) { - unsetfirstround = true; - frequency = 0; - } - anomalyDetection.setFrequency(frequency); - - if (unsetfirstround && anomalyDetection.getFirstRound()) { - anomalyDetection.setFirstRound(false); - } - - // --------- calculate the forecasted value - simple version - final Double lastForecast = anomalyDetection.getFrequencyForecasted().get(frequency); - - // get forecast for current value - final double forecastedValue = lastForecast == null ? value : expMovingAverage(value, lastForecast); - - // --------- calculate the anomalyScore - final double anomalyScore = lastForecast == null ? 0.0 : FastMath.abs(lastForecast - value); - - anomalyDetection.getFrequencyForecasted().set(frequency, forecastedValue); - - // anomaly score is ignored in the first frequency period - if (!anomalyDetection.getFirstRound()) { - ((LinkedList) anomalyDetection.getAnomalyScores()).addLast(anomalyScore); - } - - // CHECKSTYLE:OFF: checkstyle:magicNumber - // max FREQUENCY*4 anomaly scores history - listSizeControl(anomalyDetection.getAnomalyScores(), FREQUENCY * 4); - - // ---------- calculate the anomaly probability - double anomalyProbability = 0.0; - if (anomalyDetection.getAnomalyScores().size() > 30) { - // 0.5 - anomalyProbability = getStatsTest(anomalyDetection.getAnomalyScores(), ANOMALY_SENSITIVITY); - } - // CHECKSTYLE:ON: checkstyle:magicNumber - - try { - executionContext.getContextAlbum("AnomalyDetectionAlbum").unlockForWriting("AnomalyDetection"); - } catch (final ApexException e) { - logger.error("Failed to release write lock on \"AnomalyDetection\" context", e); - return null; - } - - return new double[] {forecastedValue, anomalyScore, anomalyProbability}; - } - - /** - * Is the passed value inside the interval, i.e. (value < interval[1] && value>=interval[0]). - * - * @param value The value to check - * @param interval A 2 element double array describing an interval - * @return true if the value is between interval[0] (inclusive) and interval[1] (exclusive), - * i.e. (value < interval[1] && value>=interval[0]). Otherwise false; - */ - private static boolean checkInterval(final double value, final double[] interval) { - if (interval == null || interval.length != 2) { - throw new IllegalArgumentException("something other than an interval passed to checkInterval"); - } - final double min = interval[0]; - final double max = interval[1]; - return (value < max && value >= min); - } - - /** - * calculate the anomaly probability using statistical test. - * - * @param values the values - * @param significanceLevel the significance level - * @return the anomaly probability - */ - private static double getStatsTest(final List values, final double significanceLevel) { - if (isAllEqual(values)) { - return 0.0; - } - // the targeted value or the last value - final double currentV = values.get(values.size() - 1); - Double[] lvaluesCopy = values.toArray(new Double[values.size()]); - Arrays.sort(lvaluesCopy); // takes ~40% of method time - // get mean - double mean = getMean(lvaluesCopy); - // get the test value: val - double val = getV(lvaluesCopy, mean, true); - // get the p value for the test value - double pvalue = getPValue(lvaluesCopy, val, mean); // takes approx 25% of method time - - // check the critical level - while (pvalue < significanceLevel) { // takes approx 20% of method time - // the score value as the anomaly probability - final double score = (significanceLevel - pvalue) / significanceLevel; - if (Double.compare(val, currentV) == 0) { - return score; - } - // do the critical check again for the left values - lvaluesCopy = removevalue(lvaluesCopy, val); - if (isAllEqual(lvaluesCopy)) { - return 0.0; - } - - mean = getMean(lvaluesCopy); - val = getV(lvaluesCopy, mean, true); - pvalue = getPValue(lvaluesCopy, val, mean); - } - return 0.0; - } - - /** - * Get the test value based on mean from sorted values. - * - * @param lvalues the l values - * @param mean the mean - * @param maxValueOnly : only the max extreme value will be tested - * @return the value to be tested - */ - private static double getV(final Double[] lvalues, final double mean, final boolean maxValueOnly) { - double val = lvalues[lvalues.length - 1]; - // max value as the extreme value - if (maxValueOnly) { - return val; - } - // check the extreme side - if ((val - mean) < (mean - lvalues[0])) { - val = lvalues[0]; - } - return val; - } - - /** - * calculate the P value for the t distribution. - * - * @param lvalues the l values - * @param val the value - * @param mean the mean - * @return the p value - */ - private static double getPValue(final Double[] lvalues, final double val, final double mean) { - // calculate z value - final double z = FastMath.abs(val - mean) / getStdDev(lvalues, mean); - // calculate T - final double n = lvalues.length; - final double s = (z * z * n * (2.0 - n)) / (z * z * n - (n - 1.0) * (n - 1.0)); - final double t = FastMath.sqrt(s); - // default p value = 0 - double pvalue = 0.0; - if (!Double.isNaN(t)) { - // t distribution with n-2 degrees of freedom - final TDistribution tDist = new TDistribution(n - 2); - pvalue = n * (1.0 - tDist.cumulativeProbability(t)); - // set max pvalue = 1 - pvalue = pvalue > 1.0 ? 1.0 : pvalue; - } - return pvalue; - } - - /* - * Some utility methods - */ - // exponential = 2(n+1) - private static final double EMA_EXPONENT = 2.0 / (7.0 + 1.0); - private static final double EMA_EXPONENT_1 = (1.0 - EMA_EXPONENT); - - /** - * exponential moving average. - * - * @param value the value - * @param lastForecast the last forecast - * @return the double - */ - private static double expMovingAverage(final double value, final double lastForecast) { - return (value * EMA_EXPONENT) + (lastForecast * EMA_EXPONENT_1); - } - - /** - * Remove the first occurrence of the value val from the array. - * - * @param lvalues the l values - * @param val the value - * @return the double[] - */ - private static Double[] removevalue(final Double[] lvalues, final double val) { - for (int i = 0; i < lvalues.length; i++) { - if (Double.compare(lvalues[i], val) == 0) { - final Double[] ret = new Double[lvalues.length - 1]; - System.arraycopy(lvalues, 0, ret, 0, i); - System.arraycopy(lvalues, i + 1, ret, i, lvalues.length - i - 1); - return ret; - } - } - return lvalues; - } - - /** - * get mean value of double list. - * - * @param lvalues the l values - * @return the mean - */ - private static double getMean(final Double[] lvalues) { - double sum = 0.0; - for (final double d : lvalues) { - - sum += d; - } - return sum / lvalues.length; - } - - /** - * get standard deviation of double list. - * - * @param lvalues the l values - * @param mean the mean - * @return stddev - */ - private static double getStdDev(final Double[] lvalues, final double mean) { - double temp = 0.0; - for (final double d : lvalues) { - temp += (mean - d) * (mean - d); - } - return FastMath.sqrt(temp / lvalues.length); - } - - /** - * Chop head off list to make it length max . - * - * @param list the list to chop - * @param max the max size - */ - private static void listSizeControl(final List list, final int max) { - final int k = list.size(); - if (k > max) { - // Chop the head off the list. - list.subList(0, k - max).clear(); - } - } - - /** - * return true if all values are equal. - * - * @param lvalues the l values - * @return true, if checks if is all equal - */ - private static boolean isAllEqual(final List lvalues) { - final double first = lvalues.get(0); - for (final Double d : lvalues) { - if (Double.compare(d, first) != 0) { - return false; - } - } - return true; - } - - /** - * return true if all values are equal. - * - * @param lvalues the l values - * @return true, if checks if is all equal - */ - private static boolean isAllEqual(final Double[] lvalues) { - final double first = lvalues[0]; - for (final Double d : lvalues) { - if (Double.compare(d, first) != 0) { - return false; - } - } - return true; - } -} diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java new file mode 100644 index 000000000..d60b04c00 --- /dev/null +++ b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicyDecideTaskSelectionLogic.java @@ -0,0 +1,157 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.examples.adaptive.model.java; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; +import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn; + +/** + * The Class AutoLearnPolicyDecideTaskSelectionLogic. + */ +public class AutoLearnPolicyDecideTaskSelectionLogic { + // Recurring string constants + private static final String AUTO_LEARN_ALBUM = "AutoLearnAlbum"; + private static final String AUTO_LEARN = "AutoLearn"; + + private static final Random RAND = new Random(System.currentTimeMillis()); + private static final double WANT = 50.0; + private int size; + + /** + * Gets the task. + * + * @param executor the executor + * @return the task + */ + public boolean getTask(final TaskSelectionExecutionContext executor) { + String idString = executor.subject.getId(); + executor.logger.debug(idString); + + String inFieldsString = executor.inFields.toString(); + executor.logger.debug(inFieldsString); + + final List tasks = executor.subject.getTaskNames(); + size = tasks.size(); + + try { + executor.getContextAlbum(AUTO_LEARN_ALBUM).lockForWriting(AUTO_LEARN); + } catch (final ContextException e) { + executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e); + return false; + } + + // Get the context object + AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum(AUTO_LEARN_ALBUM).get(AUTO_LEARN); + if (autoLearn == null) { + autoLearn = new AutoLearn(); + } + + // Check the lists are initialized + if (!autoLearn.isInitialized()) { + autoLearn.init(size); + } + + final double now = (Double) (executor.inFields.get("MonitoredValue")); + final double diff = now - WANT; + final int option = getOption(diff, autoLearn); + learn(option, diff, autoLearn); + + executor.getContextAlbum(AUTO_LEARN_ALBUM).put(AUTO_LEARN_ALBUM, autoLearn); + + try { + executor.getContextAlbum(AUTO_LEARN_ALBUM).unlockForWriting(AUTO_LEARN); + } catch (final ContextException e) { + executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e); + return false; + } + + executor.subject.getTaskKey(tasks.get(option)).copyTo(executor.selectedTask); + return true; + } + + /** + * Gets the option. + * + * @param diff the diff + * @param autoLearn the auto learn + * @return the option + */ + private int getOption(final double diff, final AutoLearn autoLearn) { + final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]); + final int r = RAND.nextInt(size); + int closestupi = -1; + int closestdowni = -1; + double closestup = Double.MAX_VALUE; + double closestdown = Double.MIN_VALUE; + for (int i = 0; i < size; i++) { + if (Double.isNaN(avdiffs[i])) { + return r; + } + if (avdiffs[i] >= diff && avdiffs[i] <= closestup) { + closestup = avdiffs[i]; + closestupi = i; + } + if (avdiffs[i] <= diff && avdiffs[i] >= closestdown) { + closestdown = avdiffs[i]; + closestdowni = i; + } + } + if (closestupi == -1 || closestdowni == -1) { + return r; + } + if (closestupi == closestdowni) { + return closestupi; + } + if (Math.abs(closestdown - diff) > Math.abs(closestup - diff)) { + return closestupi; + } else { + return closestdowni; + } + } + + /** + * Learn. + * + * @param option the option + * @param diff the diff + * @param autoLearn the auto learn + */ + private void learn(final int option, final double diff, final AutoLearn autoLearn) { + final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]); + final Long[] counts = autoLearn.getCounts().toArray(new Long[autoLearn.getCounts().size()]); + if (option < 0 || option >= avdiffs.length) { + throw new IllegalArgumentException("Error: option" + option); + } + counts[option]++; + if (Double.isNaN(avdiffs[option])) { + avdiffs[option] = diff; + } else { + avdiffs[option] = (avdiffs[option] * (counts[option] - 1) + diff) / counts[option]; + } + autoLearn.setAvDiffs(Arrays.asList(avdiffs)); + autoLearn.setCounts(Arrays.asList(counts)); + } +} diff --git a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicy_Decide_TaskSelectionLogic.java b/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicy_Decide_TaskSelectionLogic.java deleted file mode 100644 index 23d4e2486..000000000 --- a/examples/examples-adaptive/src/main/java/org/onap/policy/apex/examples/adaptive/model/java/AutoLearnPolicy_Decide_TaskSelectionLogic.java +++ /dev/null @@ -1,151 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.examples.adaptive.model.java; - -import java.util.Arrays; -import java.util.List; -import java.util.Random; - -import org.onap.policy.apex.context.ContextException; -import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; -import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn; - -/** - * The Class AutoLearnPolicy_Decide_TaskSelectionLogic. - */ -// CHECKSTYLE:OFF: checkstyle:typeName -public class AutoLearnPolicy_Decide_TaskSelectionLogic { - // CHECKSTYLE:ON: checkstyle:typeName - private static final Random RAND = new Random(System.currentTimeMillis()); - private static final double WANT = 50.0; - private int size; - - /** - * Gets the task. - * - * @param executor the executor - * @return the task - */ - public boolean getTask(final TaskSelectionExecutionContext executor) { - executor.logger.debug(executor.subject.getId()); - executor.logger.debug(executor.inFields.toString()); - final List tasks = executor.subject.getTaskNames(); - size = tasks.size(); - - try { - executor.getContextAlbum("AutoLearnAlbum").lockForWriting("AutoLearn"); - } catch (final ContextException e) { - executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e); - return false; - } - - // Get the context object - AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum("AutoLearnAlbum").get("AutoLearn"); - if (autoLearn == null) { - autoLearn = new AutoLearn(); - } - - // Check the lists are initialized - if (!autoLearn.isInitialized()) { - autoLearn.init(size); - } - - final double now = (Double) (executor.inFields.get("MonitoredValue")); - final double diff = now - WANT; - final int option = getOption(diff, autoLearn); - learn(option, diff, autoLearn); - - executor.getContextAlbum("AutoLearnAlbum").put("AutoLearnAlbum", autoLearn); - - try { - executor.getContextAlbum("AutoLearnAlbum").unlockForWriting("AutoLearn"); - } catch (final ContextException e) { - executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e); - return false; - } - - executor.subject.getTaskKey(tasks.get(option)).copyTo(executor.selectedTask); - return true; - } - - /** - * Gets the option. - * - * @param diff the diff - * @param autoLearn the auto learn - * @return the option - */ - private int getOption(final double diff, final AutoLearn autoLearn) { - final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]); - final int r = RAND.nextInt(size); - int closestupi = -1; - int closestdowni = -1; - double closestup = Double.MAX_VALUE; - double closestdown = Double.MIN_VALUE; - for (int i = 0; i < size; i++) { - if (Double.isNaN(avdiffs[i])) { - return r; - } - if (avdiffs[i] >= diff && avdiffs[i] <= closestup) { - closestup = avdiffs[i]; - closestupi = i; - } - if (avdiffs[i] <= diff && avdiffs[i] >= closestdown) { - closestdown = avdiffs[i]; - closestdowni = i; - } - } - if (closestupi == -1 || closestdowni == -1) { - return r; - } - if (closestupi == closestdowni) { - return closestupi; - } - if (Math.abs(closestdown - diff) > Math.abs(closestup - diff)) { - return closestupi; - } else { - return closestdowni; - } - } - - /** - * Learn. - * - * @param option the option - * @param diff the diff - * @param autoLearn the auto learn - */ - private void learn(final int option, final double diff, final AutoLearn autoLearn) { - final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]); - final Long[] counts = autoLearn.getCounts().toArray(new Long[autoLearn.getCounts().size()]); - if (option < 0 || option >= avdiffs.length) { - throw new IllegalArgumentException("Error: option" + option); - } - counts[option]++; - if (Double.isNaN(avdiffs[option])) { - avdiffs[option] = diff; - } else { - avdiffs[option] = (avdiffs[option] * (counts[option] - 1) + diff) / counts[option]; - } - autoLearn.setAvDiffs(Arrays.asList(avdiffs)); - autoLearn.setCounts(Arrays.asList(counts)); - } -} diff --git a/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask0TaskLogic.mvel b/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask0TaskLogic.mvel new file mode 100644 index 000000000..758062b8e --- /dev/null +++ b/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask0TaskLogic.mvel @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +logger.debug("AutoLearnDecideTask0: inFields:" + inFields); +java.util.Random rand = new java.util.Random(System.currentTimeMillis()); +double MIN = -100.0; +double MAX = 100.0; +double WANT = 50.0; + +double inval = (Double)(inFields["MonitoredValue"]); +double ret = inval; +/////////////// +double diff = (1.0/2.0) * (Math.abs(inval - WANT)); +/////////////// +ret = inval + diff; +if (ret > MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret MAX) + ret = MAX; +if (ret < MIN) + ret = MIN; + +outFields["MonitoredValue"] = ret; +outFields["LastMonitoredValue"] = inval; +logger.debug("AutoLearnDecideTask5: outFields:" + outFields); +return true; diff --git a/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask5_TaskLogic.mvel b/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask5_TaskLogic.mvel deleted file mode 100644 index fc2005fdf..000000000 --- a/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask5_TaskLogic.mvel +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ -logger.debug("AutoLearnDecideTask5: inFields:" + inFields); -java.util.Random rand = new java.util.Random(System.currentTimeMillis()); -double MIN = -100.0; -double MAX = 100.0; -double WANT = 50.0; - -double inval = (Double)(inFields["MonitoredValue"]); -double ret = inval; -/////////////// -double diff = (-1.0/5.0) * (Math.abs(inval - WANT)); -/////////////// -ret = inval + diff; -if (ret > MAX) - ret = MAX; -if (ret < MIN) - ret = MIN; - -outFields["MonitoredValue"] = ret; -outFields["LastMonitoredValue"] = inval; -logger.debug("AutoLearnDecideTask5: outFields:" + outFields); -return true; diff --git a/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask6TaskLogic.mvel b/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask6TaskLogic.mvel new file mode 100644 index 000000000..5eb44839c --- /dev/null +++ b/examples/examples-adaptive/src/main/resources/org/onap/policy/apex/examples/adaptive/model/mvel/AutoLearnDecideTask6TaskLogic.mvel @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +logger.debug("AutoLearnDecideTask6: inFields:" + inFields); +java.util.Random rand = new java.util.Random(System.currentTimeMillis()); +double MIN = -100.0; +double MAX = 100.0; +double WANT = 50.0; + +double inval = (Double)(inFields["MonitoredValue"]); +double ret = inval; +/////////////// +double diff = (-1.0/2.0) * (Math.abs(inval - WANT)); +/////////////// +ret = inval + diff; +if (ret > MAX) + ret = MAX; +if (ret MAX) - ret = MAX; -if (ret