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();
}