You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by bo...@apache.org on 2023/04/25 15:29:11 UTC

[streampipes] branch dev updated: [FEATURE] add division as operation for data harmonization rules (#1517)

This is an automated email from the ASF dual-hosted git repository.

bossenti pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new f2a00425d [FEATURE] add division as operation for data harmonization rules (#1517)
f2a00425d is described below

commit f2a00425dd0f47cb5f9f46fec6ebeb3fd5a64792
Author: Tim <50...@users.noreply.github.com>
AuthorDate: Tue Apr 25 17:29:05 2023 +0200

    [FEATURE] add division as operation for data harmonization rules (#1517)
---
 .../value/CorrectionValueTransformationRule.java   |  28 ++--
 .../transform/value/CorrectionValueTest.java       | 158 +++++++++++++++++++++
 ui/README.md                                       |   2 +-
 .../edit-correction-value.component.ts             |   3 +-
 .../edit-event-property.component.ts               |   2 +-
 5 files changed, 174 insertions(+), 19 deletions(-)

diff --git a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/adapter/preprocessing/transform/value/CorrectionValueTransformationRule.java b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/adapter/preprocessing/transform/value/CorrectionValueTransformationRule.java
index 4b1f6f838..e720e4b27 100644
--- a/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/adapter/preprocessing/transform/value/CorrectionValueTransformationRule.java
+++ b/streampipes-extensions-management/src/main/java/org/apache/streampipes/extensions/management/connect/adapter/preprocessing/transform/value/CorrectionValueTransformationRule.java
@@ -49,26 +49,22 @@ public class CorrectionValueTransformationRule implements ValueTransformationRul
     if (eventKey.size() == 1) {
       try {
         Object obj = event.get(eventKey.get(0));
-        double old = 0d;
+        double old;
         if (obj instanceof Number) {
           old = ((Number) obj).doubleValue();
+        } else {
+          throw new RuntimeException(
+              String.format("Selected property `%s` does not contain a numeric value: `%s", eventKey.get(0), obj)
+          );
         }
 
-        double corrected = 0d;
-        switch (operator) {
-          case "MULTIPLY":
-            corrected = old * correctionValue;
-            break;
-          case "ADD":
-            corrected = old + correctionValue;
-            break;
-          case "SUBSTRACT":
-            corrected = old - correctionValue;
-            break;
-          default:
-            corrected = old;
-            break;
-        }
+        double corrected = switch (operator) {
+          case "MULTIPLY" -> old * correctionValue;
+          case "ADD" -> old + correctionValue;
+          case "SUBTRACT" -> old - correctionValue;
+          case "DIVIDE" -> old / correctionValue;
+          default -> old;
+        };
 
         event.put(eventKey.get(0), corrected);
       } catch (ClassCastException e) {
diff --git a/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/adapter/transform/value/CorrectionValueTest.java b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/adapter/transform/value/CorrectionValueTest.java
new file mode 100644
index 000000000..5bc26c26d
--- /dev/null
+++ b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/connect/adapter/transform/value/CorrectionValueTest.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.extensions.management.connect.adapter.transform.value;
+
+import org.apache.streampipes.extensions.management.connect.adapter.preprocessing.transform.value.CorrectionValueTransformationRule;
+import org.apache.streampipes.model.schema.EventProperty;
+import org.apache.streampipes.model.schema.EventPropertyPrimitive;
+import org.apache.streampipes.model.schema.EventSchema;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+public class CorrectionValueTest {
+
+  private Map<String, Object> event;
+
+  private final String propertyNameBasicValue = "basicValue";
+  private final String propertyNameOtherValue = "otherValue";
+
+  @Before
+  public void setUp() {
+
+    EventSchema eventSchema = new EventSchema();
+    EventProperty eventProperty = new EventPropertyPrimitive();
+    eventProperty.setLabel(propertyNameBasicValue);
+    eventProperty.setRuntimeName(propertyNameBasicValue);
+    eventSchema.addEventProperty(eventProperty);
+
+    EventProperty eventPropertyOther = new EventPropertyPrimitive();
+    eventPropertyOther.setLabel(propertyNameBasicValue);
+    eventPropertyOther.setRuntimeName(propertyNameBasicValue);
+    eventSchema.addEventProperty(eventPropertyOther);
+
+    event = new HashMap<>();
+    event.put(propertyNameBasicValue, 100.0);
+    event.put(propertyNameOtherValue, "Hello");
+  }
+
+  @Test
+  public void testAdd() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+         List.of(propertyNameBasicValue),
+         10.0,
+         "ADD"
+     );
+
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(110.0, resultEvent.get(propertyNameBasicValue));
+  }
+
+  @Test
+  public void testSubtract() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameBasicValue),
+        10.0,
+        "SUBTRACT"
+    );
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(90.0, resultEvent.get(propertyNameBasicValue));
+  }
+
+  @Test
+  public void testMultiply() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameBasicValue),
+        1.5,
+        "MULTIPLY"
+    );
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(150.0, resultEvent.get(propertyNameBasicValue));
+  }
+
+  @Test
+  public void testDivide() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameBasicValue),
+        5,
+        "DIVIDE"
+    );
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(20.0, resultEvent.get(propertyNameBasicValue));
+  }
+
+  @Test
+  public void testDivideByZero() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameBasicValue),
+        0.0,
+        "DIVIDE"
+    );
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(Double.POSITIVE_INFINITY, resultEvent.get(propertyNameBasicValue));
+  }
+
+  @Test
+  public void testNonNumericValue() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameOtherValue),
+        10.0,
+        "ADD"
+    );
+    assertThrows (
+        String.format("Selected property `%s` does not contain a numeric value: `%s", propertyNameOtherValue, "Hello"),
+        RuntimeException.class,
+        () -> correctionRule.transform(event).get(propertyNameOtherValue)
+    );
+
+
+  }
+
+  @Test
+  public void testUnsupportedOperation() {
+
+    var correctionRule = new CorrectionValueTransformationRule(
+        List.of(propertyNameBasicValue),
+        10.0,
+        "TEST"
+    );
+    var resultEvent = correctionRule.transform(event);
+    assertNotNull(resultEvent);
+    assertEquals(100.0, resultEvent.get(propertyNameBasicValue));
+  }
+}
diff --git a/ui/README.md b/ui/README.md
index 91c19f6eb..3f3ce4304 100644
--- a/ui/README.md
+++ b/ui/README.md
@@ -43,4 +43,4 @@ To test your local version of the UI execute the following commands:
 Running `npm install` will cause a pre-commit hook to be created.
 This hook ensures that if you want to commit changes to this repository, these changes are compliant with our [formatting](https://prettier.io/) and [linting](https://eslint.org/) rules.
 
-If you see errors there, you can try to run `npm run format:fix` and `npm run lint:fix` to fix these issues automatically. Otherwise you have to run `npm run format` and `npm run lint` to get additional information about the problems.
+If you see errors there, you can try to run `npm run format:fix` and `npm run lint:fix` to fix these issues automatically. Otherwise, you have to run `npm run format` and `npm run lint` to get additional information about the problems.
diff --git a/ui/src/app/connect/dialog/edit-event-property/components/edit-value-transformation/edit-correction-value/edit-correction-value.component.ts b/ui/src/app/connect/dialog/edit-event-property/components/edit-value-transformation/edit-correction-value/edit-correction-value.component.ts
index 90e17883c..87b31c9fd 100644
--- a/ui/src/app/connect/dialog/edit-event-property/components/edit-value-transformation/edit-correction-value/edit-correction-value.component.ts
+++ b/ui/src/app/connect/dialog/edit-event-property/components/edit-value-transformation/edit-correction-value/edit-correction-value.component.ts
@@ -30,7 +30,8 @@ export class EditCorrectionValueComponent {
     operators = [
         { value: 'MULTIPLY', viewValue: 'Multiply' },
         { value: 'ADD', viewValue: 'Add' },
-        { value: 'SUBSTRACT', viewValue: 'Substract' },
+        { value: 'SUBTRACT', viewValue: 'Subtract' },
+        { value: 'DIVIDE', viewValue: 'Divide' },
     ];
 
     constructor() {}
diff --git a/ui/src/app/connect/dialog/edit-event-property/edit-event-property.component.ts b/ui/src/app/connect/dialog/edit-event-property/edit-event-property.component.ts
index df8839db8..b2c98542c 100644
--- a/ui/src/app/connect/dialog/edit-event-property/edit-event-property.component.ts
+++ b/ui/src/app/connect/dialog/edit-event-property/edit-event-property.component.ts
@@ -159,7 +159,7 @@ export class EditEventPropertyComponent implements OnInit {
         this.property.label = this.cachedProperty.label;
         this.property.description = this.cachedProperty.description;
 
-        // remove undefined from domain properies array
+        // remove undefined from domain properties array
         this.property.domainProperties =
             this.cachedProperty.domainProperties.filter(n => n);
         this.property.runtimeName = this.cachedProperty.runtimeName;