[jboss-svn-commits] JBL Code SVN: r17036 - in labs/jbossrules/branches/temporal_rete/drools-compiler/src/test: java/org/drools/integrationtests/eventgenerator/example and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Dec 5 09:45:14 EST 2007
Author: mgroch
Date: 2007-12-05 09:45:14 -0500 (Wed, 05 Dec 2007)
New Revision: 17036
Added:
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ExampleScenario.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/FailureEvent.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/HeartbeatEvent.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ProductionEvent.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Resource.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/SlidingWindow.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Status.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/StatusChangedEvent.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Tools.java
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/example_scenario.drl
Modified:
labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/test_eventGenerator.drl
Log:
Added sample scenario using the event generator
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ExampleScenario.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ExampleScenario.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ExampleScenario.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,129 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
+
+import org.drools.RuleBase;
+import org.drools.RuleBaseFactory;
+import org.drools.WorkingMemory;
+import org.drools.compiler.DroolsParserException;
+import org.drools.compiler.PackageBuilder;
+import org.drools.integrationtests.eventgenerator.PseudoSessionClock;
+import org.drools.integrationtests.eventgenerator.SimpleEventGenerator;
+import org.drools.integrationtests.eventgenerator.SimpleEventListener;
+import org.drools.rule.Package;
+
+
+public class ExampleScenario {
+
+ // constants
+ public final static String FILE_NAME_RULES = "../example_scenario.drl";
+ //public final static String FILE_NAME_LOGGER = "log/event";
+ private static int NUMBER_RESOURCES = 3;
+
+ // event occurrence probabilities
+ public final static int AVG_OCCUR_PRODUCTION_EVENT = 7000; // average time in milliseconds after which another item is manufactured by one resource; default: 700 ms
+ public final static int MIN_OCCUR_PRODUCTION_EVENT = 4000; // minimum time in milliseconds after which another item is manufactured by one resource; default: 700 ms
+ public final static int AVG_OCCUR_HEARTBEAT_EVENT = 90000; // average time in milliseconds after which a resource sends another heartbeat; default: 60000 ms
+ public final static int MIN_OCCUR_HEARTBEAT_EVENT = 45000; // average time in milliseconds after which a resource sends another heartbeat; default: 60000 ms
+ public final static int AVG_OCCUR_ALERT_EVENT = 1800000; // average time in milliseconds after which an alarm is sent; default: 1800000 = 30 mis
+ public final static int MIN_OCCUR_ALERT_EVENT = 0; // average time in milliseconds after which an alarm is sent; default: 1800000 = 30 mis
+
+ private static WorkingMemory wm;
+ //private static WorkingMemoryFileLogger logger;
+
+ public static void setup(){
+ // read in the source
+ Reader source = new InputStreamReader (ExampleScenario.class.getResourceAsStream(FILE_NAME_RULES));
+ // Use package builder to build up a rule package.
+ // An alternative lower level class called DrlParser can also be used ...
+ PackageBuilder builder = new PackageBuilder();
+ // this will parse and compile in one step
+ // NOTE: There are 2 methods here, the one argument one is for normal DRL.
+ try {
+ builder.addPackageFromDrl(source);
+ } catch (DroolsParserException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ // get the compiled package (which is serializable)
+ Package pkg = builder.getPackage();
+
+ // add defined object types
+ //FactTemplate ftEvent = new FactTemplateImpl();
+ //pkg.addFactTemplate(ftEvent);
+
+ // add the package to a rulebase (deploy the rule package).
+ RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ try {
+ ruleBase.addPackage (pkg);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ wm = ruleBase.newStatefulSession();
+ // create a new Working Memory Logger, that logs to file.
+ //logger = new WorkingMemoryFileLogger(wm);
+ // an event.log file is created in the log dir (which must exist)
+ // in the working directory
+ //logger.setFileName(FILE_NAME_LOGGER);
+ }
+
+ public static final void main (String[] args) {
+
+ setup();
+
+ System.out.println("Waiting for messages...");
+ System.out.println("Press [return] to quit\n");
+
+ ArrayList<Resource> resources = new ArrayList<Resource>();
+
+ //
+ SimpleEventGenerator myGenerator = new SimpleEventGenerator(wm, new SimpleEventListener(wm), PseudoSessionClock.timeInMinutes(15));
+
+ //create fab resources and add them to working memory
+ for (int i = 0; i < NUMBER_RESOURCES; i++){
+
+ Resource res = new Resource("mach"+i);
+ resources.add(res);
+ wm.insert(res.getOpStatus());
+
+ SlidingWindow sw = new SlidingWindow(0, res.getId(), PseudoSessionClock.timeInMinutes(10), PseudoSessionClock.timeInMinutes(2));
+ //GlobalWorkingMemory.getInstance().insert(new Event(Event.SLIDING_WINDOW, res.getId(), systemTime, systemTime));
+ wm.insert(sw);
+
+ // add eventSenders to EventGenerator
+ myGenerator.addEventSource("Conveyor"+i, new ProductionEvent(res.getId()), MIN_OCCUR_PRODUCTION_EVENT, AVG_OCCUR_PRODUCTION_EVENT, 0, 0);
+ }
+
+ // start generating events
+ myGenerator.generate();
+
+ BufferedReader waiter = new BufferedReader(new InputStreamReader(System.in));
+ try {
+ waiter.readLine();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ // stop logging
+ //logger.writeToDisk();
+ //System.out.println("Application terminated - Audit log written to disk");
+
+ }
+
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/FailureEvent.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/FailureEvent.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/FailureEvent.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,33 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import org.drools.integrationtests.eventgenerator.Event;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class FailureEvent extends Event {
+
+ /**
+ * Special constructor for a failure event
+ * @param parentId The id of the corresponding site, resource, ...
+ */
+ public FailureEvent(String parentId) {
+ super(EventType.FAILURE, parentId);
+ }
+
+ /**
+ * Special constructor for a faliure event
+ * @param parentId The id of the corresponding site, resource, ...
+ * @param start The start instance of the event.
+ * @param end The end instance of the event.
+ * @param parameters The event parameters.
+ */
+ public FailureEvent(String parentId, long start, long end) {
+ super(EventType.FAILURE, parentId, start, end);
+ }
+
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/HeartbeatEvent.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/HeartbeatEvent.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/HeartbeatEvent.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,33 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import org.drools.integrationtests.eventgenerator.Event;
+
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class HeartbeatEvent extends Event {
+
+ /**
+ * Special constructor for a heartbeat event
+ * @param parentId The id of the corresponding site, resource, ...
+ */
+ public HeartbeatEvent(String parentId) {
+ super(EventType.HEARTBEAT, parentId);
+ }
+
+ /**
+ * Special constructor for a heartbeat event
+ * @param parentId The id of the corresponding site, resource, ...
+ * @param start The start instance of the event.
+ * @param end The end instance of the event.
+ * @param parameters The event parameters.
+ */
+ public HeartbeatEvent(String parentId, long start, long end) {
+ super(EventType.HEARTBEAT, parentId, start, end);
+ }
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ProductionEvent.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ProductionEvent.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/ProductionEvent.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,32 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import org.drools.integrationtests.eventgenerator.Event;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class ProductionEvent extends Event {
+
+ /**
+ * Special constructor for a production event
+ * @param parentId The id of the corresponding site, resource, ...
+ */
+ public ProductionEvent(String parentId) {
+ super(EventType.PRODUCTION, parentId);
+ }
+
+ /**
+ * Special constructor for a production event
+ * @param parentId The id of the corresponding site, resource, ...
+ * @param start The start instance of the event.
+ * @param end The end instance of the event.
+ * @param parameters The event parameters.
+ */
+ public ProductionEvent(String parentId, long start, long end) {
+ super(EventType.PRODUCTION, parentId, start, end);
+ }
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Resource.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Resource.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Resource.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,121 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import java.util.Calendar;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class Resource {
+
+ public static final String[] OPERATIONAL_STATUS_VALUES = {"RED", "YELLOW", "GREEN"};
+
+ public static final int STATUS_RED = 0;
+ public static final int STATUS_YELLOW = 1;
+ public static final int STATUS_GREEN = 2;
+
+ private static int idCounter = 0;
+
+ private String id;
+ private String name;
+ private double pressure, temperature;
+ private Calendar lastHeartBeat;
+ private Status opStatus;
+
+ public Resource(String name) {
+ this.id = String.valueOf(idCounter++);
+ this.name = name;
+ this.pressure = 0;
+ this.temperature = 0;
+ this.lastHeartBeat = null;
+ this.opStatus = new Status(Status.OPERATIONAL, this.id, OPERATIONAL_STATUS_VALUES, STATUS_RED);
+ }
+
+ /**
+ * @return the id
+ */
+ public String getId() {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the resource type
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param type the resource type to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @return the opStatus
+ */
+ public Status getOpStatus() {
+ return opStatus;
+ }
+
+ /**
+ * @param opStatus the opStatus to set
+ */
+ public void setOpStatus(Status status) {
+ this.opStatus = status;
+ }
+
+ /**
+ * @return the pressure
+ */
+ public double getPressure() {
+ return pressure;
+ }
+
+ /**
+ * @param pressure the pressure to set
+ */
+ public void setPressure(double pressure) {
+ this.pressure = pressure;
+ }
+
+ /**
+ * @return the temperature
+ */
+ public double getTemperature() {
+ return temperature;
+ }
+
+ /**
+ * @param temperature the temperature to set
+ */
+ public void setTemperature(double temperature) {
+ this.temperature = temperature;
+ }
+
+ /**
+ * @return the lastHeartBeat
+ */
+ public Calendar getLastHeartBeat() {
+ return lastHeartBeat;
+ }
+
+ /**
+ * @param lastHeartBeat the lastHeartBeat to set
+ */
+ public void setLastHeartBeat(Calendar lastHeartBeat) {
+ this.lastHeartBeat = lastHeartBeat;
+ }
+
+}
\ No newline at end of file
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/SlidingWindow.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/SlidingWindow.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/SlidingWindow.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,312 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class SlidingWindow {
+
+ // Sliding window states names
+ public static final String[] SW_STATES = {"Default", "Collecting", "Evaluating", "Advancing"};
+
+ public static final int DEFAULT = 0;
+ public static final int COLLECT = 1;
+ public static final int EVALUATE = 2;
+ public static final int ADVANCE = 3;
+
+ private static int idCounter = 0;
+
+ private int id, state;
+ private boolean readyToEvaluate;
+ private String parentId;
+ private long windowStart, windowEnd, lastUpdate;
+ private long windowLength, windowShift;
+ //private List<Event> eventsInWindow;
+ int numberEvents;
+ /*private int defectiveFridgesCount, defectiveFreezersCount;
+ private int workingFridgesCount, workingFreezersCount;*/
+
+ public SlidingWindow(long wStart, String parentId, long windowLength, long windowShift) {
+ this.id = idCounter++;
+ this.readyToEvaluate = false;
+ this.state = DEFAULT;
+ this.parentId = parentId;
+ this.windowStart = wStart;
+ this.windowEnd = this.windowStart + windowLength;
+ this.lastUpdate = wStart;
+ this.windowLength = windowLength;
+ this.windowShift = windowShift;
+ //this.eventsInWindow = new ArrayList<Event>();
+ this.numberEvents = 0;
+ //System.out.println ("Sliding window "+this.id+" expires at "+Tools.formattedDate(this.windowEnd));
+ }
+
+ /**
+ * @return the id
+ */
+ public String getId() {
+ return "sw"+id;
+ }
+
+ public long getWindowStart() {
+ return windowStart;
+ }
+
+ public void setWindowStart(long start) {
+ this.windowStart = start;
+ }
+
+ public long getWindowEnd() {
+ return windowEnd;
+ }
+
+ public void setWindowEnd(long end) {
+ this.windowEnd = end;
+ }
+
+ /**
+ * @return the windowLength
+ */
+ public long getWindowLength() {
+ return windowLength;
+ }
+
+ /**
+ * @param windowLength the windowLength to set
+ */
+ public void setWindowLength(int windowLength) {
+ this.windowLength = windowLength;
+ }
+
+ /**
+ * @return the shift
+ */
+ public long getWindowShift() {
+ return windowShift;
+ }
+
+ /**
+ * @param shift the shift to set
+ */
+ public void setShift(int windowShift) {
+ this.windowShift = windowShift;
+ }
+
+ /**
+ * @return the numberEvents
+ */
+ public int getNumberEvents() {
+ return numberEvents;
+ }
+
+ /**
+ * @param numberEvents the numberEvents to set
+ */
+ public void setNumberEvents(int numberEvents) {
+ this.numberEvents = numberEvents;
+ }
+
+/* *//**
+ * @return the eventsInWindow
+ *//*
+ public int getNumberEventsInWindow() {
+ return this.eventsInWindow.size();
+ }
+
+ *//**
+ * @return the eventsInWindow
+ *//*
+ public List<Event> getEventsInWindow() {
+ return eventsInWindow;
+ }
+
+ *//**
+ * @param eventsInWindow the eventsInWindow to set
+ *//*
+ public void setEventsInWindow(List<Event> eventsInWindow) {
+ this.eventsInWindow = eventsInWindow;
+ }
+
+ *//**
+ * @param additionalEventsInWindow the additional events in window to add
+ *//*
+ public void addEventsInWindow(List<Event> additionalEventsInWindow) {
+ this.eventsInWindow.addAll(additionalEventsInWindow);
+ }
+
+ *//**
+ * @param expiredEventsInWindow the events in window to remove
+ *//*
+ public void removeEventsInWindow(List<Event> expiredEventsInWindow) {
+ this.eventsInWindow.removeAll(expiredEventsInWindow);
+ }*/
+
+ /**
+ * @return the parentId
+ */
+ public String getParentId() {
+ return parentId;
+ }
+
+ /**
+ * @param parentId the parentId to set
+ */
+ public void setParentId(String parentId) {
+ this.parentId = parentId;
+ }
+
+ /**
+ * Advances the sliding window
+ */
+ public void advance() {
+ this.windowStart += this.windowShift;
+ this.windowEnd += this.windowShift;
+ //System.out.println ("Sliding window "+this.getId()+" is reset to "+Tools.formattedInterval(this.windowStart, this.windowEnd));
+ }
+
+ /**
+ * @return the lastUpdate
+ */
+ public long getLastUpdate() {
+ return lastUpdate;
+ }
+
+ /**
+ * @param lastUpdate the lastUpdate to set
+ */
+ public void setLastUpdate(long lastUpdate) {
+ this.lastUpdate = lastUpdate;
+ }
+
+ /**
+ * @return the state
+ */
+ public int getState() {
+ return state;
+ }
+
+ /**
+ * @param state the state to set
+ */
+ public void setState(int state) {
+ this.state = state;
+ }
+
+ /**
+ * @return the readyForAction
+ */
+ public boolean isReadyToEvaluate() {
+ return readyToEvaluate;
+ }
+
+ /**
+ * @param readyForAction the readyForAction to set
+ */
+ public void setReadyToEvaluate(boolean readyToEvaluate) {
+ this.readyToEvaluate = readyToEvaluate;
+ }
+
+ /**
+ * @return the workingFreezersCount
+ *//*
+ public int getWorkingFreezersCount() {
+ return workingFreezersCount;
+ }
+
+ *//**
+ * @param workingFreezersCount the workingFreezersCount to set
+ *//*
+ public void setWorkingFreezersCount(int workingFreezersCount) {
+ this.workingFreezersCount = workingFreezersCount;
+ }
+
+ *//**
+ * @return the workingFridgesCount
+ *//*
+ public int getWorkingFridgesCount() {
+ return workingFridgesCount;
+ }
+
+ *//**
+ * @param workingFridgesCount the workingFridgesCount to set
+ *//*
+ public void setWorkingFridgesCount(int workingFridgesCount) {
+ this.workingFridgesCount = workingFridgesCount;
+ }
+
+ public int getDefectiveFridgesCount() {
+ return defectiveFridgesCount;
+ }
+
+ public void setDefectiveFridgesCount(int defectiveFridgesCount) {
+ this.defectiveFridgesCount = defectiveFridgesCount;
+ }
+
+ public int getDefectiveFreezersCount() {
+ return defectiveFreezersCount;
+ }
+
+ public void setDefectiveFreezersCount(int defectiveFreezersCount) {
+ this.defectiveFreezersCount = defectiveFreezersCount;
+ }
+
+ public int getWorkingDevicesCount() {
+ return workingFridgesCount+workingFreezersCount;
+ }
+
+ public int getDefectiveDevicesCount() {
+ return defectiveFridgesCount+defectiveFreezersCount;
+ }
+
+ public int getOverallFridgesCount() {
+ return getWorkingFridgesCount()+getDefectiveFridgesCount();
+ }
+
+ public int getOverallFreezersCount() {
+ return getWorkingFreezersCount()+getDefectiveFreezersCount();
+ }
+
+ public int getOverallDevicesCount() {
+ return getWorkingDevicesCount()+getDefectiveDevicesCount();
+ }
+
+ public double getWorkingFridgesRatio() {
+ if (getOverallFridgesCount() == 0)
+ return 0;
+ return (double)getWorkingFridgesCount()/getOverallFridgesCount();
+ }
+
+ public double getDefectiveFridgesRatio() {
+ if (getOverallFridgesCount() == 0)
+ return 0;
+ return (double)getDefectiveFridgesCount()/getOverallFridgesCount();
+ }
+
+ public double getWorkingFreezersRatio() {
+ if (getOverallFreezersCount() == 0)
+ return 0;
+ return (double)getWorkingFreezersCount()/getOverallFreezersCount();
+ }
+
+ public double getDefectiveFreezersRatio() {
+ if (getOverallFreezersCount() == 0)
+ return 0;
+ return (double)getDefectiveFreezersCount()/getOverallFreezersCount();
+ }
+
+ public double getWorkingDevicesRatio() {
+ if (getOverallDevicesCount() == 0)
+ return 0;
+ return (double)getWorkingDevicesCount()/getOverallDevicesCount();
+ }
+
+ public double getDefectiveDevicesRatio() {
+ if (getOverallDevicesCount() == 0)
+ return 0;
+ return (double)getDefectiveDevicesCount()/getOverallDevicesCount();
+ }*/
+
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Status.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Status.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Status.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,149 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class Status {
+
+ // Status names
+ public static final String[] STATUS_NAMES = {"Operational status"};
+
+ public static final int OPERATIONAL = 0;
+
+ ////////////////////////////////////////////////////////////////////
+
+ private int id;
+ private String resourceId;
+ private List<String> valueList;
+ private int currentValue;
+ private long hasValueSince;
+
+ /**
+ * @param id The status name.
+ * @param resourceId The id of the corresponding resource.
+ */
+ public Status(int id, String resourceId) {
+ this.id = id;
+ this.resourceId = resourceId;
+ this.valueList = new ArrayList<String>();
+ this.hasValueSince = 0;
+ }
+
+ /**
+ * @param id The status name.
+ * @param resourceId The id of the corresponding resource.
+ * @param currentValue current value of the status.
+ */
+ public Status(int id, String resourceId, int currentValue) {
+ this (id, resourceId);
+ this.currentValue = currentValue;
+ }
+
+ /**
+ * @param id The status name.
+ * @param resourceId The id of the corresponding resource.
+ * @param valueList The values this status can have.
+ * @param currentValue current value of the status.
+ */
+ public Status(int id, String resourceId, String[] valueList, int currentValue) {
+ this (id, resourceId, currentValue);
+ this.addValues(valueList);
+ }
+
+ /**
+ * @return the status id
+ */
+ public int getId() {
+ return this.id;
+ }
+
+ /**
+ * @param name the status id to set
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the resourceId
+ */
+ public String getResourceId() {
+ return resourceId;
+ }
+
+ /**
+ * @param resourceId the resourceId to set
+ */
+ public void setResourceId(String resourceId) {
+ this.resourceId = resourceId;
+ }
+
+ /**
+ * @return the currentValue
+ */
+ public int getCurrentValue() {
+ return currentValue;
+ }
+
+ /**
+ * @param currentValue the currentValue to set
+ */
+ public void setCurrentValue(int currentValue, long sinceWhen) {
+ this.currentValue = currentValue;
+ this.hasValueSince = sinceWhen;
+ }
+
+ /**
+ * @param oldValue the oldValue to set
+ * @param newValue the newValue to set
+ */
+ public void switchCurrentValue(int oldValue, int newValue, long sinceWhen) {
+ if (this.currentValue == oldValue)
+ this.setCurrentValue(newValue, sinceWhen);
+ }
+
+ /**
+ * @param oldValue the oldValue to set
+ * @param newValue the newValue to set
+ */
+ public void switchCurrentValue(String oldValue, String newValue, long sinceWhen) {
+ this.switchCurrentValue (Integer.parseInt(oldValue), Integer.parseInt(newValue), sinceWhen);
+ }
+
+ /**
+ * @param currentValue the currentValue to set
+ *//*
+ public void setCurrentValue(int currentValue) {
+ setCurrentValue (currentValue, (Calendar)Calendar.getInstance().clone());
+ }*/
+
+ /**
+ * @return the valueList
+ */
+ public String[] getValueList() {
+ return (String[]) valueList.toArray();
+ }
+
+ /**
+ * @param valueList the valueList to set
+ */
+ public void addValues(String[] valueList) {
+ this.valueList.addAll(Arrays.asList(valueList));
+ }
+
+ /**
+ * @return the hasValueSince
+ */
+ public long getHasValueSince() {
+ return hasValueSince;
+ }
+
+}
\ No newline at end of file
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/StatusChangedEvent.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/StatusChangedEvent.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/StatusChangedEvent.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,78 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.integrationtests.eventgenerator.Event;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class StatusChangedEvent extends Event {
+
+ public static final String[] PROPERTY_FIELD_NAMES = {"OldValue", "NewValue"};
+
+ public static final int OLD_VALUE = 0;
+ public static final int NEW_VALUE = 1;
+
+ /**
+ * Special constructor for a statusChanged event
+ * @param parentId The id of the corresponding site, resource, ...
+ */
+ public StatusChangedEvent(String parentId, int oldValue, int newValue) {
+ super(EventType.STATUSCHANGED, parentId);
+ addParameters(createStatusChangedParameters(oldValue, newValue));
+ }
+
+ /**
+ * Special constructor for a statusChanged event
+ * @param parentId The id of the corresponding site, resource, ...
+ * @param start The start instance of the event.
+ * @param end The end instance of the event.
+ * @param parameters The event parameters.
+ */
+ public StatusChangedEvent(String parentId, long start, long end) {
+ super(EventType.STATUSCHANGED, parentId, start, end);
+ }
+
+ /**
+ * Special constructor for a statusChanged event
+ * @param parentId The id of the corresponding site, resource, ...
+ * @param start The start instance of the event.
+ * @param end The end instance of the event.
+ * @param parameters The event parameters.
+ */
+ public StatusChangedEvent(String parentId, int oldValue, int newValue, long start, long end) {
+ super(EventType.STATUSCHANGED, parentId, start, end);
+ addParameters(createStatusChangedParameters(oldValue, newValue));
+ }
+
+ private static Map<String, String> createStatusChangedParameters(int oldValue, int newValue){
+ Map<String, String> params = new HashMap<String, String>();
+ params.put(PROPERTY_FIELD_NAMES[OLD_VALUE], String.valueOf(oldValue));
+ params.put(PROPERTY_FIELD_NAMES[NEW_VALUE], String.valueOf(newValue));
+ return params;
+ }
+
+ /**
+ * @return value of the oldValue parameter if such a parameter exists, null otherwise
+ */
+ public String getParamOldValue() {
+ if (this.getParameters().containsKey(PROPERTY_FIELD_NAMES[OLD_VALUE]))
+ return this.getParamValue(PROPERTY_FIELD_NAMES[OLD_VALUE]);
+ return null;
+ }
+
+ /**
+ * @return value of the newValue parameter if such a parameter exists, null otherwise
+ */
+ public String getParamNewValue() {
+ if (this.getParameters().containsKey(PROPERTY_FIELD_NAMES[NEW_VALUE]))
+ return this.getParamValue(PROPERTY_FIELD_NAMES[NEW_VALUE]);
+ return null;
+ }
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Tools.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Tools.java (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/java/org/drools/integrationtests/eventgenerator/example/Tools.java 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,41 @@
+/**
+ *
+ */
+package org.drools.integrationtests.eventgenerator.example;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+/**
+ * @author Matthias Groch
+ *
+ */
+public class Tools {
+
+ // Utitlity functions for console output
+ public static String formattedDate (Calendar date){
+ return
+ ((date.get(Calendar.HOUR_OF_DAY) < 10)? "0" : "") + date.get(Calendar.HOUR_OF_DAY) + ":" +
+ ((date.get(Calendar.MINUTE) < 10)? "0" : "") + date.get(Calendar.MINUTE) + ":" +
+ ((date.get(Calendar.SECOND) < 10)? "0" : "") + date.get(Calendar.SECOND) + "." +
+ ((date.get(Calendar.MILLISECOND) < 10)? "0" : "") + ((date.get(Calendar.MILLISECOND) < 100)? "0" : "") + date.get(Calendar.MILLISECOND);
+ }
+
+ public static String formattedDate (long dateInMillis){
+ Calendar date = new GregorianCalendar();
+ date.setTimeInMillis(dateInMillis);
+ return formattedDate(date);
+ }
+
+ public static String formattedInterval(Calendar start, Calendar end){
+ return "["+Tools.formattedDate(start)+".."+Tools.formattedDate(end)+"]";
+ }
+
+ public static String formattedInterval(long start, long end){
+ return "["+Tools.formattedDate(start)+".."+Tools.formattedDate(end)+"]";
+ }
+
+ public static void drawLine(){
+ System.out.println("----------------------------------------------------------------------------------------------------------------------------------");
+ }
+}
Added: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/example_scenario.drl
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/example_scenario.drl (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/example_scenario.drl 2007-12-05 14:45:14 UTC (rev 17036)
@@ -0,0 +1,100 @@
+package test;
+
+import java.lang.*;
+import org.drools.integrationtests.eventgenerator.*;
+import org.drools.integrationtests.eventgenerator.example.*;
+
+
+rule "Rule 1: check for expiry of sliding window (and reset window)"
+ no-loop true
+ #salience 0
+ when
+ PseudoSessionClock ( $currentTime : currentTime )
+ $sw : SlidingWindow ( state == SlidingWindow.DEFAULT, $windowStart : windowStart, $windowEnd : windowEnd < $currentTime )
+ then
+ System.out.println ("RULE 1 \"SLIDING WINDOW EXPIRED\" FIRED : Sliding window "+$sw.getId()+" of Resource " + $sw.getParentId() + " has expired at (" + Tools.formattedDate($currentTime) +"); collecting follows");
+ //$sw.setLastUpdate($currentTime);
+ $sw.setState(SlidingWindow.COLLECT);
+ update ($sw);
+end
+
+rule "Rule 2: collect specified objects within sliding window"
+ no-loop true
+ #salience 0
+ when
+ $sw : SlidingWindow ( state == SlidingWindow.COLLECT, $windowStart : windowStart, $windowEnd : windowEnd )
+ $evCount : Number()
+ from accumulate ( i: ProductionEvent( parentId == $sw.parentId, startTime >= $windowStart, endTime <= $windowEnd),
+ count(i) )
+ then
+ System.out.println ("RULE 2 \"ACCUMULATE EVENTS IN SW\" FIRED : Found "+$evCount.intValue()+" PRODUCTION events within sliding window "+$sw.getId()+"("+Tools.formattedInterval($windowStart, $windowEnd)+"); evaluation follows");
+ //$sw.setEventsInWindow($swQ);
+ $sw.setNumberEvents($evCount.intValue());
+ $sw.setState(SlidingWindow.EVALUATE);
+ update ($sw);
+end
+
+rule "Rule 3a: number events abnormal; set status of NON-RED resource to RED"
+ no-loop true
+ salience 0
+ when
+ $stat : Status ( id == Status.OPERATIONAL, currentValue != Resource.STATUS_RED )
+ $sw : SlidingWindow ( parentId == $stat.resourceId, state == SlidingWindow.EVALUATE, numberEvents < 80 || > 95 )
+ then
+ System.out.println ("RULE 3a \"EVALUATION OF SW\" FIRED : The number of PRODUCTION events found ("+$sw.getNumberEvents() +") within sliding window " + $sw.getId() + " is abnormal;");
+ System.out.println (" Resource "+ $sw.getParentId() +" has status " + Resource.OPERATIONAL_STATUS_VALUES[$stat.getCurrentValue()] + " and needs to be set to " + Resource.OPERATIONAL_STATUS_VALUES[Resource.STATUS_RED] + "; advance follows");
+ $sw.setState(SlidingWindow.ADVANCE);
+ update ($sw);
+ SimpleEventGenerator.sendGeneratedEvent(new StatusChangedEvent($sw.getParentId(), $stat.getCurrentValue(), Resource.STATUS_RED, $sw.getLastUpdate(), $sw.getLastUpdate()));
+end
+
+rule "Rule 3b: number events normal; set status of NON-GREEN resource to GREEN"
+ no-loop true
+ salience 0
+ when
+ $stat : Status ( id == Status.OPERATIONAL, currentValue != Resource.STATUS_GREEN )
+ $sw : SlidingWindow ( parentId == $stat.resourceId, state == SlidingWindow.EVALUATE, numberEvents >= 80 && <= 95 )
+ then
+ System.out.println ("RULE 3b \"EVALUATION OF SW\" FIRED : The number of PRODUCTION events found ("+$sw.getNumberEvents() +") within sliding window " + $sw.getId() + " is within the expected range;");
+ System.out.println (" Resource "+ $sw.getParentId() +" has status " + Resource.OPERATIONAL_STATUS_VALUES[$stat.getCurrentValue()] + " and can be set to " + Resource.OPERATIONAL_STATUS_VALUES[Resource.STATUS_GREEN] + "; advance follows");
+ $sw.setState(SlidingWindow.ADVANCE);
+ update ($sw);
+ SimpleEventGenerator.sendGeneratedEvent(new StatusChangedEvent($sw.getParentId(), $stat.getCurrentValue(), Resource.STATUS_GREEN, $sw.getLastUpdate(), $sw.getLastUpdate()));
+end
+
+rule "Rule 3c: none of the above conditions apply; however, the sliding window need to be advanced"
+ no-loop true
+ salience -1
+ when
+ $stat : Status ( id == Status.OPERATIONAL )
+ $sw : SlidingWindow ( state == SlidingWindow.EVALUATE )
+ then
+ System.out.println ("RULE 3c \"EVALUATION OF SW\" FIRED : No change of the state of the resource "+ $sw.getParentId() +" required;");
+ System.out.println (" Found "+$sw.getNumberEvents() +" PRODUCTION events within sliding window " + $sw.getId() +"; resource has status " + Resource.OPERATIONAL_STATUS_VALUES[$stat.getCurrentValue()] + "; advance follows");
+ $sw.setState(SlidingWindow.ADVANCE);
+ update ($sw);
+end
+
+rule "Rule 4: do nothing but advancing the window"
+ no-loop true
+ salience -1
+ when
+ $sw : SlidingWindow ( state == SlidingWindow.ADVANCE )
+ then
+ $sw.advance();
+ System.out.println ("RULE 4 \"ADVANCE SLIDING WINDOW\" FIRED : Reset sliding window to "+Tools.formattedInterval($sw.getWindowStart(), $sw.getWindowEnd())+"; action follows");
+ $sw.setState(SlidingWindow.DEFAULT);
+ update ($sw);
+end
+
+rule "Rule 5: status changed event - set resource status to desired value"
+ no-loop true
+ when
+ $stat : Status ( id == Status.OPERATIONAL )
+ $stChEv : StatusChangedEvent( parentId == $stat.resourceId )
+ then
+ System.out.println ("RULE 5 \"CHANGE STATUS\" FIRED : Change status from resource " + $stChEv.getParentId() + " from " + $stChEv.getParamOldValue() + " to " + $stChEv.getParamNewValue() + " (" + Tools.formattedDate($stChEv.getEndTime()) +")");
+ $stat.switchCurrentValue($stChEv.getParamOldValue(), $stChEv.getParamNewValue(), $stChEv.getEndTime());
+ update ($stat);
+ retract ($stChEv);
+end
\ No newline at end of file
Modified: labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/test_eventGenerator.drl
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/test_eventGenerator.drl 2007-12-05 13:43:45 UTC (rev 17035)
+++ labs/jbossrules/branches/temporal_rete/drools-compiler/src/test/resources/org/drools/integrationtests/eventgenerator/test_eventGenerator.drl 2007-12-05 14:45:14 UTC (rev 17036)
@@ -1,4 +1,4 @@
-package com.test;
+package test;
import org.drools.integrationtests.eventgenerator.*;
More information about the jboss-svn-commits
mailing list