public object Search(queryrequest xml)
        {
            SemWeb.Query.Query query = null;

            string q = string.Empty;

            try
            {
                query = new SparqlEngine(new StringReader(xml.query));
            }
            catch (QueryFormatException ex)
            {
                var malformed = new malformedquery();

                malformed.faultdetails = ex.Message;

                return(malformed);
            }

            // Load the data from sql server
            SemWeb.Stores.SQLStore store;

            string connstr = ConfigurationManager.ConnectionStrings["SemWebDB"].ConnectionString;

            DebugLogging.Log(connstr);

            store = (SemWeb.Stores.SQLStore)SemWeb.Store.CreateForInput(connstr);

            //Create a Sink for the results to be writen once the query is run.
            MemoryStream    ms     = new MemoryStream();
            XmlTextWriter   writer = new XmlTextWriter(ms, System.Text.Encoding.UTF8);
            QueryResultSink sink   = new SparqlXmlQuerySink(writer);

            try
            {
                // Run the query.
                query.Run(store, sink);
            }
            catch (Exception ex)
            {
                // Run the query.
                query.Run(store, sink);
                DebugLogging.Log("Run the query a second time");
                DebugLogging.Log(ex.Message);
            }
            //flush the writer then  load the memory stream
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            //Write the memory stream out to the response.
            ASCIIEncoding ascii = new ASCIIEncoding();

            DebugLogging.Log(ascii.GetString(ms.ToArray()).Replace("???", ""));
            writer.Close();

            DebugLogging.Log("End of Processing");

            return(SerializeXML.DeserializeObject(ascii.GetString(ms.ToArray()).Replace("???", ""), typeof(sparql)) as sparql);
        }
Example #2
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());
    }
Example #3
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);
    }
Example #4
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();
        }
    }