Cpp.tex 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825
  1. \documentclass[twoside,fleqn]{report}
  2. \usepackage[report]{Graphlet}
  3. \begin{document}
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. %
  6. % Programming Graphlet in C++
  7. %
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. \input{Config.tex}
  10. \title{Graphlet C++ Programmer Manual
  11. \\*[3.0cm]
  12. {\emph{DRAFT VERSION}}
  13. }
  14. \author{Michael Himsolt\thanks{
  15. Graphlet is partially supported by the Deutsche
  16. Forschungsgemeinschaft, Grant Br 835/6-2,
  17. research cluster ``Efficient Algorithms for
  18. Discrete Problems and Their Applications''
  19. }
  20. }
  21. \maketitle
  22. \tableofcontents
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. %
  25. % Four Steps to Graphlet Applications
  26. %
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  28. \chapter{Introduction}
  29. \label{c:Introduction}
  30. This section describes how to implement the C++ part of
  31. \Graphlet{} \emph{applications}. A Graphlet application differs
  32. from an \emph{applet} as follows. An applet is implemented solely
  33. in \GraphScript{}, whereas applications are implemented in C++
  34. and \GraphScript{}. This section of the manual describes
  35. \Graphlet{}'s C++ interface and shows how to implement new
  36. \GraphScript{} commands in C++.
  37. \begin{skills}
  38. \begin{description}
  39. \item[C++]
  40. Basics, Classes, Inheritance, Virtual Methods, Templates
  41. \item[LEDA]
  42. Basic data structures, Graphs
  43. \item[Tcl interface to C]
  44. Basics, Tcl interpreter construction
  45. \end{description}
  46. \end{skills}
  47. %
  48. % Four Steps
  49. %
  50. \section{How to implement a \Graphlet{} Application}
  51. A \Graphlet{} application typically consists of (1) a set of
  52. algorithms, (2) \GraphScript{} commands for those algorithms and
  53. (3) a user interface written in \GraphScript{}. Implementing a
  54. GraphScript application consists of he
  55. \begin{enumerate}
  56. \item \textbf{Implement algorithms in C++.}
  57. \begin{itemize}
  58. \item
  59. Algorithms are either implemented in pure LEDA or in a mixture of LEDA
  60. and the \Graphlet{} C++ toolbox.
  61. \item As a rule of thumb, \Graphlet{}'s structures are needed
  62. if the graphical appearance of nodes, edges or and labels is
  63. relevant, or \Graphlet{}'s tool box is used. This applies to
  64. all \emph{layout algorithms}, and graph theory algorithms
  65. which support \emph{algorithm animation}.
  66. \end{itemize}
  67. This step is described in Chapters \ref{c:Toolbox},
  68. \ref{c:GT_Graph}, \ref{c:Attributes} and
  69. \ref{c:Algorithms}.
  70. \item \textbf{Provide \GraphScript{} commands for these
  71. algorithms.}
  72. \begin{itemize}
  73. \item Generally, there should a \GraphScript{} command for
  74. each algorithm. A family of algorithms may be converged into
  75. a single command.
  76. \item As a rule of thumb, the top level of a complex
  77. algorithm should always be implemented in \GraphScript{}.
  78. This allows easier customization than a C++ implementation,
  79. and helps to add a user interface.
  80. \end{itemize}
  81. This step is described in \ref{c:TclInterface}.
  82. \item \textbf{Build an extended \GraphScript{} interpreter
  83. which contains the new commands.}
  84. \begin{itemize}
  85. \item To do this, write a \texttt{main} routine, and link
  86. your code with \Graphlet{}, LEDA and Tcl/Tk.
  87. \end{itemize}
  88. This step is outlined in Chapters \ref{c:Modules} and
  89. \ref{c:Makefiles}.
  90. \item \textbf{Add a user interface with \GraphScript{}}
  91. \begin{itemize}
  92. \item Each algorithm should at least provide a window for the
  93. parameter settings. This window should be implemented with
  94. Tcl/Tk/\GraphScript{}.
  95. \end{itemize}
  96. This step is described in the GraphScript manual.
  97. \end{enumerate}
  98. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99. %
  100. % \Graphlet{}'s C++ Toolbox
  101. %
  102. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  103. \chapter{\Graphlet{}'s C++ Toolbox}
  104. \label{c:Toolbox}
  105. %
  106. % Macros for class declaration
  107. %
  108. \section{Global Definitions}
  109. \label{s:GlobalDefinitions}
  110. \CSourceCode{the definitions presented in this section}{base}{Graphlet}
  111. %
  112. %
  113. %
  114. \subsection{Class Declaration Examples}
  115. \begin{example}{e:SampleClassDeclaration}%
  116. {A sample class declaration using Graphlet standards}
  117. \begin{verbatim}
  118. class GT_Sample_Class : public GT_Sample_Base_Class {
  119. GT_CLASS (GT_Sample_Class, GT_Sample_Base_Class);
  120. GT_VARIABLE (int, sample);
  121. GT_COMPLEX_VARIABLE (GT_GRAPH&, a_graph);
  122. public:
  123. GT_Sample_Class ();
  124. virtual ~GT_Sample_Class ();
  125. void sample_method ();
  126. }
  127. \end{verbatim}
  128. \end{example}
  129. \begin{notes}
  130. \item All global declarations within Graphlet, especially class
  131. names, \textbf{must} use the prefix \texttt{GT\_}. Other
  132. projects are exempt from this treaty, but we strongly recommend
  133. to use a common prefix as well.
  134. \item Graphlet's coding standards require that class names
  135. (with \texttt{GT\_} stripped) start with a capital letter.
  136. \item Always provide a constructor and a desctructor. The
  137. latter one must be \texttt{virtual} if virtual methods are
  138. used within the class.
  139. \end{notes}
  140. %
  141. % Class Declarations
  142. %
  143. \subsection{Macros for class declaration}
  144. \begin{Cdefinition}
  145. \item[\GT{BASE\_CLASS (\Param{class})}] \strut\\
  146. The macro \GT{BASE\_CLASS (class)} implements standard
  147. declarations for a class which is not derived from another \Graphlet{}
  148. class.
  149. \item[\GT{CLASS (\Param{class}, \Param{base})}] \strut\\
  150. The macro \GT{CLASS (\Param{class}, \Param{base})} implements
  151. standard declarations for a class which is derived from another
  152. \Graphlet{} class \emph{base}. \GT{CLASS} implements the
  153. following:
  154. \begin{ttdescription}
  155. \item[baseclass] \strut\\
  156. This is a local \texttt{typedef} for the base class. For
  157. example,
  158. \begin{quote}
  159. \texttt{baseclass::}\emph{method}
  160. \end{quote}
  161. refers to \emph{method} in the class \emph{base}. For
  162. example, a virtual function \emph{f} which extend the
  163. functionality of the base class method will usually call
  164. \begin{quote}
  165. \texttt{baseclass::}\emph{f}\texttt{($\ldots$);}
  166. \end{quote}
  167. at the beginning or at the end of the method.
  168. \end{ttdescription}
  169. \end{Cdefinition}
  170. \begin{notes}
  171. \item Graphlet does not encourage to use multiple inheritance
  172. widely, so there is no support for this type of class
  173. declaration.
  174. \end{notes}
  175. %
  176. % Member Variables
  177. %
  178. \subsection{Member Variables}
  179. \label{s:MemberVariables}
  180. \Graphlet{} defines the macros \GT{VARIABLE} shortcuts to declare
  181. member variables and corresponding accessor methods:
  182. \begin{Cdefinition}
  183. \item[\GT{VARIABLE (\Param{type}, \Param{name})}] \strut\\
  184. This macro defines the following:
  185. \begin{ttdescription}
  186. \item[type the\_\Param{name};] \strut\\
  187. This is the private member variable.
  188. \item[type \Param{name}() const] \strut\\
  189. This is the public accessor method for \texttt{name}.
  190. \item[virtual void \Param{name} (\Param{type})] \strut\\
  191. This is the public method which is used to set \texttt{name}.
  192. \end{ttdescription}
  193. \item[\GT{COMPLEX\_VARIABLE (\Param{type}, \Param{name})}] \strut\\
  194. This macro defines the following:
  195. \begin{ttdescription}
  196. \item[type the\_\Param{name};] \strut\\
  197. This is the private member variable.
  198. \item[const \Param{type}\& \Param{name}() const] \strut\\
  199. This is the public accessor method for \texttt{name}.
  200. \item[virtual void \Param{name} (const \Param{type}\&)] \strut\\
  201. This is the public method which is used to set \texttt{name}.
  202. \end{ttdescription}
  203. \end{Cdefinition}
  204. \begin{notes}
  205. \item The prefix \texttt{the\_} is required by \Graphlet{}'s naming
  206. conventions.
  207. \item \GT{COMPLEX\_VARIABLE} is identical to \GT{VARIABLE} with
  208. the exception that is uses \texttt{const\&} semantics in the
  209. accessor methods. This is done to avoid extensive copying of
  210. non-simple classes. Generally, \GT{VARIABLE} should only be
  211. used for simple data types such as \texttt{int},
  212. \texttt{double} and pointers, and \GT{COMPLEX\_VARIABLE} for
  213. all others.
  214. \item By default, \Graphlet{} does not export
  215. non-\texttt{const} references to member variables. One reason
  216. for this is that \Graphlet{} often needs to react to a change,
  217. and a change can only be recognized if it is done through a
  218. dedicated method.
  219. \end{notes}
  220. %
  221. % GT_Status
  222. %
  223. \subsection{The class \GT{Status}}
  224. \begin{Cdeclaration}{Status}{enumeration \GT{Status}}
  225. \begin{verbatim}
  226. enum GT_Status {
  227. GT_OK = 0,
  228. GT_ERROR = 1
  229. };
  230. \end{verbatim}
  231. \end{Cdeclaration}
  232. \noindent The enumeration \GT{Status} is modeled after the Tcl return
  233. values \texttt{TCL\_OK} and \texttt{TCL\_ERROR}. \GT{Status}
  234. provides status return values which is compatible to those used
  235. in Tcl, and should be used whenever a return value must be
  236. converted into a Tcl return value at a later point.
  237. \begin{notes}
  238. \item Use \GT{Status} only when compatibility with Tcl is necessary.
  239. In all other cases, return a boolean value or a more descriptive
  240. enumeration.
  241. \item Graphlet reserves the right to add more values
  242. \GT{Status}. Application programs should \emph{not} no that.
  243. \item \GT{Status} is \textbf{not} compatible with the builtin
  244. C++ data type \texttt{bool}.
  245. \item The main reason for using \GT{Status} instead of
  246. \texttt{TCL\_OK} and \texttt{TCL\_ERROR} is to prevent Tcl from
  247. being included in the \Graphlet{} base libraries, and makes
  248. \Graphlet{} less dependent on Tcl.
  249. \end{notes}
  250. %
  251. % The class GT
  252. %
  253. \section{The Class \texttt{GT} and the Global Variable \texttt{graphlet}}
  254. Graphlet provides a global class \texttt{GT} and a global
  255. variable\footnote{Of course the Coding Standards (Chapter
  256. \ref{c:CS:C++}) forbid global variables. Well, nobody is
  257. perfect. Also remember: ``\textsc{Quod licet jovi, non licet
  258. bovi}''} \texttt{graphlet} which contain utility methods and
  259. hold some global data. Declaration \ref{Cdeclaration:GT} shows
  260. the declaration of class \texttt{GT}.
  261. \begin{Cdeclaration}{GT}{class \texttt{GT}}
  262. \begin{verbatim}
  263. class GT
  264. {
  265. GT_BASE_CLASS (GT);
  266. public:
  267. static char* strsave (const char* s, int max_length = 0);
  268. static bool streq (const char* s1, const char* s1);
  269. GT_Id id;
  270. GT_Error error;
  271. GT_Keymapper keymapper;
  272. GT_GML* gml;
  273. GT_Parser* parser;
  274. };
  275. GT* graphlet
  276. \end{verbatim}
  277. \end{Cdeclaration}
  278. \begin{Cdefinition}
  279. \item[char* strsave (const char* s, int max\_length=0)] \strut\\
  280. \texttt{strsave} is a wrapper for copying C strings.
  281. \texttt{strasave} copies at most \texttt{max\_length}
  282. characters of the string \texttt{s} into a new string which is
  283. allocated using \texttt{malloc}. If \texttt{max\_length} is
  284. omitted or is \texttt{0}, the whole string is copied. In both
  285. cases, a trailing \verb|'\0'| character is added.
  286. \item[bool streq (const char* s1, const char* s1)] \strut\\
  287. Wrapper for \texttt{!strcmp(s1,s2)}.
  288. \item[graphlet->id] \strut\\
  289. The object \texttt{graphlet->id} manages unique \texttt{id} numbers.
  290. Each time the method \texttt{graphlet->id.next\_id()} is called, a
  291. new number is emitted. Graphlet uses \texttt{id} to assign
  292. unique identifiers to graphs, nodes, edges and user interface
  293. objects.
  294. \item[graphlet->error] \strut\\
  295. This object holds a dictionary of predefined error messages.
  296. See also section \ref{s:Error}.
  297. \item[graphlet->keymapper] \strut\\
  298. The keymapper is a hash table of often used strings. See also
  299. Sections \ref{s:Keymapper} and \ref{s:PredefinedKeys}.
  300. \item[graphlet->parser] \strut\\
  301. This is the GML parser. \NYD.
  302. \item[graphlet->gml] \strut\\
  303. This object contains additional information for procedures
  304. which output graphs in the GML file format. \NYD.
  305. \end{Cdefinition}
  306. %
  307. % The Keymapper
  308. %
  309. \section{The Keymapper}
  310. \label{s:Keymapper}
  311. \CSourceCodeLocation{}{base}{Keymapper}
  312. \CIncludeStatement{}{base}{Graphlet}
  313. \noindent \Graphlet{} uses a hash table to store often used strings.
  314. This hash table is called the \emph{keymapper}, since it was
  315. first implemented to store the keys in the the GML file
  316. format. A \emph{key} is an element of this hash table.
  317. Mathematically, a keymapper is defined as follows:
  318. \[
  319. \mbox{key} \underbrace{\longmapsto}_{\mbox{keymapper}} \mbox{string}
  320. \]
  321. \noindent \Graphlet{} uses keys for the following purposes:
  322. \begin{description}
  323. \item[Efficiency] Keys require fewer storage than strings and are
  324. much more efficient to compare.
  325. \item[Flexible enumerations] C++ enumerations are limited in
  326. that they cannot be extended (as opposed to classes). Keys can
  327. be used effectively in place of enumerations (with the
  328. restrictions that they may not used in \texttt{case}
  329. statements, and canot be conterted to integers).
  330. \item[Precomputed Information] \Graphlet{} evaluates a key at the
  331. time it is stored in the repository, and can store additional
  332. information on the key. For example, a GML key which starts with
  333. a lowercase letter is \emph{safe}, white one that starts with an
  334. upper case letter is not.
  335. \item[String values] Keys can be converted to LEDA strings.
  336. Especially, they can be written to files.
  337. \end{description}
  338. \begin{notes}
  339. \item Due to their nature, keys are not type safe. The C++
  340. compiler cannot detect a wrong key, as it could do with an
  341. enumeration. However, this does not necessarily mean that keys
  342. are unsafe by all means; correctness can be enforced with
  343. proper \texttt{assert} statements.
  344. \end{notes}
  345. %
  346. % GT_Keymapper
  347. %
  348. \subsection{The class \GT{Keymapper}}
  349. Keys stored in a global object of type \GT{Keymapper}. The
  350. following methods are available for the class \GT{Keymapper}:
  351. \begin{Cdefinition}
  352. \item[\GT{Key} add (const string\& \Param{s})] \strut \\
  353. The method \texttt{add} adds a new key with the name \emph{s}
  354. to a keymapper, and returns the key. If a key with the same
  355. name already exists, it returns the existing key.
  356. \end{Cdefinition}
  357. \begin{notes}
  358. \item There is no way to remove a key from a keymapper. This is
  359. because the integrity of Graphlet's data structures can only be
  360. guaranteed if a key keeps its value until the end of the
  361. program.
  362. \item Dont use keys for temporary objecte like labels; this
  363. would make the keymapper unneccessarily large.
  364. \item There should be only a single global object of type
  365. \GT{Keymapper}.
  366. \end{notes}
  367. %
  368. % How to add a new Key
  369. %
  370. \subsection{How to add a new Key}
  371. \label{s:HowToAddANewKey}
  372. To add a new key, use the followin C++ code:
  373. \begin{verbatim}
  374. #include <gt_base/Graphlet.h>
  375. GT_Key new_key = graphlet->keymapper.add (
  376. "This is the name of the key");
  377. \end{verbatim}
  378. %
  379. % The class GT_Key
  380. %
  381. \subsection{The class \GT{Key}}
  382. The following methods are available for the class \GT{Key}:
  383. \begin{Cdefinition}
  384. \item[\GT{Key()}] \strut\\
  385. This constructor creates a new key which is not yet
  386. \emph{defined}, i.e. it has no name. See section
  387. \ref{s:HowToAddANewKey} how to create a key with a name.
  388. \item[bool defined() const] \strut\\
  389. The method \texttt{defined} tests wether the key has been
  390. assigned a name. Generally, keys created with the constructor
  391. \GT{Key()} will return \texttt{false}, while keys created with
  392. from \GT{Keymapper::add} will return \texttt{true}. See also
  393. \texttt{active} below.
  394. \item[bool active() const] \strut\\
  395. The method \texttt{active} tests wether a key has been assigned
  396. a name (it is \texttt{defined}) \textbf{and} is not the key
  397. \GT{Keys::def}
  398. \item[const string\& name() const] \strut\\
  399. The method \texttt{named} returns the name of the key. This
  400. method may only be exceuted if \texttt{defined} is true.
  401. \item[bool operator== (const \GT{Key}\& other\_key) const] \strut\\
  402. The operator \texttt{==} compares keys. This is faster than to
  403. compare the names of the keys. Two keys are equal if (a) they
  404. have been created with the same keymapper and (b) their names
  405. are equal.
  406. \item[bool operator!= (const \GT{Key}\& other\_key) const] \strut\\
  407. The operator \texttt{!=} compares keys. This is faster than to
  408. compare the names of the keys. Two keys are not equal if (a)
  409. they have been created with different keymappers or (b) their
  410. names are nor equal.
  411. \item[const \GT{Key\_class}* description () const] \strut\\
  412. Returns a pointer to the \texttt{description} of the key (see
  413. also section \ref{s:GT_Key_description}).
  414. \end{Cdefinition}
  415. \begin{notes}
  416. \item New keys should always be created with the \texttt{add}
  417. method of keymapper. The constructor \verb|GT_Key()| may be
  418. used to create an undefined key.
  419. \item There is no way to make an undefined key defined; assign a
  420. defined key instead, as in
  421. \begin{verbatim}
  422. GT_Key sample_key;
  423. sample_key = graphlet->keymapper.add ("sample_key");
  424. \end{verbatim}
  425. \item There is no restriction on the name of a key; names which
  426. are used as GML keys must however consist of up to 127 letters
  427. and digits only, and the first character must be a letter.
  428. \end{notes}
  429. \subsection{The class \GT{Key\_description}}
  430. \label{s:GT_Key_description}
  431. The only member variable in the class \GT{Key} is a pointer to the
  432. description of the key. This description is an object of type
  433. \GT{Key\_description}, and can be accessed through the method
  434. \GT{Key::description}. The class \GT{Key\_description} provides the
  435. following features:
  436. \begin{Cdefinition}
  437. \item[const string\& name () const] \strut\\
  438. Returns the \emph{name} of the key; the method \GT{Key::name}
  439. is usually a more convenient way to access the name of a key.
  440. \item[bool save() const] \strut\\
  441. Returns wether the key is save in the sense of GML. Roughly
  442. speaking, as safe key represents data which will remain
  443. consistent when other attributes or the graph topology change.
  444. Safe keys start with a lowercase letter, while unsafe keys
  445. start with a capital letter. See the GML manual for details.
  446. There is no way to set this information, it is computed from
  447. the name of the key.
  448. \item[bool visible() const] \strut\\
  449. Returns wether the key is \emph{visible}. A key is visible if
  450. its name starts with a '\@' character and invsisible otherwise.
  451. Invisible keys are not written to GML files.
  452. \end{Cdefinition}
  453. %
  454. % Predefined Keys
  455. %
  456. \section{Predefined Keys}
  457. \label{s:PredefinedKeys}
  458. \CSourceCodeLocation{}{base}{Keys}
  459. \CIncludeStatement{}{base}{Graphlet}
  460. \noindent \Graphlet{} defines a class \GT{Keys} which holds a large
  461. number predefined keys. Each key is defined as a static member
  462. variable, and can be accessed as
  463. \begin{quote}
  464. \GT{Keys::}\emph{key}
  465. \end{quote}
  466. %
  467. % Very special keys
  468. %
  469. \subsubsection{Special keys}
  470. The following special keys are used by Graphlet:
  471. \begin{ttdescription}
  472. \item[\GT{Key} \GT{Keys}::def] \strut\\
  473. This key indictates that the \emph{default} value should be used.
  474. \end{ttdescription}
  475. %
  476. % GML Tags
  477. %
  478. \subsubsection{GML Tags}
  479. Generally, \Graphlet{} defines a key for each GML tag which is
  480. used by \Graphlet{}. Each tag has the form
  481. \begin{quote}
  482. \GT{Key::}\emph{tag}
  483. \end{quote}
  484. \noindent where \emph{tag} is the name of the GML tag.
  485. Graphlet also defines tags \texttt{option\_tag} for each tags;
  486. these are the corresponding Tcl options (``-\texttt{tag'')}.
  487. Especially, the following keys are defined in \GT{keys}:
  488. \begin{verbatim}
  489. static GT_Key graphics, label_graphics;
  490. static GT_Key graph;
  491. static GT_Key node;
  492. static GT_Key edge;
  493. static GT_Key version, option_version;
  494. static GT_Key creator, option_creator;
  495. static GT_Key id, option_id;
  496. static GT_Key uid, option_uid;
  497. static GT_Key label_uid, option_label_uid;
  498. static GT_Key name, option_name;
  499. static GT_Key label, option_label;
  500. static GT_Key graph_attrs, option_graph_attrs;
  501. static GT_Key node_attrs, option_node_attrs;
  502. static GT_Key edge_attrs, option_edge_attrs;
  503. static GT_Key center, option_center;
  504. static GT_Key directed, option_directed
  505. static GT_Key x, option_x;
  506. static GT_Key y, option_y;
  507. static GT_Key w, option_w;
  508. static GT_Key h, option_h;
  509. \end{verbatim}
  510. %
  511. % Keys for colors
  512. %
  513. \subsubsection{Colors}
  514. \begin{alltt}
  515. GT_Keys::white;
  516. GT_Keys::black;
  517. GT_Keys::red;
  518. GT_Keys::green;
  519. GT_Keys::blue;
  520. \end{alltt}
  521. %
  522. % Graphic Objects
  523. %
  524. \subsubsection{Graphic objects}
  525. \begin{alltt}
  526. GT_Keys::type_arc;
  527. GT_Keys::type_bitmap;
  528. GT_Keys::type_image;
  529. GT_Keys::type_line;
  530. GT_Keys::type_oval;
  531. GT_Keys::type_polygon;
  532. GT_Keys::type_rectangle;
  533. GT_Keys::type_text;
  534. \end{alltt}
  535. \noindent See also section \ref{s:Attributes:CommonGraphics}.
  536. \subsubsection{Anchors}
  537. \Graphlet{} predefines the following keys for anchors:
  538. \begin{alltt}
  539. // Node labels
  540. GT_Keys::anchor_center;
  541. GT_Keys::anchor_n;
  542. GT_Keys::anchor_ne;
  543. GT_Keys::anchor_e;
  544. GT_Keys::anchor_se;
  545. GT_Keys::anchor_s;
  546. GT_Keys::anchor_sw;
  547. GT_Keys::anchor_w;
  548. GT_Keys::anchor_nw;
  549. \end{alltt}
  550. \begin{alltt}
  551. // Edge labels
  552. GT_Keys::anchor_first;
  553. GT_Keys::anchor_last;
  554. \end{alltt}
  555. \begin{alltt}
  556. // Edge anchors
  557. GT_Keys::anchor_clip;
  558. GT_Keys::anchor_corners;
  559. GT_Keys::anchor_middle;
  560. GT_Keys::anchor_8;
  561. \end{alltt}
  562. %
  563. % GT_Point
  564. %
  565. \section{The class \GT{Point}}
  566. \CSourceCode{class \GT{Point}}{base}{Geometry.h}
  567. The class \GT{Point} implements 2 dimensional points with
  568. \texttt{double} coordinates.
  569. \begin{Cdefinition}
  570. \item[\GT{Point()}] \strut\\
  571. Creates a new point at $(0.0,0.0)$.
  572. \item[\GT{Point} (double \Param{x}, double \Param{y})] \strut\\
  573. Creates a new point at $(x,y)$.
  574. \item[\GT{Point}(const point\& \Param{p})] \strut\\
  575. Creates a new point from a LEDA \texttt{point} object.
  576. \item[\GT{Point} (const vector\& \Param{v})] \strut\\
  577. Creates a new point from a LEDA \texttt{vector} object.
  578. \item[void x (double \Param{x})] \strut\\
  579. Set the x coordinate of a point.
  580. \item[void y (double \Param{y})] \strut\\
  581. Set the y coordinate of a point.
  582. \item[double x() const] \strut\\
  583. Returns the \texttt{x} coordinate of a point.
  584. \item[double y() const] \strut\\
  585. Returns the \texttt{y} coordinate of a point.
  586. \item[void move (const vector\& \Param{move\_xy})] \strut\\
  587. Move the point by a vector \emph{move\_xy}.
  588. \item[virtual void scale (double scale\_by, const point\& origin = point (0.0,0.0))] \strut\\
  589. Scale the coordinates of the point by a factor
  590. \texttt{scale\_by}, with respect to \texttt{origin}.
  591. \end{Cdefinition}
  592. \begin{notes}
  593. \item In the current implementation, \GT{Point} is derived from
  594. the LEDA class \texttt{point}. This might change in the future.
  595. \item \GT{Point} differs from LEDA's \texttt{point} class in that
  596. LEDA does not provide operations which change the coordinates of a
  597. point.
  598. \end{notes}
  599. %
  600. % GT_Polyline
  601. %
  602. \section{The class \GT{Polyline}}
  603. \CSourceCode{class \GT{Polyline}}{base}{Geometry}
  604. The class \GT{Polyline} implements 2-dimensional polylines or
  605. polygons. \GT{Polyline} is derived from \verb|list<GT_Point>| and
  606. provides the following methods:
  607. \begin{Cdefinition}
  608. \item[GT\_Polyline ()] \strut\\
  609. Creates an empty polyline.
  610. \item[GT\_Polyline (const GT\_Polyline\& \Param{l})] \strut\\
  611. Copy constructor.
  612. \item[GT\_Polyline (const list<GT\_Point>\& \Param{l})] \strut\\
  613. Creates a polyline from a list of \GT{Point} objects.
  614. \item[virtual \~GT\_Polyline ()] \strut\\
  615. Destructor.
  616. \item[segment nth\_segment (const int \Param{n} const] \strut\\
  617. Returns the n\emph{th} segment of the line, that is the segment from
  618. point $n$ to $n+1$.
  619. \item[void move (const vector\& \Param{move\_xy}] \strut\\
  620. Move the polyline by a vector \emph{move\_xy}.
  621. \item[virtual void scale (double scale\_by, const point\& origin = point (0.0,0.0))] \strut\\
  622. Scale the line by a factor \texttt{scale\_by}, with respect to
  623. \texttt{origin}.
  624. \end{Cdefinition}
  625. \begin{notes}
  626. \item A \GT{Polyline} object must contain at least 2 points.
  627. \end{notes}
  628. %
  629. % GT_Rectangle
  630. %
  631. \section{The class \GT{Rectangle}}
  632. \CSourceCode{class \GT{Rectangle}}{base}{Geometry}
  633. The class \GT{Rectangle} implements 2-dimensional rectangles.
  634. \GT{Rectangle} is derived from \GT{Point} and provides the following
  635. methods:
  636. \begin{Cdefinition}
  637. % Constructor / Destructor
  638. \item[GT\_Rectangle ()] \strut\\
  639. Creates an empty rectangle at position $(0,0)$.
  640. \item[GT\_Rectangle (const point\& \Param{p}, double \Param{w}, double \Param{h})] \strut\\
  641. Creates a rectangle with width \emph{w} and height \emph{h} at
  642. position \emph{p}.
  643. \item[GT\_Rectangle (double \Param{x}, double \Param{y}, double \Param{w}, double \Param{h})] \strut\\
  644. Creates a rectangle with width \emph{w} and height \emph{h} at
  645. position $(x,y)$.
  646. \item[virtual \~GT\_Rectangle ()] \strut\\
  647. Virtual destructor.
  648. % w, h
  649. \item[void w (double \Param{new\_width})] \strut\\
  650. Set the width of the rectangle.
  651. \item[double w () const] \strut\\
  652. Return the width of the rectangle.
  653. \item[void h (double \Param{new\_height})] \strut\\
  654. Set the height of the rectangle.
  655. \item[double h () const] \strut\\
  656. Return the height of the rectangle.
  657. % Move and scale
  658. \item[void move (const vector\& \Param{move\_xy}] \strut\\
  659. Move the rectangle by a vector \emph{move\_xy}.
  660. \item[virtual void scale (double scale\_by,
  661. const point\& origin = point (0.0,0.0))] \strut\\
  662. Scale the rectangle by a factor
  663. \texttt{scale\_by}, with respect to \texttt{origin}.
  664. % Utilities
  665. \item[bool includes (const point\& \Param{p}) const] \strut\\
  666. Returns \emph{true} if the point \emph{p} lies within the
  667. rectangle, and \emph{false} otherwise.
  668. \item[void expand (double x, double y)] \strut\\
  669. Expands the rectangle by adding \texttt{x} to the width and
  670. \texttt{y} to the height of the rectangle.
  671. \item[void union\_with (const GT\_Rectangle\& rect)] \strut\\
  672. Creates the union with \texttt{rect}.
  673. % anchor points
  674. \item[point anchor\_c() const;] \strut\\
  675. Returns the coordinates which correspond to a Tk \emph{c} anchor point.
  676. \item[point anchor\_n() const;] \strut\\
  677. Returns the coordinates which correspond to a Tk \emph{n} anchor point.
  678. \item[point anchor\_ne() const;] \strut\\
  679. Returns the coordinates which correspond to a Tk \emph{ne} anchor point.
  680. \item[point anchor\_e() const;] \strut\\
  681. Returns the coordinates which correspond to a Tk \emph{e} anchor point.
  682. \item[point anchor\_se() const;] \strut\\
  683. Returns the coordinates which correspond to a Tk \emph{se} anchor point.
  684. \item[point anchor\_s() const;] \strut\\
  685. Returns the coordinates which correspond to a Tk \emph{s} anchor point.
  686. \item[point anchor\_sw() const;] \strut\\
  687. Returns the coordinates which correspond to a Tk \emph{sw} anchor point.
  688. \item[point anchor\_w() const;] \strut\\
  689. Returns the coordinates which correspond to a Tk \emph{w} anchor point.
  690. \item[point anchor\_nw() const;] \strut\\
  691. Returns the coordinates which correspond to a Tk \emph{nw} anchor point.
  692. \item[double top() const] \strut\\
  693. Return the y coordinate of the top side (i.e.\ the minimum y coordinate)
  694. of the rectangle.
  695. \item[double right() const] \strut\\
  696. Return the x coordinate of the right side (i.e.\ the maximum x coordinate)
  697. of the rectangle.
  698. \item[double bottom() const] \strut\\
  699. Return the y coordinate of the bottom side (i.e.\ the maximum y
  700. coordinate) of the rectangle.
  701. \item[double left() const] \strut\\
  702. Return the y coordinate of the bottom side (i.e.\ the minimum y
  703. coordinate) of the rectangle.
  704. \end{Cdefinition}
  705. \begin{notes}
  706. \item The methods \texttt{top}, \texttt{right}, \texttt{bottom} and
  707. \texttt{left} are not compatible with the \texttt{anchor\_n},
  708. \texttt{anchor\_e}, \texttt{anchor\_s} and \texttt{anchor\_r},
  709. respectively. Roughly speaking, they go in the opposite directions.
  710. \end{notes}
  711. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  712. %
  713. % Using Graphs
  714. %
  715. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  716. \chapter{The class \GT{Graph}}
  717. \label{c:GT_Graph}
  718. \CSourceCode{}{base}{Graph}
  719. %
  720. % The class \GT{Graph}
  721. %
  722. \section{The Class \GT{Graph}}
  723. LEDA's \texttt{graph} class is not really suitable for complex
  724. interactive applications. For example, it lacks a mechanism to
  725. store attributes in nodes and edges. Therefore, Graphlet provides
  726. another data structure \GT{Graph} which extends LEDA graphs with
  727. a rich set of attributes.
  728. Unlike other graph classes within LEDA, \GT{Graph} does not
  729. derive from \texttt{graph}, but is a separate structure which is
  730. attached to a LEDA graph, that is it has a pointer to a LEDA
  731. graph. Whenever the graph topology in the LEDA graph changes,
  732. the \GT{Graph} structure is notified and adjusts itself. This
  733. approach has several advantages:
  734. \begin{itemize}
  735. \item It makes it possible to use graph classes which are
  736. derived from LEDA's \texttt{graph} class. This could also have
  737. been achieved by using templates, but not all current compilers
  738. handle large template structures well. Also, this would lead to
  739. excessive code duplication in the compiled program.
  740. \item The interface becomes much cleaner. \GT{Graph} has only few
  741. dependencies on LEDA's graph structure.
  742. \item Our strategy is backward compatible with existing LEDA
  743. algorithms. Graphlet \emph{can} easily use LEDA algorithms which
  744. are not designed for Graphlet, although they will have no access to
  745. the user interface.
  746. \item It becomes possible\footnote{This feature is not
  747. implemented at the moment.} to change the graph class at
  748. runtime.
  749. \item The data structure is not yet fully exploited.
  750. Technically possible\footnote{Not implemented in the current
  751. version.} extenion capabilities include schemes where one
  752. graph has several independend graphical representations, and
  753. several graph data structures share one graphical
  754. representation.
  755. \end{itemize}
  756. \GT{Graph} provides support for arbitrary graph, node and edge
  757. attributes, and provides the device independent methods for
  758. displaying graphs. There are two classes of attributes:
  759. \Graphlet{} attributes and user defined attributes. See
  760. \ref{c:G++:Attributes} for details.
  761. %
  762. %
  763. %
  764. \section{Creating and Initializing GT\_Graph}
  765. \label{s:CreatingAndInitializingGraph}
  766. This section shows how to create and initialize a \GT{Graph} object.
  767. Programmers who write algorithms which manipulate a given graph may
  768. skip this section. Especially, the class \verb|GT_Tcl_Interface<>|
  769. will provide initialize the graph by itself. See Chapter
  770. \ref{c:TclInterface} for details.
  771. The following steps are necessary to use a GT\_Graph with a LEDA graph
  772. class:
  773. \begin{enumerate}
  774. \item
  775. \emph{Optional.} Declare a class \texttt{C} which is
  776. derived from LEDA's \texttt{graph} class.
  777. \item \label{l:GraphInit:Shuttle} Use the class \GT{Shuttle} to
  778. construct a graph class which can communicate with \GT{Graph}:
  779. \begin{quote}
  780. \verb|GT_Shuttle* c_shuttle = new GT_Leda_Shuttle<C>;|
  781. \end{quote}
  782. The class \GT{Leda\_Shuttle} class adds communication
  783. capabilities to the class \texttt{c}. Any class derived from
  784. \GT{Shuttle} can notify a \GT{Graph} when nodes and edges are
  785. deleted.
  786. \item \label{l:GraphInit:newGraph} Initialize the GT\_Graph as follows:
  787. \begin{quote}
  788. \verb|GT_Graph* gt_graph = new GT_Graph;|
  789. \end{quote}
  790. This creates a new \GT{Graph} which is not yet attached to a
  791. LEDA graph class.
  792. \item \label{l:GraphInit:connect}
  793. Connect the \texttt{c\_shuttle} and the \gt{graph} graph:
  794. \begin{quote}
  795. \verb|gt_graph->leda (*c_shuttle);|
  796. \end{quote}
  797. This step establishes communication between the
  798. \GT{graph} and \texttt{c\_shuttle}.
  799. \item \label{l:GraphInit:initialize} Initialize the graph:
  800. \begin{quote}
  801. \verb|gt_graph->new_graph();|
  802. \end{quote}
  803. This step is neccessary because the method
  804. \texttt{GT\_Graph::new\_graph()} is virtual and this cannot be
  805. used from the constructor.
  806. \end{enumerate}
  807. \begin{notes}
  808. \item \label{n:InitializingGraph} It is technically possible to
  809. skip step \ref{l:GraphInit:Shuttle} and use a graph class which
  810. is not derived from \GT{Leda\_Shuttle} in step
  811. \ref{l:GraphInit:connect}. However, this may only be used for
  812. static graphs, since \GT{Graph} cannot recognize insertions and
  813. deletions. \emph{Use this only if you have to, and if you know
  814. what you are doing}.
  815. \item Any class derived from \GT{Graph} may be used in step
  816. \ref{l:GraphInit:newGraph}. To use the Tcl interface,
  817. initialize with \GTTcl{Graph} instead of \GT{Graph}. See
  818. also Chapter \ref{c:TclInterface} for details.
  819. \end{notes}
  820. %
  821. % Using GT_Graph
  822. %
  823. \section{How to use \GT{Graph}}
  824. \label{s:UsingGTGRaph}
  825. \subsection{How to access the LEDA graph structure}
  826. The LEDA graph associated with a Graphlet graph can be accessed
  827. through the methods \texttt{leda} and \texttt{attached}:
  828. \begin{Cdefinition}
  829. \item[graph\& GT\_Graph::leda()] \strut\\
  830. This method returns a reference to the underlying LEDA graph. It is
  831. legal to make changes in the graph if graph has been initialized
  832. with a \GT{Shuttle} structure, which is the default in \Graphlet{}
  833. (see section \ref{s:CreatingAndInitializingGraph}, especially
  834. note \ref{n:InitializingGraph}).
  835. \item[const graph\& GT\_Graph::leda() const] \strut\\
  836. This method returns a constant reference to the underlying LEDA
  837. graph. As aleays, this is the preferred way to access a graph.
  838. \item[void GT\_Graph::leda (graph* \Param{g})] \strut\\
  839. Connects the LEDA graph \emph{g} with a \GT{Graph} object. See
  840. section \ref{s:CreatingAndInitializingGraph} for details.
  841. If this method is used, then changes in the leda graph
  842. \emph{cannot} be recognized by the \GT{Graph} object.
  843. \item[void GT\_Graph::leda (graph\& \Param{g})] \strut\\
  844. Same as above, but uses a reference instead of a pointer.
  845. \item[void GT\_Graph::leda (GT\_Shuttle\& \Param{g})] \strut\\
  846. Connects the LEDA graph \emph{g} with a \GT{Shuttle} object.
  847. See section \ref{s:CreatingAndInitializingGraph} for
  848. details. This method should always be preferred over the last
  849. two ones, since it enables \GT{Graph} to detect changes in the
  850. graph structure.
  851. \end{Cdefinition}
  852. \noindent Use the following shown in example
  853. \ref{e:InsertingAndDeletingNodesAndEdge} to insert or delete
  854. nodes and edges in a \GT{Graph}. The general approach is as
  855. follows.
  856. \begin{enumerate}
  857. \item Get a \emph{reference} to the LEDA graph via the method
  858. \GT{graph::leda()}. It is important to get a reference; the
  859. statement \verb|graph g = gt_graph.leda();| will construct a
  860. \textbf{copy} of the graph.
  861. \item Perform the changes in this graph.
  862. \end{enumerate}
  863. \begin{example}{e:InsertingAndDeletingNodesAndEdges}%
  864. {A simple example showing how to insert nodes and edges}
  865. \begin{verbatim}
  866. void sample (GT_Graph& gt_graph)
  867. {
  868. graph& g = gt_graph.leda();
  869. node n;
  870. forall_nodes (n, g) {
  871. g.delete_node (n);
  872. }
  873. for (int i=0; i<42; i++) {
  874. g.new_node ();
  875. }
  876. }
  877. \end{verbatim}
  878. \end{example}
  879. %
  880. % Source and Target
  881. %
  882. \subsection{Source and Target nodes}
  883. \Graphlet{} always makes a distinction between source and target
  884. nodes, even in undirected graphs. This is done because when an edge
  885. is drawn, the coordinate assignment (\emph{``from (x1,y1) to
  886. (x2,y2)''}) imposes a direction, even on an undirected graph.
  887. %
  888. % Copy
  889. %
  890. \subsection{Copy Operations}
  891. \GT{Graph} provides copy operations for nodes and edges. These
  892. operations will also copy \emph{all} attributes which are
  893. associated with the node or edge.
  894. \begin{Cdefinition}
  895. \item[virtual node copy (node n, \GT{Graph}\& into\_graph);] \strut\\
  896. Creates a copy of \texttt{old\_node} in graph
  897. \texttt{into\_graph} and returns it.
  898. \item[virtual edge copy (edge e, node source, node target,
  899. \GT{Graph}\& into\_graph);] \strut\\
  900. Creates a copy of \texttt{old\_edge} in graph
  901. \texttt{into\_graph} and returns it. The endnodes of the new edge are
  902. \texttt{source} and \texttt{target},
  903. \end{Cdefinition}
  904. \noindent In both methods, \texttt{into\_graph} may be \texttt{*this};
  905. in this case, a copy operation \emph{within} the same graph is
  906. accomplished.
  907. %
  908. % Utilities
  909. %
  910. \subsection{Graph Utilities}
  911. \subsubsection{Bounding Box}
  912. The bounding box is the smalles rectangle in which the graph is
  913. enclosed. \GT{Graph} provides several methods to compute the
  914. bounding box of graphs, nodes and edges:
  915. \begin{Cdefinition}
  916. \item[GT\_Rectangle bbox () const;] \strut\\
  917. Computes the bounding box of the graph.
  918. \item[GT\_Rectangle bbox (node n) const;]\ \strut\\
  919. Computes the bounding box of node \texttt{n}.
  920. \item[GT\_Rectangle bbox (edge e) const;] \strut\\
  921. Computes the bounding box of edge \texttt{e}.
  922. \item[virtual void bbox (GT\_Rectangle\& bbox) const;] \strut\\
  923. Computes the bounding box of the graph, and returns it in \texttt{bbox}.
  924. \item[virtual void bbox (node n, GT\_Rectangle\& bbox) const;] \strut\\
  925. Computes the bounding box of node \texttt{n}, and returns it in
  926. \texttt{bbox}.
  927. \item[virtual void bbox (edge e, GT\_Rectangle\& bbox) const;] \strut\\
  928. Computes the bounding box of edge \texttt{e}, and returns it in
  929. \texttt{bbox}.
  930. \end{Cdefinition}
  931. %
  932. % Search
  933. %
  934. \subsubsection{Search for a node or edge with a given id.}
  935. The following methods search for a node resp.\ edge with a given
  936. id (see also chapter \ref{c:G++:Attributes} on attributes). They
  937. return the corresponding object, if found, and \texttt{0}
  938. otherwise.
  939. \begin{Cdefinition}
  940. \item[node find\_node (const int id);] \strut\\
  941. Searches for a node \emph{n} with \verb|gt(|\emph{n}\verb|).id() == id|.
  942. \item[edge find\_edge (const int id);] \strut\\
  943. Searches for an edge \emph{e} with \verb|gt(|\emph{e}\verb|).id() == id|.
  944. \end{Cdefinition}
  945. \begin{notes}
  946. \item \texttt{id} numbers are assigned by Graphlet and are
  947. guaranteed to be unique within a given graph.
  948. \end{notes}
  949. %
  950. % GT_Graph Attributes
  951. %
  952. \section{\GT{Graph} Attributes}
  953. \label{s:GraphAttributes}
  954. The class \GT{Graph} provides the \texttt{gt} methods as
  955. shortcuts to access the attributes of a graph, node or edge (see
  956. also chapter \ref{c:G++:Attributes}).
  957. \begin{Cdefinition}
  958. \item[GT\_Graph\_Attributes\& GT\_Graph::gt()] \strut\\
  959. Provides access to the attributes of the graph.
  960. \item[const GT\_Graph\_Attributes\& GT\_Graph::gt() const] \strut\\
  961. \texttt{const} version of the above method. This is the
  962. preferred access method.
  963. \item[GT\_Node\_Attributes\& GT\_Graph::gt(node \Param{n})] \strut\\
  964. Provides access to the attribute of node \emph{n}.
  965. \item[const GT\_Node\_Attributes\& GT\_Graph::gt(node \Param{n}) const]
  966. \strut\\
  967. \texttt{const} version of the above method. This is the
  968. preferred access method.
  969. \item[GT\_Edge\_Attributes\& GT\_Graph::gt(edge \Param{e})] \strut\\
  970. Provides access to the attribute of edge \emph{e}.
  971. \item[const GT\_Edge\_Attributes\& GT\_Graph::gt(edge
  972. \Param{e}) const]
  973. \strut\\
  974. \texttt{const} version of the above method. This is the
  975. preferred access method.
  976. \end{Cdefinition}
  977. Example \ref{e:C++:AttributesIntro} shows a simple example which
  978. changes the labels of a graph. Example \ref{e:C++:stLabels} example
  979. assigns a label ``s - t'' to each edge in the graph, where \emph{s} is
  980. the label of the source node and \emph{t} is the label of the target
  981. node.
  982. \begin{example}{e:C++:AttributesIntro}{A Simple Example for the use of
  983. Graphlet Attributes}
  984. \begin{verbatim}
  985. GT_Graph g;
  986. //
  987. // Retrieve the label of a graph
  988. //
  989. const string& l1 = g.gt().label();
  990. //
  991. // Set the label of g to "42"
  992. //
  993. g.gt().label("42");
  994. \end{verbatim}
  995. \end{example}
  996. \begin{example}{e:C++:stLabels}{Assigning ``s - t`` to edge labels in a
  997. graph}
  998. \begin{verbatim}
  999. GT_Graph g;
  1000. edge e;
  1001. forall_edges (e,g) {
  1002. // Find source and target nodes
  1003. node source = g.gt(e).source();
  1004. node target = g.gt(e).target();
  1005. // Assemble a new label
  1006. string new_label = g.gt(source).label() + " - " + g.gt(target).label();
  1007. // set the new label
  1008. g.gt(e).label (l);
  1009. }
  1010. \end{verbatim}
  1011. \end{example}
  1012. %
  1013. % Attribute Implementation Notes
  1014. %
  1015. \subsection{How to detect wether an attribute has changed}
  1016. \label{s:DetectingChangesInAttributes}
  1017. \Graphlet{}'s attributes are based on the class
  1018. \GT{Tagged\_Attributes}. \GT{Tagged\_Attributes} implements a
  1019. mechanism which tracks wether attributes have been
  1020. \emph{initialized} or \emph{changed}:
  1021. \begin{description}
  1022. \item[initialized] is set as soon as a value is assigned to the
  1023. attribut.
  1024. \item[changed] indicates that the attribute has changed since the
  1025. last draw peration. \emph{changed} is set whenever the
  1026. value of the attribute is changed. \emph{changed} is reset when
  1027. the object (that is, the graph, node, edge or label) is redrawn.
  1028. \end{description}
  1029. \noindent The following classes are derived from \GT{Tagged\_Attributes}:
  1030. \begin{itemize}
  1031. \item \GT{Common\_Attributes}. This class is the base class for
  1032. \GT{Node\_Attributes}, \GT{Edge\_Attributes} and
  1033. \GT{Graph\_Attributes}.
  1034. \item \GT{Common\_Graphics}. This class is the base class for
  1035. \GT{Node\_Graphics}, \GT{Edge\_Graphics} and
  1036. \GT{Graph\_Graphics}.
  1037. \item \GT{Node\_NEI}, \GT{Edge\_NEI}. These classes implement
  1038. Graphlet's node and edge anchors.
  1039. \end{itemize}
  1040. \noindent The class \GT{Tagged\_Attributes} uses a bit set
  1041. \footnote{The tags are implemented with the C++ datatype
  1042. \texttt{unsigned} in the current implementation, although this
  1043. might change in the future} to keep track of the state of
  1044. attributes. This set is defined as follows. Let $A$ be the set
  1045. of attributes implemented in the class based on
  1046. \GT{Tagged\_Attributes}\footnote{Except for graphics, $A$
  1047. consists of (the names of) all attributes as listed in this
  1048. documentation. In the case of graphics, $A$ consists of all
  1049. attributes except \texttt{center} and \texttt{line}, which are
  1050. replaced by \texttt{geometry}}. For each $a \in A$, Graphlet
  1051. defines a constant \texttt{tag\_}$a$. Then, the bitsets
  1052. \texttt{initialized}, \texttt{changed} and \texttt{updated} are
  1053. defined as
  1054. \[
  1055. \mbox{initialized}, \mbox{changed}
  1056. \subseteq
  1057. \bigcup_{a \in A} \mathtt{tag\_}a
  1058. \]
  1059. %Wow, I can do formulas in LaTeX !
  1060. \noindent For example, the set of all graphics attributes which affect labels
  1061. can be written in C++ as
  1062. \begin{verbatim}
  1063. unsigned text_attributes =
  1064. GT_Common_Graphics::tag_anchor |
  1065. GT_Common_Graphics::tag_geometry |
  1066. GT_Common_Graphics::tag_fill |
  1067. GT_Common_Graphics::tag_font |
  1068. GT_Common_Graphics::tag_justify |
  1069. GT_Common_Graphics::tag_stipple |
  1070. GT_Common_Graphics::tag_width;
  1071. \end{verbatim}
  1072. \noindent The following methods can be used to examine the status of
  1073. attributes:
  1074. \begin{Cdefinition}
  1075. \item[bool is\_initialized (const unsigned attribute) const] \strut\\
  1076. Returns whether \texttt{attribute} has been initialized. If
  1077. used with a set of attributes, checks wether at least one of
  1078. the attributes is initialized.
  1079. \item[unsigned initialized () const] \strut\\
  1080. Returns the set of initialized attributes.
  1081. \item[unsigned nothing\_initialized () const] \strut\\
  1082. Returns \emph{true} if \emph{no} attribute has been
  1083. initialized, and \emph{false} otherwise.
  1084. \item[bool is\_changed (const unsigned attribute) const] \strut\\
  1085. Returns whether \texttt{attribute} has changed since the last draw
  1086. operation. If used with a set of attributes, checks wether at least
  1087. one of the attributes is changed.
  1088. \item[unsigned changed () const] \strut\\
  1089. Returns the set of attributes which have been changed since the
  1090. last drawing operation.
  1091. \item[unsigned nothing\_changed ()] \strut\\
  1092. Returns \emph{true} if no attribute has been changed since
  1093. the last drawing operation, and \emph{false} otherwise.
  1094. \item[bool set\_changed (const unsigned attribute) const] \strut\\
  1095. Declares that attribute (which may be a single attribute or a
  1096. set of attributes) has changed since the last drawing
  1097. operation. This means that the attribute will be upated and
  1098. drawn in the next drawing operation.
  1099. \item[bool reset\_changed (const unsigned attribute)] \strut\\
  1100. Declares that attribute (which may be a single attribute or a
  1101. set of attributes) has not changed since the last drawing
  1102. operation. This means that the attribute will \textbf{not} be
  1103. upated and \textbf{not} be drawn in the next drawing operation.
  1104. \emph{Handle with care}.
  1105. \item[void all\_changed (unsigned begin, unsigned end)] \strut\\
  1106. This method sets the state of all initialized attributed to
  1107. \emph{changed}.
  1108. \end{Cdefinition}
  1109. \begin{notes}
  1110. \item There are only few situations where
  1111. \texttt{set\_changed}, \texttt{reset\_changed} and
  1112. \texttt{all\_changed} need to be used, and all of them are
  1113. related to speed optimization. Handle with care unless you are
  1114. in the mood for an extended debugging session.
  1115. \end{notes}
  1116. %
  1117. % Drawing
  1118. %
  1119. \section{Drawing Graphs}
  1120. This section explains how to use \GT{Graph}'s \texttt{draw}
  1121. methods to draw a graph or a portion of a graph explicitly.
  1122. Algorithms which use the class \verb|GT_Tcl_Algorithm<>| and do
  1123. \emph{not} perform algorithm animation, do not need to call
  1124. \texttt{draw} explicitly. Implementore of such algorithms can
  1125. gladly skip this section.
  1126. Graphlet does not automatically redraw a graph whenever an
  1127. attribute is changed\footnote{If we would do that, the
  1128. performance penalty would almost be the death-of-the-system
  1129. penalty.}. Instead, the programmer must explicitly draw the
  1130. graph. \texttt{GT}\_Graph provides the following methods to draw
  1131. graphs:
  1132. \begin{Cdefinition}
  1133. \item[int GT\_Graph::draw()]
  1134. Draws the whole graph on all drawing areas which are associated with
  1135. the graph.
  1136. \item[int GT\_Graph::draw (node \Param{n})]
  1137. Draws the node \emph{n} on all drawing areas which are associated with
  1138. the graph.
  1139. \item[int GT\_Graph::draw (edge \Param{e})]
  1140. Draws the edge \emph{e} on all drawing areas which are associated with
  1141. the graph.
  1142. \end{Cdefinition}
  1143. \begin{example}{e:drawExample}{Move all nodes by 10 pixels}
  1144. \begin{verbatim}
  1145. forall_nodes (n, g.leda()) {
  1146. double x = g.gt(n).graphics()->x();
  1147. double y = g.gt(n).graphics()->y();
  1148. g.gt(n).graphics()->x (x+10);
  1149. g.gt(n).graphics()->y (y+10);
  1150. }
  1151. g.draw();
  1152. \end{verbatim}
  1153. \end{example}
  1154. \noindent Example \ref{e:drawExample} shows how to use the
  1155. \texttt{draw} method. In general, the method \texttt{draw()} is
  1156. less efficient than \texttt{draw(node)} or \texttt{draw(edge)},
  1157. since \texttt{draw()} has to search the whole graph for objects
  1158. which must be updated. However, it is usually a good idea to
  1159. call \texttt{draw()} at the end of an algorithm unless the
  1160. following three points apply:
  1161. \begin{itemize}
  1162. \item The application is time critical,
  1163. \item The programmer exactly knows which changes have occurred,
  1164. and updates them manually with \texttt{draw(node)} and
  1165. \texttt{draw(edge)} operations.
  1166. \end{itemize}
  1167. %
  1168. % Performance Issues
  1169. %
  1170. \subsection{Performance Issues}
  1171. Drawing graphs is actually a lot more than just translating
  1172. coordinates into graphics operations. For example, if the
  1173. coordinates of a node have changed, then the coordinates of its
  1174. label, all its adjacent edges and probably their labels must be
  1175. adjusted. If an algorithm updates coordinates step-by-step, this
  1176. would result in many too many updates.
  1177. Therefore, unless algorithm animation is intended, the
  1178. \texttt{draw} methods should only be called at the end of
  1179. an algorithm.
  1180. %
  1181. %
  1182. %
  1183. \subsection{Animation}
  1184. \GT{Graph} supports animation out of the box; all the user has
  1185. to do are the following two steps:
  1186. \begin{itemize}
  1187. \item Call \texttt{draw} at the point where the graph on the
  1188. screen should be updated.
  1189. \item Call the \textbf{Tcl command} \texttt{update} to update the
  1190. screen, e.g.
  1191. \begin{verbatim}
  1192. Tcl_Interp* interp = tcl_interpreter();
  1193. int code = Tcl_Eval (interp, "update");
  1194. if (code == TCL_ERROR) {
  1195. return TCL_ERROR;
  1196. }
  1197. \end{verbatim}
  1198. where \texttt{interp} is the current Tcl interpreter. One way to
  1199. access the interpreter is through the method \GTTcl{info::interp()}.
  1200. \end{itemize}
  1201. \begin{notes}
  1202. \item It is usually easier to implement algorithm animation
  1203. with GraphScript than from C++.
  1204. \item Contrary to common wisdom, the method \GT{Graph::update}
  1205. is \textbf{not} a wrapper for evaluating the Tcl command
  1206. \texttt{update}.
  1207. \end{notes}
  1208. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1209. %
  1210. % Graphlet Attributes
  1211. %
  1212. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1213. \chapter{Graphlet Attributes}
  1214. \label{c:Attributes}
  1215. The attributes in Graphlet can roughly be divided in the following
  1216. categories:
  1217. \begin{itemize}
  1218. \item Common attributes for graphs, nodes and edges (Section
  1219. \ref{s:Attributes:Common})
  1220. \item Graph specific attributes (Section
  1221. \ref{s:Attributes:Graph})
  1222. \item Node specific attributes (Section
  1223. \ref{s:Attributes:Node})
  1224. \item Edge specific attributes (Section \ref{s:Attributes:Edge})
  1225. \item Graphics attributes (Section
  1226. \ref{s:Attributes:Graphics}). Graphs, nodes and edges share
  1227. \emph{one} data structure for this.
  1228. \end{itemize}
  1229. Most attributes in the C++ interface are also available in
  1230. GraphScript, and have a corresponding GML key. We have tried to
  1231. keep names of these attributes and their values are kept as
  1232. similar as possible.
  1233. %
  1234. % Data Structure
  1235. %
  1236. \section{Data Structure}
  1237. %
  1238. % Common Attributes
  1239. %
  1240. \section{Common Attributes}
  1241. \label{s:Attributes:Common}
  1242. The following attributes are common with graphs, nodes and edges:
  1243. \begin{CAttributes}
  1244. \item[int id] \strut\\
  1245. The \texttt{id} is a number which is assigned to an object.
  1246. Note that node and edge \texttt{id}'s are only unique within
  1247. their graph. Nodes in different graphs may have the same id.
  1248. \item[string label] \strut\\
  1249. This is the textual label of the graph, node or edge. Currently,
  1250. graph labels are not displayed.
  1251. \item[\GT{Key} label] \strut\\
  1252. See the sections on node \ref{s:Attributes:Node} and edge
  1253. attributes \ref{s:Attributes:Edge}.
  1254. \item[int uid] \strut\\
  1255. This is the \emph{user interface id} of the object (graph,
  1256. node, edge) as used by the Tcl interface (see also \ref{unknown}).
  1257. \emph{This value is initialized by Graphlet and should not be changed.}
  1258. \item[int label\_uid] \strut\\
  1259. This is the \emph{user interface id} of the label (graph,
  1260. node, edge) as used by the Tcl interface (see also \ref{unknown}).
  1261. \emph{This value is initialized by Graphlet and should not be changed.}
  1262. \end{CAttributes}
  1263. %
  1264. % Graph specific Attributes
  1265. %
  1266. \section{Graph specific Attributes}
  1267. \label{s:Attributes:Graph}
  1268. The following attributes are specific for graphs:
  1269. \begin{CAttributes}
  1270. \item[int version] \strut\\
  1271. Reserved for future use. The corresponding GML attribute is
  1272. \texttt{Version}).
  1273. \item[string creator] \strut\\
  1274. The program which created the graph (default is ``Graphlet'').
  1275. \item[GT\_Graph\_Graphics* graphics] \strut\\
  1276. Pointer to the graphical description of the graph. For details, see
  1277. the Section \ref{s:Attributes:Graphics}.
  1278. \item[GT\_Graph\_Graphics* label\_graphics] \strut\\
  1279. Pointer to the graphical description of the label of the graph. For
  1280. details, see Section \ref{s:Attributes:Graphics}.
  1281. \end{CAttributes}
  1282. %
  1283. % Node specific Attributes
  1284. %
  1285. \section{Node specific Attributes}
  1286. \label{s:Attributes:Node}
  1287. The following attributes are specific for nodes:
  1288. \begin{CAttributes}
  1289. \item[\GT{Graph}* g] \strut\\
  1290. Pointer to the graph.
  1291. \item[edge e] \strut\\
  1292. Pointer to the edge.
  1293. \item[GT\_Node\_Graphics* graphics] \strut\\
  1294. Pointer to the graphical description of the node. For details,
  1295. see Section \ref{s:Attributes:Graphics}.
  1296. \item[GT\_Node\_Graphics* label\_graphics] \strut\\
  1297. Pointer to the graphical description of the label of the node.
  1298. For details, see Section \ref{s:Attributes:Graphics}.
  1299. \item[\GT{Key} label\_anchor] \strut\\
  1300. This controls where the label is placed. Values are:
  1301. \begin{ttdescription}
  1302. \item[GT\_Keys::anchor\_center] \strut\\
  1303. The label is placed at the center of the bounding rectangle of the
  1304. node.
  1305. \item[GT\_Keys::anchor\_n] \strut\\
  1306. The label is placed at the center of the top side of the
  1307. bounding rectangle of the node.
  1308. \item[GT\_Keys::anchor\_ne] \strut\\
  1309. The label is place in the upper right corner of the bounding
  1310. rectangle of the node.
  1311. \item[GT\_Keys::anchor\_e] \strut\\
  1312. The label is placed at the center of the right side of the
  1313. bounding rectangle of the node.
  1314. \item[GT\_Keys::anchor\_se] \strut\\
  1315. The label is place in the lower right corner of the bounding
  1316. rectangle of the node.
  1317. \item[GT\_Keys::anchor\_s] \strut\\
  1318. The label is placed at the center of the bottom side of the
  1319. bounding rectangle of the node.
  1320. \item[GT\_Keys::anchor\_sw] \strut\\
  1321. The label is place in the lower left corner of the bounding
  1322. rectangle of the node.
  1323. \item[GT\_Keys::anchor\_w] \strut\\
  1324. The label is placed at the center of the left side of the
  1325. bounding rectangle of the node.
  1326. \item[GT\_Keys::anchor\_nw] \strut\\
  1327. The label is place in the upper left corner of the bounding
  1328. rectangle of the node.
  1329. \end{ttdescription}
  1330. \begin{notes}
  1331. \item Note that the placement of a label is also affected by
  1332. the attributes \texttt{anchor} and \texttt{justify} of the
  1333. graphics information which is associated with the label. See
  1334. section \ref{s:Attributes:CommonGraphics} for details.
  1335. \end{notes}
  1336. \item[\GT{Node\_NEI}*, node\_nei] \strut\\
  1337. Pointer to the node/edge interface structure which controls how
  1338. edges are attached to their endnodes. Ask Walter Bachl,
  1339. \url{bachl@fmi.uni-passau.de} for details.
  1340. \end{CAttributes}
  1341. %
  1342. % Edge specific Attributes
  1343. %
  1344. \section{Edge specific Attributes}
  1345. \label{s:Attributes:Edge}
  1346. The following attributes are specific for edges:
  1347. \begin{CAttributes}
  1348. \item[\GT{Graph}* g] \strut\\
  1349. Pointer to the graph.
  1350. \item[edge e] \strut\\
  1351. Pointer to the edge.
  1352. \item[node source] \strut\\
  1353. Pointer to the source node of the edge. \emph{This attribute is
  1354. managed by \GT{Graph} and must not be changed.}
  1355. \item[node target] \strut\\
  1356. Pointer to the target node of the edge. \emph{This attribute is
  1357. managed by \GT{Graph} and must not be changed.}
  1358. \item[GT\_Edge\_Graphics* graphics] \strut\\
  1359. Pointer to the graphical description of the edge. For details, see
  1360. Section \ref{s:Attributes:Graphics}.
  1361. \item[GT\_Edge\_Graphics* label\_graphics] \strut\\
  1362. Pointer to the graphical description of the label of the edge. For
  1363. details, see Section \ref{s:Attributes:Graphics}.
  1364. \item[\GT{Key} label\_anchor] \strut\\
  1365. This controls where the label is placed. Values are:
  1366. \begin{description}
  1367. \item[\GT{Keys::anchor\_first}] \strut\\
  1368. Attach the label to the first segment of the edge.
  1369. \item[\GT{Keys::anchor\_center}] \strut\\
  1370. Attach the label to the middle segment of the edge.
  1371. \item[\GT{Keys::anchor\_last}] \strut\\
  1372. Attach the label to the last segment of the edge.
  1373. \end{description}
  1374. \begin{notes}
  1375. \item In all cases, the label is attached at the center of
  1376. the corresponding segment.
  1377. \item Note that the placement of a label is also affected by
  1378. the attributes \texttt{anchor} and \texttt{justify} of the
  1379. graphics information which is associated with the label. See
  1380. section \ref{s:Attributes:CommonGraphics} for details.
  1381. \end{notes}
  1382. \item[\GT{Edge\_NEI}* edge\_nei] \strut\\
  1383. Pointer to the node/edge interface structure which controls how
  1384. edges are attached to their endnodes. Ask Walter Bachl,
  1385. \url{bachl@fmi.uni-passau.de} for details.
  1386. \end{CAttributes}
  1387. %
  1388. % Graphics Attributes
  1389. %
  1390. \section{Graphics Attributes}
  1391. \label{s:Attributes:Graphics}
  1392. The \Graphlet{} attributes for graphs, nodes and edges do not
  1393. contain information on the graphical appearance of graphs, nodes
  1394. and edges. Instead, the graphical information is stored in
  1395. separate classes \GT{Graph\_Graphics}\footnote{The classes
  1396. \GT{Graph\_Graphics}, \GT{Node\_Graphics} and
  1397. \GT{Edge\_Graphics} are currently dummies and are provided for
  1398. future extensions.}, \GT{Node\_Graphics} and
  1399. \GT{Edge\_Graphics}, which are all based on the class
  1400. \GT{Common\_Graphics}. With that, the graphical appearance of
  1401. graphs, nodes, edges and their label is described and handled
  1402. through a common, device independend data structure.
  1403. The classes \GT{Graph\_Attributes}, \GT{Node\_Attributes} and
  1404. \GT{Edge\_Attributes} contain pointers to these graphical
  1405. descriptions. Per convention, these pointers are accessed through the
  1406. methods \texttt{graphics} and \texttt{label\_graphics}.
  1407. \begin{notes}
  1408. \item Graphlet does not guarantee that these pointers are
  1409. \textbf{not} \texttt{0}. Instead, every procedure which uses
  1410. graphics information must test wether the graphics are not
  1411. \texttt{0}.
  1412. \item The correct way to initialize graphics information is as follows:
  1413. \begin{verbatim}
  1414. if (g.gt().graphics() == 0) {
  1415. g.gt().graphics (g.new_graph_graphics());
  1416. }
  1417. \end{verbatim}
  1418. \item Done use \verb|new GT_Graph_Graphics| instead of
  1419. \verb|g.new_graph_graphics()| in the above program; as this
  1420. will override customization options.
  1421. \end{notes}
  1422. \subsection{How to access a graphics attribute}
  1423. The following C++ constructs retrieve the value of a graphics
  1424. attribute:
  1425. \begin{ttdescription}
  1426. \item[\Param{g}.gt().graphics()-$>$\Param{a}()] \strut\\
  1427. Access attribute \emph{a} of graph \texttt{g}.
  1428. \item[\Param{g}.gt(n).graphics()-$>$\Param{a}()] \strut\\
  1429. Access attribute \emph{a} of node \texttt{n} in graph \texttt{g}.
  1430. \item[\Param{g}.gt(e).graphics()-$>$\Param{a}()] \strut\\
  1431. Access attribute \emph{a} of edge \texttt{e} in graph \texttt{g}.
  1432. \item[\Param{g}.gt().label\_graphics()-$>$\Param{a}()] \strut\\
  1433. Access attribute \emph{a} of the label of graph \texttt{g}.
  1434. \item[\Param{g}.gt(n).label\_graphics()-$>$\Param{a}()] \strut\\
  1435. Access attribute \emph{a} of the label of node \texttt{n} in graph
  1436. \texttt{g}.
  1437. \item[\Param{g}.gt(e).label\_graphics()-$>$\Param{a}()] \strut\\
  1438. Access attribute \emph{a} of the label of edge \texttt{e} in graph
  1439. \texttt{g}.
  1440. \end{ttdescription}
  1441. The following C++ constructs are used to set or change the value
  1442. of a graphics attribute:
  1443. \begin{ttdescription}
  1444. \item[\Param{g}.gt().graphics()-$>$\Param{a} (\Param{new\_a});]
  1445. \strut\\
  1446. Set graphics attribute \Param{a} of the label of graph
  1447. \Param{g} to the value \Param{new\_a}.
  1448. \item[\Param{g}.gt(\Param{n}).graphics()-$>$\Param{a} (\Param{new\_a});]
  1449. \strut\\
  1450. Set graphics attribute \Param{a} of the label of node \Param{n}
  1451. in graph \Param{g} to the value \Param{new\_a}.
  1452. \item[\Param{g}.gt(\Param{e}).graphics()-$>$\Param{a} (\Param{new\_a});]
  1453. \strut\\
  1454. Set graphics attribute \Param{a} of the label of edge \Param{e}
  1455. in graph \Param{g} to the value \Param{new\_a}.
  1456. \item[\Param{g}.gt().label\_graphics()-$>$\Param{a} (\Param{new\_a});]
  1457. \strut\\
  1458. Set graphics attribute \Param{a} of the label of graph \Param{g}
  1459. to the value \Param{new\_a}.
  1460. \item[\Param{g}.gt(\Param{n}).label\_graphics()-$>$\Param{a} (\Param{new\_a});]
  1461. \strut\\
  1462. Set graphics attribute \Param{a} of the label of node \Param{n} in
  1463. graph \Param{g} to the value \Param{new\_a}.
  1464. \item[\Param{g}.gt(\Param{e}).label\_graphics()-$>$\Param{a} (\Param{new\_a});]
  1465. \strut\\
  1466. Set graphics attribute \Param{a} of the label of edge \Param{e} in
  1467. graph \Param{g} to the value \Param{new\_a}
  1468. \end{ttdescription}
  1469. %
  1470. % Common Graphics Attributes
  1471. %
  1472. \subsection{Common Graphics Attributes}
  1473. \label{s:Attributes:CommonGraphics}
  1474. \Graphlet{}'s class \GT{Common\_Graphics} implements a universal,
  1475. device independent description of the graphical appearance of an
  1476. object. Common graphics attributes describe the appearance of
  1477. graphs, nodes, edges and their labels. The graphical attributes
  1478. are based on the graphical attributes used in the Tk toolkit.
  1479. The following list of graphics attributes implemented with
  1480. Graphlet. All these attributes are modelled after the
  1481. corresponding Tk canvas attributes. For a detailed discussion,
  1482. see the Tk documentation on canvases.
  1483. \begin{CAttributes}
  1484. \item[GT\_Key type] \strut\\
  1485. This is the Tk item type of the object. \Graphlet{} supports all Tk
  1486. item types except window. The following values are valid types:
  1487. \begin{ttdescription}
  1488. \item[GT\_Keys::type\_arc] \strut\\
  1489. The Tk graphics object type \texttt{arc}.
  1490. \item[GT\_Keys::type\_bitmap] \strut\\
  1491. The Tk graphics object type \texttt{bitmap}.
  1492. \item[GT\_Keys::type\_image] \strut\\
  1493. The Tk graphics object type \texttt{image}.
  1494. \item[GT\_Keys::type\_line] \strut\\
  1495. The Tk graphics object type \texttt{line}.
  1496. \item[GT\_Keys::type\_oval] \strut\\
  1497. The Tk graphics object type \texttt{oval}.
  1498. \item[GT\_Keys::type\_polygon] \strut\\
  1499. The Tk graphics object type \texttt{polygon}.
  1500. \item[GT\_Keys::type\_rectangle] \strut\\
  1501. The Tk graphics object type \texttt{rectangle}.
  1502. \item[GT\_Keys::type\_text] \strut\\
  1503. The Tk graphics object type \texttt{text}.
  1504. \end{ttdescription}
  1505. \begin{notes}
  1506. \item It is legal to change the type of a graphics object after it has
  1507. been initialized. However, special precaution has to be taken to
  1508. keep the selection up to date.
  1509. \emph{Documentation of this will be extended as soon as the API is
  1510. ready}. \ToDo{}.
  1511. \item The current implementation of Graphlet assumes that the type
  1512. \emph{text} is only used for labels.
  1513. \item The current implementation of Graphlet assumes that
  1514. edges always have the type \texttt{line}.
  1515. \item The current implementation of Graphlet assumes that
  1516. edges always have the type \texttt{text}.
  1517. \end{notes}
  1518. \item[GT\_Rectangle center] \hfill
  1519. {\footnotesize arc, bitmap, image, oval, polygon, rectangle, \emph{text}} \\
  1520. This is a rectangle which describes the center of an object and
  1521. its width and height.
  1522. \begin{notes}
  1523. \item
  1524. The geometry of \emph{line} and \emph{polyline} objects is described
  1525. by the \texttt{line} attribute, and \texttt{center} is undefined for
  1526. them.
  1527. \item The \emph{width} and \emph{height} of \emph{text} objects
  1528. are undefined, as this will be calculated and set by the
  1529. windowing system.
  1530. \end{notes}
  1531. \item[GT\_Polyline line] \hfill
  1532. {\footnotesize line, polyline}\\
  1533. This is a list of points which describes an object of type
  1534. \emph{line} or \emph{polygon}.
  1535. \item[GT\_Key fill] \hfill
  1536. {\footnotesize arc, line, polygon, oval, rectangle, text} \\
  1537. The color with which an object is filled.
  1538. \item[GT\_Key outline] \hfill
  1539. {\footnotesize arc,oval,rectangle} \\
  1540. The color with which the outline of an object is drawn.
  1541. \begin{notes}
  1542. \item Tk uses the \texttt{fill} attribute to specify the color of a
  1543. line.
  1544. \item Tk polygons do not have an outline.
  1545. \end{notes}
  1546. \item[GT\_Key stipple] \hfill
  1547. {\footnotesize arc, line, polygon, oval, rectangle, text} \\
  1548. The name of a Tk bitmap which is used as a stipple to draw the
  1549. object.
  1550. \item[GT\_Key anchor] \hfill
  1551. {\footnotesize bitmap, image, text} \\
  1552. The Tk anchor of an object.
  1553. \item[double width] \hfill
  1554. {\footnotesize arc, line, oval, rectangle, text} \\
  1555. If the object is a line, then \texttt{width} of that line.
  1556. Otherwise, \texttt{width} spoecifies the width of the \emph{outline}
  1557. of an object.
  1558. \item[double extent] \hfill
  1559. {\footnotesize arc} \\
  1560. \texttt{extent} is the length of the arc,
  1561. in \emph{degrees}. For example, 90 means a quarter circle, 180 means
  1562. a half circle, and 270 is a three quarter circle.
  1563. \item[double start] \hfill
  1564. {\footnotesize arc} \\
  1565. \texttt{starty} is the start of the
  1566. arc, in degrees. For example, \texttt{start} 0 and \texttt{extent}
  1567. 180 are the right half of a circle, and \texttt{start} 90 and
  1568. \texttt{extent} 180 are the lower half of a circle.
  1569. \item[GT\_Key style] \hfill
  1570. {\footnotesize arc} \\
  1571. Implements the style of an \emph{arc} item. Valid values are
  1572. \begin{itemize}
  1573. \item \GT{Keys::style\_pie}
  1574. \item \GT{Keys::style\_chord}
  1575. \item \GT{Keys::style\_slice}
  1576. \end{itemize}
  1577. \item[GT\_Key background] \hfill
  1578. {\footnotesize bitmap} \\
  1579. Sets the background color of a bitmap.
  1580. \item[GT\_Key foreground] \hfill
  1581. {\footnotesize bitmap} \\
  1582. Sets the foreground color of a bitmap.
  1583. \item[GT\_Key bitmap] \hfill
  1584. {\footnotesize bitmap} \\
  1585. The name of the Tk bitmap.
  1586. \begin{note}
  1587. Use Tcl/Tk's \texttt{bitmap} command to create a bitmap.
  1588. \end{note}
  1589. \item[GT\_Key image] \hfill
  1590. {\footnotesize image} \\
  1591. The name of the Tk image.
  1592. \begin{note}
  1593. Use Tcl/Tk's \texttt{image} command to create an image.
  1594. \end{note}
  1595. \item[GT\_Key arrow] \hfill
  1596. {\footnotesize line} \\
  1597. Determines whether a line has an arrow. Valid values are
  1598. \begin{itemize}
  1599. \item \GT{Keys::arrow\_none}
  1600. \item \GT{Keys::arrow\_first}
  1601. \item \GT{Keys::arrow\_last}
  1602. \item \GT{Keys::arrow\_both}
  1603. \end{itemize}
  1604. \begin{notes}
  1605. \item The arrow is usually managed by \Graphlet{}. The default is
  1606. to use \GT{Keys::arrow\_none} undirected graphs and
  1607. \GT{Keys::arrow\_last} in directed graphs.
  1608. \item Other values than the defaul are possible, but might produce to
  1609. strange and probably misleading drawings.
  1610. \item When a graph changes from directed to undirected or vice
  1611. versa, Graphlet resets the arrow.
  1612. \end{notes}
  1613. \item[GT\_Key arrowshape] \hfill
  1614. {\footnotesize line} \\
  1615. \emph{Currently not implemented.}
  1616. \item[GT\_Key capstyle] \hfill
  1617. {\footnotesize line} \\
  1618. Controls how the endpoints of lines are drawn. See the Tk
  1619. documentation for details. \emph{Valid values to be determined.}
  1620. \item[GT\_Key joinstyle] \hfill
  1621. {\footnotesize line} \\
  1622. Controls how the joints of lines are drawn. See the Tk
  1623. documentation for details. \emph{Valid values to be determined.}
  1624. \item[bool smooth] \hfill
  1625. {\footnotesize line, polygon} \\
  1626. Controls whether a line or polygon is drawn as a spline
  1627. (\texttt{true}) or as a straight line segments (\texttt{false}).
  1628. The default is \texttt{false}, which means that no splines are used.
  1629. \begin{note}
  1630. Most layout algorithms expect lines to be drawn as straight lines
  1631. and do not account for splines.
  1632. \end{note}
  1633. \item[int splinesteps] \hfill
  1634. {\footnotesize line, polygon} \\
  1635. Number of interpolation steps for splines. The dewfault is 0. See
  1636. the Tk documentation for details.
  1637. \item[GT\_Key justify] \hfill
  1638. {\footnotesize text} \\
  1639. Justification of text. \emph{Valid values to be determined.}
  1640. \item[GT\_Key font] \hfill
  1641. {\footnotesize text} \\
  1642. \emph{Currently not implemented, as the support for fonts is delayed
  1643. as Tk will implement a new, more portable font scheme in future
  1644. versions.}
  1645. \end{CAttributes}
  1646. \begin{notes}
  1647. \item The following colors are predefined:
  1648. \begin{itemize}
  1649. \item \GT{Keys::white}
  1650. \item \GT{Keys::black}
  1651. \item \GT{Keys::red}
  1652. \item \GT{Keys::green}
  1653. \item \GT{Keys::blue}
  1654. \end{itemize}
  1655. \item
  1656. A new color should have the format \verb|#RRGGBB|, where
  1657. \texttt{RR}, \texttt{GG} and \texttt{BB} are hexadecimal numbers
  1658. which specify the values for the red, green and blue values of a
  1659. color. To add a new color, use the following schema:
  1660. \begin{alltt}
  1661. GT_Key silly\_color = graphlet->keymapper.add ("#123456");
  1662. \end{alltt}
  1663. \item
  1664. The class \GT{Common\_Attributes} holds attributes for \emph{all}
  1665. types of graphical objects. But, only the attributes which apply
  1666. for the current \texttt{type} are used in the display. That is, a
  1667. node which is displayed as a rectangle may have \texttt{image} set
  1668. although this is not used. When the \texttt{type} is switched to
  1669. \texttt{image}, the information stored in \texttt{image} is used.
  1670. Nevertheless, the API allows that all attributes may be changed or
  1671. retrieved at any time, regardless of type.
  1672. \item
  1673. Graphlet stores all \emph{initialized} graphics and label graphics
  1674. attribute in GML files. They are stored under the keys
  1675. \texttt{graphics} and \texttt{LabelGraphics}.
  1676. \end{notes}
  1677. %
  1678. % Shortcuts for coordinates and size
  1679. %
  1680. \subsection{Shortcuts for coordinates and size}
  1681. The class \GT{Common\_Graphics} provides the
  1682. following shortcuts for coordinates and size of an object:
  1683. \begin{Cdefinition}
  1684. \item[double x () const] \strut\\
  1685. Shortcut for accessing the \texttt{x} coordinate of the center of
  1686. the object.
  1687. \item[void x (const double x)] \strut\\
  1688. Shortcut for accessing the \texttt{x} coordinate of the center of
  1689. the object.
  1690. \item[double y () const] \strut\\
  1691. Shortcut for accessing the \texttt{y} coordinate of the center of
  1692. the object.
  1693. \item[void y (const double y)] \strut\\
  1694. Shortcut for accessing the \texttt{y} coordinate of the center of
  1695. the object.
  1696. \item[double w () const] \strut\\
  1697. Shortcut for accessing the width of the object.
  1698. \item[void w (const double w)] \strut\\
  1699. Shortcut for accessing the width of the object.
  1700. \item[double h () const] \strut\\
  1701. Shortcut for accessing the height of the object.
  1702. \item[void h (const double h)] \strut\\
  1703. Shortcut for accessing the height of the object.
  1704. \end{Cdefinition}
  1705. Example \ref{e:C++:MaximumWidthOfAllNodes} shows how to use these
  1706. shortcuts.
  1707. \begin{example}%
  1708. {e:C++:MaximumWidthOfAllNodes}%
  1709. {Computing the maximum width of all nodes in a graph}
  1710. \begin{verbatim}
  1711. double Sample_class::compute_maximum_width (const GT_Graph& g)
  1712. {
  1713. double max = 0; // Minimum legal value for a width
  1714. node n;
  1715. forall_nodes (n, g.leda()) {
  1716. if (g.gt(n).graphics() != 0) {
  1717. double w = g.gt(n).graphics()->w();
  1718. if (max < w) {
  1719. w = max;
  1720. }
  1721. }
  1722. }
  1723. return max;
  1724. }
  1725. \end{verbatim}
  1726. \end{example}
  1727. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1728. %
  1729. % GT_Algorithm
  1730. %
  1731. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1732. \chapter{Implementing Graph Algorithms}
  1733. \label{c:Algorithms}
  1734. \CSourceCode{}{base}{Algorithm}
  1735. The class \GT{Algorithm} provides a base class and a standard
  1736. interface for the implementation of graph algorithms. The key
  1737. idea is that each algorithm is a class, and is invoked with the
  1738. \texttt{run} method. Parameters and results are stored as class
  1739. member variables (instead of parameters and/or function results).
  1740. While this is obviously not the only sane way to implement
  1741. algorithms, its major advantage is that all of Graphlets
  1742. interfaces provide special support for algorithms which are
  1743. implemented with this class. Especially, the Tcl interface for
  1744. algorithms (see Chapter \ref{c:TclInterface}) is designed to
  1745. take an algorithm which has been implemented with \GT{Algorithm}
  1746. and construct a Tcl interface for it on the fly -- all the
  1747. programmer has to supply is code for parsing parameters and error
  1748. handling.
  1749. Another advantage of the above concept is that since algoithms
  1750. are objects, it is easy to have several algorithms with
  1751. \emph{different parameter sets} around. Actually, the value of a
  1752. parameter stays the same until it is reset. Also, algorithms can
  1753. be passed anonymously as \emph{parameters}, a construct which is
  1754. not easy to realize otherwise in C++.
  1755. %
  1756. % The class GT_algorithm
  1757. %
  1758. \section{The class \GT{Algorithm}}
  1759. All algorithms in \Graphlet{} should be derived from the class
  1760. \GT{Algorithm}. Example \ref{e:C++:SampleAlgorithmClass} shows a
  1761. minimal example for an algorithm class implemented with
  1762. \GT{Algorithm}.
  1763. \begin{example}%
  1764. {e:C++:SampleAlgorithmClass}%
  1765. {A Sample Algorithm Class Declaration}
  1766. \begin{verbatim}
  1767. class Sample : public GT_Algorithm {
  1768. public:
  1769. Sample (const string& name);
  1770. virtual ~Sample ();
  1771. virtual int run (GT_Graph& g);
  1772. virtual int check (GT_Graph& g, string& message);
  1773. }
  1774. \end{verbatim}
  1775. \end{example}
  1776. %
  1777. % Methods which must be provided
  1778. %
  1779. \subsection{Methods which must be provided by the derived class}
  1780. \begin{Cdefinition}
  1781. \item[Sample (const string\& \Param{name})] \strut\\
  1782. This is the constructor of the class \texttt{Sample}. Its parameter
  1783. should be the name of the command. \GT{Algorithm}'s constructor
  1784. takes a single argument, which is the \emph{name} of the algorithm,
  1785. and should be defined as follows:
  1786. \begin{alltt}
  1787. Sample::Sample(const string& name) : GT_Algorithm (name) \{
  1788. \textnormal{\emph{Initialize the algorithm's parameters here}}
  1789. \}
  1790. \end{alltt}
  1791. Usually, the name of the algorithm will later be used as the
  1792. name of the associated Tcl command.
  1793. \item[virtual \~ Sample()] \strut\\
  1794. The class \texttt{Sample} must contain a virtual destructor since
  1795. \GT{Algorithm} already has virtual functions.
  1796. \item[virtual int run (GT\_Graph\& \Param{g})] \strut\\
  1797. The method \texttt{run} executes the algorithm. Its parameter is
  1798. the input graph. This procedure should return \GT{OK} if the
  1799. command could be completed successfully, and \GT{ERROR} otherwise.
  1800. \item[virtual int check(GT\_Graph\& \Param{g}, string\& \Param{message})]
  1801. \strut\\
  1802. The method \texttt{check} should be called before \texttt{run} to
  1803. check whether the current graph \emph{g} is applicable for the
  1804. algorithm. This method should return \GT{OK} if the graph is
  1805. applicable, and \GT{ERROR} otherwise. The parameter \emph{message}
  1806. should contain a detailed error message if the check fails.
  1807. \GT{Algorithm} does not automatically call \texttt{check}
  1808. before \texttt{run}. The Tcl interface for \GT{Algorithm} (see
  1809. Section \ref{s:GT_Tcl_Interface}) enforces this. The reason is
  1810. performance, and the fact that Tcl allows for a better error
  1811. checking with its \texttt{catch} statements.
  1812. \end{Cdefinition}
  1813. %
  1814. % Other methods
  1815. %
  1816. \subsection{Other methods which may be overridden by the derived class}
  1817. \begin{Cdefinition}
  1818. \item[virtual void reset ()] \strut\\
  1819. The method \texttt{reset} should reset all options and
  1820. parameters of the algorithm.
  1821. Similarily to \texttt{check}, \texttt{reset} is never called
  1822. automatically. However, the Tcl interface has a configurable
  1823. option to to do this each time the command is executed (see
  1824. Section \ref{s:GT_Tcl_Interface} for details).
  1825. \end{Cdefinition}
  1826. %
  1827. % Methods provided
  1828. %
  1829. \section{Methods which are provided by the class \GT{Algorithm}}
  1830. \begin{Cdefinition}
  1831. \item[static edge find\_self\_loop (GT\_Graph\& \Param{g})] \strut\\
  1832. \texttt{find\_self\_loop} searches for a self look in
  1833. \texttt{g}. It returns \texttt{nil} if no self loop is found.
  1834. \item[static bool remove\_all\_bends (GT\_Graph\& \Param{g})] \strut\\
  1835. \texttt{remove\_all\_bends} removes all bends in the edges of \emph{g}.
  1836. It returns \texttt{true} if bends are found, and \texttt{false}
  1837. otherwise.
  1838. \item[void adjust\_coordinates (GT\_Graph\& \Param{g},
  1839. double \Param{min\_x} = 0,
  1840. double \Param{min\_y} = 0)]
  1841. \strut\\
  1842. \texttt{adjust\_coordinates} moves the graph so that the minimum
  1843. coordinates are at (min\_x,min\_y).
  1844. \end{Cdefinition}
  1845. %
  1846. % Example
  1847. %
  1848. \section{A complete Example}
  1849. \label{s:Algorithm:Example}
  1850. \subsection{Header}
  1851. \label{s:Algorithm:Example:Header}
  1852. The following C++ header snippet shows how to implement a
  1853. planarity test algorithm\footnote{Well, rather the interface for
  1854. a planarity test algorithm. The real algorithm is said to be
  1855. easy and therefore left as an exercise to the reader.}. We use
  1856. the following conventions for the interface:
  1857. \begin{itemize}
  1858. \item The result of the algorithm is stored in a variable
  1859. \texttt{is\_planar}.
  1860. \item If an error is detected in \texttt{check} (in this case,
  1861. a self loop), then the offending object is stored in a
  1862. variable. This is always a good idea because it might help the
  1863. user interface to provide a cleaner error message.
  1864. \item Options realized as member variables.
  1865. \end{itemize}
  1866. \begin{verbatim}
  1867. class GT_Planarity_Test_Algorithm : public GT_Algorithm {
  1868. GT_CLASS (GT_Planarity_Test_Algorithm, GT_Algorithm);
  1869. // The result is stored in this variable.
  1870. GT_VARIABLE (bool, is_planar);
  1871. // Precondition for LEDA's planarity test is the absence of
  1872. // self loops. If we find a self loop, return it in this
  1873. // variable.
  1874. GT_VARIABLE (edge, self_loop);
  1875. // Options: find kuratowski subgraph
  1876. GT_VARIABLE (bool, find_kuratowski_subgraph);
  1877. // The edges of the kuratowski subgraph are stored here.
  1878. GT_COMPLEX_VARIABLE (list<edge>, kuratowski_edges);
  1879. public:
  1880. // Constructor and Destructor
  1881. GT_Planarity_Test_Algorithm (const string& name);
  1882. virtual ~GT_Planarity_Test_Algorithm ();
  1883. // Standard methods
  1884. virtual void reset ();
  1885. virtual int run (GT_Graph& g);
  1886. virtual int check (GT_Graph& g, string& message);
  1887. };
  1888. \end{verbatim}
  1889. \subsection{Implementation}
  1890. \label{s:Algorithm:Example:Implementation}
  1891. The following code implements constructor and destructor for the
  1892. planarity test algorithm. Note the conservative treatment of
  1893. options: searching for a kuratowski graph is turned off by
  1894. default. The constructor is empty in many algorithm classes;
  1895. \begin{verbatim}
  1896. GT_Planarity_Test_Algorithm::GT_Planarity_Test_Algorithm (const string& name) :
  1897. GT_Algorithm (name)
  1898. {
  1899. the_is_planar = false;
  1900. the_self_loop = 0;
  1901. the_find_kuratowski_subgraph = false;
  1902. }
  1903. GT_Planarity_Test_Algorithm::~GT_Planarity_Test_Algorithm ()
  1904. {
  1905. // Nothing left to do.
  1906. }
  1907. \end{verbatim}
  1908. \noindent The method \texttt{run} is implemented in a straightforward way.
  1909. Depending on the \emph{kuratowski\_edges} option, it calls LEDA's
  1910. \texttt{PLANAR} algorithm with a different parameter set. Note
  1911. again that the result is stored in the member variable
  1912. \texttt{is\_planar}. The method always returns \texttt{GT\_OK},
  1913. as nothing can go wrong with a planarity test (provided that
  1914. \texttt{check} was successful).
  1915. \begin{verbatim}
  1916. int GT_Planarity_Test_Algorithm::run (GT_Graph& g)
  1917. {
  1918. graph& leda = g.leda();
  1919. if (the_find_kuratowski_subgraph) {
  1920. is_planar (PLANAR(leda, the_kuratowski_edges));
  1921. } else {
  1922. the_kuratowski_edges.clear();
  1923. is_planar (PLANAR(leda));
  1924. }
  1925. return GT_OK;
  1926. }
  1927. \end{verbatim}
  1928. \noindent The method \texttt{check} searches for self loops in the graph.
  1929. If it finds one, it (a) deposits it in the member variable
  1930. \texttt{self\_loop}, (b) sets \texttt{message} to a description
  1931. of the error and (c) returns \GT{ERROR}.
  1932. \begin{verbatim}
  1933. int GT_Planarity_Test_Algorithm::check (GT_Graph& g, string& message)
  1934. {
  1935. edge e = GT_Algorithm::find_self_loop(g);
  1936. if (e != nil) {
  1937. message = "graph contains self loop";
  1938. self_loop (e);
  1939. return GT_ERROR;
  1940. } else {
  1941. self_loop (0);
  1942. message = "";
  1943. return GT_OK;
  1944. }
  1945. }
  1946. \end{verbatim}
  1947. \noindent The method \texttt{reset} is optional and resets all options and
  1948. member variables:
  1949. \begin{verbatim}
  1950. void GT_Planarity_Test_Algorithm::reset ()
  1951. {
  1952. the_is_planar = false;
  1953. the_self_loop = 0;
  1954. the_find_kuratowski_subgraph = false;
  1955. the_kuratowski_edges.clear();
  1956. }
  1957. \end{verbatim}
  1958. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1959. %
  1960. % The Tcl interface
  1961. %
  1962. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1963. \chapter{The Tcl interface}
  1964. \label{c:TclInterface}
  1965. %
  1966. % The class GT_Tcl
  1967. %
  1968. \section{The class \GT{Tcl}}
  1969. \label{s:GT_Tcl}
  1970. \CSourceCode{}{tcl}{Tcl}
  1971. The class \GT{Tcl} provides several utilities to ease the Tcl
  1972. - \Graphlet{} interface.
  1973. % C++ -> Tcl converters
  1974. \subsection{Tools for converting C++ objects into Tcl structures}
  1975. \begin{Cdefinition}
  1976. \item[static string list\_of\_strings (const list$<$string$>$\& \Param{s})]
  1977. Constructs a Tcl list of the strings in \emph{s}.
  1978. \item[static void list\_of\_strings (const list$<$string$>$\& \Param{s},
  1979. string\& \Param{result})]
  1980. Fills \emph{result} with a Tcl list of the strings in \emph{s}.
  1981. \end{Cdefinition}
  1982. % GT --> Tcl converters
  1983. \subsection{Tools for converting GT objects into Tcl objects}
  1984. GraphScript does not directly represent graphs, nodes and edges
  1985. as Tcl objects; instead, \emph{identifiers} are introduced for
  1986. them which are then used to address graphs, nodes and edges from
  1987. GraphScript. generally, the form of such an identifier is
  1988. \begin{quote}
  1989. \texttt{GT:}\emph{uid} resp.\ \texttt{GT:}\emph{label\_uid}
  1990. \end{quote}
  1991. where \emph{uid} resp.\ \emph{label\_uid} are the values
  1992. \emph{user interface ids} as described in Section
  1993. \ref{s:Attributes:Common}. The following functions can be
  1994. used to convert nodes and edges to their Graphlet identifiers:
  1995. \begin{Cdefinition}
  1996. \item[static string gt (const \GT{Graph}\& \Param{g})]
  1997. \strut\\
  1998. Returns the \GraphScript{} identifier of graph \emph{g}.
  1999. \item[static string gt (const \GT{Graph}\& \Param{g}, const node \Param{n})]
  2000. \strut\\
  2001. Returns the \GraphScript{} identifier of node \emph{n}
  2002. in graph \emph{g}.
  2003. \item[static string gt (const \GT{Graph}\& \Param{g}, const edge \Param{e})]
  2004. \strut\\
  2005. Returns the \GraphScript{} identifier of edge \emph{e}
  2006. in graph \emph{g}.
  2007. \item[static string list\_of\_nodes (const \GT{Graph}\& \Param{g},
  2008. const list<node>\& \Param{nodes});]
  2009. \strut\\
  2010. Constructs a Tcl list of the nodes in \emph{nodes} and returns it.
  2011. \item[static void list\_of\_nodes (const \GT{Graph}\& \Param{g},
  2012. const list<node>\& \Param{nodes},
  2013. string\& \Param{result});]
  2014. \strut\\
  2015. Constructs a Tcl list of the nodes in \texttt{nodes} and
  2016. returns it in \emph{result}.
  2017. \item[static string list\_of\_edges (const \GT{Graph}\& \Param{g},
  2018. const list<edge>\& \Param{edges});]
  2019. \strut\\
  2020. Constructs a Tcl list of the edges in \emph{edges} and returns it.
  2021. \item[static void list\_of\_edges (const \GT{Graph}\& \Param{g},
  2022. const list<edge>\& \Param{edges},
  2023. string\& \Param{result});]
  2024. \strut\\
  2025. Constructs a Tcl list of the edges in \texttt{edges} and
  2026. returns it in \emph{result}.
  2027. \end{Cdefinition}
  2028. %
  2029. % Wrappers for Tcl functions
  2030. %
  2031. \subsection{Wrappers for common Tcl functions}
  2032. The following functions convert C strings to integer, double or
  2033. boolean values:
  2034. \begin{Cdefinition}
  2035. \item[static int get\_int (GT\_Tcl\_info\& \Param{info}, const
  2036. char* \Param{s}, int\& \Param{result})]
  2037. \strut\\
  2038. Wrapper for the Tcl function \texttt{Tcl\_GetInt}.
  2039. \texttt{GT\_Tcl::get\_int} converts \emph{s} into
  2040. \emph{result}. It returns the return value of
  2041. \texttt{Tcl\_GetInt}.
  2042. \item[static int get\_double (GT\_Tcl\_info\& \Param{info}, const
  2043. char* \Param{s}, double\& \Param{result})]
  2044. \strut\\
  2045. Wrapper for the Tcl function \texttt{Tcl\_GetDouble}.
  2046. \texttt{GT\_Tcl::get\_double} converts \emph{s} into
  2047. \emph{result}. It returns the return value of
  2048. \texttt{Tcl\_GetDouble}.
  2049. \item[static int get\_boolean (GT\_Tcl\_info\& \Param{info}, const
  2050. char* \Param{s}, bool\& \Param{result})]
  2051. \strut\\
  2052. Wrapper for the Tcl function \texttt{Tcl\_GetBoolean}.
  2053. \texttt{GT\_Tcl::get\_boolean} converts \emph{s} into
  2054. \emph{result}. It returns the return value of
  2055. \texttt{Tcl\_GetBoolean}.
  2056. \item[static int get\_boolean (GT\_Tcl\_info\& \Param{info}, const
  2057. char* \Param{s}, int\& \Param{result})]
  2058. \strut\\
  2059. Wrapper for the Tcl function \texttt{Tcl\_GetBoolean}.
  2060. \texttt{GT\_Tcl::get\_boolean} converts \emph{s} into
  2061. \emph{result}. It returns the return value of
  2062. \texttt{Tcl\_GetBoolean}.
  2063. \end{Cdefinition}
  2064. Example \ref{e:get_double} shows how to use these functions. It
  2065. is important to check the returned value; if it is \textbf{not}
  2066. \texttt{TCL\_OK}, then \emph{result} does not contain a valid
  2067. value.
  2068. \begin{example}{e:get_double}%
  2069. {Convert a string into an \texttt{double} value using \GT{Tcl::get\_double}}
  2070. \begin{verbatim}
  2071. int parse (GT_Tcl_info& info, int& index, GT_Tcl_Graph* g)
  2072. {
  2073. double result;
  2074. int code = GT_Tcl::get_double (info, info[index], result);
  2075. if (code != TCL_OK) {
  2076. return code;
  2077. }
  2078. // Not do something with result
  2079. }
  2080. \end{verbatim}
  2081. \end{example}
  2082. The following wrappers provide an easy way to use the Tcl
  2083. function \emph{Tcl\_SplitList} from C++.
  2084. \begin{Cdefinition}
  2085. \item[static int split\_list (Tcl\_Interp* \Param{interp},
  2086. const char* \Param{name}, list<string>\& \Param{splitted})]
  2087. \strut\\
  2088. \item[static int split\_list (Tcl\_Interp* \Param{interp}, const
  2089. char* \Param{name}, int\& \Param{argc}, char**\& \Param{argv})]
  2090. \strut\\
  2091. \end{Cdefinition}
  2092. %
  2093. %
  2094. %
  2095. \section{The class \GTTcl{info}}
  2096. \label{s:Tcl_info}
  2097. \CSourceCode{}{tcl}{Tcl\_Info}
  2098. The standard parameter set for a tcl command are client data, Tcl
  2099. interpreter, number of arguments and array aof arguments as in:
  2100. \begin{alltt}
  2101. int sample_cmd (ClientData client_data,
  2102. Tcl_Interp* interp,
  2103. int argc,
  2104. char* argv[])
  2105. \end{alltt}
  2106. GraphScript uses a different approach. First, it uses the
  2107. \verb|client_data| parameters for its own purposes. Technically,
  2108. \verb|client_data| is a pointer to some information which is
  2109. stored with the Tcl command. In a C++ interface, this is
  2110. typically used to store a pointer to the C++ object which is
  2111. associated with the command.
  2112. Second, it provides only a single parameter of type
  2113. \GTTcl{info} which contains all the above parameters (except
  2114. \texttt{client\_data}) plus a bunch of helper functions.
  2115. \begin{Cdeclaration}{Tclinfo}{class \GTTcl{info}}
  2116. \begin{verbatim}
  2117. class GT_Tcl_info
  2118. {
  2119. GT_BASE_CLASS (GT_Tcl_info);
  2120. GT_VARIABLE (Tcl_Interp*, interp);
  2121. GT_VARIABLE (int, argc);
  2122. GT_VARIABLE (char**, argv);
  2123. GT_COMPLEX_VARIABLE (string, msg);
  2124. public:
  2125. GT_Tcl_info();
  2126. GT_Tcl_info (Tcl_Interp* interp, int argc, char** argv);
  2127. virtual ~GT_Tcl_info();
  2128. inline const char* operator[] (const int i) const;
  2129. inline char* operator[] (const int i);
  2130. const GT_Key operator() (const int i) const;
  2131. GT_Key operator() (const int i);
  2132. bool is_last_arg (const int index) const;
  2133. bool exists (const int index) const;
  2134. bool args_left (const int index, const int n, bool exact = true) const;
  2135. int args_left (const int index) const;
  2136. bool args_left_at_least (const int index, const int n) const;
  2137. bool args_left_exactly (const int index, const int n) const;
  2138. string& msg(); // non-const access to msg
  2139. void msg (const int error);
  2140. void msg (const int error, const int i);
  2141. void msg (const int error, const string& s);
  2142. }
  2143. \end{verbatim}
  2144. \end{Cdeclaration}
  2145. Figure \ref{Cdeclaration:Tcl_info} shows the class \texttt{Tcl\_info}.
  2146. \begin{Cdefinition}
  2147. \item[const char* operator[] (const int \Param{i}) const;]
  2148. \strut\\
  2149. Access argument \emph{i} as a \texttt{char*}. There is also a
  2150. non-const version available. As an example,
  2151. \begin{alltt}
  2152. void sample (GT_Tcl_info& info)
  2153. \{
  2154. for (i=0; i < info.argc(), i++) \{
  2155. const char* a = info[i];
  2156. if (GT::streq (a, "-option") \{
  2157. // Now do something with a
  2158. \}
  2159. \}
  2160. \}
  2161. \end{alltt}
  2162. \item[const char* operator() (const int \Param{i}) const;]
  2163. \strut\\
  2164. Access argument \emph{i} as a \GT{Key} object. There is also a non-const
  2165. version available.
  2166. \begin{notes}
  2167. \item This will create a new object in the global keymapper
  2168. table (see Section \ref{s:Keymapper}).
  2169. \item This access method should \emph{not} be used for
  2170. arbitrary text objects, because this would fill the keymapper
  2171. with lost of one-time-used entries. One reasonable use are
  2172. options like \texttt{-something}.
  2173. \end{notes}
  2174. \item[bool is\_last\_arg (const int \Param{index})]
  2175. \strut\\
  2176. Checks wether the argument \emph{index} is the last argument.
  2177. \item[bool exists (const int \Param{index})]
  2178. \strut\\
  2179. Checks wether there exists an argument at \emph{index}.
  2180. \item[bool args\_left (const int \Param{index}, const int
  2181. \Param{n}, bool \Param{exact})]
  2182. \strut\\
  2183. Checks wether there are at least (\emph{exact} ==
  2184. \texttt{false}) or exactly (\emph{exact} == \texttt{true})
  2185. \emph{n} arguments left. The following three methods provide a
  2186. more comfortable access to this information.
  2187. \item[int args\_left (const int \Param{index})]
  2188. \strut\\
  2189. Returns how many arguments are left after \emph{index}.
  2190. \item[bool args\_left\_at\_least (const int \Param{index},
  2191. const int \Param{n}) const]
  2192. \strut\\
  2193. Checks wether there are at least \emph{n} arguments left after
  2194. \emph{index}.
  2195. \item[bool args\_left\_exactly (const int \Param{index},
  2196. const int \Param{n}) const]
  2197. \strut\\
  2198. Checks wether there are at least \emph{n} arguments left after
  2199. \emph{index}.
  2200. \item[msg()]
  2201. \strut\\
  2202. Set the return message of the command, as in the following:
  2203. \begin{verbatim}
  2204. info.msg() = "This is a pity excuse for a better error message.";
  2205. \end{verbatim}
  2206. \item[void msg (const int \Param{error})]
  2207. \strut\\
  2208. Output Graphlet error message \emph{error}. See also Section
  2209. \ref{s:Error}.
  2210. \item[void msg (const int \Param{error}, const int
  2211. \Param{i})]
  2212. \strut\\
  2213. Output Graphlet error message \emph{error} with parameter
  2214. \emph{i}. See also Section \ref{s:Error}.
  2215. \item[void msg (const int \Param{error}, const string\&
  2216. \Param{i})]
  2217. \strut\\
  2218. Output Graphlet error message \emph{error} with parameter
  2219. \emph{s}. See also Section \ref{s:Error}.
  2220. \end{Cdefinition}
  2221. %
  2222. % The Tcl Interface of a Graph Algorithm
  2223. %
  2224. \section{The class \GTTcl{Algorithm}}
  2225. \label{s:Tcl_Algorithm}
  2226. \GTTcl{Algorithm\_Command} derives from
  2227. \GTTcl{Command} and implements a command which works on
  2228. graphs. Consequently, the first parameter of the command is
  2229. always a graph. \GTTcl{Algorithm$<$$>$} is a template
  2230. class which constructs a Tcl command from an algorithm. The Tcl
  2231. interface for an algorithm is always derived from (an instance
  2232. of) this class.
  2233. %
  2234. % The Tcl Interface of a Graph Algorithm
  2235. %
  2236. \subsection{The Tcl Interface of a Graph Algorithm}
  2237. \label{s:GT_Tcl_Interface}
  2238. A Tcl interface for a \Graphlet{} algorithm is constructed with
  2239. the template class \GTTcl{Algorithm}, as shown in Example
  2240. \ref{e:C++:SampleTclAlgorithmClass}. The class
  2241. \GTTcl{Algorithm$<$\Param{a}$>$} constructs a generic Tcl
  2242. interface for a class \emph{a} which is derived from
  2243. \GT{Algorithm}.
  2244. \begin{example}%
  2245. {e:C++:SampleTclAlgorithmClass}%
  2246. {A Sample Tcl Algorithm Interface Class Declaration}
  2247. \begin{verbatim}
  2248. class Tcl_Sample : public GT_Tcl_Algorithm<Sample>
  2249. {
  2250. public:
  2251. Tcl_Sample (const string& name);
  2252. virtual ~Tcl_Sample ();
  2253. virtual int parse (GT_Tcl_info& info, int& index, GT_Tcl_Graph* g);
  2254. };
  2255. \end{verbatim}
  2256. \end{example}
  2257. Note that the so constructed class \texttt{Tcl\_Sample} derives
  2258. both from \texttt{Sample} and \GTTcl{Algorithm\_Command}.
  2259. \subsection{Required Methods}
  2260. The following methods are required for a class \texttt{Tcl\_Sample}
  2261. which is derived from \GTTcl{Algorithm$<>$}.
  2262. \begin{Cdefinition}
  2263. \item[Tcl\_Sample (const string\& name)] This is the
  2264. constructor of the class \texttt{Tcl\_Sample}. Its parameter
  2265. is the name of the Tcl command, which is usually also the name
  2266. of the algorithm.
  2267. \GTTcl{Algorithm$<$$>$}'s constructor takes a single argument
  2268. which is the name of the Tcl Command, so the constructor should
  2269. be declared as
  2270. \begin{alltt}
  2271. Tcl_Sample::Tcl_Sample (const string& \Param{name}) :
  2272. GT_Tcl_Algorithm<Sample> (\Param{name})
  2273. \end{alltt}
  2274. \item[virtual ~Tcl\_Sample()] This is the destructor of the
  2275. class \texttt{Tcl\_Sample}. It must be declared virtual since
  2276. \GTTcl{Algorithm$<>$} has virtual functions.
  2277. \end{Cdefinition}
  2278. \subsection{Optional Methods}
  2279. \begin{Cdefinition}
  2280. \item[virtual int parse (GT\_Tcl\_info\& \Param{info},
  2281. int\& \Param{index},
  2282. GT\_Tcl\_Graph* \Param{g})]
  2283. \texttt{parse} is called to parse the argument at \texttt{index}.
  2284. \end{Cdefinition}
  2285. Also, the methods \texttt{run} and \texttt{check}
  2286. from the algorithm class may be overwritten. This is necessary
  2287. to convert the results to Tcl format.
  2288. \TBD{}
  2289. %
  2290. % Returning Results
  2291. %
  2292. \subsection{Returning a result}
  2293. The class \GTTcl{Algorithm} provides a member variable
  2294. \texttt{result} of type \texttt{string} which holds the Tcl
  2295. result of the algorithm.
  2296. %
  2297. % Returning an error code
  2298. %
  2299. \subsection{Returning an error code}
  2300. To transform the result of an algorithm's \texttt{check} or
  2301. \texttt{run} methods into Tcl, proceed as follows:
  2302. \begin{enumerate}
  2303. \item Implement \texttt{check} and/or \texttt{run} methods in the
  2304. class derived from \GTTcl{Algorithm$<$Algorithm$>$}.
  2305. \item These methods execute \texttt{Algorithm::check} respectively.
  2306. \texttt{Algorithm::check}.
  2307. \item Then convert the output of \texttt{check} or \texttt{run} into
  2308. a Tcl string and put that into result.
  2309. \end{enumerate}
  2310. %
  2311. % Error Handling
  2312. %
  2313. \subsection{Error Handling}
  2314. The default behavior for the Tcl interface is to return a
  2315. Tcl error, which will cause a runtime error unless
  2316. \texttt{catch}ed if the check fails. The following piece of Tcl
  2317. can be used to prevent a runtime message:
  2318. \begin{verbatim}
  2319. if [catch { my_algorithm $GT($top,graph) } error_message] {
  2320. tk_dialog .my_errormsg "Error Message" \
  2321. $error_message error 0 "Ok"
  2322. }
  2323. \end{verbatim}%$
  2324. \noindent An even better way to solve the above problem is to implement a
  2325. dedicated GraphScript command for the \emph{check} phase. This
  2326. will also allow for a better user interface for error handling,
  2327. e.g. to select the ``bad'' nodes and edges.
  2328. %
  2329. % Installing \GraphScript{} Commands
  2330. %
  2331. \section{Installing \GraphScript{} Commands}
  2332. Installation and initialization of a \GraphScript{} command takes three steps:
  2333. \begin{enumerate}
  2334. \item Create the C++ object.
  2335. \item Install it into the Tcl interpreter.
  2336. \item \texttt{Important:}
  2337. Check the Tcl return code for errors.
  2338. \end{enumerate}
  2339. \GTTcl{Command}'s
  2340. \texttt{install} method is used to install a command into a Tcl
  2341. interpreter:
  2342. \begin{verbatim}
  2343. // (1) Create the C++ object
  2344. GT_Tcl_Algorithm_Command* sample =
  2345. new GT_Tcl_Sample ("sample");
  2346. // (2) Install the object in the interpreter
  2347. code = sample-$>$install (interp);
  2348. // (3) IMPORTANT: check return code.
  2349. if (code == TCL_ERROR) {
  2350. return code;
  2351. }
  2352. \end{verbatim}
  2353. This installation should be placed in the startup file of your
  2354. \GraphScript{} interpreter or in the
  2355. initialization of your module.
  2356. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2357. %
  2358. % Makefiles
  2359. %
  2360. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2361. \chapter{Makefiles}
  2362. \label{c:Makefiles}
  2363. This section describes how \Graphlet{}'s configuration system can be
  2364. used to add makefiles for modules. The \emph{source code
  2365. distribution} must be installed to take advantage of these features.
  2366. \begin{skills}
  2367. This section requires basic knowledge of how \emph{make} programs
  2368. work.
  2369. \end{skills}
  2370. %
  2371. % Overview of the configuration system
  2372. %
  2373. \section{Overview of the configuration system}
  2374. \Graphlet{} uses \texttt{GNU make} for its configuration. This
  2375. version of make has some features which are not normally present in
  2376. make programs, such as extended macro processing and
  2377. \emph{if\ldots{}then\ldots{}else} structures. We chose GNU make over
  2378. preprocessor based systems because the latter one would be harder to
  2379. debug.
  2380. One side effect of using GNU make is that we are using the name
  2381. \texttt{GNUmakefile} instead of \texttt{Makefile} or
  2382. \texttt{makefile}. This has the advantage that any other
  2383. \texttt{make} program will not even try to source the file and report
  2384. syntax errors, but will report that it could not find a \emph{make
  2385. file} and exit. We feel this is a cleaner approach and
  2386. gives better error messages. Also, our coding standards require to
  2387. use the name \texttt{GNUmakefile} for makefiles.
  2388. All of \Graphlet{}'s configuration files are located in
  2389. \texttt{lib/graphlet/config}. The default installation procedure
  2390. copies the configuration system to
  2391. \emph{\Graphlet{} Installation Directory}\texttt{/lib/graphlet/config}.
  2392. The configuration system does not only determine the proper compiler
  2393. flags and procedures for installing \Graphlet{} on a particular
  2394. system, but also provides a large set of predefined make variables and
  2395. targets.
  2396. To get access to the predefined variables and procedures, every
  2397. \texttt{GNUmakefile} \emph{must} include the file
  2398. \texttt{lib/graphlet/config/common}. Since the information in
  2399. \texttt{common} relies on information about the site installation, it
  2400. can only be used with a source code distribution.
  2401. %
  2402. % Anatomy of a \texttt{GNUmakefile}
  2403. %
  2404. \section{Anatomy of a \texttt{GNUmakefile}}
  2405. An example for a \texttt{GNUmakefile} can be found in the algorithms
  2406. module. A \texttt{GNUmakefile} must at least contain the following:
  2407. \begin{enumerate}
  2408. \item Define of the variable \emph{MODULE}:
  2409. \begin{quote}
  2410. \texttt{MODULE = }\emph{insert the name of the module here}
  2411. \end{quote}
  2412. \emph{MODULE} is the name of the module in which the
  2413. \texttt{GNUmakefile} resides.
  2414. \item Define the variable \emph{GRAPHLET\_BASE\_DIR}:
  2415. \begin{quote}
  2416. \texttt{GRAPHLET\_BASE\_DIR=}\emph{insert the relative path to the
  2417. toplevel of \Graphlet{}}
  2418. \end{quote}
  2419. \emph{GRAPHLET\_BASE\_DIR} must be the \texttt{relative} path to the
  2420. toplevel directory of \Graphlet{}. For example, if your module is in
  2421. \texttt{src/}\emph{mymodule}, then set
  2422. \begin{quote}
  2423. \texttt{GRAPHLET\_BASE\_DIR=../..}
  2424. \end{quote}
  2425. \item
  2426. Define a standard target \texttt{all} which is executed when
  2427. no arguments are given to make (which is usually the case):
  2428. \begin{quote}
  2429. \texttt{all: lib\$(MODULE).a \$(SUBDIRS)}
  2430. \end{quote}
  2431. This defines that the default actions are building the
  2432. object libraries, and recursively traversing the subdirectories.
  2433. For a module which has no default actions and no
  2434. subdirectories, use
  2435. \begin{alltt}
  2436. .PHONY: all
  2437. all:
  2438. \emph{Insert the actions for target \texttt{all} here}
  2439. \end{alltt}
  2440. \noindent The directive \texttt{.PHONY:} states that the following target(s)
  2441. have no dependencies.
  2442. \item Include the common configuration definitions:
  2443. \begin{quote}
  2444. \texttt{include\$(\Graphlet{}\_BASE\_DIR)/lib/\Graphlet{}/config/common}
  2445. \end{quote}
  2446. \end{enumerate}
  2447. %
  2448. % Standard Variables
  2449. %
  2450. \section{Standard Variables}
  2451. \begin{ttdescription}
  2452. \item[SUBDIRS]
  2453. The variable \texttt{SUBDIRS} holds a list of subdirectories.
  2454. Most targets will recursively descend into subdirectories.
  2455. \item[TCLFILES]
  2456. \texttt{TCLFILES} is a list of Tcl files. This target
  2457. \texttt{index} constructs a \texttt{tclIndex}
  2458. file (for autoloading)
  2459. \item[MYCFILES]
  2460. \texttt{MYCFILES} is the list of C/C++ files which
  2461. are not generated by a program (e.g. yacc or lex).
  2462. \item[CFILES]
  2463. \texttt{CFILES} is the list of all C/C++ files,
  2464. including those which generated by a program (e.g. yacc or lex).
  2465. \item[HFILES]
  2466. \texttt{HFILES} is the list of C/C++ Header files.
  2467. HFILES can be generated from MYCFILES as follows:
  2468. \begin{quote}
  2469. \texttt{HFILES = \$(CFILES:\%.cpp=\%.h)}
  2470. \end{quote}
  2471. \end{ttdescription}
  2472. \begin{notes}
  2473. \item You must assign values to both \texttt{MYCFILES} and
  2474. \texttt{CFILES}.
  2475. \item The convention that there is a header file for each C++ file is
  2476. essential for configuration system to work correctly.
  2477. \end{notes}
  2478. %
  2479. % Standard Targets
  2480. %
  2481. \section{Standard Targets}
  2482. \begin{description}
  2483. \item[\textbf{it}, \texttt{all}]
  2484. \texttt{it} and \texttt{all} must the default targets
  2485. which are executed when no targets are given to make.
  2486. Both targets \texttt{it} and \texttt{all} are required.
  2487. \item[install.local]
  2488. This target is used to perform installation operations which are
  2489. specific for the local directory. \TBD{}.
  2490. \end{description}
  2491. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2492. %
  2493. % Modules
  2494. %
  2495. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2496. \chapter{Modules}
  2497. \label{c:Modules}
  2498. This section describes how to design \Graphlet{} modules. A
  2499. \texttt{module} is a set of C++ and \GraphScript{} files which
  2500. implement one or several algorithms. The core of Graphlet itself is
  2501. divided into three modules:
  2502. \begin{itemize}
  2503. \item The module \texttt{gt\_base}, which implements basic and Tcl
  2504. independend data structures.
  2505. \item The module \texttt{gt\_tcl}, which implements Graphlet's Tcl
  2506. interface. Most of \GraphScript{} is implemented here.
  2507. \item The module \texttt{gt\_algorithms} implements various algorithms.
  2508. \end{itemize}
  2509. Each module has a name, which must be unique throughout the Graphlet
  2510. system. There are no restrictions on the name, but it is usually a
  2511. good idea to choose a descriptive name. Later, the header files of a
  2512. module \emph{name} will be installed in a directory
  2513. \texttt{name}\footnote{As a subdirectory of the Graphlet installation
  2514. directory, e.g.\ \texttt{/usr/local/include} on UNIX systems.}, and
  2515. its library will be installed under the name
  2516. \texttt{lib}\emph{name}\texttt{.a}\footnote{On UNIX systems}.
  2517. Graphlet uses the prefix \texttt{gt\_} for its names to prevent name
  2518. clashes with other software packages.
  2519. To avoid cluttering the system with too many libraries, modules should
  2520. contain several algorithms, and should possibly further structured
  2521. into submodules.
  2522. %
  2523. % Guidelines for adding C++ modules
  2524. %
  2525. \section{Guidelines for adding C++ modules}
  2526. \subsection{Example}
  2527. An example module can be found in the directory
  2528. \texttt{src/gt\_algorithms} in the Graphlet distribution.
  2529. \begin{itemize}
  2530. \item The initialization file of the algorithms module:
  2531. \begin{quote}
  2532. \texttt{src/gt\_algorithms/algorithms.cpp}
  2533. \end{quote}
  2534. \item Header file of the initialization procedure of the algorithms module:
  2535. \begin{quote}
  2536. \texttt{src/gt\_algorithms/algorithms.h}
  2537. \end{quote}
  2538. \end{itemize}
  2539. \subsection{The Initialization File}
  2540. A module \emph{mod} must provide its initialization code in a file
  2541. named \texttt{\Param{mod}.cpp} (respectively.\ \texttt{\Param{mod}.h}).
  2542. The following headers are required for module initialization files:
  2543. \begin{itemize}
  2544. \item \verb|#include <gt_base/Graphlet.h>|
  2545. \item \verb|#include <gt_tcl/Tcl_Algorithm.h>|
  2546. \item \verb|#include <gt_tcl/GraphScript.h>|
  2547. \end{itemize}
  2548. %
  2549. % The Initialization Procedure
  2550. %
  2551. \subsection{The Initialization Procedure}
  2552. The initialization procedure for a module \emph{mod} must be declared
  2553. as follows:
  2554. \begin{alltt}
  2555. int GT_\Param{mod}_init (Tcl_Interp* interp, GT_GraphScript* graphscript)
  2556. \end{alltt}
  2557. where \texttt{interp} is the current Tcl interpreter, and
  2558. \texttt{graphscript} is a pointer to the current \GraphScript{} class.
  2559. The initialization procedure must return \texttt{TCL\_OK} if the
  2560. initialization was successful, and \texttt{TCL\_ERROR} otherwise.
  2561. \begin{notes}
  2562. \item The naming convention is necessary to use the standard
  2563. initialization macros with \texttt{application\_init}.
  2564. \item Initialization procedures must \emph{never} make any
  2565. assumptions on the sequence in which initialization procedures are
  2566. called. The only valid assumption is that \Graphlet{}'s
  2567. initializations are called before any algorithm modules are
  2568. initialized.
  2569. \end{notes}
  2570. %
  2571. % Guidelines for adding \GraphScript{} modules
  2572. %
  2573. \section{Guidelines for adding \GraphScript{} modules}
  2574. \subsection{Placement}
  2575. During \emph{development}, \GraphScript{} files are best kept in the
  2576. developers directory. This is best done with autoloading (see the Tcl
  2577. documentation for details). For \emph{inclusion in the source code
  2578. distribution}, \GraphScript{} files should be placed in the directory
  2579. \texttt{lib/graphscript} within \Graphlet{}, or in subdirectories of
  2580. \texttt{lib/graphscript}.
  2581. \subsection{Filenames in \texttt{lib/graphscript}}
  2582. The filenames should reflect the name of the module:
  2583. \begin{itemize}
  2584. \item If there is only one \GraphScript{} file, it should have the
  2585. same name as the module (with suffix \texttt{.tcl}).
  2586. \item If there are several files, organize them in a subdirectory
  2587. with the name of the module.
  2588. \end{itemize}
  2589. \subsection{GNUmakefile modifications}
  2590. To add files to the \GraphScript{} library, edit the file
  2591. \begin{alltt}
  2592. lib/graphscript/GNUmakefile
  2593. \end{alltt}
  2594. \noindent and add the files to the list of files in the variable
  2595. \texttt{TCLFILES}. It is also neccessary to re-create the Tcl index
  2596. after new files were added or changed. This can be done with the
  2597. following command:
  2598. \begin{quote}
  2599. \texttt{gmake index}
  2600. \end{quote}
  2601. \noindent where \texttt{gmake} is GNU make.
  2602. \begin{notes}
  2603. \item
  2604. Subdirectories do not need to have their own GNUmakefile's.
  2605. Instead, add all files to \texttt{lib/graphscript/GNUmakefile}.
  2606. Remember to use relative paths.
  2607. \end{notes}
  2608. \subsection{Initialization}
  2609. There are two ways to initialize a \GraphScript{} module:
  2610. \begin{enumerate}
  2611. \item
  2612. Tcl will execute any statements at the top level of a
  2613. Tcl file when it loads the file. Put initialization code
  2614. here.
  2615. \item
  2616. \emph{(Preferred)}
  2617. Implement an initialization procedure in Tcl (preferably called
  2618. \GT{init\_\Param{name}} for module \emph{name}) and execute that
  2619. procedure in the initialization procedure of the C++ code.
  2620. Here is an example:
  2621. \begin{verbatim}
  2622. code = Tcl_Eval (interp, "GT_name_init");
  2623. if (code == TCL_ERROR) {
  2624. return code;
  2625. }
  2626. \end{verbatim}
  2627. The Tcl initialization procedure should come at the end of the
  2628. C++ module initialization, after all \GraphScript{} commands have
  2629. been installed.
  2630. \end{enumerate}
  2631. %
  2632. % How to determine what is installed
  2633. %
  2634. \section{How to determine what is installed}
  2635. Before features are used which are implemented in an optional module,
  2636. you need to check whether the module is present. Generally,
  2637. \Graphlet{} installations have the freedom to install or de-install
  2638. any module. Therefore, it is \textbf{illegal} to make any a priory
  2639. assumptions about installed modules. It is however possible to obtain
  2640. information on whether a specific feature is installed:
  2641. \begin{description}
  2642. \item[C++]
  2643. For each module \emph{name}, \Graphlet{} defines a preprocessor
  2644. symbol \GT{MODULE\_\Param{NAME}} (note the capital letters). This
  2645. can be used to write code which is only executed when a specific
  2646. module exists:
  2647. \begin{alltt}
  2648. #ifdef GT_MODULE_NAME
  2649. \ldots \emph{put code here that needs module \Param{name} here} \ldots
  2650. #endif
  2651. \end{alltt}
  2652. \item[\GraphScript{}]
  2653. Tcl goes even one step further than C++ and allows runtime testing
  2654. for installed commands with \texttt{info commands}. Here is an
  2655. example how to use that:
  2656. \begin{alltt}
  2657. if \{ [info commands cmd_name] != \{\} \} \{
  2658. \ldots \emph{Put code here that needs command cmd_name} \ldots
  2659. \}
  2660. \end{alltt}
  2661. \begin{note}
  2662. It is generally a good idea to add menu entries only for those
  2663. commands which are actually installed.
  2664. \end{note}
  2665. \end{description}
  2666. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2667. %
  2668. % Building \GraphScript{} Interpreters
  2669. %
  2670. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2671. \section{Building \GraphScript{} Interpreters}
  2672. For a quick take, copy and modify \Graphlet{}'s \texttt{graphscript.cpp}.
  2673. After new \GraphScript{} commands have been implemented, it is
  2674. necessary to build a new interpreter which contains these new
  2675. commands. Technically, this the same procedure as needed for a
  2676. standard Tcl interpreter, with \GraphScript{} as an additional
  2677. module.
  2678. \begin{note}
  2679. Tcl/Tk now supports dynamic loading. We will change the
  2680. initialization procedure at some point in the future to change
  2681. support dynamic loading. Stay tuned. There should be no major
  2682. problems unless you use global variables, in which case there will.
  2683. \end{note}
  2684. %
  2685. %
  2686. %
  2687. \section{The \texttt{main} procedure}
  2688. Generally, a main procedure for a \GraphScript{} interpreter should
  2689. have the following form:
  2690. \begin{verbatim}
  2691. int main (int argc, char **argv)
  2692. {
  2693. return GT_GraphScript::gt_main (argc, argv, application_init);
  2694. }
  2695. \end{verbatim}
  2696. \GT{GraphScript:gt\_main} is standard method which calls Tk's
  2697. \texttt{Tk\_Main} procedure (see the Tk documentation for
  2698. details) and parses the command line in \texttt{argc} and
  2699. \texttt{argv} for GraphScript specific options.
  2700. \texttt{application\_init} must be provided by the application
  2701. and initializes \GraphScript{} and algorithm modules.
  2702. \begin{notes}
  2703. \item It is legal to use the procedure \texttt{Tk\_main}
  2704. instead of \GT{GraphScript:gt\_main}.
  2705. \end{notes}
  2706. %
  2707. %
  2708. %
  2709. \section{The procedure \texttt{application\_init}}
  2710. Each interpreter must provide a procedure \texttt{application\_init}
  2711. which must have the following form:
  2712. \begin{verbatim}
  2713. //
  2714. // application_init (Tcl_interp* interp)
  2715. //
  2716. // interp is the current Tcl interpreter.
  2717. //
  2718. static int application_init (Tcl_Interp *interp)
  2719. {
  2720. //
  2721. // Create a GraphScript handler.
  2722. // For customization, replace the class GT_GraphScript
  2723. // by a derived class.
  2724. //
  2725. GT_GraphScript* graphscript = new GT_GraphScript (interp);
  2726. //
  2727. // Initialize Tcl, Tk and GraphScript.
  2728. //
  2729. // code holds the Tcl return code.
  2730. //
  2731. int code = graphscript->application_init (interp);
  2732. //
  2733. // Check for an error. This step is mandatory.
  2734. //
  2735. if (code == TCL_ERROR) {
  2736. return code;
  2737. }
  2738. //
  2739. // Initialize other algorithm modules
  2740. //
  2741. #ifdef GT_MODULE_ALGORITHMS
  2742. GT_ADD_MODULE(gt_algorithms)
  2743. #endif
  2744. #ifdef GT_MODULE_LSD
  2745. GT_ADD_MODULE(gt_lsd)
  2746. #endif GT_MODULE_LSD
  2747. #ifdef GT_MODULE_GRID_ALGORITHMS
  2748. GT_ADD_MODULE(gt_grid_algorithms)
  2749. #endif GT_MODULE_LSD
  2750. return code;
  2751. }
  2752. \end{verbatim}
  2753. \begin{notes}
  2754. \item We recommend the above syntax for the initialization of other
  2755. modules, provided that these modules conform to the standards
  2756. described in the module documentation.
  2757. \item \Graphlet{}'s configuration system automatically defines a
  2758. macro \GT{MODULE\_\Param{NAME}} for each installed module \emph{name}
  2759. (note the capital letters).
  2760. \item The macro \GT{ADD\_MODULE} is provided by \Graphlet{} and
  2761. performs all actions necessary to initialize a standard module. It
  2762. will exit the procedure and \texttt{return} \texttt{TCL\_ERROR} if
  2763. the module initialization fails.
  2764. \item \texttt{application\_init} must return a Tcl error code, that
  2765. is \texttt{TCL\_OK} for a successful return or \texttt{TCL\_ERROR}
  2766. otherwise.
  2767. \item Because Tcl is a C library, \texttt{application\_init} must be
  2768. a procedure or a static member function.
  2769. \end{notes}
  2770. %
  2771. %
  2772. %
  2773. \section{Linking the program}
  2774. \begin{WhoCanSkipThis}
  2775. This section is meant as a guideline for developers who do not use
  2776. the binary distribution. Developers who use the source code
  2777. distribution may use \Graphlet{}'s configuration system to create
  2778. makefiles.
  2779. \end{WhoCanSkipThis}
  2780. Link our code with the following libraries:
  2781. \begin{enumerate}
  2782. \item[\Graphlet{} libraries]
  2783. \texttt{\emph{MODULES} -lgt\_tcl -lgt\_base} where \emph{MODULES} is
  2784. the list of modules as used in \texttt{application\_init} above.
  2785. \item[Tcl/Tk libraries]
  2786. \texttt{-ltk -ltcl}
  2787. \item[LEDA libraries (release 3.4)]
  2788. \texttt{-lP -lG -lL}
  2789. \item[X11 and system libraries]
  2790. This depends on your operating system, flavor of X11 and
  2791. local installation policy. Here are some recommendations:
  2792. \begin{itemize}
  2793. \item On solaris systems (2.4/2.5), we recommend
  2794. \texttt{-L/usr/openwin/lib -lX11 -lsocket -lnsl -ldl -lm}.
  2795. \item On SunOS systems (4.1.*), we recommend
  2796. \texttt{-L/usr/openwin/lib -lX11 -lm}
  2797. \item On Linux systems, we recommend
  2798. \texttt{-L/usr/X11/lib -lX11 -ldl -lm}
  2799. \end{itemize}
  2800. \end{enumerate}
  2801. \begin{notes}
  2802. \item If you are linking with LEDA 3.3.*, you need to add the library
  2803. \texttt{-lWx}.
  2804. \item The order in which libraries are linked \texttt{is} important.
  2805. We recommend to link libraries in the order indicated above.
  2806. \end{notes}
  2807. \end{document}
  2808. %%% Local Variables:
  2809. %%% mode: latex
  2810. %%% End: