Пример #1
0
        private static void SwapLogSupports(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            { :Paul :mother :Caroline } => { :Paul :parent :Caroline }.

            {
                {
                  :Paul :mother :Caroline.
                  :Paul :father :Frans.
                } log:supports { :Paul :parent :Caroline. }.
            }
            =>
            {
                :result :supports true.
            }.
            ".ParseString());

            // TODO: Fix the log:supports implementation
            var result = model.GetFact <bool>(Names.Example.CreateName("result"), Names.Example.CreateName("supports"));

            Console.WriteLine("Document semantics supports the rule: {0}", result);
        }
Пример #2
0
        private static void SwapLogContent(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <Ontology/testData.n3> log:content ?content.
            }
            =>
            {
                :content :is ?content.
            }
            ".ParseString());

            var    stream = (Stream)model.GetFact <object>(Names.Example.CreateName("content"), Names.Example.CreateName("is"));
            string content;

            using (var reader = new StreamReader(stream))
            {
                content = reader.ReadToEnd();
            }

            Console.WriteLine("Loaded content: {0} symbols", content.Length);
        }
Пример #3
0
        private static void SwapMathSum(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?list (10 20)) list:append ?flatList.
                ?flatList math:sum ?y.
            }
            =>
            {
                ?list :sum ?y.
            }
            ".ParseString());

            var sum = model.GetFact <int>(new QName[]
            {
                1.CreateLiteral(),
                2.CreateLiteral()
            }.CreateLiteral(), Names.Example.CreateName("sum"));

            Console.WriteLine("Sum is: {0}", sum);
        }
Пример #4
0
        private static void SwapStringNotLessThan(Model model)
        {
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x a :Person.
                ?y a :Person.
                ?x :firstName ?xfn.
                ?y :firstName ?yfn.
                ?xfn string:notLessThan ?yfn.
            }
            =>
            {
                (?x ?y) :notLessThan true.
            }
            ".ParseString());

            var notLessThan = model.GetFact <bool>(new[]
            {
                Names.Rita,
                Names.Paul
            }.CreateLiteral(), Names.Example.CreateName("notLessThan"));

            Console.WriteLine("notLessThan: {0}", notLessThan);
        }
Пример #5
0
        private static void SwapStringNotMatches(Model model)
        {
            // Use .NET regex syntax for patterns.

            // Simple example: true if Person's first name matches the pattern
            // We use @in statements to indicate that ?x and ?pattern variables are always presented while querying the fact.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x, ?pattern.
            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:notMatches ?pattern.
            }
            =>
            {
                (?x ?pattern) :notMatchesPattern true.
            }
            ".ParseString());
            var paulDoesntMatchPattern = model.GetFact <bool>(new[]
            {
                Names.Paul,
                @"Jack".CreateLiteral()
            }.CreateLiteral(), Names.Example.CreateName("notMatchesPattern"));

            Console.WriteLine("Paul's first name doesn't match 'Jack' pattern: {0}", paulDoesntMatchPattern);
        }
Пример #6
0
        private static void SwapLogNotIncludes(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                { :Paul :mother :Rita. :Paul :father :Frans. } -> ?family.
                ?family log:notIncludes { :Boston :weather :sunny }.
            } => {:result :notIncludes true}.
            ".ParseString());

            var result = model.GetFact <bool>(Names.Example.CreateName("result"), Names.Example.CreateName("notIncludes"));

            Console.WriteLine("Document semantics does not include the rule: {0}", result);
        }
Пример #7
0
        private static void SwapListLast(Model model)
        {
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ( :Frans :Caroline :Rita ) list:last ?z.
            }
            =>
            {
                :Frans :lastMember ?z.
            }
            ".ParseString());

            var rita = model.GetFact(Names.Frans, Names.Example.CreateName("lastMember"));

            Console.WriteLine("Found person: {0}", Helpers.Beautify(rita));
        }
Пример #8
0
        private static void SwapListMember(Model model)
        {
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ( :Frans :Caroline :Rita ) list:member ?z.
            }
            =>
            {
                ?z :isMemberOf true.
            }
            ".ParseString());

            var isMember = model.GetFact <bool>(Names.Frans, Names.Example.CreateName("isMemberOf"));

            Console.WriteLine("Is member: {0}", isMember);
        }
Пример #9
0
        private static void SwapLogIncludes(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                { :Paul :mother :Caroline. :Paul :father :Frans. } log:includes { :Paul :mother :Caroline. }.
            }
            =>
            {
                :result :includes true.
            }.
            ".ParseString());

            var result = model.GetFact <bool>(Names.Example.CreateName("result"), Names.Example.CreateName("includes"));

            Console.WriteLine("Document semantics includes the rule: {0}", result);
        }
Пример #10
0
        private static void SwapMathGreaterThan(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:greaterThan 6.
            }
            =>
            {
                ?x :greaterThan true.
            }
            ".ParseString());

            var greaterThan = model.GetFact <bool>(7.CreateLiteral(), Names.Example.CreateName("greaterThan"));

            Console.WriteLine("7 greater than 6: {0}", greaterThan);
        }
Пример #11
0
        private static void SwapMathNotEqualTo(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:notEqualTo 2.
            }
            =>
            {
                ?x :notEqualTo true.
            }
            ".ParseString());

            var notEqualTo = model.GetFact <bool>(1.CreateLiteral(), Names.Example.CreateName("notEqualTo"));

            Console.WriteLine("1 not equal to 2: {0}", notEqualTo);
        }
Пример #12
0
        private static void SwapMathNotLessThan(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:notLessThan 2.
            }
            =>
            {
                ?x :notLessThan true.
            }
            ".ParseString());

            var notLessThan = model.GetFact <bool>(5.CreateLiteral(), Names.Example.CreateName("notLessThan"));

            Console.WriteLine("5 not less than 2: {0}", notLessThan);
        }
Пример #13
0
        private static void SwapMathQuotient(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x 2) math:quotient ?z.
            }
            =>
            {
                ?x :quotient ?z.
            }
            ".ParseString());

            var quotient = model.GetFact <double>(5.CreateLiteral(), Names.Example.CreateName("quotient"));

            Console.WriteLine("Quotient is: {0}", quotient);
        }
Пример #14
0
        private static void SwapMathRemainder(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x 2) math:remainder ?z.
            }
            =>
            {
                ?x :remainder ?z.
            }
            ".ParseString());

            var remainder = model.GetFact <int>(5.CreateLiteral(), Names.Example.CreateName("remainder"));

            Console.WriteLine("Remainder is: {0}", remainder);
        }
Пример #15
0
        private static void SwapListAppend(Model model)
        {
            // Append as result
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x :mother :Rita.
                ( (:Frans :Caroline) (?x :Rita) ) list:append ?family.
            }
            =>
            {
                ?x :myFamily ?family
            }
            ".ParseString());

            var family = model.GetFact(Names.Paul, Names.Example.CreateName("myFamily")).AsList();

            Console.WriteLine("Found family: {0}", string.Join(", ", family.Select(Helpers.Beautify).ToArray()));

            // Append as condition
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x :mother :Rita.
                :Rita :mother ?z.
                ( (?x :Rita) (:Frans ?z) ) list:append (?x :Rita :Frans ?z).
            }
            =>
            {
                ?x :inRelationWith ?z
            }
            ".ParseString());

            var caroline = model.GetFact(Names.Paul, Names.Example.CreateName("inRelationWith"));

            Console.WriteLine("Found person: {0}", Helpers.Beautify(caroline));
        }
Пример #16
0
        private static void SwapTimeInSeconds(Model model)
        {
            model.AddStatements(@"
            @prefix time: <http://www.w3.org/2000/10/swap/time#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x time:inSeconds ?y.
            }
            =>
            {
                ?x :asSeconds ?y.
            }
            ".ParseString());

            var unixTime = model.GetFact(DateTime.SpecifyKind(new DateTime(2010, 1, 1), DateTimeKind.Utc).CreateLiteral(), Names.Example.CreateName("asSeconds"));
            var seconds  = model.GetFact(TimeSpan.FromSeconds(123456).CreateLiteral(), Names.Example.CreateName("asSeconds"));

            Console.WriteLine("Unix time: {0}", unixTime);
            Console.WriteLine("Seconds: {0}", seconds);
        }
Пример #17
0
        private static void SwapLogEqualTo(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            :Paul :mother :Rita.
            :Paul :father :Frans.

            {
                ?x :mother ?y.
                ?x :father ?z.
                ?y log:equalTo :Rita.
                ?z == :Frans.
            } => { ?x :equalTo true }.
            ".ParseString());

            var result = model.GetFact <bool>(Names.Paul, Names.Example.CreateName("equalTo"));

            Console.WriteLine("The subject's parents are Rita and Frans: {0}", result);
        }
Пример #18
0
        private static void SwapLogUri(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <> log:uri ?y.
            }
            =>
            {
                :test :uriMatchesExample ?y.
            }.
            ".ParseString());

            // TODO: fix the log:uri implementation
            var result = model.GetFact(Names.Example.CreateName("test"), Names.Example.CreateName("uriMatchesExample"));

            Console.WriteLine("Paul is in examples namespace: {0}", result);
        }
Пример #19
0
        private static void SwapMathNegation(Model model)
        {
            // We must use @in to indicate input variables for the correct rule compilation.
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x.
            {
                ?x math:negation ?y.
            }
            =>
            {
                ?x :negated ?y.
            }.
            ".ParseString());

            var negatedValue = model.GetFact <int>(1.CreateLiteral(), Names.Example.CreateName("negated"));

            Console.WriteLine("negation of 1 is: {0}", negatedValue);
        }
Пример #20
0
        private static void SwapStringFormat(Model model)
        {
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?x :lastName ?ln.
                (""{0} {1}"" ?fn ?ln) string:format ?z
            }
            =>
            {
                ?x :fullName ?z.
            }
            ".ParseString());

            var fullName = model.GetFact <string>(Names.Paul, Names.Example.CreateName("fullName"));

            Console.WriteLine("Full name: {0}", fullName);
        }
Пример #21
0
        private static void SwapLogConjunction(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <Ontology/testData.n3> log:semantics ?testData.
                <Ontology/meta.n3> log:semantics ?meta.
                (?testData ?meta) log:conjunction ?formulae.
                ?formulae log:includes { ?x a :Person. }.
            }
            =>
            {
                ?x :conjunction true.
            }
            ".ParseString());

            var result = model.GetFact <bool>(Names.Paul, Names.Example.CreateName("conjunction"));

            Console.WriteLine("Conjunction result contains the statement: {0}", result);
        }
Пример #22
0
        private static void SwapStringConcatenation(Model model)
        {
            model.AddStatements(@"
                    @prefix string: <http://www.w3.org/2000/10/swap/string#>.
                    @prefix : <http://www.example.com/logics/example#>.

                    {
                        ?x a :Person.
                        ?x :firstName ?z.
                        (?a ?b) string:concatenation ?z
                    }
                    =>
                    {
                        (?a ?b) :concatFind ?x
                    }.

                ".ParseString());

            var paul = model.GetFact(
                new QName[] { "Pa".CreateLiteral(), "ul".CreateLiteral() }.CreateLiteral(),
                Names.Example.CreateName("concatFind"));

            Console.WriteLine("Found person: {0}", Helpers.Beautify(paul));
        }
Пример #23
0
        private static void SwapMathDifference(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x ?y) math:difference ?z.
            }
            =>
            {
                (?x ?y) :difference ?z.
            }
            ".ParseString());

            var difference = model.GetFact <int>(new QName[]
            {
                11.CreateLiteral(),
                1.CreateLiteral()
            }.CreateLiteral(), Names.Example.CreateName("difference"));

            Console.WriteLine("Difference is: {0}", difference);
        }
