DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Trending

  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Building a Real Pipeline With senro: Monorepos, Caching, and Triggers
  • Building IoT Time-Series Applications With Java and Apache IoTDB
  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Write DRL Rules in Kogito for Globals Use Cases

How to Write DRL Rules in Kogito for Globals Use Cases

Quick tutorial on how to write DRL rules in Kogito for globals use cases with syntax that is similar to regular Drools.

By 
Amit Nijhawan
·
Sep. 02, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
4.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

While writing  DRL rules for Kogito, its syntax is not so different from regular Drools. However, I recommend to take a look at the document:

https://docs.jboss.org/kogito/release/latest/html_single/#_using_drl_rules_in_kogito_services

Notable differences are:

  • Designate Rule Unit
  • Facts are supplied through DataSource
  • LHS is written with OOPath

You can see kogito-examples (https://github.com/kiegroup/kogito-examples/) will help you to understand these differences.

Then… You may wonder “What about Global?”

To use a global to collect rule outcome is a popular use case in Drools.


global org.example.Result result;

rule "older than 18"
when
  $p : Person(age > 18)
then
  result.addAdultPersonName("Hello " + $p.getName());
end

I will explain how to implement DRL rules in Kogito for such use cases

A) SingletonStore (recommended)

SingletonStore: A writable storage option for setting or clearing a single element and then notifying all subscribers that the element has been modified. Rules can pattern-match against the value and update or clear available values. For users familiar with Drools, this option is equivalent to a global. In fact, a Singleton<Object> is similar to an old-style global, except that when used in conjunction with rules, you can pattern-match against it.

You can replace global with SingletonStore, which is a singleton DataSource.

declare Hello extends RuleUnitData
  result : SingletonStore<Result> = DataSource.createSingleton()
  persons : DataStore<Person> = DataSource.createStore()
end


Note that SingletonStore is a DataSource so you need to refer it in the LHS (unlike globals). But I think it’s rather a good thing so you see what you do.

rule "older than 18"
when
  $r : /result
  $p : /persons[age > 18]
then
  $r.addAdultPersonName("Hello, " + $p.getName());
end

Then you can write a simple query to return the result.

query hello
  $r : /result
end


Example) https://github.com/Amitninja12345/kogito-examples1


B) Plain Field in RuleUnitData

If you have a plain field (not a DataSource) in RuleUnitData class, it is a “global”.

declare Hello extends RuleUnitData
  result : Result = new Result()
  persons : DataStore<Person> = DataSource.createStore()
end

You can use the field in RHS.

rule "older than 18"
when
  $p : /persons[age > 18]
then
  result.addAdultPersonName("Hello, " + $p.getName());
end

The field cannot be accessed in OOPath but you can refer it via “from”. So you can write a query like this.

query hello
  $r : Result() from result
end


Example) https://github.com/Amitninja12345/Kogito-example2

Database

Opinions expressed by DZone contributors are their own.

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook