
org.openremote.manager.rules.AssetQueryPredicate Maven / Gradle / Ivy
/*
* Copyright 2018, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.openremote.manager.rules;
import com.fasterxml.jackson.databind.JsonNode;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.model.attribute.AttributeInfo;
import org.openremote.model.attribute.MetaMap;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.LogicGroup;
import org.openremote.model.query.filter.*;
import org.openremote.model.util.ValueUtil;
import org.openremote.model.value.MetaHolder;
import org.openremote.model.value.NameValueHolder;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Test an {@link AttributeInfo} with a {@link AssetQuery}.
*/
public class AssetQueryPredicate implements Predicate {
final protected AssetQuery query;
final protected TimerService timerService;
final protected AssetStorageService assetStorageService;
final protected List resolvedAssetTypes;
public AssetQueryPredicate(TimerService timerService, AssetStorageService assetStorageService, AssetQuery query) {
this.timerService = timerService;
this.assetStorageService = assetStorageService;
this.query = query;
if (query.types != null && query.types.length > 0) {
resolvedAssetTypes = Arrays.asList(AssetQuery.getResolvedAssetTypes(query.types));
} else {
resolvedAssetTypes = null;
}
}
@Override
public boolean test(AttributeInfo assetState) {
if (query.ids != null && query.ids.length > 0) {
if (Arrays.stream(query.ids).noneMatch(id -> assetState.getId().equals(id))) {
return false;
}
}
if (query.names != null && query.names.length > 0) {
if (Arrays.stream(query.names)
.map(stringPredicate -> stringPredicate.asPredicate(timerService::getCurrentTimeMillis))
.noneMatch(np -> np.test(assetState.getAssetName()))) {
return false;
}
}
if (query.parents != null && query.parents.length > 0) {
if (Arrays.stream(query.parents)
.map(AssetQueryPredicate::asPredicate)
.noneMatch(np -> np.test(assetState))) {
return false;
}
}
if (resolvedAssetTypes != null && !resolvedAssetTypes.contains(assetState.getAssetType())) {
return false;
}
if (query.paths != null && query.paths.length > 0) {
if (Arrays.stream(query.paths)
.map(AssetQueryPredicate::asPredicate)
.noneMatch(np -> np.test(assetState.getPath()))) {
return false;
}
}
if (query.realm != null) {
if (!AssetQueryPredicate.asPredicate(query.realm).test(assetState)) {
return false;
}
}
if (query.attributes != null) {
// TODO: LogicGroup AND doesn't make much sense when applying to a single asset state
Set matches = asAttributeMatcher(timerService::getCurrentTimeMillis, query.attributes).apply(Collections.singleton(assetState));
if (matches == null) {
return false;
}
}
// Apply user ID predicate last as it is the most expensive
if (query.userIds != null && query.userIds.length > 0) {
return assetStorageService.isUserAsset(Arrays.asList(query.userIds), assetState.getId());
}
return true;
}
public static Predicate asPredicate(ParentPredicate predicate) {
return assetState ->
Objects.equals(predicate.id, assetState.getParentId());
}
public static Predicate asPredicate(PathPredicate predicate) {
return givenPath -> Arrays.equals(predicate.path, givenPath);
}
public static Predicate asPredicate(RealmPredicate predicate) {
return assetState ->
predicate == null || (predicate.name != null && predicate.name.equals(assetState.getRealm()));
}
public static Predicate> asPredicate(Supplier currentMillisSupplier, NameValuePredicate predicate) {
Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy