Sophie

Sophie

distrib > Fedora > 18 > x86_64 > media > updates > by-pkgid > 45a94e45036b4f2592a7e2c3922f90c7 > files > 537

adevs-doc-2.6-4.fc18.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<!--Converted with LaTeX2HTML 2008 (1.71)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Models with Many Input/Output Types</TITLE>
<META NAME="description" CONTENT="Models with Many Input/Output Types">
<META NAME="keywords" CONTENT="manual">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">

<META NAME="Generator" CONTENT="LaTeX2HTML v2008">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

<LINK REL="STYLESHEET" HREF="manual.css">

<LINK REL="next" HREF="node12.html">
<LINK REL="previous" HREF="node10.html">
<LINK REL="up" HREF="manual.html">
<LINK REL="next" HREF="node12.html">
</HEAD>

<BODY >
<!--Navigation Panel-->
<A NAME="tex2html250"
  HREF="node12.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html246"
  HREF="manual.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html240"
  HREF="node10.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html248"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>  
<BR>
<B> Next:</B> <A NAME="tex2html251"
  HREF="node12.html">Alternate types for time</A>
<B> Up:</B> <A NAME="tex2html247"
  HREF="manual.html">A Discrete EVent system</A>
<B> Previous:</B> <A NAME="tex2html241"
  HREF="node10.html">Simulation on multi-core computers</A>
 &nbsp; <B>  <A NAME="tex2html249"
  HREF="node1.html">Contents</A></B> 
<BR>
<BR>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION001100000000000000000"></A>
<A NAME="section:models_with_many_io_types"></A>
<BR>
Models with Many Input/Output Types
</H1>
It would be surprising if every component in a large model had the same input and output requirements. Some models can be satisfactorily constructed with a single type of input/output object and, if this is the case, it will simplify the design of your simulator. If not, you'll need to address this problem when you design your simulation program.

<P>
One solution to this problem is to establish a base class for all input and output types, and then to derive specific types from the common base. The simulator and all of its components exchange pointers to the base class and downcast objects as needed. The C++ dynamic_cast operator is particularly useful for this purpose. Although it is not without its problems, I have used this solution in many designs and it works well.

<P>
It is not always possible for every component in a model to share a common base class for its input and output type. This can happen if different sub-model have very different input and output needs or when models from earlier projects are reused. For example, to use a <B>CellSpace</B> model as a component of a <B>Digraph</B> model requires some means of converting <B>CellEvent</B> objects into the <B>PortValue</B> objects. A solution to this problem is to use the <B>Simulator</B> and <B>EventListener</B> classes to wrap a model with one input and output type inside of an atomic model with a different input and output type.

<P>
The <B>ModelWrapper</B> class is an <B>Atomic</B> model that encapsulates another model. The encapsulated model can be a <B>Network</B> or <B>Atomic</B> model. The <B>ModelWrapper</B> uses input/output objects of type ExternalType while the encapsulated class uses input/output objects of type InternalType. Two abstract methods are provided for converting objects with one type into objects with the other type. These methods are
<PRE>
void translateInput(const Bag&lt;ExternalType&gt;&amp; external_input, Bag&lt;Event&lt;InternalType&gt; &gt;&amp; internal_input)
void translateOutput(const Bag&lt;Event&lt;InternalType&gt; &gt;&amp; internal_output, Bag&lt;ExternalType&gt;&amp; external_output)
</PRE>

<P>
The cleanup of converted objects is managed with the <B><I>gc_output</I></B> method, which is inherited from the <B>ModelWrapper</B>'s <B>Atomic</B> base class, plus a new <B><I>gc_input</I></B> method to cleanup objects created by the <B><I>translateInput</I></B> method: its signature is
<PRE>
void gc_input(Bag&lt;Event&lt;InternalType&gt; &gt;&amp; g)
</PRE>

<P>
The model to encapsulate is passed to the constructor of the <B>ModelWrapper</B>. The <B>ModelWrapper</B> creates a <B>Simulator</B> for the model that is used to control its evolution. The <B>ModelWrapper</B> is a simulator inside of a model inside of a simulator! The <B>ModelWrapper</B> keeps track of the wrapped model's last event time, and it uses this information and the <B>Simulator</B>'s <B><I>nextEventTime</I></B> method to compute its own time advance. Internal, external, and confluent events cause the <B>WrappedModel</B> to invoke its <B>Simulator</B>'s <B><I>computeNextState</I></B> method and thereby advance the state of the wrapped model. Internal events are simplest. The <B><I>computeNextState</I></B> method is invoked with the wrapped model's next event time and an empty input bag. 

<P>
The <B><I>delta_conf</I></B> and <B><I>delta_ext</I></B> methods must convert the incoming input events, which have the type ExternalType, into input events for the wrapped model, which have the type InternalEvent. This is accomplished with the <B><I>translateInput</I></B> method. The first argument to this method is the input bag passed to the <B>ModelWrapper</B>'s <B><I>delta_ext</I></B> or <B><I>delta_conf</I></B> method. The second argument is an empty bag that the method implementation must fill. When the <B><I>translateInput</I></B> method returns this bag will be passed to the <B><I>computeNextState</I></B> method of the <B>ModelWrapper</B>'s simulator. Notice that the internal_input argument is a <B>Bag</B> filled with <B>Event</B> objects. If the wrapped model is a <B>Network</B> then the translated events can be targeted at any of the network's components. The <B>ModelWrapper</B> invokes the <B><I>gc_input</I></B> method when it is done with the events in the internal_input bag. This gives you the opportunity to delete objects that you created when <B><I>translateInput</I></B> was called.

