Search in sources :

Example 1 with NameValueHolder

use of org.openremote.model.value.NameValueHolder in project openremote by openremote.

the class AssetQueryPredicate method asPredicate.

public static Predicate<NameValueHolder<?>> asPredicate(Supplier<Long> currentMillisSupplier, NameValuePredicate predicate) {
    Predicate<Object> namePredicate = predicate.name != null ? predicate.name.asPredicate(currentMillisSupplier) : str -> true;
    Predicate<Object> valuePredicate = value -> {
        if (predicate.value == null) {
            return true;
        }
        return predicate.value.asPredicate(currentMillisSupplier).test(value);
    };
    AtomicReference<Function<NameValueHolder<?>, Object>> valueExtractor = new AtomicReference<>(nameValueHolder -> nameValueHolder.getValue().orElse(null));
    if (predicate.path != null && predicate.path.getPaths().length > 0) {
        valueExtractor.set(nameValueHolder -> {
            if (!nameValueHolder.getValue().isPresent()) {
                return null;
            }
            Object rawValue = nameValueHolder.getValue().get();
            if (!ValueUtil.isArray(rawValue.getClass()) && !ValueUtil.isObject(rawValue.getClass())) {
                return null;
            }
            JsonNode jsonNode = ValueUtil.convert(nameValueHolder.getValue(), JsonNode.class);
            for (Object path : predicate.path.getPaths()) {
                if (path == null) {
                    return null;
                }
                if (path instanceof Integer) {
                    jsonNode = jsonNode.get((int) path);
                } else if (path instanceof String) {
                    jsonNode = jsonNode.get((String) path);
                }
                if (jsonNode == null) {
                    break;
                }
            }
            return jsonNode;
        });
    }
    return nameValueHolder -> namePredicate.test(nameValueHolder.getName()) && valuePredicate.test(valueExtractor.get().apply(nameValueHolder));
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) LogicGroup(org.openremote.model.query.LogicGroup) MetaHolder(org.openremote.model.value.MetaHolder) java.util(java.util) Predicate(java.util.function.Predicate) AssetQuery(org.openremote.model.query.AssetQuery) ThingAsset(org.openremote.model.asset.impl.ThingAsset) ValueUtil(org.openremote.model.util.ValueUtil) AssetState(org.openremote.model.rules.AssetState) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) MetaMap(org.openremote.model.attribute.MetaMap) NameValueHolder(org.openremote.model.value.NameValueHolder) TimerService(org.openremote.container.timer.TimerService) org.openremote.model.query.filter(org.openremote.model.query.filter) JsonNode(com.fasterxml.jackson.databind.JsonNode) Function(java.util.function.Function) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with NameValueHolder

use of org.openremote.model.value.NameValueHolder in project openremote by openremote.

the class AssetQueryPredicate method asPredicate.

@SuppressWarnings("unchecked")
public static Predicate<AssetState<?>> asPredicate(Supplier<Long> currentMillisProducer, LogicGroup<AttributePredicate> condition) {
    if (groupIsEmpty(condition)) {
        return as -> true;
    }
    LogicGroup.Operator operator = condition.operator == null ? LogicGroup.Operator.AND : condition.operator;
    List<Predicate<AssetState<?>>> assetStatePredicates = new ArrayList<>();
    if (condition.getItems().size() > 0) {
        condition.getItems().stream().forEach(p -> {
            assetStatePredicates.add((Predicate<AssetState<?>>) (Predicate) asPredicate(currentMillisProducer, p));
            AtomicReference<Predicate<AssetState<?>>> metaPredicate = new AtomicReference<>(nameValueHolder -> true);
            AtomicReference<Predicate<AssetState<?>>> oldValuePredicate = new AtomicReference<>(value -> true);
            if (p.meta != null) {
                final Predicate<NameValueHolder<?>> innerMetaPredicate = Arrays.stream(p.meta).map(metaPred -> asPredicate(currentMillisProducer, metaPred)).reduce(x -> true, Predicate::and);
                metaPredicate.set(assetState -> {
                    MetaMap metaItems = ((MetaHolder) assetState).getMeta();
                    return metaItems.stream().anyMatch(metaItem -> innerMetaPredicate.test(assetState));
                });
                assetStatePredicates.add(metaPredicate.get());
            }
            if (p.previousValue != null) {
                Predicate<Object> innerOldValuePredicate = p.previousValue.asPredicate(currentMillisProducer);
                oldValuePredicate.set(nameValueHolder -> innerOldValuePredicate.test((nameValueHolder).getOldValue()));
                assetStatePredicates.add(oldValuePredicate.get());
            }
        });
    }
    if (condition.groups != null && condition.groups.size() > 0) {
        assetStatePredicates.addAll(condition.groups.stream().map(c -> asPredicate(currentMillisProducer, c)).collect(Collectors.toList()));
    }
    return asPredicate(assetStatePredicates, operator);
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) LogicGroup(org.openremote.model.query.LogicGroup) MetaHolder(org.openremote.model.value.MetaHolder) java.util(java.util) Predicate(java.util.function.Predicate) AssetQuery(org.openremote.model.query.AssetQuery) ThingAsset(org.openremote.model.asset.impl.ThingAsset) ValueUtil(org.openremote.model.util.ValueUtil) AssetState(org.openremote.model.rules.AssetState) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) MetaMap(org.openremote.model.attribute.MetaMap) NameValueHolder(org.openremote.model.value.NameValueHolder) TimerService(org.openremote.container.timer.TimerService) org.openremote.model.query.filter(org.openremote.model.query.filter) JsonNode(com.fasterxml.jackson.databind.JsonNode) NameValueHolder(org.openremote.model.value.NameValueHolder) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogicGroup(org.openremote.model.query.LogicGroup) Predicate(java.util.function.Predicate) MetaHolder(org.openremote.model.value.MetaHolder) AssetState(org.openremote.model.rules.AssetState) MetaMap(org.openremote.model.attribute.MetaMap)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 java.util (java.util)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Supplier (java.util.function.Supplier)2 Collectors (java.util.stream.Collectors)2 TimerService (org.openremote.container.timer.TimerService)2 AssetStorageService (org.openremote.manager.asset.AssetStorageService)2 ThingAsset (org.openremote.model.asset.impl.ThingAsset)2 MetaMap (org.openremote.model.attribute.MetaMap)2 AssetQuery (org.openremote.model.query.AssetQuery)2 LogicGroup (org.openremote.model.query.LogicGroup)2 org.openremote.model.query.filter (org.openremote.model.query.filter)2 AssetState (org.openremote.model.rules.AssetState)2 ValueUtil (org.openremote.model.util.ValueUtil)2 MetaHolder (org.openremote.model.value.MetaHolder)2 NameValueHolder (org.openremote.model.value.NameValueHolder)2