/****************************************************************/
#include <stdio.h>
+#include <postgresql/libpq-fe.h>
#include <malloc.h>
/****************************************************************/
/* 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;
/* 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;
/* 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 */
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];
/* 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 */
{
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;
}
}
/* 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<h:refObj>\n\t\t\t<h:Occurrence>\n\t\t\t\t<h:position>");
+ 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("</h:position>\n\t\t\t\t<h:occurrence>%s</h:occurrence>\n\t\t\t</h:Occurrence>\n\t\t</h:refObj>\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. */
*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;
+