swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Swarm Suppport for Exceptions


From: durga bhavani
Subject: Re: [Swarm-Support] Swarm Suppport for Exceptions
Date: Tue, 8 Nov 2005 15:27:57 -0800 (PST)

Hello
 
I did check my code I am not sure where I am missing the intialization part
 
Attached is the file which has observer,model and start
Can you please take a look at these files at your convience
 
Thanks a lot
 
Durga

"Marcus G. Daniels" <address@hidden> wrote:
durga bhavani wrote:
> Someone please tell me why it is throwing exceptions
It could be because your modelSchedule or modelActions are not
initialized.

But the best thing to do is to take your code and trim it down to the
point it is easy for others to compile and run (crash). Delete
everything not relevant to creating the crash and then post that minimal
self-contained code. Chances are, in the process of doing this, you'll
find the answer yourself!


_______________________________________________
Support mailing list
address@hidden
http://www.swarm.org/mailman/listinfo/support


Yahoo! FareChase - Search multiple travel sites in one click.
GroupModelSwarm.java

import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;
import swarm.defobj.SymbolImpl;
import swarm.defobj.FArguments;
import swarm.defobj.FArgumentsImpl;
import swarm.defobj.FCall;
import swarm.defobj.FCallImpl;
import swarm.activity.Activity;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.activity.ActionForEach;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
import swarm.objectbase.VarProbe;
import swarm.objectbase.MessageProbe;
import swarm.objectbase.EmptyProbeMapImpl;
import java.util.*;
import swarm.space.Grid2d;
import swarm.space.Grid2dImpl;
public class GroupModelSwarm extends SwarmImpl
{
        public int worldXSize=80;
        public int worldYSize=80;
        public int GroupSize=8;
        public int NoofLeaders=1;
        public int NoofFollowers=7;
        public ActionGroup modelActions;
        public ScheduleImpl modelSchedule;
        public List leaderList;
        public List followerList;
        public Grid2dImpl world;
        ActionForEach[] actionForEach = new ActionForEach[4];
        class MyMathsClass{
        public int addEmUp(){
            int i,j;
            int value=0;
            for(i=0;i<10;i++){
                for(j=0;j<10;j++){
                    value=i+j;
                }
            }
            System.out.println ("addEmUp result:" + value);
            return value;
        }
    }

        public List getLeaderList () 
        {
                return leaderList;
        }
        
        public List getFollowerList () 
        {
                return followerList;
        }
        public Grid2d getWorld () 
        {
                return world;
        }
        public GroupModelSwarm (Zone aZone) 
        {
                super (aZone);
                class GroupModelProbeMap extends EmptyProbeMapImpl 
                {
                        private VarProbe probeVariable (String name) 
                        {
                                return
                                        
Globals.env.probeLibrary.getProbeForVariable$inClass
                                        (name,GroupModelSwarm.this.getClass ());
                        }
                        private MessageProbe probeMessage (String name) 
                        {
                                return
                                        
Globals.env.probeLibrary.getProbeForMessage$inClass
                                        (name, GroupModelSwarm.this.getClass 
());
                        }
                        private void addVar (String name) 
                        {
                                addProbe (probeVariable (name));
                        }
                        private void addMessage (String name) 
                        {
                                addProbe (probeMessage (name));
                        }
                        public GroupModelProbeMap(Zone _aZone, Class aClass) 
                        {
                                super (_aZone, aClass);
                                addVar ("worldXSize");
                                addVar ("worldYSize");
                                addVar("GroupSize");
                                addVar ("NoofLeaders");
                                addVar("NoofFollowers");
                        
                        }
                }
                Globals.env.probeLibrary.setProbeMap$For
                        (new GroupModelProbeMap (aZone, getClass ()), getClass 
());
        }
        public Object buildObjects ()
        {        
                super.buildObjects();
                int  dim =4;
                int dim1=8;
                 
                world = new Grid2dImpl (getZone (), worldXSize, worldYSize);
                leaderList = new LinkedList ();
                followerList = new LinkedList ();
                world.setOverwriteWarnings(false);
                int x=4;
                int y=4;
                for (int i = 0; i < dim; i++)
                {
                        x=8;
                        for (int j = 0; j < dim1; j++)
                        {
                                Leader l;
                                l =  new Leader(world);
                                leaderList.add(l);
                                l.setX$Y (x,y);
                                x = x+4;
                        }
                        y = y+8;
                }
                x=8; y=8;
                float sum = 0;
                 
                leaderList.add(new MyMathsClass());
                world.setOverwriteWarnings(false);
                return this;
        }
        public Object buildActions () 
        {
                super.buildActions();
                Selector sel;
                Selector sel1=null;
                try{
                        sel1=new Selector(MyMathsClass.class,"addEmUp",false);
                }       
                catch(Exception e){
                        System.out.println("Signature not found Exception");
                         
                }
                ActionGroupImpl modelActions;
                 
                modelActions = new ActionGroupImpl (getZone());
                 
                modelActions.createActionForEach$message(leaderList,sel1);
                modelActions.createActionTo$message(leaderList.get(0),sel1);
                modelSchedule = new ScheduleImpl (getZone (), 1);
                modelSchedule.at$createAction (0, modelActions);
                return this;
        }
        public Activity activateIn (Swarm swarmContext) 
        {
                super.activateIn (swarmContext);
                modelSchedule.activateIn (this);
                modelActions.activateIn(this);
                return getActivity ();
        }
}