Пример #24
0
        private static void SwapStringConcatenation(Model model)
        {
            model.AddStatements(@"
                    @prefix string: <http://www.w3.org/2000/10/swap/string#>.
                    @prefix : <http://www.example.com/logics/example#>.

                    {
                        ?x a :Person.
                        ?x :firstName ?z.
                        (?a ?b) string:concatenation ?z
                    }
                    =>
                    {
                        (?a ?b) :concatFind ?x
                    }.

                ".ParseString());

            var paul = model.GetFact(
                new QName[] { "Pa".CreateLiteral(), "ul".CreateLiteral() }.CreateLiteral(),
                Names.Example.CreateName("concatFind"));
            Console.WriteLine("Found person: {0}", Helpers.Beautify(paul));
        }
Пример #25
0
        private static void SwapLogSupports(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            { :Paul :mother :Caroline } => { :Paul :parent :Caroline }.

            {
                {
                  :Paul :mother :Caroline.
                  :Paul :father :Frans.
                } log:supports { :Paul :parent :Caroline. }.
            }
            =>
            {
                :result :supports true.
            }.
            ".ParseString());

            // TODO: Fix the log:supports implementation
            var result = model.GetFact<bool>(Names.Example.CreateName("result"), Names.Example.CreateName("supports"));
            Console.WriteLine("Document semantics supports the rule: {0}", result);
        }
Пример #26
0
        private static void SwapStringMatches(Model model)
        {
            // Use .NET regex syntax for patterns.

            // Simple example: true if Person's first name matches the pattern
            // We use @in statements to indicate that ?x and ?pattern variables are always presented while querying the fact.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x, ?pattern.
            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:matches ?pattern.
            }
            =>
            {
                (?x ?pattern) :matchesPattern true.
            }
            ".ParseString());
            var paulMatchesPattern = model.GetFact<bool>(new[]
                {
                    Names.Paul,
                    @".aul".CreateLiteral()
                }.CreateLiteral(), Names.Example.CreateName("matchesPattern"));
            Console.WriteLine("Paul's first name matches '.aul' pattern: {0}", paulMatchesPattern);

            // Complex example: return the persons whose first name matches the pattern.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?list, ?pattern.
            {
                ?list list:member ?x.
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:matches ?pattern.
            }
            =>
            {
                (?list ?pattern) :matchedPersons ?x.
            }
            ".ParseString());

            var matchedPersons = model.GetFacts(
                new QName[]
                    {
                        new[]
                            {
                                Names.Paul,
                                Names.Rita,
                                Names.Frans,
                                Names.Caroline
                            }.CreateLiteral(),
                        @"[iu]+".CreateLiteral()
                    }.CreateLiteral(),
                Names.Example.CreateName("matchedPersons"));
            Console.WriteLine("Matched persons: {0}", string.Join(", ", matchedPersons.Select(Helpers.Beautify)));
        }
Пример #27
0
        private static void SwapStringFormat(Model model)
        {
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?x :lastName ?ln.
                (""{0} {1}"" ?fn ?ln) string:format ?z
            }
            =>
            {
                ?x :fullName ?z.
            }
            ".ParseString());

            var fullName = model.GetFact<string>(Names.Paul, Names.Example.CreateName("fullName"));
            Console.WriteLine("Full name: {0}", fullName);
        }
Пример #28
0
        private static void SwapTimeInSeconds(Model model)
        {
            model.AddStatements(@"
            @prefix time: <http://www.w3.org/2000/10/swap/time#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x time:inSeconds ?y.
            }
            =>
            {
                ?x :asSeconds ?y.
            }
            ".ParseString());

            var unixTime = model.GetFact(DateTime.SpecifyKind(new DateTime(2010, 1, 1), DateTimeKind.Utc).CreateLiteral(), Names.Example.CreateName("asSeconds"));
            var seconds = model.GetFact(TimeSpan.FromSeconds(123456).CreateLiteral(), Names.Example.CreateName("asSeconds"));

            Console.WriteLine("Unix time: {0}", unixTime);
            Console.WriteLine("Seconds: {0}", seconds);
        }
