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/277e39f97b97c579bd3f59c7f0f045ee to your computer and use it in GitHub Desktop.
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/6f4581f17a303ef0d5a929dd3ab84f47c60a310
// 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.3.0"
//> using dep "fr.janalyse::drools-scripting:1.1.0"
//> using dep "org.scalatest::scalatest:3.2.16"
// ---------------------
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