Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drools step by step FireKB example / published by https://github.com/dacr/code-examples-manager #44b1c8d3-d07d-4650-8476-a599b49c4ee7/2eac14cff702f504d0f5a9b0153e082b946a4c51
// summary : Drools step by step FireKB example
// keywords : scala, drools, mvel, ai, knowledgebase
// publish : gist
// authors : David Crosson
// license : Apache2
// id : 44b1c8d3-d07d-4650-8476-a599b49c4ee7
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm-2.12 scriptname.sc'
// created-on : 2019-10-23T23:00:03+02:00
def pause(msg:String):Unit = {
println("- - - - - - - - - - - - - - - - - - - - - - -")
System.out.println(msg)
println("=============================================")
System.in.read()
}
pause("USE ENTER KEY TO CONTINUE ON NEXT STEP")
import $ivy.`org.drools:drools-examples:7.44.0.Final`
pause(
"""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