Пример #29
0
        private static void SwapMathSum(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?list (10 20)) list:append ?flatList.
                ?flatList math:sum ?y.
            }
            =>
            {
                ?list :sum ?y.
            }
            ".ParseString());

            var sum = model.GetFact<int>(new QName[]
                {
                    1.CreateLiteral(),
                    2.CreateLiteral()
                }.CreateLiteral(), Names.Example.CreateName("sum"));
            Console.WriteLine("Sum is: {0}", sum);
        }
Пример #30
0
        private static void SwapLogConjunction(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <Ontology/testData.n3> log:semantics ?testData.
                <Ontology/meta.n3> log:semantics ?meta.
                (?testData ?meta) log:conjunction ?formulae.
                ?formulae log:includes { ?x a :Person. }.
            }
            =>
            {
                ?x :conjunction true.
            }
            ".ParseString());

            var result = model.GetFact<bool>(Names.Paul, Names.Example.CreateName("conjunction"));
            Console.WriteLine("Conjunction result contains the statement: {0}", result);
        }
Пример #31
0
        private static void SwapStringNotLessThan(Model model)
        {
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x a :Person.
                ?y a :Person.
                ?x :firstName ?xfn.
                ?y :firstName ?yfn.
                ?xfn string:notLessThan ?yfn.
            }
            =>
            {
                (?x ?y) :notLessThan true.
            }
            ".ParseString());

            var notLessThan = model.GetFact<bool>(new[]
                {
                    Names.Rita,
                    Names.Paul
                }.CreateLiteral(), Names.Example.CreateName("notLessThan"));
            Console.WriteLine("notLessThan: {0}", notLessThan);
        }
Пример #32
0
        private static void SwapMathNotEqualTo(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:notEqualTo 2.
            }
            =>
            {
                ?x :notEqualTo true.
            }
            ".ParseString());

            var notEqualTo = model.GetFact<bool>(1.CreateLiteral(), Names.Example.CreateName("notEqualTo"));
            Console.WriteLine("1 not equal to 2: {0}", notEqualTo);
        }
Пример #33
0
        private static void SwapLogNotEqualTo(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            :Paul :mother :Rita.
            :Paul :father :Frans.
            :Caroline :mother :Greta.
            :Caroline :father :Dirk.

            {
                ?x :mother ?y.
                ?x :father ?z.
                ?y log:notEqualTo :Rita.
                ?z != :Frans.
            } => { ?x :notEqualTo true }.
            ".ParseString());

            var result = model.GetFact<bool>(Names.Caroline, Names.Example.CreateName("notEqualTo"));
            Console.WriteLine("The subject's parents are not Rita and Frans: {0}", result);
        }
Пример #34
0
        private static void SwapMathNotLessThan(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:notLessThan 2.
            }
            =>
            {
                ?x :notLessThan true.
            }
            ".ParseString());

            var notLessThan = model.GetFact<bool>(5.CreateLiteral(), Names.Example.CreateName("notLessThan"));
            Console.WriteLine("5 not less than 2: {0}", notLessThan);
        }
Пример #35
0
        private static void SwapMathQuotient(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x 2) math:quotient ?z.
            }
            =>
            {
                ?x :quotient ?z.
            }
            ".ParseString());

            var quotient = model.GetFact<double>(5.CreateLiteral(), Names.Example.CreateName("quotient"));
            Console.WriteLine("Quotient is: {0}", quotient);
        }
Пример #36
0
        private static void SwapListLast(Model model)
        {
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ( :Frans :Caroline :Rita ) list:last ?z.
            }
            =>
            {
                :Frans :lastMember ?z.
            }
            ".ParseString());

            var rita = model.GetFact(Names.Frans, Names.Example.CreateName("lastMember"));
            Console.WriteLine("Found person: {0}", Helpers.Beautify(rita));
        }
