Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fixed javac warnings, unfinished drools API test for rule generation

Christian Bauer4f726729dcf1
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index f9d8d9365..849a60487 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -265,6 +265,7 @@ public abstract class AbstractProtocol implements Protocol {
* {@link MessageFilter}s that have been set for the {@link Attribute} against the {@link AttributeState#value}
* before sending on the sensor queue.
*/
+ @SuppressWarnings("unchecked")
protected void updateLinkedAttribute(AttributeState state, long timestamp) {
AssetAttribute attribute;
synchronized (linkedAttributes) {
@@ -296,7 +297,6 @@ public abstract class AbstractProtocol implements Protocol {
} else {
try {
LOG.finest("Applying message filter: " + filter.getClass().getName());
- //noinspection unchecked
value = filter.process(value);
} catch (Exception e) {
LOG.log(
diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
index 335b25a93..52d8ba3d8 100644
--- a/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentAllLightsOff.drl
@@ -1,9 +1,9 @@
package org.openremote.test.rules;
-import org.openremote.model.*;
import org.openremote.model.asset.*
+import org.openremote.model.attribute.*
+import org.openremote.model.value.*
import org.openremote.model.value.Values
-import org.openremote.model.attribute.AttributeEvent
global java.util.logging.Logger LOG;
global org.openremote.model.rules.Assets assets;
@@ -11,7 +11,7 @@ global org.openremote.model.rules.Assets assets;
rule "Switch room lights off when apartment ALL LIGHTS OFF push-button is pressed"
when
// In an asset of type RESIDENCE the All Lights Off push-button is pressed, which sends a 'true' value
- $e: AssetEvent(attributeName == "allLightsOffSwitch", type == AssetType.RESIDENCE, valueAsBoolean == true)
+ $e: AssetEvent(type == AssetType.RESIDENCE, attributeName == "allLightsOffSwitch", valueAsBoolean == true)
then
assets.query() // Execute a query to get "other" assets
.parent($e.getId()) // Where parent is the apartment in LHS
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index e03ae46ee..6850566e7 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -112,6 +112,7 @@ public class Values {
return getArrayElements(arrayValue, elementType, throwOnError, includeNulls, value -> value);
}
+ @SuppressWarnings("unchecked")
public static <T extends Value, U> Optional<List<U>> getArrayElements(ArrayValue arrayValue,
Class<T> elementType,
boolean throwOnError,
@@ -135,7 +136,6 @@ public class Values {
values = values.filter(value -> value != null && value.getType().getModelType() == elementType);
}
- //noinspection unchecked
Stream<U> stream = values.map(value -> (T)value).map(converter);
if (!includeNulls) {
@@ -145,6 +145,7 @@ public class Values {
return Optional.of(stream.collect(Collectors.toList()));
}
+ @SuppressWarnings("unchecked")
public static <T extends Value> Optional<T> getMetaItemValueOrThrow(AssetAttribute attribute,
String name,
Class<T> valueClazz,
@@ -177,7 +178,6 @@ public class Values {
}
- //noinspection unchecked
return Optional.of((T)value.get());
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/designer/RulesDesignerTest.groovy b/test/src/test/groovy/org/openremote/test/rules/designer/RulesDesignerTest.groovy
new file mode 100644
index 000000000..444804903
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/rules/designer/RulesDesignerTest.groovy
@@ -0,0 +1,153 @@
+package org.openremote.test.rules.designer
+
+/*
+import org.drools.workbench.models.commons.backend.oracle.PackageDataModelOracleImpl
+import org.drools.workbench.models.commons.backend.rule.ActionCallMethodBuilder
+import org.drools.workbench.models.commons.backend.rule.RuleModelDRLPersistenceImpl
+import org.drools.workbench.models.datamodel.imports.Import
+import org.drools.workbench.models.datamodel.oracle.DataType
+import org.drools.workbench.models.datamodel.rule.*
+import org.hamcrest.Matchers
+import org.openremote.model.asset.AssetEvent
+import org.openremote.model.asset.AssetType
+import org.openremote.model.rules.Assets
+*/
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Specification
+
+import java.util.logging.Logger
+
+
+/*
+TODO Build DRL programmatically
+
+Requires in test/build.xml:
+
+ compile "org.hamcrest:hamcrest-library:1.3"
+
+Requires in manager/server/build.xml
+
+ compile "org.drools:drools-workbench-models-commons:$droolsVersion"
+
+TODO Examples can be found:
+
+https://github.com/kiegroup/drools-wb/tree/master/drools-wb-screens/drools-wb-guided-rule-editor
+https://github.com/kiegroup/drools/tree/master/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule
+https://github.com/kiegroup/drools/blob/master/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceUnmarshallingTest.java
+https://github.com/kiegroup/drools/blob/master/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/BRLRuleModelTest.java
+
+========================================================
+
+package org.openremote.test.rules;
+
+import org.openremote.model.asset.*
+import org.openremote.model.attribute.*
+import org.openremote.model.value.*
+import org.openremote.model.value.Values
+
+global java.util.logging.Logger LOG;
+global org.openremote.model.rules.Assets assets;
+
+rule "Switch room lights off when apartment ALL LIGHTS OFF push-button is pressed"
+when
+ // In an asset of type RESIDENCE the All Lights Off push-button is pressed, which sends a 'true' value
+ $e: AssetEvent(type == AssetType.RESIDENCE, attributeName == "allLightsOffSwitch", valueAsBoolean == true)
+then
+ assets.query() // Execute a query to get "other" assets
+ .parent($e.getId()) // Where parent is the apartment in LHS
+ .type(AssetType.ROOM) // And the children are of type ROOM
+ .applyResults(assetIds -> assetIds.forEach(assetId -> assets.dispatch( // For each asset ID in result...
+ new AttributeEvent(assetId, "lightSwitch", Values.create(false)) // Dispatch an event that turns the switch off
+ )));
+end
+
+========================================================
+ */
+
+class RulesDesignerTest extends Specification implements ManagerContainerTrait {
+/*
+ def "Unmarshall DRL into model"() {
+
+ when: "some DRL is parsed"
+
+ def drl = getClass().getResource("/demo/rules/DemoApartmentAllLightsOff.drl").text
+
+ def globals = ["LOG": Logger.class.getName(), "assets": Assets.class.getName()]
+ def dmo = new PackageDataModelOracleImpl()
+ dmo.addPackageGlobals(globals)
+
+ def model = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo)
+
+ then: "the result should be ok"
+ def rhs0 = model.rhs[0] as ActionCallMethod
+ rhs0.methodName == "query"
+ rhs0.variable == "assets"
+ def drlOut = RuleModelDRLPersistenceImpl.instance.marshal(model)
+ println(drlOut)
+ // Matchers.equalToIgnoringWhiteSpace(drl).matches(drlOut)
+ }
+
+ def "Marshall model into DRL"() {
+
+ when: "a rules model is build"
+
+ def model = new RuleModel()
+ model.packageName = "org.openremote.test.rules"
+ model.imports.addImport(new Import("org.openremote.model.asset.*"))
+ model.imports.addImport(new Import("org.openremote.model.attribute.*"))
+ model.imports.addImport(new Import("org.openremote.model.value.*"))
+ model.imports.addImport(new Import("org.openremote.model.value.Values"))
+
+ def globals = ["LOG": Logger.class.getName(), "assets": Assets.class.getName()]
+ def dmo = new PackageDataModelOracleImpl()
+ dmo.addPackageGlobals(globals)
+ def actionCallMethodBuilder = new ActionCallMethodBuilder(model, dmo, true, new HashMap<String, String>())
+
+ model.name = "Switch room lights off when apartment ALL LIGHTS OFF push-button is pressed"
+
+ def assetEventFactPattern = new FactPattern(AssetEvent.class.getSimpleName())
+ def assetEventFactPatternConstraints = new CompositeFieldConstraint()
+ assetEventFactPatternConstraints.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND)
+ assetEventFactPattern.addConstraint(assetEventFactPatternConstraints)
+ model.addLhsItem(assetEventFactPattern)
+ assetEventFactPattern.setBoundName("\$e")
+
+ def assetTypeConstraint = new SingleFieldConstraint()
+ assetEventFactPatternConstraints.addConstraint(assetTypeConstraint)
+ assetTypeConstraint.setFieldName("type")
+ assetTypeConstraint.setFieldType(DataType.TYPE_COMPARABLE)
+ assetTypeConstraint.setOperator("==")
+ assetTypeConstraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_ENUM)
+ assetTypeConstraint.setValue(AssetType.class.getSimpleName() + "." + AssetType.RESIDENCE.name())
+
+ def attributeNameConstraint = new SingleFieldConstraint()
+ assetEventFactPatternConstraints.addConstraint(attributeNameConstraint)
+ attributeNameConstraint.setFieldName("attributeName")
+ attributeNameConstraint.setFieldType(DataType.TYPE_STRING)
+ attributeNameConstraint.setOperator("==")
+ attributeNameConstraint.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL)
+ attributeNameConstraint.setValue("allLightsOffSwitch")
+ // TODO Find out how to bind safe values
+ // attributeNameConstraint.setValue("allLightsOffSwitch \" && injectionAttack == possible")
+
+ def attributeValueConstraint = new SingleFieldConstraint()
+ assetEventFactPatternConstraints.addConstraint(attributeValueConstraint)
+ attributeValueConstraint.setFieldName("valueAsBoolean")
+ attributeValueConstraint.setFieldType(DataType.TYPE_BOOLEAN)
+ attributeValueConstraint.setOperator("==")
+ attributeValueConstraint.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL)
+ attributeValueConstraint.setValue(Boolean.TRUE.toString())
+
+ def dispatchAction = actionCallMethodBuilder.get("assets", "dispatch", "foo")
+ model.addRhsItem(new ExpressionGlobalVariable("assets", Assets.class.getName(), Assets.class.getName()))
+ model.addRhsItem(dispatchAction)
+
+ String drl = RuleModelDRLPersistenceImpl.instance.marshal(model)
+ println(drl)
+
+ then: "it should be good"
+ model.lhs.length == 1
+ model.lhs[0] == assetEventFactPattern
+ }
+ */
+}