Exemplo n.º 1
0
    public void Assert(Term assertion, bool asserta)
    {
      assertion = assertion.CleanCopy(); // make a fresh copy

      Term head;
      TermNode body = null;

      if (assertion.Functor == Parser.IMPLIES)
      {
        head = assertion.Arg(0);
        body = assertion.Arg(1).ToGoalList();
      }
      else
        head = assertion;

      if (head.IsVar) PrologIO.Error("Cannot assert a variable as predicate head");

      string key = head.KbKey;

      // A predefined predicate (which may also be defined as operator) may not be redefined.
      // Operators ':-', ',', ';', '-->', '->' (precedence >= 1000) may also not be redefined.

      if (predefineds.Contains(key) || (head.Precedence >= 1000))
        PrologIO.Error("assert cannot be applied to predefined predicate or system operator {0}", assertion.Index);

      PredicateDescr pd = (PredicateDescr)predTable[key];

#if persistent
      if (pd != null && pd is PersistentPredDescr)
      {
        ((PersistentPredDescr)pd).Assert (head);

        return;
      }
#endif

      ClauseNode newC = new ClauseNode(head, body);

      if (pd == null) // first head
      {
        SetClauseList(PredEnum.session, head.Functor, head.Arity, newC);
        ResolveIndices();
      }
      else if (asserta) // at beginning
      {
        newC.NextClause = pd.ClauseNode; // pd.ClauseNode may be null
        SetClauseList(PredEnum.session, head.Functor, head.Arity, newC);
#if arg1index
        pd.CreateFirstArgIndex (); // re-create
#endif
      }
      else // at end
      {
        pd.AppendToClauseList(newC);
#if arg1index
        pd.CreateFirstArgIndex (); // re-create
#endif
      }
    }