1 /*********************************************************************/
2 /* Copyright (C) 2000, HELM Team */
4 /* This file is part of HELM, an Hypertextual, Electronic */
5 /* Library of Mathematics, developed at the Computer Science */
6 /* Department, University of Bologna, Italy. */
8 /* HELM is free software; you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation; either version 2 */
11 /* of the License, or (at your option) any later version. */
13 /* HELM is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with HELM; if not, write to the Free Software */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, */
21 /* MA 02111-1307, USA. */
23 /* For details, see the HELM World-Wide-Web page, */
24 /* http://cs.unibo.it/helm/. */
25 /*********************************************************************/
27 /****************************************************************/
29 /****************************************************************/
30 /* This module supplies routines for symbol table handling. */
31 /* - init_symbol_table(): it initializes the symbol table */
33 /* - search_bucket(): it searches the symbol table for the */
34 /* bucket containing a given identifier, and */
35 /* inserts it if it is not present; */
36 /****************************************************************/
37 /* First draft 11/12/2001, by Andrea Asperti */
38 /****************************************************************/
40 /****************************************************************/
41 /* 1. Inclusion of header files. */
42 /****************************************************************/
47 /****************************************************************/
49 /****************************************************************/
54 #define HASH2 0xf0000000
66 /****************************************************************/
68 /****************************************************************/
72 struct int_list *next;
79 struct int_list *depths;
80 struct st_bucket *next_st_bucket;
81 /* next bucket in the list */
82 struct st_bucket *all_next;
83 /* all buckets in symbol
84 table are linked together */
89 struct st_bucket *dictionary[DICTSIZE];
90 /* pointers to bucket lists */
92 /****************************************************************/
93 /* 4. Local functions. */
94 /****************************************************************/
95 struct int_list *add(struct int_list *,int);
97 /****************************************************************/
98 /* 5. Definitions of functions to be exported. */
99 /****************************************************************/
101 struct st_bucket *all;
103 /* The following function initializes the symbol table to NULL */
104 void init_symbol_table()
106 struct st_bucket *st;
109 /* initialize the dictionary */
110 for (i = 0; i < DICTSIZE; i++)
111 dictionary[i] = NULL;
115 /* The following function searches the symbol table for an identifier */
116 /* and inserts it if it is not present.
117 /* The bucket associated with the given identifier */
118 /* becomes the first one in its list. */
120 search_bucket(id, where, depth)
127 /* value returned by the */
133 struct st_bucket *st;
135 /* apply the hash function */
136 dict_index = hash_pjw(id);
137 /* fprintf(stderr,"%d\n", dict_index); fflush(stdout); */
139 /* scan the bucket list indicated by the hash function */
140 prev = curr = dictionary[dict_index];
141 while ((curr != NULL) && (strcmp(id, curr->id)))
144 curr = curr->next_st_bucket;
147 /* the identifier is not in the list */
149 allocate_bucket(&st,id,where);
150 if (where == MAINCONCL)
151 st->main_depth = depth;
152 else if (where == MAINHYP)
153 st->depths = add(st->depths,depth);
154 move_bucket(st,dict_index);
159 fprintf(stderr,"uno=%s\n", id);
160 fprintf(stderr,"st=%s\n", curr->id); fflush(stdout) */
162 /* the identifier is already in the list */
165 curr->pos[where] = 1;
167 curr->pos[INBODY] = 0; /* it will never be set again to 1 */
168 if (where == MAINHYP)
169 curr->depths=add(curr->depths,depth);
170 else if (where == MAINCONCL)
171 curr->main_depth = depth;
173 /* the identifier is not in the first position */
175 prev->next_st_bucket = curr->next_st_bucket;
183 print_all(about,out,outrel,outsort)
191 struct st_bucket *curr;
195 for (i = 0; i < 5; ++i)
196 if ((curr->pos[i]) == 1)
199 print_mainhyp(about,out,outrel,outsort,curr->id,curr->depths);
200 else if (i == MAINCONCL)
201 print_mainconcl(about,out,outrel,outsort,curr->id,curr->main_depth);
203 print_one(out,curr->id,i);
205 curr = curr->all_next;
210 /****************************************************************/
211 /* 5. Definitions of functions local to the module. */
212 /****************************************************************/
214 struct int_list *add(l,m)
218 struct int_list *curr;
219 /* scan the list looking for m */
221 while ((curr != NULL) && (m != (curr->val)))
224 /* m is not in the list */
226 curr = (struct int_list *)malloc(sizeof(struct int_list));
236 print_mainhyp(about,out,outrel,outsort,uri,l)
244 struct int_list *curr;
246 if (!strcmp(uri,"Rel"))
251 fprintf(outrel,"\t<h:Object rdf:about=\"");
252 fprintf(outrel,"%s",about);
253 fprintf(outrel,"\">\n");
254 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
255 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
256 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
257 fprintf(outrel,"\n\t\t</h:refRel>\n");
258 fprintf(outrel,"\t</h:Object>\n");
262 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
263 (!strcmp(uri,"Set")))
268 fprintf(outsort,"\t<h:Object rdf:about=\"");
269 fprintf(outsort,"%s",about);
270 fprintf(outsort,"\">\n");
271 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
272 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
273 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
274 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
275 fprintf(outsort,"\n\t\t</h:refSort>\n");
276 fprintf(outsort,"\t</h:Object>\n");
285 fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
286 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
287 fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
288 fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
289 fprintf(out,"\n\t\t</h:refObj>\n");
295 print_mainconcl(about,out,outrel,outsort,uri,depth)
304 if (!strcmp(uri,"Rel"))
306 fprintf(outrel,"\t<h:Object rdf:about=\"");
307 fprintf(outrel,"%s",about);
308 fprintf(outrel,"\">\n");
309 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
310 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
311 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
312 fprintf(outrel,"\n\t\t</h:refRel>\n");
313 fprintf(outrel,"\t</h:Object>\n");
315 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
316 (!strcmp(uri,"Set")))
318 fprintf(outsort,"\t<h:Object rdf:about=\"");
319 fprintf(outsort,"%s",about);
320 fprintf(outsort,"\">\n");
321 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
322 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
323 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
324 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
325 fprintf(outsort,"\n\t\t</h:refSort>\n");
326 fprintf(outsort,"\t</h:Object>\n");
330 fprintf(out,"\t\t<h:refObj rdf:parseType=\"Resource\">");
331 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
332 fprintf(out,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
333 fprintf(out,"\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
334 fprintf(out,"\n\t\t</h:refObj>\n");
338 // dome: cambiata per usare il modello con position
339 print_one(out,uri,pos)
344 fprintf(out,"\t\t<h:refObj df:parseType=\"Resource\">");
345 fprintf(out,"\n\t\t\t\t<h:position rdf:resource=\"&hns;");
347 fprintf(out,"InBody");
348 else if (pos == MAINHYP)
349 fprintf(out,"MainHypothesis");
350 else if (pos == INHYP)
351 fprintf(out,"InHypothesis");
352 else if (pos == INCONCL)
353 fprintf(out,"InConclusion");
354 else if (pos == MAINCONCL)
355 fprintf(out,"MainConclusion");
356 fprintf(out,"\"/>\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>\n\t\t</h:refObj>\n", uri);
360 /* The following function allocates a bucket for an identifier. */
361 allocate_bucket(st, id, where)
364 /* pointer to the bucket to be */
372 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
373 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
374 strcpy((*st)->id,id);
375 (*st)->main_depth = 0;
376 (*st)->depths = NULL;
377 (*st)->next_st_bucket = NULL;
378 (*st)->all_next = all;
380 for (i = 0; i < 5; ++i)
382 (*st)->pos[where] = 1;
385 /* The following function moves a bucket to the head of the */
386 /* list in which it lies. */
387 move_bucket(st, dict_index)
390 /* pointer to the bucket to */
393 /* index corresponding to */
394 /* the list in which the */
397 st->next_st_bucket = dictionary[dict_index];
398 dictionary[dict_index] = st;
401 /* The following function implements Weinberger's hash function. */
405 /* identifier to be hashed */
410 for (h = 0; *id != EOS; id++)
412 h = (h << HASH1) + (*id);
414 h = h ^ (g >> HASH3) ^ g;
416 return(h % DICTSIZE);