Author Topic: C# delegate signature declarations  (Read 5611 times)

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
C# delegate signature declarations
« on: April 18, 2004, 06:35:56 pm »
 ???
How do I model the delegate signature?  By way of example, consider the simulation of an AlarmSensor - I am using a form with a button to simulate the enviromental event say "WindowBroken", on this event a bunch of other thicgs must respond - like say the AudibleAlarm on the house, the phonecall to the monitoring company etc etc.

The problem is this - the signature for the delegate handler must be declared - as in the following code

Code: [Select]
using System.Windows.Forms;

public delegate void MySignalHandler (object Sender, MyEventArgs e);

/// <summary>
/// This is the Alarm Sensor hardware. It monitors the
/// environment for a specific detectable change - in this case
/// it is a click on the button - and raises an event AlarmRing.
/// </summary>
public class SignallerForm : System.Windows.Forms.Form
     {

           // Declare the event that is raised
           public event MySignalHandler WindowBroken;
     
     
etc etc            

Now when I reverse engineer this the second line, without any containing classifier is "ignored" on the code import, that is nothing is generated in the model that matches it.

Then (after I have created the pattern I'm trying to create) when I generate a new alarm type, the delegate signature is never declared.

Any ideas on what I'm doing wrong?

tia
Bruce

p.s. the base code actually works - it correctly simulates a set of alarm sensors and detectors.  But I am having trouble extending what I'm doing as each new sensor class needs code fiddling to reset up the delegate signatures.

(p.p.s.  Yes, alright - its part of the control system for my model train set - dont say a word)
"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

jps

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: C# delegate signature declarations
« Reply #1 on: April 20, 2004, 09:50:30 pm »
I think I've seen Martin Fowler model .NET delegates by showing an unnamed class with an operation that is the delegate.  He then marked the class with a stereotype of delegate.  Try to imagine the following as a class:  

---------------------------------
     <<delegate>>          
---------------------------------
---------------------------------
+ MySigHandler() : void
---------------------------------


It's late, and I'm not sure if I remember this correctly.  I believe it was in Patterns of Enterprise Application Architecture, which I do not have with me at the moment.

In any case, I don't think you'll find a "standard" way of modeling this as the authors of UML of course could not have taken this into account.

Good luck!

James

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
Rats!
« Reply #2 on: April 20, 2004, 10:21:33 pm »
  :) That's the correct answer, unfortunately its not the "right" answer  :(

In EA I can create an anonymous class that lets me set up the signature correctly. However, when I try and forward engineer it I get "Generation of anonymous classes currently not supported".

Rats! So close and yet so far. :(

Bruce
"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

jps

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: C# delegate signature declarations
« Reply #3 on: April 21, 2004, 06:32:41 am »
Hmmm.... I don't know that you'll get round-trip engineering to work for this unless SparxSystems decides to put in special support for it.  If you simply want to model it, then there are number of ways to do it... if you want to write code in UML I think you might have a tough time.

I’m interested in seeing whatever solution you come up with, so please post back what you end up doing!

James

jps

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: C# delegate signature declarations
« Reply #4 on: April 21, 2004, 06:36:45 am »
Actually, is it possible to create code templates to handle delegates based on certain stereotypes?  I really have not done that much with the code templates myself so I have no idea.

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
Re: C# delegate signature declarations
« Reply #5 on: April 21, 2004, 03:31:21 pm »
Lety me restate the problem I have and see if we can get further.

BACKGROUND
I have an very event driven system that simulates a scheduling problem.  I have built the generic component prototype in C# and reversed it into EA.  Essentially there are "n" different types of sensors hat detect real world events. (These are the typical "radioactive decay counter" type of simulator - every clock tick the sensor detects or doesn't detect the rw event.  The clocks run at different rates.)  Each sensor generates a different "type" of event, queue entered, queue left, queue collision etc etc.  There are many, many components in the system that respond to the triggers fired by the sensors.  Each component has a strict set of sensors to which it responds.  There are also some general events, such as a manual intervention in the scheduling, that are responded to by many/all components (e.g the clocks all respond to a manual Synch event).

The system breaks down into scheduling "segments" which provide the monitoring and reporting of the simulation.  A segment has a configured set of sensors and components.

THE PROBLEM
Having built the singularly simplistic segment and reverse engineered it into EA I have a nice model from which I can create a new sensortype, componenttype etc.  I am trying to use patterns to do this as the event/response model works really well.

There are over 100 types of sensors and components in the entire forseen system.

THE NUB!
So I can create a new system_component pretty quickly using EA patterns - except for the declaration of the delegate signature.

I can "fake it", using System.EventHandler as the generic delegate pattern but then, if the signature for a sensor event varies there is a lot of coding work to be done manually fixing each handlers parameters etc.

So what I'm looking for is some way to model the signature so that all handlers automatically generate the correct signature in the EA generated code.
James' anonymous class idea seems the best way at the moment but it
1) wont generate the delegate declaration as per my previous message. (I also have a personal dislike for unnamed classier elements in a model but that we can disregard)
2) still wants to encapsulate the declaration in a classifier, whereas I want them at the namespace level.


So, has anyone found a way of including the declaration of a delegate in a model that will forward engineer correctly?

Bruce


"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

benc

  • EA Administrator
  • EA User
  • *****
  • Posts: 200
  • Karma: +0/-0
    • View Profile
Re: C# delegate signature declarations
« Reply #6 on: April 22, 2004, 12:02:47 am »
Hi Bruce,

Here's a possibility (actually a hack to get the delegate signature generated, as opposed to an elegant modeling solution) :

1. Define the wrapper class for the delegate as per James' proposal- yes the class will need some kind of name to generate

2. Define a "delegate" stereotype- or whatever stereotype make sense

3. Stereotype this wrapper class as "delegate" (or whatever stereotype you created above)

4. Create the delegate method in the wrapper class

5. Open the template editor. Override the C# Class template for the "delegate" stereotype with something like the following:
%list="Operation" @separator="" @indent=""%

6. Point the target file for the delegate/wrapper class to an existing one and synch it into the file as a new "class"

Two problems I encounter with this approach so far:

1. It's a one way excercise- subsequent synchronizations/reversing of the file won't work because EA does not currently import delegates. If you're in a position where the entire model is setup and you only want to forward generate- then doing so at the package level, with all filenames setup in advance would be the way to go.

2. To generate delegates before classes would require something like the following in the Namesapce Body template:
.....
%list="Class" @separator="\n\n" @indent="\t" classStereotype=="delegate"%
%list="Class" @separator="\n\n" @indent="\t" classStereotype!="delegate"%
.....

However, a minor bug prevents the above conditions from being evaluated properly at the namespace level- hopefully to be resolved within the next build or so.

I hope this provides at least a partial solution for code gen purposes.  Regarding the crux of the issue- how to properly model the situation- we welcome further suggestions.

Regards,
Ben