100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
JSON rules make 'ANDED' asset query attributes work
Rich Turner50172a9c23b9
diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex 0ce712c8b..cd704037e 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -128,6 +128,7 @@ public class AssetQueryPredicate implements Predicate<AssetState> {}if (query.attributes != null) {+ // TODO: LogicGroup AND doesn't make much sense when applying to a single asset stateif (!asPredicate(timerService::getCurrentTimeMillis, query.attributes).test(assetState)) {return false;}diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex d6c785941..1d8c3109a 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -48,6 +48,7 @@ import org.quartz.CronExpression;import java.util.*;import java.util.function.BiConsumer;import java.util.function.Predicate;+import java.util.function.Supplier;import java.util.logging.Level;import java.util.stream.Collectors;import java.util.stream.Stream;@@ -85,6 +86,7 @@ public class JsonRulesBuilder extends RulesBuilder {BaseAssetQuery.OrderBy orderBy;int limit;LogicGroup<AttributePredicate> attributePredicates = null;+ Collection<Predicate<AssetState>> attributeAndedPredicates = null;RuleCondition ruleCondition;Set<AssetState> unfilteredAssetStates = new HashSet<>();Set<AssetState> previouslyMatchedAssetStates = new HashSet<>();@@ -135,6 +137,16 @@ public class JsonRulesBuilder extends RulesBuilder {orderBy = ruleCondition.assets.orderBy;limit = ruleCondition.assets.limit;attributePredicates = ruleCondition.assets.attributes;++ if (attributePredicates != null && attributePredicates.items != null) {+ // Note only supports a single level or logic group for attributes (i.e. cannot nest groups)+ attributePredicates.groups = null;+ if (attributePredicates.operator == null || attributePredicates.operator == RuleOperator.AND) {+ attributeAndedPredicates = Arrays.stream(attributePredicates.items)+ .map(attributePredicate -> AssetQueryPredicate.asPredicate(timerService::getCurrentTimeMillis, attributePredicate))+ .collect(Collectors.toList());+ }+ }ruleCondition.assets.orderBy = null;ruleCondition.assets.limit = 0;ruleCondition.assets.attributes = null;@@ -195,12 +207,43 @@ public class JsonRulesBuilder extends RulesBuilder {if (attributePredicates == null) {matchedAssetStates = new ArrayList<>(unfilteredAssetStates);} else {- Predicate<AssetState> predicate = AssetQueryPredicate.asPredicate(timerService::getCurrentTimeMillis, attributePredicates);- Map<Boolean,List<AssetState>> results = unfilteredAssetStates.stream().collect(Collectors.groupingBy(predicate::test));+ Map<Boolean, List<AssetState>> results;++ if (attributeAndedPredicates != null) {+ // ANDs need to be applied in the context of an entire asset as don't make any sense otherwise+ results = new HashMap<>();+ ArrayList<AssetState> matched = new ArrayList<>();+ ArrayList<AssetState> unmatched = new ArrayList<>();+ results.put(true, matched);+ results.put(false, unmatched);++ unfilteredAssetStates.stream().collect(Collectors.groupingBy(AssetState::getId)).forEach((id, states) -> {+ Set<AssetState> assetMatched = new HashSet<>();++ for (Predicate<AssetState> attributePredicate : attributeAndedPredicates) {+ Collection<AssetState> assetPredicateMatched = states.stream().filter(attributePredicate).collect(Collectors.toList());++ if (assetPredicateMatched.isEmpty()) {+ assetMatched.clear();+ break;+ }++ assetMatched.addAll(assetPredicateMatched);+ }++ matched.addAll(assetMatched);+ unmatched.addAll(states.stream().filter(as -> !assetMatched.contains(as)).collect(Collectors.toList()));+ });++ } else {+ Predicate<AssetState> predicate = AssetQueryPredicate.asPredicate(timerService::getCurrentTimeMillis, attributePredicates);+ results = unfilteredAssetStates.stream().collect(Collectors.groupingBy(predicate::test));+ }+matchedAssetStates = results.getOrDefault(true, Collections.emptyList());+ unmatchedAssetStates = results.getOrDefault(false, Collections.emptyList());if (trackUnmatched) {- unmatchedAssetStates = results.getOrDefault(false, Collections.emptyList());// Clear out previous unmatched that now matchpreviouslyUnmatchedAssetStates.removeIf(matchedAssetStates::contains);diff --git a/ui/component/or-rules-editor/src/index.ts b/ui/component/or-rules-editor/src/index.tsindex 088636dc5..3fbd7662e 100644--- a/ui/component/or-rules-editor/src/index.ts+++ b/ui/component/or-rules-editor/src/index.ts@@ -105,6 +105,7 @@ export interface RulesConfig {hideConditionTypeOptions?: boolean;hideThenAddAction?: boolean;hideWhenAddCondition?: boolean;+ hideWhenAddAttribute?: boolean;hideWhenAddGroup?: boolean;hideWhenGroupOutline?: boolean;};diff --git a/ui/component/or-rules-editor/src/or-rule-asset-query.ts b/ui/component/or-rules-editor/src/or-rule-asset-query.tsindex ea429a36f..b0e4bfeaa 100644--- a/ui/component/or-rules-editor/src/or-rule-asset-query.ts+++ b/ui/component/or-rules-editor/src/or-rule-asset-query.ts@@ -9,7 +9,9 @@ import {ValuePredicateUnion,ValueType,AttributeValueDescriptor,- AssetDescriptor+ AssetDescriptor,+ RuleOperator,+ LogicGroup} from "@openremote/model";import {AssetQueryOperator, AssetTypeAttributeName,@@ -54,11 +56,13 @@ class OrRuleAssetQuery extends LitElement {const attributes = this.getAttributes(assetDescriptor).map((a) => [a, i18next.t(a)]);return html`- <or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setAttributeName(attributePredicate, e.detail.value)}" ?readonly="${this.readonly}" .options="${attributes}" .value="${attributeName}"></or-select>-- ${attributeName ? html`<or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setOperator(assetDescriptor, attributePredicate, e.detail.value)}" ?readonly="${this.readonly}" .options="${this.getOperators(assetDescriptor, attributeName)}" .value="${operator}"></or-select>` : ``}-- ${attributePredicate ? this.attributePredicateValueEditorTemplate(assetDescriptor, attributePredicate) : ``}+ <div class="attribute-editor">+ <or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setAttributeName(attributePredicate, e.detail.value)}" ?readonly="${this.readonly}" .options="${attributes}" .value="${attributeName}"></or-select>++ ${attributeName ? html`<or-select @or-select-changed="${(e: OrSelectChangedEvent) => this.setOperator(assetDescriptor, attributePredicate, e.detail.value)}" ?readonly="${this.readonly}" .options="${this.getOperators(assetDescriptor, attributeName)}" .value="${operator}"></or-select>` : ``}++ ${attributePredicate ? this.attributePredicateValueEditorTemplate(assetDescriptor, attributePredicate) : ``}+ </div>`;}@@ -143,11 +147,33 @@ class OrRuleAssetQuery extends LitElement {this.query.attributes.items = [{}];}+ const showAddAttribute = !this.readonly && (!this.config || !this.config.controls || this.config.controls.hideWhenAddAttribute !== true);+ const showRemoveAttribute = !this.readonly && this.query.attributes && this.query.attributes.items && this.query.attributes.items.length > 1;+return html`- <div class="attribute-predicate-list">- ${this.query.attributes && this.query.attributes.items ? this.query.attributes.items.map((attributePredicate) => {- return html`${this.attributePredicateEditorTemplate(assetDescriptor, attributePredicate)}`;- }) : ``}+ <div class="attribute-group">+ <div class="attribute-items">+ ${this.query.attributes && this.query.attributes.items ? this.query.attributes.items.map((attributePredicate) => {++ return html`+ <div class="attribute-item">+ <button @click="${() => this.toggleAttributeGroup(this.query!.attributes!)}" class="button-clear operator"><span>${i18next.t(!this.query!.attributes!.operator || this.query!.attributes!.operator === RuleOperator.AND ? "and" : "or")}</span></button>+ <div class="attribute">+ ${this.attributePredicateEditorTemplate(assetDescriptor, attributePredicate)}+ ${showRemoveAttribute ? html`+ <button class="button-clear" @click="${() => this.removeAttributePredicate(this.query!.attributes!, attributePredicate)}"><or-icon icon="close-circle"></or-icon></input>+ ` : ``}+ </div>+ </div>+ `;+ }) : ``}+ </div>++ ${showAddAttribute ? html`+ <div class="add-buttons-container">+ ${showAddAttribute ? html`<button class="button-clear add-button" @click="${() => this.addAttributePredicate(this.query!.attributes!)}"><or-icon icon="plus-circle"></or-icon><or-translate value="rulesEditorAddCondition"></or-translate></button>` : ``}+ </div>+ ` : ``}</div>`;}@@ -532,4 +558,32 @@ class OrRuleAssetQuery extends LitElement {this.dispatchEvent(new OrRuleChangedEvent());this.requestUpdate();}++ protected removeAttributePredicate(group: LogicGroup<AttributePredicate>, attributePredicate: AttributePredicate) {+ const index = group.items!.indexOf(attributePredicate);+ if (index >= 0) {+ group.items!.splice(index, 1);+ }+ this.dispatchEvent(new OrRuleChangedEvent());+ this.requestUpdate();+ }++ protected addAttributePredicate(group: LogicGroup<AttributePredicate>) {+ if (!group.items) {+ group.items = [];+ }+ group.items.push({});+ this.dispatchEvent(new OrRuleChangedEvent());+ this.requestUpdate();+ }++ protected toggleAttributeGroup(group: LogicGroup<AttributePredicate>) {+ if (group.operator === RuleOperator.OR) {+ group.operator = RuleOperator.AND;+ } else {+ group.operator = RuleOperator.OR;+ }+ this.dispatchEvent(new OrRuleChangedEvent());+ this.requestUpdate();+ }}diff --git a/ui/component/or-rules-editor/src/or-rule-when.ts b/ui/component/or-rules-editor/src/or-rule-when.tsindex c6fcaf757..c89daacf5 100644--- a/ui/component/or-rules-editor/src/or-rule-when.ts+++ b/ui/component/or-rules-editor/src/or-rule-when.ts@@ -25,18 +25,20 @@ class OrRuleWhen extends LitElement {return html`<button class="button-clear add-button" @click="${() => this.addGroup(parent)}"><or-icon icon="plus-circle"></or-icon><or-translate value="rulesEditorAddGroup"></or-translate></button>`;}- protected ruleGroupTemplate(group: LogicGroup<RuleCondition>): TemplateResult | string {+ protected ruleGroupTemplate(group: LogicGroup<RuleCondition>, parentGroup?: LogicGroup<RuleCondition>): TemplateResult | string {if (!group) {return ``;}- const showAddCondition = !this.config || !this.config.controls || this.config.controls.hideWhenAddCondition !== true;- const showAddgroup = !this.config || !this.config.controls || this.config.controls.hideWhenAddGroup !== true;const showGroupOutline = !this.config || !this.config.controls || this.config.controls.hideWhenGroupOutline !== true;+ const showAddCondition = !this.readonly && (!this.config || !this.config.controls || this.config.controls.hideWhenAddCondition !== true);+ const showRemoveCondition = !this.readonly && group.items && group.items.length > 1;+ const showAddGroup = !this.readonly && (!this.config || !this.config.controls || this.config.controls.hideWhenAddGroup !== true);+ const showRemoveGroup = !this.readonly && (parentGroup || (group.groups && group.groups.length > 0));return html`<div class="rule-group ${showGroupOutline ? "visible" : "hidden"}">- ${!this.readonly && showAddgroup ? html`+ ${showRemoveGroup ? html`<button class="button-clear remove-button" @click="${() => this.removeGroup(group)}"><div></div><or-icon icon="close-circle"></or-icon>@@ -50,7 +52,7 @@ class OrRuleWhen extends LitElement {<button @click="${() => this.toggleGroup(group)}" class="button-clear operator"><span>${i18next.t(!group.operator || group.operator === RuleOperator.AND ? "and" : "or")}</span></button><div class="rule-condition"><or-rule-condition .config="${this.config}" .assetDescriptors="${this.assetDescriptors}" .ruleCondition="${condition}" .readonly="${this.readonly}" ></or-rule-condition>- ${!this.readonly ? html`+ ${showRemoveCondition ? html`<button class="button-clear" @click="${() => this.removeCondition(condition)}"><or-icon icon="close-circle"></or-icon></input>` : ``}</div>@@ -62,16 +64,16 @@ class OrRuleWhen extends LitElement {${group.groups.map((childGroup: LogicGroup<RuleCondition>) => html`<div class="rule-group-item"><button @click="${() => this.toggleGroup(group)}" class="button-clear operator"><span>${i18next.t(!group.operator || group.operator === RuleOperator.AND ? "and" : "or")}</span></button>- ${this.ruleGroupTemplate(childGroup)}+ ${this.ruleGroupTemplate(childGroup, group)}</div>`)}` : ``}</div>- ${showAddCondition || showAddgroup ? html`+ ${showAddCondition || showAddGroup ? html`<div class="add-buttons-container">${showAddCondition ? html`<button class="button-clear add-button" @click="${() => this.addCondition(group)}"><or-icon icon="plus-circle"></or-icon><or-translate value="rulesEditorAddCondition"></or-translate></button>` : ``}- ${showAddgroup ? this.groupAddTemplate(group) : ``}+ ${showAddGroup ? this.groupAddTemplate(group) : ``}</div>` : ``}</div>diff --git a/ui/component/or-rules-editor/src/style.ts b/ui/component/or-rules-editor/src/style.tsindex 4180a4ab3..dff9747fc 100644--- a/ui/component/or-rules-editor/src/style.ts+++ b/ui/component/or-rules-editor/src/style.ts@@ -504,10 +504,17 @@ export const conditionEditorStyle = css`:host > * {margin-right: 20px;}++ or-rule-asset-query {+ flex-grow: 1;+ }`;// language=CSSexport const assetQueryEditorStyle = css`++ ${buttonStyle}+:host {display: flex;flex-direction: row;@@ -516,4 +523,58 @@ export const assetQueryEditorStyle = css`:host > * {margin-right: 20px;}++ .attribute-group {+ flex-grow: 1;+ display: flex;+ flex-direction: column;+ align-items: stretch;+ position: relative;+ }++ .attribute-items {+ display: flex;+ flex-direction: column;+ }++ .attribute-items > div {+ margin: 10px 0;+ }++ .attribute-item {+ position: relative;+ padding-left: 40px;+ }++ .attribute {+ display: flex;+ }++ .operator {+ text-transform: uppercase;+ position: absolute;+ left: 0;+ top: 100%;+ padding: 2px;+ border: 2px solid var(--internal-or-rule-foreground-color);+ border-radius: 3px;+ font-weight: bold;+ }++ .operator * {+ font-size: small;+ color: var(--internal-or-rule-foreground-color);+ }++ .attribute > * {+ flex-grow: 0;+ }++ .attribute > .attribute-editor {+ flex-grow: 1;+ }++ .attribute-items div:last-child > .operator {+ display: none;+ }`;