Esempio n. 1
0
    static void RunManifestFile(string manifestfile)
    {
        // Load the manifest file
        manifestfile = Path.GetDirectoryName(manifestfile) + "/"; // for good measure
        MemoryStore manifest = new MemoryStore();
        using (RdfReader m = new N3Reader(manifestfile + "/manifest.ttl")) {
            m.BaseUri = manifestfile;
            manifest.Import(m);
        }

        // Declare some resources

        Entity rdf_first = "http://www.w3.org/1999/02/22-rdf-syntax-ns#first";
        Entity rdf_rest = "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest";
        Entity rdf_nil = "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil";

        Entity mf_entries = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#entries";

        // Get the start of the entries list.
        Entity entries_root = (Entity)manifest.SelectObjects(manifestfile, mf_entries)[0];

        // Loop through the tests.
        while (true) {
            Entity test = (Entity)manifest.SelectObjects(entries_root, rdf_first)[0];
            RunTest(test, manifest);

            entries_root = (Entity)manifest.SelectObjects(entries_root, rdf_rest)[0];
            if (entries_root == rdf_nil)
                break;
        }
    }
Esempio n. 2
0
        public static RdfReader LoadFromUri(Uri webresource)
        {
            // TODO: Add Accept header for HTTP resources.

            System.Net.WebRequest  rq   = System.Net.WebRequest.Create(webresource);
            System.Net.WebResponse resp = rq.GetResponse();

            string mimetype = resp.ContentType;

            if (mimetype.IndexOf(';') > -1)
            {
                mimetype = mimetype.Substring(0, mimetype.IndexOf(';'));
            }

            mimetype = NormalizeMimeType(mimetype.Trim());

            RdfReader reader;

            if (mimetype == "xml" || mimetype == "application/rss+xml")
            {
                reader = new RdfXmlReader(resp.GetResponseStream());
            }

            else if (mimetype == "n3" || mimetype == "turtle")
            {
                reader = new N3Reader(new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8));
            }

            else if (webresource.LocalPath.EndsWith(".rdf") || webresource.LocalPath.EndsWith(".xml") || webresource.LocalPath.EndsWith(".rss"))
            {
                reader = new RdfXmlReader(resp.GetResponseStream());
            }

            else if (webresource.LocalPath.EndsWith(".n3") || webresource.LocalPath.EndsWith(".ttl") || webresource.LocalPath.EndsWith(".nt"))
            {
                reader = new N3Reader(new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8));
            }

            else
            {
                throw new InvalidOperationException("Could not determine the RDF format of the resource.");
            }

            reader.BaseUri = resp.ResponseUri.ToString();

            return(reader);
        }
Esempio n. 3
0
    public static void Main(string[] args)
    {
        if (args.Length < 2) {
            Console.WriteLine("Usage: euler.exe axioms.n3 axioms... {questions.n3 | -sparql query.sparql}");
            return;
        }

        // Load Axioms

        bool sparql = false;

        MemoryStore axioms = new MemoryStore();
        for (int i = 0; i < args.Length-1; i++) {
            if (i > 0 && i == args.Length-2 && args[i] == "-sparql") {
                sparql = true;
                break;
            }

            N3Reader axiomsreader = new N3Reader(args[i]);
            axiomsreader.BaseUri = "http://www.example.org/arbitrary/base#";
            axioms.Import(axiomsreader);
        }

        Euler engine = new Euler(axioms);

        // Load question
        if (!sparql) {
            MemoryStore question = new MemoryStore();
            question.Import(new N3Reader(args[args.Length-1]));

            Proof[] proofs = engine.Prove(null, question.ToArray());

            foreach (Proof p in proofs) {
                Console.WriteLine(p.ToString());
            }
        } else {
            using (StreamReader fs = new StreamReader(args[args.Length-1])) {
                string q = fs.ReadToEnd();

                Store store = new Store();
                store.AddReasoner(engine);

                SparqlEngine s = new SparqlEngine(q);
                s.Run(store, Console.Out);
            }
        }
    }
Esempio n. 4
0
 public ArtifactStore(string storeLocation)
 {
     string ontology = "";
     string data = "";
     store = new MemoryStore();
     ontology = Read(Constants.OntologyUri);
     store.AddReasoner(new Euler(new N3Reader(new StringReader(ontology))));
     if (File.Exists(storeLocation))
     {
         switch (GetStoreFormat(storeLocation))
         {
             case StoreFormat.N3:
                 data = File.ReadAllText(storeLocation);
                 N3Reader reader = new N3Reader(new StringReader(data));
                 store.Import(reader);
                 break;
             case StoreFormat.RDF:
                 data = File.ReadAllText(storeLocation);
                 store.Import(new RdfXmlReader(new StringReader(data)));
                 break;
         }
     }
 }
Esempio n. 5
0
    public static void Main(string[] args)
    {
        if (args.Length != 2) {
            Console.WriteLine("runtests.exe basepath manifestfile");
            return;
        }

        basepath = args[0];
        string manifestfile = Path.Combine(basepath, args[1]);

        RdfReader reader = new RdfXmlReader(manifestfile);
        if (!manifestfile.EndsWith(".rdf"))
            reader = new N3Reader(manifestfile);
        reader.BaseUri = "http://www.example.org/";

        MemoryStore testlist = new MemoryStore(reader);

        // RDF/XML TESTS

        foreach (Entity test in testlist.GetEntitiesOfType("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#PositiveParserTest"))
            RunTest(testlist, test, true, 0);
        foreach (Entity test in testlist.GetEntitiesOfType("http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#NegativeParserTest"))
            RunTest(testlist, test, false, 0);

        // N3 TESTS

        foreach (Entity test in testlist.GetEntitiesOfType("http://www.w3.org/2004/11/n3test#PositiveParserTest"))
            RunTest(testlist, test, true, 1);
        foreach (Entity test in testlist.GetEntitiesOfType("http://www.w3.org/2004/11/n3test#NegativeParserTest"))
            RunTest(testlist, test, false, 1);

        Console.WriteLine("Total Tests:\t{0}", total);
        Console.WriteLine("Total Failures:\t{0} ({1}%)", badfail+badpass, (int)(100*(float)(badpass+badfail)/total));
        Console.WriteLine("Positive Fails:\t{0}", badfail);
        Console.WriteLine("Negative Fails:\t{0}", badpass);
        Console.WriteLine("Test Errors:\t{0}", error);
    }
Esempio n. 6
0
		public static RdfReader LoadFromUri(Uri webresource) {
			// TODO: Add Accept header for HTTP resources.
			
			System.Net.WebRequest rq = System.Net.WebRequest.Create(webresource);
			System.Net.WebResponse resp = rq.GetResponse();
			
			string mimetype = resp.ContentType;
			if (mimetype.IndexOf(';') > -1)
				mimetype = mimetype.Substring(0, mimetype.IndexOf(';'));
				
			mimetype = NormalizeMimeType(mimetype.Trim());
			
			RdfReader reader;
			
			if (mimetype == "xml" || mimetype == "application/rss+xml")
				reader = new RdfXmlReader(resp.GetResponseStream());
					
			else if (mimetype == "n3")
				reader = new N3Reader(new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8));
			
			else if (webresource.LocalPath.EndsWith(".rdf") || webresource.LocalPath.EndsWith(".xml") || webresource.LocalPath.EndsWith(".rss"))
				reader = new RdfXmlReader(resp.GetResponseStream());
			
			else if (webresource.LocalPath.EndsWith(".n3") || webresource.LocalPath.EndsWith(".ttl") || webresource.LocalPath.EndsWith(".nt"))
				reader = new N3Reader(new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8));

			else
				throw new InvalidOperationException("Could not determine the RDF format of the resource.");
				
			reader.BaseUri = resp.ResponseUri.ToString();
			
			return reader;
		}
Esempio n. 7
0
    static void RunTest(Entity test, Store manifest)
    {
        Entity rdf_type = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
        Entity mf_PositiveSyntaxTest = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#PositiveSyntaxTest";
        Entity mf_NegativeSyntaxTest = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#NegativeSyntaxTest";
        Entity mf_QueryTest = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#QueryEvaluationTest";
        Entity mf_action = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action";
        Entity mf_result = "http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#result";
        Entity qt_data = "http://www.w3.org/2001/sw/DataAccess/tests/test-query#data";
        Entity qt_query = "http://www.w3.org/2001/sw/DataAccess/tests/test-query#query";

        Entity test_type = (Entity)manifest.SelectObjects(test, rdf_type)[0];
        Entity action = (Entity)manifest.SelectObjects(test, mf_action)[0];

        if (test_type == mf_PositiveSyntaxTest || test_type == mf_NegativeSyntaxTest) {
            // The action is a query.

            // Load the action as a string.
            string q = ReadFile(action.Uri);

            // Run the action.
            try {
                new SparqlEngine(q);
            } catch (SemWeb.Query.QueryFormatException qfe) {
                // On a negative test: Good!
                if (test_type == mf_NegativeSyntaxTest) {
                    pass++;
                    return;
                }

                Console.WriteLine("Test Failed: " + action);
                Console.WriteLine(qfe.Message);
                Console.WriteLine(q);
                Console.WriteLine();
                fail++;
                return;
            }

            // On a positive test: Good!
            if (test_type == mf_PositiveSyntaxTest) {
                pass++;
                return;
            }

            Console.WriteLine("Test Failed: " + action);
            Console.WriteLine("Query is syntactically incorrect.");
            Console.WriteLine(q);
            Console.WriteLine();
            fail++;

        } else if (test_type == mf_QueryTest) {

            Entity data = (Entity)manifest.SelectObjects(action, qt_data)[0];
            Entity query = (Entity)manifest.SelectObjects(action, qt_query)[0];
            Entity result = (Entity)manifest.SelectObjects(test, mf_result)[0];

            MemoryStore data_store = new MemoryStore(new N3Reader(data.Uri));
            string q = ReadFile(query.Uri);

            if (q.IndexOf("ASK") >= 0) {
                Console.WriteLine("ASK Test Skipped: " + test);
                skip++;
                return;
            }

            string run_individual_test = "mono ../../bin/rdfquery.exe -type sparql n3:" + data.Uri + " < " + query.Uri;

            SparqlEngine sp;
            try {
                sp = new SparqlEngine(q);
            } catch (SemWeb.Query.QueryFormatException qfe) {
                Console.WriteLine("Test Failed: " + test);
                Console.WriteLine(run_individual_test);
                Console.WriteLine(q);
                Console.WriteLine(qfe.Message);
                Console.WriteLine();
                fail++;
                return;
            }

            QueryResultBuffer results = new QueryResultBuffer();
            bool results_bool = false;
            try {
                if (sp.Type != SparqlEngine.QueryType.Ask)
                    sp.Run(data_store, results);
                else
                    results_bool = sp.Ask(data_store);
            } catch (Exception e) {
                Console.WriteLine("Test Failed: " + test);
                Console.WriteLine(run_individual_test);
                Console.WriteLine(q);
                Console.WriteLine(e);
                Console.WriteLine();
                fail++;
                return;
            }

            bool failed = false;
            StringBuilder info = new StringBuilder();

            if (result.Uri.EndsWith(".ttl") || result.Uri.EndsWith(".srx") || result.Uri.EndsWith(".rdf")) {

                bool sorted = false;
                QueryResultBuffer results2 = new QueryResultBuffer();

                if (result.Uri.EndsWith(".srx")) {
                        using (FileStream fs = new FileStream(result.Uri, FileMode.Open))
                            SemWeb.Remote.SparqlHttpSource.ParseSparqlResponse(fs, results2);

                } else if (result.Uri.EndsWith(".rdf") || result.Uri.EndsWith(".ttl")) {
                    RdfReader reader = null;
                    if (result.Uri.EndsWith(".rdf"))
                        reader = new RdfXmlReader(result.Uri);
                    else if (result.Uri.EndsWith(".ttl"))
                        reader = new N3Reader(result.Uri);
                    MemoryStore result_store = new MemoryStore(reader);

                    string rs = "http://www.w3.org/2001/sw/DataAccess/tests/result-set#";
                    Entity rsResultSet = rs + "ResultSet";
                    Entity rsresultVariable = rs + "resultVariable";
                    Entity rssolution = rs + "solution";
                    Entity rsindex = rs + "index";
                    Entity rsbinding = rs + "binding";
                    Entity rsvariable = rs + "variable";
                    Entity rsvalue = rs + "value";

                    // get a list of variables in the query output
                    Entity resultset = result_store.GetEntitiesOfType(rsResultSet)[0];
                    ArrayList vars = new ArrayList();
                    foreach (Literal var in result_store.SelectObjects(resultset, rsresultVariable))
                        vars.Add(new Variable(var.Value));
                    Variable[] varsarray = (Variable[])vars.ToArray(typeof(Variable));

                    // try to order as best we can to our own output, so we sort the results the same way
                    for (int i = 0; i < results.Variables.Length; i++) {
                        if (i >= varsarray.Length) break;
                        for (int j = i; j < varsarray.Length; j++) {
                            if (varsarray[j].LocalName == results.Variables[i].LocalName) {
                                Variable temp = varsarray[i];
                                varsarray[i] = varsarray[j];
                                varsarray[j] = temp;
                                break;
                            }
                        }
                    }

                    Hashtable varmap = new Hashtable();
                    foreach (Variable v in varsarray)
                            varmap[v.LocalName] = varmap.Count;

                    results2.Init(varsarray);

                    Resource[] resultbindings = result_store.SelectObjects(resultset, rssolution);

                    // Try sorting by index
                    int[] indexes = new int[resultbindings.Length];
                    for (int i = 0; i < resultbindings.Length; i++) {
                        Entity binding = (Entity)resultbindings[i];
                        Literal index = (Literal)result_store.SelectObjects(binding, rsindex)[0];
                        indexes[i] = (int)(Decimal)index.ParseValue();
                        sorted = true;
                    }
                    Array.Sort(indexes, resultbindings);

                    // Add bindings into results2.
                    for (int i = 0; i < resultbindings.Length; i++) {
                        Resource[] row = new Resource[vars.Count];
                        Entity binding = (Entity)resultbindings[i];
                        foreach (Entity var in result_store.SelectObjects(binding, rsbinding)) {
                            string name = ((Literal)result_store.SelectObjects(var, rsvariable)[0]).Value;
                            Resource val = result_store.SelectObjects(var, rsvalue)[0];
                            row[(int)varmap[name]] = val;
                        }
                        results2.Add(new VariableBindings(varsarray, row));
                    }
                }

                // Check variable list
                ArrayList vars1 = new ArrayList();
                foreach (Variable v in results.Variables)
                    vars1.Add(v.LocalName);
                ArrayList vars2 = new ArrayList();
                foreach (Variable v in results2.Variables)
                    vars2.Add(v.LocalName);
                failed |= !SetsSame(vars1, vars2, "Result Set Variables", info);

                // Checking bindings
                if (results.Bindings.Count != results2.Bindings.Count) {
                    info.Append("Solutions have different number of bindings.\n");
                    failed = true;
                } else {
                    // Now actually run comparison.

                    if (!sorted) {
                        ((ArrayList)results.Bindings).Sort();
                        ((ArrayList)results2.Bindings).Sort();
                    }

                    for (int i = 0; i < results.Bindings.Count; i++) {
                            VariableBindings b1 = (VariableBindings)results.Bindings[i];
                            VariableBindings b2 = (VariableBindings)results2.Bindings[i];
                            foreach (Variable var in results.Variables) {
                                Resource val1 = b1[var.LocalName];
                                Resource val2 = b2[var.LocalName];
                                if (val1 != val2 && !(val1 is BNode) && !(val2 is BNode)) { // TODO: Test bnodes are returned correctly
                                    info.Append("Binding row " + i + " differ in value of " + var.LocalName + " variable: " + val2 + ", should be: " + val1 + "\n");
                                    failed = true;
                                }
                            }
                    }
                }

            } else {
                skip++;
                Console.WriteLine(test + ": Unknown result type " + result.Uri);
            }

            if (failed) {
                Console.WriteLine("Test Failed: " + test);
                Console.WriteLine(run_individual_test);
                Console.WriteLine(q);
                Console.WriteLine(info.ToString());
                Console.WriteLine();
                fail++;
            } else {
                pass++;
            }

        } else {
            skip++;
            Console.WriteLine(test + ": Unknown test type " + test_type);
            Console.WriteLine();
        }
    }
Esempio n. 8
0
    public Hashtable Validate()
    {
        string content = HttpContext.Current.Request.Form["content"];
        string format = HttpContext.Current.Request.Form["format"];
        if (content == null || content.Trim() == "" || format == null) {
            HttpContext.Current.Response.Redirect("index.xpd");
            throw new InvalidOperationException();
        }

        StringWriter output = new StringWriter();

        RdfReader reader;
        RdfWriter writer;

        Hashtable response = new Hashtable();

        response["InDocument"] = AddLineNumbers(content);

        if (format == "xml") {
            reader = new RdfXmlReader(new StringReader(content));
            writer = new N3Writer(output);
            response["InFormat"] = "RDF/XML";
            response["OutFormat"] = "Notation 3";
        } else if (format == "n3") {
            reader = new N3Reader(new StringReader(content));
            writer = new RdfXmlWriter(output);
            response["OutFormat"] = "RDF/XML";
            response["InFormat"] = "Notation 3";
        } else {
            throw new Exception("Invalid format.");
        }

        response["Validation"] = "Syntax validated OK.";

        response["OutDocument"] = "";
        response["Triples"] = "";

        MemoryStore data = new MemoryStore();
        try {
            data.Import(reader);
        } catch (Exception e) {
            response["Validation"] = "Validation failed: " + e.Message + ".";
            return response;
        } finally {
            if (reader.Warnings.Count > 0) {
                response["Validation"] += "  There were warnings: ";
                foreach (string warning in reader.Warnings)
                    response["Validation"] += " " + warning + ".";
            }
        }

        writer.Namespaces.AddFrom(reader.Namespaces);

        try {
            writer.Write(data);
            writer.Close();
            response["OutDocument"] = output.ToString();
        } catch (Exception e) {
            response["OutDocument"] = e.Message;
        }

        StringWriter triplesoutput = new StringWriter();
        using (NTriplesWriter tripleswriter = new NTriplesWriter(triplesoutput)) {
            tripleswriter.Write(data);
        }
        response["Triples"] = triplesoutput.ToString();

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

            Console.WriteLine("Reading the input using SemWeb");
            N3Reader reader = new N3Reader("InferenceTest.ttl");
            reader.Select(source);
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples using N3Writer");
            N3Writer writer = new N3Writer(Console.Out);
            source.Select(writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?type");
            Statement template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Variable());
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples of the form ?s rdf:type ?car");
            template = new Statement(new Variable(), new Entity(RdfSpecsHelper.RdfType), new Entity("http://example.org/vehicles/Car"));
            source.Select(template, writer);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Outputting all Triples for Cars and Planes");
            SelectFilter filter = new SelectFilter();
            filter.Predicates = new Entity[] { new Entity(RdfSpecsHelper.RdfType) };
            filter.Objects = new Entity[] { new Entity("http://example.org/vehicles/Car"), new Entity("http://example.org/vehicles/Plane") };
            source.Select(filter, writer);
            Console.WriteLine();
            Console.WriteLine();

            writer.Close();
        }