36 #ifndef __GRAPHFAB_NETWORK_H_
37 #define __GRAPHFAB_NETWORK_H_
42 #include "graphfab/layout/point.h"
43 #include "graphfab/layout/curve.h"
50 #include "graphfab/sbml/autolayoutSBML.h"
52 #include "sbml/SBMLTypes.h"
53 #include "sbml/packages/layout/common/LayoutExtensionTypes.h"
67 std::string eltTypeToStr(
const NetworkEltType t);
69 void dumpEltType(std::ostream& os,
const NetworkEltType t, uint32 ind);
72 inline bool typeMatchEither(
const NetworkEltType a,
const NetworkEltType b,
const NetworkEltType z) {
85 class SubCurve :
public RxnBezier {
88 RxnCurveType getRole()
const {
89 return RXN_CURVE_SUBSTRATE;
92 Point getCentroidCP()
const {
return c2; }
94 bool isStartNodeSide()
const {
return true; }
96 virtual bool hasArrowhead()
const {
return true; }
98 ArrowheadStyle getArrowheadStyle()
const;
101 virtual Arrowhead* getArrowhead() {
102 Arrowhead* result =
new SubstrateArrowhead();
103 transformArrowhead(*result);
110 class PrdCurve :
public RxnBezier {
113 RxnCurveType getRole()
const {
114 return RXN_CURVE_PRODUCT;
117 Point getCentroidCP()
const {
return c1; }
119 bool isStartNodeSide()
const {
return false; }
121 virtual bool hasArrowhead()
const {
return true; }
123 ArrowheadStyle getArrowheadStyle()
const;
126 virtual Arrowhead* getArrowhead() {
127 Arrowhead* result =
new ProductArrowhead();
128 transformArrowhead(*result);
135 class ActCurve :
public RxnBezier {
138 RxnCurveType getRole()
const {
139 return RXN_CURVE_ACTIVATOR;
142 Point getCentroidCP()
const {
return c2; }
144 bool isStartNodeSide()
const {
return true; }
146 virtual bool hasArrowhead()
const {
return true; }
148 ArrowheadStyle getArrowheadStyle()
const;
151 virtual Arrowhead* getArrowhead() {
152 Arrowhead* result =
new ActivatorArrowhead();
153 transformArrowhead(*result);
160 class InhCurve :
public RxnBezier {
163 RxnCurveType getRole()
const {
164 return RXN_CURVE_INHIBITOR;
167 Point getCentroidCP()
const {
return c2; }
169 bool isStartNodeSide()
const {
return true; }
171 virtual bool hasArrowhead()
const {
return true; }
173 ArrowheadStyle getArrowheadStyle()
const;
176 virtual Arrowhead* getArrowhead() {
177 Arrowhead* result =
new InhibitorArrowhead();
178 transformArrowhead(*result);
185 class ModCurve :
public RxnBezier {
188 RxnCurveType getRole()
const {
189 return RXN_CURVE_MODIFIER;
192 Point getCentroidCP()
const {
return c2; }
194 bool isStartNodeSide()
const {
return true; }
196 virtual bool hasArrowhead()
const {
return true; }
198 ArrowheadStyle getArrowheadStyle()
const;
201 virtual Arrowhead* getArrowhead() {
202 Arrowhead* result =
new ModifierArrowhead();
203 transformArrowhead(*result);
208 extern ArrowheadStyle sub_arrow_style_;
209 inline ArrowheadStyle ArrowheadStyleLookup(
const SubCurve* ) {
210 return sub_arrow_style_;
213 extern ArrowheadStyle prod_arrow_style_;
214 inline ArrowheadStyle ArrowheadStyleLookup(
const PrdCurve* ) {
215 return prod_arrow_style_;
218 extern ArrowheadStyle act_arrow_style_;
219 inline ArrowheadStyle ArrowheadStyleLookup(
const ActCurve* ) {
220 return act_arrow_style_;
223 extern ArrowheadStyle inh_arrow_style_;
224 inline ArrowheadStyle ArrowheadStyleLookup(
const InhCurve* ) {
225 return inh_arrow_style_;
228 extern ArrowheadStyle mod_arrow_style_;
229 inline ArrowheadStyle ArrowheadStyleLookup(
const ModCurve* ) {
230 return mod_arrow_style_;
241 class NetworkElement {
250 : _pset(0), _v(0,0), _deg(0), _ldeg(0), _lock(0), networkEltBytePattern_(0x1199) {}
253 NetworkEltType getType()
const {
return _type; }
255 bool hasNetworkElementBase() {
256 if(networkEltBytePattern_ == 0x1199)
263 void set_degree(uint64 deg) { _deg = deg; }
264 virtual uint64 degree()
const {
return _deg; }
265 virtual uint64& degree() {
return _deg; }
268 virtual void resetActivity();
271 void addDelta(
const Point& d);
274 void capDelta(
const Real cap);
277 virtual void capDelta2(
const Real cap2);
280 virtual void doMotion(
const Real scale);
283 virtual void setCentroid(
const Point& p);
284 virtual void setGlobalCentroid(
const Point& p);
285 void setCentroid(
Real x,
Real y) { setCentroid(Point(x,y)); }
287 virtual bool isCentroidSet()
const {
return _pset; }
290 virtual Point getCentroid(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL)
const;
295 Point getMin(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL)
const {
return getExtents(coord).getMin(); }
296 Point getMax(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL)
const {
return getExtents(coord).getMax(); }
298 Real getMinX()
const {
return getExtents().getMin().x; }
299 Real getMaxX()
const {
return getExtents().getMax().x; }
300 Real getMinY()
const {
return getExtents().getMin().y; }
301 Real getMaxY()
const {
return getExtents().getMax().y; }
304 Real getWidth()
const { AT(getMaxX() >= getMinX());
return getMaxX() - getMinX(); }
305 Real getHeight()
const { AT(getMaxY() >= getMinY());
return getMaxY() - getMinY(); }
308 Real getGlobalWidth()
const { AT(getMaxX() >= getMinX());
return (getMaxX() - getMinX())*tf_.scaleFactor(); }
309 Real getGlobalHeight()
const { AT(getMaxY() >= getMinY());
return (getMaxY() - getMinY())*tf_.scaleFactor(); }
313 virtual Box getExtents(COORD_SYSTEM coord = COORD_SYSTEM_LOCAL)
const {
315 case COORD_SYSTEM_LOCAL:
316 return getLocalExtents();
317 case COORD_SYSTEM_GLOBAL:
318 return tf_*getLocalExtents();
320 AN(0,
"Unknown coord system");
321 return getLocalExtents();
327 virtual Box getLocalExtents()
const {
333 void setExtents(
const Box& b) { _ext = b; recalcCentroid(); }
335 Box getBoundingBox()
const {
return getExtents(); }
338 virtual void applyTransform(
const Affine2d& t) {
339 _ext = xformBox(_ext, t);
340 _p = xformPoint(_p, t);
344 void recalcCentroid() { _p = (_ext.getMin() + _ext.getMax())*0.5; }
347 virtual void recalcExtents() = 0;
350 void lock() { _lock = 1; }
353 void unlock() { _lock = 0; }
356 bool isLocked()
const {
return _lock; }
358 NetworkEltShape getShape()
const {
return _shape; }
361 virtual bool isContainer()
const = 0;
364 Real radius()
const {
return _r; }
367 Real distance(
const NetworkElement& e)
const;
370 bool overlap(
const NetworkElement& e)
const;
374 Point forceVec(
const NetworkElement& e)
const;
377 Point centroidDisplacementFrom(
const NetworkElement& e)
const;
380 void forceVec_(
const NetworkElement& e, Point& p)
const;
382 virtual void dump(std::ostream& os, uint32 ind) = 0;
385 virtual void dumpForces(std::ostream& os, uint32 ind)
const = 0;
387 virtual Affine2d getTransform()
const {
return tf_; }
389 virtual void setTransform(
const Affine2d& tf,
bool recurse =
true) { tf_ = tf; }
391 virtual Affine2d getInverseTransform()
const {
return itf_; }
393 virtual void setInverseTransform(
const Affine2d& itf,
bool recurse =
true) { itf_ = itf; }
411 NetworkEltShape _shape;
413 NetworkEltType _type;
421 long networkEltBytePattern_;
428 class Node :
public NetworkElement {
433 _shape = ELT_SHAPE_RECT;
435 _type = NET_ELT_TYPE_SPEC;
436 _ext = Box(0,0,40,20);
437 bytepattern = 0xc455;
444 void setName(
const std::string& name);
447 const std::string& getName()
const;
450 const std::string& getId()
const;
453 void setId(
const std::string&
id);
456 const std::string& getGlyph()
const;
459 void setGlyph(
const std::string&
id);
464 uint32& numUses() {
return _numUses; }
465 uint32 numUses()
const {
return _numUses; }
469 bool isAlias()
const {
return _isAlias; }
472 void setAlias(
bool b) { _isAlias = b; }
474 int alias(Network* net);
476 int getSubgraphIndex()
const {
478 GRAPHFAB_THROW(InvalidParameterException,
"No subgraph index set",
"Network::getSubgraphIndex");
482 void setSubgraphIndex(
int v) { isub_ = v; }
484 bool isSetSubgraphIndex() {
return isub_ < 0; }
486 void clearSubgraphIndex() { isub_ = -1; }
488 bool excludeFromSubgraphEnum()
const {
return exsub_; }
490 bool setExcludeFromSubgraphEnum() {
return exsub_ =
true; }
492 void clearExcludeFromSubgraphEnum() { exsub_ =
false; }
497 Point getUpperLeftCorner()
const;
499 Point getLowerRightCorner()
const;
501 void recalcExtents() {
502 Real width = _ext.width();
503 Real height = _ext.height();
504 Point del(0.5*width, 0.5*height);
506 _ext = Box(_p - del, _p + del);
507 _r = _ext.maxDim()*0.5;
511 void setWidth(
Real w);
514 void setHeight(
Real h);
517 void affectGlobalWidth(
Real w);
520 void affectGlobalHeight(
Real h);
522 void set_i(
size_t i) { i_ = i; }
523 size_t get_i()
const {
return i_; }
527 bool isContainer()
const {
return false; }
532 void dump(std::ostream& os, uint32 ind);
535 void dumpForces(std::ostream& os, uint32 ind)
const;
539 bool doByteCheck() {
if(bytepattern == 0xc455)
return true;
else return false; }
544 std::string _name, _id;
561 inline Node* CastToNode(
void* p) {
562 NetworkElement* e = (NetworkElement*)p;
563 AN(e->hasNetworkElementBase(),
"Runtime type check failed");
565 AN(dynamic_cast<Node*>(e),
"Runtime type check failed");
566 return dynamic_cast<Node*
>(e);
575 RXN_ROLE_SIDESUBSTRATE,
576 RXN_ROLE_SIDEPRODUCT,
582 class RxnCurveFactory {
584 static RxnBezier* CreateCurve(RxnRoleType role);
589 class Reaction :
public NetworkElement {
592 typedef std::pair<Node*, RxnRoleType> SpeciesElt;
594 typedef std::vector< SpeciesElt > NodeVec;
598 typedef std::vector<RxnBezier*> CurveVec;
601 typedef NodeVec::iterator NodeIt;
602 typedef NodeVec::const_iterator ConstNodeIt;
607 typedef CurveVec::iterator CurveIt;
608 typedef CurveVec::const_iterator ConstCurveIt;
610 NodeIt NodesBegin() {
return _spec.begin(); }
611 NodeIt NodesEnd() {
return _spec.end(); }
613 ConstNodeIt NodesBegin()
const {
return _spec.begin(); }
614 ConstNodeIt NodesEnd()
const {
return _spec.end(); }
616 CurveIt CurvesBegin() {
return _curv.begin(); }
617 CurveIt CurvesEnd() {
return _curv.end(); }
619 ConstCurveIt CurvesBegin()
const {
return _curv.begin(); }
620 ConstCurveIt CurvesEnd()
const {
return _curv.end(); }
626 _shape = ELT_SHAPE_ROUND;
627 _type = NET_ELT_TYPE_RXN;
628 bytepattern = 0xff83;
631 void hierarchRelease();
636 const std::string& getId()
const {
return _id; }
639 void setId(
const std::string&
id) { _id = id; }
644 uint64 numSpecies()
const {
return _spec.size(); }
647 void addSpeciesRef(Node* n, RxnRoleType role);
650 void removeNode(Node* n);
653 Node* findSpeciesById(
const std::string&
id);
656 bool hasSpecies(
const Node* n)
const;
659 uint64 degree(
const Node* n);
661 RxnRoleType getSpeciesRole(
size_t i) {
return _spec.at(i).second; }
663 RxnRoleType getSpeciesRole(Node* n);
665 Node* getSpecies(
size_t i) {
return _spec.at(i).first; }
671 void substituteSpeciesById(
const std::string&
id, Node* spec);
675 void substituteSpeciesByIdwRole(
const std::string&
id, Node* spec, RxnRoleType role);
679 void substituteSpecies(Node* before, Node* after);
682 CurveVec& getCurves();
686 size_t getNumCurves() { curveGuard();
return _curv.size(); }
690 RxnBezier* getCurve(
size_t i) { curveGuard();
return _curv.at(i); }
693 NodeVec& getSpec() {
return _spec; }
694 const NodeVec& getSpec()
const {
return _spec; }
697 void forceRecalcCentroid();
702 void rebuildCurves();
708 void recalcCurveCPs();
716 bool isContainer()
const {
return false; }
719 void recalcExtents() {
721 _ext = Box(_p - Point(_r,_r), _p + Point(_r,_r));
724 virtual Box getLocalExtents()
const {
725 return Box(getCentroid() - Point(5, 5), getCentroid() + Point(5, 5));
728 virtual void applyTransform(
const Affine2d& t) {
729 NetworkElement::applyTransform(t);
730 for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
731 (*i)->applyTransform(t);
735 virtual void setTransform(
const Affine2d& tf,
bool recurse =
true) {
737 for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
738 (*i)->setTransform(tf);
742 virtual void setInverseTransform(
const Affine2d& itf,
bool recurse =
true) {
744 for(CurveIt i = CurvesBegin(); i != CurvesEnd(); ++i) {
745 (*i)->setInverseTransform(itf);
750 void dump(std::ostream& os, uint32 ind);
753 void dumpForces(std::ostream& os, uint32 ind)
const;
755 bool doByteCheck() {
if(bytepattern == 0xff83)
return true;
else return false; }
757 void clearDirtyFlag() { _cdirty =
false; }
760 RxnBezier* addCurve(RxnRoleType role) {
761 _curv.push_back(RxnCurveFactory::CreateCurve(role));
762 _curv.back()->setTransform(itf_);
763 _curv.back()->setInverseTransform(itf_);
777 void recompCentroid();
780 if(_cdirty && _spec.size()) {
809 inline Reaction* CastToReaction(
void* p) {
810 NetworkElement* e = (NetworkElement*)p;
811 AN(e->hasNetworkElementBase(),
"Runtime type check failed");
812 AN(dynamic_cast<Reaction*>(e),
"Runtime type check failed");
813 return dynamic_cast<Reaction*
>(e);
819 COMP_EDGE_TYPE_BOTTOM,
821 } CompartmentEdgeType;
825 class Compartment :
public NetworkElement {
831 typedef std::vector<Graphfab::NetworkElement*> EltVec;
833 typedef EltVec::iterator EltIt;
834 typedef EltVec::const_iterator ConstEltIt;
836 EltIt EltsBegin() {
return _elt.begin(); }
837 EltIt EltsEnd() {
return _elt.end(); }
839 ConstEltIt EltsBegin()
const {
return _elt.begin(); }
840 ConstEltIt EltsEnd()
const {
return _elt.end(); }
842 Graphfab::NetworkElement* getElt(
const uint64 i) {
return _elt.at(i); }
844 const Graphfab::NetworkElement* getElt(
const uint64 i)
const {
return _elt.at(i); }
846 uint64 getNElts()
const {
return _elt.size(); }
849 : _ra(50.*50.), _E(10.), _res(0.25), NetworkElement() {
850 _shape = ELT_SHAPE_RECT;
851 _type = NET_ELT_TYPE_COMP;
855 const std::string& getId()
const {
return _id; }
858 void setId(
const std::string&
id) { _id = id; }
861 const std::string& getGlyph()
const {
return _gly; }
864 void setGlyph(
const std::string& glyph) { _gly = glyph; }
866 void setCentroid(
const Point& p) {
867 AN(0,
"setCentroid should not be called on a compt");
871 void recalcExtents() {
872 _r = _ext.maxDim()*0.5;
873 _p = (_ext.getMin() + _ext.getMax())*0.5;
879 void addElt(NetworkElement* e);
881 bool containsElt(
const NetworkElement* e)
const;
884 void removeElt(NetworkElement* e);
887 void setRestExtents(
const Box& ext);
890 void resizeEnclose();
896 Real restArea()
const {
return _ra; }
898 void setMin(
const Point& p) { _ext.setMin(p); }
899 void setMax(
const Point& p) { _ext.setMax(p); }
904 virtual void resetActivity();
912 void applyBoundaryForce(
const Real fx1,
const Real fy1,
const Real fx2,
const Real fy2);
919 void doInternalForce(NetworkElement* e,
const Real f,
const Real t);
922 void doInternalForceAll(
const Real f,
const Real t);
925 void doMotion(
const Real scale);
927 void capDelta2(
const Real cap2);
929 bool isContainer()
const {
return true; }
932 bool contains(
const NetworkElement* e)
const;
935 bool empty()
const {
return _elt.size() ?
false :
true; }
938 void dump(std::ostream& os, uint32 ind);
941 void dumpForces(std::ostream& os, uint32 ind)
const;
957 Real _fx1, _fy1, _fx2, _fy2;
964 class Network :
public Compartment {
969 typedef std::vector<Node*> NodeVec;
971 typedef std::vector<Graphfab::Reaction*> RxnVec;
973 typedef std::vector<Graphfab::Compartment*> CompVec;
976 typedef NodeVec::iterator NodeIt;
977 typedef NodeVec::const_iterator ConstNodeIt;
979 typedef RxnVec::iterator RxnIt;
980 typedef RxnVec::const_iterator ConstRxnIt;
982 typedef CompVec::iterator CompIt;
983 typedef CompVec::const_iterator ConstCompIt;
988 bytepattern = 0x3355;
989 layoutspecified_ =
false;
995 void hierarchRelease();
1000 void addNode(Node* n);
1004 void removeNode(Node* n);
1007 Node* findNodeById(
const std::string&
id);
1008 const Node* findNodeById(
const std::string&
id)
const;
1011 std::string getUniqueId()
const;
1014 std::size_t getUniqueIndex()
const;
1017 Node* findNodeByGlyph(
const std::string& gly);
1019 Node* getNodeAt(
const size_t i) {
return _nodes.at(i); }
1021 bool containsNode(
const Node* n)
const;
1023 typedef std::vector<Reaction*> AttachedRxnList;
1025 AttachedRxnList getConnectedReactions(
const Node* n);
1027 typedef std::vector<RxnBezier*> AttachedCurveList;
1029 AttachedCurveList getAttachedCurves(
const Node* n);
1031 int getNumSubgraphs();
1034 void enumerateSubgraphs();
1037 void propagateSubgraphIndex(Node* x,
int isub);
1039 void clearSubgraphInfo();
1041 void clearExcludeFromSubgraphEnum();
1044 Reaction* findReactionById(
const std::string&
id);
1046 Reaction* getRxnAt(
const size_t i) {
return _rxn.at(i); }
1049 void resetUsageInfo();
1052 const std::string& getId()
const {
return _id; }
1054 bool isLayoutSpecified()
const {
return layoutspecified_; }
1056 void setLayoutSpecified(
bool value) {
1057 layoutspecified_ = value;
1063 void addReaction(Reaction* rxn);
1066 void removeReaction(Reaction* r);
1071 void addCompartment(Compartment* c) { _comp.push_back(c); addElt(c); }
1076 Compartment* findCompById(
const std::string&
id);
1081 Compartment* findCompByGlyph(
const std::string& gly);
1083 Compartment* getCompAt(
const size_t i) {
return _comp.at(i); }
1085 Compartment* findContainingCompartment(
const NetworkElement* e);
1089 uint64 getTotalNumComps()
const {
return _comp.size(); }
1092 uint64 getTotalNumPts()
const {
return _nodes.size() + _rxn.size(); }
1095 uint64 getTotalNumRxns()
const {
return _rxn.size(); }
1098 uint64 getTotalNumNodes()
const {
return _nodes.size(); }
1100 Box getBoundingBox()
const;
1102 void fitToWindow(
const Box& w);
1104 void applyTransform(
const Affine2d& t);
1106 void setTransform(
const Affine2d& t,
bool recurse =
true);
1108 void setInverseTransform(
const Affine2d& it,
bool recurse =
true);
1111 void elideEmptyComps();
1114 void randomizePositions(
const Box& bounds);
1117 void rebuildCurves();
1121 void recalcCurveCPs();
1124 void recenterJunctions();
1127 void resetActivity();
1139 void capDeltas(
const Real cap);
1144 void updatePositions(
const Real scale);
1147 void updateExtents();
1150 void resizeCompsEnclose();
1153 void autosizeComps();
1157 Point pmean()
const;
1161 Point center()
const;
1165 Box getExtents()
const;
1170 void recenter(
const Point& p);
1178 Point pvariance()
const;
1183 void dump(std::ostream& os, uint32 ind);
1186 void dumpEltForces(std::ostream& os, uint32 ind)
const;
1188 Node* getNodeAtIndex(
int index) {
return _nodes[index]; }
1192 NodeIt NodesBegin() {
return _nodes.begin(); }
1193 NodeIt NodesEnd() {
return _nodes.end(); }
1195 ConstNodeIt NodesBegin()
const {
return _nodes.begin(); }
1196 ConstNodeIt NodesEnd()
const {
return _nodes.end(); }
1198 RxnIt RxnsBegin() {
return _rxn.begin(); }
1199 RxnIt RxnsEnd() {
return _rxn.end(); }
1201 ConstRxnIt RxnsBegin()
const {
return _rxn.begin(); }
1202 ConstRxnIt RxnsEnd()
const {
return _rxn.end(); }
1204 CompIt CompsBegin() {
return _comp.begin(); }
1205 CompIt CompsEnd() {
return _comp.end(); }
1207 ConstCompIt CompsBegin()
const {
return _comp.begin(); }
1208 ConstCompIt CompsEnd()
const {
return _comp.end(); }
1210 bool doByteCheck()
const {
if(bytepattern == 0x3355)
return true;
else return false; }
1213 void removeReactionsForNode(Node* n);
1223 bool layoutspecified_;
1230 inline Network* CastToNetwork(
void* p) {
1231 NetworkElement* e = (NetworkElement*)p;
1232 AN(e->hasNetworkElementBase(),
"Runtime type check failed");
1233 return dynamic_cast<Network*
>(e);
1244 Network* networkFromLayout(
const Layout& lay,
const Model& mod);
1251 Network* networkFromModel(
const Model& mod);
1255 int layout_getNumFloatingSpecies(
const Layout& lay,
const Model& mod);
SAGITTARIUS_REAL Real
Make Real visible to C. Consider letting Real lie in top namespace.
Definition: SagittariusCommon.h:136
Definition: SagittariusCommon.cpp:38