예제 #1
0
 public ClauseIterator(PredicateTable predTable, BaseTerm clauseHead, VarStack varStack)
 {
     this.pd         = predTable[clauseHead.Key]; // null if not found
     this.clauseHead = clauseHead;
     this.varStack   = varStack;
     iterator        = GetEnumerator();
 }
예제 #2
0
        public IFSA <TValue> Build(string input, PredicateTable <TValue> predicateTable, ORegexOptions options)
        {
            var ast = _parser.Parse(input, predicateTable);
            var fa  = _stb.Create(ast, options);

            return(fa);
        }
예제 #3
0
            // put the predicate definition (if found) into the TermNode if it is not already there
            public bool FindPredicateDefinition(PredicateTable predicateTable)
            {
                if (predDescr == null)
                {
                    //IO.WriteLine ("predDescr == null for {0}", term.Name);
                    if ((predDescr = predicateTable [term.Key]) == null)
                    {
                        return(false);
                    }
                }

#if arg1index // first-argument indexing enabled
                BaseTerm arg;

                // caching would disturb the search process (since caching does not
                // cause the arg0Index to be rebuild, since this might be to costly)
                if (predDescr.IsFirstArgIndexed && !predDescr.HasCachedValues)
                {
                    if ((arg = term.Arg(0)).IsVar)
                    {
                        nextClause = predDescr.FirstArgVarClause();
                    }
                    else // not a variable
                    {
                        nextClause = predDescr.FirstArgNonvarClause(arg.FunctorToString);

                        // check whether there is an indexed var clause
                        if (nextClause == null)
                        {
                            nextClause = predDescr.FirstArgVarClause();
                        }

                        // if the above failed, the entire predicate fails (no unification possible)
                        if (nextClause == null)
                        {
                            nextClause = ClauseNode.FAIL;
                        }
                    }

                    if (nextClause == null)
                    {
                        nextClause = predDescr.ClauseList;
                    }
                }
                else // not indexed
#endif
                nextClause = predDescr.ClauseList;

                return(true);
            }
예제 #4
0
        public AstRootNode Parse(string input, PredicateTable <TValue> predicateTable)
        {
            var lexer       = new RegexGrammarLexer(new AntlrInputStream(input));
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new RegexGrammarParser(tokenStream);

            parser.AddErrorListener(new ORegexErrorListener());

            var context = parser.expr();

            var args   = new ORegexAstFactoryArgs <TValue>(predicateTable, parser);
            var result = ORegexAstFactory <TValue> .CreateAstTree(context, args);

#if DEBUG
            AstNodeBase.Print(result);
#endif
            return(result);
        }
