Beispiel #1
0
        public void AddListAndKeepingReferenceToStart()
        {
            var rdf = new RdfData(new NodeFactory());

            var g = rdf.GetGraph(rdf.Uri("graph:/g1"))
                    .Update
                    .Let(out var graphId);

            //(http://www.snee.com/bobdc.blog/2014/04/rdf-lists-and-sparql.html)
            // x:sub rdfs:first "one"
            // x.sub rdfs:rest _:1
            // _:1 rdfs:first "two"
            // _:1 rdfs:rest _:2
            // _:2 rdfs:first "three"
            // _:2 rdfs:rest rsf:nil
            g.For(rdf.Uri("x:sub"))
            .Let(out var listStart)
            .List(rdf.New("one"), rdf.New("two"), rdf.New("three"))
            .With(rdf.Uri("x:p")).Add(rdf.New("Extra data on last item"))
            //And jump back to starting subject.
            .ThenFor(listStart);

            var listModel = new RdfsList(rdf, rdf.GetGraph(graphId));

            var items = listModel.GetList(listStart)
                        .ToList();

            Assert.AreEqual(3, items.Count, "Got 3 items from list");
            Assert.AreEqual("two", items[1].Value <string>(), "2nd is as expected");
        }
Beispiel #2
0
        public void QueryIncludesOtherGraphs()
        {
            var rdf = new RdfData(new NodeFactory());

            var g1 = rdf.GetGraph(rdf.BlankSelf());

            g1.Assert(rdf.Uri("app:test"), rdf.Uri("app:name"),
                      rdf.New("baseName"));

            var g2 = rdf.GetGraph(rdf.BlankSelf());

            g2.Assert(rdf.Uri("app:test"), rdf.Uri("app:name"),
                      rdf.New("mainName"));

            var target = rdf.GetGraph(g2.Id, g1.Id);

            var names = target.Query.With(rdf, "app:name").ToArray();

            Assert.AreEqual(2, names.Length, "Finds both entries");

            //Get the name value.
            var name = names
                       .Get <string>()
                       .FirstOrNull();

            Assert.AreEqual("mainName", name, "Main graph is first in the query");
        }
Beispiel #3
0
        public void BuildingAChain()
        {
            var rdf = new RdfData(new NodeFactory());

            var g = rdf.GetBlankGraph().Update
                    .Let(out var graphId);

            // x:joe x:phone _:auto1
            // _:auto1 x:home "a"
            // _:auto1 x:home "123"^^int
            g.For(rdf.Uri("x:joe"))
            .With(rdf.Uri("x:phone"))
            .To(rdf.Blank(graphId))
            .With(rdf.Uri("x:home"))
            .Add(rdf.New("123"), rdf.New(123));

            Assert.AreEqual(3, rdf.Query.Count);

            //now query data.
            var graph      = rdf.Query.From(graphId).ToList();
            var linkToData = graph.LinksFor(rdf.Uri("x:joe")).Single();

            Assert.AreEqual(rdf.Uri("x:phone"), linkToData.Predicate, "Got Joe's phone entries");

            var properties = graph.PropertiesFor((Node <UriOrBlank>)linkToData.Object).ToList();

            Assert.AreEqual(2, properties.Count, "Expected 2 entries");

            var thePhoneNumber = properties.Select(x => x.Object.As <int>())
                                 .Single(x => !(x is null))
                                 ?? throw new Exception("Didn't find phone number");

            Assert.AreEqual(123, thePhoneNumber.Value);
        }
        public async Task WriteRead()
        {
            var file = new FileInfo(
                Path.Combine(Files.AppData.NqFileTests.FullName,
                             nameof(WriteRead) + ".nt"));

            var rdf = new RdfData(new NodeFactory());

            var g = rdf.GetFullGraph("graph:1")
                    .Assert(rdf.Uri("sub1:/"), rdf.Uri("pred1:/"), rdf.New("basic text"));

            file.Directory.EnsureDelete();
            var t = new NTripleFile(file);
            await t.Write(g);

            file.Refresh();
            Assert.IsTrue(file.Exists);
            Assert.IsTrue(file.Length > 0, "wrote some data");

            //copy current so it doesn't delete.
            var gCopy = g.Query.ToList();

            g.Clear();
            await t.Read(g);

            IEqualityComparer <Quad> eq = new SameQuad();

            var gDiffs = g.Query.Except(gCopy, eq).ToList();

            Assert.IsFalse(gDiffs.Any(), "No difference");
        }
