/[ascend]/trunk/ascend/system/graph.c
ViewVC logotype

Diff of /trunk/ascend/system/graph.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2422 by jpye, Wed Dec 15 06:35:43 2010 UTC revision 2423 by jpye, Wed Mar 23 04:42:11 2011 UTC
# Line 30  Line 30 
30  #endif  #endif
31  #include <ascend/general/platform.h>  #include <ascend/general/platform.h>
32    
33  #ifdef WITH_GRAPHVIZ  /*#ifdef WITH_GRAPHVIZ
34  # ifdef __WIN32__  # ifdef __WIN32__
35  #  include <gvc.h>  #  include <gvc.h>
36  # else  # else
37  #  include <graphviz/gvc.h>  #  include <graphviz/gvc.h>
38  # endif  # endif
39  # define HAVE_BOOLEAN  # define HAVE_BOOLEAN
40  #endif  #endif*/
41    
42  boolean X;  boolean X;
43    
# Line 46  boolean X; Line 46  boolean X;
46  #include "incidence.h"  #include "incidence.h"
47  #include <ascend/general/ascMalloc.h>  #include <ascend/general/ascMalloc.h>
48  #include <ascend/general/panic.h>  #include <ascend/general/panic.h>
49    #include <dlfcn.h>
50    #include <graphviz/gvc.h>
51    
52  int system_write_graph(slv_system_t sys  int system_write_graph(slv_system_t sys
53      , FILE *fp      , FILE *fp
# Line 54  int system_write_graph(slv_system_t sys Line 56  int system_write_graph(slv_system_t sys
56      incidence_vars_t id;      incidence_vars_t id;
57      build_incidence_data(sys, &id);      build_incidence_data(sys, &id);
58    
59  #ifdef WITH_GRAPHVIZ      void * handle;
60        handle = dlopen("libgvc.so",RTLD_NOW);
61        if(handle==NULL)
62        {
63            printf("Graphviz was not found on your system");
64            return 1;
65        }
66            
67        Agraph_t *g;
68        Agraph_t *(*agop)(char* , int);
69        
70        GVC_t *gvc;
71        GVC_t *(*gvConte)();
72    
73        void (*agnodeat) (Agraph_t* , char* , char*);
74        
75        *(void **) (&gvConte) = dlsym(handle,"gvContext");
76        *(void **) (&agop) = dlsym(handle,"agopen");
77        *(void **) (&agnodeat) = dlsym(handle,"agnodeattr");
78    
79    
80        unsigned edgecount = 0;
81        unsigned nodecount = 0;
82    
83        gvc = (*gvConte)();
84        g = (*agop)("g",AGDIGRAPH);
85        (*agnodeat)(g,"shape","ellipse");
86        (*agnodeat)(g,"label","");
87        (*agnodeat)(g,"color","");
88        (*agnodeat)(g,"style","");
89        
90        char temp[200];
91    
92        /* first create nodes for the relations */
93        unsigned i;
94        Agnode_t *n, *m;
95        Agnode_t *(*agno)(Agraph_t* , char*);
96        void (*ags) (Agnode_t* , char* , char*);
97    
98        *(void **) (&agno) = dlsym(handle,"agnode");
99        *(void **) (&ags) = dlsym(handle,"agset");
100        
101    
102        for(i=0; i < id.neqn; ++i){
103            char *relname;
104            relname = rel_make_name(sys,id.rlist[i]);
105            sprintf(temp,"r%d",rel_sindex(id.rlist[i]));
106            n = (*agno)(g,temp);
107            (*ags)(n,"label",relname);
108            if(rel_satisfied(id.rlist[i])){
109                (*ags)(n,"style","filled");
110                (*ags)(n,"color","blue");
111            }
112            ASC_FREE(relname);
113            nodecount++;
114        }
115    
116        /* now create nodes for the variables */
117        unsigned j;
118        for(j=0; j < id.nvar; ++j){
119            char *varname;
120            varname = var_make_name(sys,id.vlist[j]);
121            sprintf(temp,"v%d",var_sindex(id.vlist[j]));
122            n = (*agno)(g,temp);
123            (*ags)(n,"label",varname);
124            (*ags)(n, "shape", "box");
125            if(var_fixed(id.vlist[j])){
126                CONSOLE_DEBUG("VAR '%s' IS FIXED",varname);
127                (*ags)(n,"style","filled");
128                (*ags)(n,"color","green");
129            }
130            if(!var_active(id.vlist[j])){
131                CONSOLE_DEBUG("VAR '%s' IS FIXED",varname);
132                (*ags)(n,"style","filled");
133                (*ags)(n,"color","gray");
134            }
135            ASC_FREE(varname);
136            nodecount++;
137        }
138    
139        /* now create edges */
140        const struct var_variable **ivars;
141        unsigned niv;
142        char reltemp[200];
143        Agedge_t *e;
144        Agedge_t *(*aged)(Agraph_t* , Agnode_t*, Agnode_t*);
145    
146        *(void **) (&aged) = dlsym(handle,"agedge");
147    
148        for(i=0; i < id.nprow; ++i){
149            ivars = rel_incidence_list(id.rlist[i]);
150            niv = rel_n_incidences(id.rlist[i]);
151            sprintf(reltemp,"r%d",rel_sindex(id.rlist[i]));
152            char *relname;
153            relname = rel_make_name(sys,id.rlist[i]);
154            CONSOLE_DEBUG("rel = '%s'",relname);
155            ASC_FREE(relname);
156            for(j=0; j < niv; ++j){
157                const struct var_variable *v;
158                v = ivars[j];
159                sprintf(temp,"v%d",var_sindex(v));
160                n = (*agno)(g, reltemp);
161                m = (*agno)(g, temp);
162    
163                if(id.v2pc[var_sindex(v)]==id.e2pr[rel_sindex(id.rlist[i])]){
164                    e = (*aged)(g,n,m); /* from rel to var */
165                }else{
166                    e = (*aged)(g,m,n); /* from var to rel */
167                }
168                edgecount++;
169            }
170        }
171    
172        if(nodecount > 300 || edgecount > 300){
173            ERROR_REPORTER_HERE(ASC_USER_ERROR,"Graph is too complex, will not launch GraphViz (%d nodes, %d edges)", nodecount, edgecount);
174            return 1;
175        }
176    
177    
178        void (*gvLayo)(GVC_t* , Agraph_t*, char*);
179        void (*gvRend)(GVC_t* , Agraph_t*, char*, FILE*);
180    
181        *(void **) (&gvLayo) = dlsym(handle,"gvLayout");
182        *(void **) (&gvRend) = dlsym(handle,"gvRender");
183    
184    
185        (*gvLayo)(gvc, g, "dot");
186        (*gvRend)(gvc, g, (char*)format, fp);
187    
188        printf("\nErrors encountered %s\n",(*dlerror)());
189        dlclose(handle);
190    
191    #if 0//def WITH_GRAPHVIZ
192    
193    
194      Agraph_t *g;      Agraph_t *g;
195      GVC_t *gvc;      GVC_t *gvc;
196    
# Line 145  int system_write_graph(slv_system_t sys Line 281  int system_write_graph(slv_system_t sys
281      }      }
282    
283      gvLayout(gvc, g, "dot");      gvLayout(gvc, g, "dot");
284      gvRender(gvc, g, (char*)format, fp);      gvRender(gvc, g, (char*)format, fp); */
285    
286  #else  #else
287      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Function system_write_graph not available (GraphViz not present at build-time)");  //  ERROR_REPORTER_HERE(ASC_PROG_ERR,"Function system_write_graph not available (GraphViz not present at build-time)");
288      return 1; /* error */  //  return 1; /* error */
289  #endif  #endif
290    
291  #if 0  #if 0

Legend:
Removed from v.2422  
changed lines
  Added in v.2423

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22