Reusable Associations
by Martin Soukup and Jiri Soukup


Listing One

class ChipLib {     
  Collection<Master> masters;
  Master *chip;    
  Collection<Geometry> geometries;
};
class Master {    
  String name;
  int xWidth,yWidth; 
  Collection<Block> blocks; 
  Collection<Net> nets;     
};
class Block {
  String name;
  int x,y;             
  int orientation;     
  Master *master;      
  Collection<Terminal> termsByBlock; 
};
class Net {
  String name;
  Collection<Connector> connectors;   
  Collection<Terminal> termsByNet;  
  Collection<Pin> pins; 
  Master *master;           
};
class Terminal {
  Block *block;   

  Net *net;       
  Net *masterNet; 
};
 ... 


Listing Two

// LIBRARY OF ASSOCIATIONS:
    template<class Parent,class Child> class ParentAggregate {
        Collection<Child> children;
    };
    template<class Parent,class Child> class ChildAggregate {
        Parent *parent;
    };
    template<class Source,class Link,class Target> class SourceXtoX {
        Collection<Link> links;
    };
    template< class Source,class Link,class Target > class LinkXtoX {
        Source *source;
        Target *target;
    };
    template<class Source,class Link,class Target> class TargetXtoX {
        Collection<Link> links;
    };
    // APPLICATION CODE
    class Master {
  ParentAggregate<Master,Net> nets;
        ...
    };
    class Net {
        ChildAggregate<Master,Net> nets;
        TargetXtoX<Block,Terminal,Net> blockNets;
        ...
    };
    class Block {
        SourceXtoX<Block,Terminal,Net> blockNets;
        ...
    };
    class Terminal {
        LinkXtoX<Block,Terminal,Net> blockNets;
        ...
    };


Listing Three

class Net {
    TargetXtoX _blockNets;
    ...
};
class Block {
    SourceXtoX _blockNets;
    ...
};
class Terminal {
    LinkXtoX _blockNets;
    ...
};

// The association control class

class XtoX {
    static void add(Source *s,Terminal *t,Net *n){
        s->_blockNets.add(t);
        n->_blockNets.add(t);
        t->_blockNets.block=b;
        t->_blockNets.net=n;
    }
    static void remove(Target *t){
        t->_blockNets.block->_blockNets.remove(this);
        t->_blockNets.net->_blockNets.remove(this);
        t->_blockNets.block=NULL;
        t->_blockNets.net=NULL;
    }
}

// USING THE METHODS  STYLE 1
typedef class XtoX<Block,Terminal,Net> blockNets;

Block *b; Terminal *t; Net *n; 
blockNets::add(b,t,n);
blockNets::remove(t);

// USING THE METHODS  STYLE 2
XtoX<Block,Terminal,Net> blockNets;

Block *b; Terminal *t; Net *n; 
blockNets.add(b,t,n);
blockNets.remove(t);

Listing Four

// THE LIBRARY
template<class Parent,class Child> class ParentAggregate {
    Collection<Child> children;
};
template<class Parent,class Child> class ChildAggregate {
    Parent *parent;
};
template<class Parent,class Child,Name id> class Aggregate {
    Participants(ParentAggregate,ChildAggregate);
    void add(Parent *p, Child *c){
        p->id.children.add(c);
        c->id.parent=p;
    }
    void remove(Child *c){
        Parent* p=c->id.parent;
        p->id.children.remove(c);
        c->id.parent=NULL;
    }
};

// THE APPLICATION
class Master {
    ParentAggregate<Master,Net> _nets; // hidden
    ...
};
class Net {
    ChildAggregate<Master,Net> _nets; // hidden
    ...
};

Association Aggregate<Net,Pin> pins; // declaration of association, id=pins

typedef Aggregate<Net,Pin,_pins> pins; // how compiler interprets it in C++
class pins extends pinsAggregate<Net,Pin,_pins> { } // interpreting it in Java

Net *n; Pin *p;
pins::add(n,p);  // pins.add(n,p);  in Java
pins::remove(p); // pins.remove(p); in Java


Listing Five

// part to insert into the parent class 
public class $$_ParentAggregate {
    public $2 tail;
    public $$_ParentAggregate(){ tail=null; }
}
// part to insert into the child class
public class $$_ChildAggregate {
    public $2 next; // children form a ring
    public $1 parent;
    public $$_ChildAggregate(){ next=null; parent=null; }
}

// abstract class controls the association,
// $0 simplifies the code when accessing members
public abstract class $$ {
    public static void addHead($1 p, $2 c){
        $2 ct=p.$0.tail;
        if(ct==null){c.$0.next=p.$0.tail=c;}
        else (
            c.$0.next=ct.$0.next;
            ct.$0.next=c;
        }
        c.$0.parent=t;
    }
    ...
}

// application classes and definition of the association
import jlibGen.*; // files generated by the code generator
public class Net {
    public ZZ_Net ZZds; // includes all inserted members
    ... // nothing related to associations
}
public class Pin {
    public ZZ_Pin ZZds; // includes all inserted members
    ... // nothing related to associations
}
Association Aggregate<Net,Pin> pins;
Association XtoX<Block,Terminal,Net> blockNets;

// using the association
    Net n; Pin p; Block b; Terminal t;
    pins.addHead(n,p);
    pins.add(b,t,n);


// classes to insert members (automatically generated)
public class ZZ_Net {
    pins_ParentAggregate _pins;
    blockNets_TargetXtoX _blockNets;
}
public class ZZ_Pin {
    pins_ChildAggregate _pins;
}

4


