#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
+#include <postgresql/libpq-fe.h>
#include "sthandler.h"
%}
int position = INBODY;
int first_child = HERE;
int skip = 0; // boolean to skip the insertion of a URI
-int no_open_source =0;
+int no_open_source = 0;
int spine_depth = 0;
int depth = 0;
int tmp_n;
"</body>"(" "|\n)*"<type" {
position = INTYPE; // Variables have both a body and a type
+ first_child = HERE;
+ no_open_source = 0;
+ spine_depth = 0;
+ depth = 0;
}
"<decl" |
}
"</decl>" |
-"</def" {
+"</def>" {
if (position == INHYP)
{
no_open_source--;
position = INTYPE;
spine_depth++;
depth = 0;
+ first_child = HERE;
}
- first_child = HERE;
+ /* bug? first_child = HERE; */
}
main(int argc, char *argv[])
{
- struct stat buf;
- FILE *outrel, *outsort;
-
- init_symbol_table();
- if (!(outrel = fopen("forward_rel.xml","a")))
- {
- fprintf(stderr, "error in openinf file forward_rel.xml\n");
- exit(-1);
- }
- if (!(outsort = fopen("forward_sort.xml","a")))
- {
- fprintf(stderr, "error in openinf file forward_rel.xml\n");
- exit(-1);
- }
- // We process the body
- if (!stat("tmp/body.xml",&buf)) {
- yyin = fopen("tmp/body.xml", "r");
- position = INBODY;
- yylex();
- fclose(yyin);
- }
+ struct stat buf;
+
+ char *pghost,
+ *pgport,
+ *pgoptions,
+ *pgtty;
+ char *dbName;
+
+ /* FILE *debug; */
+
+ PGconn *conn;
+ PGresult *res;
+
+ /*
+ * begin, by setting the parameters for a backend connection if the
+ * parameters are null, then the system will try to use reasonable
+ * defaults by looking up environment variables or, failing that,
+ * using hardwired constants
+ */
+
+ /* make a connection to the database */
+ conn = PQconnectdb("user=helm dbname=mowgli");
+
+ /*
+ * check to see that the backend connection was successfully made
+ */
+ if (PQstatus(conn) == CONNECTION_BAD)
+ {
+ fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
+ fprintf(stderr, "%s", PQerrorMessage(conn));
+ exit_nicely(conn);
+ }
+
+ /* debug = fopen("/tmp/trace.out","w"); */
+ /* PQtrace(conn, debug); */
+
+ /* initialize the symbol table */
+ init_symbol_table();
+
+ // We process the body
+ if (!stat("tmp/body.xml",&buf))
+ {
+ yyin = fopen("tmp/body.xml", "r");
+ position = INBODY;
+ yylex();
+ fclose(yyin);
+ }
+
+ // We process the type
+ yyin = fopen("tmp/type.xml", "r");
+ position = INTYPE;
+ first_child = HERE;
+ no_open_source = 0;
+ spine_depth = 0;
+ depth = 0;
+ yylex();
+ fclose(yyin);
+ print_all(argv[1],conn);
+}
- // We process the type
- yyin = fopen("tmp/type.xml", "r");
- position = INTYPE;
- first_child = HERE;
- no_open_source = 0;
- spine_depth = 0;
- depth = 0;
- yylex();
-
- printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n");
-printf("<!DOCTYPE rdf:RDF [
- <!ENTITY rdfns 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
- <!ENTITY hthns 'http://www.cs.unibo.it/helm/schemas/schema-helmth#'>
- <!ENTITY hns 'http://www.cs.unibo.it/helm/schemas/schema-helm#'>
-
- ]>\n");
-
- printf("<rdf:RDF xml:lang=\"en\" xmlns:rdf=\"&rdfns;\" xmlns:h=\"&hns;\" xmlns:hth=\"&hthns;\">\n");
- printf("\t<h:Object rdf:about=\"");
- printf("%s",argv[1]);
- printf("\">\n");
- print_all(argv[1],outrel,outsort);
- printf("\t</h:Object>\n");
- printf("</rdf:RDF>\n");
- fclose(yyin);
- }
search(uri,first_child,position,depth)
char *uri;
else
found = search_bucket(uri,position,depth);
- /* if (found == NOTFOUND)
- fprintf(stderr,"pos = %d, uri = %s\n", position, uri); */
+ /*
+ if (found == NOTFOUND)
+ fprintf(stderr,"here = %d, pos = %d, uri = %s\n", first_child,position, uri); */
}
/*
(first_child == HERE)
return 1;
}
+
+
+
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
-#include "sthandler_ind.h"
+#include <postgresql/libpq-fe.h>
+#include "sthandler.h"
%}
/***************************************************************/
char *xpointer = "#xpointer(1/";
char *uri;
char *tmp;
-char *filename;
-char *file_uri;
-char *inductive_uri;
-char *filename_prefix;
-char *file_uri_prefix;
+char *source_uri;
+char *source_uri_prefix;
+PGconn *conn;
%}
/***************************************************************/
}
"</arity>" { tmp = (char *)malloc(sizeof('a')*128);
- strcpy(filename,filename_prefix);
- /* fprintf(stderr,"tre"); */
- strcpy(file_uri,file_uri_prefix);
- sprintf(tmp,",%d.xml", inductive_type);
- /* fprintf(stderr,"quattro"); */
- strcat(filename,tmp);
+ strcpy(source_uri,source_uri_prefix);
sprintf(tmp,"#xpointer(1/%d)", inductive_type);
- strcat(file_uri,tmp);
+ strcat(source_uri,tmp);
/* fprintf(stderr,"cinque"); */
free(tmp);
- print_file();
+ print_all(source_uri,conn);
+ /* print_file(); */
}
"<Constructor" { init_symbol_table();
depth = 0;
spine_depth = 0;
constructor++;
- strcpy(inductive_uri,file_uri_prefix);
position = INTYPE;
first_child = HERE;
}
"</Constructor>" { tmp = (char *)malloc(sizeof('a')*128);
- strcpy(filename,filename_prefix);
- strcpy(file_uri,file_uri_prefix);
- strcpy(inductive_uri,file_uri_prefix);
- sprintf(tmp,",%d,%d.xml", inductive_type,constructor);
- strcat(filename,tmp);
+ strcpy(source_uri,source_uri_prefix);
sprintf(tmp,"#xpointer(1/%d/%d)",inductive_type,constructor);
- strcat(file_uri,tmp);
+ strcat(source_uri,tmp);
free(tmp);
- print_file();
+ print_all(source_uri,conn);
+ /* print_file(); */
}
"<decl" |
-"<def" {
+"<def" {
if (position == INTYPE)
position = MAINHYP;
else if (position == MAINHYP)
}
"</decl>" |
-"</def" {
+"</def>" {
if (position == INHYP)
{
no_open_source--;
position = INTYPE;
spine_depth++;
depth = 0;
+ first_child = HERE;
}
- first_child = HERE;
}
main(int argc, char *argv[])
{
- filename = malloc((sizeof('a')*2000));
- file_uri = malloc((sizeof('a')*2000));
- inductive_uri = malloc((sizeof('a')*2000));
- filename_prefix=argv[1];
- file_uri_prefix=argv[2];
- /* fprintf(stderr,"qua"); */
- yyin = fopen("tmp/inductive_type.xml", "r");
- yylex();
-}
-
-print_file()
-{
- FILE *out, *outrel, *outsort;
-
- if (!(out = fopen(filename,"w")))
- {
- fprintf(stderr, "error in openinf file %s\n", filename);
- exit(-1);
- }
- if (!(outrel = fopen("forward_rel.xml","a")))
- {
- fprintf(stderr, "error in openinf file forward_rel.xml\n");
- exit(-1);
- }
- if (!(outsort = fopen("forward_sort.xml","a")))
- {
- fprintf(stderr, "error in openinf file forward_rel.xml\n");
- exit(-1);
- }
-
- // We process the type
-
- fprintf(out,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n");
-fprintf(out,"<!DOCTYPE rdf:RDF [
- <!ENTITY rdfns 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
- <!ENTITY hthns 'http://www.cs.unibo.it/helm/schemas/schema-helmth#'>
- <!ENTITY hns 'http://www.cs.unibo.it/helm/schemas/schema-helm#'>
-
- ]>\n");
- fprintf(out,"<rdf:RDF xml:lang=\"en\" xmlns:rdf=\"&rdfns;\" xmlns:h=\"&hns;\" xmlns:hth=\"&hthns;\">\n");
- fprintf(out,"\t<h:Object rdf:about=\"");
- fprintf(out,"%s",file_uri);
- fprintf(out,"\">\n");
- print_all(file_uri,out,outrel,outsort);
- fprintf(out,"\t</h:Object>\n");
- fprintf(out,"</rdf:RDF>\n");
- fclose(out);
- fclose(outrel);
- fclose(outsort);
+ struct stat buf;
+
+ char *pghost,
+ *pgport,
+ *pgoptions,
+ *pgtty;
+ char *dbName;
+
+ /* FILE *debug; */
+
+ PGresult *res;
+
+ /*
+ * begin, by setting the parameters for a backend connection if the
+ * parameters are null, then the system will try to use reasonable
+ * defaults by looking up environment variables or, failing that,
+ * using hardwired constants
+ */
+
+ /* make a connection to the database */
+ conn = PQconnectdb("user=helm dbname=mowgli");
+
+ /*
+ * check to see that the backend connection was successfully made
+ */
+ if (PQstatus(conn) == CONNECTION_BAD)
+ {
+ fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
+ fprintf(stderr, "%s", PQerrorMessage(conn));
+ exit_nicely(conn);
+ }
+
+ /* debug = fopen("/tmp/trace.out","w"); */
+ /* PQtrace(conn, debug); */
+
+ source_uri = malloc((sizeof('a')*2000));
+ source_uri_prefix=argv[1];
+ /* fprintf(stderr,"qua"); */
+ yyin = fopen("tmp/inductive_type.xml", "r");
+ yylex();
}
search(uri,first_child,position,depth)
else
found = search_bucket(uri,position,depth);
- /* if (found == NOTFOUND)
- printf( "pos = %d, uri = %s\n", position, uri); */
+ if (found == NOTFOUND)
+ printf( "pos = %d, uri = %s\n", position, uri);
}
/****************************************************************/
#include <stdio.h>
+#include <postgresql/libpq-fe.h>
#include <malloc.h>
/****************************************************************/
/* 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(about,outrel,outsort)
+print_all(about,conn)
char *about;
- FILE *outrel,
- *outsort;
+ PGconn *conn;
{
int i;
if ((curr->pos[i]) == 1)
{
if (i == MAINHYP)
- print_mainhyp(about,outrel,outsort,curr->id,curr->depths);
+ print_mainhyp(about,conn,curr->id,curr->depths);
else if (i == MAINCONCL)
- print_mainconcl(about,outrel,outsort,curr->id,curr->main_depth);
+ print_mainconcl(about,conn,curr->id,curr->main_depth);
else
- print_one(curr->id,i);
+ 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);
+}
+
struct int_list *add(l,m)
struct int_list *l;
int m;
}
-print_mainhyp(about,outrel,outsort,uri,l)
+print_mainhyp(about,conn,uri,l)
char *about;
- FILE *outrel,
- *outsort;
+ 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)
{
- fprintf(outrel,"\t<h:Object rdf:about=\"");
- fprintf(outrel,"%s",about);
- fprintf(outrel,"\">\n");
- fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
- fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
- fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
- fprintf(outrel,"\n\t\t</h:refRel>\n");
- fprintf(outrel,"\t</h:Object>\n");
+ 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;
}
}
/* scan the list */
while (curr != NULL)
{
- fprintf(outsort,"\t<h:Object rdf:about=\"");
- fprintf(outsort,"%s",about);
- fprintf(outsort,"\">\n");
- fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
- fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
- fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
- fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
- fprintf(outsort,"\n\t\t</h:refSort>\n");
- fprintf(outsort,"\t</h:Object>\n");
+ 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;
}
}
/* scan the list */
while (curr != NULL)
{
- printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
- printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;MainHypothesis\"/>");
- printf("\n\t\t\t\t<h:depth>%d</h:depth>",curr->val);
- printf("\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
- printf("\n\t\t</h:refObj>\n");
+ 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,outrel,outsort,uri,depth)
+print_mainconcl(about,conn,uri,depth)
char *about;
- FILE *outrel,
- *outsort;
+ 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"))
{
- fprintf(outrel,"\t<h:Object rdf:about=\"");
- fprintf(outrel,"%s",about);
- fprintf(outrel,"\">\n");
- fprintf(outrel,"\t\t<h:refRel rdf:parseType=\"Resource\">");
- fprintf(outrel,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
- fprintf(outrel,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
- fprintf(outrel,"\n\t\t</h:refRel>\n");
- fprintf(outrel,"\t</h:Object>\n");
+ 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")))
{
- fprintf(outsort,"\t<h:Object rdf:about=\"");
- fprintf(outsort,"%s",about);
- fprintf(outsort,"\">\n");
- fprintf(outsort,"\t\t<h:refSort rdf:parseType=\"Resource\">");
- fprintf(outsort,"\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
- fprintf(outsort,"\n\t\t\t\t<h:sort rdf:resource=\"&hns;%s\"/>",uri);
- fprintf(outsort,"\n\t\t\t\t<h:depth>%d</h:depth>",depth);
- fprintf(outsort,"\n\t\t</h:refSort>\n");
- fprintf(outsort,"\t</h:Object>\n");
+ 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
{
- printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
- printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;MainConclusion\"/>");
- printf("\n\t\t\t\t<h:depth>%d</h:depth>",depth);
- printf("\n\t\t\t\t<h:occurrence><h:Object rdf:about=\"%s\"/></h:occurrence>",uri);
- printf("\n\t\t</h:refObj>\n");
+ 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(uri,pos)
- char *uri;
+print_one(conn,about,uri,pos)
+ PGconn *conn;
+ char *about,
+ *uri;
int pos;
{
- printf("\t\t<h:refObj rdf:parseType=\"Resource\">");
- printf("\n\t\t\t\t<h:position rdf:resource=\"&hns;");
+ 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<h:occurrence><h:Object rdf:about=\"%s\"/></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. */