Exemplo n.º 1
0
        void WriteGraph(RdfGraph graph, RdfSourceWrapper sourcewrapper, StatementSink sink)
        {
            java.util.Iterator iter = graph.iterator();
            while (iter.hasNext())
            {
                GraphStatement stmt = (GraphStatement)iter.next();
                Statement      s;
                if (stmt is GraphStatementWrapper)
                {
                    s = ((GraphStatementWrapper)stmt).s;
                }
                else
                {
                    s = new Statement(
                        sourcewrapper.ToEntity(stmt.getSubject()),
                        sourcewrapper.ToEntity(stmt.getPredicate()),
                        sourcewrapper.ToResource(stmt.getObject()),
                        stmt.getGraphName() == null ? Statement.DefaultMeta : sourcewrapper.ToEntity(stmt.getGraphName()));
                }

                if (s.AnyNull)
                {
                    continue;                            // unbound variable, or literal in bad position
                }
                sink.Add(s);
            }
        }
Exemplo n.º 2
0
 public org.openrdf.model.Value evaluate(org.openrdf.model.Value[] args)
 {
     try {
         Resource ret = func.Evaluate(source.ToResources(args));
         return(RdfSourceWrapper.Wrap(ret));
     } catch (Exception e) {
         throw new name.levering.ryan.sparql.logic.function.ExternalFunctionException(e);
     }
 }
Exemplo n.º 3
0
        public void Describe(SelectableSource source, StatementSink sink)
        {
            if (!(query is DescribeQuery))
            {
                throw new InvalidOperationException("Only DESCRIBE queries are supported by this method (" + query.GetType() + ").");
            }
            DescribeQuery    q             = (DescribeQuery)query;
            RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);
            RdfGraph         graph         = q.execute(sourcewrapper);

            WriteGraph(graph, sourcewrapper, sink);
        }
Exemplo n.º 4
0
        public void Construct(SelectableSource source, StatementSink sink)
        {
            if (!(query is ConstructQuery))
            {
                throw new InvalidOperationException("Only CONSTRUCT queries are supported by this method (" + query.GetType() + ").");
            }
            ConstructQuery   q             = (ConstructQuery)query;
            RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);
            RdfGraph         graph         = q.execute(sourcewrapper);

            WriteGraph(graph, sourcewrapper, sink);
        }
Exemplo n.º 5
0
            Resource ToRes(object expr, java.util.Map knownValues, bool entities, Hashtable varMap1, Hashtable varMap2, RdfSourceWrapper src, QueryOptions opts, VariableList distinguishedVars, VariableList undistinguishedVars, java.util.Set sparqlDistinguished)
            {
                if (expr is SparqlVariable) {
                    Variable v;
                    if (varMap1.ContainsKey(expr)) {
                        v = (Variable)varMap1[expr];
                    } else {
                        v = new Variable(expr.ToString());
                        varMap1[expr] = v;
                        varMap2[v] = expr;

                        if (knownValues != null && knownValues.get(expr) != null) {
                            java.util.Set values = (java.util.Set)knownValues.get(expr);
                            VarKnownValuesList values2 = new VarKnownValuesList();
                            for (java.util.Iterator iter = values.iterator(); iter.hasNext(); ) {
                                Resource r = src.ToResource((name.levering.ryan.sparql.common.Value)iter.next());
                                if (r != null)
                                    values2.Add(r);
                            }

                            opts.VariableKnownValues[v] = values2;
                        }

                        if (sparqlDistinguished != null && sparqlDistinguished.contains(expr))
                            distinguishedVars.Add(v);
                        else
                            undistinguishedVars.Add(v);
                    }
                    return v;
                }

                return entities ? src.ToEntity((name.levering.ryan.sparql.common.Value)expr) : src.ToResource((name.levering.ryan.sparql.common.Value)expr);
            }
Exemplo n.º 6
0
 public ExtFuncWrapper(RdfSourceWrapper s, RdfFunction f)
 {
     source = s;
     func = f;
 }
Exemplo n.º 7
0
        void WriteGraph(RdfGraph graph, RdfSourceWrapper sourcewrapper, StatementSink sink)
        {
            if (sink is RdfWriter)
                ((RdfWriter)sink).Namespaces.AddFrom(GetQueryPrefixes());

            java.util.Iterator iter = graph.iterator();
            while (iter.hasNext()) {
                GraphStatement stmt = (GraphStatement)iter.next();
                Statement s;
                if (stmt is GraphStatementWrapper) {
                    s = ((GraphStatementWrapper)stmt).s;
                } else {
                    s = new Statement(
                        sourcewrapper.ToEntity(stmt.getSubject()),
                        sourcewrapper.ToEntity(stmt.getPredicate()),
                        sourcewrapper.ToResource(stmt.getObject()),
                        stmt.getGraphName() == null ? Statement.DefaultMeta : sourcewrapper.ToEntity(stmt.getGraphName()));
                }

                if (s.AnyNull) continue; // unbound variable, or literal in bad position
                sink.Add(s);
            }
        }
Exemplo n.º 8
0
        /* INTERNAL METHODS TO CONTROL QUERY EXECUTION */
        private RdfSourceWrapper BindLogic(SelectableSource source)
        {
            RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, this);

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

            query.prepare(sourcewrapper, logic);

            return sourcewrapper;
        }
Exemplo n.º 9
0
 public StatementIterator(SelectableSource source, SelectFilter filter, RdfSourceWrapper wrapper)
 {
     this.source = source;
     this.filter = filter;
     this.wrapper = wrapper;
     this.wantMetas = true;
 }
Exemplo n.º 10
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();
		}
Exemplo n.º 11
0
		public void Describe(SelectableSource source, StatementSink sink) {
			if (!(query is DescribeQuery))
				throw new InvalidOperationException("Only DESCRIBE queries are supported by this method (" + query.GetType() + ").");
			DescribeQuery q = (DescribeQuery)query;
			RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);
			RdfGraph graph = q.execute(sourcewrapper);
			WriteGraph(graph, sourcewrapper, sink);
		}
Exemplo n.º 12
0
		public void Construct(SelectableSource source, StatementSink sink) {
			if (!(query is ConstructQuery))
				throw new InvalidOperationException("Only CONSTRUCT queries are supported by this method (" + query.GetType() + ").");
			ConstructQuery q = (ConstructQuery)query;
			RdfSourceWrapper sourcewrapper = new RdfSourceWrapper(source, QueryMeta, this);
			RdfGraph graph = q.execute(sourcewrapper);
			WriteGraph(graph, sourcewrapper, sink);
		}
Exemplo n.º 13
0
 public ExtFuncWrapper(RdfSourceWrapper s, RdfFunction f)
 {
     source = s;
     func   = f;
 }
Exemplo n.º 14
0
 public org.openrdf.model.Value getObject()
 {
     return(RdfSourceWrapper.Wrap(s.Object));
 }
Exemplo n.º 15
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();
        }