public void SaveGraphToFile(string filename)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentException("Filename was empty or not ending with .txt");
            }
            var writer = new Notation3Writer();

            writer.Save(graph, filename);
            OnNewLogMessage("Saved changed Knowledge Base to " + filename);
        }
Beispiel #2
0
        private void button4_Click(object sender, EventArgs e)
        {
            yb = new Graph();
            Notation3Parser n3p = new Notation3Parser();
            Notation3Writer n3w = new Notation3Writer();

            n3p.Load(yb, @"Properties.n3");
            INode subject   = yb.CreateUriNode("process:retrain");
            INode predicate = yb.CreateUriNode("rdf:epochs");
            INode simple    = yb.CreateLiteralNode(textBox1.Text);

            Extensions.Assert(yb, subject, predicate, simple);
            n3w.Save(yb, "C:\\Users\\misha\\OneDrive\\Рабочий стол\\proj1\\proj1\\bin\\Debug\\Properties.n3");
        }
Beispiel #3
0
        static void WriteAnRDF()
        {
            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));

            Notation3Writer n3writer = new Notation3Writer();

            n3writer.Save(g, "Example.n3");
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            y = new Graph();
            Notation3Writer n3w = new Notation3Writer();

            b = new Graph();
            Notation3Parser n3p = new Notation3Parser();

            n3p.Load(b, @"reasoning.n3");
            string          query1 = @"
                prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                prefix owl: <http://www.w3.org/2002/07/owl#>
                SELECT DISTINCT ?A ?B ?x
                WHERE
                {
                ?A rdfs:subClassOf ?B.
                ?x rdf:type ?Y.
                FILTER (?A = ?Y)}";
            SparqlResultSet srRez1 = (SparqlResultSet)b.ExecuteQuery(query1);

            foreach (SparqlResult sr1 in srRez1)
            {
                n3p.Load(y, @"reasoning.n3");
                INode subject   = y.CreateUriNode(sr1["x"].ToString());
                INode predicate = y.CreateUriNode("rdf:type");;
                INode simple    = y.CreateUriNode(sr1["B"].ToString());
                Extensions.Assert(y, subject, predicate, simple);
            }
            n3p.Load(y, @"Properties.n3");
            string query10 = @"
                    prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                    prefix process: <process:>
                    SELECT ?pr ?AssertedFile ?project ?intrepetor
                    WHERE
                    { ?pr rdfs:subClassOf ?project;
                    rdf:AssertedFile ?AssertedFile;
                    rdf:intrepetor ?intrepetor.}";



            SparqlResultSet srRez10      = (SparqlResultSet)y.ExecuteQuery(query10);
            var             AssertedFile = (srRez10[0]["AssertedFile"].ToString());

            n3w.Save(y, @AssertedFile);
        }
        internal static void WriteGraph(TextWriter writer, IGraph graph, string named, bool plain, bool inHtml)
        {
            string printerName = "plain";

            if (plain)
            {
                DumpTriples(graph, writer, named);
                return;
            }
            else
            {
                IRdfWriter   n3w;
                StringWriter dtt              = new StringWriter();
                string       dttToString      = "error";
                bool         prettyWillWorked = false;

                if (prettyWillWorked)
                {
                    try
                    {
                        n3w = new CompressingTurtleWriter()
                        {
                            DefaultNamespaces = graph.NamespaceMap, PrettyPrintMode = true
                        };
                        n3w.Save(graph, dtt);
                        dttToString      = dtt.ToString();
                        prettyWillWorked = true;
                        printerName      = n3w.GetType().Name;
                    }
                    catch (Exception e)
                    {
                        dttToString      = dtt.ToString();
                        prettyWillWorked = false;
                    }
                }
                if (!prettyWillWorked)
                {
                    n3w = new Notation3Writer()
                    {
                        DefaultNamespaces = graph.NamespaceMap, PrettyPrintMode = true
                    };
                    dtt = new StringWriter();
                    try
                    {
                        lock (graph) n3w.Save(graph, dtt);
                        dttToString = dtt.ToString();
                        printerName = n3w.GetType().Name;
                    }
                    catch (Exception e)
                    {
                        if (inHtml)
                        {
                            writer.WriteLine("<pre><font color='red'>{0} {1} {2}</font></pre>", e.GetType(), e.Message,
                                             e.StackTrace);
                            printerName = "triples";
                            DumpTriples(graph, writer, named);
                        }
                        else
                        {
                            printerName = "plain";
                            DumpTriplesPlain(graph.Triples, writer, "{0}", graph);
                        }
                        return;
                    }
                }
                if (inHtml)
                {
                    writer.WriteLine("<h3>{0} KB {1}</h3>", named, printerName);
                    writer.WriteLine("<pre>");
                }
                dttToString = dttToString.Replace("\r\n", "\n").Replace("\r", "\n");
                foreach (string line in dttToString.Split('\n'))
                {
                    if (String.IsNullOrEmpty(line))
                    {
                        writer.WriteLine();
                        continue;
                    }
                    if (line.StartsWith("@prefix "))
                    {
                        continue;
                    }
                    var hline = line;
                    hline = (" " + hline).Replace(" robokind:", " rk:");
                    hline = hline.Replace("<robokind:", "<rk:");
                    if (inHtml)
                    {
                        hline = hline.Replace("<", "&lt;").Replace(">", "&gt;");
                    }
                    writer.WriteLine(hline);
                }
                if (inHtml)
                {
                    writer.WriteLine("</pre>");
                }
            }
        }
Beispiel #6
0
        public void Dispose()
        {
            //Create all of the directories required for the file
            var f = new FileInfo(_outputPath);

            f.Directory.Create();

            if (this._outputFormat == ERdfFormat.RdfXml)
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(_outputPath, new UTF8Encoding(false)))
                //Set encoding
                {
                    _output.Save(xmlWriter);
                }
            }
            else if (this._outputFormat == ERdfFormat.TriG)
            {
                string fileNameAsTrig = GetFilePathBasedOnFormat();
                var    outparams      = new StreamParams(fileNameAsTrig);
                outparams.Encoding = Encoding.UTF8;
                var writer = new TriGWriter();

                if (_store == null)
                {
                    var g = GetXmlDocumentAsGraph();
                    _store = new TripleStore();
                    _store.Add(g, true);
                }

                writer.Save(_store, outparams);
            }
            else if (this._outputFormat == ERdfFormat.Turtle)
            {
                var    g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var    writer            = new TurtleWriter(TurtleSyntax.W3C);
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.NTriples)
            {
                var    g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var    writer            = new NTriplesWriter();
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.N3)
            {
                var    g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var    writer            = new Notation3Writer();
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.NQuads)
            {
                string filePathForFormat = GetFilePathBasedOnFormat();
                var    outparams         = new StreamParams(filePathForFormat);
                outparams.Encoding = Encoding.UTF8;

                if (_store == null)
                {
                    var g = GetXmlDocumentAsGraph();
                    _store = new TripleStore();
                    _store.Add(g, true);
                }

                var writer = new NQuadsWriter();
                writer.Save(_store, outparams);
            }

            //make sure it's not null - can happen if no graphs have yet to be asserted!!
            if (_store != null)
            {
                foreach (var graph in _store.Graphs)
                {
                    graph.Dispose();
                }
                _store.Dispose();
                GC.Collect();
            }
        }