Last active
October 6, 2024 10:54
-
-
Save dacr/277e39f97b97c579bd3f59c7f0f045ee to your computer and use it in GitHub Desktop.
Drools constraints analysis / published by https://github.com/dacr/code-examples-manager #581eb9be-b67a-4c2f-87cf-412b681c3e0c/b3255aca46abcf11c87371a81112774da24ad1dd
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 constraints analysis | |
// keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable | |
// 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 : 581eb9be-b67a-4c2f-87cf-412b681c3e0c | |
// created-on : 2020-01-10T08:06:29+01:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.5.1" | |
//> using dep "fr.janalyse::drools-scripting:1.2.0" | |
//> using dep "org.scalatest::scalatest:3.2.19" | |
// --------------------- | |
import fr.janalyse.droolscripting.*, org.scalatest.*, wordspec.*, matchers.*, OptionValues.* | |
object UnderstandingRulesConstraintLimits extends AnyWordSpec with should.Matchers { | |
override def suiteName: String = "UnderstandingRulesConstraintLimits" | |
def checkOK(drl:String):Unit = { | |
val engine = DroolsEngine(drl, DroolsEngineConfig(equalsWithIdentity=false, eventProcessingMode = StreamMode, pseudoClock = false)) | |
engine.fireAllRules() | |
engine.strings shouldBe List("OK") | |
} | |
"DROOLS" should { | |
// ====================================================================== | |
"support attribute changes without infinite loop for attributes not used in any constraints " in checkOK { | |
info("ALWAYS PLACE ATTRIBUTE NAME IN THE LHS PART OF CONSTRAINTS") | |
info("WRITE : origin == $origin, card == $card") | |
info("DON'T WRITE : $origin == origin, $card == card") | |
info("This is a 'known' limitation of the property reactivity analysis") | |
info("TO AVOID RULE INFINITE RECURSION") | |
info("Check : https://issues.redhat.com/browse/DROOLS-4909") | |
info("Check : https://issues.redhat.com/browse/DROOLS-4288") | |
"""package dummy | |
|dialect "mvel" | |
| | |
|declare Init id:String end | |
|declare Origin id:String end | |
|declare Card id:String end | |
|declare Context origin:Origin card:Card end | |
| | |
|declare Metric origin:Origin card:Card name:String value:double end | |
|declare Issue origin:Origin card:Card desc:String risk:int category:String end | |
| | |
|rule "init" when then insert(new Init("abcd")) end | |
| | |
|rule "origin" when Init($id:id) then | |
| insertLogical(new Context(new Origin($id), new Card($id))) | |
|end | |
| | |
|rule "init counter metric" | |
|when | |
| Context($o:origin, $c:card) | |
| not Metric($o == origin, $c == card, name == "counter") | |
|then | |
| insert(new Metric($o, $c, "counter", 0)) | |
|end | |
| | |
|//----------------- | |
|rule "reset" | |
|when | |
| $context:Context($origin:origin, $card:card) | |
| not Issue(origin == $origin, $card == card, category!="scoring") | |
| $metric:Metric(name == "counter", origin == $origin, card == $card) | |
|then | |
| modify($metric) { | |
| setValue(100); | |
| }; | |
|end | |
| | |
|//----------------- | |
|rule "end" salience -10 when Metric(value == 100) then insert("OK"); end | |
| | |
|""".stripMargin | |
} | |
} | |
} | |
UnderstandingRulesConstraintLimits.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment