Esempio n. 1
0
		public override void Init(VariableBinding[] variables, bool distinct, bool ordered) {
			output.WriteStartElement("sparql");
			output.WriteAttributeString("xmlns", "http://www.w3.org/2005/sparql-results#");
			output.WriteStartElement("head");
			foreach (VariableBinding var in variables) {
				if (var.Name == null) continue;
				output.WriteStartElement("variable");
				output.WriteAttributeString("name", var.Name);
				output.WriteEndElement();
			}
			output.WriteEndElement(); // head
			output.WriteStartElement("results");
			output.WriteAttributeString("ordered", ordered ? "true" : "false");
			output.WriteAttributeString("distinct", distinct ? "true" : "false");
			
			// instead of <results>, we might want <boolean>true</boolean>
		}
Esempio n. 2
0
		public override bool Add(VariableBinding[] result) {
			output.WriteStartElement("result");
			foreach (VariableBinding var in result) {
				if (var.Name == null) continue;
				
				output.WriteStartElement("binding");
				output.WriteAttributeString("name", var.Name);
				if (var.Target == null) {
					output.WriteStartElement("unbound");
					output.WriteEndElement();
				} else if (var.Target.Uri != null) {
					output.WriteElementString("uri", var.Target.Uri);
				} else if (var.Target is Literal) {
					output.WriteStartElement("literal");
					Literal literal = (Literal)var.Target;
					if (literal.DataType != null)
						output.WriteAttributeString("datatype", literal.DataType);
					if (literal.Language != null)
						output.WriteAttributeString("xml", "lang", null, literal.Language);
					output.WriteString(literal.Value);
					output.WriteEndElement();				
				} else {
					string id;
					if (blankNodes.ContainsKey(var.Target))
						id = (string)blankNodes[var.Target];
					else {
						id = "r" + (++blankNodeCounter);
						blankNodes[var.Target] = id;
					}
					output.WriteStartElement("bnode");
					output.WriteString(id);
					output.WriteEndElement();
				}
				
				output.WriteEndElement();
			}
			output.WriteEndElement();
			
			return true;
		}
Esempio n. 3
0
			public override bool Add(VariableBinding[] result) {
				output.WriteLine("<tr>");
				foreach (VariableBinding var in result) {
					if (var.Name == null) continue;
					string t = var.Target.ToString();
					if (var.Target is Literal) t = ((Literal)var.Target).Value;
					t = t.Replace("&", "&amp;");
					t = t.Replace("<", "&lt;");
					output.WriteLine("<td>" + t + "</td>");
				}
				output.WriteLine("</tr>");			
				return true;
			}
Esempio n. 4
0
			public override void Init(VariableBinding[] variables, bool distinct, bool ordered) {
				output.WriteLine("<table>");
				output.WriteLine("<tr>");
				foreach (VariableBinding var in variables) {
					if (var.Name == null) continue;
					output.WriteLine("<th>" + var.Name + "</th>");
				}
				output.WriteLine("</tr>");
			}
Esempio n. 5
0
		public override void Run(SelectableSource source, QueryResultSink resultsink) {
			if (!(query is SelectQuery))
				throw new InvalidOperationException("Only SELECT queries are supported by this method (" + query.GetType() + ").");

			// Perform the query
			SelectQuery squery = (SelectQuery)query;
			
			RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);

			MyLogicFactory logic = new MyLogicFactory();
			foreach (RdfFunction f in extFunctions)
				logic.registerExternalFunction(
					new URIWrapper(f.Uri),
					new ExtFuncWrapper(sourcewrapper, f));

			squery.bindLogic(logic);
			
			RdfBindingSet results;
			try {
				results = squery.execute(sourcewrapper);
			} catch (java.lang.Exception e) {
				throw new QueryExecutionException("Error executing query: " + e.Message, e);
			}
			
			// Prepare binding objects
			java.util.List vars = results.getVariables();
			VariableBinding[] bindings = new VariableBinding[vars.size()];
			SparqlVariable[] svars = new SparqlVariable[vars.size()];
			SemWebVariable[] vars2 = new SemWebVariable[vars.size()];
			for (int i = 0; i < bindings.Length; i++) {
				svars[i] = (SparqlVariable)vars.get(i);
				vars2[i] = new SemWebVariable(svars[i].getName());
				bindings[i] = new VariableBinding(vars2[i], null);
			}
			
			// Initialize the result sink
			resultsink.Init(bindings, false, false); // set distinct and ordered
			
			// Set the comments
			resultsink.AddComments(queryString + "\n");
			resultsink.AddComments(sourcewrapper.GetLog());

			// Iterate the bindings
			java.util.Iterator iter = results.iterator();
			long ctr = -1, ctr2 = 0;
			while (iter.hasNext()) {
				RdfBindingRow row = (RdfBindingRow)iter.next();

				// Since SPARQL processing may be lazy-delayed,
				// add any new comments that might have been logged.
				resultsink.AddComments(sourcewrapper.GetLog());

				ctr++;
			
				if (ctr < ReturnStart && ReturnStart != -1) continue;

				for (int i = 0; i < bindings.Length; i++) {
					Resource r = sourcewrapper.ToResource(row.getValue(svars[i]));
					r = sourcewrapper.Persist(r);
					bindings[i] = new VariableBinding(bindings[i].Variable, r);
				}

				resultsink.AddComments(sourcewrapper.GetLog());
				
				resultsink.Add(bindings);

				ctr2++;
				if (ctr2 >= ReturnLimit && ReturnLimit != -1) break;
			}
			
			resultsink.AddComments(sourcewrapper.GetLog());
			
			// Close the result sink.
			resultsink.Finished();
		}
Esempio n. 6
0
        public override void Run(SelectableSource source, QueryResultSink resultsink)
        {
            if (!(query is SelectQuery))
            {
                throw new InvalidOperationException("Only SELECT queries are supported by this method (" + query.GetType() + ").");
            }

            // Perform the query
            SelectQuery squery = (SelectQuery)query;

            RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);

            MyLogicFactory logic = new MyLogicFactory();

            foreach (RdfFunction f in extFunctions)
            {
                logic.registerExternalFunction(
                    new URIWrapper(f.Uri),
                    new ExtFuncWrapper(sourcewrapper, f));
            }

            squery.bindLogic(logic);

            RdfBindingSet results;

            try {
                results = squery.execute(sourcewrapper);
            } catch (java.lang.Exception e) {
                throw new QueryExecutionException("Error executing query: " + e.Message, e);
            }

            // Prepare binding objects
            java.util.List    vars     = results.getVariables();
            VariableBinding[] bindings = new VariableBinding[vars.size()];
            SparqlVariable[]  svars    = new SparqlVariable[vars.size()];
            SemWebVariable[]  vars2    = new SemWebVariable[vars.size()];
            for (int i = 0; i < bindings.Length; i++)
            {
                svars[i]    = (SparqlVariable)vars.get(i);
                vars2[i]    = new SemWebVariable(svars[i].getName());
                bindings[i] = new VariableBinding(vars2[i], null);
            }

            // Initialize the result sink
            resultsink.Init(bindings, false, false);             // set distinct and ordered

            // Set the comments
            resultsink.AddComments(queryString + "\n");
            resultsink.AddComments(sourcewrapper.GetLog());

            // Iterate the bindings
            java.util.Iterator iter = results.iterator();
            long ctr = -1, ctr2 = 0;

            while (iter.hasNext())
            {
                RdfBindingRow row = (RdfBindingRow)iter.next();

                // Since SPARQL processing may be lazy-delayed,
                // add any new comments that might have been logged.
                resultsink.AddComments(sourcewrapper.GetLog());

                ctr++;

                if (ctr < ReturnStart && ReturnStart != -1)
                {
                    continue;
                }

                for (int i = 0; i < bindings.Length; i++)
                {
                    Resource r = sourcewrapper.ToResource(row.getValue(svars[i]));
                    r           = sourcewrapper.Persist(r);
                    bindings[i] = new VariableBinding(bindings[i].Variable, r);
                }

                resultsink.AddComments(sourcewrapper.GetLog());

                resultsink.Add(bindings);

                ctr2++;
                if (ctr2 >= ReturnLimit && ReturnLimit != -1)
                {
                    break;
                }
            }

            resultsink.AddComments(sourcewrapper.GetLog());

            // Close the result sink.
            resultsink.Finished();
        }
