Search in sources :

Example 1 with MetaMap

use of org.openremote.model.attribute.MetaMap 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)1 java.util (java.util)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 TimerService (org.openremote.container.timer.TimerService)1 AssetStorageService (org.openremote.manager.asset.AssetStorageService)1 ThingAsset (org.openremote.model.asset.impl.ThingAsset)1 MetaMap (org.openremote.model.attribute.MetaMap)1 AssetQuery (org.openremote.model.query.AssetQuery)1 LogicGroup (org.openremote.model.query.LogicGroup)1 org.openremote.model.query.filter (org.openremote.model.query.filter)1 AssetState (org.openremote.model.rules.AssetState)1 ValueUtil (org.openremote.model.util.ValueUtil)1 MetaHolder (org.openremote.model.value.MetaHolder)1 NameValueHolder (org.openremote.model.value.NameValueHolder)1