Пример #37
0
        private static void SwapLogSemantics(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            :Paul :mother :Caroline.

            {
                <> log:semantics ?M.
                ?M log:includes { :Paul :mother :Caroline. }.
            }
            =>
            {
                :result :semantics true.
            }.
            ".ParseString());

            var result = model.GetFact<bool>(Names.Example.CreateName("result"), Names.Example.CreateName("semantics"));
            Console.WriteLine("Document semantics includes the rule: {0}", result);
        }
Пример #38
0
        private static void SwapListMember(Model model)
        {
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ( :Frans :Caroline :Rita ) list:member ?z.
            }
            =>
            {
                ?z :isMemberOf true.
            }
            ".ParseString());

            var isMember = model.GetFact<bool>(Names.Frans, Names.Example.CreateName("isMemberOf"));
            Console.WriteLine("Is member: {0}", isMember);
        }
Пример #39
0
        private static void SwapLogNotIncludes(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                { :Paul :mother :Rita. :Paul :father :Frans. } -> ?family.
                ?family log:notIncludes { :Boston :weather :sunny }.
            } => {:result :notIncludes true}.
            ".ParseString());

            var result = model.GetFact<bool>(Names.Example.CreateName("result"), Names.Example.CreateName("notIncludes"));
            Console.WriteLine("Document semantics does not include the rule: {0}", result);
        }
Пример #40
0
        private static void SwapStringNotMatches(Model model)
        {
            // Use .NET regex syntax for patterns.

            // Simple example: true if Person's first name matches the pattern
            // We use @in statements to indicate that ?x and ?pattern variables are always presented while querying the fact.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x, ?pattern.
            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:notMatches ?pattern.
            }
            =>
            {
                (?x ?pattern) :notMatchesPattern true.
            }
            ".ParseString());
            var paulDoesntMatchPattern = model.GetFact<bool>(new[]
                {
                    Names.Paul,
                    @"Jack".CreateLiteral()
                }.CreateLiteral(), Names.Example.CreateName("notMatchesPattern"));
            Console.WriteLine("Paul's first name doesn't match 'Jack' pattern: {0}", paulDoesntMatchPattern);
        }
Пример #41
0
        private static void SwapLogContent(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <Ontology/testData.n3> log:content ?content.
            }
            =>
            {
                :content :is ?content.
            }
            ".ParseString());

            var stream = (Stream)model.GetFact<object>(Names.Example.CreateName("content"), Names.Example.CreateName("is"));
            string content;
            using (var reader = new StreamReader(stream))
            {
                content = reader.ReadToEnd();
            }

            Console.WriteLine("Loaded content: {0} symbols", content.Length);
        }
Пример #42
0
        private static void SwapMathNegation(Model model)
        {
            // We must use @in to indicate input variables for the correct rule compilation.
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x.
            {
                ?x math:negation ?y.
            }
            =>
            {
                ?x :negated ?y.
            }.
            ".ParseString());

            var negatedValue = model.GetFact<int>(1.CreateLiteral(), Names.Example.CreateName("negated"));
            Console.WriteLine("negation of 1 is: {0}", negatedValue);
        }
Пример #43
0
        private static void SwapMathGreaterThan(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x math:greaterThan 6.
            }
            =>
            {
                ?x :greaterThan true.
            }
            ".ParseString());

            var greaterThan = model.GetFact<bool>(7.CreateLiteral(), Names.Example.CreateName("greaterThan"));
            Console.WriteLine("7 greater than 6: {0}", greaterThan);
        }
Пример #44
0
        private static void SwapMathDifference(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x ?y) math:difference ?z.
            }
            =>
            {
                (?x ?y) :difference ?z.
            }
            ".ParseString());

            var difference = model.GetFact<int>(new QName[]
                {
                    11.CreateLiteral(),
                    1.CreateLiteral()
                }.CreateLiteral(), Names.Example.CreateName("difference"));
            Console.WriteLine("Difference is: {0}", difference);
        }
