Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drools constraints analysis / published by https://github.com/dacr/code-examples-manager #581eb9be-b67a-4c2f-87cf-412b681c3e0c/49aa2f0f856472abc33a47c3a741a660ac4573b
// summary : Drools constraints analysis
// keywords : scala, drools, mvel, scalatest, ai, knowledgebase, @testable
// publish : gist
// authors : David Crosson
// license : Apache2
// id : 581eb9be-b67a-4c2f-87cf-412b681c3e0c
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// created-on : 2020-01-10T08:06:29+01:00
interp.resolutionHooks += { fetch =>
// -- This is mandatory with drools >= 7.0.46 because drools sources artifacts also brings kie.conf
// -- (it generates resources conflict at KIE init) and because by default ammonite also load sources artifact...
// -- WARN : YOU MAY HAVE TO CLEANUP YOUR LOCAL COURSIER CACHE ~/.cache/coursier to remove sources artifact
// -- WARN : find ~/.cache/coursier/ -name "*drools*-sources.jar" | xargs rm -f
import scala.jdk.CollectionConverters._
fetch.withClassifiers(fetch.getClassifiers.asScala.filter(_ != "sources").asJava)
}
@
import $ivy.`fr.janalyse::drools-scripting:1.0.15`, $ivy.`org.scalatest::scalatest:3.2.6`
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