public static Object LoadFromUri(java.net.URL url, string baseUri, org.openrdf.rio.RDFFormat rdff) { Object obj; Uri u = new Uri(url.toString()); if (rdff == dotSesameFormats.RDFFormat.N3) { obj = new Graph(); if (baseUri != null) { ((IGraph)obj).BaseUri = new Uri(baseUri); } UriLoader.Load((IGraph)obj, u, new Notation3Parser()); } else if (rdff == dotSesameFormats.RDFFormat.NTRIPLES) { obj = new Graph(); if (baseUri != null) { ((IGraph)obj).BaseUri = new Uri(baseUri); } UriLoader.Load((IGraph)obj, u, new NTriplesParser()); } else if (rdff == dotSesameFormats.RDFFormat.RDFXML) { obj = new Graph(); if (baseUri != null) { ((IGraph)obj).BaseUri = new Uri(baseUri); } UriLoader.Load((IGraph)obj, u, new RdfXmlParser()); } else if (rdff == dotSesameFormats.RDFFormat.TRIG || rdff == dotSesameFormats.RDFFormat.TRIX) { obj = new TripleStore(); UriLoader.Load((ITripleStore)obj, u); } else if (rdff == dotSesameFormats.RDFFormat.TURTLE) { obj = new Graph(); if (baseUri != null) { ((IGraph)obj).BaseUri = new Uri(baseUri); } UriLoader.Load((IGraph)obj, u, new TurtleParser()); } else { throw new RdfParserSelectionException("The given Input Format is not supported by dotNetRDF"); } return(obj); }
/// <summary> /// Adds a Graph into the Triple Store which is retrieved from the given Uri using the chosen Merging Behaviour /// </summary> /// <param name="graphUri">Graph to add</param> /// <param name="mergeIfExists">Whether the Graph should be merged with an existing Graph with the same Base Uri</param> /// <remarks> /// <strong>Important:</strong> Under Silverlight/Windows Phone 7 this will happen asynchronously so the Graph may not be immediatedly available in the store /// </remarks> public virtual bool AddFromUri(Uri graphUri, bool mergeIfExists) { Graph g = new Graph(); #if !SILVERLIGHT UriLoader.Load(g, graphUri); return(this._graphs.Add(g, mergeIfExists)); #else UriLoader.Load(g, graphUri, (gr, _) => { this._graphs.Add(gr, mergeIfExists); }, null); return(true); #endif }
private void btnImportUri_Click(object sender, EventArgs e) { if (this.txtSourceUri.Text.Equals(string.Empty)) { MessageBox.Show("Please enter a URI you wish to import RDF from...", "No URI Specified"); } else { Graph g = new Graph(); try { UriLoader.Load(g, new Uri(this.txtSourceUri.Text)); try { if (this._dataset.HasGraph(g.BaseUri)) { int triplesBefore = this._dataset[g.BaseUri].Triples.Count; this._dataset[g.BaseUri].Merge(g); this._tripleCount += this._dataset[g.BaseUri].Triples.Count - triplesBefore; } else { this._dataset.AddGraph(g); this._tripleCount += g.Triples.Count; } this.LogImportSuccess(new Uri(this.txtSourceUri.Text), 1, g.Triples.Count); } catch (Exception ex) { this.LogImportFailure(new Uri(this.txtSourceUri.Text), ex); MessageBox.Show("An error occurred trying to add the RDF Graph to the Dataset:\n" + ex.Message, "URI Import Error"); return; } } catch (UriFormatException uriEx) { MessageBox.Show("The URI you have entered is malformed:\n" + uriEx.Message, "Malformed URI"); } catch (Exception ex) { this.LogImportFailure(new Uri(this.txtSourceUri.Text), ex); MessageBox.Show("An error occurred while loading RDF from the given URI:\n" + ex.Message, "URI Import Error"); return; } this._dataset.Flush(); this.stsGraphs.Text = this._dataset.GraphUris.Count() + " Graphs"; this.stsTriples.Text = this._tripleCount + " Triples"; MessageBox.Show("RDF added to the Dataset OK", "URI Import Done"); } }
public void SerializationJsonGraph3() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { Assert.Inconclusive("Test Config marks Remote Parsing as unavailable, test cannot be run"); } Graph g = new Graph(); UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Ilkeston")); this.TestGraphSerializationJson(g); }
public void SerializationDataContractGraph3() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run"); } Graph g = new Graph(); UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Ilkeston")); this.TestGraphSerializationDataContract(g); }
private void GraphToStructured(IEnumerable <Resource> input, Resource output) { ValidateMask(input, ResourceFormat.GraphMask); var graph = new Graph(); foreach (var file in input) { if (file.Format == ResourceFormat.RdfGeneric) { if (file.TargetPath == null) { throw new ApplicationException("Standard input resource must have a concrete format specified."); } UriLoader.Load(graph, ResolveUri(file.TargetPath)); } else { var rdfReader = GetReader(file.Format); using (var reader = new StreamReader(OpenInput(file.TargetPath), true)) { var baseUri = GetBaseUri(file.TargetPath); graph.BaseUri = baseUri != null ? new Uri(baseUri) : null; rdfReader.Load(graph, reader); } } } var root = graph.CreateUriNode(new Uri(GetBaseUri(output.TargetPath))); if (!graph.GetTriplesWithSubject(root).Any()) { throw new ApplicationException("Base URI must refer to an existing root!"); } using (var writer = XmlWriter.Create(OpenOutput(output.TargetPath), XmlWriterSettings)) { switch (output.Format) { case ResourceFormat.Xml: new RdfXmlConverter(graph).Write(writer, root); break; case ResourceFormat.XmlXPath: new RdfXPathNavigator(graph, root).WriteSubtree(writer); break; default: throw new ApplicationException("Unsupported output format!"); } } }
/// <summary> /// Implements the import /// </summary> /// <param name="handler">Handler</param> protected override void ImportUsingHandler(IRdfHandler handler) { this.Information = "Importing from URI " + this._u.AbsoluteUri; try { //Assume a RDF Graph UriLoader.Load(handler, this._u); } catch (RdfParserSelectionException) { //Assume a RDF Dataset UriLoader.LoadDataset(handler, this._u); } }
private Uri ReadRemoteTripleFormat(Uri graph, Uri location, RdfSerializationFormat format) { using (VDS.RDF.Storage.VirtuosoManager m = new VDS.RDF.Storage.VirtuosoManager(CreateConnectionString())) { using (VDS.RDF.Graph g = new VDS.RDF.Graph()) { UriLoader.Load(g, location); g.BaseUri = graph; m.SaveGraph(g); } } return(graph); }
public void CacheUpdatesFileCreationTimeOnReload() { var g = new Graph(); var uri = new Uri(_serverFixture.Server.Urls[0] + "/rvesse.ttl"); UriLoader.Load(g, uri); Assert.True(UriLoader.IsCached(uri)); Thread.Sleep(UriLoader.CacheDuration + TimeSpan.FromMilliseconds(500)); Assert.True(UriLoader.IsCached(uri)); Assert.False(UriLoader.Cache.HasLocalCopy(uri, true)); UriLoader.Load(g, uri); Assert.True(UriLoader.IsCached(uri)); Assert.True(UriLoader.Cache.HasLocalCopy(uri, true)); }
public override void Convert() { if (this.ConversionHandler == null) { throw new Exception("Cannot convert the Input URI '" + this.SourceUri.ToString() + "' as rdfConvert could not determine a Conversion Handler to use for the Conversion"); } try { UriLoader.Load(this.ConversionHandler, this.SourceUri); } catch { UriLoader.LoadDataset(this.ConversionHandler, this.SourceUri); } }
public void ServiceDescriptionDescriptionUriSparqlServer() { EnsureIIS(); String path = TestConfigManager.GetSetting(TestConfigManager.LocalGraphStoreUri) + "description"; Console.WriteLine("Making an request for the Service Description from the web demos SPARQL Server at " + path); Console.WriteLine(); NTriplesFormatter formatter = new NTriplesFormatter(); Graph g = new Graph(); UriLoader.Load(g, new Uri(path)); TestTools.ShowGraph(g); Assert.IsFalse(g.IsEmpty, "A non-empty Service Description Graph should have been returned"); }
public void WritingCollections() { Graph g = new Graph(); #if !NO_URICACHE Options.UriLoaderCaching = false; #endif UriLoader.Load(g, new Uri("http://www.wurvoc.org/vocabularies/om-1.6/Kelvin_scale")); CompressingTurtleWriter ttlwriter = new CompressingTurtleWriter(WriterCompressionLevel.High); #if PORTABLE var tmpWriter = new StreamWriter(new MemoryStream()); ttlwriter.Save(g, tmpWriter); #else ttlwriter.Save(g, Console.Out); #endif }
private IGraph LoadGraph(String uri, bool fromFile) { Graph g = new Graph(); try { if (fromFile) { FileLoader.Load(g, uri); } else { Uri u = new Uri(uri); if (u.IsAbsoluteUri) { UriLoader.Load(g, u); } else { FileLoader.Load(g, uri); } } return(g); } catch (UriFormatException) { //Try loading as a file as it's not a valid URI return(this.LoadGraph(uri, true)); } catch (RdfParseException parseEx) { Console.Error.WriteLine("rdfQuery: Parser Error: Unable to parse data from URI '" + uri + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Parser Error: " + parseEx.Message); } catch (RdfException rdfEx) { Console.Error.WriteLine("rdfQuery: RDF Error: Unable to read data from URI '" + uri + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: RDF Error: " + rdfEx.Message); } catch (Exception ex) { Console.Error.WriteLine("rdfQuery: Error: Unable to read data from URI '" + uri + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Error: " + ex.Message); } return(null); }
public static IGraph GetGraphFromOntology(Ontology ontology) { IGraph g = new Graph(); IRdfReader r = ontology.RdfType == RdfType.TTL ? new TurtleParser() : null; if (ontology.RdfSource == RdfSource.Uri) { if (r == null) { UriLoader.Load(g, new Uri(ontology.Location)); } else { UriLoader.Load(g, new Uri(ontology.Location), r); } } else if (ontology.RdfType == RdfType.TTL) { if (ontology.RdfSource == RdfSource.String) { r.Load(g, new StringReader(ontology.Location)); } else { r.Load(g, ontology.Location); } } else { try { FileLoader.Load(g, ontology.Location); } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } throw new Exception($"Rdf Source and Type Unknown - {ontology.Location} - cannot load as TTL from file: {e.Message}"); } } return(g); }
public void ParsingGraphHandlerImplicitBaseUriPropogation() { try { Options.UriLoaderCaching = false; Graph g = new Graph(); UriLoader.Load(g, new Uri("http://wiki.rkbexplorer.com/id/void")); NTriplesFormatter formatter = new NTriplesFormatter(); foreach (Triple t in g.Triples) { Console.WriteLine(t.ToString()); } } finally { Options.UriLoaderCaching = true; } }
private void ExpandByUriLookup(UriToExpand u, ExpansionContext context, Uri lookupEndpoint) { if (u.Depth == context.Profile.MaxExpansionDepth) { return; } StringBuilder requestUri = new StringBuilder(); requestUri.Append(lookupEndpoint.ToString()); requestUri.Append(Uri.EscapeDataString(u.Uri.ToString())); Graph g = new Graph(); Thread.Sleep(HttpRequestInterval); UriLoader.Load(g, new Uri(requestUri.ToString())); this.ExpandGraph(u, g, context); }
static void Main(string[] args) { IGraph g = new Graph(); UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Russia")); Console.WriteLine("=== TRIPLES ==="); foreach (var x in g.Triples) { if (x.Predicate.ToString().Contains("population")) { Console.WriteLine($"O={x.Object}, P={x.Predicate}, S={x.Subject}"); } } Console.WriteLine("=== QUERY ==="); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlResultSet results = endpoint.QueryWithResultSet(@" PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX : <http://dbpedia.org/resource/> PREFIX dbpedia2: <http://dbpedia.org/property/> PREFIX dbpedia: <http://dbpedia.org/> PREFIX skos: <http://www.w3.org/2004/02/skos/core#> SELECT ?birth STR(?name) WHERE { ?person a dbo:MusicalArtist . ?person dbo:birthPlace :Moscow . ?person dbo:birthDate ?birth . ?person foaf:name ?name . } ORDER BY ?name "); foreach (SparqlResult result in results) { Console.WriteLine(result.ToString()); } Console.ReadKey(); }
/// <summary> /// Evaluates the Command in the given Context /// </summary> /// <param name="context">Evaluation Context</param> public override void Evaluate(SparqlUpdateEvaluationContext context) { //Q: Does LOAD into a named Graph require that Graph to be pre-existing? //if (this._graphUri != null) //{ // //When adding to specific Graph need to ensure that Graph exists // //In the case when we're adding to the default graph we'll create it if it doesn't exist // if (!context.Data.HasGraph(this._graphUri)) // { // throw new RdfUpdateException("Cannot LOAD into a Graph that does not exist in the Store"); // } //} try { //Load from the URI Graph g = new Graph(); #if SILVERLIGHT throw new PlatformNotSupportedException("The SPARQL LOAD command is not currently supported under Silverlight/Windows Phone 7"); #else UriLoader.Load(g, this._sourceUri); #endif if (context.Data.HasGraph(this._graphUri)) { //Merge the Data into the existing Graph context.Data.GetModifiableGraph(this._graphUri).Merge(g); } else { //Add New Graph to the Dataset g.BaseUri = this._graphUri; context.Data.AddGraph(g); } } catch { if (!this._silent) { throw; } } }
public Form1() { InitializeComponent(); IGraph graph = new Graph(); UriLoader.Load(graph, new Uri(RDFUrl)); try { Object results = graph.ExecuteQuery("SELECT * WHERE {\r\n ?sub ?pre ?dupa.\r\n} \r\nLIMIT 10"); if (results is SparqlResultSet) { //SELECT/ASK queries give a SparqlResultSet SparqlResultSet rset = (SparqlResultSet)results; foreach (SparqlResult r in rset) { //Do whatever you want with each Result } } else if (results is IGraph) { //CONSTRUCT/DESCRIBE queries give a IGraph IGraph resGraph = (IGraph)results; foreach (Triple t in resGraph.Triples) { //Do whatever you want with each Triple } } else { //If you don't get a SparqlResutlSet or IGraph something went wrong //but didn't throw an exception so you should handle it here Console.WriteLine("ERROR"); } } catch (RdfQueryException queryEx) { //There was an error executing the query so handle it here Console.WriteLine(queryEx.Message); } }
private static IGraph GraphFromUri(Uri uri) { var incorrectBaseUri = new Uri(baseUri.AbsoluteUri.Replace(@"https://", "http://")); var uriMapping = new Dictionary <Uri, Uri> { { incorrectBaseUri, baseUri } }; var graph = new Graph(); var handler = new UriMappingHandler(new GraphHandler(graph), graph, uriMapping); var parser = MimeTypesHelper.GetParserByFileExtension(Path.GetExtension(uri.LocalPath)); graph.BaseUri = uri; UriLoader.Load(handler, uri, parser); return(graph); }
/// <summary> /// Tries to load a Graph on demand from a URI /// </summary> /// <param name="graphUri">Graph URI</param> /// <returns></returns> protected override IGraph LoadOnDemand(Uri graphUri) { if (graphUri != null) { try { Graph g = new Graph(); UriLoader.Load(g, graphUri); return(g); } catch { throw new RdfException("The Graph with the URI " + graphUri.AbsoluteUri + " does not exist in this collection"); } } else { throw new RdfException("The Graph with the URI does not exist in this collection"); } }
public IGraph LoadImport(Uri uri) { var import = new Graph(); if (uri.IsFile) { FileLoader.Load(import, uri.ToString()); } else { try { UriLoader.Load(import, uri); } catch { } } import.BaseUri = null; return(import); }
/// <summary> /// Evaluates the Command in the given Context /// </summary> /// <param name="context">Evaluation Context</param> public override void Evaluate(SparqlUpdateEvaluationContext context) { // Q: Does LOAD into a named Graph require that Graph to be pre-existing? // if (this._graphUri != null) // { // //When adding to specific Graph need to ensure that Graph exists // //In the case when we're adding to the default graph we'll create it if it doesn't exist // if (!context.Data.HasGraph(this._graphUri)) // { // throw new RdfUpdateException("Cannot LOAD into a Graph that does not exist in the Store"); // } // } try { // Load from the URI Graph g = new Graph(); UriLoader.Load(g, _sourceUri); if (context.Data.HasGraph(_graphUri)) { // Merge the Data into the existing Graph context.Data.GetModifiableGraph(_graphUri).Merge(g); } else { // Add New Graph to the Dataset g.BaseUri = _graphUri; context.Data.AddGraph(g); } } catch { if (!_silent) { throw; } } }
/// <summary> /// Adds a Graph into the Triple Store by retrieving it from the given Uri and using the selected Merge Behaviour /// </summary> /// <param name="graphUri">Uri of the Graph to add</param> /// <param name="mergeIfExists">Whether the Graph should be merged with an existing Graph with the same Base Uri</param> public override void AddFromUri(Uri graphUri, bool mergeIfExists) { //Check if it already exists bool exists = this._graphs.Contains(graphUri); if (exists && !mergeIfExists) { throw new RdfException("The Graph you tried to add already exists in the Graph Collection"); } else if (exists) { //Load into SqlGraph //The merge is implied here and will be handled by the Parsers base.AddFromUri(graphUri, mergeIfExists); } else { //Load into SqlGraph and add Graph g = new Graph(); UriLoader.Load(g, graphUri); this.Add(g, mergeIfExists); } }
public void SparqlFilterLazyDBPedia() { SparqlParameterizedString query = new SparqlParameterizedString(); query.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS)); query.CommandText = "SELECT * WHERE {?s ?p ?label . FILTER(ISLITERAL(?label) && LANGMATCHES(LANG(?label), \"en\")) } LIMIT 5"; TripleStore store = new TripleStore(); Graph g = new Graph(); UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Southampton")); store.Add(g); SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(query); Console.WriteLine(q.ToAlgebra().ToString()); Assert.IsTrue(q.ToAlgebra().ToString().Contains("LazyBgp"), "Should have been optimised to use a Lazy BGP"); Console.WriteLine(); LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store)); Object results = processor.ProcessQuery(q); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; foreach (SparqlResult r in rset) { Console.WriteLine(r.ToString()); } Assert.IsTrue(rset.Count == 5, "Expected exactly 5 results"); } else { Assert.Fail("Expected a SPARQL Result Set"); } }
public void RunConvert(String[] args) { //Single Help Argument means Show Help if (args.Length == 1 && (args[0].Equals("-h") || args[0].Equals("--help"))) { this.ShowUsage(); return; } //Set the Options if (!this.SetOptions(args)) { //If SetOptions returns false then some options were invalid and errors have been output to the error stream return; } if (this._input == null) { //If no Input then abort Console.Error.WriteLine("The required argument Input URI was not specified"); return; } if (this._writer == null && !this._count) { //If no Output Format was specified and the Count option was not specified we abort if (!this._quiet) { Console.Error.WriteLine("rdfConvert: No Ouput Format specified - using default serializer NTriples"); } this._writer = new NTriplesWriter(); } if (this._parser == null && !this._guess) { //Use guess mode if no specific input format or guess mode was specified if (!this._quiet) { Console.Error.WriteLine("rdfConvert: No Input Format was specified and the guess option was not specified - using default parser RDF/XML"); } this._parser = new RdfXmlParser(); } //Set Parser to Null if using guess mode if (this._guess) { this._parser = null; } if (this._parser != null && !this._ignoreWarnings) { //Output warnings to stderror if not ignoring warnings or using a mode where can't add a handler to the warning this._parser.Warning += this.ShowWarnings; if (this._writer != null) { this._writer.Warning += this.ShowWarnings; } } else if (!this._ignoreWarnings) { UriLoader.Warning += this.ShowWarnings; UriLoader.StoreWarning += this.ShowWarnings; FileLoader.Warning += this.ShowWarnings; FileLoader.StoreWarning += this.ShowWarnings; } //Try to parse in the Input try { if (!this._quiet) { if (this._parser != null) { Console.Error.WriteLine("rdfConvert: Parsing URI " + this._input + " with Parser " + this._parser.GetType().Name); } else { Console.Error.WriteLine("rdfConvert: Parsing URI " + this._input + " with guessing of Content Type"); } } if (this._input == "-") { //Read from Standard In if (this._guess) { StringParser.Parse(this._g, Console.In.ReadToEnd()); } else { this._parser.Load(this._g, new StreamReader(Console.OpenStandardInput())); } } else { try { Uri u = new Uri(this._input); if (u.IsAbsoluteUri) { //Valid Absolute URI UriLoader.Load(this._g, u, this._parser); } else { //If not an absolute URI then probably a filename FileLoader.Load(this._g, this._input, this._parser); } } catch (UriFormatException) { //If not a valid URI then probably a filename FileLoader.Load(this._g, this._input, this._parser); } } } catch (RdfParseException parseEx) { this.ShowErrors(parseEx, "Parse Error"); return; } catch (RdfException rdfEx) { this.ShowErrors(rdfEx, "RDF Error"); return; } catch (Exception ex) { this.ShowErrors(ex, "Error"); return; } if (!this._quiet) { Console.Error.WriteLine("rdfConvert: Parsing returned " + this._g.Triples.Count + " Triples"); } //Show only count if that was asked for if (this._count) { Console.WriteLine(this._g.Triples.Count); return; } //Show Namespaces if asked for if (this._showNamespaces) { foreach (String prefix in this._g.NamespaceMap.Prefixes) { Console.WriteLine(prefix + ": <" + this._g.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri + ">"); } } //Now do the serialization if (this._outputBase != null || this._useNullOutputBase) { //Set the Output Base URI if specified this._g.BaseUri = this._outputBase; } else if (this._useInputBase) { //Set the Output Base URI to the Input Base URI if specified //Have to reset this since parsing the Input may have changed the Base URI this._g.BaseUri = this._inputBase; } try { if (!this._quiet) { Console.Error.WriteLine("rdfConvert: Serializing with serializer " + this._writer.GetType().Name); } //Save the Graph to Standard Out this._writer.Save(this._g, Console.Out); } catch (RdfOutputException outEx) { this.ShowErrors(outEx, "Output Error"); return; } catch (RdfException rdfEx) { this.ShowErrors(rdfEx, "RDF Error"); return; } catch (Exception ex) { this.ShowErrors(ex, "Error"); return; } }
private bool SetOptions(String[] args) { if (args.Length == 0 || args.Length == 1 && args[0].Equals("-help")) { this.ShowUsage(); return(false); } String arg; int i = 0; while (i < args.Length) { arg = args[i]; if (arg.StartsWith("-uri:")) { if (this._mode == RdfQueryMode.Remote) { Console.Error.WriteLine("rdfQuery: Cannot specify input URIs as well as specifying a remote endpoint to query"); return(false); } String uri = arg.Substring(5); try { this._mode = RdfQueryMode.Local; //Try and parse RDF from the given URI if (!this._print) { Uri u = new Uri(uri); Graph g = new Graph(); UriLoader.Load(g, u); this._store.Add(g); } else { Console.Error.WriteLine("rdfQuery: Ignoring the input URI '" + uri + "' since -print has been specified so the query will not be executed so no need to load the data"); } } catch (UriFormatException uriEx) { Console.Error.WriteLine("rdfQuery: Ignoring the input URI '" + uri + "' since this is not a valid URI"); if (this._debug) { this.DebugErrors(uriEx); } } catch (RdfParseException parseEx) { Console.Error.WriteLine("rdfQuery: Ignoring the input URI '" + uri + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Parser Error: " + parseEx.Message); if (this._debug) { this.DebugErrors(parseEx); } } catch (Exception ex) { Console.Error.WriteLine("rdfQuery: Ignoring the input URI '" + uri + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Error: " + ex.Message); if (this._debug) { this.DebugErrors(ex); } } } else if (arg.StartsWith("-endpoint:")) { if (this._mode == RdfQueryMode.Local) { Console.Error.WriteLine("rdfQuery: Cannot specify a remote endpoint to query as well as specifying local files and/or input URIs"); return(false); } else if (this._mode == RdfQueryMode.Remote) { if (!(this._endpoint is FederatedSparqlRemoteEndpoint)) { this._endpoint = new FederatedSparqlRemoteEndpoint(this._endpoint); } } try { this._mode = RdfQueryMode.Remote; if (this._endpoint is FederatedSparqlRemoteEndpoint) { ((FederatedSparqlRemoteEndpoint)this._endpoint).AddEndpoint(new SparqlRemoteEndpoint(new Uri(arg.Substring(arg.IndexOf(':') + 1)))); } else { this._endpoint = new SparqlRemoteEndpoint(new Uri(arg.Substring(arg.IndexOf(':') + 1))); } } catch (UriFormatException uriEx) { Console.Error.WriteLine("rdfQuery: Unable to use remote endpoint with URI '" + arg.Substring(arg.IndexOf(':') + 1) + "' since this is not a valid URI"); if (this._debug) { this.DebugErrors(uriEx); } return(false); } } else if (arg.StartsWith("-output:") || arg.StartsWith("-out:")) { this._output = arg.Substring(arg.IndexOf(':') + 1); } else if (arg.StartsWith("-outformat:")) { String format = arg.Substring(arg.IndexOf(':') + 1); try { if (format.Contains("/")) { //MIME Type this._graphWriter = MimeTypesHelper.GetWriter(format); this._resultsWriter = MimeTypesHelper.GetSparqlWriter(format); } else { //File Extension this._graphWriter = MimeTypesHelper.GetWriterByFileExtension(format); this._resultsWriter = MimeTypesHelper.GetSparqlWriterByFileExtension(format); } } catch (RdfException) { Console.Error.WriteLine("rdfQuery: The file extension '" + format + "' could not be used to determine a MIME Type and select a writer - default writers will be used"); } } else if (arg.StartsWith("-syntax")) { if (arg.Contains(':')) { String syntax = arg.Substring(arg.IndexOf(':') + 1); switch (syntax) { case "1": case "1.0": this._parser.SyntaxMode = SparqlQuerySyntax.Sparql_1_0; break; case "1.1": this._parser.SyntaxMode = SparqlQuerySyntax.Sparql_1_1; break; case "E": case "e": this._parser.SyntaxMode = SparqlQuerySyntax.Extended; break; default: Console.Error.WriteLine("rdfQuery: The value '" + syntax + "' is not a valid query syntax specifier - assuming SPARQL 1.1 with Extensions"); this._parser.SyntaxMode = SparqlQuerySyntax.Extended; break; } } else { this._parser.SyntaxMode = SparqlQuerySyntax.Extended; } } else if (arg.StartsWith("-timeout:")) { long timeout; if (Int64.TryParse(arg.Substring(arg.IndexOf(':') + 1), out timeout)) { this._timeout = timeout; } else { Console.Error.WriteLine("rdfQuery: The value '" + arg.Substring(arg.IndexOf(':') + 1) + "' is not a valid timeout in milliseconds - default timeouts will be used"); } } else if (arg.StartsWith("-r:")) { arg = arg.Substring(arg.IndexOf(':') + 1); switch (arg) { case "rdfs": ((IInferencingTripleStore)this._store).AddInferenceEngine(new RdfsReasoner()); break; case "skos": ((IInferencingTripleStore)this._store).AddInferenceEngine(new SkosReasoner()); break; default: Console.Error.WriteLine("rdfQuery: The value '" + arg + "' is not a valid Reasoner - ignoring this option"); break; } } else if (arg.StartsWith("-partialResults")) { if (arg.Contains(':')) { bool partial; if (Boolean.TryParse(arg.Substring(arg.IndexOf(':') + 1), out partial)) { this._partialResults = partial; } else { Console.Error.WriteLine("rdfQuery: The value '" + arg.Substring(arg.IndexOf(':') + 1) + "' is not a valid boolean - partial results mode is disabled"); } } else { this._partialResults = true; } } else if (arg.StartsWith("-noopt")) { if (arg.Equals("-noopt")) { Options.QueryOptimisation = false; Options.AlgebraOptimisation = false; } else if (arg.Length >= 7) { String opts = arg.Substring(7); foreach (char c in opts.ToCharArray()) { if (c == 'a' || c == 'A') { Options.AlgebraOptimisation = false; } else if (c == 'q' || c == 'Q') { Options.QueryOptimisation = false; } else { Console.Error.WriteLine("rdfQuery: The value '" + c + "' as part of the -noopt argument is not supported - it has been ignored"); } } } } else if (arg.Equals("-nocache")) { Options.UriLoaderCaching = false; } else if (arg.Equals("-nobom")) { Options.UseBomForUtf8 = false; } else if (arg.Equals("-print")) { this._print = true; } else if (arg.Equals("-debug")) { this._debug = true; } else if (arg.StartsWith("-explain")) { this._explain = true; if (arg.Length > 9) { try { this._level = (ExplanationLevel)Enum.Parse(typeof(ExplanationLevel), arg.Substring(9)); this._level = (this._level | ExplanationLevel.OutputToConsoleStdErr | ExplanationLevel.Simulate) ^ ExplanationLevel.OutputToConsoleStdOut; } catch { Console.Error.WriteLine("rdfQuery: The argument '" + arg + "' does not specify a valid Explanation Level"); return(false); } } } else if (arg.Equals("-help")) { //Ignore Help Argument if other arguments present } else if (arg.StartsWith("-")) { //Report Invalid Argument Console.Error.WriteLine("rdfQuery: The argument '" + arg + "' is not a supported argument - it has been ignored"); } else if (i == args.Length - 1) { //Last Argument must be the Query this._query = arg; } else { //Treat as an input file if (this._mode == RdfQueryMode.Remote) { Console.Error.WriteLine("rdfQuery: Cannot specify local files as well as specifying a remote endpoint to query"); return(false); } try { this._mode = RdfQueryMode.Local; //Try and parse RDF from the given file if (!this._print) { Graph g = new Graph(); FileLoader.Load(g, arg); this._store.Add(g); } else { Console.Error.WriteLine("rdfQuery: Ignoring the local file '" + arg + "' since -print has been specified so the query will not be executed so no need to load the data"); } } catch (RdfParseException parseEx) { Console.Error.WriteLine("rdfQuery: Ignoring the local file '" + arg + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Parser Error: " + parseEx.Message); if (this._debug) { this.DebugErrors(parseEx); } } catch (Exception ex) { Console.Error.WriteLine("rdfQuery: Ignoring the local file '" + arg + "' due to the following error:"); Console.Error.WriteLine("rdfQuery: Error: " + ex.Message); if (this._debug) { this.DebugErrors(ex); } } } i++; } return(true); }
private void Expand(ExpansionContext context) { while (context.Uris.Count > 0) { //Get the next URI to expand upon and check it's not above the max expansion depth //Or that it's already a Graph in the Store UriToExpand u = context.GetNextUri();//context.Uris.Dequeue(); if (u == null) { return; } Debug.WriteLine("Expanding URI <" + u.Uri.ToString() + "> at Depth " + u.Depth); Debug.WriteLine(context.Uris.Count + " remaining to expand"); Debug.WriteLine("Got " + context.Store.Graphs.Count + " Graphs so far"); if (u.Depth > context.Profile.MaxExpansionDepth) { continue; } if (context.Store.HasGraph(u.Uri)) { continue; } //Try and retrieve RDF from the next URI Graph g = new Graph(); try { UriLoader.Load(g, u.Uri); } catch (RdfException rdfEx) { //Ignore this.DebugErrors("Error: Tried to expand URI <" + u.Uri.ToString() + "> but an RDF Error occurred", rdfEx); } catch (WebException webEx) { //Ignore this.DebugErrors("Error: Tried to expand URI <" + u.Uri.ToString() + "> but a HTTP Error occurred", webEx); } ExpandGraph(u, g, context); //If we've got any URIs to expand and we're not already multi-threading then spawn some //threads to share out the work if (!context.MultiThreading && context.Uris.Count > 0) { //REQ: Convert to an IAsyncResult pattern context.MultiThreading = true; List <Thread> threads = new List <Thread>(); for (int i = 0; i < Math.Min(context.Uris.Count, this._threadsToUse); i++) { threads.Add(new Thread(new ThreadStart(delegate { this.Expand(context); }))); } threads.ForEach(t => t.Start()); while (threads.Any(t => t.ThreadState == System.Threading.ThreadState.Running)) { Thread.Sleep(ThreadPollingInterval); } context.MultiThreading = false; } } }
/// <summary> /// Loads the terms from a given namespace /// </summary> /// <param name="namespaceUri">Namespace URI</param> /// <returns></returns> public static IEnumerable <NamespaceTerm> LoadNamespaceTerms(String namespaceUri) { //Don't load if already loaded if (_loadedNamespaces.Contains(namespaceUri)) { return(GetNamespaceTerms(namespaceUri)); } try { Graph g = new Graph(); try { UriLoader.Load(g, new Uri(namespaceUri)); if (g.Triples.Count == 0) { throw new Exception("Did not appear to receive an RDF Format from Namespace URI " + namespaceUri); } } catch { //Try and load from our local copy if there is one String prefix = GetDefaultPrefix(namespaceUri); if (!prefix.Equals(String.Empty)) { Stream localCopy = Assembly.GetExecutingAssembly().GetManifestResourceStream("VDS.RDF.Utilities.Editor.AutoComplete.Vocabularies." + prefix + ".ttl"); if (localCopy != null) { TurtleParser ttlparser = new TurtleParser(); ttlparser.Load(g, new StreamReader(localCopy)); } } } List <NamespaceTerm> terms = new List <NamespaceTerm>(); String termUri; //UriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)); IUriNode rdfsClass = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "Class")); IUriNode rdfsLabel = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "label")); IUriNode rdfsComment = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "comment")); IUriNode rdfProperty = g.CreateUriNode(new Uri(NamespaceMapper.RDF + "Property")); IUriNode rdfsDatatype = g.CreateUriNode(new Uri(NamespaceMapper.RDFS + "Datatype")); SparqlParameterizedString queryString = new SparqlParameterizedString(); queryString.CommandText = "SELECT ?term (STR(?label) AS ?RawLabel) (STR(?comment) AS ?RawComment) WHERE { {{?term a @class} UNION {?term a @property} UNION {?term a @datatype}} OPTIONAL {?term @label ?label} OPTIONAL {?term @comment ?comment} }"; queryString.SetParameter("class", rdfsClass); queryString.SetParameter("property", rdfProperty); queryString.SetParameter("datatype", rdfsDatatype); queryString.SetParameter("label", rdfsLabel); queryString.SetParameter("comment", rdfsComment); Object results = g.ExecuteQuery(queryString.ToString()); if (results is SparqlResultSet) { foreach (SparqlResult r in ((SparqlResultSet)results)) { termUri = r["term"].ToString(); if (termUri.StartsWith(namespaceUri)) { //Use the Comment as the label if available if (r.HasValue("RawComment")) { if (r["RawComment"] != null) { terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length), r["RawComment"].ToString())); continue; } } //Use the Label as the label if available if (r.HasValue("RawLabel")) { if (r["RawLabel"] != null) { terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length), r["RawLabel"].ToString())); continue; } } //Otherwise no label terms.Add(new NamespaceTerm(namespaceUri, termUri.Substring(namespaceUri.Length))); } } } lock (_terms) { terms.RemoveAll(t => _terms.Contains(t)); _terms.AddRange(terms.Distinct()); } } catch { //Ignore Exceptions - just means we won't have those namespace terms available } lock (_loadedNamespaces) { _loadedNamespaces.Add(namespaceUri); } return(GetNamespaceTerms(namespaceUri)); }
/// <summary> /// Tries to load a Graph based on information from the Configuration Graph. /// </summary> /// <param name="g">Configuration Graph.</param> /// <param name="objNode">Object Node.</param> /// <param name="targetType">Target Type.</param> /// <param name="obj">Output Object.</param> /// <returns></returns> public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj) { obj = null; IGraph output; // Check whether to use a specific Triple Collection INode collectionNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingTripleCollection))); try { if (collectionNode == null) { // Simple Graph creation output = (IGraph)Activator.CreateInstance(targetType); } else { // Graph with custom triple collection BaseTripleCollection tripleCollection = ConfigurationLoader.LoadObject(g, collectionNode) as BaseTripleCollection; if (tripleCollection == null) { throw new DotNetRdfConfigurationException("Unable to load the Graph identified by the Node '" + objNode.ToString() + "' as the dnr:usingTripleCollection points to an object which cannot be loaded as an instance of the required type BaseTripleCollection"); } output = (IGraph)Activator.CreateInstance(targetType, new Object[] { tripleCollection }); } } catch { // Any error means this loader can't load this type return(false); } // Now we want to find out where the data for the Graph is coming from // Data Source loading order is Graphs, Files, Strings, Databases, Stores, URIs IEnumerable <INode> sources; // Load from Graphs sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromGraph))); foreach (INode source in sources) { ConfigurationLoader.CheckCircularReference(objNode, source, "dnr:fromGraph"); Object graph = ConfigurationLoader.LoadObject(g, source); if (graph is IGraph) { output.Merge((IGraph)graph); } else { throw new DotNetRdfConfigurationException("Unable to load data from another Graph for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface"); } } // Load from Embedded Resources sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromEmbedded))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { EmbeddedResourceLoader.Load(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required"); } } // Load from Files sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { FileLoader.Load(output, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a file for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required"); } } // Load from Strings sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromString))); foreach (INode source in sources) { if (source.NodeType == NodeType.Literal) { StringParser.Parse(output, ((ILiteralNode)source).Value); } else { throw new DotNetRdfConfigurationException("Unable to load data from a string for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromString property is not a Literal Node as required"); } } IEnumerable <Object> connections; // Load from Stores IEnumerable <INode> stores = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromStore))); stores.All(s => !ConfigurationLoader.CheckCircularReference(objNode, s, "dnr:fromStore")); connections = stores.Select(s => ConfigurationLoader.LoadObject(g, s)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyWithUri))); foreach (Object store in connections) { if (store is IStorageProvider) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri || source.NodeType == NodeType.Literal) { ((IStorageProvider)store).LoadGraph(output, source.ToString()); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Generic Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else if (store is ITripleStore) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { output.Merge(((ITripleStore)store)[((IUriNode)source).Uri]); } else if (source.NodeType == NodeType.Literal) { output.Merge(((ITripleStore)store)[UriFactory.Create(((ILiteralNode)source).Value)]); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a Store for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromStore property points to an Object which cannot be loaded as an object which implements either the IStorageProvider/ITripleStore interface"); } } // Load from Datasets IEnumerable <INode> ds = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromDataset))); ds.All(d => !ConfigurationLoader.CheckCircularReference(objNode, d, ConfigurationLoader.PropertyFromDataset)); IEnumerable <Object> datasets = ds.Select(d => ConfigurationLoader.LoadObject(g, d)); sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyWithUri))); foreach (Object dataset in datasets) { if (dataset is ISparqlDataset) { foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { output.Merge(((ISparqlDataset)dataset)[((IUriNode)sources).Uri]); } else if (source.NodeType == NodeType.Literal) { output.Merge(((ISparqlDataset)dataset)[UriFactory.Create(((ILiteralNode)source).Value)]); } else { throw new DotNetRdfConfigurationException("Unable to load data from a Dataset for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:withUri property is not a URI/Literal Node as required"); } } } else { throw new DotNetRdfConfigurationException("Unable to load data from a Dataset for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values of the dnr:fromDataset property points to an Object which cannot be loaded as an object which implements the required ISparqlDataset interface"); } } // Finally load from Remote URIs sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromUri))); foreach (INode source in sources) { if (source.NodeType == NodeType.Uri) { UriLoader.Load(output, ((IUriNode)source).Uri); } else if (source.NodeType == NodeType.Literal) { UriLoader.Load(output, UriFactory.Create(((ILiteralNode)source).Value)); } else { throw new DotNetRdfConfigurationException("Unable to load data from a URI for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromUri property is not a URI/Literal Node as required"); } } // Then are we assigning a Base URI to this Graph which overrides any existing Base URI? INode baseUri = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyAssignUri))); if (baseUri != null) { if (baseUri.NodeType == NodeType.Uri) { output.BaseUri = ((IUriNode)baseUri).Uri; } else if (baseUri.NodeType == NodeType.Literal) { output.BaseUri = UriFactory.Create(((ILiteralNode)baseUri).Value); } else { throw new DotNetRdfConfigurationException("Unable to assign a new Base URI for the Graph identified by the Node '" + objNode.ToString() + "' as the value for the dnr:assignUri property is not a URI/Literal Node as required"); } } // Finally we'll apply any reasoners IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyReasoner))); foreach (INode reasoner in reasoners) { Object temp = ConfigurationLoader.LoadObject(g, reasoner); if (temp is IInferenceEngine) { ((IInferenceEngine)temp).Apply(output); } else { throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface"); } } obj = output; return(true); }