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 string GetLabel(Entity entity, MemoryStore rdfStore)
 {
     string label = new ArrayList<string>(entity.Uri.Split('/', '#')).Last;
     Resource[] labels = rdfStore.SelectObjects(entity, P_LABEL);
     if (labels.Length > 0)
     {
         label = ((Literal)labels[0]).Value; // *** always take the first available label
         label = label.Replace('/', '-');
     }
     return label;
 }
Esempio n. 3
0
    public static void Main()
    {
        MemoryStore store = new MemoryStore();

        Entity container = new Entity("http://www.example.org/#container");

        store.Add(new Statement(container, RDF+"type", (Entity)(RDF+"Bag")));
        store.Add(new Statement(container, RDF+"_3", (Literal)"Three"));
        store.Add(new Statement(container, RDF+"_2", (Literal)"Two"));
        store.Add(new Statement(container, RDF+"_1", (Literal)"One"));

        // use the rdfs:member property to match for any rdf:_### predicates.
        Entity rdfs_member = (Entity)(RDFS+"member");

        using (RdfWriter writer = new N3Writer(Console.Out)) {
            writer.Namespaces.AddNamespace(RDF, "rdf");
            store.Select(new Statement(container, rdfs_member, null), writer);
        }

        foreach (Resource r in store.SelectObjects(container, rdfs_member))
            Console.WriteLine(r);
    }
Esempio n. 4
0
    public static void Main()
    {
        Store store = new MemoryStore();
        store.Import(RdfReader.LoadFromUri(new Uri(example_foaf_file)));

        // Dump out the data, for fun
        using (RdfWriter writer = new RdfXmlWriter(Console.Out)) {
            writer.Write(store);
        }

        Console.WriteLine("These are the people in the file:");
        foreach (Statement s in store.Select(new Statement(null, rdftype, foafPerson))) {
            foreach (Resource r in store.SelectObjects(s.Subject, foafname))
                Console.WriteLine(r);
        }
        Console.WriteLine();

        Console.WriteLine("And here's RDF/XML just for some of the file:");
        using (RdfWriter w = new RdfXmlWriter(Console.Out)) {
            store.Select(new Statement(null, foafname, null), w);
            store.Select(new Statement(null, foafknows, null), w);
        }
        Console.WriteLine();
    }
Esempio n. 5
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. 6
0
 private void ReadGazetteerSettings(MemoryStore rdfStore, out CaseMatchingType caseMatchingType, out bool lemmatize, out bool enabled)
 {
     caseMatchingType = CaseMatchingType.IgnoreCase;
     lemmatize = false;
     enabled = true;
     ArrayList<string> crumbs = new ArrayList<string>(new string[] { mUri });
     Entity[] objects = rdfStore.SelectSubjects(P_IDENTIFIED_BY, new Entity(mUri));
     if (objects.Length > 0)
     {
         Resource[] objTypes = rdfStore.SelectObjects(objects[0].Uri, P_TYPE);
         if (objTypes.Length > 0)
         {
             crumbs.Add(objTypes[0].Uri);
             Resource[] superClass = rdfStore.SelectObjects((Entity)objTypes[0], P_SUBCLASS_OF);
             while (superClass.Length > 0)
             {
                 crumbs.Add(superClass[0].Uri);
                 superClass = rdfStore.SelectObjects((Entity)superClass[0], P_SUBCLASS_OF);
             }
         }
     }
     crumbs.Reverse();
     foreach (string uri in crumbs)
     {
         Resource[] settings = rdfStore.SelectObjects(uri, P_SETTINGS);
         if (settings.Length == 0) { settings = rdfStore.SelectObjects(uri, P_COMMENT); } // for compatibility with OWL-DL
         if (settings.Length > 0)
         {
             string settingsStr = ((Literal)settings[0]).Value;
             ParseGazetteerSettings(settingsStr, ref caseMatchingType, ref lemmatize, ref enabled);
         }
     }
 }
Esempio n. 7
0
 public void ReadTerms(MemoryStore rdfStore)
 {
     // read default settings
     //Console.WriteLine("*** " + mUri + " ***");
     CaseMatchingType caseMatchingType;
     bool lemmatize;
     ReadGazetteerSettings(rdfStore, out caseMatchingType, out lemmatize, out mEnabled);
     // read terms
     Resource[] terms = rdfStore.SelectObjects(mUri, P_TERM);
     Set<string> skipList = new Set<string>();
     foreach (Literal term in terms)
     {
         //Console.Write(term.Value + " -> ");
         GazetteerTerm termObj = new GazetteerTerm(term.Value, /*gazetteer=*/this, caseMatchingType, lemmatize, mEnabled);
         string termStr = termObj.ToString();
         if (termObj.mTokens.Count > 0 && !skipList.Contains(termStr))
         {
             //Console.WriteLine(termStr);
             mTerms.Add(termObj);
             skipList.Add(termStr);
         }
         else
         {
             //Console.WriteLine("/");
         }
     }
 }
Esempio n. 8
0
 public void ReadStopWords(MemoryStore rdfStore)
 {
     Resource[] stopWords = rdfStore.SelectObjects(mUri, P_STOP_WORD);
     foreach (Literal word in stopWords)
     {
         string stopWordStr = Normalize(word.Value).ToLower();
         mStopWords.Add(stopWordStr);
     }
 }
