[overlord-commits] Overlord SVN: r175 - in sam/trunk/modules/sim/src/main/java: org and 3 other directories.
overlord-commits at lists.jboss.org
overlord-commits at lists.jboss.org
Wed Jul 16 06:48:51 EDT 2008
Author: heiko.braun at jboss.com
Date: 2008-07-16 06:48:51 -0400 (Wed, 16 Jul 2008)
New Revision: 175
Added:
sam/trunk/modules/sim/src/main/java/org/
sam/trunk/modules/sim/src/main/java/org/jboss/
sam/trunk/modules/sim/src/main/java/org/jboss/sam/
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/ContextAwareEntity.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventMessageFactory.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSimulation.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkEntity.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkHandler.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSourceEntity.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/Measurement.java
sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/SimulationContext.java
Removed:
sam/trunk/modules/sim/src/main/java/java/
Log:
Cleanup sim module
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/ContextAwareEntity.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/ContextAwareEntity.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/ContextAwareEntity.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+/**
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public interface ContextAwareEntity
+{
+ public void begin();
+
+ public void end();
+
+ void setContext(SimulationContext ctx);
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/ContextAwareEntity.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventMessageFactory.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventMessageFactory.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventMessageFactory.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+/**
+ * Creates event messages for {@link org.jboss.sam.simulation.EventSourceEntity}'s
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public interface EventMessageFactory
+{
+ Object createEventMessage(SimulationContext context);
+
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventMessageFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSimulation.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSimulation.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSimulation.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,281 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+import org.javasim.*;
+import com.espertech.esper.client.UnmatchedListener;
+import com.espertech.esper.event.EventBean;
+import org.jboss.sam.EventProcessor;
+import org.jboss.sam.EventProcessorFactory;
+import org.jboss.sam.StreamInput;
+import org.jboss.sam.internal.esper.EsperRuntimeImpl;
+import org.jboss.sam.internal.stream.InVMStreamInput;
+import org.jboss.sam.internal.stream.InVMDispatcher;
+import org.javasim.Scheduler;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.UUID;
+
+/**
+ * Simulations are created from global configuration,
+ * which will be available to any simulation entity and a URL to
+ * {@link #bootstrap(java.util.Properties, java.net.URL)} the {@link org.jboss.sam.EventProcessor}:
+ * <p>
+ *
+ * <b>Event sources</b><br>
+ * {@link org.jboss.sam.simulation.EventSourceEntity}'s drive the whole simulation by creating and dispatching
+ * typed event messages. They need to be added to a simulation ({@link #addSource(EventSourceEntity)})
+ * in order to emit events to the event processor.
+ * <p>
+ *
+ * <b>Event sinks</b><br>
+ * {@link org.jboss.sam.simulation.EventSinkEntity}'s processes event messages that have been forwarded
+ * from an event processor. They need to be added ({@link #addSink(EventSinkEntity)}) to a simualtion
+ * and bound to a processing statement ({@link #bindStatement(String, EventSinkEntity)})
+ * <p>
+ *
+ * <b>Shared context</b><br>
+ * The {@link org.jboss.sam.simulation.SimulationContext} is shared between all simulation
+ * entities and acts as a blackboard where any entity can read and write simulation data and configuration.
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class EventSimulation extends SimulationProcess
+{
+ // simulation properties
+ private List<EventSinkEntity> eventSinks = new ArrayList<EventSinkEntity>();
+ private List<EventSourceEntity> eventSources = new ArrayList<EventSourceEntity>();
+ private SimulationContext context;
+ private InVMDispatcher dispatcher;
+
+ // SAM properties
+ private EventProcessor eventProcessor;
+ private StreamInput streamInput;
+
+ // Constants
+ public final static String TOTAL_SIM_TIME = "sam.total-sim-time";
+ public final static String NUM_UNMATCHED_EVENTS = "sam.unmatched-events";
+
+ private EventSimulation(Properties config, URL bootstrap)
+ {
+ // thread setup
+ setName("EventSimulation {" + getName()+"}");
+ setPriority(1);
+
+ this.context = new SimulationContext(config);
+
+ // setup SAM processor
+ this.eventProcessor = EventProcessorFactory.newInstance().createRuntime( "EventSimulation", bootstrap );
+
+ // create input for the default in-vm channel
+ this.streamInput = new InVMStreamInput(InVMDispatcher.DEFAULT_CHANNEL_NAME);
+
+ // create a dispatcher used within event source
+ this.dispatcher = new InVMDispatcher(InVMDispatcher.DEFAULT_CHANNEL_NAME);
+
+ // register in/out with hub
+ this.eventProcessor.addInput(streamInput);
+
+ ((EsperRuntimeImpl) eventProcessor).getUnderlying().getEPRuntime()
+ .setUnmatchedListener(
+ new UnmatchedListener()
+ {
+
+ public void update(EventBean event)
+ {
+ System.out.println("! Unmatched event: " + event);
+ context.getMeasurement(NUM_UNMATCHED_EVENTS).increment();
+ }
+ }
+ );
+
+ }
+
+ public EventSinkEntity addSink(EventSinkEntity sink)
+ {
+ sink.setContext(this.context);
+
+ this.eventSinks.add(sink);
+ this.eventProcessor.addOutput(sink);
+ return sink;
+ }
+
+ public EventSourceEntity addSource(EventSourceEntity source)
+ {
+ source.setContext(this.context);
+ source.setDispatcher(this.dispatcher);
+
+ this.eventSources.add(source);
+ return source;
+ }
+
+ /**
+ * Create a aggregation/filtering statement
+ * @param stmt
+ */
+ public void createStatement(String stmt)
+ {
+ this.eventProcessor.registerStatement(
+ UUID.randomUUID().toString(), stmt
+ );
+ }
+
+ /**
+ * Bind a statment that forwards to an EventSink
+ * @param stmt
+ * @param sink
+ */
+ public void bindStatement(String stmt, EventSinkEntity sink)
+ {
+ this.eventProcessor.registerBoundStatement(
+ sink.getStreamName(), UUID.randomUUID().toString(), stmt
+ );
+ }
+
+ /**
+ * Execute a single run of this simulation.
+ * Suspends the main thread until the simulation is done.
+ */
+ public void singleRun()
+ {
+ // [HB] System.out.println("-----------");
+ // [HB] System.out.println("Start called: " + this);
+ this.resumeProcess();
+ // [HB] System.out.println("Trying mainSuspend on: " + this);
+ SimulationProcess.mainSuspend();
+ // [HB] System.out.println("Done with mainSuspend on: " + this);
+ }
+
+ public void done()
+ {
+ try
+ {
+ this.terminate();
+ Scheduler.reset();
+ }
+ catch(SimulationException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ private void begin()
+ {
+ for(EventSinkEntity sink : eventSinks)
+ {
+ sink.begin();
+ }
+
+ for(EventSourceEntity source : eventSources)
+ {
+ source.begin();
+ }
+ }
+
+ private void end()
+ {
+ for(EventSinkEntity sink : eventSinks)
+ {
+ sink.end();
+ }
+
+ for(EventSourceEntity source : eventSources)
+ {
+ source.end();
+ }
+ }
+
+ public SimulationContext getContext()
+ {
+ return context;
+ }
+
+ public Measurement measurement(String key)
+ {
+ return context.getMeasurement(key);
+ }
+
+ public void run()
+ {
+ try
+ {
+ // wait for main thread
+ waitForMainSuspended();
+
+ // [HB] System.out.println("Running " + this);
+
+ // begin
+ begin();
+ Scheduler.startSimulation();
+
+ Double totalSimTime = (Double) context.getConfig(EventSimulation.TOTAL_SIM_TIME);
+ hold(totalSimTime);
+
+ // [HB] System.out.println("End simulation:" + this );
+ // [HB] System.out.println("-----------");
+
+ // cleanup
+ Scheduler.stopSimulation();
+ end();
+
+ returnControl();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+
+ }
+
+ /**
+ * Returns control to the test class thread
+ */
+ private void returnControl()
+ {
+ try
+ {
+ // [HB] System.out.println("Return control to main thread");
+ SimulationProcess.mainResume();
+ this.suspendProcess();
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ System.exit(-1);
+ }
+ }
+
+ public static EventSimulation bootstrap(Properties config)
+ {
+ return new EventSimulation(config,
+ Thread.currentThread().getContextClassLoader().getResource("samples/esper-default-cfg.xml")
+ );
+ }
+
+ public static EventSimulation bootstrap(Properties config, URL bootstrap)
+ {
+ return new EventSimulation(config, bootstrap);
+ }
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSimulation.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkEntity.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkEntity.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkEntity.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+import org.jboss.sam.EventMessage;
+import org.jboss.sam.StreamOutput;
+
+/**
+ * Simulates an event sink.<br>
+ * Event sink's process messages that have been forwarded by an event processor.
+ * <p>
+ * <b>EventSinkHandler</b><br>
+ * The actual processing is done a {@link org.jboss.sam.simulation.EventSinkHandler}
+ * that needs to be associated with an event sink entity:
+ *
+ * <pre>
+ *
+ * EventSinkEntity sink = new EventSinkEntity("foo.bar-EventSink");
+ * sink.setEventHandler(
+ * new EventSinkHandler()
+ * {
+ *
+ * public void handle(EventMessage[] events, SimulationContext context)
+ * {
+ * // dome something here
+ * }
+ * }
+ * );
+ *
+ * </pre>
+ *
+ * <br>
+ * <b>EPL Statements and event forwarding</b><br>
+ * In order to invoke an event sink, you need to register a statement
+ * through {@link org.jboss.sam.simulation.EventSimulation#bindStatement(String, EventSinkEntity)}
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class EventSinkEntity implements StreamOutput, ContextAwareEntity
+{
+
+ private String streamName;
+ private SimulationContext context;
+ private EventSinkHandler handler = null;
+
+ public EventSinkEntity(String streamName)
+ {
+ this.streamName = streamName;
+ }
+
+ public void setEventHandler(EventSinkHandler handler)
+ {
+ this.handler = handler;
+ }
+
+ public StreamOutput start()
+ {
+ return this;
+ }
+
+ public StreamOutput stop()
+ {
+ return this;
+ }
+
+ public boolean isStarted()
+ {
+ return true;
+ }
+
+ public String getStreamName()
+ {
+ return this.streamName;
+ }
+
+ public void write(EventMessage[] events)
+ {
+ if(null==this.context)
+ throw new IllegalStateException("Simulation context not set");
+
+ if(handler!=null)
+ handler.handle(events, context);
+ }
+
+
+ public void begin()
+ {
+
+ }
+
+ public void end()
+ {
+
+ }
+
+ public void setContext(SimulationContext ctx)
+ {
+ this.context = ctx;
+ }
+
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkEntity.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkHandler.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkHandler.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkHandler.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+import org.jboss.sam.EventMessage;
+
+/**
+ * Represents the actual business logic of an {@link org.jboss.sam.simulation.EventSinkEntity}
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public interface EventSinkHandler
+{
+ void handle(EventMessage[] events, SimulationContext context);
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSinkHandler.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSourceEntity.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSourceEntity.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSourceEntity.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,160 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+import org.javasim.RestartException;
+import org.javasim.SimulationException;
+import org.javasim.SimulationEntity;
+import org.jboss.sam.internal.stream.InVMDispatcher;
+
+/**
+ * Simulates an event source.<br>
+ * <p>
+ * <b>EventMessageFactories</b><br>
+ * An event source leverages an {@link org.jboss.sam.simulation.EventMessageFactory}
+ * to create event messages and dispatch them to the event processor:
+ *
+ * <pre>
+ * EventSourceEntity source = new EventSourceEntity("foo.bar-EventSource", 0.1);
+ * source.setEventMessageFactory(
+ * new EventMessageFactory()
+ * {
+ * public Object createEventMessage(SimulationContext context)
+ * {
+ * // Create an event message here
+ * return ...;
+ * }
+ * }
+ * );
+ * </pre>
+ *
+ * <br>
+ * <b>Event ratio</b><br>
+ * Specifies the ratio with regard to the total simulation time (between 0.01-1)
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class EventSourceEntity extends SimulationEntity implements ContextAwareEntity
+{
+ private String name;
+ private Class eventMessageType;
+ private double eventRatio;
+ private InVMDispatcher dispatcher = null;
+ private SimulationContext context = null;
+ private EventMessageFactory factory = null;
+
+ /**
+ *
+ * @param name the event source name
+ * @param eventRatio event creation ratio wrt the total simulation time. Value between 0-1
+ */
+ public EventSourceEntity(String name, double eventRatio)
+ {
+ // thread setup
+ setPriority(5);
+ setName("EventSourceEntity {" + getName()+"}");
+
+
+ this.name = name;
+
+ if(eventRatio<=0 || eventRatio>1)
+ throw new IllegalArgumentException("Invalid event ratio: " + eventRatio);
+
+ this.eventMessageType = eventMessageType;
+ this.eventRatio = eventRatio;
+ }
+
+ public void setContext(SimulationContext controller)
+ {
+ this.context = controller;
+ }
+
+ void setDispatcher(InVMDispatcher dispatcher)
+ {
+ this.dispatcher = dispatcher;
+ }
+
+ public void setEventMessageFactory(EventMessageFactory factory)
+ {
+ this.factory = factory;
+ }
+
+ public void run()
+ {
+ while (!terminated())
+ {
+ try
+ {
+ double totalSim = (Double) context.getConfig(EventSimulation.TOTAL_SIM_TIME);
+ double holdTime = (totalSim*eventRatio);
+ hold(holdTime);
+ }
+ catch (SimulationException e)
+ {
+ e.printStackTrace();
+ }
+ catch (RestartException e)
+ {
+ e.printStackTrace();
+ }
+
+
+ if(null==context)
+ throw new IllegalStateException("Simulation context not set");
+
+ if(null==factory)
+ throw new IllegalStateException("EventMessageFactory not set");
+
+ if(null==dispatcher)
+ throw new IllegalStateException("EventDispatcher not set");
+
+ // Create and dispatch event
+ Object event = factory.createEventMessage(this.context);
+ this.dispatcher.dispatch(event);
+
+ }
+
+ // [HB] System.out.println("EventSource " + name + " was terminated");
+ }
+
+ public void begin()
+ {
+ try
+ {
+ this.activate();
+ } catch (SimulationException e)
+ {
+ e.printStackTrace();
+
+ } catch (RestartException e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ public void end()
+ {
+ this.terminate();
+ }
+
+
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/EventSourceEntity.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/Measurement.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/Measurement.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/Measurement.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+/**
+ * A simulation measurement helper
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class Measurement
+{
+ private Double current = 0.0;
+
+ public Double getCurrent()
+ {
+ return current;
+ }
+
+ public Double increase(Double d)
+ {
+ this.current += d;
+ return this.current;
+ }
+
+ public Double decrease(Double d)
+ {
+ this.current -= d;
+ return this.current;
+ }
+
+ public Double setCurrent(Double d)
+ {
+ this.current = d;
+ return this.current;
+ }
+
+ public Double increment()
+ {
+ this.current += 1;
+ return this.current;
+ }
+
+ public Double decrement()
+ {
+ this.current -= 1;
+ return this.current;
+ }
+
+
+ public String toString()
+ {
+ return String.valueOf(getCurrent());
+ }
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/Measurement.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/SimulationContext.java
===================================================================
--- sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/SimulationContext.java (rev 0)
+++ sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/SimulationContext.java 2008-07-16 10:48:51 UTC (rev 175)
@@ -0,0 +1,96 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.sam.simulation;
+
+import org.javasim.Scheduler;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.io.PrintStream;
+
+/**
+ * Shared context between simulation entities and their implementation.
+ * The context is used collect simulation data and pass it along.<br>
+ * It propagates:
+ * <ul>
+ * <li>the event simulation configuration
+ * <li>measurements from event sinks, event sources and associated delegates
+ * <li>arbitrary attachments
+ * <ul>
+ *
+ * @see org.jboss.sam.simulation.Measurement
+ *
+ * @author Heiko.Braun <heiko.braun at jboss.com>
+ */
+public class SimulationContext
+{
+ private Properties config = null;
+ private Map<String, Measurement> measurements = new HashMap<String, Measurement>();
+ private Map<Class, Object> attachments = new HashMap<Class, Object>();
+
+ public SimulationContext(Properties config)
+ {
+ this.config = config;
+ }
+
+ public Object getConfig(String key)
+ {
+ if(null == config.get(key))
+ throw new IllegalArgumentException("No such config: " + key);
+ return config.get(key);
+ }
+
+ public Measurement getMeasurement(String key)
+ {
+ Measurement measurement = measurements.get(key);
+ if(null == measurement)
+ {
+ measurement = new Measurement();
+ measurements.put(key, measurement);
+ }
+ return measurement;
+ }
+
+ public <T> T getAttachment(Class<T> clazz)
+ {
+ return (T)attachments.get(clazz);
+ }
+
+ public <T> T addAttachment(Class<T> clazz, Object obj)
+ {
+ return (T)attachments.put(clazz, obj);
+ }
+
+ public double getCurrentSimulationTime()
+ {
+ return Scheduler.currentTime();
+ }
+
+ public void dump(PrintStream out)
+ {
+ for(String key : measurements.keySet())
+ {
+ out.println(key + ": " + measurements.get(key));
+ }
+ }
+}
Property changes on: sam/trunk/modules/sim/src/main/java/org/jboss/sam/simulation/SimulationContext.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
More information about the overlord-commits
mailing list