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(); } }
private static bool MakeLeanMSG3(Store msg, ResSet predicates, StatementSink removed, ResSet nodesremoved, SyncPath path) { // The variable path has to be expanded by including the statements // connected to the variables on the frontier. Statements // mentioning a variable node have already been considered. // The target of each such statement can be considered fixed // or variable. If a variable is considered fixed, the edge // must exist in the MSG substituting the variables for their // values. If it's variable, it has to have at least one // match in the MSG but not as any of the variable nodes. // If all targets are considered fixed (and have matches), // then the variables so far (and their edges) can all be // removed and no more processing needs to be done. // There are (2^N)-1 other considerations. For each of those, // the targets considered variables all become the new // frontier, and this is repeated. // First, get a list of edges from the frontier that we // haven't considered yet. ArrayList alledges = new ArrayList(); foreach (BNode b in path.FrontierVariables) { // Make sure all edges are kept because even the ones // to literals have to be removed when duplication is found. foreach (Statement s in msg.Select(new Statement(b, null, null))) alledges.Add(new Edge(true, b, s.Predicate, s.Object)); foreach (Statement s in msg.Select(new Statement(null, null, b))) alledges.Add(new Edge(false, b, s.Predicate, s.Subject)); } ArrayList newedges = new ArrayList(); ResSet alltargets = new ResSet(); ResSet fixabletargetsset = new ResSet(); // can be fixed ResSet variabletargetsset = new ResSet(); // must be variable foreach (Edge e in alledges) { if (path.Path.ContainsKey(e)) continue; path.Path[e] = e; // This checks if we can keep the target of this edge // fixed, given the variable mappings we have so far. bool isTargetFixable = msg.Contains(e.AsStatement().Replace(path.Mapping)); // If the target of e is any of the following, we // can check immediately if the edge is supported // by the MSG under the variable mapping we have so far: // a named node, literal, fixed node, or predicate // a variable we've seen already // If it's not supported, this path fails. If it is // supported, we're done with this edge. if (!(e.End is BNode) || path.FixedNodes.Contains(e.End) || predicates.Contains(e.End) || path.VariableNodes.Contains(e.End)) { if (!isTargetFixable) return false; continue; // this edge is supported, so we can continue } // The target of e is a new BNode. // If this target is not fixable via this edge, it's // not fixable at all. if (!isTargetFixable) { fixabletargetsset.Remove(e.End); variabletargetsset.Add(e.End); } if (!alltargets.Contains(e.End)) { alltargets.Add(e.End); fixabletargetsset.Add(e.End); } newedges.Add(e); } // If all of the targets were fixable (trivially true also // if there simple were no new edges/targets), then we've reached // the end of this path. We can immediately remove // the edges we've seen so far, under the variable mapping // we've chosen. if (variabletargetsset.Count == 0) { foreach (Edge e in path.Path.Keys) { Statement s = e.AsStatement(); msg.Remove(s); if (removed != null) removed.Add(s); } foreach (Entity e in path.Mapping.Keys) nodesremoved.Add(e); return true; } // At this point, at least one target must be a variable // and we'll have to expand the path in that direction. // We might want to permute through the ways we can // take fixable nodes as either fixed or variable, but // we'll be greedy and assume everything fixable is // fixed and everything else is a variable. path.FixedNodes.AddRange(fixabletargetsset); path.VariableNodes.AddRange(variabletargetsset); // But we need to look at all the ways each variable target // can be mapped to a new value, which means intersecting // the possible matches for each relevant edge. Entity[] variables = variabletargetsset.ToEntityArray(); ResSet[] values = new ResSet[variables.Length]; Entity[][] values_array = new Entity[variables.Length][]; int[] choices = new int[variables.Length]; for (int i = 0; i < variables.Length; i++) { foreach (Edge e in newedges) { if (e.End != variables[i]) continue; // Get the possible values this edge allows Resource[] vr; if (e.Direction) vr = msg.SelectObjects((Entity)path.Mapping[e.Start], e.Predicate); else vr = msg.SelectSubjects(e.Predicate, (Entity)path.Mapping[e.Start]); // Filter out literals and any variables // on the path! The two paths can't intersect // except at fixed nodes. ResSet v = new ResSet(); foreach (Resource r in vr) { if (r is Literal) continue; if (path.Mapping.ContainsKey(r)) continue; v.Add(r); } // Intersect these with the values we have already. if (values[i] == null) values[i] = v; else values[i].RetainAll(v); // If no values are available for this variable, // we're totally done. if (values[i].Count == 0) return false; } choices[i] = values[i].Count; values_array[i] = values[i].ToEntityArray(); } // Now we have to permute through the choice of values. // Make an array of the number of choices for each variable. Permutation p = new Permutation(choices); int[] pstate; while ((pstate = p.Next()) != null) { SyncPath newpath = new SyncPath(); newpath.FixedNodes.AddRange(path.FixedNodes); newpath.VariableNodes.AddRange(path.VariableNodes); newpath.Mapping = (Hashtable)path.Mapping.Clone(); newpath.Path = (Hashtable)path.Path.Clone(); newpath.FrontierVariables = variabletargetsset; for (int i = 0; i < variables.Length; i++) { Entity value = values_array[i][pstate[i]]; newpath.Mapping[variables[i]] = value; newpath.FixedNodes.Add(value); } if (MakeLeanMSG3(msg, predicates, removed, nodesremoved, newpath)) return true; } return false; }
static void RunTest(Store testlist, Entity test, bool shouldSucceed, int mode) { string inputpath = null, outputpath = null, inputformat = null, outputformat = null, inputbase = null, outputbase = null; if (mode == 0) { inputformat = "xml"; outputformat = "n3"; string uribase = "http://www.w3.org/2000/10/rdf-tests/rdfcore/"; Resource input = testlist.SelectObjects(test, "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#inputDocument")[0]; inputbase = input.Uri; inputpath = input.Uri.Substring(uribase.Length); if (testlist.SelectObjects(test, "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#outputDocument").Length > 0) { Resource output = testlist.SelectObjects(test, "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#outputDocument")[0]; outputpath = output.Uri.Substring(uribase.Length); outputbase = output.Uri; } } else if (mode == 1) { inputformat = "n3"; outputformat = "n3"; inputbase = "file:/home/syosi/cvs-trunk/WWW/2000/10/swap/test/n3parser.tests"; string uribase = "http://www.example.org/"; if (testlist.SelectObjects(test, "http://www.w3.org/2004/11/n3test#inputDocument").Length == 0) return; Resource input = testlist.SelectObjects(test, "http://www.w3.org/2004/11/n3test#inputDocument")[0]; inputpath = input.Uri.Substring(uribase.Length); if (testlist.SelectObjects(test, "http://www.w3.org/2004/11/n3test#outputDocument").Length > 0) { Resource output = testlist.SelectObjects(test, "http://www.w3.org/2004/11/n3test#outputDocument")[0]; outputpath = output.Uri.Substring(uribase.Length); } } string desc = test.ToString(); try { desc += " " + ((Literal)testlist.SelectObjects(test, "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#description")[0]).Value; } catch (Exception e) { } try { total++; RdfReader reader = RdfReader.Create(inputformat, Path.Combine(basepath, inputpath)); reader.BaseUri = inputbase; MemoryStore inputmodel = new MemoryStore(reader); if (reader.Warnings.Count > 0) { string warnings = String.Join("; ", (string[])((ArrayList)reader.Warnings).ToArray(typeof(string))); throw new ParserException(warnings); } if (!shouldSucceed) { Console.WriteLine(desc + ": Should Not Have Passed **\n"); badpass++; return; } if (shouldSucceed && outputpath != null) { RdfReader reader2 = RdfReader.Create(outputformat, Path.Combine(basepath, outputpath)); reader2.BaseUri = outputbase; MemoryStore outputmodel = new MemoryStore(reader2); CompareModels(inputmodel, outputmodel); } } catch (System.IO.FileNotFoundException ex) { Console.WriteLine(inputpath + " Not Found"); error++; } catch (System.IO.DirectoryNotFoundException ex) { Console.WriteLine(inputpath + " Not Found"); error++; } catch (ParserException ex) { if (shouldSucceed) { Console.WriteLine(desc + ": " + ex.Message + " **"); Console.WriteLine(); badfail++; } } }
private int GetIntOption(Store queryModel, Entity query, Entity predicate) { Resource[] rr = queryModel.SelectObjects(query, predicate); if (rr.Length == 0) return -1; Resource r = rr[0]; if (r == null || !(r is Literal)) return -1; try { return int.Parse(((Literal)r).Value); } catch (Exception e) { return -1; } }