GroupObserver.java
import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;
import swarm.activity.Activity;
import swarm.activity.ActionGroup;
import swarm.activity.ActionGroupImpl;
import swarm.activity.Schedule;
import swarm.activity.ScheduleImpl;
import swarm.objectbase.Swarm;
import swarm.objectbase.VarProbe;
import swarm.objectbase.MessageProbe;
import swarm.objectbase.EmptyProbeMapImpl;
import swarm.gui.Colormap;
import swarm.gui.ColormapImpl;
import swarm.gui.ZoomRaster;
import swarm.gui.ZoomRasterImpl;
import swarm.analysis.*;
import swarm.simtoolsgui.GUISwarm;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.space.Value2dDisplay;
import swarm.space.Value2dDisplayImpl;
import swarm.space.Object2dDisplay;
import swarm.space.Object2dDisplayImpl;
import java.util.List;
import swarm.space.Grid2d;
import swarm.space.Grid2dImpl;
import java.util.*;
import java.io.*;
public class GroupObserver extends GUISwarmImpl
{       
        public boolean simulationFinished = false;
        public int displayFrequency;
        public static Grid2d world;
        public Schedule displaySchedule;
        public ActionGroup displayActions;
        public GroupModelSwarm groupModelSwarm;
        public Leader leader1;
        public Colormap colormap;
        public ZoomRaster worldRaster;
         public List LeaderList;
        public Object2dDisplay followerDisplay;
        public Object2dDisplay leaderDisplay;
        public int simulatedTime=0;
        public int zoomFactor=8;
        public Selector sel;
        public GroupObserver(Zone aZone) 
        {
                super(aZone);
                displayFrequency = 1;
                class GroupObserverProbeMap extends EmptyProbeMapImpl 
                {
                        private VarProbe probeVariable (String name) 
                        {
                                return
                                        
Globals.env.probeLibrary.getProbeForVariable$inClass(name, 
GroupObserver.this.getClass ());
                        }
                        private MessageProbe probeMessage (String name) 
                        {
                                return
                                        
Globals.env.probeLibrary.getProbeForMessage$inClass
                                        (name, GroupObserver.this.getClass ());
                        }
                        private void addVar (String name) 
                        {
                                addProbe (probeVariable (name));
                        }
                        private void addMessage (String name) 
                        {
                                addProbe (probeMessage (name));
                        }
                        public GroupObserverProbeMap (Zone _aZone, Class 
aClass) 
                        {
                                super (_aZone, aClass);
                                addVar ("displayFrequency");
                                addMessage("zoomFactor");
                                addMessage("simulatedTime");
                        }
                }
                Globals.env.probeLibrary.setProbeMap$For(new 
GroupObserverProbeMap (aZone, getClass ()), getClass ());
        }
        public Object _worldRasterDeath_ (Object caller) 
        {
                worldRaster.drop ();
                worldRaster = null;
                return this;
        }
        public Object _ezGraphDeath_(Object caller){
                ezGraph.drop();
                ezGraph = null;
                return this;
        }
        public Object _clGraphDeath_(Object caller){
                clGraph.drop();
                clGraph = null;
                return this;
        }
        public Object buildObjects () 
        {
                super.buildObjects ();
                groupModelSwarm = new GroupModelSwarm (getZone ());
                Globals.env.createArchivedProbeDisplay 
(groupModelSwarm,"groupModelSwarm");
                Globals.env.createArchivedProbeDisplay (this, "groupObserver");
                getControlPanel ().setStateStopped ();
                if(simulatedTime!=0)
                {
                System.out.println("The user is not allowed to change the 
simulated time\n");
                }       
                groupModelSwarm.buildObjects ();
                colormap = new ColormapImpl (getZone ());
                colormap.setColor$ToName ((byte) 63, "blue");
                colormap.setColor$ToName ((byte) 64, "green");
                colormap.setColor$ToName ((byte) 65, "red");
                colormap.setColor$ToName ((byte) 66, "yellow");
                List leader = groupModelSwarm.getLeaderList();
                worldRaster = new ZoomRasterImpl (getZone (), "worldRaster");
                sel = SwarmUtils.getSelector(this, "_worldRasterDeath_");
                try 
                {
                        worldRaster.enableDestroyNotification$notificationMethod
                                (this,
                                new Selector (getClass (), 
"_worldRasterDeath_", false));
                } 
                catch (Exception e) 
                {
                        System.err.println ("Exception _worldRasterDeath_: "+ 
e.getMessage ());
                }
                worldRaster.erase();
                worldRaster.setColormap (colormap);
                worldRaster.setZoomFactor (4);
                worldRaster.setWidth$Height((groupModelSwarm.getWorld 
()).getSizeX (),(groupModelSwarm.getWorld ()).getSizeY ());
                worldRaster.setWindowTitle ("Simulation of WorkGroups Using 
Swarm");
                worldRaster.pack();
                try 
                {
                        sel = SwarmUtils.getSelector("Leader","drawSelfOn");
                        leaderDisplay = new Object2dDisplayImpl
                                (getZone (), worldRaster, 
groupModelSwarm.getWorld (),sel);
                        
leaderDisplay.setObjectCollection(groupModelSwarm.getLeaderList());
                        
sel=SwarmUtils.getSelector(leaderDisplay,"makeProbeAtX$Y");
                        
worldRaster.setButton$Client$Message(3,leaderDisplay,sel);
                }
                catch (Exception e) 
                {
                        System.err.println ("Exception drawSelfOn: " + 
e.getMessage ());
                }
                try 
                {
                        followerDisplay = new Object2dDisplayImpl
                                (getZone (), worldRaster, 
groupModelSwarm.getWorld (),
                                new Selector (Class.forName ("Follower"), 
"drawSelfOn", false));
                }
                catch (Exception e) 
                {
                        System.err.println ("Exception drawSelfOn: " + 
e.getMessage ());
                }
                leaderDisplay.setObjectCollection(groupModelSwarm.getLeaderList 
());
                
followerDisplay.setObjectCollection(groupModelSwarm.getFollowerList ());
                try 
                {
                        worldRaster.setButton$Client$Message
                                (3, leaderDisplay, new Selector 
(leaderDisplay.getClass (),
                                "makeProbeAtX$Y", true));
                } 
                catch (Exception e) 
                {
                        System.err.println ("Exception makeProbeAtX$Y: "+ 
e.getMessage ());
                }
                try 
                {
                        worldRaster.setButton$Client$Message
                                (3, followerDisplay, new Selector 
(followerDisplay.getClass (),
                                "makeProbeAtX$Y", true));
                }
                catch (Exception e) 
                {
                        System.err.println ("Exception makeProbeAtX$Y: "+ 
e.getMessage ());
                }
                        return this;
        }       
                
        public Object _update_ ()  
        {
                worldRaster.erase();
                if (worldRaster != null) 
                {
         
                        leaderDisplay.display ();
                        followerDisplay.display ();
                        worldRaster.drawSelf();
                }
                if (ezGraph != null)
                {
                        ezGraph.step ();
         
                }
                if(clGraph!=null)
                {
                        clGraph.step();
                }
                return this;
        }
        public Object buildActions () 
        {
         
                super.buildActions();
                groupModelSwarm.buildActions();
                displayActions = new ActionGroupImpl (getZone());


                 sel = SwarmUtils.getSelector(leaderDisplay, "display");
        displayActions.createActionTo$message(leaderDisplay, sel);

 

        sel = SwarmUtils.getSelector(worldRaster, "drawSelf");
        displayActions.createActionTo$message(worldRaster, sel);

        sel = SwarmUtils.getSelector(this, "updateSimulatedTime");
        displayActions.createActionTo$message(this, sel);

        sel = SwarmUtils.getSelector(Globals.env.probeDisplayManager,
                                     "update");
        displayActions.createActionTo$message(Globals.env.probeDisplayManager,
                                              sel);

        sel = SwarmUtils.getSelector(getActionCache(), "doTkEvents");
        displayActions.createActionTo$message(getActionCache(), sel);

        sel = SwarmUtils.getSelector(this, "checkForDone");
        displayActions.createActionTo$message(this, sel);

                displaySchedule = new ScheduleImpl (getZone (), 
displayFrequency);
                displaySchedule.at$createAction(0,displayActions);
                return this;
        }
        public Activity activateIn (Swarm swarmContext) 
        {
                super.activateIn (swarmContext);
                groupModelSwarm.activateIn (this);
                displaySchedule.activateIn (this);
                return getActivity();
        }
        public void checkForDone()
    {
        if (simulationFinished)
            {
             System.out.println("I said to QUIT!");
            groupModelSwarm.getActivity().terminate();
            groupModelSwarm.drop();
            getControlPanel().setStateQuit();
            }
        else if (groupModelSwarm.getActivity().getStatus() ==
                 Globals.env.Completed)
            {
             simulationFinished = true;
            System.out.println("The simulation ended after "
                               + Globals.env.getCurrentTime()
                               + " periods.");
            System.out.println("Press QUIT when ready.");
            getControlPanel().setStateStopped();
            }
    }
        
        public void drop()
        {
                if (worldRaster != null)
                        worldRaster.disableDestroyNotification ();
          
                super.drop ();
        }
        public void updateSimulatedTime()
    {
        simulatedTime = Globals.env.getCurrentTime();
    }
 }

StartSim

startsim.java
import swarm.Globals;
import swarm.defobj.Zone;
import swarm.SwarmEnvironmentImpl;
import swarm.activity.ActionGroupImpl;
import swarm.activity.ActionGroup;
import swarm.activity.Activity;
import swarm.objectbase.SwarmImpl;
import swarm.objectbase.Swarm;
import swarm.Selector;
public class StartSim {
    public static void main (String[] args) {
      Globals.env.initSwarm ("Simulation of Work Group Using Swarm", "0.3.1",
"address@hidden", args);
      GroupObserver topLevelSwarm = new GroupObserver (Globals.env.globalZone);
 //       Swarm swarm = new GroupModelSwarm(Globals.env.globalZone);
        Globals.env.setWindowGeometryRecordName (topLevelSwarm, 
"topLevelSwarm");
      topLevelSwarm.buildObjects();
      topLevelSwarm.buildActions();
      topLevelSwarm.activateIn(null);
      topLevelSwarm.go ();
      topLevelSwarm.drop ();
    }
}



























reply via email to

[Prev in Thread] Current Thread [Next in Thread]