Permalink
Browse files

improvements

  • Loading branch information...
1 parent 92558a3 commit bfc716b599c171296c611382e7ce742b64488d9a @dacr committed Sep 7, 2012
Showing with 98 additions and 59 deletions.
  1. +7 −12 build.sbt
  2. +1 −5 project/plugins.sbt
  3. +11 −1 src/main/resources/KBExpertise.drl
  4. +79 −41 src/main/scala/dummy/Dummy.scala
View
@@ -4,7 +4,7 @@ seq(assemblySettings: _*)
name := "ScalaDroolsDummyProject"
-version := "0.0.1"
+version := "2"
scalaVersion := "2.9.2"
@@ -14,20 +14,15 @@ jarName in assembly := "dummy.jar"
-libraryDependencies += "org.drools" % "drools-compiler" % "5.4.0.Final"
-
-libraryDependencies += "org.drools" % "drools-core" % "5.4.0.Final"
-
-libraryDependencies += "org.drools" % "drools-jsr94" % "5.4.0.Final"
-
-libraryDependencies += "org.drools" % "drools-decisiontables" % "5.4.0.Final"
-
-libraryDependencies += "org.drools" % "knowledge-api" % "5.4.0.Final"
-
+libraryDependencies ++= {
+ Seq("drools-compiler", "drools-core","drools-jsr94", "drools-decisiontables", "knowledge-api")
+ .map("org.drools" % _ % "5.4.0.Final")
+}
+libraryDependencies += "com.sun.xml.bind" % "jaxb-xjc" % "2.2.4-1"
libraryDependencies += "com.thoughtworks.xstream" % "xstream" % "1.4.2"
-
+
libraryDependencies += "org.scalatest" %% "scalatest" % "1.8" % "test"
View
@@ -1,9 +1,5 @@
-//resolvers += Classpaths.typesafeResolver
-
-resolvers += Resolver.url("sbt-plugin-releases",
- new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/"))(Resolver.ivyStylePatterns)
-
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.8.4")
+
@@ -3,6 +3,7 @@ package dummy;
dialect "mvel"
import java.util.LinkedList;
+import dummy.model.*;
// --------------------------------------------------------------
@@ -60,7 +61,7 @@ rule "Who live at London"
$someone:Someone($name:name)
Home(someone == $someone, address.isDefined, ((Address)address.get).town == "London")
then
- insert(new InformationRequest($someone, $name+" lives in London"))
+ insert(new InformationRemarkable($someone, $name+" lives in London"))
end
// --------------------------------------------------------------
@@ -72,6 +73,15 @@ rule "process information requests"
System.out.println("INFORMATION REQUESTED FOR "+$who+" : "+$what)
end
+
+rule "process remarkable information"
+ when
+ InformationRemarkable($who:someone, $what:info)
+ then
+ System.out.println("REMARKABLE INFORMATION ABOUT "+$who+" : "+$what)
+end
+
+
// --------------------------------------------------------------
rule "who owns more than 1 car"
@@ -16,44 +16,60 @@
package dummy
-// ----------------------------------------------------------------
-// DOMAIN MODEL
-
-case class Someone(name:String, age:Int)
-
-case class Car(someone:Someone, model:String, year:Int, color:Color)
-
-case class Color(name:String)
-
-object Color {
- val red = Color("red")
- val blue = Color("blue")
- val green = Color("green")
- val black = Color("black")
-}
-
-case class Address(street:String, town:String, country:String)
-
-case class Home(someone:Someone, address:Option[Address])
-
-case class InformationRequest(someone:Someone, message:String)
-
-
-
-
-// ----------------------------------------------------------------
-
+import dummy.model._
import java.io.FileInputStream
import java.io.InputStreamReader
import org.drools.RuleBaseFactory
import org.drools.audit.WorkingMemoryFileLogger
import org.drools.compiler.PackageBuilder
+import org.drools.builder.KnowledgeBuilderFactory
+import org.drools.io.ResourceFactory
+import java.io.File
+import org.drools.builder.ResourceType
+import collection.JavaConversions._
object Dummy {
+ def factsExample1:List[Any] = {
+ val martine = Someone(name="Martine", age=30)
+ val martin = Someone(name="Martin", age=40)
+ val jack = Someone(name="Jack", age=12)
+ val martineCar = Car(martine, "Ford", 2010, Color.blue)
+ val martinCar = Car(martin, "GM", 2010, Color.black)
+ val martinCar2 = Car(martin, "Ferrari", 2012, Color.red)
+ val martinCar3 = Car(martin, "Porshe", 2011, Color.red)
+
+ val martinHome = Home(martin, None)
+ val jackHome = Home(jack, Some(Address("221B Baker Street", "London", "England")))
+
+ List(
+ martine,
+ martin,
+ jack,
+ martineCar,
+ martinCar,
+ martinCar2,
+ martinCar3,
+ martinHome,
+ jackHome
+ )
+ }
+
+
+
+
def main(args: Array[String]) {
+ usageCase2(factsExample1)
+ }
+
+
+
+
+ // ===> newStatefulSession
+
+ def usageCase1(facts: List[Any]) {
System.setProperty("drools.dialect.mvel.strict", "false")
val rulesfilename = "src/main/resources/KBExpertise.drl"
@@ -79,24 +95,46 @@ object Dummy {
logger.setFileName("drools")
logger.writeToDisk()
- val martine = Someone(name="Martine", age=30)
- val martin = Someone(name="Martin", age=40)
- val jack = Someone(name="Jack", age=12)
- val martineCar = Car(martine, "Ford", 2010, Color.blue)
- val martinCar = Car(martin, "GM", 2010, Color.black)
- val martinCar2 = Car(martin, "Ferrari", 2012, Color.red)
- val martinCar3 = Car(martin, "Porshe", 2011, Color.red)
+ facts.foreach(session.insert(_))
+
+ session.fireAllRules()
+
+ session.dispose()
+ }
+
+
+ // ===> newStatefulKnowledgeSession
+ // how to extract facts, and process them from java/scala
+
+ def usageCase2(facts: List[Any]):Iterable[Information] = {
+ val rulesResource = "src/main/resources/KBExpertise.drl"
+
+ val config = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration()
+ config.setProperty("drools.dialect.mvel.strict", "false")
+ val kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config)
- val martinHome = Home(martin, None)
- val jackHome = Home(jack, Some(Address("221B Baker Street", "London", "England")))
+ kbuilder.add(ResourceFactory.newFileResource(new File(rulesResource)), ResourceType.DRL )
+
+ val kbase = kbuilder.newKnowledgeBase()
+ val session = kbase.newStatefulKnowledgeSession()
+ //session.addEventListener(new org.drools.event.DebugAgendaEventListener())
+ //session.addEventListener(new org.drools.event.DebugWorkingMemoryEventListener())
- List(martine, martin, jack,
- martineCar, martinCar, martinCar2, martinCar3,
- martinHome, jackHome
- ).foreach(session.insert(_))
+ // setup the audit logging
+ val logger:WorkingMemoryFileLogger = new WorkingMemoryFileLogger(session)
+ logger.setFileName("drools")
+ logger.writeToDisk()
+ facts.foreach(session.insert(_))
+
session.fireAllRules()
-
+
+ val results = session.getObjects() collect { case x:Information => x }
+
session.dispose()
+
+ results
+
}
+
}

0 comments on commit bfc716b

Please sign in to comment.