Exemplo n.º 1
0
        public void LoadSameData()
        {
            var f   = new NodeFactory();
            var rdf = new RdfData(new NodeFactoryThreadSafe(f));

            var data = Files.TestDataDir.GetFile("brazil.nt");

            var f1 = data.CopyTo(
                Files.AppDataDir.GetFile(
                    nameof(NodeFactoryTests), "f1.nt"), true);
            var g1 = rdf.GetBlankGraph("g1");

            var f2 = data.CopyTo(
                Files.AppDataDir.GetFile(
                    nameof(NodeFactoryTests), "f2.nt"), true);
            var g2 = rdf.GetBlankGraph("g2");

            new NTripleFile(f1).Read(g1).Wait();

            var blankCount = rdf.Query.Where(x => x.Subject is BlankNode)
                             .Select(x => x.Subject)
                             .Union(rdf.Query.Where(x => x.Object is BlankNode)
                                    .Select(x => x.Object))
                             .Distinct().Count();

            var count1 = f.Stats().TotalNodes - blankCount;

            new NTripleFile(f2).Read(g2).Wait();
            var count2 = f.Stats().TotalNodes - blankCount * 2;

            Assert.AreEqual(count1, count2, "No extra nodes, except blanks.");
        }
Exemplo n.º 2
0
        public void ReplaceValue()
        {
            var rdf = new RdfData(new NodeFactory());
            var g   = rdf.GetBlankGraph("test");

            g.Update
            .For("app:test", out var test)
            .With("app:name", out var aName).Add("fred");

            g.Update
            .For(test).With(aName).Remove <string>();

            var nameNode = g.Query.For(test).With(aName)
                           .FirstOrNull();

            Assert.IsNull(nameNode);

            g.Update
            .For(test).With(aName).Set("bob");

            var name = g.Query.For(g, "app:test")
                       .With(g, "app:name")
                       .First().Object.Value <string>();

            Assert.AreEqual("bob", name);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public void FindForValue()
        {
            var rdf = new RdfData(new NodeFactory());

            var g    = rdf.GetBlankGraph();
            var sub  = rdf.Uri("app:me");
            var name = rdf.Uri("app:name");

            g.Update
            .For(sub)
            .With(name).Add("fred", "en", out _)
            .Add("fred-literal", out _)
            .Then()
            .With("app:age", out _).Add(23, out _)
            .Then().Then()
            .For("app:other", out _)
            .With("app:friend", out _).Add(sub);

            //look for name as text.
            var found = rdf.Query.From(g.Id)
                        .For(sub).With(name)
                        .IsType <Text>()
                        .Get <Text>()
                        .Single();

            Assert.AreEqual("fred", found.Value);
        }
Exemplo n.º 5
0
        public void Negative()
        {
            var f   = new NodeFactory();
            var rdf = new RdfData(f);

            var fileRoot = Files.TestDataDir.GetFolder("w3c");

            var files = fileRoot.EnumerateFiles("*.nt")
                        .Where(x => x.Name.Contains("-bad-"));

            var idx = 18;

            foreach (var item in files.Skip(idx))
            {
                Exception?error = null;
                try
                {
                    new NTripleFile(item)
                    .Read(rdf.GetBlankGraph($"BadTest-{idx}")).Wait();
                }
                catch (Exception ex)
                {
                    error = ex;
                }

                Assert.IsNotNull(error, $"Negative test : {item.Name} failed");

                idx++;
            }
        }
Exemplo n.º 6
0
        public void BuildFileSystemInfo()
        {
            //data store
            var rdf = new RdfData(new NodeFactory());

            //read info
            var g = rdf.GetBlankGraph(nameof(BuildFileSystemInfo));

            g.Update.For(g.Uri(_rootFolder.AsUri()))
            .With("app:root", out _)
            .Add(true)
            .Add(g.Uri(_rootFolder.AsUri()));

            var q = new Queue <DirectoryInfo>();

            q.Enqueue(_rootFolder);

            //limit depth so not too long.
            BreadthFirstReadInfo(q, g.Update, rdf, 2);

            //save data
            _dataFile.EnsureDelete();
            var rdfFile = new NTripleFile(_dataFile);

            rdfFile.Write(g).Wait();

            Assert.IsTrue(_dataFile.Exists());
        }
Exemplo n.º 7
0
        public void ReadBlanks()
        {
            var f   = new NodeFactory();
            var rdf = new RdfData(f);

            var g = rdf.GetBlankGraph();

            var target = new NTripleParse(g);

            target.Load("<s:1> <p:1> _:blank . ");

            var label = g.Query.Single().ValueBlank();

            Assert.AreEqual("blank", label);
        }
Exemplo n.º 8
0
        public void FindWithOr()
        {
            var rdf = new RdfData(new NodeFactory());

            var g = rdf.GetBlankGraph();

            g.Update
            .For("app:test", out var test)
            .With("app:name", out var aName)
            .Add("fred")
            .Add("fred", "en");

            g.Query.For(test)
            .With(aName)
            .IsType(n => n is Node <Text> || n is Node <string>);
        }
Exemplo n.º 9
0
        public void ReadMultiLine()
        {
            var data     = "Multi \\n Line text \\\" with spaces\\u0021.";
            var expected = "Multi \n Line text \" with spaces!.";

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

            var target = new NTripleParse(rdf.GetBlankGraph());
            var quad   = "<a:s> <a:p> \"" + data + "\" .";

            target.Load(quad);

            var result = rdf.Query.Single().Value <string>();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 10
0
        public void Parse_LiteralWithDifferentEncoding()
        {
            var rdf = new RdfData(new NodeFactory());

            var g = rdf.GetBlankGraph("test");

            var aHex = "\\u" + ((int)'a').ToString("X4");

            var data = "<a:s1> <a:p> \"a literal\"." +
                       $"\r\n <a:s2> <a:p> \"a liter{aHex}l\".";

            var target = new NTripleParse(g);

            target.Load(data);
            var q1 = rdf.Query.ElementAt(0);
            var q2 = rdf.Query.ElementAt(1);

            Assert.AreEqual(q1.Object, q2.Object, "Same literal node");
        }
Exemplo n.º 11
0
        public void Positive()
        {
            var f   = new NodeFactory();
            var rdf = new RdfData(f);

            var fileRoot = Files.TestDataDir.GetFolder("w3c");

            var files = fileRoot.EnumerateFiles("*.nt")
                        .Where(x => !x.Name.Contains("-bad-"));

            var idx = 0;

            foreach (var item in files.Skip(idx))
            {
                new NTripleFile(item)
                .Read(rdf.GetBlankGraph($"Test-{idx}")).Wait();

                idx++;
            }
        }
Exemplo n.º 12
0
        public void ReadData()
        {
            var rdf = new RdfData(new NodeFactory());

            var target = new NTripleFile(Files.TestData.Sample1);

            var defaultGraph = rdf.GetBlankGraph("default");

            target.Read(defaultGraph).Wait();

            var quad = rdf.Query.With(rdf, "app:blank")
                       .Single();

            Assert.AreEqual(quad.Subject, quad.Object);
            Assert.AreEqual("b1", quad.ValueBlank());

            var txt = rdf.Query.With(rdf, "app:literal")
                      .Single().Value <string>();

            Assert.AreEqual("x", txt);

            var uriNode = rdf.Query.With(rdf, "app:uri")
                          .Select(x => x.Object)
                          .OfType <UriNode>().Single();

            txt = uriNode.Uri;

            Assert.AreEqual("http://a.example/s", txt);

            txt = rdf.Query.With(rdf, "app:text")
                  .Select(x => x.Object)
                  .OfType <Node <Text> >()
                  .Single(x => x.Value.Language == "fr")
                  .Value.Value;
            Assert.AreEqual("fredo", txt);

            var intValue = rdf.Query.With(rdf, "app:number")
                           .Single().Value <int>();

            Assert.AreEqual(123, intValue);
        }
Exemplo n.º 13
0
        public async Task WriteWithCrossGraphBlank()
        {
            var rdf = new RdfData(new NodeFactory());

            var g1 = rdf.GetBlankGraph();

            g1.Assert(rdf.Uri("bad:link"), rdf.Uri("app:test"), rdf.BlankSelf("other"));

            var file = Files.AppDataDir.GetFile(nameof(NodeWriterTests),
                                                nameof(WriteWithCrossGraphBlank) + ".nt")
                       .EnsureDelete();

            var target = new NTripleFile(file);

            try
            {
                await target.Write(g1);

                Assert.Fail("Cannot write if blank is scoped to other graph");
            }catch (BlankNodeScopedToInvalidGraph) {}
        }
Exemplo n.º 14
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);
        }