Ejemplo n.º 1
0
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (RemoteRadio.IsChecked != null)
                {
                    if (RemoteRadio.IsChecked.Value) // Remote Endpoint Mode
                    {
                        config.endpoint = new SparqlRemoteEndpoint(new Uri(UrlTextBox.Text));

                        MessageBox.Show("Application Connected To Endpoint Successfully", "Connected", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    else // File Mode
                    {
                        config.endpoint = null;

                        NTriplesParser ntp = new NTriplesParser();
                        config.myGraph.Clear();
                        ntp.Load(config.myGraph, PathTextBox.Text);

                        MessageBox.Show("Graph Loaded From File Successfully", "Connected", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR6", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 2
0
        public void LoadFromFile(string filePath, DatasourceFileType fileType)
        {
            try
            {
                switch (fileType)
                {
                case DatasourceFileType.Rdfxml:     // RDF/XML
                    RdfXmlParser rdfXmlParser = new RdfXmlParser(RdfXmlParserMode.Streaming);
                    myGraph = new Graph();
                    rdfXmlParser.Load(myGraph, filePath);
                    break;

                case DatasourceFileType.NTriples:     // NTriples
                    NTriplesParser ntp = new NTriplesParser();
                    myGraph = new Graph();
                    ntp.Load(myGraph, filePath);
                    break;
                }
            }
            catch (Exception ex)
            {
                MySoapFault fault = new MySoapFault();
                fault.Operation   = "LoadFromFile";
                fault.Reason      = "Error in loading datasource file into graph .";
                fault.Details     = ex.Message;
                fault.MoreDetails = ex.StackTrace;
                throw new FaultException <MySoapFault>(fault);
            }
        }
        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.InitialDirectory = Environment.CurrentDirectory;
                ofd.Filter           = "RDF Files (*.rdf)|*.rdf|Turtle Files (*.ttl)|*.ttl|All Files (*.*)|*.*";
                if (ofd.ShowDialog().Value)
                {
                    string filePath = ofd.FileName;

                    switch (ModeComboBox.SelectedIndex)
                    {
                    case 0:     // RDF/XML
                        RdfXmlParser rdfXmlParser = new RdfXmlParser(RdfXmlParserMode.Streaming);
                        myGraph = new Graph();
                        rdfXmlParser.Load(myGraph, filePath);
                        break;

                    case 1:     // NTriples
                        NTriplesParser ntp = new NTriplesParser();
                        myGraph = new Graph();
                        ntp.Load(myGraph, filePath);
                        break;
                    }

                    MessageBox.Show("Graph Loaded From File Successfully", "Connected", MessageBoxButton.OK,
                                    MessageBoxImage.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "ERROR", MessageBoxButton.OK);
            }
        }
Ejemplo n.º 4
0
        public void TestImportAndValidateSingleFile()
        {
            //var fileName = "bsbm_370k.nt";
            const string fileName = "bsbm_1M.nt";

            if (_storeManager.DoesStoreExist("bsbm_370k"))
            {
                _storeManager.DeleteStore("bsbm_370k");
                while (_storeManager.DoesStoreExist("bsbm_370k"))
                {
                    Thread.Sleep(10);
                }
            }
            using (var store = _storeManager.CreateStore("bsbm_370k"))
            {
                var jobId = Guid.NewGuid();
                using (var triplesStream = File.OpenRead(fileName))
                {
                    store.Import(jobId, triplesStream);
                }
                store.Commit(jobId);
            }

            using (var triplesStream = File.OpenRead(fileName))
            {
                using (var store = _storeManager.OpenStore("bsbm_370k"))
                {
                    var validatorSink = new ValidatorSink(store);
                    var parser        = new NTriplesParser();
                    parser.Parse(triplesStream, validatorSink, Constants.DefaultGraphUri);
                    Console.WriteLine("Validated {0} triples in store", validatorSink.ValidationCount);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns all metadata and the stored instances as graph.
        /// There are some adjustments to the data to make the metadata compatible with the Shacl validator.
        /// </summary>
        /// <returns></returns>
        private IGraph GetShapesGraph()
        {
            var shapes = _cacheService.GetOrAdd($"shapes-graph", () =>
            {
                var store = new TripleStore();
                var data  = _metadataService.GetAllShaclAsGraph();

                store.Add(data);

                ModifiyShapesForTargetClass(store);
                ModifyShapesForShaclClass(store);

                var dataGraph = store.Graphs.FirstOrDefault(t => t.BaseUri?.OriginalString == data.BaseUri.OriginalString);

                if (dataGraph == null)
                {
                    throw new ArgumentNullException("Shapes graph is null");
                }

                NTriplesWriter writer = new NTriplesWriter(NTriplesSyntax.Original);
                var shapes            = VDS.RDF.Writing.StringWriter.Write(dataGraph, writer);

                return(shapes);
            });

            var shapesGraph = new VDS.RDF.Graph(true);
            var reader      = new StringReader(shapes);

            var nTriplesParser = new NTriplesParser();

            nTriplesParser.Load(shapesGraph, reader);

            return(shapesGraph);
        }
Ejemplo n.º 6
0
        public void TestBrightstarParserStillFaster()
        {
            var t = new Stopwatch();

            t.Start();
            using (var fs = new FileStream(TestPaths.DataPath + "BSBM_370k.nt", FileMode.Open))
            {
                var parser = new NTriplesParser();
                parser.Parse(fs, new NoopParser(), Constants.DefaultGraphUri);
            }
            t.Stop();
            Console.WriteLine("Time for Brightstar Parser is " + t.ElapsedMilliseconds);

            var t2 = new Stopwatch();

            t2.Start();
            using (var fs = new FileStream(TestPaths.DataPath + "BSBM_370k.nt", FileMode.Open))
            {
                var parser = new VDS.RDF.Parsing.NTriplesParser();
                parser.Load(new NoopParser(), new StreamReader(fs));
            }
            t2.Stop();
            Console.WriteLine("Time for dotNetRDF Parser is " + t2.ElapsedMilliseconds);

            Assert.That(t.ElapsedMilliseconds, Is.LessThan(t2.ElapsedMilliseconds));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Imports triples from the stream provided
        /// </summary>
        /// <param name="store">The store to import triples into</param>
        /// <param name="jobId">The GUID identifier for the import job</param>
        /// <param name="triples">The stream to read the triples from</param>
        /// <param name="graphUri">The URI of the graph to import the triples into, or null to import into the default graph</param>
        public static void Import(this IStore store, Guid jobId, Stream triples, Uri graphUri = null)
        {
            var tripleParser = new NTriplesParser();

            tripleParser.Parse(triples, new StoreTripleSink(store, jobId, 500000),
                               graphUri == null ? Constants.DefaultGraphUri : graphUri.ToString());
        }
Ejemplo n.º 8
0
 public void Run()
 {
     var p = new NTriplesParser();
     using (var fileReader = new StreamReader(_srcPath))
     {
         p.Parse(fileReader, this, Constants.DefaultGraphUri);
     }
 }
Ejemplo n.º 9
0
        public void TestBasicNtriples()
        {
            var ntp = new NTriplesParser();

            using (var fs = new FileStream("simple.txt", FileMode.Open))
            {
                ntp.Parse(fs, new NoopParser(), Constants.DefaultGraphUri);
            }
        }
Ejemplo n.º 10
0
        public void SparqlQuery()
        {
            TripleStore store = new TripleStore();
            var         g     = new Graph();

            var parser = new NTriplesParser();

            parser.Load(g, new StringReader(
                            @"<http://example.org/a> <http://example.org/b> <http://example.org/c>.
  <http://example.org/a> <http://example.org/b> <http://example.org/d>.
  <http://example.org/a> <http://example.org/b> <http://example.org/e>.
  <http://example.org/d> <http://example.org/f> <http://example.org/g>."));

            store.Add(g);

            // Normal SPARQL results ARE NOT rdf data. But a rows of bindings.
            Object results = store.ExecuteQuery("SELECT * WHERE {?s ?p ?o}");

            if (results is SparqlResultSet)
            {
                var rset = (SparqlResultSet)results;
                Assert.AreEqual(4, rset.Count);

                foreach (SparqlResult result in rset)
                {
                    Console.WriteLine(result.ToString());
                }
            }

            // SPARQL can be used to construct RDF as a result of the query. This can be loaded into a graph
            SparqlQueryParser sparqlparser = new SparqlQueryParser();
            SparqlQuery       query        = sparqlparser.ParseFromString("CONSTRUCT { ?s ?p ?o } WHERE {?s ?p ?o}");

            results = store.ExecuteQuery(query);
            if (results is IGraph)
            {
                IGraph gr = (IGraph)results;
                foreach (Triple t in gr.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
            }

            // SPARQL works by matching patterns
            results = store.ExecuteQuery("SELECT * WHERE {<http://example.org/a> ?p ?o}");
            if (results is SparqlResultSet)
            {
                var rset = (SparqlResultSet)results;
                Assert.AreEqual(3, rset.Count);

                foreach (SparqlResult result in rset)
                {
                    Console.WriteLine(result.ToString());
                }
            }
        }
Ejemplo n.º 11
0
        public void TestBrightstarParser()
        {
            var t = new Stopwatch();

            t.Start();
            using (var fs = new FileStream("BSBM_370k.nt", FileMode.Open))
            {
                var parser = new NTriplesParser();
                parser.Parse(fs, new NoopParser(), Constants.DefaultGraphUri);
            }
            t.Stop();
            Console.WriteLine("Time for Brightstar Parser is " + t.ElapsedMilliseconds);
        }
Ejemplo n.º 12
0
        private static IGraph Load(string source)
        {
            var result           = new Graph();
            var graphHandler     = new GraphHandler(result);
            var strippingHandler = new StripStringHandler(graphHandler);
            var parser           = new NTriplesParser();

            using (var reader = new StringReader(source))
            {
                parser.Load(strippingHandler, reader);
            }

            return(result);
        }
Ejemplo n.º 13
0
        internal TransactionPreconditionsFailedException(string existenceFailures, string nonexistenceFailures)
            : base("Transaction preconditions were not met.")
        {
            FailedPreconditions = existenceFailures;
            if (existenceFailures != null)
            {
                try
                {
#if WINDOWS_PHONE
                    _invalidSubjects = new Dictionary <string, bool>();
#else
                    _invalidSubjects = new HashSet <string>();
#endif
                    var p = new NTriplesParser();
                    this._parsingNonexistenceFailures = false;
                    using (var rdr = new StringReader(existenceFailures))
                    {
                        p.Parse(rdr, this, Constants.DefaultGraphUri);
                    }
                }
                catch
                {
                    // Ignore any errors when trying to parse the failed preconditions
                }
            }

            FailedNonExistencePreconditions = nonexistenceFailures;
            if (nonexistenceFailures != null)
            {
                try
                {
#if WINDOWS_PHONE
                    _invalidNonExistenceSubjects = new Dictionary <string, bool>();
#else
                    _invalidNonExistenceSubjects = new HashSet <string>();
#endif
                    this._parsingNonexistenceFailures = true;
                    var p = new NTriplesParser();
                    using (var rdr = new StringReader(nonexistenceFailures))
                    {
                        p.Parse(rdr, this, Constants.DefaultGraphUri);
                    }
                }
                catch
                {
                    // Ignore errors when trying to parse the failed preconditions
                }
            }
        }
        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                NTriplesParser ntp = new NTriplesParser();
                myGraph = new Graph();
                ntp.Load(myGraph, dbpeidaNtFileName);

                MessageBox.Show("Loaded", "Done", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR5", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 15
0
        public void TestExportWhileWriting()
        {
            var storeName = Guid.NewGuid().ToString();
            var client    = GetClient();

            client.CreateStore(storeName);
            var batch1 = MakeTriples(0, 50000);
            var batch2 = MakeTriples(50000, 51000);
            var batch3 = MakeTriples(51000, 52000);
            var batch4 = MakeTriples(52000, 53000);

            // Verify batch size
            var p           = new NTriplesParser();
            var counterSink = new CounterTripleSink();

            p.Parse(new StringReader(batch1), counterSink, Constants.DefaultGraphUri);
            Assert.AreEqual(50000, counterSink.Count);

            var jobInfo = client.ExecuteTransaction(storeName, String.Empty, String.Empty, batch1);

            Assert.AreEqual(true, jobInfo.JobCompletedOk);

            // Second export with parallel store writes
            var exportJobInfo = client.StartExport(storeName, storeName + "_export.nt");

            jobInfo = client.ExecuteTransaction(storeName, null, null, batch2);
            Assert.AreEqual(true, jobInfo.JobCompletedOk);
            exportJobInfo = client.GetJobInfo(storeName, exportJobInfo.JobId);
            Assert.IsTrue(exportJobInfo.JobStarted, "Test inconclusive - export job completed before end of first concurrent import job."); // This is just to check that the export is still running while at least one commit occurs
            jobInfo = client.ExecuteTransaction(storeName, null, null, batch3);
            Assert.AreEqual(true, jobInfo.JobCompletedOk);
            jobInfo = client.ExecuteTransaction(storeName, null, null, batch4);
            Assert.AreEqual(true, jobInfo.JobCompletedOk);
            while (!exportJobInfo.JobCompletedOk)
            {
                Assert.IsFalse(exportJobInfo.JobCompletedWithErrors);
                Thread.Sleep(1000);
                exportJobInfo = client.GetJobInfo(storeName, exportJobInfo.JobId);
            }

            var exportFile = new FileInfo("c:\\brightstar\\import\\" + storeName + "_export.nt");

            Assert.IsTrue(exportFile.Exists);
            var lineCount = File.ReadAllLines(exportFile.FullName).Where(x => !String.IsNullOrEmpty(x)).Count();

            Assert.AreEqual(50000, lineCount);
        }
 internal TransactionPreconditionsFailedException(string failedTriples)
     : base("Transaction preconditions were not met.")
 {
     FailedPreconditions = failedTriples;
     try
     {
         _invalidSubjects = new List <string>();
         var p = new NTriplesParser();
         using (var rdr = new StringReader(failedTriples))
         {
             p.Parse(rdr, this, Constants.DefaultGraphUri);
         }
     }
     catch
     {
         // Ignore any errors when trying to parse the failed preconditions
     }
 }
Ejemplo n.º 17
0
        public IActionResult LoadFileG()
        {
            TripleStore store = new TripleStore();


            IGraph g = new Graph();

            NTriplesParser ntparser = new NTriplesParser();

            //Load using Filename
            ntparser.Load(g, new StreamReader("Data/anime_dataset.nt"));
            g.NamespaceMap.AddNamespace("schema", new Uri("http://schema.org/"));
            store.Add(g);

            /*
             * ITripleFormatter formatter = new TurtleFormatter(g);
             * Console.WriteLine("------------------------");
             * //Print triples with this formatter
             * foreach (Triple t in g.Triples)
             * {
             *  Console.WriteLine(t.ToString());
             * }
             */


            //Execute a raw SPARQL Query
            //Should get a SparqlResultSet back from a SELECT query

            Object results = store.ExecuteQuery("PREFIX schema: <http://schema.org/> SELECT * WHERE {?iri a schema:TVSeries .?iri schema:image ?image.}LIMIT 10");


            if (results is SparqlResultSet)
            {
                //Print out the Results
                SparqlResultSet rset = (SparqlResultSet)results;
                foreach (SparqlResult result in rset)
                {
                    Console.WriteLine(result.ToString());
                }
            }


            return(View());
        }
Ejemplo n.º 18
0
        public void TestBackslashEscape()
        {
            const string ntriples = @"<http://example.org/s> <http://example.org/p1> ""c:\\users""
<http://example.org/s> <http://example.org/p2> ""\\users\\tom""";
            var          parser   = new NTriplesParser();
            var          sink     = new LoggingTripleSink();

            parser.Parse(new StringReader(ntriples), sink, "http://example.org/g");

            Assert.That(sink.Triples, Has.Count.EqualTo(2));
            var triple1 = sink.Triples.FirstOrDefault(t => t.Predicate.Equals("http://example.org/p1"));
            var triple2 = sink.Triples.FirstOrDefault(t => t.Predicate.Equals("http://example.org/p2"));

            Assert.That(triple1, Is.Not.Null);
            Assert.That(triple1.IsLiteral);
            Assert.That(triple1.Object, Is.EqualTo(@"c:\users"));
            Assert.That(triple2, Is.Not.Null);
            Assert.That(triple2.IsLiteral);
            Assert.That(triple2.Object, Is.EqualTo(@"\users\tom"));
        }
Ejemplo n.º 19
0
        public void ParsingTurtleW3CComplexPrefixedNames10()
        {
            NTriplesFormatter formatter = new NTriplesFormatter();

            Graph ttl = new Graph();

            ttl.LoadFromFile(@"resources\\turtle11\localName_with_non_leading_extras.ttl");
            Assert.IsFalse(ttl.IsEmpty);
            Console.WriteLine("Subject from Turtle: " + ttl.Triples.First().Subject.ToString(formatter));

            Graph          nt     = new Graph();
            NTriplesParser parser = new NTriplesParser();

            parser.Warning += TestTools.WarningPrinter;
            nt.LoadFromFile(@"resources\\turtle11\localName_with_non_leading_extras.nt", parser);
            Assert.IsFalse(nt.IsEmpty);
            Console.WriteLine("Subject from NTriples: " + nt.Triples.First().Subject.ToString(formatter));

            Assert.AreEqual(ttl.Triples.First().Subject, nt.Triples.First().Subject, "Subjects should be equal");
        }
Ejemplo n.º 20
0
        public void WritingNTriplesCharEscaping()
        {
            TurtleParser   parser   = new TurtleParser();
            NTriplesParser ntparser = new NTriplesParser();
            NTriplesWriter ntwriter = new NTriplesWriter();

            foreach (String sample in samples)
            {
                Graph g = new Graph();
                Console.WriteLine("Original RDF Fragment");
                Console.WriteLine(prefix + sample);
                StringParser.Parse(g, prefix + sample, parser);
                Console.WriteLine();
                Console.WriteLine("Triples in Original");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                String serialized = VDS.RDF.Writing.StringWriter.Write(g, ntwriter);
                Console.WriteLine("Serialized RDF Fragment");
                Console.WriteLine(serialized);

                Graph h = new Graph();
                StringParser.Parse(h, serialized, ntparser);

                Console.WriteLine();
                Console.WriteLine("Triples in Serialized");
                foreach (Triple t in g.Triples)
                {
                    Console.WriteLine(t.ToString());
                }
                Console.WriteLine();

                Assert.AreEqual(g, h, "Graphs should have been equal");

                Console.WriteLine("Graphs were equal as expected");
                Console.WriteLine();
            }
        }
Ejemplo n.º 21
0
        public void TestImportEscaping()
        {
            var parser = new NTriplesParser();
            var sink   = new LoggingTripleSink();

            using (
                var stream = new FileStream(TestPaths.DataPath + "escaping.nt", FileMode.Open, FileAccess.Read,
                                            FileShare.ReadWrite))
            {
                parser.Parse(stream, sink, "http://example.org/g");
            }

            Assert.That(sink.Triples, Has.Count.EqualTo(8));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("simple literal"));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("backslash:\\"));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("dquote:\""));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("newline:\n"));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("tab:\t"));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("\u00E9"));
            Assert.That(sink.Triples, Has.Some.Property("Object").EqualTo("\u20AC"));
        }
Ejemplo n.º 22
0
 public void Run()
 {
     try
     {
         var jobGuid = Guid.Parse(_jobId);
         using (_importStream = _importSource.OpenRead())
         {
             var parser = new NTriplesParser();
             _importTripleSink = new StoreTripleSink(_worker.WriteStore, jobGuid,
                                                     Configuration.TransactionFlushTripleCount);
             parser.Parse(_importStream, this, _graphUri);
             _importStream.Close();
         }
         _worker.WriteStore.Commit(jobGuid);
         _worker.InvalidateReadStore();
     }
     catch (RdfParserException parserException)
     {
         Logging.LogError(
             BrightstarEventId.ImportDataError,
             "Encountered parser error : {0}", parserException);
         _statusCallback(_jobId,
                         String.Format("Import failed due to parser error: {0}", parserException));
         Errors       = true;
         ErrorMessage = parserException.HaveLineNumber
                            ? String.Format("Parser error at line {0}: {1}", parserException.LineNumber,
                                            parserException.Message)
                            : String.Format("Parser error: {0}", parserException.Message);
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.JobProcessingError,
                          "Error processing import job on source " + _importSource + ". Error Message: " +
                          ex.Message + " Stack trace: " + ex.StackTrace);
         throw;
     }
 }
Ejemplo n.º 23
0
        private static void ProcessTriplesDeserialization(Stream stream, IGraph graph, string accepted)
        {
            IRdfReader reader = null;

            switch (accepted)
            {
            case TextTurtle:
            case ApplicationTurtle:
            case ApplicationXTurtle:
            case TextNTriplesTurtle:
                reader = new TurtleParser();
                break;

            case ApplicationOwlXml:
            case ApplicationRdfXml:
                reader = new RdfXmlParser();
                break;

            case ApplicationNTriples:
            case ApplicationNTriples2:
            case ApplicationXnTriples:
            case ApplicationRdfTriples:
            case TextPlain:
                reader = new NTriplesParser();
                break;

            case TextN3:
            case TextRdfN3:
                reader = new Notation3Parser();
                break;
            }

            using (var textWriter = new StreamReader(stream))
            {
                reader.Load(graph, textWriter);
            }
        }
Ejemplo n.º 24
0
        // srcFn stazeno z https://wiki.dbpedia.org/downloads-2016-10, Raw html tables
        public static void parseTTL()
        {
            void parseTTLLow(string lang)
            {
                var srcFn = $"{Dirs.wikiesDbpedia}raw_tables_{lang}.ttl";

                if (!File.Exists(srcFn))
                {
                    return;
                }
                Console.WriteLine(srcFn);
                var destFn    = $"{Dirs.root}dbpedia\\{lang}.txt";
                var ttlparser = new NTriplesParser();

                using (var graph = new MyGraph(destFn))
                    using (var rdr = new StreamReader(srcFn))
                        ttlparser.Load(graph, rdr);
            }

            //foreach (var lang in langs) parseTTLLow(lang);
            Parallel.ForEach(langs, new ParallelOptions {
                MaxDegreeOfParallelism = 4
            }, parseTTLLow);
        }
Ejemplo n.º 25
0
        public void TestBasicNQuads()
        {
            var ntp = new NTriplesParser();

            ntp.Parse(new FileStream("nquads.txt", FileMode.Open), new NoopParser(), Constants.DefaultGraphUri);
        }
Ejemplo n.º 26
0
        public override void Run()
        {
            try
            {
                StoreWorker.TransactionLog.LogStartTransaction(this);

                var writeStore = StoreWorker.WriteStore;

                // process preconditions
                Logging.LogInfo("UpdateTransaction {0} - processing preconditions", JobId);
                try
                {
                    var preconditionSink = new PreconditionSink(writeStore, PreconditionSink.PreconditionType.ExistsPrecondition);
                    var parser           = new NTriplesParser();
                    parser.Parse(new StringReader(_preconditions), preconditionSink, _defaultGraphUri);
                    if (preconditionSink.FailedPreconditionCount > 0)
                    {
                        throw new PreconditionFailedException(preconditionSink.FailedPreconditionCount, preconditionSink.GetFailedPreconditions(),
                                                              0, String.Empty);
                    }
                }
                catch (RdfParserException parserException)
                {
                    throw new BrightstarClientException("Syntax error in preconditions.", parserException);
                }

                // process deletes
                Logging.LogInfo("UpdateTransaction {0} - processing deletes", JobId);
                try
                {
                    var delSink = new DeletePatternSink(writeStore);
                    var parser  = new NTriplesParser();
                    parser.Parse(new StringReader(_deletePatterns), delSink, _defaultGraphUri);
                }
                catch (RdfParserException parserException)
                {
                    throw new BrightstarClientException("Syntax error in delete patterns.", parserException);
                }

                try
                {
                    // insert data
                    Logging.LogInfo("UpdateTransaction {0} - processing inserts", JobId);
                    var parser = new NTriplesParser();
                    parser.Parse(new StringReader(_insertData),
                                 new StoreTripleSink(writeStore, JobId, Configuration.TransactionFlushTripleCount),
                                 _defaultGraphUri);
                }
                catch (RdfParserException parserException)
                {
                    throw new BrightstarClientException("Syntax error in triples to add.", parserException);
                }

                // commit changes
                Logging.LogInfo("UpdateTransaction {0} - committing changes", JobId);
                writeStore.Commit(JobId);

                // change read store
                Logging.LogInfo("UpdateTransaction {0} - invalidating read store", JobId);
                StoreWorker.InvalidateReadStore();

                // log txn completed
                Logging.LogInfo("UpdateTransaction {0} - logging completion", JobId);
                StoreWorker.TransactionLog.LogEndSuccessfulTransaction(this);
                Logging.LogInfo("UpdateTransaction {0} - done", JobId);
            }
            catch (PreconditionFailedException ex)
            {
                StoreWorker.TransactionLog.LogEndFailedTransaction(this);
                Logging.LogInfo("Preconditions failed in UpdateTransaction ({0}): Count={1}, Triples={2}", JobId, ex.ExistenceFailureCount, ex.ExistenceFailedTriples);
                throw;
            }
            catch (BrightstarClientException ex)
            {
                StoreWorker.TransactionLog.LogEndFailedTransaction(this);
                Logging.LogError(BrightstarEventId.TransactionClientError,
                                 "Client error reported in UpdateTransaction ({0}): {1}", JobId, ex.InnerException.ToString());
                throw;
            }
            catch (Exception ex)
            {
                StoreWorker.TransactionLog.LogEndFailedTransaction(this);
                Logging.LogError(BrightstarEventId.TransactionServerError,
                                 "Unexpected exception caught in UpdateTransaction ({0}): {1}", JobId, ex);
                throw;
            }
        }
Ejemplo n.º 27
0
        public override Stream GetFragment(string id, string contentType)
        {
            // need to see which definition we match
            ResourcePublishingDefinition definition = null;
            UriTemplateMatch             match      = null;

            foreach (var resourcePublishingDefinition in _publishingDefinitions)
            {
                var newuri = new Uri(id);
                match = resourcePublishingDefinition.UriTemplate.Match(resourcePublishingDefinition.ResourcePrefix, newuri);
                if (match != null)
                {
                    definition = resourcePublishingDefinition;
                    break;
                }
            }

            if (definition == null)
            {
                throw new Exception("Unable to find matching definition for uri " + id);
            }

            var sb = new StringBuilder();

            foreach (var generationDefinition in definition.FragmentGenerationDefinitions)
            {
                try
                {
                    var data = ExecuteQuery(_dataSourceConnectionString, generationDefinition.FragmentQuery.Replace("[[id]]", match.BoundVariables["id"]));
                    foreach (DataRow row in data.Rows)
                    {
                        var dra = new DbDataRow(row);
                        foreach (var line in generationDefinition.RdfTemplateLines)
                        {
                            var linePattern = new NTripleLinePattern(line);
                            linePattern.GenerateNTriples(sb, dra, generationDefinition.GenericTemplateExcludeColumns, contentType.Equals("xml"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogError(1, "Error processing definition {0} {1} {2} {3}", ex.Message, generationDefinition.SnapshotQuery, _dataSourceConnectionString, ex.StackTrace);
                }
            }

            try
            {
                var g          = new Graph();
                var parser     = new NTriplesParser(TokenQueueMode.SynchronousBufferDuringParsing);
                var triplesStr = sb.ToString();
                parser.Load(g, new StringReader(triplesStr));

                if (contentType.Equals("xml"))
                {
                    var ms           = new MemoryStream();
                    var sw           = new StreamWriter(ms, Encoding.UTF8);
                    var rdfxmlwriter = new RdfXmlWriter();

                    var strw = new System.IO.StringWriter();
                    rdfxmlwriter.Save(g, strw);
                    var data = strw.ToString();

                    data = data.Replace("~~~2B~~~", "%2B");
                    data = data.Replace("~~~SLASH~~~", "%2F");
                    data = data.Replace("utf-16", "utf-8");
                    sw.Write(data);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
                else
                {
                    var ms = new MemoryStream();
                    var sw = new StreamWriter(ms);
                    sw.Write(triplesStr);
                    sw.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return(ms);
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(1, "Error getting fragment {0} {1}", ex.Message, ex.StackTrace);
                throw;
            }
        }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            // tipos de graph e hello world
            //IGraph g = new Graph();
            //IUriNode dotnetRDF = g.CreateUriNode(UriFactory.Create("http://www.dotnetrdf.org"));
            //IUriNode says = g.CreateUriNode(UriFactory.Create("http://example.org/says"));
            //ILiteralNode helloWorld = g.CreateLiteralNode("Hello World");
            //ILiteralNode bonjourMonde = g.CreateLiteralNode("Bonjour tout le Monde", "fr");

            //g.Assert(new Triple(dotnetRDF, says, helloWorld));
            //g.Assert(new Triple(dotnetRDF, says, bonjourMonde));

            // somente para debug
            //foreach (Triple t in graph.Triples)
            //{
            //    Console.WriteLine(t);
            //}

            // formato NT válido
            //NTriplesWriter nTWriter = new NTriplesWriter();
            //nTWriter.Save(g, "HelloWorld.nt");


            // definição XML
            //RdfXmlWriter rdfXmlWriter = new RdfXmlWriter();
            //rdfXmlWriter.Save(g, "HelloWorld.rdf");

            // não funcionou
            //IGraph g = new Graph();
            //var a = new TripleStore();
            //UriLoader.Load(graph2, new Uri("http://dbpedia.org/resource/Barack_Obama"));
            //UriLoader.Load(graph2, new Uri("http://dbpedia.org/page/Barack_Obama"));
            //UriLoader.Load(a, new Uri("http://dbpedia.org/page/Battle_of_Waterloo"));

            // funcionou
            //NTriplesParser nTriplesParser = new NTriplesParser();
            //nTriplesParser.Load(graph2, "Barack_Obama.ntriples");

            // working with graph
            //Graph g = new Graph();
            //g.BaseUri = new Uri("http://example.org/base");
            //var isEmpty = g.IsEmpty;
            //var triples = g.Triples;

            //First define a SPARQL Endpoint for DBPedia
            SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"));

            //Next define our query
            //We're going to ask DBPedia to describe the first thing it finds which is a Person
            String query = "DESCRIBE ?person WHERE {?person a <http://dbpedia.org/ontology/Person>} LIMIT 1";

            //Get the result
            var g = endpoint.QueryWithResultGraph(query);

            TripleStore store          = new TripleStore();
            Graph       graph          = new Graph();
            var         nTriplesParser = new NTriplesParser();

            UriLoader.Load(graph, new Uri("http://dbpedia.org/resource/List_of_ongoing_armed_conflicts"), nTriplesParser);
            store.Add(graph);
            // store.Triples as the conflicts itself?

            Console.ReadKey();
        }
Ejemplo n.º 29
0
        public static Object LoadFromReader(Reader r, string baseUri, org.openrdf.rio.RDFFormat rdff)
        {
            Object obj;

            if (rdff == dotSesameFormats.RDFFormat.N3)
            {
                obj = new Graph();
                if (baseUri != null)
                {
                    ((IGraph)obj).BaseUri = new Uri(baseUri);
                }
                Notation3Parser parser = new Notation3Parser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.NTRIPLES)
            {
                obj = new Graph();
                if (baseUri != null)
                {
                    ((IGraph)obj).BaseUri = new Uri(baseUri);
                }
                NTriplesParser parser = new NTriplesParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.RDFXML)
            {
                obj = new Graph();
                if (baseUri != null)
                {
                    ((IGraph)obj).BaseUri = new Uri(baseUri);
                }
                RdfXmlParser parser = new RdfXmlParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else if (rdff == dotSesameFormats.RDFFormat.TRIG)
            {
                obj = new TripleStore();
                TriGParser trig = new TriGParser();
                trig.Load((ITripleStore)obj, new StreamParams(r.ToDotNetReadableStream().BaseStream));
            }
            else if (rdff == dotSesameFormats.RDFFormat.TRIX)
            {
                obj = new TripleStore();
                TriXParser trix = new TriXParser();
                trix.Load((ITripleStore)obj, new StreamParams(r.ToDotNetReadableStream().BaseStream));
            }
            else if (rdff == dotSesameFormats.RDFFormat.TURTLE)
            {
                obj = new Graph();
                if (baseUri != null)
                {
                    ((IGraph)obj).BaseUri = new Uri(baseUri);
                }
                TurtleParser parser = new TurtleParser();
                parser.Load((IGraph)obj, r.ToDotNetReadableStream());
            }
            else
            {
                throw new RdfParserSelectionException("The given Input Format is not supported by dotNetRDF");
            }

            return(obj);
        }
Ejemplo n.º 30
0
        /// <summary>
        /// https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Reading-RDF
        /// </summary>
        public void PlayWithReadingRdf()
        {
            #region Reading RDF from Files
            IGraph       g         = new Graph();
            IGraph       h         = new Graph();
            TurtleParser ttlparser = new TurtleParser();

            // Load using a Filename
            ttlparser.Load(g, "HelloWorld.ttl");

            // Load using a StreamReader
            ttlparser.Load(h, new StreamReader("HelloWorld.ttl"));

            try
            {
                IGraph         g2       = new Graph();
                NTriplesParser ntparser = new NTriplesParser();

                //Load using Filename
                ntparser.Load(g2, "HelloWorld.nt");
            }
            catch (RdfParseException parseEx)
            {
                //This indicates a parser error e.g unexpected character, premature end of input, invalid syntax etc.
                Console.WriteLine("Parser Error");
                Console.WriteLine(parseEx.Message);
            }
            catch (RdfException rdfEx)
            {
                //This represents a RDF error e.g. illegal triple for the given syntax, undefined namespace
                Console.WriteLine("RDF Error");
                Console.WriteLine(rdfEx.Message);
            }
            #endregion

            #region Reading RDF from URIs

            try
            {
                IGraph g3 = new Graph();
                //UriLoader.Load(g3, new Uri("http://dbpedia.org/resource/Barack_Obama"));
            }
            catch (RdfParseException parseEx)
            {
                //This indicates a parser error e.g unexpected character, premature end of input, invalid syntax etc.
                Console.WriteLine("Parser Error");
                Console.WriteLine(parseEx.Message);
            }
            catch (RdfException rdfEx)
            {
                //This represents a RDF error e.g. illegal triple for the given syntax, undefined namespace
                Console.WriteLine("RDF Error");
                Console.WriteLine(rdfEx.Message);
            }
            #endregion

            #region Reading RDF from Embedded Resources

            try
            {
                IGraph g4 = new Graph();
                EmbeddedResourceLoader.Load(g4, "embedded.ttl, PlayWithDotNetRDF");
                Console.WriteLine(g4.IsEmpty);
            }
            catch (RdfParseException parseEx)
            {
                //This indicates a parser error e.g unexpected character, premature end of input, invalid syntax etc.
                Console.WriteLine("Parser Error");
                Console.WriteLine(parseEx.Message);
            }
            catch (RdfException rdfEx)
            {
                //This represents a RDF error e.g. illegal triple for the given syntax, undefined namespace
                Console.WriteLine("RDF Error");
                Console.WriteLine(rdfEx.Message);
            }
            #endregion

            #region Reading RDF from Strings

            Graph g5 = new Graph();
            StringParser.Parse(g5, "<http://example.org/a> <http://example.org/b> <http://example.org/c>.");

            Graph          g6     = new Graph();
            NTriplesParser parser = new NTriplesParser();
            parser.Load(g6, new StringReader("<http://example.org/a> <http://example.org/b> <http://example.org/c>."));

            #endregion

            #region Store Parsers

            /*
             * TripleStore store = new TripleStore();
             * TriGParser trigparser = new TriGParser();
             *
             * //Load the Store
             * trigparser.Load(store, "Example.trig");
             */

            #endregion

            #region Advanced Parsing

            // Create a Handler and use it for parsing
            CountHandler handler      = new CountHandler();
            TurtleParser turtleParser = new TurtleParser();
            turtleParser.Load(handler, "HelloWorld.ttl");

            //Print the resulting count
            Console.WriteLine(handler.Count + " Triple(s)");

            // https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Handlers-API
            #endregion

            /*
             * Parser Class         Supported Input
             * NTriplesParser       NTriples
             * Notation3Parser  Notation 3, Turtle, NTriples, some forms of TriG
             * NQuadsParser         NQuads, NTriples
             * RdfAParser           RDFa 1.0 embedded in (X)HTML, some RDFa 1.1 support
             * RdfJsonParser        RDF/JSON (Talis specification)
             * RdfXmlParser         RDF/XML
             * TriGParser           TriG
             * TriXParser           TriX
             * TurtleParser         Turtle, NTriples
             * JsonLdParser         JSON-LD
             */
        }