示例#1
0
            public void Assert(BaseTerm assertion, bool asserta)
            {
                BaseTerm       head;
                TermNode       body = null;
                PredicateDescr pd;
                BaseTerm       assertionCopy = assertion.Copy(true);

                if (assertionCopy.HasFunctor(PrologParser.IMPLIES))
                {
                    head = assertionCopy.Arg(0);
                    body = assertionCopy.Arg(1).ToGoalList();
                }
                else
                {
                    head = assertionCopy;
                }

                if (!head.IsCallable)
                {
                    IO.Error("Illegal predicate head '{0}'", head.ToString());
                }

                string key = head.Key;

                if ((predefineds.Contains(key)) || (head.Precedence >= 1000))
                {
                    IO.Error("assert/1 cannot be applied to predefined predicate or operator '{0}'",
                             assertionCopy.Index);
                }

                predTable.TryGetValue(key, out pd);
                ClauseNode newC = new ClauseNode(head, body);

                if (pd == null) // first head
                {
                    SetClauseList(head.FunctorToString, head.Arity, newC);
                    ResolveIndices();
                }
                else if (pd.IsCacheable)
                {
                    IO.Error("assert/1 cannot be applied to cached predicate '{0}'",
                             assertionCopy.Index);
                }
                else if (asserta)                    // at beginning
                {
                    newC.NextClause = pd.ClauseList; // pd.ClauseList may be null
                    SetClauseList(head.FunctorToString, head.Arity, newC);
#if arg1index
                    pd.CreateFirstArgIndex(); // re-create
#endif
                }
                else // at end
                {
                    pd.AppendToClauseList(newC);
#if arg1index
                    pd.CreateFirstArgIndex(); // re-create
#endif
                }

                InvalidateCrossRef();
            }