X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fmetadata%2Fcreate_V7_mowgli%2FMETADATA%2Fsthandler.c;h=94b87c0c4708d7c24bba4c0fbce509347e65f488;hb=7cb90c67bc6f8113188a91ecc29f6db20db5aeb8;hp=963200038beda7a83ff051fd4205f16a9f22cfc0;hpb=0e6b8620b9c128c3f256c00b422cee7708321171;p=helm.git diff --git a/helm/metadata/create_V7_mowgli/METADATA/sthandler.c b/helm/metadata/create_V7_mowgli/METADATA/sthandler.c index 963200038..94b87c0c4 100644 --- a/helm/metadata/create_V7_mowgli/METADATA/sthandler.c +++ b/helm/metadata/create_V7_mowgli/METADATA/sthandler.c @@ -42,6 +42,7 @@ /****************************************************************/ #include +#include #include /****************************************************************/ @@ -67,9 +68,16 @@ /* 3. Types. */ /****************************************************************/ +struct int_list { + int val; + struct int_list *next; + }; + struct st_bucket { char *id; - /* identifier */ + /* identifier */ + int main_depth; + struct int_list *depths; struct st_bucket *next_st_bucket; /* next bucket in the list */ struct st_bucket *all_next; @@ -83,7 +91,12 @@ struct st_bucket *dictionary[DICTSIZE]; /* pointers to bucket lists */ /****************************************************************/ -/* 4. Definitions of functions to be exported. */ +/* 4. Local functions. */ +/****************************************************************/ +struct int_list *add(struct int_list *,int); + +/****************************************************************/ +/* 5. Definitions of functions to be exported. */ /****************************************************************/ struct st_bucket *all; @@ -105,10 +118,11 @@ void init_symbol_table() /* The bucket associated with the given identifier */ /* becomes the first one in its list. */ -search_bucket(id, where) +search_bucket(id, where, depth) char *id; /* identifier */ int where; + int depth; { int dict_index; /* value returned by the */ @@ -120,8 +134,8 @@ search_bucket(id, where) struct st_bucket *st; /* apply the hash function */ - dict_index = hash_pjw(id); - /* printf( "%d\n", dict_index); */ + dict_index = hash_pjw(id); + /* fprintf(stderr,"%d\n", dict_index); */ /* scan the bucket list indicated by the hash function */ prev = curr = dictionary[dict_index]; @@ -134,12 +148,17 @@ search_bucket(id, where) /* the identifier is not in the list */ { allocate_bucket(&st,id,where); + if (where == MAINCONCL) + st->main_depth = depth; + else if (where == MAINHYP) + st->depths = add(st->depths,depth); move_bucket(st,dict_index); return NOTFOUND; } else - /* printf("uno=%s\n", id); - printf("st=%s\n", curr->id); */ + /* + printf("uno=%s\n", id); + printf("st=%s\n", curr->id); fflush(stdout) */ /* the identifier is already in the list */ { @@ -147,28 +166,40 @@ search_bucket(id, where) curr->pos[where] = 1; if (where >= 1) curr->pos[INBODY] = 0; /* it will never be set again to 1 */ + if (where == MAINHYP) + curr->depths=add(curr->depths,depth); + else if (where == MAINCONCL) + curr->main_depth = depth; if (prev != curr) /* the identifier is not in the first position */ { prev->next_st_bucket = curr->next_st_bucket; - move_bucket(curr, - dict_index); + move_bucket(curr,dict_index); }; return where; } } -print_all() +print_all(about,conn) + char *about; + PGconn *conn; { + int i; struct st_bucket *curr; curr = all; - while (curr != NULL) { for (i = 0; i < 5; ++i) - if (curr->pos[i] == 1) - print_one(curr->id,i); + if ((curr->pos[i]) == 1) + { + if (i == MAINHYP) + print_mainhyp(about,conn,curr->id,curr->depths); + else if (i == MAINCONCL) + print_mainconcl(about,conn,curr->id,curr->main_depth); + else + print_one(conn,about,curr->id,i); + } curr = curr->all_next; } } @@ -178,24 +209,198 @@ print_all() /* 5. Definitions of functions local to the module. */ /****************************************************************/ +void exit_nicely(PGconn *conn) +{ + PQfinish(conn); + exit(1); +} -// dome: cambiata per usare il modello con position -print_one(uri,pos) +struct int_list *add(l,m) + struct int_list *l; + int m; +{ + struct int_list *curr; + /* scan the list looking for m */ + curr = l; + while ((curr != NULL) && (m != (curr->val))) + curr = curr->next; + if (curr == NULL) + /* m is not in the list */ + { + curr = (struct int_list *)malloc(sizeof(struct int_list)); + curr->val = m; + curr->next = l; + return curr; + } + else + return l; + +} + +print_mainhyp(about,conn,uri,l) + char *about; + PGconn *conn; + char *uri; + struct int_list *l; +{ + PGresult *res; + char *command = (char *)malloc((sizeof('a')*200)); + struct int_list *curr; + curr = l; + if (!strcmp(uri,"Rel")) + { + /* scan the list */ + while (curr != NULL) + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + PQescapeString(qabout,about,len); + sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,curr->val); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + { + fprintf(stderr, "BEGIN command failed\n"); + PQclear(res); + exit_nicely(conn); + } + curr = curr->next; + } + } + else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) || + (!strcmp(uri,"Set"))) + { + /* scan the list */ + while (curr != NULL) + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + PQescapeString(qabout,about,len); + sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d, '%s')",qabout,curr->val,uri); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + { + fprintf(stderr, "BEGIN command failed\n"); + PQclear(res); + exit_nicely(conn); + } + curr = curr->next; + } + } + else + { + /* scan the list */ + while (curr != NULL) + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + char *quri; + PQescapeString(qabout,about,len); + len = strlen(uri) + 1; + quri = malloc (sizeof(char) * len * 2); + PQescapeString(quri,uri,len); + sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,quri,curr->val); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + { + fprintf(stderr, "BEGIN command failed\n"); + PQclear(res); + exit_nicely(conn); + } + curr = curr->next; + } + } +} + +print_mainconcl(about,conn,uri,depth) + char *about; + PGconn *conn; char *uri; + int depth; + +{ + PGresult *res; + char *command = (char *)malloc((sizeof('a')*200)); + /* fprintf(stderr,"about = %s\n",about); */ + if (!strcmp(uri,"Rel")) + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + PQescapeString(qabout,about,len); + sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,depth); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + } + else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) || + (!strcmp(uri,"Set"))) + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + PQescapeString(qabout,about,len); + sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d, '%s')",qabout,depth,uri); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + } + else + { + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + char *quri; + PQescapeString(qabout,about,len); + len = strlen(uri) + 1; + quri = malloc (sizeof(char) * len * 2); + PQescapeString(quri,uri,len); + sprintf(command,"INSERT INTO refObj values ('%s', '%s','http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,quri,depth); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + } + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + { + fprintf(stderr, "BEGIN command failed\n"); + PQclear(res); + exit_nicely(conn); + } + /* fprintf(stderr,"FINITO\n"); */ +} + +// dome: cambiata per usare il modello con position +print_one(conn,about,uri,pos) + PGconn *conn; + char *about, + *uri; int pos; { - printf("\t\t\n\t\t\t\n\t\t\t\t"); + PGresult *res; + char *command = (char *)malloc((sizeof('a')*200)); + char *position = (char *)malloc((sizeof('a')*20)); + size_t len = strlen(about) + 1; + char *qabout = malloc (sizeof(char) * len * 2); + char *quri; + PQescapeString(qabout,about,len); + len = strlen(uri) + 1; + quri = malloc (sizeof(char) * len * 2); + PQescapeString(quri,uri,len); if (pos == INBODY) - printf("InBody"); + position="InBody"; else if (pos == MAINHYP) - printf("MainHypothesis"); + position="MainHypothesis"; /* This should never happen */ else if (pos == INHYP) - printf("InHypothesis"); + position="InHypothesis"; else if (pos == INCONCL) - printf("InConclusion"); + position="InConclusion"; else if (pos == MAINCONCL) - printf("MainConclusion"); - printf("\n\t\t\t\t%s\n\t\t\t\n\t\t\n", uri); + position="MainConclusion"; /* This should never happen */ + sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#%s')",qabout,quri,position); + /* fprintf(stderr, "%s\n", command); */ + res = PQexec(conn, command); + if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + { + fprintf(stderr, "BEGIN command failed\n"); + PQclear(res); + exit_nicely(conn); + } } /* The following function allocates a bucket for an identifier. */ @@ -213,6 +418,8 @@ allocate_bucket(st, id, where) *st = (struct st_bucket *)malloc(sizeof(struct st_bucket)); (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1)); strcpy((*st)->id,id); + (*st)->main_depth = 0; + (*st)->depths = NULL; (*st)->next_st_bucket = NULL; (*st)->all_next = all; all = *st; @@ -259,3 +466,4 @@ hash_pjw(id) +