/
Recipe: Change Behavior of Detection Mechanism

Recipe: Change Behavior of Detection Mechanism

By default Price Flexibility Monitor detects any change in observed attributes. This includes every change: from one value to another, e.g. 2 → 4 or “red” → “blue”, from null to any value or from any value to null.

This article shows how this default change detection can be customized.

In this section:

Scope of Change

You will find here the following examples of change detection customization:

  • Ignore changes from null to any value and consider only changes when the attribute already had a non-null value. Also ignore when a value appears for the first time.

  • Define an explicit status change of the observed attribute. Do not detect any change, but only when the attribute changes to some target value.

  • Define how much a numerical value (e.g. cost) has to change to be detected as a change (e.g. more than 5%).

Background

Generally, Price Flexibility Monitor works with a special Price Extension (PX). This PX contains one pair of values for every observable attribute. One of these values is the “active value”. In every run the monitor compares the current value of the observable attribute with the active one and when it is different, it detects a change. In this case, the active value is copied to the “old value” field and the current value is stored as a new active value.

To sum it up:

  • Current value – The value which is right now in the source of the observed attribute. This is the “live” value of the lookup.

  • Active value – The value which is in the “active” field in the monitor. It was a “current value” in the last run before the change detection.

  • Old value – The value which was valid before the last change was detected.

When we detect a change during a run, the following takes place:

  • The current value is stored as the new active value.

  • The last active value is stored as the new old value.

When no change is detected, no action is taken.

Tweak the Logic

The change detection is done by a CFS running over the “Monitor” PX.

The logic for this CFS is called DetectChangesInMonitor_CFS. To change the behavior of change detection, we have to tweak this logic, specifically the element DetectChanges.

Create Custom Change Detection

For the change detection, we have the following function in the element:

Boolean isValueChanged(def oldValue, def newValue) { Boolean valueChanged = oldValue != newValue Boolean isNullHandlingException = newValue == null && oldValue == api.getElement("Configuration").MONITOR_CONFIG.NULL_VALUE return valueChanged && !isNullHandlingException }

Note isNullHandlingException: We use a placeholder <<null>> in the PX to distinguish between an empty (uninitialized) value and explicit null. You can access this placeholder via api.getElement("Configuration").MONITOR_CONFIG.NULL_VALUE. So this check handles empty values (null) and checks if it was our “null placeholder” before.

 

We propose to create a new function here with a signature like this:

Boolean isValueChangedCustom(def oldValue, def newValue) { Boolean valueChanged = oldValue != newValue Boolean isNullHandlingException = newValue == null && oldValue == api.getElement("Configuration").MONITOR_CONFIG.NULL_VALUE return valueChanged && !isNullHandlingException }

Now you can add the new required behavior. Let´s say we only want to listen to changes when a value changes to “true”, no matter what it was before.

Boolean isValueChangedCustom(def oldValue, def newValue) { Boolean valueChanged = oldValue != newValue && newValue == "true" Boolean isNullHandlingException = newValue == null && oldValue == api.getElement("Configuration").MONITOR_CONFIG.NULL_VALUE return valueChanged && !isNullHandlingException }

In this sample, only a value change to “true” is considered a change.

Employ Custom Change Detection

To make use of this custom change detection you have to tweak another part in the DetectChanges element:

You need to invoke the new function in case of change detection:

Now Price Flexibility Monitor will only detect changes when the new value is “true”.

Behavior Depending on Observable Attribute (Optional)

You may want to restrict the new change detection to only some observable attributes. In this case you can invoke different functions depending on what observable attribute you are looking at:

You need to branch here.

Related content

How Price Flexibility Package Works
How Price Flexibility Package Works
More like this
Price Flexibility Overview
Price Flexibility Overview
More like this
Price Flexibility Package 1.1.0
Price Flexibility Package 1.1.0
More like this