├── .gitignore ├── README ├── chapter01 ├── calendar-based-rules │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ ├── Server.java │ │ │ ├── Virtualization.java │ │ │ └── VirtualizationRequest.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── CalendarBasedRulesTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ └── rules.drl ├── declaring-facts-in-the-engine │ ├── pom.xml │ └── src │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── FactsDeclarationTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ └── rules.drl ├── declaring-facts-using-xml │ ├── pom.xml │ └── src │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── XSDFactsDeclarationTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ ├── model.xsd │ │ └── rules.drl ├── event-listener-logging │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ ├── CustomAgendaEventListener.java │ │ │ └── CustomWorkingMemoryEventListener.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── RulesExecutionLoggingTest.java │ │ └── resources │ │ ├── drools │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── rules.drl │ │ └── log4j.properties ├── live-queries │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ ├── Server.java │ │ │ ├── Virtualization.java │ │ │ ├── VirtualizationRequest.java │ │ │ └── listener │ │ │ ├── CustomViewChangedEventListener.java │ │ │ └── GlazedListViewChangedEventListener.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter01 │ │ │ └── LiveQueriesTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ └── rules.drl ├── pom.xml └── timer-based-rules │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ ├── Server.java │ │ ├── ServerAlert.java │ │ ├── ServerEvent.java │ │ ├── ServerStatus.java │ │ └── Virtualization.java │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter01 │ │ └── TimerBasedRulesTest.java │ └── resources │ └── drools │ └── cookbook │ └── chapter01 │ └── rules.drl ├── chapter02 ├── duplicated-facts │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ ├── Server.java │ │ │ ├── Virtualization.java │ │ │ └── VirtualizationRequest.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── DuplicatedFactsInsertion.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ └── rules.drl ├── knowledge-agent-classloader │ ├── pom.xml │ └── src │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── KnowledgeAgentClassloaderTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ ├── change-set.xml │ │ ├── model.jar │ │ └── rules.drl ├── knowledge-persistence │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ ├── Server.java │ │ │ ├── Virtualization.java │ │ │ └── VirtualizationRequest.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── JPAKnowledgeSessionPersistenceTest.java │ │ └── resources │ │ ├── META-INF │ │ └── persistence.xml │ │ ├── drools │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── rules.drl │ │ ├── jndi.properties │ │ └── log4j.xml ├── marshalling-knowledge-sessions │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ ├── Server.java │ │ │ └── virtualization │ │ │ ├── Virtualization.java │ │ │ └── VirtualizationRequest.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── MarshallingKnowledgeSessionTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ └── rules.drl ├── monitoring-knowledge-metrics │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ ├── Server.java │ │ │ ├── Service.java │ │ │ ├── ServiceRequest.java │ │ │ ├── ServiceRequestFactory.java │ │ │ ├── ServiceType.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter02 │ │ │ └── ServiceRequestSimulation.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ └── rules.drl ├── pom.xml └── verifying-rules │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ ├── Server.java │ │ ├── Service.java │ │ ├── ServiceRequest.java │ │ ├── ServiceRequestFactory.java │ │ ├── ServiceType.java │ │ └── Virtualization.java │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter02 │ │ └── RulesVerifier.java │ └── resources │ └── drools │ └── cookbook │ └── chapter02 │ └── rules.drl ├── chapter04 ├── pom.xml └── rest-api │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── org │ │ └── drools │ │ └── guvnor │ │ ├── api │ │ └── GuvnorRestApi.java │ │ └── jaxb │ │ ├── Asset.java │ │ └── AssetMetadata.java │ └── test │ └── java │ └── drools │ └── cookbook │ └── chapter04 │ └── GuvnorRestApiTest.java ├── chapter05 ├── entry-points │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ ├── helper │ │ │ │ └── FlighSimulation.java │ │ │ │ └── model │ │ │ │ ├── FlightControl.java │ │ │ │ └── FlightStatus.java │ │ └── resources │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── rules.drl │ │ └── test │ │ └── java │ │ └── drools │ │ └── cookbook │ │ └── FlightControlTest.java ├── event-declaration │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ └── model │ │ │ │ ├── FlightControl.java │ │ │ │ └── FlightStatus.java │ │ └── resources │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── rules.drl │ │ └── test │ │ └── java │ │ └── drools │ │ └── cookbook │ │ └── FlightControlTest.java ├── pom.xml ├── pseudo-clock │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ ├── helper │ │ │ │ ├── EmergencySystem.java │ │ │ │ └── FlighSimulation.java │ │ │ │ └── model │ │ │ │ ├── EmergencySignal.java │ │ │ │ ├── FlightControl.java │ │ │ │ └── FlightStatus.java │ │ └── resources │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── rules.drl │ │ └── test │ │ └── java │ │ └── drools │ │ └── cookbook │ │ └── FlightControlTest.java ├── sliding-windows │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ ├── helper │ │ │ │ └── FlighSimulation.java │ │ │ │ └── model │ │ │ │ ├── EmergencyInCourse.java │ │ │ │ ├── EmergencySignal.java │ │ │ │ ├── FlightControl.java │ │ │ │ └── FlightStatus.java │ │ └── resources │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── rules.drl │ │ └── test │ │ └── java │ │ └── drools │ │ └── cookbook │ │ └── FlightControlTest.java └── temporal-operators │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ ├── helper │ │ │ ├── EmergencySystem.java │ │ │ └── FlighSimulation.java │ │ │ └── model │ │ │ ├── FlightControl.java │ │ │ └── FlightStatus.java │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── rules.drl │ └── test │ └── java │ └── drools │ └── cookbook │ └── FlightControlTest.java ├── chapter06 ├── pom.xml ├── rest-execution │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter06 │ │ │ ├── Server.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter06 │ │ │ └── RestExecutionTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter06 │ │ └── rules.drl └── xstream-commands │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── drools │ │ └── cookbook │ │ └── chapter06 │ │ ├── Server.java │ │ └── Virtualization.java │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter06 │ │ └── XStreamCommandsTest.java │ └── resources │ └── drools │ └── cookbook │ └── chapter06 │ └── rules.drl ├── chapter07 ├── apache-camel-integration │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── model │ │ │ ├── Server.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── CamelIntegrationTest.java │ │ └── resources │ │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── rules.drl ├── apache-camel-jms │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ ├── jms │ │ │ └── JMSQueueProducer.java │ │ │ └── model │ │ │ ├── Server.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── org │ │ │ └── plugtree │ │ │ └── drools │ │ │ └── camel │ │ │ ├── CamelActiveMQTest.java │ │ │ └── CamelEmbeddedActiveMQTest.java │ │ └── resources │ │ ├── camel-amq.xml │ │ ├── camel-embedded-amq.xml │ │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── rules.drl ├── apache-camel-spring-integration │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── model │ │ │ ├── Server.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── CamelSpringIntegrationTest.java │ │ └── resources │ │ ├── applicationContext.xml │ │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── rules.drl ├── pom.xml ├── spring-integration │ ├── pom.xml │ └── src │ │ ├── main │ │ └── java │ │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── model │ │ │ ├── Server.java │ │ │ └── Virtualization.java │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter07 │ │ │ └── SpringIntegrationTest.java │ │ └── resources │ │ ├── applicationContext.xml │ │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── rules.drl └── spring-jpa-integration │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── model │ │ ├── Server.java │ │ └── Virtualization.java │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter07 │ │ └── SpringJpaIntegrationTest.java │ └── resources │ ├── META-INF │ └── persistence.xml │ ├── applicationContext.xml │ └── drools │ └── cookbook │ └── chapter07 │ └── rules.drl ├── chapter08 ├── benchmarker │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ └── chapter08 │ │ │ │ ├── domain │ │ │ │ ├── Location.java │ │ │ │ ├── ServiceRequest.java │ │ │ │ ├── Skill.java │ │ │ │ ├── Technician.java │ │ │ │ └── TrainingLevel.java │ │ │ │ └── planner │ │ │ │ ├── SelectNextTechnicians.java │ │ │ │ ├── TechnicianMove.java │ │ │ │ └── TechniciansSolution.java │ │ └── resources │ │ │ ├── ServiceRequestSolverBenchmarkConfig.xml │ │ │ ├── serviceRequestScoreRules.drl │ │ │ └── unsolvedTechnicianRequest.xml │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter08 │ │ │ └── BestAvailableTechnicianBenchmark.java │ │ └── resources │ │ └── log4j.xml ├── pom.xml ├── resource-assignation │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── drools │ │ │ │ └── cookbook │ │ │ │ └── chapter08 │ │ │ │ ├── domain │ │ │ │ ├── Location.java │ │ │ │ ├── ServiceRequest.java │ │ │ │ ├── Skill.java │ │ │ │ ├── Technician.java │ │ │ │ └── TrainingLevel.java │ │ │ │ └── planner │ │ │ │ ├── SelectNextTechnicians.java │ │ │ │ ├── TechnicianMove.java │ │ │ │ └── TechniciansSolution.java │ │ └── resources │ │ │ ├── ServiceRequestSolverConfig.xml │ │ │ └── serviceRequestScoreRules.drl │ │ └── test │ │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter08 │ │ │ ├── BestAvailableTechnician.java │ │ │ └── BestTechnicianAvailableTest.java │ │ └── resources │ │ └── log4j.xml └── simulated-annealing │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter08 │ │ │ ├── domain │ │ │ ├── Location.java │ │ │ ├── ServiceRequest.java │ │ │ ├── Skill.java │ │ │ ├── Technician.java │ │ │ └── TrainingLevel.java │ │ │ └── planner │ │ │ ├── SelectNextTechnicians.java │ │ │ ├── TechnicianMove.java │ │ │ └── TechniciansSolution.java │ └── resources │ │ ├── ServiceRequestSolverConfig.xml │ │ └── serviceRequestScoreRules.drl │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter08 │ │ └── SimulatedAnnealingTest.java │ └── resources │ └── log4j.xml └── chapter09 ├── business-activity-monitoring-reports ├── pom.xml └── src │ ├── main │ └── resources │ │ ├── AuditLog.hbm.xml │ │ ├── hibernate.cfg.xml │ │ └── reports │ │ ├── overall_activity.rptdesign │ │ ├── process_instances_report.rptdesign │ │ └── process_summary.rptdesign │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter09 │ │ └── BAMReporting.java │ └── resources │ ├── processWithScriptTask.bpmn │ └── processWithSignalEvent.bpmn ├── business-monitoring-with-drools-fusion ├── pom.xml └── src │ ├── main │ ├── java │ │ └── drools │ │ │ └── cookbook │ │ │ └── chapter09 │ │ │ └── CustomProcessEventListener.java │ └── resources │ │ ├── withdrawalProcess.bpmn │ │ └── withdrawalRules.drl │ └── test │ └── java │ └── drools │ └── cookbook │ └── chapter09 │ └── BusinessMonitoring.java ├── creating-process-using-the-api ├── pom.xml └── src │ └── test │ └── java │ └── drools │ └── cookbook │ └── chapter09 │ └── CreatingProcessUsingTheApiTest.java ├── human-tasks ├── pom.xml └── src │ └── test │ ├── java │ └── drools │ │ └── cookbook │ │ └── chapter09 │ │ └── HumanTaskTest.java │ └── resources │ ├── META-INF │ ├── orm.xml │ └── persistence.xml │ └── document.bpmn ├── pom.xml └── unit-testing ├── pom.xml └── src └── test ├── java └── drools │ └── cookbook │ └── chapter09 │ ├── JbpmJUnitTestCase.java │ ├── SimpleProcessTest.java │ └── SimpleProcessWithPersistenceTest.java └── resources ├── META-INF ├── orm.xml └── persistence.xml ├── jndi.properties ├── processWithScriptTask.bpmn └── processWithSignalEvent.bpmn /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .classpath 3 | .project 4 | .settings 5 | .DS_Store 6 | btm1.tlog 7 | btm2.tlog 8 | ksession.info 9 | JPADroolsFlow.h2.db 10 | **/benchmark-results/ 11 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Drools Developer's Cookbook Examples 2 | ==================================== 3 | 4 | In this repository you will find the examples of the Drools Developer's Cookbook 5 | 6 | -------------------------------------------------------------------------------- /chapter01/calendar-based-rules/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | calendar-based-rules 10 | Calendar based rules 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.opensymphony.quartz 27 | quartz 28 | 1.6.1 29 | 30 | 31 | junit 32 | junit 33 | test 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter01/calendar-based-rules/src/main/java/drools/cookbook/chapter01/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setServerName(String serverName) { 31 | this.serverName = serverName; 32 | } 33 | 34 | public String getServerName() { 35 | return serverName; 36 | } 37 | 38 | public void setMemory(int memory) { 39 | this.memory = memory; 40 | } 41 | 42 | public int getMemory() { 43 | return memory; 44 | } 45 | 46 | public void setDiskSpace(int diskSpace) { 47 | this.diskSpace = diskSpace; 48 | } 49 | 50 | public int getDiskSpace() { 51 | return diskSpace; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter01/calendar-based-rules/src/main/java/drools/cookbook/chapter01/VirtualizationRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class VirtualizationRequest { 9 | 10 | private String serverName; 11 | private Virtualization virtualization; 12 | private boolean successful; 13 | 14 | public VirtualizationRequest(Virtualization virtualization) { 15 | this.serverName = virtualization.getServerName(); 16 | this.virtualization = virtualization; 17 | } 18 | 19 | public void setServerName(String serverName) { 20 | this.serverName = serverName; 21 | } 22 | 23 | public String getServerName() { 24 | return serverName; 25 | } 26 | 27 | public void setVirtualization(Virtualization virtualization) { 28 | this.virtualization = virtualization; 29 | } 30 | 31 | public Virtualization getVirtualization() { 32 | return virtualization; 33 | } 34 | 35 | public void setSuccessful(boolean successful) { 36 | this.successful = successful; 37 | } 38 | 39 | public boolean isSuccessful() { 40 | return successful; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter01/calendar-based-rules/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | rule "New virtualization request" 7 | calendars "only-weekdays" 8 | when 9 | $request : VirtualizationRequest($serverName : serverName) 10 | $server : Server(name==$serverName) 11 | then 12 | System.out.println("New virtualization added on server " + $serverName); 13 | $server.getVirtualizations().add($request.getVirtualization()); 14 | $request.setSuccessful(true); 15 | retract($request); 16 | end -------------------------------------------------------------------------------- /chapter01/declaring-facts-in-the-engine/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | declaring-facts-in-the-engine 10 | Declaring facts directly in the engine 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter01/declaring-facts-in-the-engine/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | global java.util.List serversAvailability; 7 | 8 | declare Server 9 | name : String 10 | processors : int 11 | memory : int // megabytes 12 | diskSpace : int // gigabytes 13 | virtualizations : List // list of Virtualization objects 14 | cpuUsage : int // percentage 15 | end 16 | 17 | declare ServerStatus 18 | name : String 19 | freeMemory : int 20 | percentageFreeMemory : int 21 | freeDiskSpace : int 22 | percentageFreeDiskSpace : int 23 | currentCpuUsage : int 24 | end 25 | 26 | declare Virtualization 27 | name : String 28 | diskSpace : int 29 | memory : int 30 | end 31 | 32 | rule "check minimum server configuration" 33 | dialect "mvel" 34 | when 35 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 36 | then 37 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 38 | retract($server); 39 | end 40 | 41 | rule "check available server for a new virtualization" 42 | dialect "mvel" 43 | when 44 | $virtualization : Virtualization() 45 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 46 | $freeMemory : Number(intValue > 0) from accumulate (Virtualization($vmemory : memory) from $server.virtualizations, 47 | init(int total = $memory - $virtualization.memory), 48 | action(total -= $vmemory;), 49 | reverse(total += $vmemory;), 50 | result(total)) 51 | $freeDiskSpace : Number(intValue > 0) from accumulate (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, 52 | init(int total = $diskSpace - $virtualization.diskSpace), 53 | action(total -= $vdiskSpace;), 54 | reverse(total += $vdiskSpace;), 55 | result(total)) 56 | then 57 | ServerStatus serverStatus = new ServerStatus(); 58 | serverStatus.name = $server.name; 59 | serverStatus.freeMemory = $freeMemory; 60 | serverStatus.percentageFreeMemory = $freeMemory * 100 / $memory; 61 | serverStatus.freeDiskSpace = $freeDiskSpace; 62 | serverStatus.percentageFreeDiskSpace = $freeDiskSpace * 100 / $memory; 63 | serversAvailability.add(serverStatus); 64 | end 65 | -------------------------------------------------------------------------------- /chapter01/declaring-facts-using-xml/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | declaring-facts-using-xml 10 | Declaring facts using XML 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | javax.xml.bind 27 | jaxb-api 28 | 2.2.1 29 | 30 | 31 | com.sun.xml.bind 32 | jaxb-impl 33 | 2.2.1.1 34 | 35 | 36 | com.sun.xml.bind 37 | jaxb-xjc 38 | 2.2.1.1 39 | 40 | 41 | junit 42 | junit 43 | test 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /chapter01/declaring-facts-using-xml/src/test/resources/drools/cookbook/chapter01/model.xsd: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /chapter01/declaring-facts-using-xml/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | -------------------------------------------------------------------------------- /chapter01/event-listener-logging/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | event-listener-logging 10 | Event listener logging 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | org.slf4j 32 | slf4j-api 33 | 1.6.1 34 | 35 | 36 | org.slf4j 37 | slf4j-log4j12 38 | 1.6.1 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter01/event-listener-logging/src/main/java/drools/cookbook/chapter01/CustomAgendaEventListener.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | import org.drools.event.rule.ActivationCancelledEvent; 4 | import org.drools.event.rule.ActivationCreatedEvent; 5 | import org.drools.event.rule.AfterActivationFiredEvent; 6 | import org.drools.event.rule.AgendaEventListener; 7 | import org.drools.event.rule.AgendaGroupPoppedEvent; 8 | import org.drools.event.rule.AgendaGroupPushedEvent; 9 | import org.drools.event.rule.BeforeActivationFiredEvent; 10 | import org.slf4j.Logger; 11 | import org.slf4j.LoggerFactory; 12 | 13 | /** 14 | * 15 | * @author Lucas Amador 16 | * 17 | */ 18 | public class CustomAgendaEventListener implements AgendaEventListener { 19 | 20 | private static final Logger logger = LoggerFactory.getLogger(CustomAgendaEventListener.class); 21 | 22 | public void activationCancelled(ActivationCancelledEvent event) { 23 | logger.info("Activation Cancelled: " + event.getActivation()); 24 | } 25 | 26 | public void activationCreated(ActivationCreatedEvent event) { 27 | logger.info("Activation Created: " + event.getActivation()); 28 | } 29 | 30 | public void beforeActivationFired(BeforeActivationFiredEvent event) { 31 | logger.info("Before Activation Fired: " + event.getActivation()); 32 | } 33 | 34 | public void afterActivationFired(AfterActivationFiredEvent event) { 35 | logger.info("After Activation Fired: " + event.getActivation()); 36 | } 37 | 38 | public void agendaGroupPopped(AgendaGroupPoppedEvent event) { 39 | logger.info("Agenda Group Popped: " + event.getAgendaGroup()); 40 | } 41 | 42 | public void agendaGroupPushed(AgendaGroupPushedEvent event) { 43 | logger.info("Agenda Group Pushed: " + event.getAgendaGroup()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter01/event-listener-logging/src/main/java/drools/cookbook/chapter01/CustomWorkingMemoryEventListener.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | import org.drools.event.rule.ObjectInsertedEvent; 4 | import org.drools.event.rule.ObjectRetractedEvent; 5 | import org.drools.event.rule.ObjectUpdatedEvent; 6 | import org.drools.event.rule.WorkingMemoryEventListener; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | /** 11 | * 12 | * @author Lucas Amador 13 | * 14 | */ 15 | public class CustomWorkingMemoryEventListener implements WorkingMemoryEventListener { 16 | 17 | private static final Logger logger = LoggerFactory.getLogger(CustomWorkingMemoryEventListener.class); 18 | 19 | public void objectInserted(ObjectInsertedEvent event) { 20 | logger.info("Object Inserted: " + event.getFactHandle() + " Knowledge Runtime: " + event.getKnowledgeRuntime()); 21 | } 22 | 23 | public void objectRetracted(ObjectRetractedEvent event) { 24 | logger.info("Object Retracted: " + event.getFactHandle() + " Knowledge Runtime: " + event.getKnowledgeRuntime()); 25 | } 26 | 27 | public void objectUpdated(ObjectUpdatedEvent event) { 28 | logger.info("Object Updated: " + event.getFactHandle() + " Knowledge Runtime: " + event.getKnowledgeRuntime()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /chapter01/event-listener-logging/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | declare Server 7 | name : String 8 | processors : int 9 | memory : int // megabytes 10 | diskSpace : int // gigabytes 11 | virtualizations : List // list of Virtualization objects 12 | cpuUsage : int // percentage 13 | end 14 | 15 | rule "check minimum server configuration" 16 | dialect "mvel" 17 | when 18 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 19 | then 20 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 21 | retract($server); 22 | end -------------------------------------------------------------------------------- /chapter01/event-listener-logging/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 2 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-4p - %m%n 4 | 5 | log4j.rootLogger=INFO,CONSOLE 6 | -------------------------------------------------------------------------------- /chapter01/live-queries/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | live-queries 10 | Monitoring queries in real time 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | net.java.dev.glazedlists 27 | glazedlists_java15 28 | 1.8.0 29 | 30 | 31 | junit 32 | junit 33 | test 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter01/live-queries/src/main/java/drools/cookbook/chapter01/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setServerName(String serverName) { 31 | this.serverName = serverName; 32 | } 33 | 34 | public String getServerName() { 35 | return serverName; 36 | } 37 | 38 | public void setMemory(int memory) { 39 | this.memory = memory; 40 | } 41 | 42 | public int getMemory() { 43 | return memory; 44 | } 45 | 46 | public void setDiskSpace(int diskSpace) { 47 | this.diskSpace = diskSpace; 48 | } 49 | 50 | public int getDiskSpace() { 51 | return diskSpace; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter01/live-queries/src/main/java/drools/cookbook/chapter01/VirtualizationRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class VirtualizationRequest { 9 | 10 | private String serverName; 11 | private Virtualization virtualization; 12 | private boolean successful; 13 | 14 | public VirtualizationRequest(Virtualization virtualization) { 15 | this.serverName = virtualization.getServerName(); 16 | this.virtualization = virtualization; 17 | } 18 | 19 | public void setServerName(String serverName) { 20 | this.serverName = serverName; 21 | } 22 | 23 | public String getServerName() { 24 | return serverName; 25 | } 26 | 27 | public void setVirtualization(Virtualization virtualization) { 28 | this.virtualization = virtualization; 29 | } 30 | 31 | public Virtualization getVirtualization() { 32 | return virtualization; 33 | } 34 | 35 | public void setSuccessful(boolean successful) { 36 | this.successful = successful; 37 | } 38 | 39 | public boolean isSuccessful() { 40 | return successful; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /chapter01/live-queries/src/main/java/drools/cookbook/chapter01/listener/CustomViewChangedEventListener.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01.listener; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.drools.runtime.rule.Row; 7 | import org.drools.runtime.rule.ViewChangedEventListener; 8 | 9 | import drools.cookbook.chapter01.Server; 10 | 11 | /** 12 | * 13 | * @author Lucas Amador 14 | * 15 | */ 16 | public class CustomViewChangedEventListener implements ViewChangedEventListener { 17 | 18 | private List updatedServers = new ArrayList(); 19 | private List removedServers = new ArrayList(); 20 | private List currentServers = new ArrayList(); 21 | 22 | public void rowUpdated(Row row) { 23 | updatedServers.add((Server) row.get("$server")); 24 | } 25 | 26 | public void rowRemoved(Row row) { 27 | removedServers.add((Server) row.get("$server")); 28 | } 29 | 30 | public void rowAdded(Row row) { 31 | currentServers.add((Server) row.get("$server")); 32 | } 33 | 34 | public List getUpdatedServers() { 35 | return updatedServers; 36 | } 37 | 38 | public List getRemovedServers() { 39 | return removedServers; 40 | } 41 | 42 | public List getCurrentServers() { 43 | return currentServers; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter01/live-queries/src/main/java/drools/cookbook/chapter01/listener/GlazedListViewChangedEventListener.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01.listener; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.drools.runtime.rule.Row; 7 | import org.drools.runtime.rule.ViewChangedEventListener; 8 | 9 | import ca.odell.glazedlists.AbstractEventList; 10 | 11 | /** 12 | * 13 | * @author Lucas Amador 14 | * 15 | */ 16 | public class GlazedListViewChangedEventListener extends AbstractEventList implements ViewChangedEventListener { 17 | 18 | private List data = new ArrayList(); 19 | 20 | public void rowUpdated(Row row) { 21 | int index = this.data.indexOf(row); 22 | updates.beginEvent(); 23 | updates.elementUpdated(index, row, row); 24 | updates.commitEvent(); 25 | } 26 | 27 | public void rowRemoved(Row row) { 28 | int index = this.data.indexOf(row); 29 | updates.beginEvent(); 30 | data.remove(row); 31 | updates.elementDeleted(index, row); 32 | updates.commitEvent(); 33 | } 34 | 35 | public void rowAdded(Row row) { 36 | int index = size(); 37 | updates.beginEvent(); 38 | updates.elementInserted(index, row); 39 | data.add(row); 40 | updates.commitEvent(); 41 | } 42 | 43 | public void dispose() { 44 | data.clear(); 45 | } 46 | 47 | @Override 48 | public Row get(int index) { 49 | return data.get(index); 50 | } 51 | 52 | @Override 53 | public int size() { 54 | return data.size(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /chapter01/live-queries/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | query serverCpuUsage(int maxValue) 4 | $server : Server($serverName : name, cpuUsage <= maxValue) 5 | end 6 | 7 | rule "Check the minimum server configuration" 8 | dialect "mvel" 9 | when 10 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 11 | then 12 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 13 | retract($server); 14 | end -------------------------------------------------------------------------------- /chapter01/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | drools.cookbook 5 | chapter01 6 | Drools Expert :: The rule engine 7 | pom 8 | 1.0.0 9 | 10 | 11 | declaring-facts-in-the-engine 12 | declaring-facts-using-xml 13 | event-listener-logging 14 | timer-based-rules 15 | calendar-based-rules 16 | live-queries 17 | 18 | 19 | 20 | 21 | 22 | 23 | org.drools 24 | knowledge-api 25 | ${drools.version} 26 | 27 | 28 | org.drools 29 | drools-core 30 | ${drools.version} 31 | 32 | 33 | org.drools 34 | drools-compiler 35 | ${drools.version} 36 | 37 | 38 | 39 | junit 40 | junit 41 | 4.4 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-compiler-plugin 52 | 2.0.2 53 | 54 | 1.5 55 | 1.5 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 5.2.0.Final 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter01 7 | 1.0.0 8 | 9 | timer-based-rules 10 | Timer based rules 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/src/main/java/drools/cookbook/chapter01/ServerAlert.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class ServerAlert { 12 | 13 | private List events; 14 | 15 | public void addEvent(ServerEvent serverEvent) { 16 | getEvents().add(serverEvent); 17 | } 18 | 19 | public List getEvents() { 20 | if (events==null) { 21 | events = new ArrayList(); 22 | } 23 | return events; 24 | } 25 | 26 | public void setEvents(List events) { 27 | this.events = events; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/src/main/java/drools/cookbook/chapter01/ServerEvent.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class ServerEvent { 11 | 12 | private Server server; 13 | private Date time; 14 | private ServerStatus status; 15 | 16 | public ServerEvent(Server server, Date time, ServerStatus status) { 17 | this.server = server; 18 | this.time = time; 19 | this.setStatus(status); 20 | } 21 | 22 | public Server getServer() { 23 | return server; 24 | } 25 | 26 | public void setServer(Server server) { 27 | this.server = server; 28 | } 29 | 30 | public Date getTime() { 31 | return time; 32 | } 33 | 34 | public void setTime(Date time) { 35 | this.time = time; 36 | } 37 | 38 | public void setStatus(ServerStatus status) { 39 | this.status = status; 40 | } 41 | 42 | public ServerStatus getStatus() { 43 | return status; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/src/main/java/drools/cookbook/chapter01/ServerStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | public enum ServerStatus { 4 | 5 | ONLINE, 6 | OFFLINE, 7 | MAINTENANCE; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/src/main/java/drools/cookbook/chapter01/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private int memory; 12 | private int diskSpace; 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | public String getName() { 18 | return name; 19 | } 20 | public void setMemory(int memory) { 21 | this.memory = memory; 22 | } 23 | public int getMemory() { 24 | return memory; 25 | } 26 | public void setDiskSpace(int diskSpace) { 27 | this.diskSpace = diskSpace; 28 | } 29 | public int getDiskSpace() { 30 | return diskSpace; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /chapter01/timer-based-rules/src/test/resources/drools/cookbook/chapter01/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | global drools.cookbook.chapter01.ServerAlert alerts 7 | 8 | rule "Server status" 9 | dialect "mvel" 10 | timer (cron:0/5 * * * * ?) 11 | when 12 | $server : Server(online==false) 13 | then 14 | System.out.println("WARNING: Server \"" + $server.name + "\" is offline at " + $server.lastTimeOnline); 15 | alerts.addEvent(new ServerEvent($server, $server.lastTimeOnline, ServerStatus.OFFLINE)); 16 | end -------------------------------------------------------------------------------- /chapter02/duplicated-facts/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | duplicated-facts 10 | How discard duplicated facts 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter02/duplicated-facts/src/main/java/drools/cookbook/chapter02/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | public String getName() { 26 | return name; 27 | } 28 | public void setServerName(String serverName) { 29 | this.serverName = serverName; 30 | } 31 | public String getServerName() { 32 | return serverName; 33 | } 34 | public void setMemory(int memory) { 35 | this.memory = memory; 36 | } 37 | public int getMemory() { 38 | return memory; 39 | } 40 | public void setDiskSpace(int diskSpace) { 41 | this.diskSpace = diskSpace; 42 | } 43 | public int getDiskSpace() { 44 | return diskSpace; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /chapter02/duplicated-facts/src/main/java/drools/cookbook/chapter02/VirtualizationRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class VirtualizationRequest { 9 | 10 | private String serverName; 11 | private Virtualization virtualization; 12 | private boolean successful; 13 | private boolean processed; 14 | 15 | public VirtualizationRequest(Virtualization virtualization) { 16 | this.serverName = virtualization.getServerName(); 17 | this.virtualization = virtualization; 18 | } 19 | 20 | public void setServerName(String serverName) { 21 | this.serverName = serverName; 22 | } 23 | 24 | public String getServerName() { 25 | return serverName; 26 | } 27 | 28 | public void setVirtualization(Virtualization virtualization) { 29 | this.virtualization = virtualization; 30 | } 31 | 32 | public Virtualization getVirtualization() { 33 | return virtualization; 34 | } 35 | 36 | public void setSuccessful(boolean successful) { 37 | this.successful = successful; 38 | } 39 | 40 | public boolean isSuccessful() { 41 | return successful; 42 | } 43 | 44 | public void setProcessed(boolean processed) { 45 | this.processed = processed; 46 | } 47 | 48 | public boolean isProcessed() { 49 | return processed; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /chapter02/duplicated-facts/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02 2 | 3 | import java.util.Date 4 | import java.util.List 5 | import drools.cookbook.chapter02.Server 6 | import drools.cookbook.chapter02.Virtualization 7 | import drools.cookbook.chapter02.VirtualizationRequest 8 | 9 | rule "Check server alternatives with more availabe resources" 10 | salience 100 11 | dialect "mvel" 12 | when 13 | $request : VirtualizationRequest($serverName : serverName) 14 | $server : Server(name == $serverName) 15 | $anotherServer : Server(name != $serverName) 16 | $servers : List(size > 0) from collect(Server(name != $serverName, 17 | processors > $server.processors, 18 | memory > $server.memory, 19 | diskSpace > $server.diskSpace)) 20 | then 21 | System.out.println("Warning, the next servers have more available resources."); 22 | for (Object object : $servers) { 23 | Server server = (Server)object; 24 | System.out.println("Server: " + server); 25 | } 26 | $request.successful = false; 27 | retract($request); 28 | end 29 | 30 | rule "New virtualization request" 31 | dialect "mvel" 32 | when 33 | $request : VirtualizationRequest($serverName : serverName) 34 | $server : Server(name==$serverName) 35 | then 36 | System.out.println("New virtualization added on server " + $serverName); 37 | $server.virtualizations.add($request.virtualization); 38 | $request.successful = true; 39 | retract($request); 40 | end -------------------------------------------------------------------------------- /chapter02/knowledge-agent-classloader/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | knowledge-agent-classloader 10 | Knowledge Agent classloader 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter02/knowledge-agent-classloader/src/test/java/drools/cookbook/chapter02/KnowledgeAgentClassloaderTest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import java.net.URL; 6 | import java.net.URLClassLoader; 7 | 8 | import org.drools.KnowledgeBase; 9 | import org.drools.KnowledgeBaseConfiguration; 10 | import org.drools.KnowledgeBaseFactory; 11 | import org.drools.agent.KnowledgeAgent; 12 | import org.drools.agent.KnowledgeAgentConfiguration; 13 | import org.drools.agent.KnowledgeAgentFactory; 14 | import org.drools.builder.KnowledgeBuilderConfiguration; 15 | import org.drools.builder.KnowledgeBuilderFactory; 16 | import org.drools.io.impl.ClassPathResource; 17 | import org.drools.runtime.StatefulKnowledgeSession; 18 | import org.junit.Test; 19 | 20 | /** 21 | * 22 | * @author Lucas Amador 23 | * 24 | */ 25 | public class KnowledgeAgentClassloaderTest { 26 | 27 | @Test 28 | public void customClassloaderTest() { 29 | 30 | KnowledgeBase kbase = createKnowledgeBase(); 31 | 32 | assertTrue(kbase.getKnowledgePackages().size() == 1); 33 | 34 | StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); 35 | 36 | ksession.fireAllRules(); 37 | 38 | } 39 | 40 | private KnowledgeBase createKnowledgeBase() { 41 | 42 | URL modelJarURL = getClass().getResource("model.jar"); 43 | URLClassLoader customURLClassloader = new URLClassLoader(new URL[] { modelJarURL }); 44 | 45 | KnowledgeBuilderConfiguration kbuilderConfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, customURLClassloader); 46 | 47 | KnowledgeBaseConfiguration kbaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, customURLClassloader); 48 | KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConfig); 49 | 50 | KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(); 51 | KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("test", kbase, aconf, kbuilderConfig); 52 | 53 | kagent.applyChangeSet(new ClassPathResource("change-set.xml", getClass())); 54 | 55 | return kagent.getKnowledgeBase(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter02/knowledge-agent-classloader/src/test/resources/drools/cookbook/chapter02/change-set.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /chapter02/knowledge-agent-classloader/src/test/resources/drools/cookbook/chapter02/model.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lczmdr/drools-developers-cookbook-examples/bdc3b6fc884ef449a562383113d49bbbe61068cc/chapter02/knowledge-agent-classloader/src/test/resources/drools/cookbook/chapter02/model.jar -------------------------------------------------------------------------------- /chapter02/knowledge-agent-classloader/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02 2 | 3 | import java.util.Date 4 | import java.util.List 5 | import drools.cookbook.chapter02.Server 6 | import drools.cookbook.chapter02.Virtualization 7 | import drools.cookbook.chapter02.VirtualizationRequest 8 | 9 | rule "Check server alternatives with more availabe resources" 10 | salience 100 11 | dialect "mvel" 12 | when 13 | $request : VirtualizationRequest($serverName : serverName) 14 | $server : Server(name == $serverName) 15 | $anotherServer : Server(name != $serverName) 16 | $servers : List(size > 0) from collect(Server(name != $serverName, 17 | processors > $server.processors, 18 | memory > $server.memory, 19 | diskSpace > $server.diskSpace)) 20 | then 21 | System.out.println("Warning, the next servers have more available resources."); 22 | for (Object object : $servers) { 23 | Server server = (Server)object; 24 | System.out.println("Server: " + server); 25 | } 26 | $request.successful = false; 27 | retract($request); 28 | end 29 | 30 | rule "New virtualization request" 31 | dialect "mvel" 32 | when 33 | $request : VirtualizationRequest($serverName : serverName) 34 | $server : Server(name==$serverName) 35 | then 36 | System.out.println("New virtualization added on server " + $serverName); 37 | $server.virtualizations.add($request.virtualization); 38 | $request.successful = true; 39 | retract($request); 40 | end -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | knowledge-persistence 10 | Knowledge persistence 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-persistence-jpa 28 | 29 | 30 | org.codehaus.btm 31 | btm 32 | 1.3.3 33 | test 34 | 35 | 36 | com.h2database 37 | h2 38 | 1.2.128 39 | test 40 | 41 | 42 | org.slf4j 43 | slf4j-api 44 | 1.6.1 45 | 46 | 47 | org.slf4j 48 | slf4j-log4j12 49 | 1.6.1 50 | 51 | 52 | junit 53 | junit 54 | test 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/main/java/drools/cookbook/chapter02/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class Virtualization implements Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | private String name; 15 | private String serverName; 16 | private int memory; 17 | private int diskSpace; 18 | 19 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 20 | this.name = name; 21 | this.serverName = serverName; 22 | this.memory = memory; 23 | this.diskSpace = diskSpace; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setServerName(String serverName) { 35 | this.serverName = serverName; 36 | } 37 | 38 | public String getServerName() { 39 | return serverName; 40 | } 41 | 42 | public void setMemory(int memory) { 43 | this.memory = memory; 44 | } 45 | 46 | public int getMemory() { 47 | return memory; 48 | } 49 | 50 | public void setDiskSpace(int diskSpace) { 51 | this.diskSpace = diskSpace; 52 | } 53 | 54 | public int getDiskSpace() { 55 | return diskSpace; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/main/java/drools/cookbook/chapter02/VirtualizationRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class VirtualizationRequest { 11 | 12 | private String serverName; 13 | private Virtualization virtualization; 14 | private boolean successful; 15 | 16 | public VirtualizationRequest(Virtualization virtualization) { 17 | this.serverName = virtualization.getServerName(); 18 | this.virtualization = virtualization; 19 | } 20 | 21 | public void setServerName(String serverName) { 22 | this.serverName = serverName; 23 | } 24 | 25 | public String getServerName() { 26 | return serverName; 27 | } 28 | 29 | public void setVirtualization(Virtualization virtualization) { 30 | this.virtualization = virtualization; 31 | } 32 | 33 | public Virtualization getVirtualization() { 34 | return virtualization; 35 | } 36 | 37 | public void setSuccessful(boolean successful) { 38 | this.successful = successful; 39 | } 40 | 41 | public boolean isSuccessful() { 42 | return successful; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/test/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | org.hibernate.ejb.HibernatePersistence 8 | jdbc/testDatasource 9 | org.drools.persistence.info.SessionInfo 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | import drools.cookbook.chapter02.Server 6 | import drools.cookbook.chapter02.Virtualization 7 | import drools.cookbook.chapter02.VirtualizationRequest 8 | 9 | rule "New virtualization request" 10 | when 11 | $request : VirtualizationRequest($serverName : serverName) 12 | $server : Server(name==$serverName) 13 | then 14 | System.out.println("New virtualization added on server " + $serverName); 15 | $server.getVirtualizations().add($request.getVirtualization()); 16 | $request.setSuccessful(true); 17 | retract($request); 18 | end -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/test/resources/jndi.properties: -------------------------------------------------------------------------------- 1 | java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory -------------------------------------------------------------------------------- /chapter02/knowledge-persistence/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /chapter02/marshalling-knowledge-sessions/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | marshalling-knowledge-sessions 10 | Marshalling knowledge sessions 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter02/marshalling-knowledge-sessions/src/main/java/drools/cookbook/chapter02/virtualization/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02.virtualization; 2 | 3 | 4 | /** 5 | * 6 | * @author Lucas Amador 7 | * 8 | */ 9 | public class Virtualization { 10 | 11 | private String name; 12 | private String serverName; 13 | private int memory; 14 | private int diskSpace; 15 | 16 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 17 | this.name = name; 18 | this.serverName = serverName; 19 | this.memory = memory; 20 | this.diskSpace = diskSpace; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | public String getName() { 27 | return name; 28 | } 29 | public void setServerName(String serverName) { 30 | this.serverName = serverName; 31 | } 32 | public String getServerName() { 33 | return serverName; 34 | } 35 | public void setMemory(int memory) { 36 | this.memory = memory; 37 | } 38 | public int getMemory() { 39 | return memory; 40 | } 41 | public void setDiskSpace(int diskSpace) { 42 | this.diskSpace = diskSpace; 43 | } 44 | public int getDiskSpace() { 45 | return diskSpace; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /chapter02/marshalling-knowledge-sessions/src/main/java/drools/cookbook/chapter02/virtualization/VirtualizationRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02.virtualization; 2 | 3 | 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class VirtualizationRequest { 11 | 12 | private String serverName; 13 | private Virtualization virtualization; 14 | private boolean successful; 15 | 16 | public VirtualizationRequest(Virtualization virtualization) { 17 | this.serverName = virtualization.getServerName(); 18 | this.virtualization = virtualization; 19 | } 20 | 21 | public void setServerName(String serverName) { 22 | this.serverName = serverName; 23 | } 24 | 25 | public String getServerName() { 26 | return serverName; 27 | } 28 | 29 | public void setVirtualization(Virtualization virtualization) { 30 | this.virtualization = virtualization; 31 | } 32 | 33 | public Virtualization getVirtualization() { 34 | return virtualization; 35 | } 36 | 37 | public void setSuccessful(boolean successful) { 38 | this.successful = successful; 39 | } 40 | 41 | public boolean isSuccessful() { 42 | return successful; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /chapter02/marshalling-knowledge-sessions/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import java.util.Date 4 | import java.util.List 5 | import drools.cookbook.chapter02.Server 6 | import drools.cookbook.chapter02.virtualization.Virtualization 7 | import drools.cookbook.chapter02.virtualization.VirtualizationRequest 8 | 9 | rule "New virtualization request" 10 | when 11 | $request : VirtualizationRequest($serverName : serverName) 12 | $server : Server(name==$serverName) 13 | then 14 | System.out.println("New virtualization added on server " + $serverName); 15 | $server.getVirtualizations().add($request.getVirtualization()); 16 | $request.setSuccessful(true); 17 | retract($request); 18 | end -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | monitoring-knowledge-metrics 10 | Monitoring knowledge metrics 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/main/java/drools/cookbook/chapter02/Service.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Service { 9 | 10 | private String serviceName; 11 | private ServiceType type; 12 | 13 | public Service(String serviceName, ServiceType type) { 14 | this.serviceName = serviceName; 15 | this.type = type; 16 | } 17 | 18 | public void setServiceName(String serviceName) { 19 | this.serviceName = serviceName; 20 | } 21 | 22 | public String getServiceName() { 23 | return serviceName; 24 | } 25 | 26 | public void setType(ServiceType type) { 27 | this.type = type; 28 | } 29 | 30 | public ServiceType getType() { 31 | return type; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/main/java/drools/cookbook/chapter02/ServiceRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class ServiceRequest { 9 | 10 | private String serverName; 11 | private String serviceName; 12 | private ServiceType serviceType; 13 | 14 | public void setServerName(String serverName) { 15 | this.serverName = serverName; 16 | } 17 | public String getServerName() { 18 | return serverName; 19 | } 20 | public void setServiceName(String serviceName) { 21 | this.serviceName = serviceName; 22 | } 23 | public String getServiceName() { 24 | return serviceName; 25 | } 26 | public void setServiceType(ServiceType serviceType) { 27 | this.serviceType = serviceType; 28 | } 29 | public ServiceType getServiceType() { 30 | return serviceType; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "ServiceRequest[serverName=" + serverName + " serviceName=" + serviceName + " serviceType=" + serviceType + "]"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/main/java/drools/cookbook/chapter02/ServiceRequestFactory.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import java.util.Random; 4 | 5 | public class ServiceRequestFactory { 6 | 7 | private static final String[] serverNames = {"debianServer", "ubuntuServer"}; 8 | private static final String[] ftpServicesNames = new String[]{"ftp.server01.com","ftp.server02.com"}; 9 | private static final String[] httpServicesNames = new String[]{"http.server01.com"}; 10 | private static Random rnd = new Random(); 11 | 12 | public static ServiceRequest create() { 13 | ServiceRequest serviceRequest = new ServiceRequest(); 14 | int serverNameIndex = rnd.nextInt(serverNames.length); 15 | serviceRequest.setServerName(serverNames[serverNameIndex]); 16 | int serviceType = rnd.nextInt(2); 17 | if (serviceType==0) { 18 | serviceRequest.setServiceType(ServiceType.FTP); 19 | int ftpServicesIndex = rnd.nextInt(ftpServicesNames.length); 20 | serviceRequest.setServiceName(ftpServicesNames[ftpServicesIndex]); 21 | } 22 | else if (serviceType==1) { 23 | serviceRequest.setServiceType(ServiceType.HTTP); 24 | int httpServicesIndex = rnd.nextInt(httpServicesNames.length); 25 | serviceRequest.setServiceName(httpServicesNames[httpServicesIndex]); 26 | } 27 | return serviceRequest; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/main/java/drools/cookbook/chapter02/ServiceType.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public enum ServiceType { 9 | 10 | HTTP, 11 | FTP, 12 | SSH; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/main/java/drools/cookbook/chapter02/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | /** 8 | * 9 | * @author Lucas Amador 10 | * 11 | */ 12 | public class Virtualization { 13 | 14 | private String name; 15 | private String serverName; 16 | private int memory; 17 | private int diskSpace; 18 | private List services = new ArrayList(); 19 | 20 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 21 | this.name = name; 22 | this.serverName = serverName; 23 | this.memory = memory; 24 | this.diskSpace = diskSpace; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | public String getName() { 31 | return name; 32 | } 33 | public void setServerName(String serverName) { 34 | this.serverName = serverName; 35 | } 36 | public String getServerName() { 37 | return serverName; 38 | } 39 | public void setMemory(int memory) { 40 | this.memory = memory; 41 | } 42 | public int getMemory() { 43 | return memory; 44 | } 45 | public void setDiskSpace(int diskSpace) { 46 | this.diskSpace = diskSpace; 47 | } 48 | public int getDiskSpace() { 49 | return diskSpace; 50 | } 51 | 52 | public void setServices(List services) { 53 | this.services = services; 54 | } 55 | 56 | public List getServices() { 57 | return services; 58 | } 59 | 60 | public void addService(Service service) { 61 | this.services.add(service); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /chapter02/monitoring-knowledge-metrics/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | 4 | import drools.cookbook.chapter02.* 5 | import java.util.Date 6 | import java.util.List 7 | 8 | rule "check minimum server configuration" 9 | dialect "mvel" 10 | salience 100 11 | when 12 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 13 | then 14 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 15 | retract($server); 16 | end 17 | 18 | rule "service request" 19 | dialect "mvel" 20 | when 21 | $sr : ServiceRequest(serverName!=null, serviceName!=null, serviceType!=null) 22 | $server : Server(name==$sr.serverName) 23 | // $vir : List(size > 0) from collect(Virtualization () from $server.virtualizations) 24 | // List(size > 0) from collect(Service() from $vir.services) 25 | then 26 | System.out.println("service request " + $sr); 27 | retract($sr); 28 | end -------------------------------------------------------------------------------- /chapter02/verifying-rules/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter02 7 | 1.0.0 8 | 9 | verifying-rules 10 | Verifying rules with Verifier 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-verifier 28 | 29 | 30 | junit 31 | junit 32 | test 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/main/java/drools/cookbook/chapter02/Service.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Service { 9 | 10 | private String serviceName; 11 | private ServiceType type; 12 | 13 | public Service(String serviceName, ServiceType type) { 14 | this.serviceName = serviceName; 15 | this.type = type; 16 | } 17 | 18 | public void setServiceName(String serviceName) { 19 | this.serviceName = serviceName; 20 | } 21 | 22 | public String getServiceName() { 23 | return serviceName; 24 | } 25 | 26 | public void setType(ServiceType type) { 27 | this.type = type; 28 | } 29 | 30 | public ServiceType getType() { 31 | return type; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/main/java/drools/cookbook/chapter02/ServiceRequest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class ServiceRequest { 9 | 10 | private String serverName; 11 | private String serviceName; 12 | private ServiceType serviceType; 13 | 14 | public void setServerName(String serverName) { 15 | this.serverName = serverName; 16 | } 17 | public String getServerName() { 18 | return serverName; 19 | } 20 | public void setServiceName(String serviceName) { 21 | this.serviceName = serviceName; 22 | } 23 | public String getServiceName() { 24 | return serviceName; 25 | } 26 | public void setServiceType(ServiceType serviceType) { 27 | this.serviceType = serviceType; 28 | } 29 | public ServiceType getServiceType() { 30 | return serviceType; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "ServiceRequest[serverName=" + serverName + " serviceName=" + serviceName + " serviceType=" + serviceType + "]"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/main/java/drools/cookbook/chapter02/ServiceRequestFactory.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import java.util.Random; 4 | 5 | public class ServiceRequestFactory { 6 | 7 | private static final String[] serverNames = {"debianServer", "ubuntuServer"}; 8 | private static final String[] ftpServicesNames = new String[]{"ftp.server01.com","ftp.server02.com"}; 9 | private static final String[] httpServicesNames = new String[]{"http.server01.com"}; 10 | private static Random rnd = new Random(); 11 | 12 | public static ServiceRequest create() { 13 | ServiceRequest serviceRequest = new ServiceRequest(); 14 | int serverNameIndex = rnd.nextInt(serverNames.length); 15 | serviceRequest.setServerName(serverNames[serverNameIndex]); 16 | int serviceType = rnd.nextInt(2); 17 | if (serviceType==0) { 18 | serviceRequest.setServiceType(ServiceType.FTP); 19 | int ftpServicesIndex = rnd.nextInt(ftpServicesNames.length); 20 | serviceRequest.setServiceName(ftpServicesNames[ftpServicesIndex]); 21 | } 22 | else if (serviceType==1) { 23 | serviceRequest.setServiceType(ServiceType.HTTP); 24 | int httpServicesIndex = rnd.nextInt(httpServicesNames.length); 25 | serviceRequest.setServiceName(httpServicesNames[httpServicesIndex]); 26 | } 27 | return serviceRequest; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/main/java/drools/cookbook/chapter02/ServiceType.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public enum ServiceType { 9 | 10 | HTTP, 11 | FTP, 12 | SSH; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/main/java/drools/cookbook/chapter02/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter02; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | /** 8 | * 9 | * @author Lucas Amador 10 | * 11 | */ 12 | public class Virtualization { 13 | 14 | private String name; 15 | private String serverName; 16 | private int memory; 17 | private int diskSpace; 18 | private List services = new ArrayList(); 19 | 20 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 21 | this.name = name; 22 | this.serverName = serverName; 23 | this.memory = memory; 24 | this.diskSpace = diskSpace; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | public String getName() { 31 | return name; 32 | } 33 | public void setServerName(String serverName) { 34 | this.serverName = serverName; 35 | } 36 | public String getServerName() { 37 | return serverName; 38 | } 39 | public void setMemory(int memory) { 40 | this.memory = memory; 41 | } 42 | public int getMemory() { 43 | return memory; 44 | } 45 | public void setDiskSpace(int diskSpace) { 46 | this.diskSpace = diskSpace; 47 | } 48 | public int getDiskSpace() { 49 | return diskSpace; 50 | } 51 | 52 | public void setServices(List services) { 53 | this.services = services; 54 | } 55 | 56 | public List getServices() { 57 | return services; 58 | } 59 | 60 | public void addService(Service service) { 61 | this.services.add(service); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /chapter02/verifying-rules/src/test/resources/drools/cookbook/chapter02/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter01 2 | 3 | import drools.cookbook.chapter02.Server 4 | import java.util.Date 5 | import java.util.List 6 | 7 | rule "rule 1" 8 | when 9 | $server : Server(processors==2, memory==1024) 10 | then 11 | retract($server); 12 | end 13 | 14 | rule "rule 2" 15 | when 16 | $server : Server(processors==2, processors!=2) 17 | then 18 | retract($server); 19 | end 20 | 21 | rule "rule 3" 22 | when 23 | $server : Server(processors==2, memory==1024) 24 | then 25 | retract($server); 26 | end 27 | -------------------------------------------------------------------------------- /chapter04/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | drools.cookbook 5 | chapter04 6 | Guvnor 7 | Drools Guvnor 8 | pom 9 | 1.0.0 10 | 11 | 12 | rest-api 13 | 14 | 15 | 16 | 17 | 18 | 19 | org.drools 20 | knowledge-api 21 | ${drools.version} 22 | 23 | 24 | org.drools 25 | drools-compiler 26 | ${drools.version} 27 | 28 | 29 | org.drools 30 | drools-core 31 | ${drools.version} 32 | 33 | 34 | 35 | junit 36 | junit 37 | 4.4 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 2.0.2 49 | 50 | 1.5 51 | 1.5 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 5.2.0.Final 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /chapter04/rest-api/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter04 7 | 1.0.0 8 | 9 | rest-api 10 | Guvnor REST API 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-compiler 20 | 21 | 22 | org.drools 23 | drools-core 24 | 25 | 26 | javax.ws.rs 27 | jsr311-api 28 | 1.1.1 29 | jar 30 | 31 | 32 | javax.xml.bind 33 | jaxb-api 34 | 2.2.1 35 | jar 36 | 37 | 38 | com.sun.xml.bind 39 | jaxb-impl 40 | jar 41 | 2.2.1.1 42 | 43 | 44 | junit 45 | junit 46 | test 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /chapter04/rest-api/src/main/java/org/drools/guvnor/api/GuvnorRestApi.java: -------------------------------------------------------------------------------- 1 | package org.drools.guvnor.api; 2 | 3 | import java.io.InputStream; 4 | import java.net.HttpURLConnection; 5 | import java.net.URL; 6 | 7 | import javax.ws.rs.core.MediaType; 8 | 9 | /** 10 | * 11 | * @author Lucas Amador 12 | * 13 | */ 14 | public class GuvnorRestApi { 15 | 16 | private String guvnorURI; 17 | 18 | public GuvnorRestApi(String guvnorURI) { 19 | this.guvnorURI = guvnorURI; 20 | } 21 | 22 | public InputStream getBinaryPackage(String packageName) throws Exception { 23 | URL url = new URL(guvnorURI + "/rest/packages/" + packageName + "/binary"); 24 | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 25 | connection.setRequestMethod("GET"); 26 | connection.setRequestProperty("Accept", MediaType.APPLICATION_OCTET_STREAM); 27 | connection.connect(); 28 | if (connection.getResponseCode() != 200) { 29 | throw new Exception("Bad response code: " + connection.getResponseCode()); 30 | } 31 | if (!connection.getContentType().equalsIgnoreCase(MediaType.APPLICATION_OCTET_STREAM)) { 32 | throw new Exception("Bad response content type: " + connection.getContentType()); 33 | } 34 | return connection.getInputStream(); 35 | } 36 | 37 | public String getGuvnorURI() { 38 | return guvnorURI; 39 | } 40 | 41 | public void setGuvnorURI(String guvnorURI) { 42 | this.guvnorURI = guvnorURI; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /chapter05/entry-points/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter05 7 | 1.0.0 8 | 9 | entry-points 10 | Entry Points 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter05/entry-points/src/main/java/drools/cookbook/helper/FlighSimulation.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | import drools.cookbook.model.FlightStatus; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlighSimulation { 11 | 12 | private static final int DISTANCE = 50; 13 | 14 | private final String flight; 15 | private final String origin; 16 | private final String destination; 17 | private long distance; 18 | 19 | public FlighSimulation(String flight, String origin, String destination, long distance) { 20 | this.flight = flight; 21 | this.origin = origin; 22 | this.destination = destination; 23 | this.distance = distance; 24 | } 25 | 26 | public FlightStatus update() { 27 | FlightStatus flightStatus = new FlightStatus(); 28 | flightStatus.setFlight(flight); 29 | flightStatus.setDestination(destination); 30 | flightStatus.setOrigin(origin); 31 | flightStatus.setDestination(destination); 32 | this.distance = calculateDistance(); 33 | flightStatus.setDistance(this.distance); 34 | return flightStatus; 35 | } 36 | 37 | private long calculateDistance() { 38 | return ((distance - DISTANCE) <= 0) ? 0 : distance - DISTANCE; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /chapter05/entry-points/src/main/java/drools/cookbook/model/FlightControl.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class FlightControl { 12 | 13 | private String airport; 14 | private Map flights = new HashMap(); 15 | 16 | public void setAirport(String airport) { 17 | this.airport = airport; 18 | } 19 | 20 | public String getAirport() { 21 | return airport; 22 | } 23 | 24 | public void setFlights(Map flights) { 25 | this.flights = flights; 26 | } 27 | 28 | public Map getFlights() { 29 | return flights; 30 | } 31 | 32 | public void addFlight(FlightStatus flightStatus) { 33 | flights.put(flightStatus.getFlight(), flightStatus); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/entry-points/src/main/java/drools/cookbook/model/FlightStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlightStatus { 11 | 12 | private String flight; 13 | private Date timestamp; 14 | private String origin; 15 | private String destination; 16 | private long distance; 17 | 18 | public String getFlight() { 19 | return flight; 20 | } 21 | 22 | public void setFlight(String flight) { 23 | this.flight = flight; 24 | } 25 | 26 | public Date getTimestamp() { 27 | return timestamp; 28 | } 29 | 30 | public void setTimestamp(Date timestamp) { 31 | this.timestamp = timestamp; 32 | } 33 | 34 | public String getOrigin() { 35 | return origin; 36 | } 37 | 38 | public void setOrigin(String origin) { 39 | this.origin = origin; 40 | } 41 | 42 | public String getDestination() { 43 | return destination; 44 | } 45 | 46 | public void setDestination(String destination) { 47 | this.destination = destination; 48 | } 49 | 50 | public long getDistance() { 51 | return distance; 52 | } 53 | 54 | public void setDistance(long distance) { 55 | this.distance = distance; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter05/entry-points/src/main/resources/drools/cookbook/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook; 2 | 3 | import drools.cookbook.model.FlightStatus 4 | import drools.cookbook.model.FlightControl 5 | 6 | global drools.cookbook.model.FlightControl control; 7 | 8 | declare FlightStatus 9 | @role(event) 10 | end 11 | 12 | rule "First contact" 13 | salience 100 14 | when 15 | $currentFlight : FlightStatus() from entry-point "flight-control" 16 | not (exists (FlightStatus(this != $currentFlight, flight == $currentFlight.flight) from entry-point "flight-control")) 17 | then 18 | control.addFlight($currentFlight); 19 | System.out.println("First contact with Flight " + $currentFlight.getFlight()); 20 | end 21 | 22 | 23 | rule "flight arrival" 24 | when 25 | $flight : FlightStatus() from entry-point "flight-arrival" 26 | // Obtain resources to prepare the flight landing 27 | then 28 | System.out.println("Flight " + $flight.getFlight() + " arriving to " + control.getAirport() + ". Sending instructions"); 29 | // Send instructions to arriving flight 30 | end -------------------------------------------------------------------------------- /chapter05/event-declaration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter05 7 | 1.0.0 8 | 9 | event-declaration 10 | Event declaration 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter05/event-declaration/src/main/java/drools/cookbook/model/FlightControl.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class FlightControl { 12 | 13 | private String airport; 14 | private Map flights = new HashMap(); 15 | 16 | public void setAirport(String airport) { 17 | this.airport = airport; 18 | } 19 | 20 | public String getAirport() { 21 | return airport; 22 | } 23 | 24 | public void setFlights(Map flights) { 25 | this.flights = flights; 26 | } 27 | 28 | public Map getFlights() { 29 | return flights; 30 | } 31 | 32 | public void addFlight(FlightStatus flightStatus) { 33 | flights.put(flightStatus.getFlight(), flightStatus); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/event-declaration/src/main/java/drools/cookbook/model/FlightStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlightStatus { 11 | 12 | private String flight; 13 | private Date timestamp; 14 | private String origin; 15 | private String destination; 16 | private long distance; 17 | 18 | public String getFlight() { 19 | return flight; 20 | } 21 | 22 | public void setFlight(String flight) { 23 | this.flight = flight; 24 | } 25 | 26 | public Date getTimestamp() { 27 | return timestamp; 28 | } 29 | 30 | public void setTimestamp(Date timestamp) { 31 | this.timestamp = timestamp; 32 | } 33 | 34 | public String getOrigin() { 35 | return origin; 36 | } 37 | 38 | public void setOrigin(String origin) { 39 | this.origin = origin; 40 | } 41 | 42 | public String getDestination() { 43 | return destination; 44 | } 45 | 46 | public void setDestination(String destination) { 47 | this.destination = destination; 48 | } 49 | 50 | public long getDistance() { 51 | return distance; 52 | } 53 | 54 | public void setDistance(long distance) { 55 | this.distance = distance; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter05/event-declaration/src/main/resources/drools/cookbook/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook; 2 | 3 | import drools.cookbook.model.FlightStatus 4 | import drools.cookbook.model.FlightControl 5 | 6 | global drools.cookbook.model.FlightControl control; 7 | 8 | declare FlightStatus 9 | @role(event) 10 | @timestamp(timestamp) 11 | end 12 | 13 | rule "First contact" 14 | salience 100 15 | when 16 | $currentFlight : FlightStatus() 17 | not (exists (FlightStatus(this != $currentFlight, flight == $currentFlight.flight))) 18 | then 19 | control.addFlight($currentFlight); 20 | System.out.println("First contact with Flight " + $currentFlight.getFlight()); 21 | end 22 | -------------------------------------------------------------------------------- /chapter05/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | drools.cookbook 5 | chapter05 6 | Drools Fusion :: Processing Complex Events 7 | pom 8 | 1.0.0 9 | 10 | 11 | event-declaration 12 | entry-points 13 | pseudo-clock 14 | sliding-windows 15 | temporal-operators 16 | 17 | 18 | 19 | 20 | 21 | 22 | org.drools 23 | knowledge-api 24 | ${drools.version} 25 | 26 | 27 | org.drools 28 | drools-core 29 | ${drools.version} 30 | 31 | 32 | org.drools 33 | drools-compiler 34 | ${drools.version} 35 | 36 | 37 | 38 | junit 39 | junit 40 | 4.4 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.apache.maven.plugins 50 | maven-compiler-plugin 51 | 2.0.2 52 | 53 | 1.5 54 | 1.5 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 5.2.0.Final 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter05 7 | 1.0.0 8 | 9 | pseudo-clock 10 | How test our application using the pseudo clock 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/java/drools/cookbook/helper/EmergencySystem.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class EmergencySystem { 9 | 10 | public static void sendFireTrucks() { 11 | System.out.println("-> Fire trucks sended"); 12 | } 13 | 14 | public static void sendAmbulances() { 15 | System.out.println("-> Ambulances sended"); 16 | } 17 | 18 | public static void cleanRunways() { 19 | System.out.println("-> Cleaning runways in progress"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/java/drools/cookbook/helper/FlighSimulation.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | import drools.cookbook.model.FlightStatus; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlighSimulation { 11 | 12 | private static final int AIRPORT_AIR_SPACE = 50; 13 | 14 | private final String flight; 15 | private final String origin; 16 | private final String destination; 17 | private long distance; 18 | private boolean landed; 19 | 20 | public FlighSimulation(String flight, String origin, String destination, long distance) { 21 | this.flight = flight; 22 | this.origin = origin; 23 | this.destination = destination; 24 | this.distance = distance; 25 | } 26 | 27 | public FlightStatus update() { 28 | FlightStatus flightStatus = new FlightStatus(); 29 | flightStatus.setFlight(flight); 30 | flightStatus.setDestination(destination); 31 | flightStatus.setOrigin(origin); 32 | flightStatus.setDestination(destination); 33 | this.distance = calculateDistance(); 34 | flightStatus.setDistance(this.distance); 35 | return flightStatus; 36 | } 37 | 38 | private long calculateDistance() { 39 | if ((distance - AIRPORT_AIR_SPACE) <= AIRPORT_AIR_SPACE) { 40 | landed = true; 41 | return 0; 42 | } 43 | return distance - AIRPORT_AIR_SPACE; 44 | } 45 | 46 | public boolean isLanded() { 47 | return landed; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/java/drools/cookbook/model/EmergencySignal.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class EmergencySignal { 9 | 10 | private String flight; 11 | private long distance; 12 | 13 | public EmergencySignal(String flight, long distance) { 14 | this.flight = flight; 15 | this.distance = distance; 16 | } 17 | 18 | public String getFlight() { 19 | return flight; 20 | } 21 | 22 | public void setFlight(String flight) { 23 | this.flight = flight; 24 | } 25 | 26 | public long getDistance() { 27 | return distance; 28 | } 29 | 30 | public void setDistance(long distance) { 31 | this.distance = distance; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/java/drools/cookbook/model/FlightControl.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class FlightControl { 12 | 13 | private String airport; 14 | private Map flights = new HashMap(); 15 | 16 | public void setAirport(String airport) { 17 | this.airport = airport; 18 | } 19 | 20 | public String getAirport() { 21 | return airport; 22 | } 23 | 24 | public void setFlights(Map flights) { 25 | this.flights = flights; 26 | } 27 | 28 | public Map getFlights() { 29 | return flights; 30 | } 31 | 32 | public void addFlight(FlightStatus flightStatus) { 33 | flights.put(flightStatus.getFlight(), flightStatus); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/java/drools/cookbook/model/FlightStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlightStatus { 11 | 12 | private String flight; 13 | private Date timestamp; 14 | private String origin; 15 | private String destination; 16 | private long distance; 17 | 18 | public String getFlight() { 19 | return flight; 20 | } 21 | 22 | public void setFlight(String flight) { 23 | this.flight = flight; 24 | } 25 | 26 | public Date getTimestamp() { 27 | return timestamp; 28 | } 29 | 30 | public void setTimestamp(Date timestamp) { 31 | this.timestamp = timestamp; 32 | } 33 | 34 | public String getOrigin() { 35 | return origin; 36 | } 37 | 38 | public void setOrigin(String origin) { 39 | this.origin = origin; 40 | } 41 | 42 | public String getDestination() { 43 | return destination; 44 | } 45 | 46 | public void setDestination(String destination) { 47 | this.destination = destination; 48 | } 49 | 50 | public long getDistance() { 51 | return distance; 52 | } 53 | 54 | public void setDistance(long distance) { 55 | this.distance = distance; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter05/pseudo-clock/src/main/resources/drools/cookbook/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook; 2 | 3 | import drools.cookbook.model.FlightStatus 4 | import drools.cookbook.model.FlightControl 5 | import drools.cookbook.model.EmergencySignal 6 | 7 | import drools.cookbook.helper.EmergencySystem 8 | 9 | global drools.cookbook.model.FlightControl control; 10 | 11 | declare FlightStatus 12 | @role(event) 13 | end 14 | 15 | declare EmergencySignal 16 | @role(event) 17 | end 18 | 19 | rule "First contact" 20 | salience 100 21 | when 22 | $currentFlight : FlightStatus() from entry-point "flight-control" 23 | not (exists (FlightStatus(this != $currentFlight, flight == $currentFlight.flight) from entry-point "flight-control")) 24 | then 25 | control.addFlight($currentFlight); 26 | System.out.println("First contact with Flight " + $currentFlight.getFlight()); 27 | end 28 | 29 | 30 | rule "flight arrival" 31 | when 32 | $flight : FlightStatus() from entry-point "flight-arrival" 33 | // Obtain resources to prepare the flight landing 34 | then 35 | System.out.println("Flight " + $flight.getFlight() + " arriving to " + control.getAirport() + ". Sending instructions"); 36 | // Send instructions to arriving flight 37 | end 38 | 39 | rule "emergency system" 40 | when 41 | $emergency : EmergencySignal() from entry-point "emergency-channel" 42 | FlightStatus(flight==$emergency.flight, this before[0m, 10m] $emergency) from entry-point "flight-arrival" 43 | then 44 | // Emergency in curse 45 | System.out.println("Emergency in progress. Flight " + $emergency.getFlight()); 46 | EmergencySystem.sendFireTrucks(); 47 | EmergencySystem.sendAmbulances(); 48 | EmergencySystem.cleanRunways(); 49 | end -------------------------------------------------------------------------------- /chapter05/sliding-windows/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter05 7 | 1.0.0 8 | 9 | sliding-windows 10 | Sliding windows 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/java/drools/cookbook/helper/FlighSimulation.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | import java.util.Random; 4 | 5 | import drools.cookbook.model.FlightStatus; 6 | 7 | /** 8 | * 9 | * @author Lucas Amador 10 | * 11 | */ 12 | public class FlighSimulation { 13 | 14 | private static final int AIRPORT_AIR_SPACE = 50; 15 | private Random rnd = new Random(); 16 | 17 | private final String flight; 18 | private final String origin; 19 | private final String destination; 20 | private long distance; 21 | private boolean landed; 22 | 23 | public FlighSimulation(String flight, String origin, String destination, long distance) { 24 | this.flight = flight; 25 | this.origin = origin; 26 | this.destination = destination; 27 | this.distance = distance; 28 | } 29 | 30 | public FlightStatus update() { 31 | FlightStatus flightStatus = new FlightStatus(); 32 | flightStatus.setFlight(flight); 33 | flightStatus.setDestination(destination); 34 | flightStatus.setOrigin(origin); 35 | flightStatus.setDestination(destination); 36 | this.distance = calculateDistance(); 37 | flightStatus.setDistance(this.distance); 38 | flightStatus.setSpeed(currentSpeed()); 39 | System.out.println("speed: " + flightStatus.getSpeed()); 40 | return flightStatus; 41 | } 42 | 43 | private long calculateDistance() { 44 | if ((distance - AIRPORT_AIR_SPACE) <= AIRPORT_AIR_SPACE) { 45 | landed = true; 46 | return 0; 47 | } 48 | return distance - AIRPORT_AIR_SPACE; 49 | } 50 | 51 | private float currentSpeed() { 52 | return (rnd.nextFloat() * 10) + 600; 53 | } 54 | 55 | public boolean isLanded() { 56 | return landed; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/java/drools/cookbook/model/EmergencyInCourse.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class EmergencyInCourse { 9 | 10 | public static void sendFireTrucks() { 11 | System.out.println("-> Fire trucks sended"); 12 | } 13 | 14 | public static void sendAmbulances() { 15 | System.out.println("-> Ambulances sended"); 16 | } 17 | 18 | public static void cleanRunways() { 19 | System.out.println("-> Cleaning runways in progress"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/java/drools/cookbook/model/EmergencySignal.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class EmergencySignal { 9 | 10 | private String flight; 11 | private long distance; 12 | 13 | public EmergencySignal(String flight, long distance) { 14 | this.flight = flight; 15 | this.distance = distance; 16 | } 17 | 18 | public String getFlight() { 19 | return flight; 20 | } 21 | 22 | public void setFlight(String flight) { 23 | this.flight = flight; 24 | } 25 | 26 | public long getDistance() { 27 | return distance; 28 | } 29 | 30 | public void setDistance(long distance) { 31 | this.distance = distance; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/java/drools/cookbook/model/FlightControl.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class FlightControl { 12 | 13 | private String airport; 14 | private Map flights = new HashMap(); 15 | 16 | public void setAirport(String airport) { 17 | this.airport = airport; 18 | } 19 | 20 | public String getAirport() { 21 | return airport; 22 | } 23 | 24 | public void setFlights(Map flights) { 25 | this.flights = flights; 26 | } 27 | 28 | public Map getFlights() { 29 | return flights; 30 | } 31 | 32 | public void addFlight(FlightStatus flightStatus) { 33 | flights.put(flightStatus.getFlight(), flightStatus); 34 | } 35 | 36 | public void update(FlightStatus flightStatus) { 37 | flights.put(flightStatus.getFlight(), flightStatus); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/java/drools/cookbook/model/FlightStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlightStatus { 11 | 12 | private String flight; 13 | private Date timestamp; 14 | private String origin; 15 | private String destination; 16 | private long distance; 17 | private float speed; 18 | private boolean processed; 19 | 20 | public String getFlight() { 21 | return flight; 22 | } 23 | 24 | public void setFlight(String flight) { 25 | this.flight = flight; 26 | } 27 | 28 | public Date getTimestamp() { 29 | return timestamp; 30 | } 31 | 32 | public void setTimestamp(Date timestamp) { 33 | this.timestamp = timestamp; 34 | } 35 | 36 | public String getOrigin() { 37 | return origin; 38 | } 39 | 40 | public void setOrigin(String origin) { 41 | this.origin = origin; 42 | } 43 | 44 | public String getDestination() { 45 | return destination; 46 | } 47 | 48 | public void setDestination(String destination) { 49 | this.destination = destination; 50 | } 51 | 52 | public long getDistance() { 53 | return distance; 54 | } 55 | 56 | public void setDistance(long distance) { 57 | this.distance = distance; 58 | } 59 | 60 | public float getSpeed() { 61 | return speed; 62 | } 63 | 64 | public void setSpeed(float speed) { 65 | this.speed = speed; 66 | } 67 | 68 | public void setProcessed(boolean processed) { 69 | this.processed = processed; 70 | } 71 | 72 | public boolean getProcessed() { 73 | return processed; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /chapter05/sliding-windows/src/main/resources/drools/cookbook/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook; 2 | 3 | import drools.cookbook.model.FlightStatus 4 | import drools.cookbook.model.FlightControl 5 | import drools.cookbook.model.EmergencySignal 6 | 7 | declare FlightStatus 8 | @role(event) 9 | end 10 | 11 | declare EmergencySignal 12 | @role(event) 13 | end 14 | 15 | rule "First contact" 16 | salience 100 17 | when 18 | $currentFlight : FlightStatus() from entry-point "flight-control" 19 | not (exists (FlightStatus(this != $currentFlight, flight == $currentFlight.flight) from entry-point "flight-control")) 20 | $control : FlightControl() 21 | then 22 | $control.addFlight($currentFlight); 23 | System.out.println("First contact with Flight " + $currentFlight.getFlight()); 24 | end 25 | 26 | rule "flight arrival" 27 | when 28 | $flight : FlightStatus() from entry-point "flight-arrival" 29 | $control : FlightControl() 30 | // Obtain resources to prepare the flight landing 31 | then 32 | System.out.println("Flight " + $flight.getFlight() + " arriving to " + $control.getAirport() + ". Sending instructions"); 33 | // Send instructions to arriving flight 34 | end 35 | 36 | rule "flight average speed" 37 | when 38 | $flight : FlightStatus() from entry-point "flight-control" 39 | $averageSpeed : Number(floatValue > 0) from accumulate(FlightStatus(flight==$flight.flight, $speed:speed) over window:length(5) 40 | from entry-point "flight-control", 41 | average($speed)) 42 | then 43 | System.out.println($flight.getFlight() + " average speed: " + $averageSpeed); 44 | end 45 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter05 7 | 1.0.0 8 | 9 | temporal-operators 10 | Event correlation using temporal operators 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | junit 27 | junit 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/src/main/java/drools/cookbook/helper/EmergencySystem.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import drools.cookbook.model.FlightStatus; 7 | 8 | /** 9 | * 10 | * @author Lucas Amador 11 | * 12 | */ 13 | public class EmergencySystem { 14 | 15 | private List redirectedFligts = new ArrayList(); 16 | 17 | public void sendFireTrucks() { 18 | System.out.println("-> Fire trucks sended"); 19 | } 20 | 21 | public void sendAmbulances() { 22 | System.out.println("-> Ambulances sended"); 23 | } 24 | 25 | public void cleanRunways() { 26 | System.out.println("-> Cleaning runways in progress"); 27 | } 28 | 29 | public void redirect(FlightStatus flightStatus) { 30 | System.out.println("Redirecting flight " + flightStatus.getFlight() + " to another runway"); 31 | redirectedFligts.add(flightStatus); 32 | } 33 | 34 | public List getRedirectedFlights() { 35 | return redirectedFligts; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/src/main/java/drools/cookbook/helper/FlighSimulation.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.helper; 2 | 3 | import drools.cookbook.model.FlightStatus; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlighSimulation { 11 | 12 | private static final int AIRPORT_AIR_SPACE = 50; 13 | 14 | private final String flight; 15 | private final String origin; 16 | private final String destination; 17 | private long distance; 18 | private boolean landed; 19 | 20 | public FlighSimulation(String flight, String origin, String destination, long distance) { 21 | this.flight = flight; 22 | this.origin = origin; 23 | this.destination = destination; 24 | this.distance = distance; 25 | } 26 | 27 | public FlightStatus update() { 28 | FlightStatus flightStatus = new FlightStatus(); 29 | flightStatus.setFlight(flight); 30 | flightStatus.setDestination(destination); 31 | flightStatus.setOrigin(origin); 32 | flightStatus.setDestination(destination); 33 | this.distance = calculateDistance(); 34 | flightStatus.setDistance(this.distance); 35 | return flightStatus; 36 | } 37 | 38 | private long calculateDistance() { 39 | if ((distance - AIRPORT_AIR_SPACE) <= AIRPORT_AIR_SPACE) { 40 | landed = true; 41 | return 0; 42 | } 43 | return distance - AIRPORT_AIR_SPACE; 44 | } 45 | 46 | public boolean isLanded() { 47 | return landed; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/src/main/java/drools/cookbook/model/FlightControl.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * 8 | * @author Lucas Amador 9 | * 10 | */ 11 | public class FlightControl { 12 | 13 | private String airport; 14 | private Map flights = new HashMap(); 15 | 16 | public void setAirport(String airport) { 17 | this.airport = airport; 18 | } 19 | 20 | public String getAirport() { 21 | return airport; 22 | } 23 | 24 | public void setFlights(Map flights) { 25 | this.flights = flights; 26 | } 27 | 28 | public Map getFlights() { 29 | return flights; 30 | } 31 | 32 | public void addFlight(FlightStatus flightStatus) { 33 | flights.put(flightStatus.getFlight(), flightStatus); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/src/main/java/drools/cookbook/model/FlightStatus.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class FlightStatus { 11 | 12 | public enum STATUS { 13 | LANDING, FLYING, IN_EMERGENCY 14 | }; 15 | 16 | private String flight; 17 | private Date timestamp; 18 | private String origin; 19 | private String destination; 20 | private long distance; 21 | private STATUS status; 22 | 23 | public String getFlight() { 24 | return flight; 25 | } 26 | 27 | public void setFlight(String flight) { 28 | this.flight = flight; 29 | } 30 | 31 | public Date getTimestamp() { 32 | return timestamp; 33 | } 34 | 35 | public void setTimestamp(Date timestamp) { 36 | this.timestamp = timestamp; 37 | } 38 | 39 | public String getOrigin() { 40 | return origin; 41 | } 42 | 43 | public void setOrigin(String origin) { 44 | this.origin = origin; 45 | } 46 | 47 | public String getDestination() { 48 | return destination; 49 | } 50 | 51 | public void setDestination(String destination) { 52 | this.destination = destination; 53 | } 54 | 55 | public long getDistance() { 56 | return distance; 57 | } 58 | 59 | public void setDistance(long distance) { 60 | this.distance = distance; 61 | } 62 | 63 | public STATUS getStatus() { 64 | return status; 65 | } 66 | 67 | public void setStatus(STATUS status) { 68 | this.status = status; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /chapter05/temporal-operators/src/main/resources/drools/cookbook/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook; 2 | 3 | import drools.cookbook.model.FlightStatus 4 | 5 | global drools.cookbook.helper.EmergencySystem emergencySystem; 6 | 7 | declare FlightStatus 8 | @role(event) 9 | end 10 | 11 | rule "landing system" 12 | when 13 | $flight : FlightStatus() from entry-point "flight-landing" 14 | FlightStatus(this!=$flight, this before[0m, 3m] $flight) from entry-point "flight-landing" 15 | then 16 | emergencySystem.redirect($flight); 17 | end 18 | -------------------------------------------------------------------------------- /chapter06/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | drools.cookbook 5 | chapter06 6 | Executing Drools remotly 7 | pom 8 | 1.0.0 9 | 10 | 11 | xstream-commands 12 | rest-execution 13 | 14 | 15 | 16 | 17 | 18 | 19 | org.drools 20 | knowledge-api 21 | ${drools.version} 22 | 23 | 24 | org.drools 25 | drools-core 26 | ${drools.version} 27 | 28 | 29 | org.drools 30 | drools-compiler 31 | ${drools.version} 32 | 33 | 34 | com.thoughtworks.xstream 35 | xstream 36 | 1.4.1 37 | 38 | 39 | 40 | junit 41 | junit 42 | 4.4 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-compiler-plugin 53 | 2.0.2 54 | 55 | 1.5 56 | 1.5 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 5.2.0.Final 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /chapter06/rest-execution/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter06 7 | 1.0.0 8 | 9 | rest-execution 10 | REST execution 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | com.thoughtworks.xstream 27 | xstream 28 | 29 | 30 | commons-httpclient 31 | commons-httpclient 32 | 3.1-rc1 33 | test 34 | jar 35 | 36 | 37 | junit 38 | junit 39 | test 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /chapter06/rest-execution/src/main/java/drools/cookbook/chapter06/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter06; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | public String getName() { 26 | return name; 27 | } 28 | public void setServerName(String serverName) { 29 | this.serverName = serverName; 30 | } 31 | public String getServerName() { 32 | return serverName; 33 | } 34 | public void setMemory(int memory) { 35 | this.memory = memory; 36 | } 37 | public int getMemory() { 38 | return memory; 39 | } 40 | public void setDiskSpace(int diskSpace) { 41 | this.diskSpace = diskSpace; 42 | } 43 | public int getDiskSpace() { 44 | return diskSpace; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /chapter06/rest-execution/src/test/resources/drools/cookbook/chapter06/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter06 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | global java.util.List serversAvailability; 7 | 8 | rule "check minimum server configuration" 9 | dialect "mvel" 10 | salience 100 11 | when 12 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 13 | then 14 | System.out.println("Server \"" + $server.getName() + "\" was rejected by don't apply the minimum configuration."); 15 | retract($server); 16 | end 17 | 18 | rule "check available server for a new virtualization" 19 | when 20 | $virtualization : Virtualization() 21 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 22 | $freeMemory : Number(intValue > 0) from accumulate (Virtualization($vmemory : memory) from $server.virtualizations, 23 | init(int total = $memory - $virtualization.getMemory()), 24 | action(total -= $vmemory;), 25 | reverse(total += $vmemory;), 26 | result(total)) 27 | $freeDiskSpace : Number(intValue > 0) from accumulate (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, 28 | init(int total = $diskSpace - $virtualization.getDiskSpace()), 29 | action(total -= $vdiskSpace;), 30 | reverse(total += $vdiskSpace;), 31 | result(total)) 32 | then 33 | $server.addVirtualization($virtualization); 34 | retract($virtualization); 35 | end 36 | -------------------------------------------------------------------------------- /chapter06/xstream-commands/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter06 7 | 1.0.0 8 | 9 | xstream-commands 10 | XStream commands creation 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | com.thoughtworks.xstream 27 | xstream 28 | 29 | 30 | junit 31 | junit 32 | test 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter06/xstream-commands/src/main/java/drools/cookbook/chapter06/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter06; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | public String getName() { 26 | return name; 27 | } 28 | public void setServerName(String serverName) { 29 | this.serverName = serverName; 30 | } 31 | public String getServerName() { 32 | return serverName; 33 | } 34 | public void setMemory(int memory) { 35 | this.memory = memory; 36 | } 37 | public int getMemory() { 38 | return memory; 39 | } 40 | public void setDiskSpace(int diskSpace) { 41 | this.diskSpace = diskSpace; 42 | } 43 | public int getDiskSpace() { 44 | return diskSpace; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /chapter06/xstream-commands/src/test/java/drools/cookbook/chapter06/XStreamCommandsTest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter06; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import junit.framework.Assert; 7 | 8 | import org.drools.command.BatchExecutionCommand; 9 | import org.drools.command.Command; 10 | import org.drools.command.CommandFactory; 11 | import org.drools.runtime.help.BatchExecutionHelper; 12 | import org.junit.Test; 13 | 14 | /** 15 | * 16 | * @author Lucas Amador 17 | * 18 | */ 19 | public class XStreamCommandsTest { 20 | 21 | @Test 22 | public void xstreamCommandsFactoryTest() throws InterruptedException { 23 | 24 | Server server1 = new Server("windows-nt", 1, 2048, 2048, 3); 25 | 26 | List commands = new ArrayList(); 27 | BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(commands, "ksession1"); 28 | 29 | Command insertObjectCommand = CommandFactory.newInsert(server1); 30 | Command fireAllRulesCommand = CommandFactory.newFireAllRules(); 31 | commands.add(insertObjectCommand); 32 | commands.add(fireAllRulesCommand); 33 | 34 | String result = BatchExecutionHelper.newXStreamMarshaller().toXML(batchExecutionCommand); 35 | 36 | String expected = "\n"; 37 | expected += " \n"; 38 | expected += " \n"; 39 | expected += " windows-nt\n"; 40 | expected += " 1\n"; 41 | expected += " 2048\n"; 42 | expected += " 2048\n"; 43 | expected += " \n"; 44 | expected += " 3\n"; 45 | expected += " false\n"; 46 | expected += " \n"; 47 | expected += " \n"; 48 | expected += " \n"; 49 | expected += ""; 50 | 51 | Assert.assertEquals(expected, result); 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /chapter06/xstream-commands/src/test/resources/drools/cookbook/chapter06/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter06 2 | 3 | import java.util.Date 4 | import java.util.List 5 | 6 | global java.util.List serversAvailability; 7 | 8 | rule "check minimum server configuration" 9 | dialect "mvel" 10 | salience 100 11 | when 12 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 13 | then 14 | System.out.println("Server \"" + $server.getName() + "\" was rejected by don't apply the minimum configuration."); 15 | retract($server); 16 | end 17 | 18 | rule "check available server for a new virtualization" 19 | when 20 | $virtualization : Virtualization() 21 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 22 | $freeMemory : Number(intValue > 0) from accumulate (Virtualization($vmemory : memory) from $server.virtualizations, 23 | init(int total = $memory - $virtualization.getMemory()), 24 | action(total -= $vmemory;), 25 | reverse(total += $vmemory;), 26 | result(total)) 27 | $freeDiskSpace : Number(intValue > 0) from accumulate (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, 28 | init(int total = $diskSpace - $virtualization.getDiskSpace()), 29 | action(total -= $vdiskSpace;), 30 | reverse(total += $vdiskSpace;), 31 | result(total)) 32 | then 33 | $server.addVirtualization($virtualization); 34 | retract($virtualization); 35 | end 36 | -------------------------------------------------------------------------------- /chapter07/apache-camel-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter07 7 | 1.0.0 8 | 9 | apache-camel-integration 10 | Apache Camel integration 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-camel 28 | 29 | 30 | junit 31 | junit 32 | test 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter07/apache-camel-integration/src/main/java/drools/cookbook/chapter07/model/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setServerName(String serverName) { 31 | this.serverName = serverName; 32 | } 33 | 34 | public String getServerName() { 35 | return serverName; 36 | } 37 | 38 | public void setMemory(int memory) { 39 | this.memory = memory; 40 | } 41 | 42 | public int getMemory() { 43 | return memory; 44 | } 45 | 46 | public void setDiskSpace(int diskSpace) { 47 | this.diskSpace = diskSpace; 48 | } 49 | 50 | public int getDiskSpace() { 51 | return diskSpace; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter07/apache-camel-integration/src/test/resources/drools/cookbook/chapter07/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07 2 | 3 | import drools.cookbook.chapter07.model.Server 4 | import drools.cookbook.chapter07.model.Virtualization 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | 15 | rule "check available server for a new virtualization" 16 | dialect "mvel" 17 | when 18 | $virtualization : Virtualization($virtMemory : memory, $virtDiskSpace : diskSpace) 19 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 20 | Number((intValue + $virtMemory) < $memory) from accumulate 21 | (Virtualization($vmemory : memory) from $server.virtualizations, sum($vmemory)) 22 | Number((intValue + $virtDiskSpace) < $diskSpace) from accumulate 23 | (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, sum($vdiskSpace)) 24 | then 25 | $server.addVirtualization($virtualization); 26 | retract($virtualization); 27 | end 28 | -------------------------------------------------------------------------------- /chapter07/apache-camel-jms/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter07 7 | 1.0.0 8 | 9 | apache-camel-jms 10 | Apache Camel + Drools + ActiveMQ JMS 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-camel 28 | 29 | 30 | org.apache.camel 31 | camel-jms 32 | 33 | 34 | org.apache.xbean 35 | xbean-spring 36 | 37 | 38 | org.apache.activemq 39 | activemq-core 40 | 41 | 42 | org.apache.activemq 43 | activemq-camel 44 | 45 | 46 | junit 47 | junit 48 | test 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /chapter07/apache-camel-jms/src/main/java/drools/cookbook/chapter07/jms/JMSQueueProducer.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.jms; 2 | 3 | import org.springframework.jms.core.JmsTemplate; 4 | 5 | public class JMSQueueProducer { 6 | 7 | private String queueName; 8 | private JmsTemplate jmsTemplate; 9 | 10 | public void send(String message) { 11 | jmsTemplate.convertAndSend(queueName, message); 12 | } 13 | 14 | public void setJmsTemplate(JmsTemplate jmsTemplate) { 15 | this.jmsTemplate = jmsTemplate; 16 | } 17 | 18 | public JmsTemplate getJmsTemplate() { 19 | return jmsTemplate; 20 | } 21 | 22 | public void setQueueName(String queueName) { 23 | this.queueName = queueName; 24 | } 25 | 26 | public String getQueueName() { 27 | return queueName; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /chapter07/apache-camel-jms/src/main/java/drools/cookbook/chapter07/model/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | public String getName() { 26 | return name; 27 | } 28 | public void setServerName(String serverName) { 29 | this.serverName = serverName; 30 | } 31 | public String getServerName() { 32 | return serverName; 33 | } 34 | public void setMemory(int memory) { 35 | this.memory = memory; 36 | } 37 | public int getMemory() { 38 | return memory; 39 | } 40 | public void setDiskSpace(int diskSpace) { 41 | this.diskSpace = diskSpace; 42 | } 43 | public int getDiskSpace() { 44 | return diskSpace; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /chapter07/apache-camel-jms/src/test/resources/drools/cookbook/chapter07/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07 2 | 3 | import drools.cookbook.chapter07.model.Server 4 | import drools.cookbook.chapter07.model.Virtualization 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | 15 | rule "check available server for a new virtualization" 16 | dialect "mvel" 17 | when 18 | $virtualization : Virtualization($virtMemory : memory, $virtDiskSpace : diskSpace) 19 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 20 | Number((intValue + $virtMemory) < $memory) from accumulate 21 | (Virtualization($vmemory : memory) from $server.virtualizations, sum($vmemory)) 22 | Number((intValue + $virtDiskSpace) < $diskSpace) from accumulate 23 | (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, sum($vdiskSpace)) 24 | then 25 | $server.addVirtualization($virtualization); 26 | retract($virtualization); 27 | end 28 | -------------------------------------------------------------------------------- /chapter07/apache-camel-spring-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter07 7 | 1.0.0 8 | 9 | apache-camel-spring-integration 10 | Apache Camel + Drools + Spring integration 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-spring 28 | 29 | 30 | org.drools 31 | drools-camel 32 | 33 | 34 | junit 35 | junit 36 | test 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /chapter07/apache-camel-spring-integration/src/main/java/drools/cookbook/chapter07/model/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setServerName(String serverName) { 31 | this.serverName = serverName; 32 | } 33 | 34 | public String getServerName() { 35 | return serverName; 36 | } 37 | 38 | public void setMemory(int memory) { 39 | this.memory = memory; 40 | } 41 | 42 | public int getMemory() { 43 | return memory; 44 | } 45 | 46 | public void setDiskSpace(int diskSpace) { 47 | this.diskSpace = diskSpace; 48 | } 49 | 50 | public int getDiskSpace() { 51 | return diskSpace; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter07/apache-camel-spring-integration/src/test/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /chapter07/apache-camel-spring-integration/src/test/resources/drools/cookbook/chapter07/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07 2 | 3 | import drools.cookbook.chapter07.model.Server 4 | import drools.cookbook.chapter07.model.Virtualization 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | 15 | rule "check available server for a new virtualization" 16 | dialect "mvel" 17 | when 18 | $virtualization : Virtualization($virtMemory : memory, $virtDiskSpace : diskSpace) 19 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 20 | Number((intValue + $virtMemory) < $memory) from accumulate 21 | (Virtualization($vmemory : memory) from $server.virtualizations, sum($vmemory)) 22 | Number((intValue + $virtDiskSpace) < $diskSpace) from accumulate 23 | (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, sum($vdiskSpace)) 24 | then 25 | $server.addVirtualization($virtualization); 26 | retract($virtualization); 27 | end 28 | -------------------------------------------------------------------------------- /chapter07/spring-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter07 7 | 1.0.0 8 | 9 | spring-integration 10 | Spring integration 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-spring 28 | 29 | 30 | junit 31 | junit 32 | test 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter07/spring-integration/src/main/java/drools/cookbook/chapter07/model/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.model; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public class Virtualization { 9 | 10 | private String name; 11 | private String serverName; 12 | private int memory; 13 | private int diskSpace; 14 | 15 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 16 | this.name = name; 17 | this.serverName = serverName; 18 | this.memory = memory; 19 | this.diskSpace = diskSpace; 20 | } 21 | 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setServerName(String serverName) { 31 | this.serverName = serverName; 32 | } 33 | 34 | public String getServerName() { 35 | return serverName; 36 | } 37 | 38 | public void setMemory(int memory) { 39 | this.memory = memory; 40 | } 41 | 42 | public int getMemory() { 43 | return memory; 44 | } 45 | 46 | public void setDiskSpace(int diskSpace) { 47 | this.diskSpace = diskSpace; 48 | } 49 | 50 | public int getDiskSpace() { 51 | return diskSpace; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /chapter07/spring-integration/src/test/java/drools/cookbook/chapter07/SpringIntegrationTest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07; 2 | 3 | import junit.framework.Assert; 4 | 5 | import org.drools.runtime.StatefulKnowledgeSession; 6 | import org.junit.Test; 7 | import org.springframework.context.support.ClassPathXmlApplicationContext; 8 | 9 | import drools.cookbook.chapter07.model.Server; 10 | 11 | /** 12 | * 13 | * @author Lucas Amador 14 | * 15 | */ 16 | public class SpringIntegrationTest { 17 | 18 | @Test 19 | public void simpleSpringIntegration() { 20 | ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 21 | applicationContext.start(); 22 | StatefulKnowledgeSession ksession1 = (StatefulKnowledgeSession) applicationContext.getBean("ksession1"); 23 | Assert.assertNotNull(ksession1); 24 | Server debianServer = new Server("debian-1", 4, 2048, 250, 0); 25 | ksession1.insert(debianServer); 26 | ksession1.fireAllRules(); 27 | Assert.assertEquals(0, ksession1.getFactCount()); 28 | applicationContext.stop(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /chapter07/spring-integration/src/test/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /chapter07/spring-integration/src/test/resources/drools/cookbook/chapter07/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07 2 | 3 | import drools.cookbook.chapter07.model.Server 4 | import drools.cookbook.chapter07.model.Virtualization 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | 15 | rule "check available server for a new virtualization" 16 | dialect "mvel" 17 | when 18 | $virtualization : Virtualization($virtMemory : memory, $virtDiskSpace : diskSpace) 19 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 20 | Number((intValue + $virtMemory) < $memory) from accumulate 21 | (Virtualization($vmemory : memory) from $server.virtualizations, sum($vmemory)) 22 | Number((intValue + $virtDiskSpace) < $diskSpace) from accumulate 23 | (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, sum($vdiskSpace)) 24 | then 25 | $server.addVirtualization($virtualization); 26 | retract($virtualization); 27 | end 28 | -------------------------------------------------------------------------------- /chapter07/spring-jpa-integration/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter07 7 | 1.0.0 8 | 9 | spring-jpa-integration 10 | Spring JPA integration 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.drools 27 | drools-spring 28 | 29 | 30 | org.drools 31 | drools-persistence-jpa 32 | 33 | 34 | org.springframework 35 | spring-orm 36 | 37 | 38 | org.springframework 39 | spring-jdbc 40 | 41 | 42 | org.jbpm 43 | jbpm-persistence-jpa 44 | 5.1.0.Final 45 | 46 | 47 | 48 | com.h2database 49 | h2 50 | test 51 | 52 | 53 | junit 54 | junit 55 | test 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /chapter07/spring-jpa-integration/src/main/java/drools/cookbook/chapter07/model/Virtualization.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07.model; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 7 | * @author Lucas Amador 8 | * 9 | */ 10 | public class Virtualization implements Serializable { 11 | 12 | private static final long serialVersionUID = 1L; 13 | 14 | private String name; 15 | private String serverName; 16 | private int memory; 17 | private int diskSpace; 18 | 19 | public Virtualization(String name, String serverName, int memory, int diskSpace) { 20 | this.name = name; 21 | this.serverName = serverName; 22 | this.memory = memory; 23 | this.diskSpace = diskSpace; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public String getName() { 31 | return name; 32 | } 33 | 34 | public void setServerName(String serverName) { 35 | this.serverName = serverName; 36 | } 37 | 38 | public String getServerName() { 39 | return serverName; 40 | } 41 | 42 | public void setMemory(int memory) { 43 | this.memory = memory; 44 | } 45 | 46 | public int getMemory() { 47 | return memory; 48 | } 49 | 50 | public void setDiskSpace(int diskSpace) { 51 | this.diskSpace = diskSpace; 52 | } 53 | 54 | public int getDiskSpace() { 55 | return diskSpace; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /chapter07/spring-jpa-integration/src/test/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | org.hibernate.ejb.HibernatePersistence 10 | org.drools.persistence.info.SessionInfo 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /chapter07/spring-jpa-integration/src/test/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /chapter07/spring-jpa-integration/src/test/resources/drools/cookbook/chapter07/rules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter07 2 | 3 | import drools.cookbook.chapter07.model.Server 4 | import drools.cookbook.chapter07.model.Virtualization 5 | 6 | rule "check minimum server configuration" 7 | dialect "mvel" 8 | when 9 | $server : Server(processors < 2 || memory<=1024 || diskSpace<=250) 10 | then 11 | System.out.println("Server \"" + $server.name + "\" was rejected by don't apply the minimum configuration."); 12 | retract($server); 13 | end 14 | 15 | rule "check available server for a new virtualization" 16 | dialect "mvel" 17 | when 18 | $virtualization : Virtualization($virtMemory : memory, $virtDiskSpace : diskSpace) 19 | $server : Server($memory : memory, $diskSpace : diskSpace, virtualizations !=null) 20 | Number((intValue + $virtMemory) < $memory) from accumulate 21 | (Virtualization($vmemory : memory) from $server.virtualizations, sum($vmemory)) 22 | Number((intValue + $virtDiskSpace) < $diskSpace) from accumulate 23 | (Virtualization($vdiskSpace : diskSpace) from $server.virtualizations, sum($vdiskSpace)) 24 | then 25 | $server.addVirtualization($virtualization); 26 | retract($virtualization); 27 | end 28 | -------------------------------------------------------------------------------- /chapter08/benchmarker/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | 6 | drools.cookbook 7 | chapter08 8 | 1.0.0 9 | 10 | benchmarker 11 | Creating a simple benchmarker 12 | 13 | 14 | 15 | org.drools.planner 16 | drools-planner-core 17 | 18 | 19 | org.slf4j 20 | slf4j-api 21 | 1.6.1 22 | 23 | 24 | org.slf4j 25 | slf4j-log4j12 26 | 1.6.1 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | jfree 35 | jfreechart 36 | 1.0.13 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/domain/Location.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum Location { 4 | 5 | SAN_DIEGO, MONTANA, NY, NORTH_CAROLINA, WASHINGTON_DC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/domain/Skill.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum Skill { 4 | 5 | JAVA, SCALA, REST, DROOLS, HADOOP; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/domain/TrainingLevel.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum TrainingLevel { 4 | 5 | SENIOR("sr"), SEMISENIOR("ssr"), JUNIOR("jr"), TRAINEE("tr"); 6 | 7 | private String abreviation; 8 | 9 | private TrainingLevel(String abreviation) { 10 | this.abreviation = abreviation; 11 | } 12 | 13 | public String getAbreviation() { 14 | return this.abreviation; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/planner/SelectNextTechnicians.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.drools.planner.core.move.Move; 7 | import org.drools.planner.core.move.factory.CachedMoveFactory; 8 | import org.drools.planner.core.solution.Solution; 9 | 10 | import drools.cookbook.chapter08.domain.ServiceRequest; 11 | import drools.cookbook.chapter08.domain.Technician; 12 | 13 | public class SelectNextTechnicians extends CachedMoveFactory { 14 | 15 | @Override 16 | public List createCachedMoveList(Solution solution) { 17 | TechniciansSolution techSolution = (TechniciansSolution) solution; 18 | List moves = new ArrayList(); 19 | for (ServiceRequest serviceRequest : techSolution.getServiceRequests()) { 20 | for (Technician technician : techSolution.getTechnicians()) { 21 | moves.add(new TechnicianMove(serviceRequest, technician)); 22 | } 23 | } 24 | return moves; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/planner/TechnicianMove.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.drools.FactHandle; 6 | import org.drools.WorkingMemory; 7 | import org.drools.planner.core.move.Move; 8 | 9 | import drools.cookbook.chapter08.domain.ServiceRequest; 10 | import drools.cookbook.chapter08.domain.Technician; 11 | 12 | public class TechnicianMove implements Move { 13 | 14 | private ServiceRequest serviceRequest; 15 | private Technician technician; 16 | 17 | public TechnicianMove(ServiceRequest serviceRequest, Technician technician) { 18 | this.serviceRequest = serviceRequest; 19 | this.technician = technician; 20 | } 21 | 22 | @Override 23 | public boolean isMoveDoable(WorkingMemory workingMemory) { 24 | return !serviceRequest.getTechnician().equals(technician); 25 | } 26 | 27 | @Override 28 | public Move createUndoMove(WorkingMemory workingMemory) { 29 | return new TechnicianMove(serviceRequest, serviceRequest.getTechnician()); 30 | } 31 | 32 | @Override 33 | public void doMove(WorkingMemory workingMemory) { 34 | FactHandle serviceRequestHandle = workingMemory.getFactHandle(serviceRequest); 35 | serviceRequest.setTechnician(technician); 36 | workingMemory.update(serviceRequestHandle, serviceRequest); 37 | } 38 | 39 | @Override 40 | public boolean equals(Object o) { 41 | if (this == o) { 42 | return true; 43 | } else if (o instanceof TechnicianMove) { 44 | TechnicianMove other = (TechnicianMove) o; 45 | return new EqualsBuilder().append(serviceRequest, other.serviceRequest) 46 | .append(technician, other.technician).isEquals(); 47 | } else { 48 | return false; 49 | } 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | return new HashCodeBuilder().append(serviceRequest).append(technician).toHashCode(); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return serviceRequest.toString() + " to " + technician.toString(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/java/drools/cookbook/chapter08/planner/TechniciansSolution.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import org.drools.planner.api.domain.solution.PlanningEntityCollectionProperty; 8 | import org.drools.planner.core.score.SimpleScore; 9 | import org.drools.planner.core.solution.Solution; 10 | 11 | import drools.cookbook.chapter08.domain.ServiceRequest; 12 | import drools.cookbook.chapter08.domain.Technician; 13 | 14 | public class TechniciansSolution implements Solution { 15 | 16 | private List technicians; 17 | private List serviceRequests; 18 | private SimpleScore score; 19 | 20 | public TechniciansSolution(List technicians, List serviceRequests) { 21 | this.technicians = technicians; 22 | this.serviceRequests = serviceRequests; 23 | } 24 | 25 | private TechniciansSolution() { 26 | } 27 | 28 | @Override 29 | public SimpleScore getScore() { 30 | return score; 31 | } 32 | 33 | @Override 34 | public void setScore(SimpleScore score) { 35 | this.score = (SimpleScore) score; 36 | } 37 | 38 | @Override 39 | public Collection getProblemFacts() { 40 | return technicians; 41 | } 42 | 43 | @Override 44 | public Solution cloneSolution() { 45 | TechniciansSolution solution = new TechniciansSolution(); 46 | solution.score = score; 47 | solution.technicians = technicians; 48 | List clonedServices = new ArrayList(serviceRequests.size()); 49 | for (ServiceRequest sr : serviceRequests) { 50 | clonedServices.add(new ServiceRequest(sr)); 51 | } 52 | solution.serviceRequests = clonedServices; 53 | return solution; 54 | } 55 | 56 | public List getTechnicians() { 57 | return technicians; 58 | } 59 | 60 | @PlanningEntityCollectionProperty 61 | public List getServiceRequests() { 62 | return serviceRequests; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/resources/ServiceRequestSolverBenchmarkConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | benchmark-results 4 | BEST_SOLUTION_CHANGED 5 | CALCULATE_COUNT_PER_SECOND 6 | 30 7 | 8 | 9 | src/main/resources/unsolvedTechnicianRequest.xml 10 | 11 | drools.cookbook.chapter08.planner.TechniciansSolution 12 | drools.cookbook.chapter08.domain.ServiceRequest 13 | /serviceRequestScoreRules.drl 14 | 15 | SIMPLE 16 | 17 | 18 | 20 19 | 20 | 21 | 22 | 23 | 24 | tabuSearch 25 | 26 | 27 | 28 | drools.cookbook.chapter08.planner.SelectNextTechnicians 29 | 30 | 31 | 1000 32 | 33 | 34 | NEVER 35 | 36 | 37 | 38 | 39 | 40 | simulatedAnnealing 41 | 42 | 43 | 44 | drools.cookbook.chapter08.planner.SelectNextTechnicians 45 | 46 | 47 | 5 48 | 49 | 50 | NEVER 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/main/resources/serviceRequestScoreRules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08; 2 | 3 | import java.util.EnumSet; 4 | import java.util.Set; 5 | import drools.cookbook.chapter08.domain.ServiceRequest; 6 | import drools.cookbook.chapter08.domain.Skill; 7 | import org.drools.planner.core.score.constraint.IntConstraintOccurrence; 8 | import org.drools.planner.core.score.calculator.SimpleScoreCalculator; 9 | 10 | global SimpleScoreCalculator scoreCalculator; 11 | 12 | rule "sameCity" 13 | dialect "java" 14 | when 15 | $sd : ServiceRequest(technician.location != location) 16 | then 17 | insertLogical(new IntConstraintOccurrence("sameCity", 1, $sd)); 18 | end 19 | 20 | rule "isBusy" 21 | dialect "java" 22 | when 23 | $sd : ServiceRequest(technician.busy == true) 24 | then 25 | insertLogical(new IntConstraintOccurrence("isBusy", 6, $sd)); 26 | end 27 | 28 | rule "skillMatch" 29 | dialect "java" 30 | when 31 | $sd : ServiceRequest($neededSkills : neededSkills, $tec : technician) 32 | then 33 | Set tempSkills = EnumSet.copyOf($neededSkills); 34 | tempSkills.removeAll($tec.getSkills()); 35 | insertLogical(new IntConstraintOccurrence("skillMatch", tempSkills.size() * 3, $sd)); 36 | end 37 | 38 | rule "hardConstraintsBroken" 39 | dialect "java" 40 | salience -1 // Do the other rules first (optional, for performance) 41 | when 42 | $hardTotal : Number() from accumulate(IntConstraintOccurrence($weight : weight), 43 | sum($weight)) 44 | then 45 | scoreCalculator.setScore(- $hardTotal.intValue()); 46 | end 47 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/test/java/drools/cookbook/chapter08/BestAvailableTechnicianBenchmark.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08; 2 | 3 | import org.drools.planner.benchmark.XmlSolverBenchmarker; 4 | 5 | public class BestAvailableTechnicianBenchmark { 6 | 7 | public static void main(String[] args) { 8 | XmlSolverBenchmarker benchmarker = new XmlSolverBenchmarker(); 9 | benchmarker.configure("/ServiceRequestSolverBenchmarkConfig.xml"); 10 | benchmarker.benchmark(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /chapter08/benchmarker/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | 6 | drools.cookbook 7 | chapter08 8 | 1.0.0 9 | 10 | resource-assignation 11 | Resource assignation planning problem 12 | 13 | 14 | 15 | org.drools.planner 16 | drools-planner-core 17 | 18 | 19 | org.slf4j 20 | slf4j-api 21 | 1.6.1 22 | 23 | 24 | org.slf4j 25 | slf4j-log4j12 26 | 1.6.1 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/domain/Location.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public enum Location { 9 | 10 | SAN_DIEGO, MONTANA, NY, NORTH_CAROLINA, WASHINGTON_DC 11 | 12 | } 13 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/domain/Skill.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public enum Skill { 9 | 10 | JAVA, SCALA, REST, DROOLS, HADOOP; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/domain/TrainingLevel.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | /** 4 | * 5 | * @author Lucas Amador 6 | * 7 | */ 8 | public enum TrainingLevel { 9 | 10 | SENIOR("sr"), SEMISENIOR("ssr"), JUNIOR("jr"), TRAINEE("tr"); 11 | 12 | private String abreviation; 13 | 14 | private TrainingLevel(String abreviation) { 15 | this.abreviation = abreviation; 16 | } 17 | 18 | public String getAbreviation() { 19 | return this.abreviation; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/planner/SelectNextTechnicians.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.drools.planner.core.move.Move; 7 | import org.drools.planner.core.move.factory.CachedMoveFactory; 8 | import org.drools.planner.core.solution.Solution; 9 | 10 | import drools.cookbook.chapter08.domain.ServiceRequest; 11 | import drools.cookbook.chapter08.domain.Technician; 12 | 13 | /** 14 | * 15 | * @author Lucas Amador 16 | * 17 | */ 18 | public class SelectNextTechnicians extends CachedMoveFactory { 19 | 20 | @Override 21 | public List createCachedMoveList(Solution solution) { 22 | TechniciansSolution techSolution = (TechniciansSolution) solution; 23 | List moves = new ArrayList(); 24 | for (ServiceRequest serviceRequest : techSolution.getServiceRequests()) { 25 | for (Technician technician : techSolution.getTechnicians()) { 26 | moves.add(new TechnicianMove(serviceRequest, technician)); 27 | } 28 | } 29 | return moves; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/planner/TechnicianMove.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.drools.FactHandle; 6 | import org.drools.WorkingMemory; 7 | import org.drools.planner.core.move.Move; 8 | 9 | import drools.cookbook.chapter08.domain.ServiceRequest; 10 | import drools.cookbook.chapter08.domain.Technician; 11 | 12 | /** 13 | * 14 | * @author Lucas Amador 15 | * 16 | */ 17 | public class TechnicianMove implements Move { 18 | 19 | private ServiceRequest serviceRequest; 20 | private Technician technician; 21 | 22 | public TechnicianMove(ServiceRequest serviceRequest, Technician technician) { 23 | this.serviceRequest = serviceRequest; 24 | this.technician = technician; 25 | } 26 | 27 | @Override 28 | public boolean isMoveDoable(WorkingMemory workingMemory) { 29 | return !serviceRequest.getTechnician().equals(technician); 30 | } 31 | 32 | @Override 33 | public Move createUndoMove(WorkingMemory workingMemory) { 34 | return new TechnicianMove(serviceRequest, serviceRequest.getTechnician()); 35 | } 36 | 37 | @Override 38 | public void doMove(WorkingMemory workingMemory) { 39 | FactHandle serviceRequestHandle = workingMemory.getFactHandle(serviceRequest); 40 | serviceRequest.setTechnician(technician); 41 | workingMemory.update(serviceRequestHandle, serviceRequest); 42 | } 43 | 44 | @Override 45 | public boolean equals(Object o) { 46 | if (this == o) { 47 | return true; 48 | } else if (o instanceof TechnicianMove) { 49 | TechnicianMove other = (TechnicianMove) o; 50 | return new EqualsBuilder().append(serviceRequest, other.serviceRequest) 51 | .append(technician, other.technician).isEquals(); 52 | } else { 53 | return false; 54 | } 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return new HashCodeBuilder().append(serviceRequest).append(technician).toHashCode(); 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return serviceRequest.toString() + " to " + technician.toString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/java/drools/cookbook/chapter08/planner/TechniciansSolution.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import org.drools.planner.api.domain.solution.PlanningEntityCollectionProperty; 8 | import org.drools.planner.core.score.SimpleScore; 9 | import org.drools.planner.core.solution.Solution; 10 | 11 | import drools.cookbook.chapter08.domain.ServiceRequest; 12 | import drools.cookbook.chapter08.domain.Technician; 13 | 14 | /** 15 | * 16 | * @author Lucas Amador 17 | * 18 | */ 19 | public class TechniciansSolution implements Solution { 20 | 21 | private List technicians; 22 | private List serviceRequests; 23 | private SimpleScore score; 24 | 25 | public TechniciansSolution(List technicians, List serviceRequests) { 26 | this.technicians = technicians; 27 | this.serviceRequests = serviceRequests; 28 | } 29 | 30 | private TechniciansSolution() { 31 | } 32 | 33 | @Override 34 | public SimpleScore getScore() { 35 | return score; 36 | } 37 | 38 | @Override 39 | public void setScore(SimpleScore score) { 40 | this.score = (SimpleScore) score; 41 | } 42 | 43 | @Override 44 | public Collection getProblemFacts() { 45 | return technicians; 46 | } 47 | 48 | @Override 49 | public Solution cloneSolution() { 50 | TechniciansSolution solution = new TechniciansSolution(); 51 | solution.score = score; 52 | solution.technicians = technicians; 53 | List clonedServices = new ArrayList(serviceRequests.size()); 54 | for (ServiceRequest sr : serviceRequests) { 55 | clonedServices.add(new ServiceRequest(sr)); 56 | } 57 | solution.serviceRequests = clonedServices; 58 | return solution; 59 | } 60 | 61 | public List getTechnicians() { 62 | return technicians; 63 | } 64 | 65 | @PlanningEntityCollectionProperty 66 | public List getServiceRequests() { 67 | return serviceRequests; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/resources/ServiceRequestSolverConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | DEBUG 4 | drools.cookbook.chapter08.planner.TechniciansSolution 5 | drools.cookbook.chapter08.domain.ServiceRequest 6 | /serviceRequestScoreRules.drl 7 | 8 | SIMPLE 9 | 10 | 11 | FIRST_FIT 12 | 13 | 14 | 15 | OR 16 | 100 17 | 0 18 | 19 | 20 | drools.cookbook.chapter08.planner.SelectNextTechnicians 21 | 22 | 23 | 1000 24 | 25 | 26 | NEVER 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/main/resources/serviceRequestScoreRules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08; 2 | 3 | import java.util.EnumSet; 4 | import java.util.Set; 5 | import drools.cookbook.chapter08.domain.ServiceRequest; 6 | import drools.cookbook.chapter08.domain.Skill; 7 | import org.drools.planner.core.score.constraint.IntConstraintOccurrence; 8 | import org.drools.planner.core.score.calculator.SimpleScoreCalculator; 9 | 10 | global SimpleScoreCalculator scoreCalculator; 11 | 12 | rule "sameCity" 13 | dialect "java" 14 | when 15 | $sd : ServiceRequest(technician.location != location) 16 | then 17 | insertLogical(new IntConstraintOccurrence("sameCity", 1, $sd)); 18 | end 19 | 20 | rule "isBusy" 21 | dialect "java" 22 | when 23 | $sd : ServiceRequest(technician.busy == true) 24 | then 25 | insertLogical(new IntConstraintOccurrence("isBusy", 6, $sd)); 26 | end 27 | 28 | rule "skillMatch" 29 | dialect "java" 30 | when 31 | $sd : ServiceRequest($neededSkills : neededSkills, $tec : technician) 32 | then 33 | Set tempSkills = EnumSet.copyOf($neededSkills); 34 | tempSkills.removeAll($tec.getSkills()); 35 | insertLogical(new IntConstraintOccurrence("skillMatch", tempSkills.size() * 3, $sd)); 36 | end 37 | 38 | rule "hardConstraintsBroken" 39 | dialect "java" 40 | salience -1 // Do the other rules first (optional, for performance) 41 | when 42 | $hardTotal : Number() from accumulate(IntConstraintOccurrence($weight : weight), 43 | sum($weight)) 44 | then 45 | scoreCalculator.setScore(- $hardTotal.intValue()); 46 | end 47 | -------------------------------------------------------------------------------- /chapter08/resource-assignation/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 4.0.0 5 | 6 | drools.cookbook 7 | chapter08 8 | 1.0.0 9 | 10 | simulated-annealing 11 | Using a simulated annealing acceptor 12 | 13 | 14 | 15 | org.drools.planner 16 | drools-planner-core 17 | 18 | 19 | org.slf4j 20 | slf4j-api 21 | 1.6.1 22 | 23 | 24 | org.slf4j 25 | slf4j-log4j12 26 | 1.6.1 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/domain/Location.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum Location { 4 | 5 | SAN_DIEGO, MONTANA, NY, NORTH_CAROLINA, WASHINGTON_DC 6 | 7 | } 8 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/domain/Skill.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum Skill { 4 | 5 | JAVA, SCALA, REST, DROOLS, HADOOP; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/domain/TrainingLevel.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.domain; 2 | 3 | public enum TrainingLevel { 4 | 5 | SENIOR("sr"), SEMISENIOR("ssr"), JUNIOR("jr"), TRAINEE("tr"); 6 | 7 | private String abreviation; 8 | 9 | private TrainingLevel(String abreviation) { 10 | this.abreviation = abreviation; 11 | } 12 | 13 | public String getAbreviation() { 14 | return this.abreviation; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/planner/SelectNextTechnicians.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.drools.planner.core.move.Move; 7 | import org.drools.planner.core.move.factory.CachedMoveFactory; 8 | import org.drools.planner.core.solution.Solution; 9 | 10 | import drools.cookbook.chapter08.domain.ServiceRequest; 11 | import drools.cookbook.chapter08.domain.Technician; 12 | 13 | public class SelectNextTechnicians extends CachedMoveFactory { 14 | 15 | @Override 16 | public List createCachedMoveList(Solution solution) { 17 | TechniciansSolution techSolution = (TechniciansSolution) solution; 18 | List moves = new ArrayList(); 19 | for (ServiceRequest serviceRequest : techSolution.getServiceRequests()) { 20 | for (Technician technician : techSolution.getTechnicians()) { 21 | moves.add(new TechnicianMove(serviceRequest, technician)); 22 | } 23 | } 24 | return moves; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/planner/TechnicianMove.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | import org.drools.FactHandle; 6 | import org.drools.WorkingMemory; 7 | import org.drools.planner.core.move.Move; 8 | 9 | import drools.cookbook.chapter08.domain.ServiceRequest; 10 | import drools.cookbook.chapter08.domain.Technician; 11 | 12 | public class TechnicianMove implements Move { 13 | 14 | private ServiceRequest serviceRequest; 15 | private Technician technician; 16 | 17 | public TechnicianMove(ServiceRequest serviceRequest, Technician technician) { 18 | this.serviceRequest = serviceRequest; 19 | this.technician = technician; 20 | } 21 | 22 | @Override 23 | public boolean isMoveDoable(WorkingMemory workingMemory) { 24 | return !serviceRequest.getTechnician().equals(technician); 25 | } 26 | 27 | @Override 28 | public Move createUndoMove(WorkingMemory workingMemory) { 29 | return new TechnicianMove(serviceRequest, serviceRequest.getTechnician()); 30 | } 31 | 32 | @Override 33 | public void doMove(WorkingMemory workingMemory) { 34 | FactHandle serviceRequestHandle = workingMemory.getFactHandle(serviceRequest); 35 | serviceRequest.setTechnician(technician); 36 | workingMemory.update(serviceRequestHandle, serviceRequest); 37 | } 38 | 39 | @Override 40 | public boolean equals(Object o) { 41 | if (this == o) { 42 | return true; 43 | } else if (o instanceof TechnicianMove) { 44 | TechnicianMove other = (TechnicianMove) o; 45 | return new EqualsBuilder().append(serviceRequest, other.serviceRequest) 46 | .append(technician, other.technician).isEquals(); 47 | } else { 48 | return false; 49 | } 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | return new HashCodeBuilder().append(serviceRequest).append(technician).toHashCode(); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return serviceRequest.toString() + " to " + technician.toString(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/java/drools/cookbook/chapter08/planner/TechniciansSolution.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08.planner; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import org.drools.planner.api.domain.solution.PlanningEntityCollectionProperty; 8 | import org.drools.planner.core.score.SimpleScore; 9 | import org.drools.planner.core.solution.Solution; 10 | 11 | import drools.cookbook.chapter08.domain.ServiceRequest; 12 | import drools.cookbook.chapter08.domain.Technician; 13 | 14 | public class TechniciansSolution implements Solution { 15 | 16 | private List technicians; 17 | private List serviceRequests; 18 | private SimpleScore score; 19 | 20 | public TechniciansSolution(List technicians, List serviceRequests) { 21 | this.technicians = technicians; 22 | this.serviceRequests = serviceRequests; 23 | } 24 | 25 | private TechniciansSolution() { 26 | } 27 | 28 | @Override 29 | public SimpleScore getScore() { 30 | return score; 31 | } 32 | 33 | @Override 34 | public void setScore(SimpleScore score) { 35 | this.score = (SimpleScore) score; 36 | } 37 | 38 | @Override 39 | public Collection getProblemFacts() { 40 | return technicians; 41 | } 42 | 43 | @Override 44 | public Solution cloneSolution() { 45 | TechniciansSolution solution = new TechniciansSolution(); 46 | solution.score = score; 47 | solution.technicians = technicians; 48 | List clonedServices = new ArrayList(serviceRequests.size()); 49 | for (ServiceRequest sr : serviceRequests) { 50 | clonedServices.add(new ServiceRequest(sr)); 51 | } 52 | solution.serviceRequests = clonedServices; 53 | return solution; 54 | } 55 | 56 | public List getTechnicians() { 57 | return technicians; 58 | } 59 | 60 | @PlanningEntityCollectionProperty 61 | public List getServiceRequests() { 62 | return serviceRequests; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/resources/ServiceRequestSolverConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | DEBUG 4 | drools.cookbook.chapter08.planner.TechniciansSolution 5 | drools.cookbook.chapter08.domain.ServiceRequest 6 | /serviceRequestScoreRules.drl 7 | 8 | SIMPLE 9 | 10 | 11 | FIRST_FIT 12 | 13 | 14 | 15 | OR 16 | 100 17 | 0 18 | 19 | 20 | drools.cookbook.chapter08.planner.SelectNextTechnicians 21 | 22 | 23 | 5 24 | 25 | 26 | NEVER 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/main/resources/serviceRequestScoreRules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter08; 2 | 3 | import java.util.EnumSet; 4 | import java.util.Set; 5 | import drools.cookbook.chapter08.domain.ServiceRequest; 6 | import drools.cookbook.chapter08.domain.Skill; 7 | import org.drools.planner.core.score.constraint.IntConstraintOccurrence; 8 | import org.drools.planner.core.score.calculator.SimpleScoreCalculator; 9 | 10 | global SimpleScoreCalculator scoreCalculator; 11 | 12 | rule "sameCity" 13 | when 14 | $sd : ServiceRequest(technician.location != location) 15 | then 16 | insertLogical(new IntConstraintOccurrence("sameCity", 1, $sd)); 17 | end 18 | 19 | rule "isBusy" 20 | when 21 | $sd : ServiceRequest(technician.busy == true) 22 | then 23 | insertLogical(new IntConstraintOccurrence("isBusy", 6, $sd)); 24 | end 25 | 26 | rule "skillMatch" 27 | when 28 | $sd : ServiceRequest($neededSkills : neededSkills, $tec : technician) 29 | then 30 | Set tempSkills = EnumSet.copyOf($neededSkills); 31 | tempSkills.removeAll($tec.getSkills()); 32 | insertLogical(new IntConstraintOccurrence("skillMatch", tempSkills.size() * 3, $sd)); 33 | end 34 | 35 | rule "hardConstraintsBroken" 36 | salience -1 // Do the other rules first (optional, for performance) 37 | when 38 | $hardTotal : Number() from accumulate(IntConstraintOccurrence($weight : weight), 39 | sum($weight)) 40 | then 41 | scoreCalculator.setScore(- $hardTotal.intValue()); 42 | end 43 | -------------------------------------------------------------------------------- /chapter08/simulated-annealing/src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter09/business-activity-monitoring-reports/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter09 7 | 1.0.0 8 | 9 | business-activity-monitoring-reports 10 | Business activity monitoring and reports 11 | 12 | 13 | 14 | org.jbpm 15 | jbpm-flow 16 | 17 | 18 | org.jbpm 19 | jbpm-flow-builder 20 | 21 | 22 | org.jbpm 23 | jbpm-bpmn2 24 | 25 | 26 | org.jbpm 27 | jbpm-bam 28 | 29 | 30 | com.h2database 31 | h2 32 | 33 | 34 | javassist 35 | javassist 36 | 3.4.GA 37 | 38 | 39 | mysql 40 | mysql-connector-java 41 | 5.1.3 42 | 43 | 44 | org.slf4j 45 | slf4j-api 46 | 1.6.1 47 | 48 | 49 | org.slf4j 50 | slf4j-log4j12 51 | 1.6.1 52 | 53 | 54 | junit 55 | junit 56 | test 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /chapter09/business-activity-monitoring-reports/src/main/resources/AuditLog.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /chapter09/business-activity-monitoring-reports/src/main/resources/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | com.mysql.jdbc.Driver 25 | jdbc:mysql://192.168.0.12/jbpmbam 26 | fm_user 27 | fm_pass 28 | 1 29 | org.hibernate.dialect.MySQLDialect 30 | thread 31 | org.hibernate.cache.NoCacheProvider 32 | false 33 | create 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /chapter09/business-activity-monitoring-reports/src/test/java/drools/cookbook/chapter09/BAMReporting.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter09; import java.util.Random; import org.drools.KnowledgeBase; import org.drools.builder.KnowledgeBuilder; import org.drools.builder.KnowledgeBuilderError; import org.drools.builder.KnowledgeBuilderFactory; import org.drools.builder.ResourceType; import org.drools.io.ResourceFactory; import org.drools.runtime.StatefulKnowledgeSession; import org.drools.runtime.process.ProcessInstance; import org.jbpm.process.audit.WorkingMemoryDbLogger; /** * * @author Lucas Amador * */ public class BAMReporting { public static void main(String[] args) throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("processWithSignalEvent.bpmn"), ResourceType.BPMN2); kbuilder.add(ResourceFactory.newClassPathResource("processWithScriptTask.bpmn"), ResourceType.BPMN2); if (kbuilder.hasErrors()) { for (KnowledgeBuilderError error : kbuilder.getErrors()) { System.err.println(error); } throw new IllegalArgumentException("Unable to parse knowledge."); } KnowledgeBase kbase = kbuilder.newKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); WorkingMemoryDbLogger historyLogger = new WorkingMemoryDbLogger(ksession); for (int i = 0; i < 20; i++) { ksession.startProcess("processWithScriptTask"); } Random random = new Random(); for (int i = 0; i < 50; i++) { ProcessInstance processInstance = ksession.startProcess("processWithSignalEvent"); if (random.nextBoolean()) { Thread.sleep(random.nextInt(3000)); ksession.signalEvent("mySignal", null, processInstance.getId()); } } ksession.dispose(); historyLogger.dispose(); } } -------------------------------------------------------------------------------- /chapter09/business-monitoring-with-drools-fusion/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter09 7 | 1.0.0 8 | 9 | business-monitoring-with-drools-fusion 10 | Monitoring business process with Drools Fusion 11 | 12 | 13 | 14 | org.drools 15 | knowledge-api 16 | 17 | 18 | org.drools 19 | drools-core 20 | 21 | 22 | org.drools 23 | drools-compiler 24 | 25 | 26 | org.jbpm 27 | jbpm-flow 28 | 29 | 30 | org.jbpm 31 | jbpm-flow-builder 32 | 33 | 34 | org.jbpm 35 | jbpm-bpmn2 36 | 37 | 38 | junit 39 | junit 40 | test 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /chapter09/business-monitoring-with-drools-fusion/src/main/java/drools/cookbook/chapter09/CustomProcessEventListener.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter09; 2 | 3 | import org.drools.event.process.ProcessCompletedEvent; 4 | import org.drools.event.process.ProcessEventListener; 5 | import org.drools.event.process.ProcessNodeLeftEvent; 6 | import org.drools.event.process.ProcessNodeTriggeredEvent; 7 | import org.drools.event.process.ProcessStartedEvent; 8 | import org.drools.event.process.ProcessVariableChangedEvent; 9 | import org.drools.runtime.StatefulKnowledgeSession; 10 | 11 | /** 12 | * 13 | * @author Lucas Amador 14 | * 15 | */ 16 | public class CustomProcessEventListener implements ProcessEventListener { 17 | 18 | private StatefulKnowledgeSession eventKsession; 19 | 20 | public CustomProcessEventListener(StatefulKnowledgeSession ksession) { 21 | eventKsession = ksession; 22 | } 23 | 24 | public void beforeProcessStarted(ProcessStartedEvent event) { 25 | eventKsession.insert(event); 26 | eventKsession.fireAllRules(); 27 | } 28 | 29 | public void afterNodeLeft(ProcessNodeLeftEvent arg0) { 30 | } 31 | 32 | public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) { 33 | } 34 | 35 | public void afterProcessCompleted(ProcessCompletedEvent event) { 36 | } 37 | 38 | public void afterProcessStarted(ProcessStartedEvent arg0) { 39 | } 40 | 41 | public void beforeNodeLeft(ProcessNodeLeftEvent arg0) { 42 | } 43 | 44 | public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) { 45 | } 46 | 47 | public void beforeProcessCompleted(ProcessCompletedEvent arg0) { 48 | } 49 | 50 | public void afterVariableChanged(ProcessVariableChangedEvent arg0) { 51 | } 52 | 53 | public void beforeVariableChanged(ProcessVariableChangedEvent arg0) { 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /chapter09/business-monitoring-with-drools-fusion/src/main/resources/withdrawalRules.drl: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter9; 2 | 3 | import org.drools.event.process.ProcessStartedEvent; 4 | 5 | declare ProcessStartedEvent 6 | @role(event) 7 | @expires(1h) 8 | end 9 | 10 | rule "More than 10 withdrawals in less than one hour " 11 | dialect "java" 12 | when 13 | Number(processesCount : intValue > 10) from accumulate( 14 | e : ProcessStartedEvent(processInstance.processId == "withdrawalProcess") over window:size(1h), 15 | count(e)) 16 | then 17 | System.err.println("Warning: more than 10 withdrawals in the last hour. Processes count: " + processesCount); 18 | end 19 | -------------------------------------------------------------------------------- /chapter09/creating-process-using-the-api/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter09 7 | 1.0.0 8 | 9 | creating-process-using-the-api 10 | Creating process using the API 11 | 12 | 13 | 14 | org.jbpm 15 | jbpm-flow 16 | 17 | 18 | org.jbpm 19 | jbpm-flow-builder 20 | 21 | 22 | junit 23 | junit 24 | test 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /chapter09/human-tasks/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter09 7 | 1.0.0 8 | 9 | human-tasks 10 | Human Tasks 11 | 12 | 13 | 14 | org.jbpm 15 | jbpm-flow 16 | 17 | 18 | org.jbpm 19 | jbpm-flow-builder 20 | 21 | 22 | org.jbpm 23 | jbpm-bpmn2 24 | 25 | 26 | org.jbpm 27 | jbpm-human-task 28 | 29 | 30 | com.h2database 31 | h2 32 | 33 | 34 | junit 35 | junit 36 | test 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /chapter09/unit-testing/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | drools.cookbook 6 | chapter09 7 | 1.0.0 8 | 9 | unit-testing 10 | Testing jBPM using junit 11 | 12 | 13 | 14 | org.jbpm 15 | jbpm-flow 16 | 17 | 18 | org.jbpm 19 | jbpm-flow-builder 20 | 21 | 22 | org.jbpm 23 | jbpm-bam 24 | 25 | 26 | org.jbpm 27 | jbpm-bpmn2 28 | 29 | 30 | org.jbpm 31 | jbpm-persistence-jpa 32 | 33 | 34 | org.codehaus.btm 35 | btm 36 | 1.3.3 37 | test 38 | 39 | 40 | com.h2database 41 | h2 42 | 1.2.128 43 | 44 | 45 | org.slf4j 46 | slf4j-api 47 | 1.6.1 48 | 49 | 50 | org.slf4j 51 | slf4j-log4j12 52 | 1.6.1 53 | 54 | 55 | junit 56 | junit 57 | test 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /chapter09/unit-testing/src/test/java/drools/cookbook/chapter09/SimpleProcessTest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter09; 2 | 3 | import org.drools.KnowledgeBase; 4 | import org.drools.runtime.StatefulKnowledgeSession; 5 | import org.drools.runtime.process.ProcessInstance; 6 | import org.junit.Assert; 7 | 8 | /** 9 | * 10 | * @author Lucas Amador 11 | * 12 | */ 13 | public class SimpleProcessTest extends JbpmJUnitTestCase { 14 | 15 | public SimpleProcessTest() { 16 | super(false); 17 | } 18 | 19 | public void testProcessWithSignalEvent() throws Exception { 20 | KnowledgeBase kbase = createKnowledgeBase("processWithSignalEvent.bpmn"); 21 | StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); 22 | ProcessInstance processInstance = ksession.startProcess("processWithSignalEvent"); 23 | Assert.assertNotNull(processInstance); 24 | assertProcessInstanceActive(processInstance.getId(), ksession); 25 | assertNodeActive(processInstance.getId(), ksession, "Catch Signal Event"); 26 | ksession.signalEvent("mySignal", null, processInstance.getId()); 27 | assertProcessInstanceCompleted(processInstance.getId(), ksession); 28 | } 29 | 30 | public void testProcessWithScriptTask() throws Exception { 31 | KnowledgeBase kbase = createKnowledgeBase("processWithScriptTask.bpmn"); 32 | StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); 33 | ProcessInstance processInstance = ksession.startProcess("processWithScriptTask"); 34 | Assert.assertNotNull(processInstance); 35 | assertProcessInstanceCompleted(processInstance.getId(), ksession); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /chapter09/unit-testing/src/test/java/drools/cookbook/chapter09/SimpleProcessWithPersistenceTest.java: -------------------------------------------------------------------------------- 1 | package drools.cookbook.chapter09; 2 | 3 | import org.drools.KnowledgeBase; 4 | import org.drools.runtime.StatefulKnowledgeSession; 5 | import org.drools.runtime.process.ProcessInstance; 6 | import org.junit.Assert; 7 | 8 | /** 9 | * 10 | * @author Lucas Amador 11 | * 12 | */ 13 | public class SimpleProcessWithPersistenceTest extends JbpmJUnitTestCase { 14 | 15 | public void testProcessWithSignalEventUsingPersistence() throws Exception { 16 | KnowledgeBase kbase = createKnowledgeBase("processWithSignalEvent.bpmn"); 17 | StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); 18 | ProcessInstance processInstance = ksession.startProcess("processWithSignalEvent"); 19 | Assert.assertNotNull(processInstance); 20 | assertProcessInstanceActive(processInstance.getId(), ksession); 21 | ksession = restoreSession(ksession, false); 22 | ksession.signalEvent("mySignal", null, processInstance.getId()); 23 | assertProcessInstanceCompleted(processInstance.getId(), ksession); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /chapter09/unit-testing/src/test/resources/META-INF/orm.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | select 9 | processInstanceInfo.processInstanceId 10 | from 11 | ProcessInstanceInfo processInstanceInfo 12 | where 13 | :type in elements(processInstanceInfo.eventTypes) 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /chapter09/unit-testing/src/test/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | org.hibernate.ejb.HibernatePersistence 5 | jdbc/jbpmDS 6 | org.jbpm.persistence.processinstance.ProcessInstanceInfo 7 | org.drools.persistence.info.SessionInfo 8 | org.drools.persistence.info.WorkItemInfo 9 | org.jbpm.process.audit.ProcessInstanceLog 10 | org.jbpm.process.audit.NodeInstanceLog 11 | org.jbpm.process.audit.VariableInstanceLog 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /chapter09/unit-testing/src/test/resources/jndi.properties: -------------------------------------------------------------------------------- 1 | java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory --------------------------------------------------------------------------------