Пример #45
0
        private static void SwapStringMatches(Model model)
        {
            // Use .NET regex syntax for patterns.

            // Simple example: true if Person's first name matches the pattern
            // We use @in statements to indicate that ?x and ?pattern variables are always presented while querying the fact.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?x, ?pattern.
            {
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:matches ?pattern.
            }
            =>
            {
                (?x ?pattern) :matchesPattern true.
            }
            ".ParseString());
            var paulMatchesPattern = model.GetFact <bool>(new[]
            {
                Names.Paul,
                @".aul".CreateLiteral()
            }.CreateLiteral(), Names.Example.CreateName("matchesPattern"));

            Console.WriteLine("Paul's first name matches '.aul' pattern: {0}", paulMatchesPattern);

            // Complex example: return the persons whose first name matches the pattern.
            model.AddStatements(@"
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            @in ?list, ?pattern.
            {
                ?list list:member ?x.
                ?x a :Person.
                ?x :firstName ?fn.
                ?fn string:matches ?pattern.
            }
            =>
            {
                (?list ?pattern) :matchedPersons ?x.
            }
            ".ParseString());

            var matchedPersons = model.GetFacts(
                new QName[]
            {
                new[]
                {
                    Names.Paul,
                    Names.Rita,
                    Names.Frans,
                    Names.Caroline
                }.CreateLiteral(),
                @"[iu]+".CreateLiteral()
            }.CreateLiteral(),
                Names.Example.CreateName("matchedPersons"));

            Console.WriteLine("Matched persons: {0}", string.Join(", ", matchedPersons.Select(Helpers.Beautify)));
        }
Пример #46
0
        private static void SwapLogUri(Model model)
        {
            model.AddStatements(@"
            @prefix log: <http://www.w3.org/2000/10/swap/log#>.
            @prefix string: <http://www.w3.org/2000/10/swap/string#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                <> log:uri ?y.
            }
            =>
            {
                :test :uriMatchesExample ?y.
            }.
            ".ParseString());

            // TODO: fix the log:uri implementation
            var result = model.GetFact(Names.Example.CreateName("test"), Names.Example.CreateName("uriMatchesExample"));
            Console.WriteLine("Paul is in examples namespace: {0}", result);
        }
Пример #47
0
        private static void SwapListAppend(Model model)
        {
            // Append as result
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x :mother :Rita.
                ( (:Frans :Caroline) (?x :Rita) ) list:append ?family.
            }
            =>
            {
                ?x :myFamily ?family
            }
            ".ParseString());

            var family = model.GetFact(Names.Paul, Names.Example.CreateName("myFamily")).AsList();
            Console.WriteLine("Found family: {0}", string.Join(", ", family.Select(Helpers.Beautify).ToArray()));

            // Append as condition
            model.AddStatements(@"
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                ?x :mother :Rita.
                :Rita :mother ?z.
                ( (?x :Rita) (:Frans ?z) ) list:append (?x :Rita :Frans ?z).
            }
            =>
            {
                ?x :inRelationWith ?z
            }
            ".ParseString());

            var caroline = model.GetFact(Names.Paul, Names.Example.CreateName("inRelationWith"));
            Console.WriteLine("Found person: {0}", Helpers.Beautify(caroline));
        }
Пример #48
0
        private static void SwapMathRemainder(Model model)
        {
            model.AddStatements(@"
            @prefix math: <http://www.w3.org/2000/10/swap/math#>.
            @prefix list: <http://www.w3.org/2000/10/swap/list#>.
            @prefix : <http://www.example.com/logics/example#>.

            {
                (?x 2) math:remainder ?z.
            }
            =>
            {
                ?x :remainder ?z.
            }
            ".ParseString());

            var remainder = model.GetFact<int>(5.CreateLiteral(), Names.Example.CreateName("remainder"));
            Console.WriteLine("Remainder is: {0}", remainder);
        }