Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/75a56fdb3c5088835abaa5823dc415ca to your computer and use it in GitHub Desktop.
Save dacr/75a56fdb3c5088835abaa5823dc415ca to your computer and use it in GitHub Desktop.
Drools step by step FireKB example / published by https://github.com/dacr/code-examples-manager #44b1c8d3-d07d-4650-8476-a599b49c4ee7/549e93f5cd0af2fa5cab78213e3b5b66db0cd28a
// summary : Drools step by step FireKB example
// keywords : scala, drools, mvel, ai, knowledgebase
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 44b1c8d3-d07d-4650-8476-a599b49c4ee7
// created-on : 2019-10-23T23:00:03+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.drools:drools-examples:7.54.0.Final"
// ---------------------
def pause(msg:String):Unit = {
println("- - - - - - - - - - - - - - - - - - - - - - -")
System.out.println(msg)
println("=============================================")
System.in.read()
}
pause("USE ENTER KEY TO CONTINUE ON NEXT STEP")
pause( // TODO update for scala-cli -repl
"""import $ivy.`org.drools:drools-examples:7.44.0.Final`
| Dynamic loading of this library, it'll also load all related sub dependencies
| This jar contains all drools example knownledge bases. Both DRL files and all
| the compiled java classes they may use. The configuration file for all defined
| knowledge bases is store within the META-INF/kmodule.xml within this jar.
|""".stripMargin
)
import org.kie.api._, org.kie.api.runtime._
pause(
"""import org.kie.api._, org.kie.api.runtime._
| Brings required classes in the current scope. Allow us for example to have direct
| access for example to the KieServices class without to specify its full path which
| is "org.kie.api.KieServices"
|""".stripMargin
)
val kServices = KieServices.Factory.get
pause(
"""val kServices = KieServices.Factory.get
| The main entry point to all DROOLS API. Returns the access singleton.
|""".stripMargin
)
val kContainer = kServices.getKieClasspathContainer()
pause(
"""val kContainer = kServices.getKieClasspathContainer()
| Get the a classpath KIE container which allow us to list all available
| knowledge bases, and also to create one from its declared name. In fact
| it will search for all available META-INF/kmodule.xml in the class path,
| some coming from the current application or other coming from loaded
| dependencies.
|""".stripMargin
)
val kbNames = kContainer.getKieBaseNames
println(kbNames)
pause(
"""val kbNames = kContainer.getKieBaseNames
|println(kbNames)
| Get the names of all available knowledge bases. The names as those declared
| in found META-INF/kmodule.xml such as "FireKB" for :
| <?xml version="1.0" encoding="UTF-8"?>
| <kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xmlns="http://www.drools.org/xsd/kmodule">
| ...
| <kbase name="FireKB" packages="org.drools.examples.fire.simple">
| ...
|""".stripMargin
)
val kbase = kContainer.getKieBase("FireKB")
pause(
"""val kbase = kContainer.getKieBase("FireKB")
| Create the knowledge base API entry point, from which we can
| get information about available rules, declarations, ... Through
| this API we can also create an execution session to start the
| expert system.
|""".stripMargin
)
val session = kbase.newKieSession()
pause(
"""val session = kbase.newKieSession()
| Create a DROOLS statefull session, start the expert system and
| provide all method to interact with the expert system.
| insert, fireAllRules, fireUntilHalt, delete, ... methods.
|""".stripMargin
)
import org.drools.examples.fire._
pause(
"""import org.drools.examples.fire._
| The FireKB knowledge base uses fact type declared from java code
| (not declarations within drl files) so in order to create we need
| to import the classes from the place they reside.
|""".stripMargin
)
val fireFact = new Fire(new Room("42"))
pause(
"""val fireFact = new Fire(new Room("42"))
| Create a Fire fact which has been detected in Room 42.
| The create fire instance is going to be added to drools working memory
|""".stripMargin
)
session.insert(fireFact)
pause(
"""session.insert(fireFact)
| The fact fireFact is inserted into drools working memory.
|""".stripMargin
)
session.fireAllRules()
pause(
"""session.fireAllRules()
| Ask drools to fire all rules which can be fired up. This method will
| return only when no more rules can be run.
|""".stripMargin
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment