Пример #1
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)));
        }
Пример #2
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)));
        }