<!--
// this is not pretty code; I learned Javascript by figuring out how
// to write stuff that call's Jan's (much nicer) prolog.js code. 
// go and have a look at that instead!
// daniel.brickley@bristol.ac.uk

// 1999-11-20
// removed 'push' operation on arrays as Netscape specific. Runs in IE5 now.

        function ResultSet(label) {
                this.type = "ResultSet";
                this.name = label;
                this.results = new Array();
                this.toString = function () { return(this.name); };
                this.addResult = function(r) {
                                this.results[this.results.length ] = r ;
                                }

                // Simple HTML output (example code showing use of RDF ResultSet)
                this.htmlreport = function() {
                                tmp = "\n<SMALL><TABLE BORDER=1>\n\n";
                                for (var h=0; h< this.results[0].items.length;h++) {
                                var item = this.results[0].items[h];
                                tmp += "<TH> "+item.name+" </TH>";
                                }
                                tmp += "</TR>\n";
                                for (var i=0; i< this.results.length; i++) {
                                        var res = this.results[i];

                                        tmp += "<TR>\n  ";
                                        for (var j=0; j<this.results[i].items.length; j++) {
                                                var item = this.results[i].items[j];
                                                tmp += " <TD VALIGN=\"TOP\"> " + item.val +" </TD>\n ";
                                                }
                                        tmp += "</TR>\n";
                                        }
                                tmp += "\n</TABLE></SMALL>\n";
                                return( tmp );

                                }

                // Simple textual dump of this result set
                this.report = function() {
                                tmp= "\nResultSet Dump: "+this.name+ "\n=========\n";
                                for (var i=0; i< this.results.length; i++) {
                                        var res = this.results[i];
                                        tmp += ("\nresult:  " +  res.report());
                                        }
                                return(tmp+ " \n//========= [end report] \n\n");
                                }
        }//end ResultSet


        // An RDFResult is a collection of RDFItems
        function RDFResult(label, data) {
                this.name = label;
                this.data = data;
                this.type = "RDFResult";
                this.items = new Array();
                this.variablenames = new Array();

                this.report = function () {
                                var tmp= "";
                                for (var i=0; i< this.items.length; i++) {
                                        var res = this.items[i];
                                        tmp += ( res.toString()  );
                                        }
                        return( tmp );
                        }

                this.addItem = function(i) {
//                                print("\nAdding item "+i.toString()+" to "+this.name +"\n");
				this.items[ this.items.length ] = i;
				this.variablenames[ this.variablenames.length ] = i.name;
                                }
        }


        // An RDFItem is a row/column combo in a resultset, eg. X="Smith"
        function RDFItem(varname,value) {
                this.name = varname;
                this.val = value;
                this.toString = function () { return(this.name+ " => " + this.val + "\n"); };
                this.type = "RDFItem";
        }


        // Crude HTML output mechanism
        function htmlWindow(data,query) {
          myWin= open("", "displayWindow","status=yes,toolbar=yes,menubar=yes,resizable,scrollbars,location");
          myWin.document.open();
          myWin.document.write("<html><head><title>RDF Query results for : "+query);
          myWin.document.write("</title></head><body><H3>RDF Query demo</H3> <P>Query was:<BR><CODE> "+query +"</CODE><BR> <H3>Result Set</H3>");
          myWin.document.write(data);
          myWin.document.write("<HR>maintainer: <A HREF=\"mailto:danbri@w3.org\">danbri@w3.org</A></body></html>");
          myWin.document.close();
        }



// Example RDF Query code
// mostly copied from prolog.js
// assumes a particular document structure

        function rdfquerytest() {
                cls();
                var rules = document.rules.rules.value;
                var query = document.input.query.value;

                print ("RDFQ: Parsing rulesets.\n");
                rules = rules.split("\n");
                var outr = [], outi=0;
                for (var r=0; r < rules.length; r++) {
                        var rule = rules[r];
                        if (rule.substring(0, 1) == "#" || rule == "") continue;
                        var or = ParseRule(new Tokeniser(rule));
                        if (or == null) continue;
                        outr[outi++] = or;
                        // print ("Rule "+outi+" is : ");
                }

                print ("\nRDFQ: Parsing query.\n");
                var q = ParseBody(new Tokeniser(query));
                if (q == null) {
                        print ("An error occurred parsing the query.\n");
                        return;
                }
                q = new Body(q);
                print ("RDFQ: Query is: ");
                q.print();
                print ("\n\n");
                var vs = varNames(q.list);

                // Prove the query.

                results = new ResultSet("demo_resultset_1");//somewhere to store results
                prove(renameVariables(q.list, 0), [], outr, 1, vs); // do the thinky thing...
                print ("Getting report from RDF ResultSet: \n"+ results.report() );

                htmlWindow(results.htmlreport(),query); // dump the RDFQ results to HTML table

        }



// -->


