Exemple #1
0
    public static void Main(string[] argv)
    {
        if (argv.Length < 3)
        {
            Console.WriteLine("Usage: query.exe format queryfile datafile");
            return;
        }

        string format    = argv[0];
        string queryfile = argv[1];
        string datafile  = argv[2];

        Query query;

        if (format == "rsquary")
        {
            // Create a simple-entailment "RSquary" query
            // from the N3 file.
            query = new GraphMatch(new N3Reader(queryfile));
        }
        else
        {
            // Create a SPARQL query by reading the file's
            // contents.
            query = new SparqlEngine(new StreamReader(queryfile));
        }

        // Load the data file from disk
        MemoryStore data = new MemoryStore();

        data.Import(new N3Reader(datafile));

        // First, print results in SPARQL XML Results format...

        // Create a result sink where results are written to.
        QueryResultSink sink = new SparqlXmlQuerySink(Console.Out);

        // Run the query.
        query.Run(data, sink);

        // Second, print the results via our own custom QueryResultSink...
        query.Run(data, new PrintQuerySink());
    }
Exemple #2
0
    protected override void OnLoad(EventArgs e)
    {
        RdfStore = new MemoryStore();
        RdfStore.Import(
            new RdfXmlReader(@"c:\temp\_1.rdf"));

        string depRules = @"
@prefix n: <urn:schemas-nreco:metadata:terms#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.

{ ?a n:dependentFrom ?b . ?b n:dependentFrom ?c .} => {?a n:dependentFrom ?c}.
{ ?a n:dependentFrom ?b } => { ?b n:usedBy ?a}.
{ ?a n:usedBy ?b . ?b n:usedBy ?c .} => {?a n:usedBy ?c}.
";

        Euler engine = new Euler(new N3Reader(new StringReader(depRules)));

        RdfStore.AddReasoner(new RDFS(RdfStore));
        RdfStore.AddReasoner(engine);

        string rdfQuery = @"
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix p: <urn:schemas-nreco:metadata:dotnet:property#>.
@prefix t: <urn:schemas-nreco:metadata:dotnet:type#>.
@prefix w: <file:///d:/Vitalik/GoogleCode/NReco/examples/NReco.Examples.WebApp/web/#>.
@prefix cso: <http://cos.ontoware.org/cso#>.
@prefix n: <urn:schemas-nreco:metadata:terms#>.
w:db n:usedBy ?t2.
";

        Query           query = new GraphMatch(new N3Reader(new StringReader(rdfQuery)));
        StringWriter    wr    = new StringWriter();
        QueryResultSink sink  = new SparqlXmlQuerySink(wr);

        query.Run(RdfStore, sink);
        Result = wr.ToString();

        base.OnLoad(e);
    }
Exemple #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();
        }
    }
        public static void MakeLean(Store store, SelectableSource relativeTo, StatementSink removed)
        {
            // Break the data source into MSGs.  Make each MSG
            // lean first (in isolation).  Then check each lean MSG
            // to see if it's already entailed by the whole store,
            // or by relativeTo if it's provided (not null).

            MSG.Graph[] msgs = MSG.FindMSGs(store, true);

            foreach (MSG.Graph msgg in msgs)
            {
                // Load the MSG into memory.
                MemoryStore msg = new MemoryStore(msgg);                 // unnecessary duplication...

                // Make this MSG lean.  The "right" thing to do is
                // to consider all of the 'connected' subgraphs of MSG
                // against the whole store, rather than the MSG in
                // isolation.  But that gets much too expensive.
                MemoryStore msgremoved = new MemoryStore();
                MakeLeanMSG(msg, msgg.GetBNodes(), msgremoved);

                // Whatever was removed from msg, remove it from the main graph.
                store.RemoveAll(msgremoved.ToArray());

                // And track what was removed.
                if (removed != null)
                {
                    msgremoved.Select(removed);
                }

                // If this MSG is now (somehow) empty (shouldn't happen,
                // but one never knows), don't test for entailment.
                if (msg.StatementCount == 0)
                {
                    continue;
                }

                // Remove this MSG if it is already entailed.

                // The GraphMatch will treat all blank nodes in
                // msg as variables.
                GraphMatch            match = new GraphMatch(msg);
                QueryResultBufferSink sink  = new QueryResultBufferSink();
                match.Run(new SubtractionSource(store, msg), sink);
                if (sink.Bindings.Count > 0)
                {
                    // This MSG can be removed.
                    store.RemoveAll(msg.ToArray());
                    if (removed != null)
                    {
                        msg.Select(removed);
                    }
                }
                else if (relativeTo != null)
                {
                    match.Run(relativeTo, sink);
                    if (sink.Bindings.Count > 0)
                    {
                        // This MSG can be removed.
                        store.RemoveAll(msg.ToArray());
                        if (removed != null)
                        {
                            msg.Select(removed);
                        }
                    }
                }
            }
        }