tkobayas commented on code in PR #4597:
URL:
https://github.com/apache/incubator-kie-docs/pull/4597#discussion_r3849823662
##########
drools-docs/src/modules/ROOT/pages/KIE/BuildDeployUtilizeAndRun/_rule-unit-dsl.adoc:
##########
@@ -105,4 +105,99 @@ As you define rules using `defineRules` method, you can
execute the rules, of co
}
----
-You can find various test cases under
https://github.com/apache/incubator-kie-drools/tree/main/drools-ruleunits/drools-ruleunits-dsl
\ No newline at end of file
+You can find various test cases under
https://github.com/apache/incubator-kie-drools/tree/main/drools-ruleunits/drools-ruleunits-dsl
+
+== SyntheticRuleUnitBuilder
+
+While the Rule Unit DSL described above requires you to write a concrete Java
class implementing `RuleUnitDefinition`, `SyntheticRuleUnitBuilder` lets you
construct a rule unit entirely at runtime without writing a dedicated class.
DataSources, globals, and even the unit name are all registered dynamically
through a builder API.
+
+This is useful when the structure of the rule unit itself — not just the
rules, but which DataSources exist and what types they hold — is only known at
runtime. For example, when rules and their data models are defined in YAML, a
database, or a UI.
+
+=== Basic example
+
+The following example builds and executes the same "Hello World" logic shown
earlier, but without defining a `HelloWorldUnit` class:
Review Comment:
fixed
##########
drools-docs/src/modules/ROOT/pages/KIE/BuildDeployUtilizeAndRun/_rule-unit-dsl.adoc:
##########
@@ -105,4 +105,99 @@ As you define rules using `defineRules` method, you can
execute the rules, of co
}
----
-You can find various test cases under
https://github.com/apache/incubator-kie-drools/tree/main/drools-ruleunits/drools-ruleunits-dsl
\ No newline at end of file
+You can find various test cases under
https://github.com/apache/incubator-kie-drools/tree/main/drools-ruleunits/drools-ruleunits-dsl
+
+== SyntheticRuleUnitBuilder
+
+While the Rule Unit DSL described above requires you to write a concrete Java
class implementing `RuleUnitDefinition`, `SyntheticRuleUnitBuilder` lets you
construct a rule unit entirely at runtime without writing a dedicated class.
DataSources, globals, and even the unit name are all registered dynamically
through a builder API.
+
+This is useful when the structure of the rule unit itself — not just the
rules, but which DataSources exist and what types they hold — is only known at
runtime. For example, when rules and their data models are defined in YAML, a
database, or a UI.
+
+=== Basic example
+
+The following example builds and executes the same "Hello World" logic shown
earlier, but without defining a `HelloWorldUnit` class:
+
+[source,java]
+----
+ DataStore<String> strings = DataSource.createStore();
+ DataStore<Integer> ints = DataSource.createStore();
+ List<String> results = new ArrayList<>();
+
+ SyntheticRuleUnit unit = SyntheticRuleUnitBuilder.build("HelloWorld") //
<1>
+ .registerDataSource("strings", strings, String.class) // <2>
+ .registerDataSource("ints", ints, Integer.class)
+ .registerGlobal("results", results) // <3>
+ .defineRules(rulesFactory -> { // <4>
+ // /strings[ this == "Hello World" ]
+ rulesFactory.rule()
+ .on(strings)
+ .filter(EQUAL, "Hello World")
+ .execute(results, r -> r.add("it worked!"));
+
+ // /strings[ length > 5 ]
+ rulesFactory.rule()
+ .on(strings)
+ .filter(s -> s.length(), GREATER_THAN, 5)
+ .execute(results, (r, s) -> r.add("it also worked with
" + s.toUpperCase()));
+ });
+
+ RuleUnitInstance<SyntheticRuleUnit> unitInstance =
RuleUnitProvider.get().createRuleUnitInstance(unit);
+
+ strings.add("Hello World");
+ assertThat(unitInstance.fire()).isEqualTo(2);
+ assertThat(results).containsExactlyInAnyOrder("it worked!", "it also
worked with HELLO WORLD");
+
+ unitInstance.close();
+----
+<1> The unit name is an arbitrary string, not tied to a Java class name.
+<2> DataSources are registered by name and type. You can register `DataStore`,
`DataStream`, or `SingletonStore`.
+<3> Globals are registered by name.
+<4> Rules are defined using the same `RulesFactory` API as the standard Rule
Unit DSL.
+
+=== Accessing DataSources and globals
+
+Because there are no typed getter methods on the class, you access DataSources
and globals by name after the unit is created:
+
+[source,java]
+----
+ // Access DataSources by name and type
+ unit.getDataStore("strings", String.class).add("Hello World");
+ unit.getDataStream("events", Event.class);
+ unit.getSingletonStore("config", Config.class);
+
+ // Access globals by name and type
+ List<String> results = unit.getGlobal("results", List.class);
Review Comment:
fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]