View Javadoc

1   /*
2    * @(#)SimEvent.java Feb 1, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.formalisms.devs;
11  
12  import java.lang.reflect.Constructor;
13  import java.lang.reflect.Method;
14  
15  import nl.tudelft.simulation.dsol.SimRuntimeException;
16  import nl.tudelft.simulation.language.reflection.ClassUtil;
17  
18  /***
19   * The SimEvent forms the essential scheduling mechanism for D-SOL. Objects do
20   * not invoke methods directly on eachother; they bundle the object on which the
21   * method is planned to be invoked together with the arguments and the name of
22   * the method in a simEvent. The SimEvent is then stored in the eventList and
23   * executed.
24   * <p>
25   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
26   * University of Technology </a>, the Netherlands. <br>
27   * See for project information <a href="http://www.simulation.tudelft.nl">
28   * www.simulation.tudelft.nl </a> <br>
29   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
30   * License (GPL) </a>, no warranty <br>
31   * 
32   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
33   *         Jacobs </a>
34   * @version 1.11 2004-03-26
35   * @since 1.0
36   */
37  public class SimEvent extends AbstractSimEvent
38  {
39  
40  	/***
41  	 * source reflects the source that created the simevent
42  	 * 
43  	 * @uml.property name="source"
44  	 */
45  	protected Object source = null;
46  
47  	/***
48  	 * target reflects the target on which a state change is scheduled
49  	 * 
50  	 * @uml.property name="target"
51  	 */
52  	protected Object target = null;
53  
54  	/***
55  	 * method is the method which embodies the state change
56  	 * 
57  	 * @uml.property name="method"
58  	 */
59  	protected String method = null;
60  
61  	/***
62  	 * args are the arguments which are used to invoke the method with
63  	 * 
64  	 * @uml.property name="args"
65  	 */
66  	protected Object[] args = null;
67  
68  
69  	/***
70  	 * The constuctor of the event stores the time the event must be executed
71  	 * and the object and method to invoke
72  	 * 
73  	 * @param executionTime reflects the time the event has to be executed.
74  	 * @param source reflects the source that created the method
75  	 * @param target reflects the object on which the method must be invoked.
76  	 * @param method reflects the method to invoke
77  	 * @param args reflects the argumenst the method to invoke with
78  	 */
79  	public SimEvent(final double executionTime, final Object source,
80  			final Object target, final String method, final Object[] args)
81  	{
82  		this(executionTime, SimEventInterface.NORMAL_PRIORITY, source, target,
83  				method, args);
84  	}
85  
86  	/***
87  	 * The constuctor of the event stores the time the event must be executed
88  	 * and the object and method to invoke
89  	 * 
90  	 * @param executionTime reflects the time the event has to be executed.
91  	 * @param priority reflects the priority of the event
92  	 * @param source reflects the source that created the method
93  	 * @param target reflects the object on which the method must be invoked.
94  	 * @param method reflects the method to invoke
95  	 * @param args reflects the argumenst the method to invoke with
96  	 */
97  	public SimEvent(final double executionTime, final short priority,
98  			final Object source, final Object target, final String method,
99  			final Object[] args)
100 	{
101 		super(executionTime, priority);
102 		if (source == null || target == null || method == null)
103 		{
104 			throw new IllegalArgumentException(
105 					"either source, target or method==null");
106 		}
107 		this.source = source;
108 		this.target = target;
109 		this.method = method;
110 		this.args = args;
111 	}
112 
113 	/***
114 	 * @see SimEventInterface#execute()
115 	 */
116 	public synchronized void execute() throws SimRuntimeException
117 	{
118 		try
119 		{
120 			if (this.method.equals("<init>"))
121 			{
122 				Constructor constructor = ClassUtil.resolveConstructor(
123 						(Class) this.target, this.args);
124 				if (!ClassUtil.isVisible(constructor, this.source.getClass()))
125 				{
126 					throw new SimRuntimeException(this.method
127 							+ " is not accessible for " + this.source);
128 				}
129 				constructor.setAccessible(true);
130 				constructor.newInstance(this.args);
131 			} else
132 			{
133 				Method method = ClassUtil.resolveMethod(this.target,
134 						this.method, this.args);
135 				if (!ClassUtil.isVisible(method, this.source.getClass()))
136 				{
137 					throw new SimRuntimeException(this.method
138 							+ " is not accessible for " + this.source);
139 				}
140 				method.setAccessible(true);
141 				method.invoke(this.target, this.args);
142 			}
143 		} catch (Exception exception)
144 		{
145 			throw new SimRuntimeException(exception);
146 		}
147 	}
148 
149 	/***
150 	 * @return Returns the args.
151 	 * 
152 	 * @uml.property name="args"
153 	 */
154 	public Object[] getArgs()
155 	{
156 		return this.args;
157 	}
158 
159 	/***
160 	 * @return Returns the method.
161 	 * 
162 	 * @uml.property name="method"
163 	 */
164 	public String getMethod()
165 	{
166 		return this.method;
167 	}
168 
169 	/***
170 	 * @return Returns the source.
171 	 * 
172 	 * @uml.property name="source"
173 	 */
174 	public Object getSource()
175 	{
176 		return this.source;
177 	}
178 
179 	/***
180 	 * @return Returns the target.
181 	 * 
182 	 * @uml.property name="target"
183 	 */
184 	public Object getTarget()
185 	{
186 		return this.target;
187 	}
188 
189 	/***
190 	 * @see java.lang.Object#toString()
191 	 */
192 	public String toString()
193 	{
194 		return "SimEvent[time=" + this.absoluteExecutionTime + "; priority="
195 				+ this.priority + "; source=" + this.source + "; target="
196 				+ this.target + "; method=" + this.method + "; args="
197 				+ this.args + "]";
198 	}
199 }