-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(#3208): Add math expression evaluator, UI fixes * Fix dependency convergence * Add test, improve docs
- Loading branch information
1 parent
755ca49
commit 970ccf4
Showing
23 changed files
with
658 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...apache/streampipes/processors/enricher/jvm/processor/expression/JexlContextGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.processors.enricher.jvm.processor.expression; | ||
|
||
import org.apache.streampipes.model.runtime.Event; | ||
|
||
import org.apache.commons.jexl3.MapContext; | ||
|
||
public class JexlContextGenerator { | ||
|
||
private final MathExpressionFieldExtractor extractor; | ||
private final MapContext mapContext; | ||
|
||
public JexlContextGenerator(MathExpressionFieldExtractor extractor) { | ||
this.extractor = extractor; | ||
this.mapContext = makeInitialContext(); | ||
} | ||
|
||
private MapContext makeInitialContext() { | ||
var ctx = new MapContext(); | ||
ctx.set("Math", Math.class); | ||
extractor.getInputProperties().forEach(ep -> | ||
ctx.set(ep.getRuntimeName(), 0) | ||
); | ||
return ctx; | ||
} | ||
|
||
public MapContext makeContext(Event event) { | ||
event.getRaw().forEach(mapContext::set); | ||
return mapContext; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../org/apache/streampipes/processors/enricher/jvm/processor/expression/JexlDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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.processors.enricher.jvm.processor.expression; | ||
|
||
import org.apache.streampipes.model.schema.EventPropertyPrimitive; | ||
|
||
public record JexlDescription(EventPropertyPrimitive ep, | ||
String script) { | ||
|
||
public String getFieldName() { | ||
return ep.getRuntimeName(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...g/apache/streampipes/processors/enricher/jvm/processor/expression/JexlEngineProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.processors.enricher.jvm.processor.expression; | ||
|
||
import org.apache.commons.jexl3.JexlBuilder; | ||
import org.apache.commons.jexl3.JexlEngine; | ||
import org.apache.commons.jexl3.JexlFeatures; | ||
import org.apache.commons.jexl3.introspection.JexlPermissions; | ||
|
||
public class JexlEngineProvider { | ||
|
||
public JexlEngine getEngine() { | ||
var features = new JexlFeatures() | ||
.loops(false) | ||
.sideEffect(false) | ||
.sideEffectGlobal(false); | ||
|
||
var permissions = new JexlPermissions.ClassPermissions(java.lang.Math.class); | ||
|
||
return new JexlBuilder().features(features).permissions(permissions).create(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...va/org/apache/streampipes/processors/enricher/jvm/processor/expression/JexlEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.processors.enricher.jvm.processor.expression; | ||
|
||
import org.apache.streampipes.model.runtime.Event; | ||
|
||
import org.apache.commons.jexl3.JexlEngine; | ||
import org.apache.commons.jexl3.JexlScript; | ||
import org.apache.commons.jexl3.MapContext; | ||
|
||
public class JexlEvaluator { | ||
|
||
private final JexlDescription jexlDescription; | ||
private final JexlScript script; | ||
|
||
public JexlEvaluator(JexlDescription jexlDescription, | ||
JexlEngine engine) { | ||
this.jexlDescription = jexlDescription; | ||
this.script = engine.createScript(jexlDescription.script()); | ||
} | ||
|
||
public void evaluate(MapContext context, | ||
Event event) { | ||
event.addField(jexlDescription.getFieldName(), script.execute(context)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...treampipes/processors/enricher/jvm/processor/expression/MathExpressionFieldExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.processors.enricher.jvm.processor.expression; | ||
|
||
import org.apache.streampipes.extensions.api.extractor.IDataProcessorParameterExtractor; | ||
import org.apache.streampipes.model.graph.DataProcessorInvocation; | ||
import org.apache.streampipes.model.schema.EventProperty; | ||
import org.apache.streampipes.model.staticproperty.CodeInputStaticProperty; | ||
import org.apache.streampipes.model.staticproperty.FreeTextStaticProperty; | ||
import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor; | ||
import org.apache.streampipes.sdk.helpers.EpProperties; | ||
import org.apache.streampipes.sdk.helpers.Labels; | ||
import org.apache.streampipes.vocabulary.SO; | ||
|
||
import java.util.List; | ||
|
||
import static org.apache.streampipes.processors.enricher.jvm.processor.expression.MathExpressionProcessor.ENRICHED_FIELDS; | ||
import static org.apache.streampipes.processors.enricher.jvm.processor.expression.MathExpressionProcessor.EXPRESSION; | ||
import static org.apache.streampipes.processors.enricher.jvm.processor.expression.MathExpressionProcessor.FIELD_NAME; | ||
|
||
public class MathExpressionFieldExtractor { | ||
|
||
private final DataProcessorInvocation processingElement; | ||
private final IDataProcessorParameterExtractor extractor; | ||
|
||
public MathExpressionFieldExtractor(DataProcessorInvocation processingElement) { | ||
this.processingElement = processingElement; | ||
this.extractor = ProcessingElementParameterExtractor.from(processingElement); | ||
} | ||
|
||
public MathExpressionFieldExtractor(DataProcessorInvocation processingElement, | ||
IDataProcessorParameterExtractor extractor) { | ||
this.processingElement = processingElement; | ||
this.extractor = extractor; | ||
} | ||
|
||
public List<JexlDescription> getAdditionalFields() { | ||
return extractor | ||
.collectionMembersAsGroup(ENRICHED_FIELDS) | ||
.stream().map(group -> { | ||
var runtimeName = extractor.extractGroupMember(FIELD_NAME, group).as(FreeTextStaticProperty.class).getValue(); | ||
var expression = extractor.extractGroupMember(EXPRESSION, group).as(CodeInputStaticProperty.class).getValue(); | ||
return new JexlDescription(EpProperties.doubleEp(Labels.empty(), runtimeName, SO.NUMBER), expression); | ||
}).toList(); | ||
} | ||
|
||
public List<EventProperty> getInputProperties() { | ||
var inputStreams = processingElement.getInputStreams(); | ||
if (!inputStreams.isEmpty()) { | ||
return inputStreams.get(0).getEventSchema().getEventProperties(); | ||
} else { | ||
return List.of(); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.