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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Production Database Migration or Modernization: A Comprehensive Planning Guide [Part 2]

Trending

  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  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 user avatar
Amit Nijhawan
·
Sep. 02, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Production Database Migration or Modernization: A Comprehensive Planning Guide [Part 2]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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