예제 #5
0
        public void PersonSelectionTest()
        {
            //INPUT_TEXT: Пяточкова Тамара решила выгулять Джека и встретилась с Михаилом А.М.
            var sentence = new[]
            {
                new Word("Пяточкова", SemanticType.FamilyName),
                new Word("Тамара", SemanticType.Name),
                new Word("решила", SemanticType.Other),
                new Word("выгулять", SemanticType.Other),
                new Word("Джека", SemanticType.Name),
                new Word("и", SemanticType.Other),
                new Word("встретилась", SemanticType.Other),
                new Word("с", SemanticType.Other),
                new Word("Михаилом", SemanticType.Name),
                new Word("А.", SemanticType.Other),
                new Word("М", SemanticType.Other),
            };

            //Creating table which will contain our predicates.
            var pTable = new PredicateTable <Word>();

            pTable.AddPredicate("Фамилия", x => x.SemType == SemanticType.FamilyName);  //Check if word is FamilyName.
            pTable.AddPredicate("Имя", x => x.SemType == SemanticType.Name);            //Check if word is simple Name.
            pTable.AddPredicate("Инициал", x => IsInitial(x.Value));                    //Complex check if Value is Inital character.

            var oregex = new ORegex <Word>(@"
                {Фамилия}(?<name>{Имя})                     //Comments can written inside pattern...
                |
                (?<name>{Имя})({Фамилия}|{Инициал}{1,2})?  /*...even complex ones.*/
            ", pTable);

            var persons = oregex.Matches(sentence).Select(x => new Person(x)).ToArray();

            foreach (var person in persons)
            {
                Console.WriteLine("Person found: {0}, length: {1}", person.Name, person.Words.Length);
            }
            Assert.AreEqual(persons.Length, 3);
            //OUTPUT:
            //Person found: Тамара, length: 2
            //Person found: Джека, length: 1
            //Person found: Михаилом, length: 3
        }
예제 #6
0
        public void PersonSelectionTest()
        {
            //INPUT_TEXT: Пяточкова Тамара решила выгулять Джека и встретилась с Михаилом А.М.
            var sentence = new[]
            {
                new Word("Пяточкова", SemanticType.FamilyName),
                new Word("Тамара", SemanticType.Name),
                new Word("решила", SemanticType.Other),
                new Word("выгулять", SemanticType.Other),
                new Word("Джека", SemanticType.Name),
                new Word("и", SemanticType.Other),
                new Word("встретилась", SemanticType.Other),
                new Word("с", SemanticType.Other),
                new Word("Михаилом", SemanticType.Name),
                new Word("А.", SemanticType.Other),
                new Word("М", SemanticType.Other),
            };

            //Creating table which will contain our predicates.
            var pTable = new PredicateTable<Word>();
            pTable.AddPredicate("Фамилия", x => x.SemType == SemanticType.FamilyName);  //Check if word is FamilyName.
            pTable.AddPredicate("Имя", x => x.SemType == SemanticType.Name);            //Check if word is simple Name.
            pTable.AddPredicate("Инициал", x => IsInitial(x.Value));                    //Complex check if Value is Inital character.

            var oregex = new ORegex<Word>(@"
                {Фамилия}(?<name>{Имя})                     //Comments can written inside pattern...
                |
                (?<name>{Имя})({Фамилия}|{Инициал}{1,2})?  /*...even complex ones.*/
            ", pTable);

            var persons = oregex.Matches(sentence).Select(x => new Person(x)).ToArray();

            foreach (var person in persons)
            {
                Console.WriteLine("Person found: {0}, length: {1}", person.Name, person.Words.Length);
            }
            Assert.AreEqual(persons.Length, 3);
            //OUTPUT:
            //Person found: Тамара, length: 2
            //Person found: Джека, length: 1
            //Person found: Михаилом, length: 3
        }
            // put the predicate definition (if found) into the TermNode if it is not already there
            public bool FindPredicateDefinition(PredicateTable predicateTable)
            {
                if (predDescr == null)
                {
                  //IO.WriteLine ("predDescr == null for {0}", term.Name);
                  if ((predDescr = predicateTable [term.Key]) == null) return false;
                }

                #if arg1index // first-argument indexing enabled
                BaseTerm arg;

                // caching would disturb the search process (since caching does not
                // cause the arg0Index to be rebuild, since this might be to costly)
                if (predDescr.IsFirstArgIndexed && !predDescr.HasCachedValues)
                {
                  if ((arg = term.Arg (0)).IsVar)
                nextClause = predDescr.FirstArgVarClause ();
                  else // not a variable
                  {
                nextClause = predDescr.FirstArgNonvarClause (arg.FunctorToString);

                // check whether there is an indexed var clause
                if (nextClause == null)
                  nextClause = predDescr.FirstArgVarClause ();

                // if the above failed, the entire predicate fails (no unification possible)
                if (nextClause == null)
                  nextClause = ClauseNode.FAIL;
                  }

                  if (nextClause == null)
                nextClause = predDescr.ClauseList;
                }
                else // not indexed
                #endif
                  nextClause = predDescr.ClauseList;

                return true;
            }
예제 #8
0
            private void CompileGoal(Structure goal, ref ushort failAddress, ref ushort backPatchAddress)
            {
                byte continuationRegister = env.GetRegister();
                // Allocate registers to goal arguments.
                var argRegisters = new byte[goal.Arity];

                for (int i = 0; i < goal.Arity; i++)
                {
                    object arg = goal.Argument(i);
                    if (arg is Structure)
                    {
                        argRegisters[i] = env.GetRegister();
                    }
                    else
                    {
                        var @var = arg as LogicVariable;
                        if (@var != null)
                        {
                            argRegisters[i] = this.env.InsureRegisterAndLock(@var);
                        }
                        else
                        {
                            // It's a literal.
                            argRegisters[i] = NoRegister;
                        }
                    }
                }

                // Build goal arguments into registers.
                for (int i = 0; i < goal.Arity; i++)
                {
                    if (argRegisters[i] != NoRegister)
                    {
                        CompileBuild(goal.Argument(i), NoRegister, argRegisters[i]);
                    }
                }

                // Emit call instruction
                ushort startOfCallInstruction = CurrentPC;

                BackPatch(backPatchAddress, startOfCallInstruction);
                PrologPrimitives.PrimitiveImplementation primitiveImplementation;
                bool isPrimitive = PrologPrimitives.Implementations.TryGetValue(goal.Functor,
                                                                                out primitiveImplementation);

                // Call header
                Emit(isPrimitive?Opcode.CallPrimitive : Opcode.Call, continuationRegister);
                EmitUShort(failAddress);
                backPatchAddress = CurrentPC;
                EmitUShort(0);      // This will get backpatched
                // Call target
                if (isPrimitive)
                {
                    EmitUShort(PrimitiveTable.IndexOf(primitiveImplementation));
                    EmitByte((byte)goal.Arity);
                }
                else
                {
                    EmitUShort(PredicateTable.IndexOf(this.knowledgeBase.EntryForStoring(goal.PredicateIndicator)));
                }
                // Call arguments
                for (int i = 0; i < goal.Arity; i++)
                {
                    byte reg = argRegisters[i];
                    if (reg == NoRegister)
                    {
                        EmitUShort((ushort)(0x8000 + GlobalLiteralTable.IndexOf(goal.Argument(i))));
                    }
                    else
                    {
                        EmitByte(reg);
                    }
                }
                failAddress = startOfCallInstruction;
            }
예제 #9
0
 public ORegexAstFactoryArgs(PredicateTable <TValue> predicateTable, RegexGrammarParser parser)
 {
     _predicateTable   = new PredicateTable <TValue>(predicateTable.ThrowIfNull());
     _parser           = parser.ThrowIfNull();
     CaptureGroupNames = new List <string>();
 }