This is an automated email from the ASF dual-hosted git repository. bossenti pushed a commit to branch STREAMPIPES-320 in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git
commit c31ab8758c3f2bc71ffa289ad5df6f57be56ae1a Author: bossenti <[email protected]> AuthorDate: Mon Mar 29 19:32:09 2021 +0200 [STREANPIPES-334] add UI option for label name --- .../jvm/processor/state/labeler/LabelerUtils.java | 33 ++++++++++++++++++++++ .../state/labeler/model/StatementUtils.java | 12 ++++++-- .../state/labeler/number/NumberLabeler.java | 5 +++- .../labeler/number/NumberLabelerController.java | 30 ++++++++++++++------ .../labeler/number/NumberLabelerParameters.java | 8 +++++- .../strings.en | 3 ++ 6 files changed, 78 insertions(+), 13 deletions(-) diff --git a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/LabelerUtils.java b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/LabelerUtils.java new file mode 100644 index 0000000..31db2f2 --- /dev/null +++ b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/LabelerUtils.java @@ -0,0 +1,33 @@ +package org.apache.streampipes.processors.transformation.jvm.processor.state.labeler; + +import org.apache.streampipes.commons.exceptions.SpRuntimeException; +import org.apache.streampipes.model.graph.DataProcessorInvocation; +import org.apache.streampipes.model.schema.EventProperty; +import org.apache.streampipes.model.schema.EventSchema; +import org.apache.streampipes.model.schema.PropertyScope; +import org.apache.streampipes.sdk.builder.PrimitivePropertyBuilder; +import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor; +import org.apache.streampipes.sdk.utils.Datatypes; +import org.apache.streampipes.vocabulary.SPSensor; + +import java.util.List; + +public class LabelerUtils { + + public static EventSchema resolveOutputStrategy(DataProcessorInvocation processingElement, String labelName) throws SpRuntimeException { + + List<EventProperty> properties = processingElement + .getInputStreams() + .get(0) + .getEventSchema() + .getEventProperties(); + + properties.add(PrimitivePropertyBuilder + .create(Datatypes.String, labelName) + .domainProperty(SPSensor.STATE) + .scope(PropertyScope.DIMENSION_PROPERTY) + .build()); + + return new EventSchema(properties); + } +} diff --git a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/model/StatementUtils.java b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/model/StatementUtils.java index 3e01c96..74a0dd3 100644 --- a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/model/StatementUtils.java +++ b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/model/StatementUtils.java @@ -20,8 +20,16 @@ package org.apache.streampipes.processors.transformation.jvm.processor.state.lab import org.apache.streampipes.commons.exceptions.SpRuntimeException; import org.apache.streampipes.logging.api.Logger; +import org.apache.streampipes.model.graph.DataProcessorInvocation; import org.apache.streampipes.model.runtime.Event; +import org.apache.streampipes.model.schema.EventProperty; +import org.apache.streampipes.model.schema.EventSchema; +import org.apache.streampipes.model.schema.PropertyScope; import org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number.NumberLabelerController; +import org.apache.streampipes.sdk.builder.PrimitivePropertyBuilder; +import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor; +import org.apache.streampipes.sdk.utils.Datatypes; +import org.apache.streampipes.vocabulary.SPSensor; import java.util.ArrayList; import java.util.Collections; @@ -38,10 +46,10 @@ public class StatementUtils { * @return * @throws SpRuntimeException */ - public static Event addLabel(Event inputEvent, double value, List<Statement> statements, Logger log) { + public static Event addLabel(Event inputEvent, String labelName, double value, List<Statement> statements, Logger log) { String label = getLabel(value, statements); if (label != null) { - inputEvent.addField(NumberLabelerController.LABEL, label); + inputEvent.addField(labelName, label); } else { log.info("No condition of statements was fulfilled, add a default case (*) to the statements"); } diff --git a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabeler.java b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabeler.java index b8d5dfe..b23e63c 100644 --- a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabeler.java +++ b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabeler.java @@ -35,6 +35,7 @@ public class NumberLabeler implements EventProcessor<NumberLabelerParameters> { private static Logger LOG; private String sensorListValueProperty; + private String labelName; private List<Statement> statements; @Override @@ -49,6 +50,8 @@ public class NumberLabeler implements EventProcessor<NumberLabelerParameters> { numberLabelerParameters.getComparators()); this.sensorListValueProperty = numberLabelerParameters.getSensorListValueProperty(); + + this.labelName = numberLabelerParameters.getLabelName(); } @Override @@ -56,7 +59,7 @@ public class NumberLabeler implements EventProcessor<NumberLabelerParameters> { Double value = inputEvent.getFieldBySelector(this.sensorListValueProperty).getAsPrimitive().getAsDouble(); - Event resultEvent = StatementUtils.addLabel(inputEvent, value, this.statements, LOG); + Event resultEvent = StatementUtils.addLabel(inputEvent, labelName, value, this.statements, LOG); out.collect(resultEvent); } diff --git a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerController.java b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerController.java index 44cf1ff..52a457e 100644 --- a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerController.java +++ b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerController.java @@ -18,18 +18,23 @@ package org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number; +import org.apache.streampipes.commons.exceptions.SpRuntimeException; +import org.apache.streampipes.container.api.ResolvesContainerProvidedOutputStrategy; import org.apache.streampipes.model.graph.DataProcessorDescription; import org.apache.streampipes.model.graph.DataProcessorInvocation; +import org.apache.streampipes.model.schema.EventProperty; +import org.apache.streampipes.model.schema.EventSchema; import org.apache.streampipes.model.schema.PropertyScope; import org.apache.streampipes.model.staticproperty.*; -import org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.model.Statement; -import org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.model.StatementUtils; +import org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.LabelerUtils; import org.apache.streampipes.sdk.StaticProperties; +import org.apache.streampipes.sdk.builder.PrimitivePropertyBuilder; import org.apache.streampipes.sdk.builder.ProcessingElementBuilder; import org.apache.streampipes.sdk.builder.StreamRequirementsBuilder; import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor; import org.apache.streampipes.sdk.helpers.*; import org.apache.streampipes.sdk.utils.Assets; +import org.apache.streampipes.sdk.utils.Datatypes; import org.apache.streampipes.vocabulary.SPSensor; import org.apache.streampipes.wrapper.standalone.ConfiguredEventProcessor; import org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventProcessingDeclarer; @@ -37,15 +42,14 @@ import org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventProcess import java.util.List; import java.util.stream.Collectors; -public class NumberLabelerController extends StandaloneEventProcessingDeclarer<NumberLabelerParameters> { +public class NumberLabelerController extends StandaloneEventProcessingDeclarer<NumberLabelerParameters> implements ResolvesContainerProvidedOutputStrategy<DataProcessorInvocation, ProcessingElementParameterExtractor> { public static final String SENSOR_VALUE_ID = "sensorValueId"; public static final String LABEL_COLLECTION_ID = "labelCollectionId"; public static final String COMPARATOR_ID = "comparatorId"; public static final String NUMBER_VALUE_ID = "numberValueId"; public static final String LABEL_STRING_ID = "labelStringId"; - - public static final String LABEL = "label"; + public static final String LABEL_NAME = "labelName"; @Override public DataProcessorDescription declareModel() { @@ -58,6 +62,7 @@ public class NumberLabelerController extends StandaloneEventProcessingDeclarer<N Labels.withId(SENSOR_VALUE_ID), PropertyScope.NONE) .build()) + .requiredTextParameter(Labels.withId(LABEL_NAME)) .requiredCollection( Labels.withId(LABEL_COLLECTION_ID), StaticProperties.group(Labels.from("group", "Group", ""), false, @@ -68,9 +73,7 @@ public class NumberLabelerController extends StandaloneEventProcessingDeclarer<N ) ) - .outputStrategy(OutputStrategies.append( - EpProperties.stringEp(Labels.withId(LABEL), LABEL, SPSensor.STATE, PropertyScope.DIMENSION_PROPERTY) - )) + .outputStrategy(OutputStrategies.customTransformation()) .build(); } @@ -79,6 +82,8 @@ public class NumberLabelerController extends StandaloneEventProcessingDeclarer<N String sensorListValueProperty = extractor.mappingPropertyValue(SENSOR_VALUE_ID); + String labelName = extractor.textParameter(LABEL_NAME); + List<StaticPropertyGroup> groupItems = extractor.collectionMembersAsGroup(LABEL_COLLECTION_ID); List<Integer> numberValues = groupItems @@ -110,9 +115,16 @@ public class NumberLabelerController extends StandaloneEventProcessingDeclarer<N .collect(Collectors.toList()); - NumberLabelerParameters params = new NumberLabelerParameters(graph, sensorListValueProperty, numberValues, labelStrings, comparators); + NumberLabelerParameters params = new NumberLabelerParameters(graph, sensorListValueProperty, labelName, numberValues, labelStrings, comparators); return new ConfiguredEventProcessor<>(params, NumberLabeler::new); } + + @Override + public EventSchema resolveOutputStrategy(DataProcessorInvocation processingElement, ProcessingElementParameterExtractor parameterExtractor) throws SpRuntimeException { + String labelName = parameterExtractor.textParameter(LABEL_NAME); + + return LabelerUtils.resolveOutputStrategy(processingElement, labelName); + } } diff --git a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerParameters.java b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerParameters.java index ef13001..424f3a7 100644 --- a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerParameters.java +++ b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/state/labeler/number/NumberLabelerParameters.java @@ -26,14 +26,16 @@ import java.util.List; public class NumberLabelerParameters extends EventProcessorBindingParams { private String sensorListValueProperty; + private String labelName; private List<Integer> numberValues; private List<String> labelStrings; private List<String> comparators; - public NumberLabelerParameters(DataProcessorInvocation graph, String sensorListValueProperty, List<Integer> numberValues, List<String> labelStrings, List<String> comparators) { + public NumberLabelerParameters(DataProcessorInvocation graph, String sensorListValueProperty, String labelName, List<Integer> numberValues, List<String> labelStrings, List<String> comparators) { super(graph); this.sensorListValueProperty = sensorListValueProperty; + this.labelName = labelName; this.numberValues = numberValues; this.labelStrings = labelStrings; this.comparators = comparators; @@ -70,4 +72,8 @@ public class NumberLabelerParameters extends EventProcessorBindingParams { public void setComparators(List<String> comparators) { this.comparators = comparators; } + + public String getLabelName() { + return labelName; + } } diff --git a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number/strings.en b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number/strings.en index 94ef97a..d073ffb 100644 --- a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number/strings.en +++ b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.state.labeler.number/strings.en @@ -27,3 +27,6 @@ comparatorId.description=Select a comparator numberValueId.title=Numerical Value numberValueId.description=Specify the numerical value for the condition + +labelName.title=Label Name +labelName.description=Specify the name of the label (how it can be accessed in following components)