Esempio n. 9
0
 public void ReadConditions(MemoryStore rdfStore, Dictionary<string, Gazetteer> gazetteers)
 {
     ArrayList<string> crumbs = new ArrayList<string>(new string[] { mUri });
     Entity[] objects = rdfStore.SelectSubjects(P_IDENTIFIED_BY, new Entity(mUri));
     if (objects.Length > 0)
     {
         Resource[] objTypes = rdfStore.SelectObjects(objects[0].Uri, P_TYPE);
         if (objTypes.Length > 0)
         {
             crumbs.Add(objTypes[0].Uri);
             Resource[] superClass = rdfStore.SelectObjects((Entity)objTypes[0], P_SUBCLASS_OF);
             while (superClass.Length > 0)
             {
                 crumbs.Add(superClass[0].Uri);
                 superClass = rdfStore.SelectObjects((Entity)superClass[0], P_SUBCLASS_OF);
             }
         }
     }
     crumbs.Reverse();
     foreach (string uri in crumbs)
     {
         Resource[] conditionGazetteers = rdfStore.SelectObjects(uri, P_HAS_SENTENCE_LEVEL_CONDITION);
         foreach (Entity conditionGazetteer in conditionGazetteers)
         {
             mConditions.Add(new Condition(gazetteers[conditionGazetteer.Uri], Condition.Level.Sentence));
         }
         conditionGazetteers = rdfStore.SelectObjects(uri, P_HAS_BLOCK_LEVEL_CONDITION);
         foreach (Entity conditionGazetteer in conditionGazetteers)
         {
             mConditions.Add(new Condition(gazetteers[conditionGazetteer.Uri], Condition.Level.Block));
         }
         conditionGazetteers = rdfStore.SelectObjects(uri, P_HAS_DOCUMENT_LEVEL_CONDITION);
         foreach (Entity conditionGazetteer in conditionGazetteers)
         {
             mConditions.Add(new Condition(gazetteers[conditionGazetteer.Uri], Condition.Level.Document));
         }
     }
 }
Esempio n. 10
0
 public void ImportGazetteers(MemoryStore rdfStore, Dictionary<string, Gazetteer> gazetteers)
 {
     Resource[] importedGazetteers = rdfStore.SelectObjects(mUri, P_IMPORTS);
     foreach (Entity importedGazetteer in importedGazetteers)
     {
         mImportedGazetteers.Add(gazetteers[importedGazetteer.Uri]);
     }
 }
Esempio n. 11
0
 public void ReadTerms(MemoryStore rdfStore)
 {
     Resource[] terms = rdfStore.SelectObjects(mUri, P_HAS_TERM);
     Set<string> skipList = new Set<string>();
     foreach (Literal term in terms)
     {
         mTokenizer.Text = Normalize(mIgnoreCase ? term.Value.ToLower() : term.Value);
         ArrayList<string> tokens = new ArrayList<string>();
         foreach (string token in mTokenizer)
         {
             string tokenLower = token.ToLower();
             if (!IsStopWord(tokenLower)) { tokens.Add(token); }
         }
         if (tokens.Count > 0 && !skipList.Contains(tokens.ToString()))
         {
             Term termObj = new Term();
             termObj.mWords = tokens;
             mTerms.Add(termObj);
             skipList.Add(tokens.ToString());
         }
     }
 }
Esempio n. 12
0
 public void ReadSettings(MemoryStore mRdfStore)
 {
     Resource[] r = mRdfStore.SelectObjects(mUri, P_IGNORES_CASE);
     if (r.Length > 0 && ((Literal)r[0]).Value == "false") { mIgnoreCase = false; }
 }
Esempio n. 13
0
        private static void CollectCallArgs(MemoryStore body, Hashtable callArgs)
        {
            foreach (Statement s in new MemoryStore(body)) { // make a copy since we remove statements from b within
                if (FindUserPredicate(s.Predicate) == null) continue;

                Entity subj = s.Subject;
                if (!(subj is BNode)) continue; // it would be nice to exclude variables, but we've already converted bnodes to variables

                BNode head = (BNode)subj;

                ArrayList argList = new ArrayList();

                while (true) {
                    Resource[] objs = body.SelectObjects(subj, entRDFFIRST);
                    if (objs.Length == 0) break; // if this is the first iteration, then we're just not looking at a list
                                                 // on later iterations, the list must not be well-formed, so we'll just
                                                 // stop reading it here

                    argList.Add(objs[0]); // assume a properly formed list
                    body.Remove(new Statement(subj, entRDFFIRST, null));

                    Resource[] rests = body.SelectObjects(subj, entRDFREST);
                    if (rests.Length == 0) break; // not well formed
                    body.Remove(new Statement(subj, entRDFREST, null));
                    if (rests[0] == entRDFNIL) break; // that's the end of the list
                    if (!(rests[0] is Entity)) break; // also not well formed

                    subj = (Entity)rests[0];
                }

                if (argList.Count > 0)
                    callArgs[head] = argList.ToArray(typeof(Resource));
            }
        }