This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 8763f8876a Rules executor compiles the Drools rules for every row,
fixes #9333 (#9336)
8763f8876a is described below
commit 8763f8876a48cd5be1c54b36bab8cd085c0d4659
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 6 20:37:11 2026 +0100
Rules executor compiles the Drools rules for every row, fixes #9333 (#9336)
---
.../transforms/drools/RulesExecutorData.java | 88 +++++++++-----
.../transforms/drools/RulesExecutorDataTest.java | 128 +++++++++++++++++++++
2 files changed, 185 insertions(+), 31 deletions(-)
diff --git
a/plugins/transforms/drools/src/main/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorData.java
b/plugins/transforms/drools/src/main/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorData.java
index 375aa5fb6b..8587a4dff1 100644
---
a/plugins/transforms/drools/src/main/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorData.java
+++
b/plugins/transforms/drools/src/main/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorData.java
@@ -22,12 +22,17 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.UUID;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.IValueMeta;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.pipeline.transform.BaseTransformData;
import org.apache.hop.pipeline.transform.ITransformData;
+import org.kie.api.KieBase;
+import org.kie.api.KieServices;
+import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.Message;
+import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.Results;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
@@ -40,7 +45,7 @@ public class RulesExecutorData extends BaseTransformData
implements ITransformDa
private IRowMeta outputRowMeta;
- private KieHelper kieHelper;
+ private KieBase kieBase;
private Rules.Column[] columnList;
@@ -74,6 +79,12 @@ public class RulesExecutorData extends BaseTransformData
implements ITransformDa
return outputRowMeta;
}
+ /**
+ * Compiles the rules once. Every row then runs in a new session on the
compiled rule base, so the
+ * DRL is not parsed and compiled again for each row.
+ *
+ * @throws RuleValidationException when the rules do not compile
+ */
public void initializeRules() throws RuleValidationException {
// To ensure the plugin classloader use for dependency resolution
@@ -81,24 +92,37 @@ public class RulesExecutorData extends BaseTransformData
implements ITransformDa
ClassLoader loader = getClass().getClassLoader();
Thread.currentThread().setContextClassLoader(loader);
- Resource ruleSet = null;
+ try {
+ Resource ruleSet;
+ if (ruleString != null) {
+ ruleSet = ResourceFactory.newReaderResource(new
StringReader(ruleString));
+ } else {
+ ruleSet = ResourceFactory.newFileResource(ruleFilePath);
+ }
- if (ruleString != null) {
- ruleSet = ResourceFactory.newReaderResource(new
StringReader(ruleString));
- } else {
- ruleSet = ResourceFactory.newFileResource(ruleFilePath);
- }
+ // A release id of its own: the default one is shared by every rule set
built in this JVM,
+ // so another transform building its rules at the same time could
replace ours.
+ KieServices kieServices = KieServices.Factory.get();
+ ReleaseId releaseId =
+ kieServices.newReleaseId("org.apache.hop", "rules-executor-" +
UUID.randomUUID(), "1.0");
- kieHelper = new KieHelper();
- kieHelper.addResource(ruleSet, ResourceType.DRL);
+ KieHelper kieHelper = new
KieHelper().setClassLoader(loader).setReleaseId(releaseId);
+ kieHelper.addResource(ruleSet, ResourceType.DRL);
- Results results1 = kieHelper.verify();
- if (results1.hasMessages(Message.Level.ERROR)) {
- throw new RuleValidationException(results1.getMessages());
- }
+ KieBuilder kieBuilder = kieServices.newKieBuilder(kieHelper.kfs,
loader).buildAll();
+ Results results = kieBuilder.getResults();
+ if (results.hasMessages(Message.Level.ERROR)) {
+ throw new RuleValidationException(results.getMessages());
+ }
+
+ kieBase = kieServices.newKieContainer(releaseId, loader).getKieBase();
- // reset classloader back to original
- Thread.currentThread().setContextClassLoader(orig);
+ // The rule base is built: the module is not needed in the shared
repository any more.
+ kieServices.getRepository().removeKieModule(releaseId);
+ } finally {
+ // reset classloader back to original
+ Thread.currentThread().setContextClassLoader(orig);
+ }
}
public void initializeColumns(IRowMeta inputRowMeta) {
@@ -138,24 +162,26 @@ public class RulesExecutorData extends BaseTransformData
implements ITransformDa
ClassLoader loader = getClass().getClassLoader();
Thread.currentThread().setContextClassLoader(loader);
- if (kieHelper != null) {
- KieSession session = kieHelper.getKieContainer().newKieSession();
-
- for (Rules.Column column : columnList) {
- session.insert(column);
+ try {
+ if (kieBase != null) {
+ // A new session per row, so facts of one row never reach the next.
+ // KieSession.close() delegates to dispose()
+ try (KieSession session = kieBase.newKieSession()) {
+ for (Rules.Column column : columnList) {
+ session.insert(column);
+ }
+
+ session.fireAllRules();
+
+ Collection<Object> oList = fetchColumns(session);
+ for (Object o : oList) {
+ resultMap.put(((Rules.Column) o).getName(), (Rules.Column) o);
+ }
+ }
}
-
- session.fireAllRules();
-
- Collection<Object> oList = fetchColumns(session);
- for (Object o : oList) {
- resultMap.put(((Rules.Column) o).getName(), (Rules.Column) o);
- }
-
- session.dispose();
+ } finally {
+ Thread.currentThread().setContextClassLoader(orig);
}
-
- Thread.currentThread().setContextClassLoader(orig);
}
protected Collection<Object> fetchColumns(KieSession session) {
diff --git
a/plugins/transforms/drools/src/test/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorDataTest.java
b/plugins/transforms/drools/src/test/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorDataTest.java
new file mode 100644
index 0000000000..44d1b43cc3
--- /dev/null
+++
b/plugins/transforms/drools/src/test/java/org/apache/hop/pipeline/transforms/drools/RulesExecutorDataTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.hop.pipeline.transforms.drools;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaNumber;
+import org.junit.jupiter.api.Test;
+
+class RulesExecutorDataTest {
+
+ private static final String PRICE_CLASS_DRL =
+ """
+ package test;
+ import org.apache.hop.pipeline.transforms.drools.Rules.Column;
+
+ rule "Premium product"
+ when
+ Column(name == "list_price", ((Number) payload).doubleValue() >= 100)
+ then
+ insert(new Column("price_class", "String", "premium"));
+ end
+
+ rule "Standard product"
+ when
+ Column(name == "list_price", ((Number) payload).doubleValue() < 100)
+ then
+ insert(new Column("price_class", "String", "standard"));
+ end
+
+ rule "Input columns in this session"
+ when
+ Number($count : intValue) from accumulate(Column(externalSource ==
true), count(1))
+ then
+ insert(new Column("input_columns", "Integer", $count));
+ end
+ """;
+
+ private static final String CONSTANT_DRL =
+ """
+ package other;
+ import org.apache.hop.pipeline.transforms.drools.Rules.Column;
+
+ rule "Always"
+ when
+ Column(name == "list_price")
+ then
+ insert(new Column("price_class", "String", "other rules"));
+ end
+ """;
+
+ private static RulesExecutorData newData(String drl) throws
RuleValidationException {
+ IRowMeta rowMeta = new RowMeta();
+ rowMeta.addValueMeta(new ValueMetaNumber("list_price"));
+
+ RulesExecutorData data = new RulesExecutorData();
+ data.setRuleString(drl);
+ data.initializeRules();
+ data.initializeColumns(rowMeta);
+ return data;
+ }
+
+ private static Object run(RulesExecutorData data, double listPrice, String
column) {
+ data.loadRow(new Object[] {listPrice});
+ data.execute();
+ Rules.Column result = (Rules.Column) data.fetchResult(column);
+ return result == null ? null : result.getPayload();
+ }
+
+ @Test
+ void rulesAreAppliedToEveryRow() throws Exception {
+ RulesExecutorData data = newData(PRICE_CLASS_DRL);
+
+ assertEquals("premium", run(data, 150.0, "price_class"));
+ assertEquals("standard", run(data, 50.0, "price_class"));
+ assertEquals("premium", run(data, 100.0, "price_class"));
+ assertNull(run(data, 100.0, "no_such_column"));
+ }
+
+ @Test
+ void everyRowGetsAFreshSession() throws Exception {
+ RulesExecutorData data = newData(PRICE_CLASS_DRL);
+
+ // With one session over several rows, the input columns of earlier rows
would be counted too.
+ for (int row = 0; row < 5; row++) {
+ assertEquals(1, ((Number) run(data, row * 50.0,
"input_columns")).intValue());
+ }
+ }
+
+ @Test
+ void transformsKeepTheirOwnRules() throws Exception {
+ RulesExecutorData first = newData(PRICE_CLASS_DRL);
+ RulesExecutorData second = newData(CONSTANT_DRL);
+
+ assertEquals("premium", run(first, 150.0, "price_class"));
+ assertEquals("other rules", run(second, 150.0, "price_class"));
+ assertEquals("standard", run(first, 50.0, "price_class"));
+ }
+
+ @Test
+ void invalidRulesFailAtInit() {
+ RulesExecutorData data = new RulesExecutorData();
+ data.setRuleString("package broken;\nrule \"No end\"\nwhen\n
eval(true)\nthen\n");
+
+ RuleValidationException e = assertThrows(RuleValidationException.class,
data::initializeRules);
+ assertFalse(e.getMessages().isEmpty());
+ }
+}