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); */
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 printf("uno=%s\n", id);
160 printf("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,outrel,outsort)
190 struct st_bucket *curr;
194 for (i = 0; i < 5; ++i)
195 if ((curr->pos[i]) == 1)
198 print_mainhyp(about,outrel,outsort,curr->id,curr->depths);
199 else if (i == MAINCONCL)
200 print_mainconcl(about,outrel,outsort,curr->id,curr->main_depth);
202 print_one(curr->id,i);
204 curr = curr->all_next;
209 /****************************************************************/
210 /* 5. Definitions of functions local to the module. */
211 /****************************************************************/
213 struct int_list *add(l,m)
217 struct int_list *curr;
218 /* scan the list looking for m */
220 while ((curr != NULL) && (m != (curr->val)))
223 /* m is not in the list */
225 curr = (struct int_list *)malloc(sizeof(struct int_list));
235 print_mainhyp(about,outrel,outsort,uri,l)
242 struct int_list *curr;
244 if (!strcmp(uri,"Rel"))
249 fprintf(outrel,"\t<h:Object rdf:about=\"");
250 fprintf(outrel,"%s",about);
251 fprintf(outrel,"\">\n");
252 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
253 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
254 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
255 fprintf(outrel,"\n\t\t</h:refRel>\n");
256 fprintf(outrel,"\t</h:Object>\n");
260 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
261 (!strcmp(uri,"Set")))
266 fprintf(outsort,"\t<h:Object rdf:about=\"");
267 fprintf(outsort,"%s",about);
268 fprintf(outsort,"\">\n");
269 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
270 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
271 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
272 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
273 fprintf(outsort,"\n\t\t</h:refSort>\n");
274 fprintf(outsort,"\t</h:Object>\n");
283 printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
284 printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
285 printf("\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
286 printf("\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
287 printf("\n\t\t</h:refObj>\n");
293 print_mainconcl(about,outrel,outsort,uri,depth)
301 if (!strcmp(uri,"Rel"))
303 fprintf(outrel,"\t<h:Object rdf:about=\"");
304 fprintf(outrel,"%s",about);
305 fprintf(outrel,"\">\n");
306 fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
307 fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
308 fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
309 fprintf(outrel,"\n\t\t</h:refRel>\n");
310 fprintf(outrel,"\t</h:Object>\n");
312 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
313 (!strcmp(uri,"Set")))
315 fprintf(outsort,"\t<h:Object rdf:about=\"");
316 fprintf(outsort,"%s",about);
317 fprintf(outsort,"\">\n");
318 fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
319 fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
320 fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
321 fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
322 fprintf(outsort,"\n\t\t</h:refSort>\n");
323 fprintf(outsort,"\t</h:Object>\n");
327 printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
328 printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
329 printf("\n\t\t\t\t<h:depth>%d</h:depth>",depth);
330 printf("\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
331 printf("\n\t\t</h:refObj>\n");
335 // dome: cambiata per usare il modello con position
340 printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
341 printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;");
344 else if (pos == MAINHYP)
345 printf("MainHypothesis");
346 else if (pos == INHYP)
347 printf("InHypothesis");
348 else if (pos == INCONCL)
349 printf("InConclusion");
350 else if (pos == MAINCONCL)
351 printf("MainConclusion");
352 printf("\"/>\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>\n\t\t</h:refObj>\n", uri);
355 /* The following function allocates a bucket for an identifier. */
356 allocate_bucket(st, id, where)
359 /* pointer to the bucket to be */
367 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
368 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
369 strcpy((*st)->id,id);
370 (*st)->main_depth = 0;
371 (*st)->depths = NULL;
372 (*st)->next_st_bucket = NULL;
373 (*st)->all_next = all;
375 for (i = 0; i < 5; ++i)
377 (*st)->pos[where] = 1;
380 /* The following function moves a bucket to the head of the */
381 /* list in which it lies. */
382 move_bucket(st, dict_index)
385 /* pointer to the bucket to */
388 /* index corresponding to */
389 /* the list in which the */
392 st->next_st_bucket = dictionary[dict_index];
393 dictionary[dict_index] = st;
396 /* The following function implements Weinberger's hash function. */
400 /* identifier to be hashed */
405 for (h = 0; *id != EOS; id++)
407 h = (h << HASH1) + (*id);
409 h = h ^ (g >> HASH3) ^ g;
411 return(h % DICTSIZE);