Last active
October 6, 2024 10:54
-
-
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/d26cecee2ffdaaf7d55144b1f9ae0f7867828329
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.5.1" | |
//> using dep "org.drools:drools-examples:9.44.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