This is an automated email from the ASF dual-hosted git repository.
zehnder pushed a commit to branch dev
in repository
https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git
The following commit(s) were added to refs/heads/dev by this push:
new 50581a8 [STREAMPIPES-308] Add String mappings to BooleanToState
Processor
new 64a331b Merge branch 'dev' of
github.com:apache/incubator-streampipes-extensions into dev
50581a8 is described below
commit 50581a8e9bf0eef08945642fbcc8d859e4a2c555
Author: Philipp Zehnder <[email protected]>
AuthorDate: Thu Mar 4 21:21:37 2021 +0100
[STREAMPIPES-308] Add String mappings to BooleanToState Processor
---
.../booloperator/state/BooleanToState.java | 16 +++++++----
.../state/BooleanToStateController.java | 31 +++++++++++++++++++---
.../state/BooleanToStateParameters.java | 13 ++++++++-
.../documentation.md | 9 +++++--
.../strings.en | 5 +++-
5 files changed, 62 insertions(+), 12 deletions(-)
diff --git
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToState.java
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToState.java
index a714e15..5b46324 100644
---
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToState.java
+++
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToState.java
@@ -25,7 +25,9 @@ import
org.apache.streampipes.wrapper.routing.SpOutputCollector;
import org.apache.streampipes.wrapper.runtime.EventProcessor;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
public class BooleanToState implements
EventProcessor<BooleanToStateParameters> {
@@ -33,6 +35,7 @@ public class BooleanToState implements
EventProcessor<BooleanToStateParameters>
private List<String> stateFields;
private String defaultState;
+ private Map<String, String> jsonConfiguration;
@Override
public void onInvocation(BooleanToStateParameters booleanInverterParameters,
@@ -41,22 +44,25 @@ public class BooleanToState implements
EventProcessor<BooleanToStateParameters>
LOG = booleanInverterParameters.getGraph().getLogger(BooleanToState.class);
this.stateFields = booleanInverterParameters.getStateFields();
this.defaultState = booleanInverterParameters.getDefaultState();
+ this.jsonConfiguration = booleanInverterParameters.getJsonConfiguration();
}
@Override
public void onEvent(Event inputEvent, SpOutputCollector out) {
- List<String> states = new ArrayList<>();
+ String state = this.defaultState;
for (String stateField : stateFields) {
if
(inputEvent.getFieldBySelector(stateField).getAsPrimitive().getAsBoolean().equals(true))
{
- states.add(inputEvent.getFieldBySelector(stateField).getFieldNameIn());
+ state = inputEvent.getFieldBySelector(stateField).getFieldNameIn();
}
}
- if (states.size() == 0) {
- states.add(this.defaultState);
+ // replace the state string when user provided a mapping
+ if (this.jsonConfiguration.containsKey(state)) {
+ state = this.jsonConfiguration.get(state);
}
- inputEvent.addField(BooleanToStateController.CURRENT_STATE,
states.toArray());
+
+ inputEvent.addField(BooleanToStateController.CURRENT_STATE, state);
out.collect(inputEvent);
}
diff --git
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateController.java
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateController.java
index c715b50..cce472f 100644
---
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateController.java
+++
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateController.java
@@ -18,6 +18,8 @@
package
org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.apache.streampipes.commons.exceptions.SpRuntimeException;
import org.apache.streampipes.model.graph.DataProcessorDescription;
import org.apache.streampipes.model.graph.DataProcessorInvocation;
import org.apache.streampipes.model.schema.PropertyScope;
@@ -28,19 +30,32 @@ 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.serializers.json.JacksonSerializer;
import org.apache.streampipes.vocabulary.SPSensor;
import org.apache.streampipes.wrapper.standalone.ConfiguredEventProcessor;
import
org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventProcessingDeclarer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.List;
+import java.util.Map;
public class BooleanToStateController extends
StandaloneEventProcessingDeclarer<BooleanToStateParameters> {
+ private static final Logger LOG =
LoggerFactory.getLogger(BooleanToStateController.class);
+
public static final String BOOLEAN_STATE_FIELD = "boolean_state_field";
public static final String DEFAULT_STATE_ID = "default-state-id";
+ public static final String JSON_CONFIGURATION = "json-configuration";
public static final String CURRENT_STATE = "current_state";
+ private static final String defaultSkeleton = "// Add the configuration for
the string mappings here:\n" +
+ "{\n" +
+ " \"exampleRuntimeName1\": \"newValue1\",\n" +
+ " \"exampleRuntimeName2\": \"newValue2\"\n" +
+ "}";
+
@Override
public DataProcessorDescription declareModel() {
return
ProcessingElementBuilder.create("org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state")
@@ -50,8 +65,9 @@ public class BooleanToStateController extends
StandaloneEventProcessingDeclarer<
.requiredPropertyWithNaryMapping(EpRequirements.booleanReq(),
Labels.withId(BOOLEAN_STATE_FIELD), PropertyScope.NONE)
.build())
.requiredTextParameter(Labels.withId(DEFAULT_STATE_ID))
+ .requiredCodeblock(Labels.withId(JSON_CONFIGURATION),
CodeLanguage.Javascript, defaultSkeleton)
.outputStrategy(OutputStrategies.append(
- EpProperties.listStringEp(Labels.withId(CURRENT_STATE),
CURRENT_STATE, SPSensor.STATE)
+ EpProperties.stringEp(Labels.withId(CURRENT_STATE),
CURRENT_STATE, SPSensor.STATE)
))
.build();
}
@@ -61,9 +77,18 @@ public class BooleanToStateController extends
StandaloneEventProcessingDeclarer<
List<String> stateFields =
extractor.mappingPropertyValues(BOOLEAN_STATE_FIELD);
String defaultState = extractor.singleValueParameter(DEFAULT_STATE_ID,
String.class);
+ String jsonConfigurationString =
extractor.codeblockValue(JSON_CONFIGURATION);
- BooleanToStateParameters params = new BooleanToStateParameters(graph,
stateFields, defaultState);
+ try {
+ jsonConfigurationString =
jsonConfigurationString.replaceAll("(?m)^//.*", "");
+ Map<String, String> jsonConfiguration =
JacksonSerializer.getObjectMapper().readValue(jsonConfigurationString,
Map.class);
+ BooleanToStateParameters params = new BooleanToStateParameters(graph,
stateFields, defaultState, jsonConfiguration);
- return new ConfiguredEventProcessor<>(params, BooleanToState::new);
+ return new ConfiguredEventProcessor<>(params, BooleanToState::new);
+ } catch (JsonProcessingException e) {
+ LOG.info("Error when parsing the json configuration: " +
jsonConfigurationString);
+ throw new SpRuntimeException("The following mapping configuration is not
valid: " + jsonConfigurationString);
+ }
}
+
}
diff --git
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateParameters.java
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateParameters.java
index 4ff3a0a..4269d49 100644
---
a/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateParameters.java
+++
b/streampipes-processors-transformation-jvm/src/main/java/org/apache/streampipes/processors/transformation/jvm/processor/booloperator/state/BooleanToStateParameters.java
@@ -22,15 +22,18 @@ import
org.apache.streampipes.model.graph.DataProcessorInvocation;
import
org.apache.streampipes.wrapper.params.binding.EventProcessorBindingParams;
import java.util.List;
+import java.util.Map;
public class BooleanToStateParameters extends EventProcessorBindingParams {
private List<String> stateFields;
private String defaultState;
+ private Map<String, String> jsonConfiguration;
- public BooleanToStateParameters(DataProcessorInvocation graph,
List<String> stateFields, String defaultState) {
+ public BooleanToStateParameters(DataProcessorInvocation graph,
List<String> stateFields, String defaultState, Map<String, String>
jsonConfiguration) {
super(graph);
this.stateFields = stateFields;
this.defaultState = defaultState;
+ this.jsonConfiguration = jsonConfiguration;
}
public List<String> getStateFields() {
@@ -48,4 +51,12 @@ public class BooleanToStateParameters extends
EventProcessorBindingParams {
public void setDefaultState(String defaultState) {
this.defaultState = defaultState;
}
+
+ public Map<String, String> getJsonConfiguration() {
+ return jsonConfiguration;
+ }
+
+ public void setJsonConfiguration(Map<String, String> jsonConfiguration) {
+ this.jsonConfiguration = jsonConfiguration;
+ }
}
diff --git
a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/documentation.md
b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/documentation.md
index f08fca7..d8499c4 100644
---
a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/documentation.md
+++
b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/documentation.md
@@ -26,9 +26,9 @@
## Description
-Convert boolean fields to a state string representing the current state of the
system.
+Converts boolean fields to a state string representing the current state of
the system.
This processor requires one or multiple boolean values in the data stream.
-For each of the selected values which are true, the runtime name is added to
the states field.
+For the selected value which is true, the runtime name is added as the state
field.
***
## Required input
@@ -38,6 +38,11 @@ Boolean fields that are converted to the state when true
### Default State
When all boolean values are false, a default state can be defined
+
+### Mapping Configuration
+Configuration to provide a string mapping for each possible value.
+On the left ist the value of the runtime name and on the right the new value
(e.g. {"runtimeName": "newValue"}).
+
***
## Configuration
diff --git
a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/strings.en
b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/strings.en
index a6b4cd3..3c24dd8 100644
---
a/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/strings.en
+++
b/streampipes-processors-transformation-jvm/src/main/resources/org.apache.streampipes.processors.transformation.jvm.processor.booloperator.state/strings.en
@@ -11,4 +11,7 @@ default-state-id.title=Default State
default-state-id.description=Define a default state, when all values are false
boolean_state_field.title=Current State
-boolean_state_field.description=When value is true runtime name is added as
state to resulting event
+boolean_state_field.description=When the boolean value is true the runtime
name is used for the final state value
+
+json-configuration.title=Mapping Configuration
+json-configuration.description=Define mappings to replace the runtime names
with own Strings