Beispiel #5
0
        public void HasCrossGraphBlank()
        {
            var rdf = new RdfData(new NodeFactory());

            var g1 = rdf.GetGraph(rdf.BlankSelf());
            var g2 = rdf.GetFullGraph("app:test");

            var who = rdf.Uri("app:who");
            var b1  = g1.Blank("b1");

            //b1 in g1 points to blank in g2 for details.
            g1.Assert(b1, who, g2.Id);

            //b1 has name fred.
            var name = rdf.Uri("app:name");

            g2.Assert(b1, name, rdf.New("Fred"));

            //Now find via cross graph query.
            var otherG = g1.Query.Single(x =>
                                         x.Predicate == who &&
                                         x.Object is Node <UriOrBlank>)
                         .Object.Cast <UriOrBlank>();

            var myName = rdf.GetGraph(otherG).Query.Where(x =>
                                                          x.Subject == b1 &&
                                                          x.Predicate == name)
                         .Get <string>()
                         .Single();

            Assert.AreEqual("Fred", myName);
        }
Beispiel #6
0
        public void RdfUpdate_AvailableInGraph()
        {
            var f = new NodeFactory();

            var rdf = new RdfData(f);

            var g1 = f.BlankSelf();
            var g2 = f.BlankSelf();

            var graphPreUpdates = rdf.GetGraph(g1, g2);

            //add directly via rdf data.
            var sub  = rdf.Uri("x:/s1");
            var pred = rdf.Uri("x:/p1");
            var val  = rdf.New("text");

            rdf.Assert(g1, sub, pred, val);
            rdf.Assert(g2, sub, pred, val);

            //immediately have the owned quad,
            //but not the data from other graph.
            Assert.AreEqual(1, graphPreUpdates.Query.Count, "Got the new entry");

            var graphReInit = rdf.GetGraph(g1, g2);

            Assert.AreEqual(2, graphReInit.Query.Count, "Re-access graph gets latest");
        }
Beispiel #7
0
        public void GraphIds_Ordered()
        {
            var rdf = new RdfData(new NodeFactory());

            var g1 = rdf.BlankSelf("test");

            rdf.Assert(g1, rdf.Blank(g1), rdf.Uri("a:pred"), rdf.New(123));

            var graphs = rdf.GraphIds;

            Assert.AreEqual(3, graphs.Length);
            Assert.AreEqual(rdf.System, graphs.ElementAt(0));
            Assert.AreEqual(rdf.Default, graphs.ElementAt(1));
        }
        public void WriteTripleData()
        {
            var rdf = new RdfData(new NodeFactory());

            var target = new NTripleFile(Files.AppDataDir.GetFile(
                                             nameof(NTripleFileTests), nameof(WriteTripleData) + ".nt"));

            var g = rdf.BlankSelf("g1");

            rdf.Assert(g,
                       rdf.Uri("x:sub1"),
                       rdf.Uri("x:age"),
                       rdf.New(23));

            target.Write(rdf.GetGraph(g)).Wait();
        }
Beispiel #9
0
        public void ClearGraph()
        {
            var data = new RdfData(new NodeFactory());

            data.Update.From("sys:/data/", out _)
            .For("sub:s1", out _)
            .With("x:/p1", out _).Add(123);

            var g = data.GetGraph(data.BlankSelf())
                    .Assert(data.Uri("x:/s1"),
                            data.Uri("p:/1"),
                            data.New("text"));

            Assert.AreEqual(2, data.Query.Count, "have the nodes");

            g.Clear();
            Assert.AreEqual(0, g.Query.Count, "no graph nodes");
            Assert.AreEqual(1, data.Query.Count, "still have other data");
        }
Beispiel #10
0
        public void PrintLiteral()
        {
            var rdf = new RdfData(new NodeFactory());
            var g   = rdf.GetBlankGraph();

            var expected = "_:g <app:test> \"Literal\\rMultiline\\\\\" .";

            g.Assert(g.Blank("g"),
                     rdf.Uri("app:test"),
                     rdf.New("Literal\rMultiline\\"));

            var target = new NodeWriter(g);

            using var wr = new StringWriter();

            target.Print(wr);

            wr.Flush();
            var result = wr.GetStringBuilder().ToString().Trim();

            Assert.AreEqual(expected, result);
        }