swarm-support
[Top][All Lists]
Advanced

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

Stupid Swarm Tricks Vol 1 No 1: EZGraph hack


From: Benedikt Stefansson
Subject: Stupid Swarm Tricks Vol 1 No 1: EZGraph hack
Date: Wed, 07 Jul 1999 22:11:00 +0200

+-----------------------------------------------------------+
|                                                           |
| Problem: EZGraph can't accept selectors/methods which     |
| take an argument.                                         |
|                                                           |
| Hack: Subclass of EZGraph which accepts methods with one  |
| argument.                                                 |
|                                                           |
| Comment: This is a sloppy solution, more elegant would be |
| to generalize the whole approach to arbitrary numbers of  |
| arguments. However, it might be useful...                 |
|                                                           |
| Contact: Benedikt Stefansson  <address@hidden>         |
+-----------------------------------------------------------+

CODE

NewEZGraph.h:

#import <analysis/EZGraph.h>

@interface NewEZGraph: EZGraph 

- createGraphSequence: (const char *)aName 
          forSequence: aSeq
         withFeedFrom: anObj 
          andSelector: (SEL)aSel : arg1;

- createSequence: (const char *)aName
    withFeedFrom: anObj 
     andSelector: (SEL) aSel : arg1;


@end

NewEZGraph.m:

#import "NewEZGraph.h"
#define NUMCOLORS 12
const char graphColors[NUMCOLORS][16] =
//    { "Red",     "Green",  "Yellow",    "Pink",      "SeaGreen",
//      "Magenta", "Purple", "DarkGreen", "Goldenrod", "Black" };
        { "Red",   "Blue",   "Orange", "DarkGreen", "Magenta",  
"Purple",
        "Green", "Yellow", "Cyan",   "SeaGreen",  "Goldenrod", "Black"
};  

@implementation NewEZGraph

// internal method called by createSequence:withFeedFrom:andSelector
- createGraphSequence: (const char *)aName
          forSequence: aSeq
         withFeedFrom: anObj 
          andSelector: (SEL)aSel : arg1
{
  id aGrapher;
  char argStr[20];
  int ARGN=0;

  sprintf(argStr,"%d",(int)arg1);

  if (graphics)
    {
      id anElement;
      
      anElement = [graph createElement];
      [anElement setLabel: aName];
      [anElement setColor: graphColors[colorIdx++ % NUMCOLORS]];

      aGrapher = [ActiveGraph createBegin: [self getZone]];
      [aGrapher setElement: anElement];
      [aGrapher setDataFeed: anObj]; 
      [aGrapher setProbedSelector: aSel];
      aGrapher = [aGrapher createEnd];
      [aGrapher setArg: ARGN ToString: strdup(argStr)]; 
      
      [aSeq setActiveGrapher: aGrapher];    
    }
 
  if (fileOutput)
    {
      id aFileObj;
      const char *fName;

      // fName = sequence_graph_filename(fileName, aName);
      aFileObj = [OutFile create: [self getZone] withName: fName];
      
      aGrapher = [ActiveOutFile createBegin: [self getZone]];
      [aGrapher setFileObject: aFileObj];
      [aGrapher setDataFeed: anObj]; 
      [aGrapher setProbedSelector: aSel];
      aGrapher = [aGrapher createEnd];
      [aGrapher setArg: ARGN ToString: strdup(argStr)]; 

      [aSeq setActiveOutFile: aGrapher];    
      
    }
  
  [sequenceList addLast: aSeq];
  
  return self;
}

- createSequence: (const char *)aName
    withFeedFrom: anObj 
     andSelector: (SEL)aSel : arg1
{
  id aSeq;
  
  aSeq = [EZSequence create: [self getZone]];

  [self createGraphSequence: aName forSequence: aSeq
        withFeedFrom: anObj andSelector: aSel : arg1];
  
  return aSeq;
}


@end


EXAMPLE OF USE

In this example an NewEZGraph instance is created and the
-(double)getValueFor: method might fetch next datapoint
in timeseries for each of NUM agents.

(In example code we actually just return a random double on 
each call to getValueFor:).

#import "NewEZGraph.h"
#define NUM 5

-buildGraphs {
  int i;
  char name[20];

  theTestGraph = [NewEZGraph createBegin: self];
  [theTestGraph setTitle: "test vs. time"];
  [theTestGraph setAxisLabelsX: "time" Y: "test"];
  [theTestGraph setFileOutput: 0];
  theTestGraph = [theTestGraph createEnd] ;

  for(i=0;i<NUM;i++) {
    sprintf(name, "test_%d",i);
    [theTestGraph createSequence: name
                   withFeedFrom: self
                    andSelector: M(getValueFor:) : (id)i]; 
  }
  
  return self;
  
}
  
-(double)getValueFor: (int) i {
  return [uniformDblRand getDoubleWithMin: 0.0 withMax: 1.0];
}


Benedikt Stefansson July 7th 1999

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

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