<P>
A similar process occurs when the <B>ModelWrapper</B>'s <B><I>output_func</I></B> method is invoked, but in this case it is necessary to convert output objects from the wrapped model, which have type InternalType, to output objects from the <B>ModelWrapper</B>, which have type ExternalType. This is accomplished by invoking the <B><I>translateOutput</I></B> method. The method's first argument is the bag of output events produced collectively by all of the wrapped model's components. Notice that the contents of the internal_output bag are <B>Event</B> objects. The model field points to the component of the wrapped model (or the wrapped model itself) that produced the event and the value field contains an output object produced by that model. These <B>Event</B> objects must be converted to objects of type ExternalType and stored in the external_output bag. The external_output bag is, in fact, the bag passed to the wrapper's <B><I>output_func</I></B>, and so its contents become the output objects produced by the wrapper. The <B><I>gc_output</I></B> method is used in the usual way to clean up any objects created by this process.

<P>
The <B>Wrapper</B> class shown below illustrates how to use the <B>WrapperModel</B> class. The <B>Wrapper</B> is derived from the <B>WrapperModel</B> and implements its four virtual methods: <B><I>translateInput</I></B>, <B><I>translateOutput</I></B>, <B><I>gc_input</I></B>, and <B><I>gc_output</I></B>. This class wraps an <B>Atomic</B> model that uses int* objects for input and output. The <B>Wrapper</B> uses C strings for its input and output. The translation methods convert integers to strings and vice versa. The <B>Wrapper</B> can be used just like any <B>Atomic</B> model: it can be a component in a network model or simulated by itself. The behavior of the <B>Wrapper</B> is identical to the model it wraps. The only change is in the interface.
<PRE>
// This class converts between char* and int* event types.
class Wrapper: public adevs::ModelWrapper&lt;char*,int*&gt; {
    public:
        Wrapper(adevs::Atomic&lt;int*&gt;* model):
            // Pass the model to the base class constructor
            adevs::ModelWrapper&lt;char*,int*&gt;(model){}
        void translateInput(const adevs::Bag&lt;char*&gt;&amp; external,
                adevs::Bag&lt;adevs::Event&lt;int*&gt; &gt;&amp; internal) {
            // Iterate through the incoming events
            adevs::Bag&lt;char*&gt;::const_iterator iter;
            for (iter = external.begin(); iter != external.end(); iter++) {
                // Convert each one into an int* and send it to the
                // wrapped model
                adevs::Event&lt;int*&gt; event;
                // Set the event value
                event.value = new int(atoi(*iter));
                // Set the event target
                event.model = getWrappedModel();
                // Put it into the bag of translated objects
                internal.insert(event);
            }
        }
        void translateOutput(const adevs::Bag&lt;adevs::Event&lt;int*&gt; &gt;&amp; internal,
                adevs::Bag&lt;char*&gt;&amp; external) {
            // Iterate through the incoming events
            adevs::Bag&lt;adevs::Event&lt;int*&gt; &gt;::const_iterator iter;
            for (iter = internal.begin(); iter != internal.end(); iter++) {
                // Convert the incoming event value to a string
                char* str = new char[100];
                sprintf(str,"%d",*((*iter).value));
                // Put it into the bag of translated objects
                external.insert(str);
            }
        }
        void gc_output(adevs::Bag&lt;char*&gt;&amp; g) {
            // Delete strings allocated in the translateOutput method
            adevs::Bag&lt;char*&gt;::iterator iter;
            for (iter = g.begin(); iter != g.end(); iter++)
                delete [] *iter;
        }
        void gc_input(adevs::Bag&lt;adevs::Event&lt;int*&gt; &gt;&amp; g) {
            // Delete integers allocated in the translateInput method
            adevs::Bag&lt;adevs::Event&lt;int*&gt; &gt;::iterator iter;;
            for (iter = g.begin(); iter != g.end(); iter++)
                delete (*iter).value;
        }
};
</PRE>
	<HR>
<!--Navigation Panel-->
<A NAME="tex2html250"
  HREF="node12.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html246"
  HREF="manual.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html240"
  HREF="node10.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html248"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>  
<BR>
<B> Next:</B> <A NAME="tex2html251"
  HREF="node12.html">Alternate types for time</A>
<B> Up:</B> <A NAME="tex2html247"
  HREF="manual.html">A Discrete EVent system</A>
<B> Previous:</B> <A NAME="tex2html241"
  HREF="node10.html">Simulation on multi-core computers</A>
 &nbsp; <B>  <A NAME="tex2html249"
  HREF="node1.html">Contents</A></B> 
<!--End of Navigation Panel-->
<ADDRESS>

2013-06-18
</ADDRESS>
</BODY>
</HTML>