Skip to content

Commit

Permalink
[incubator-kie-drools#5659] Update drools documentation : Bring back …
Browse files Browse the repository at this point in the history
…traditional DRL syntax explanation (#5660)

* [incubator-kie-drools#5659] Update drools documentation : Bring back traditional DRL syntax explanation

* - Remove ruleunit and oopath from traditional syntax chapter
  • Loading branch information
tkobayas authored Jan 24, 2024
1 parent 505f882 commit f9bae76
Show file tree
Hide file tree
Showing 28 changed files with 3,476 additions and 0 deletions.
1 change: 1 addition & 0 deletions drools-docs/src/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* xref:KIE/index.adoc[leveloffset=+1]
* xref:rule-engine/index.adoc[leveloffset=+1]
* xref:language-reference/index.adoc[leveloffset=+1]
* xref:language-reference-traditional/index.adoc[leveloffset=+1]
* xref:DMN/index.adoc[leveloffset=+1]
* xref:pragmatic-ai/index.adoc[leveloffset=+1]
* xref:Commands/index.adoc[leveloffset=+1]
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[id='drl-declarations-access-con_{context}']
= Access to DRL declared types in application code

Declared types in DRL are typically used within the DRL files while Java models are typically used when the model is shared between rules and applications. Because declared types are generated at KIE base compile time, an application cannot access them until application run time. In some cases, an application needs to access and handle facts directly from the declared types, especially when the application wraps the {RULE_ENGINE} and provides higher-level, domain-specific user interfaces for rules management.

To handle declared types directly from the application code, you can use the `org.drools.definition.type.FactType` API in {PRODUCT}. Through this API, you can instantiate, read, and write fields in the declared fact types.

The following example code modifies a `Person` fact type directly from an application:

.Example application code to handle a declared fact type through the FactType API
[source,java]
----
import java.util.Date;
import org.kie.api.definition.type.FactType;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
...
// Get a reference to a KIE base with the declared type:
KieBase kbase = ...
// Get the declared fact type:
FactType personType = kbase.getFactType("org.drools.examples", "Person");
// Create instances:
Object bob = personType.newInstance();
// Set attribute values:
personType.set(bob, "name", "Bob" );
personType.set(bob, "dateOfBirth", new Date());
personType.set(bob, "address", new Address("King's Road","London","404"));
// Insert the fact into a KIE session:
KieSession ksession = ...
ksession.insert(bob);
ksession.fireAllRules();
// Read attributes:
String name = (String) personType.get(bob, "name");
Date date = (Date) personType.get(bob, "dateOfBirth");
----

The API also includes other helpful methods, such as setting all the attributes at once, reading values from a `Map` collection, or reading all attributes at once into a `Map` collection.

Although the API behavior is similar to Java reflection, the API does not use reflection and relies on more performant accessors that are implemented with generated bytecode.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[id='drl-declarations-con_{context}']
= Type declarations and metadata in DRL

ifdef::DROOLS,JBPM,OP[]
.Type declaration
image::language-reference/type_declaration.png[align="center"]

.Metadata
image::language-reference/meta_data.png[align="center"]
endif::[]

Declarations in DRL files define new fact types or metadata for fact types to be used by rules in the DRL file:

* *New fact types:* The default fact type in the `java.lang` package of {PRODUCT} is `Object`, but you can declare other types in DRL files as needed. Declaring fact types in DRL files enables you to define a new fact model directly in the {RULE_ENGINE}, without creating models in a lower-level language like Java. You can also declare a new type when a domain model is already built and you want to complement this model with additional entities that are used mainly during the reasoning process.
* *Metadata for fact types:* You can associate metadata in the format `@key(value)` with new or existing facts. Metadata can be any kind of data that is not represented by the fact attributes and is consistent among all instances of that fact type. The metadata can be queried at run time by the {RULE_ENGINE} and used in the reasoning process.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[id='drl-declarations-enumerative-con_{context}']
= Enumerative type declarations in DRL

DRL supports the declaration of enumerative types in the format `declare enum <factType>`, followed by a comma-separated list of values ending with a semicolon. You can then use the enumerative list in the rules in the DRL file.

For example, the following enumerative type declaration defines days of the week for an employee scheduling rule:

.Example enumerative type declaration with a scheduling rule
[source]
----
declare enum DaysOfWeek
SUN("Sunday"),MON("Monday"),TUE("Tuesday"),WED("Wednesday"),THU("Thursday"),FRI("Friday"),SAT("Saturday");
fullName : String
end
rule "Using a declared Enum"
when
$emp : Employee( dayOff == DaysOfWeek.MONDAY )
then
...
end
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[id='drl-declarations-extended-con_{context}']
= Extended type declarations in DRL

DRL supports type declaration inheritance in the format `declare <factType1> extends <factType2>`. To extend a type declared in Java by a subtype declared in DRL, you repeat the parent type in a declaration statement without any fields.

For example, the following type declarations extend a `Student` type from a top-level `Person` type, and a `LongTermStudent` type from the `Student` subtype:

.Example extended type declarations
[source]
----
import org.people.Person
declare Person end
declare Student extends Person
school : String
end
declare LongTermStudent extends Student
years : int
course : String
end
----
Loading

0 comments on commit f9bae76

Please sign in to comment.