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));
}
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);
}
Aggregations