[xsl] Using XSLT for multi-agent simulation [was Creating a changeable globally scoped variable?]

Subject: [xsl] Using XSLT for multi-agent simulation [was Creating a changeable globally scoped variable?]
From: "Roger L. Costello" <costello@xxxxxxxxx>
Date: Sun, 21 Mar 2004 12:04:44 -0500
Michael Kay wrote:
> 
> #
> # I need to write a stylesheet which dynamically alters the
> # value of a globally scoped variable.
> #
> 
> I'm surprised, you've been on this list for some time, so you should 
> know that this isn't possible.

I was hoping that I was overlooking something in 2.0 and it would
provide this capability.

> Tell
> us what you actually need to do (i.e. what transformation you want to
> perform), and someone will be able to help.

Fair enough.  I am trying to use XSLT 2.0 to perform multi-agent
simulation.

Consider a multi-agent simulation: I would like to have code to express
the movement rules for a *single* agent, and then *separately* have
code which *applies* the movement rules to each agent (a classic case of
separation of concerns, and a classic paradigm for parallel processing).

I will show a simple example in pseudocode:

Agent Movement Rule
   if left-cell is empty then move there
   else if right-cell is empty then move there
   else stay put

Agent Processing
   For each agent do:
      Apply Agent Movement Rule

In this example we see that I have cleanly separated the statement of
the how each agent should move (Agent Movement Rule), from the
application of the movement rule to each agent (Agent Processing).

In order for the Agent Processing code to apply the Movement Rule
to each agent it will need to modify the value of a global
variable. Let's see why this is the case.

Here are some more details:

<xsl:template name="Agent-Movement-Rule">
    <xsl:choose>
        <xsl:when test="not(exists(left-cell()/agent))">
            <xsl:call-template name="move-left"/>
        </xsl:when>
        <xsl:when test="not(exists(right-cell()/agent))">
            <xsl:call-template name="move-right"/>
        </xsl:when>
        <xsl:otherwise/>
    </xsl:choose>
</xsl:template>

Notice that the Agent Movement Rule has been written:
- independent of the processing of all the other agents
- independent of location, i.e., "for *whatever cell* I am currently on,
execute these movement rules"

In other words, this is a bottom-up approach to template creation.

Now let me provide more details on the processing of each agent (I show
a mix of pseudocode and XSLT):

<xsl:template name="Agent-Processing">
   <xsl:for-each select="1 to $num-agents">
      Set $current-cell <-- modify a global variable!!!
      <xsl:call-template name="Agent-Movement-Rule"/>
   </xsl:for-each>
</xsl:template>

This template *applies* the Agent Movement Rule to each agent.  To do so
requires that the "current-cell" be set.  The current-cell is a global
variable.  This template needs to dynamically alter the global variable.

I realize that I *could* nest the Agent Movement Rule within the Agent
Processing code (and I have implemented this).  However, it results in
(very) brittle, complex code.  In addition, it is contrary to the
Principle of Separation of Concerns (one person should be able to create
the Agent Movement Rules, while another person creates the code to apply
the Movement Rules to each agent).

If you have suggestions on how to do this without using dynamically
changeable global variables I would be most interested.  /Roger

Current Thread