Skip to content
Permalink
Browse files

Fix for casting exception of list to array

  • Loading branch information...
Miggets7 committed Sep 11, 2019
1 parent d1db277 commit 3811446eceb36a625266ed76af8daa09080fdab1
@@ -1151,9 +1151,7 @@ protected void addAttributePredicateGroupQuery(StringBuilder sb, List<ParameterB
sb.append("(");
boolean isFirst = true;

if (attributePredicateGroup.items != null) {
for (AttributePredicate attributePredicate : attributePredicateGroup.items) {

for (AttributePredicate attributePredicate : attributePredicateGroup.getItems()) {
if (!isFirst) {
sb.append(operator == LogicGroup.Operator.OR ? " or " : " and ");
}
@@ -1163,9 +1161,8 @@ protected void addAttributePredicateGroupQuery(StringBuilder sb, List<ParameterB
sb.append(buildAttributeFilter(attributePredicate, binders));
sb.append(")");
}
}

if (attributePredicateGroup.groups != null) {
if (attributePredicateGroup.groups != null && attributePredicateGroup.groups.size() > 0) {
for (LogicGroup<AttributePredicate> group : attributePredicateGroup.groups) {
addAttributePredicateGroupQuery(sb, binders, group);
}
@@ -483,9 +483,9 @@ public boolean test(AssetState assetState) {

List<Predicate<AssetState>> assetStatePredicates = new ArrayList<>();

if (condition.items != null && condition.items.length > 0) {
if (condition.getItems().size() > 0) {
assetStatePredicates.addAll(
Arrays.stream(condition.items)
condition.getItems().stream()
.map(p -> {
if (p instanceof NewAttributePredicate) {
return asPredicate(currentMillisProducer, (NewAttributePredicate)p);
@@ -495,9 +495,9 @@ public boolean test(AssetState assetState) {
);
}

if (condition.groups != null && condition.groups.length > 0) {
if (condition.groups != null && condition.groups.size() > 0) {
assetStatePredicates.addAll(
Arrays.stream(condition.groups)
condition.groups.stream()
.map(c -> asPredicate(currentMillisProducer, c)).collect(Collectors.toList())
);
}
@@ -506,8 +506,8 @@ public boolean test(AssetState assetState) {
}

protected static boolean conditionIsEmpty(LogicGroup condition) {
return (condition.items == null || condition.items.length == 0)
&& (condition.groups == null || condition.groups.length == 0);
return condition.getItems().size() == 0
&& (condition.groups != null && condition.groups.size() > 0);
}

protected static <T> Predicate<T> asPredicate(Collection<Predicate<T>> predicates, LogicGroup.Operator operator) {
@@ -26,9 +26,7 @@
import org.openremote.manager.concurrent.ManagerExecutorService;
import org.openremote.manager.rules.facade.NotificationsFacade;
import org.openremote.model.asset.Asset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.notification.Notification;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.LogicGroup;
@@ -141,7 +139,7 @@ public RuleTriggerState(RuleCondition ruleCondition, boolean trackUnmatched, Tim
// 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 == LogicGroup.Operator.AND) {
attributeAndedPredicates = Arrays.stream(attributePredicates.items)
attributeAndedPredicates = attributePredicates.items.stream()
.map(attributePredicate -> AssetQueryPredicate.asPredicate(timerService::getCurrentTimeMillis, attributePredicate))
.collect(Collectors.toList());
}
@@ -473,8 +471,8 @@ public JsonRulesBuilder add(JsonRule rule) {

protected void addRuleTriggerStates(LogicGroup<RuleCondition> ruleConditionCondition, boolean trackUnmatched, int index, Map<String, RuleTriggerState> triggerStateMap, RuleConditionReset defaultReset) {
if (ruleConditionCondition != null) {
if (ruleConditionCondition.items != null && ruleConditionCondition.items.length > 0) {
for (RuleCondition ruleCondition : ruleConditionCondition.items) {
if (ruleConditionCondition.getItems().size() > 0) {
for (RuleCondition ruleCondition : ruleConditionCondition.getItems()) {
if (TextUtil.isNullOrEmpty(ruleCondition.tag)) {
ruleCondition.tag = Integer.toString(index);
}
@@ -485,7 +483,7 @@ protected void addRuleTriggerStates(LogicGroup<RuleCondition> ruleConditionCondi
index++;
}
}
if (ruleConditionCondition.groups != null && ruleConditionCondition.groups.length > 0) {
if (ruleConditionCondition.groups != null && ruleConditionCondition.groups.size() > 0) {
for (LogicGroup<RuleCondition> childRuleTriggerCondition : ruleConditionCondition.groups) {
addRuleTriggerStates(childRuleTriggerCondition, trackUnmatched, index, triggerStateMap, defaultReset);
}
@@ -840,20 +838,20 @@ protected static void matches(LogicGroup<RuleCondition> ruleConditionGroup, Rule
LogicGroup.Operator operator = ruleConditionGroup.operator == null ? LogicGroup.Operator.AND : ruleConditionGroup.operator;

if (operator == LogicGroup.Operator.AND) {
if (ruleConditionGroup.items != null) {
if (ruleConditionGroup.getItems().size() > 0) {

ruleEvaluationResult.thenMatched = Arrays.stream(ruleConditionGroup.items)
ruleEvaluationResult.thenMatched = ruleConditionGroup.getItems().stream()
.map(ruleCondition -> ruleEvaluationResult.conditionStateMap.get(ruleCondition.tag))
.filter(ruleConditionState -> ruleConditionState.lastTriggerResult != null && ruleConditionState.lastTriggerResult.matches) //Only trigger results which matches
.map(RuleTriggerState::getMatchedAssetIds)//Get all matched assetIds
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(assetId -> assetId, Collectors.counting()))//Group them and count the number of times appearing in the rule conditions
.entrySet()
.stream()
.anyMatch(entry -> entry.getValue() == ruleConditionGroup.items.length);//If at least one of the assets appear in all the rule conditions, then fire
.anyMatch(entry -> entry.getValue() == ruleConditionGroup.getItems().size());//If at least one of the assets appear in all the rule conditions, then fire

if (trackUnmatched) {
ruleEvaluationResult.otherwiseMatched = Arrays.stream(ruleConditionGroup.items)
ruleEvaluationResult.otherwiseMatched = ruleConditionGroup.getItems().stream()
.map(ruleCondition -> ruleEvaluationResult.conditionStateMap.get(ruleCondition.tag))
.anyMatch(ruleConditionState -> ruleConditionState.trackUnmatched && ruleConditionState.lastTriggerResult != null && ruleConditionState.lastTriggerResult.matches);
}
@@ -864,27 +862,27 @@ protected static void matches(LogicGroup<RuleCondition> ruleConditionGroup, Rule
}

if (ruleConditionGroup.groups != null) {
Arrays.stream(ruleConditionGroup.groups)
ruleConditionGroup.groups
.forEach(condition -> matches(condition, ruleEvaluationResult, trackUnmatched));
}
}

if (operator == LogicGroup.Operator.OR) {
if (ruleConditionGroup.items != null) {
ruleEvaluationResult.thenMatched = Arrays.stream(ruleConditionGroup.items)
ruleEvaluationResult.thenMatched = ruleConditionGroup.items.stream()
.map(ruleCondition -> ruleEvaluationResult.conditionStateMap.get(ruleCondition.tag))
.anyMatch(ruleConditionState -> ruleConditionState.lastTriggerResult != null && ruleConditionState.lastTriggerResult.matches);

if (trackUnmatched) {
ruleEvaluationResult.otherwiseMatched = Arrays.stream(ruleConditionGroup.items)
ruleEvaluationResult.otherwiseMatched = ruleConditionGroup.items.stream()
.map(ruleCondition -> ruleEvaluationResult.conditionStateMap.get(ruleCondition.tag))
.filter(ruleConditionState -> ruleConditionState.trackUnmatched && ruleConditionState.lastTriggerResult != null && ruleConditionState.lastTriggerResult.matches) //Only trigger results which matches
.map(RuleTriggerState::getUnmatchedAssetIds)//Get all unmatched assetIds
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(assetId -> assetId, Collectors.counting()))//Group them and count the number of times appearing in the rule conditions
.entrySet()
.stream()
.anyMatch(entry -> entry.getValue() == ruleConditionGroup.items.length);//If at least one of the assets appear in all the rule conditions, then fire
.anyMatch(entry -> entry.getValue() == ruleConditionGroup.items.size());//If at least one of the assets appear in all the rule conditions, then fire
}

if (ruleEvaluationResult.thenMatched) {
@@ -893,7 +891,7 @@ protected static void matches(LogicGroup<RuleCondition> ruleConditionGroup, Rule
}

if (ruleConditionGroup.groups != null) {
Arrays.stream(ruleConditionGroup.groups)
ruleConditionGroup.groups
.forEach(condition -> matches(condition, ruleEvaluationResult, trackUnmatched));
}
}

0 comments on commit 3811446

Please sign in to comment.
You can’t perform that action at this time.