Esempio n. 7
0
		public static Statement Substitute(VariableBinding[] variables, Statement template) {
			// This may throw an InvalidCastException if a variable binds
			// to a literal but was used as the subject, predicate, or meta
			// of the template.
			foreach (VariableBinding v in variables) {
				if (v.Variable == template.Subject) template = new Statement((Entity)v.Target, template.Predicate, template.Object, template.Meta);
				if (v.Variable == template.Predicate) template = new Statement(template.Subject, (Entity)v.Target, template.Object, template.Meta);
				if (v.Variable == template.Object) template = new Statement(template.Subject, template.Predicate, v.Target, template.Meta);
				if (v.Variable == template.Meta) template = new Statement(template.Subject, template.Predicate, template.Object, (Entity)v.Target);
			}
			return template;
		}
Esempio n. 8
0
		public override bool Add(VariableBinding[] result) {
			Bindings.Add(result.Clone());
			return true;
		}
Esempio n. 9
0
		public abstract bool Add(VariableBinding[] result);
Esempio n. 10
0
		public virtual void Init(VariableBinding[] variables, bool distinct, bool ordered) {
		}
Esempio n. 11
0
		public override void Run(SelectableSource targetModel, QueryResultSink result) {
			CheckInit();
			
			foreach (Statement s in novariablestatements)
				if (!targetModel.Contains(s))
					return;
			
			VariableBinding[] finalbindings = new VariableBinding[variables.Length];
			for (int i = 0; i < variables.Length; i++)
				finalbindings[i].Variable = variableEntities[i];
			
			result.Init(finalbindings, true, false);
			
			Debug("Begnning Query");
			
			BindingSet bindings = new BindingSet(this);
			for (int group = 0; group < statements.Length; group++) {
				bool ret = Query(group, bindings, targetModel);
				if (!ret) {
					// A false return value indicates the query
					// certainly failed -- a non-optional statement
					// failed to match at all.
					result.Finished();
					return;
				}
			}

			int ctr = -1;
			foreach (QueryResult r in bindings.Results) {
				Permutation permutation = new Permutation(r.Bindings);
				do {
					ctr++;
					if (ctr < ReturnStart) continue;
					for (int i = 0; i < variables.Length; i++)
						finalbindings[i].Target = permutation[i];
					result.Add(finalbindings);
					if (ReturnLimit != -1 && ctr == ReturnStart+ReturnLimit) break;	
				} while (permutation.Next());
				if (ReturnLimit != -1 && ctr == ReturnStart+ReturnLimit) break;	
			}

			
			result.Finished();
		}
Esempio n. 12
0
        public override void Run(SelectableSource targetModel, QueryResultSink result)
        {
            CheckInit();

            foreach (Statement s in novariablestatements)
            {
                if (!targetModel.Contains(s))
                {
                    return;
                }
            }

            VariableBinding[] finalbindings = new VariableBinding[variables.Length];
            for (int i = 0; i < variables.Length; i++)
            {
                finalbindings[i].Variable = variableEntities[i];
            }

            result.Init(finalbindings, true, false);

            Debug("Begnning Query");

            BindingSet bindings = new BindingSet(this);

            for (int group = 0; group < statements.Length; group++)
            {
                bool ret = Query(group, bindings, targetModel);
                if (!ret)
                {
                    // A false return value indicates the query
                    // certainly failed -- a non-optional statement
                    // failed to match at all.
                    result.Finished();
                    return;
                }
            }

            int ctr = -1;

            foreach (QueryResult r in bindings.Results)
            {
                Permutation permutation = new Permutation(r.Bindings);
                do
                {
                    ctr++;
                    if (ctr < ReturnStart)
                    {
                        continue;
                    }
                    for (int i = 0; i < variables.Length; i++)
                    {
                        finalbindings[i].Target = permutation[i];
                    }
                    result.Add(finalbindings);
                    if (ReturnLimit != -1 && ctr == ReturnStart + ReturnLimit)
                    {
                        break;
                    }
                } while (permutation.Next());
                if (ReturnLimit != -1 && ctr == ReturnStart + ReturnLimit)
                {
                    break;
                }
            }


            result.Finished();
        }