Exemplo n.º 1
0
        public void InteropSemWebGraphQuery()
        {
            Graph g = new Graph();

            FileLoader.Load(g, "InferenceTest.ttl");
            GraphSource source = new GraphSource(g);

            Console.WriteLine("Query for Cars and their Speeds");
            Console.WriteLine();
            SemWeb.Query.SparqlXmlQuerySink sink = new SemWeb.Query.SparqlXmlQuerySink(Console.Out);
            Variable car = new Variable("car");

            Statement[] graphPattern = new Statement[]
            {
                new Statement(car, new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car")),
                new Statement(car, new Entity("http://example.org/vehicles/Speed"), new Variable("speed"))
            };

            source.Query(graphPattern, new SemWeb.Query.QueryOptions(), sink);

            Console.WriteLine("Query for the 1st Car and it's Speed");
            Console.WriteLine();
            sink         = new SemWeb.Query.SparqlXmlQuerySink(Console.Out);
            graphPattern = new Statement[]
            {
                new Statement(car, new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car")),
                new Statement(car, new Entity("http://example.org/vehicles/Speed"), new Variable("speed"))
            };
            SemWeb.Query.QueryOptions options = new SemWeb.Query.QueryOptions();
            options.Limit = 1;
            source.Query(graphPattern, options, sink);
        }
Exemplo n.º 2
0
        public override void Query(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource targetModel, QueryResultSink result)
        {
            SemWeb.Query.GraphMatch q = new SemWeb.Query.GraphMatch();
            foreach (Statement s in graph)
            {
                q.AddGraphStatement(s);
            }

            q.ReturnLimit = options.Limit;

            if (options.VariableKnownValues != null)
            {
                        #if !DOTNET2
                foreach (DictionaryEntry ent in options.VariableKnownValues)
                {
                    q.SetVariableRange((Variable)ent.Key, (ICollection)ent.Value);
                }
                        #else
                foreach (KeyValuePair <Variable, ICollection <Resource> > ent in options.VariableKnownValues)
                {
                    q.SetVariableRange(ent.Key, ent.Value);
                }
                        #endif
            }

            if (options.VariableLiteralFilters != null)
            {
                        #if !DOTNET2
                foreach (DictionaryEntry ent in options.VariableLiteralFilters)
                {
                    foreach (LiteralFilter filter in (ICollection)ent.Value)
                    {
                        q.AddLiteralFilter((Variable)ent.Key, filter);
                    }
                }
                        #else
                foreach (KeyValuePair <Variable, ICollection <LiteralFilter> > ent in options.VariableLiteralFilters)
                {
                    foreach (LiteralFilter filter in ent.Value)
                    {
                        q.AddLiteralFilter(ent.Key, filter);
                    }
                }
                        #endif
            }

            if (options.DistinguishedVariables != null)
            {
                foreach (Variable v in options.DistinguishedVariables)
                {
                    q.SetDistinguishedVariable(v);
                }
            }

            q.Run(targetModel, result);
        }
Exemplo n.º 3
0
    public static void Main(string[] args)
    {
        System.Net.ServicePointManager.Expect100Continue = false;

        Opts opts = new Opts();

        opts.ProcessArgs(args);

        if (opts.RemainingArguments.Length != 1)
        {
            opts.DoHelp();
            return;
        }

        string baseuri = "query://query/#";

        QueryResultSink qs;

        if (opts.format == "simple")
        {
            qs = new PrintQuerySink();
        }
        else if (opts.format == "html")
        {
            qs = new HTMLQuerySink(Console.Out);
        }
        else if (opts.format == "xml")
        {
            qs = new SparqlXmlQuerySink(Console.Out);
        }
        else if (opts.format == "lubm")
        {
            qs = new LUBMReferenceAnswerOutputQuerySink();
        }
        else if (opts.format == "csv")
        {
            qs = new CSVQuerySink();
        }
        else
        {
            Console.Error.WriteLine("Invalid output format.");
            return;
        }

        Query query;

        MemoryStore queryModel = null;

                #if !DOTNET2
        System.Collections.ICollection queryModelVars = null;
                #else
        System.Collections.Generic.ICollection <Variable> queryModelVars = null;
                #endif

        Store model = Store.Create(opts.RemainingArguments[0]);

        if (opts.type == "rsquary")
        {
            RdfReader queryparser = RdfReader.Create("n3", "-");
            queryparser.BaseUri = baseuri;
            queryModel          = new MemoryStore(queryparser);
            queryModelVars      = queryparser.Variables;
            query = new GraphMatch(queryModel);
        }
        else if (opts.type == "sparql" && model.DataSources.Count == 1 && model.DataSources[0] is SemWeb.Remote.SparqlSource)
        {
            string querystring = Console.In.ReadToEnd();
            ((SemWeb.Remote.SparqlSource)model.DataSources[0]).RunSparqlQuery(querystring, Console.Out);
            return;
        }
        else if (opts.type == "sparql")
        {
            string querystring = Console.In.ReadToEnd();
            query = new SparqlEngine(querystring);
        }
        else
        {
            throw new Exception("Invalid query format: " + opts.type);
        }

        if (opts.limit > 0)
        {
            query.ReturnLimit = opts.limit;
        }

        //Console.Error.WriteLine(query.GetExplanation());

        if (query is SparqlEngine && ((SparqlEngine)query).Type != SparqlEngine.QueryType.Select)
        {
            SparqlEngine sparql = (SparqlEngine)query;
            sparql.Run(model, Console.Out);
        }
        else if (model is QueryableSource && queryModel != null)
        {
            SemWeb.Query.QueryOptions qopts = new SemWeb.Query.QueryOptions();
            qopts.DistinguishedVariables = queryModelVars;

            // Replace bnodes in the query with Variables
            int bnodectr = 0;
            foreach (Entity e in queryModel.GetEntities())
            {
                if (e is BNode && !(e is Variable))
                {
                    BNode b = (BNode)e;
                    queryModel.Replace(e, new Variable(b.LocalName != null ? b.LocalName : "bnodevar" + (++bnodectr)));
                }
            }

            model.Query(queryModel.ToArray(), qopts, qs);
        }
        else
        {
            query.Run(model, qs);
        }

        if (qs is IDisposable)
        {
            ((IDisposable)qs).Dispose();
        }
    }
Exemplo n.º 4
0
        public override MetaQueryResult MetaQuery(Statement[] graph, SemWeb.Query.QueryOptions options, SelectableSource source)
        {
            SemWeb.Query.MetaQueryResult ret = new SemWeb.Query.MetaQueryResult();

            ret.QuerySupported = true;

            ret.NoData = new bool[graph.Length];
            for (int i = 0; i < graph.Length; i++)
            {
                // Take this statement and replace variables by nulls
                // to make it a statement template.
                Statement st = graph[i];
                for (int j = 0; j < 4; j++)
                {
                    if (st.GetComponent(j) is Variable)
                    {
                        st.SetComponent(j, null);
                    }
                }

                // See if the store contains this template.
                if (st != Statement.All && !source.Contains(st))
                {
                    ret.NoData[i] = true;
                    continue;
                }

                // Process it further in case we have variables
                // with known values, in which case if none of the
                // known values is in the store, we also know this
                // statement is unanswerable.
                for (int j = 0; j < 4; j++)
                {
                    Resource r = graph[i].GetComponent(j);

                    // No need to check the following given the check above.
                    //if (r != null && !(r is Variable) && !source.Contains(r))
                    //	ret.NoData[i] = true;

                    if (r != null && r is Variable && options.VariableKnownValues != null &&
                                        #if !DOTNET2
                        options.VariableKnownValues.Contains((Variable)r)
                                        #else
                        options.VariableKnownValues.ContainsKey((Variable)r)
                                        #endif
                        )
                    {
                        bool found = false;
                                                #if !DOTNET2
                        foreach (Resource s in (ICollection)options.VariableKnownValues[(Variable)r])
                        {
                                                #else
                        foreach (Resource s in (ICollection <Resource>)options.VariableKnownValues[(Variable)r])
                        {
                                                #endif
                            if (source.Contains(s))
                            {
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            ret.NoData[i] = true;
                        }
                    }
                }
            }

            return(ret);
        }
        public void InteropSemWebGraphQuery() 
        {
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            GraphSource source = new GraphSource(g);

            Console.WriteLine("Query for Cars and their Speeds");
            Console.WriteLine();
            SemWeb.Query.SparqlXmlQuerySink sink = new SemWeb.Query.SparqlXmlQuerySink(Console.Out);
            Variable car = new Variable("car");
            Statement[] graphPattern = new Statement[] 
            {
                new Statement(car, new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car")),
                new Statement(car, new Entity("http://example.org/vehicles/Speed"), new Variable("speed"))
            };

            source.Query(graphPattern, new SemWeb.Query.QueryOptions(), sink);

            Console.WriteLine("Query for the 1st Car and it's Speed");
            Console.WriteLine();
            sink = new SemWeb.Query.SparqlXmlQuerySink(Console.Out);
            graphPattern = new Statement[] 
            {
                new Statement(car, new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car")),
                new Statement(car, new Entity("http://example.org/vehicles/Speed"), new Variable("speed"))
            };
            SemWeb.Query.QueryOptions options = new SemWeb.Query.QueryOptions();
            options.Limit = 1;
            source.Query(graphPattern, options, sink);
        }
Exemplo n.º 6
0
    public static void Main(string[] args)
    {
        System.Net.ServicePointManager.Expect100Continue = false;

        Opts opts = new Opts();
        opts.ProcessArgs(args);

        if (opts.RemainingArguments.Length != 1) {
            opts.DoHelp();
            return;
        }

        string baseuri = "query://query/#";

        QueryResultSink qs;
        if (opts.format == "simple")
            qs = new PrintQuerySink();
        else if (opts.format == "html")
            qs = new HTMLQuerySink(Console.Out);
        else if (opts.format == "xml")
            qs = new SparqlXmlQuerySink(Console.Out);
        else if (opts.format == "lubm")
            qs = new LUBMReferenceAnswerOutputQuerySink();
        else if (opts.format == "csv")
            qs = new CSVQuerySink();
        else {
            Console.Error.WriteLine("Invalid output format.");
            return;
        }

        Query query;

        MemoryStore queryModel = null;
        #if !DOTNET2
        System.Collections.ICollection queryModelVars = null;
        #else
        System.Collections.Generic.ICollection<Variable> queryModelVars = null;
        #endif

        Store model = Store.Create(opts.RemainingArguments[0]);

        if (opts.type == "rsquary") {
            RdfReader queryparser = RdfReader.Create("n3", "-");
            queryparser.BaseUri = baseuri;
            queryModel = new MemoryStore(queryparser);
            queryModelVars = queryparser.Variables;
            query = new GraphMatch(queryModel);
        } else if (opts.type == "sparql" && model.DataSources.Count == 1 && model.DataSources[0] is SemWeb.Remote.SparqlSource) {
            string querystring = Console.In.ReadToEnd();
            ((SemWeb.Remote.SparqlSource)model.DataSources[0]).RunSparqlQuery(querystring, Console.Out);
            return;
        } else if (opts.type == "sparql") {
            string querystring = Console.In.ReadToEnd();
            query = new SparqlEngine(querystring);
        } else {
            throw new Exception("Invalid query format: " + opts.type);
        }

        if (opts.limit > 0)
            query.ReturnLimit = opts.limit;

        //Console.Error.WriteLine(query.GetExplanation());

        if (query is SparqlEngine && ((SparqlEngine)query).Type != SparqlEngine.QueryType.Select) {
            SparqlEngine sparql = (SparqlEngine)query;
            sparql.Run(model, Console.Out);
        } else if (model is QueryableSource && queryModel != null) {
            SemWeb.Query.QueryOptions qopts = new SemWeb.Query.QueryOptions();
            qopts.DistinguishedVariables = queryModelVars;

            // Replace bnodes in the query with Variables
            int bnodectr = 0;
            foreach (Entity e in queryModel.GetEntities()) {
                if (e is BNode && !(e is Variable)) {
                    BNode b = (BNode)e;
                    queryModel.Replace(e, new Variable(b.LocalName != null ? b.LocalName : "bnodevar" + (++bnodectr)));
                }
            }

            model.Query(queryModel.ToArray(), qopts, qs);
        } else {
            query.Run(model, qs);
        }

        if (qs is IDisposable)
            ((IDisposable)qs).Dispose();
    }