Ejemplo n.º 1
0
 public TermSet(Term list)
 {
     while (list.Arity == 2)
       {
     Add(list.Arg(0));
     list = list.Arg(1);
       }
 }
Ejemplo n.º 2
0
    public void SetUnpersistent (Term t)
    {
      Term s;

      if ( t == null ||
           t.Arity != 1 ||
           (s = t.Arg (0)).Arity != 2 ||
           s.Functor != SLASH ||
           !s.Arg (0).IsAtom ||
           !s.Arg (1).IsInteger )
        IO.Error ("Bad argument(s) {0} for unpersistent( <predicate>/<arity>)", t);

      string functor = t.Arg (0).Arg (0).Functor; // do not touch case
      int    arity   = Convert.ToInt32 (t.Arg (0).Arg (1).Functor);
      string key     = Term.Key (functor, arity);

      if (predefineds [key] != null)
        IO.Error ("Predefined predicate '{0}/{1}' cannot be declared as unpersistent", functor, arity);

      PredicateDescr pd = this [key];

      if (pd == null || !(pd is PersistentPredDescr))
        IO.Error ("Predicate '{0}/{1}' was not declared as persistent", functor, arity);
      else
        this [key] = null;
    }
Ejemplo n.º 3
0
    private enum PredEnum { session, table, execproc, selproc } // table and view are treated identically

#if persistent
    public void SetPersistent (Term t)
    {
      Term pred       = null;
      Term db_info    = null;  // table( <name>) or procedure( <name>, <executable/selectable>)
      bool isTable    = false;
      bool isExec     = false; // executable stored procedure (as opposed to selectable)
      Term dbEntity   = null;
      DbLogin dbLogin = null;
      ArrayList args  = null;
      PredEnum predType;

      t.IsPacked = false;
      if (t != null) args = t.ArgumentsToArrayList (false);
      //for (int i = 0; i < args.Count; i++) Console.WriteLine ("args [{0}] = {1}", i, args [i]);

      bool OK =
        t != null &&
        (args.Count == 2 || args.Count == 5) &&
        (pred = (Term)args [0]).Arity == 2 &&
        pred.Functor == SLASH &&
        pred.Arg (0).IsAtom &&
        pred.Arg (1).IsInteger;

      if (!OK)
        IO.Error ("Bad first argument '{0}' for persistent( <predicate>/<arity>, ...)", t.Arg (0));

      OK =
        (db_info = (Term)args [1]) != null &&
        ( (isTable = (db_info.Functor == "table" || db_info.Functor == "view")) ||
          db_info.Functor == "procedure" ||
          db_info.Functor == "proc") &&
          db_info.Arity > 0 &&
        ( (dbEntity = db_info.Arg (0)).IsAtom || dbEntity.IsString );

      if (!OK)
        IO.Error ("Bad second argument '{0}' for persistent( ..., table/view/procedure(...))", db_info);

      if (isTable)
      {
        if (db_info.Arity != 1)
          IO.Error ("Bad second argument '{0}' for persistent( ..., table/view( <db_entity_name>))", db_info);

        predType = PredEnum.table;
      }
      else
      {
        string invocation;

        OK = (db_info.Arity == 2) &&  // procedure( <name>, <executable/selectable>)
             ( (isExec = ((invocation = db_info.Arg (1).Functor) == "executable" || invocation == "exec")) ||
               invocation == "selectable" || invocation == "select" );

        if (!OK)
          IO.Error ("Bad second argument '{0}' for persistent( ..., procedure( <db_entity_name>, [selectable|executable]))", t.Arg (1));

        predType = isExec ? PredEnum.execproc : PredEnum.selproc;
      }

      string functor = pred.Arg (0).Functor;
      int    arity   = Convert.ToInt32 (pred.Arg (1).Functor);
      string index   = Term.Key (functor, arity);

      if (predefineds [index] != null)
        IO.Error ("Predefined predicate '{0}/{1}' cannot be declared as persistent", functor, arity);

      if (args.Count == 5)
      {
        for (int i = 2; i <= 4; i++)
          if (!(((Term)args [i]).IsAtom || ((Term)args [i]).IsString))
            IO.Error ("Argument '{0}' not an atom or string", (Term)args [i]);

        dbLogin = new DbLogin (((Term)args [2]).Functor, ((Term)args [3]).Functor, ((Term)args [4]).Functor);
      }

      PredicateDescr pd = this [index];

      if (pd != null) // apparently already defined
      {
        string definingFile = (pd.DefiningFile == Globals.ConsultFileName) ? "this file" : pd.DefiningFile;

        if (!(pd is PersistentPredDescr))
          IO.Error ("Predicate '{0}/{1}' cannot be declared as persistent (predicate defined in {2})", functor, arity, definingFile);
      }

      pd = SetClauseList (predType, functor, arity, null);

      ((PersistentPredDescr)pd).DbEntity = dbEntity.Functor;
      ((PersistentPredDescr)pd).DbLogin  = dbLogin;
    }
Ejemplo n.º 4
0
    public bool SetUndefPredAction(Term t, bool err)
    {
      Term pred = null;
      Term action = null;
      ArrayList args = null;
      string msg;
      bool result = true;

      t.IsPacked = false;
      if (t != null) args = t.ArgumentsToArrayList(false);

      bool OK =
        t != null &&
        (args.Count == 2) &&
        (pred = (Term)args[0]).Arity == 2 &&
        pred.Functor == SLASH &&
        pred.Arg(0).IsAtom &&
        pred.Arg(1).IsInteger;

      if (!OK)
      {
        result = false;
        msg = string.Format("Bad first argument '{0}' for undef_pred_action( <predicate>/<arity>, ...)", t.Arg(0));

        if (err) PrologIO.Error(msg); else PrologIO.Warning(msg);
      }

      OK =
        (action = (Term)args[1]) != null &&
        (action.Functor == "fail" ||
        //          action.Functor == "succeed" ||
          action.Functor == "error" ||
          action.Functor == "warning");

      if (!OK)
      {
        result = false;
        msg = string.Format("Bad second argument '{0}' for undef_pred_action( ..., fail/succeed/warning)", action);

        if (err) PrologIO.Error(msg); else PrologIO.Warning(msg);
      }

      if (result)
      {
        string key = Term.Key(pred.Arg(0).Functor, pred.Arg(1).Functor);
        actionWhenUndefined[key] = (UndefAction)Enum.Parse(typeof(UndefAction), action.Functor, false);
      }

      return result;
    }
Ejemplo n.º 5
0
    public void SetDiscontiguous(Term t)
    {
      if (t == null || t.Functor != SLASH || !t.Arg(0).IsAtom || !t.Arg(1).IsInteger)
        PrologIO.Error("Illegal or missing argument '{0}' for discontiguous/1", t);

      // The predicate descriptor does not yet exist (and may even not come at all!)
      string key = Term.Key(t.Arg(0).Functor, t.Arg(1).Functor);

      //Console.WriteLine ("--- Setting discontiguous for {0} in definingFile {1}", key, Globals.ConsultFileName);
      isDiscontiguous[key] = "true";
    }
Ejemplo n.º 6
0
    public bool SetSpy(bool enabled, string functor, int arity, Term list)
    {
      SpyPort ports;

      if (list == null)
        ports = SpyPort.full;
      else
      {
        ports = SpyPort.none;
        string s;

        while (list.Arity == 2)
        {
          s = list.Arg(0).Functor;

          try
          {
            ports |= (SpyPort)Enum.Parse(typeof(SpyPort), s);
          }
          catch
          {
            PrologIO.Error("Illegal value '{0}'", s);
          }
          list = list.Arg(1);
        }
      }

      PredicateDescr pd;

      if (arity == -1)
      {
        bool found = false;

        foreach (DictionaryEntry de in predTable)
          if ((pd = (PredicateDescr)de.Value).Name == functor)
          {
            found = true;
            pd.SetSpy(enabled, pd.Name, pd.Arity, ports, !enabled);
          }

        if (!found) PrologIO.Error("Predicate does not exist: {0}", functor);

        return found;
      }
      else
      {
        if ((pd = (PredicateDescr)predTable[Term.Key(functor, arity)]) == null)
        {
          PrologIO.Error("Predicate does not exist: {0}/{1}", functor, arity);

          return false;
        }
        pd.SetSpy(enabled, functor, arity, ports, !enabled);
      }

      return true;
    }
Ejemplo n.º 7
0
    protected ClauseNode GetExecutablePersistentData (Term t)
    {
      ProcParamCollection ppc = (ProcParamCollection)MetaDataCollection;

      if (ppc.Count != t.Arity)
        IO.Error ("Number of parameters ({0}) for stored procedure '{1}' does not match the arity of persistent predicate {2}/{3}",
                  ppc.Count, dbEntity, name, arity);
      // get all output arguments from t and construct the corresponding SELECT-statement.

      DbAc.InitExecProcedure (dbEntity, inputParamCount);

      // set up input parameters
      for (int i = 0; i < inputParamCount; i++)
      {
        Term   arg   = t.Arg (i);
        string value = null;

        if (arg.IsAtom || arg.IsString)
          value = Utils.EnsureQuoted (arg.Functor);
        else if (arg.IsNumber)
          value = arg.Functor;
        else if (arg.IsVar)
          value = "null";
        else
          IO.Error ("Illegal stored procedure input parameter {0} in persistent term {1}", arg, t);

        DbAc.AddExecInputParameter (i, ppc [i], value);
      }

      // set up output parameters
      for (int i = inputParamCount; i < ppc.Count; i++) DbAc.AddExecOutputParameter (i, ppc [i]);

      // perform the procedure call
      Term result = DbAc.DoExecProcedure (dbEntity, query);
//Console.WriteLine ("GetExecutablePersistentData -- result = {0}", result);
      return new ClauseNode (result, null);
    }
Ejemplo n.º 8
0
        // process an element( <tag>, <attributes>, <content>)
        private static bool ElementTermToXml(XmlTextWriter xwr, Term e)
        {
            if (e.Arity != 3) return false;

              // open tag
              xwr.WriteStartElement(e.Arg(0).ToString());  // writes ns:localName: Arg(0) *may* be ':'(ns, localName)

              // attributes
              Term le = e.Arg(1); // list with attribute-value pairs
              while (le != Term.NULLLIST)
              {
            Term av = le.Arg(0); // Term: attr = value
            xwr.WriteAttributeString(av.Arg(0).Functor, av.Arg(1).Functor);
            le = le.Arg(1);
              }

              // content
              if (!ContentTermToXml(xwr, e.Arg(2))) return false;
              xwr.WriteEndElement();

              return true;
        }
Ejemplo n.º 9
0
    public override void Retract (Term Term, VarStack stack, Term where)
    {
      StringBuilder deleteStat = new StringBuilder ("DELETE FROM " + dbEntity);
      TableColumnCollection tcc = (TableColumnCollection)MetaDataCollection;
      ListDictionary varNrs = new ListDictionary ();
      bool first = true;

      for (int i = 0; i < Term.Arity; i++)
      {
        Term arg = Term.Arg (i);
        string colName = tcc [i].Name;

        if (arg.IsAtom || arg.IsString)
          deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, Utils.EnsureQuoted (arg.Functor));
        else if (arg.IsNumber)
          deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, arg.Functor);
        else if (arg.IsVar)
        {
          object prevColNo;
          // check whether value occurred as argument before (anonymous vars always have a unique varNo)
          if ((prevColNo = varNrs [arg.VarNo]) != null) // ... yes
            deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), tcc [(int)prevColNo].Name, colName);
          varNrs [arg.VarNo] = i; // save for comparison with next argument(s)
        }
        else if (!arg.IsVar)
          IO.Error ("Illegal argument {0} for retract of persistent clause {1}", arg, Term);
      }

      if (where != null) // add the extra where-clause (if any)
      {
        // Copy the ref-table of column numers into a ref-table of corresponding column names
        ListDictionary varNames = new ListDictionary (); // ... in-situ modifications of Collection-values is not possible
        foreach (DictionaryEntry de in varNrs) varNames [(int)de.Key] = tcc [(int)de.Value].Name;
        deleteStat.AppendFormat ("{0} {1}", Conj (ref first), where.ToStringSql (varNames));
      }

      if (first)
        IO.Warning ("Retract -- DELETE without WHERE -- will NOT be executed !!!!!!!!");
      else
        ExecuteSqlCommand (deleteStat.ToString ());
    }
Ejemplo n.º 10
0
    public override void Assert (Term assertion)
    {
      StringBuilder insertStat = new StringBuilder ("INSERT INTO " + dbEntity + " VALUES (");
      TableColumnCollection tcc = (TableColumnCollection)MetaDataCollection;
      bool first = true;

      for (int i = 0; i < assertion.Arity; i++)
      {
        Term arg = assertion.Arg (i);
        if (first) first = false; else insertStat.Append (", ");

        if (arg.IsAtom || arg.IsString)
          insertStat.Append (Utils.EnsureQuoted (arg.Functor));
        else if (arg.IsNumber)
          insertStat.Append (arg.Functor);
        else if (arg.IsVar)
        {
          // check whether this column may be null
          if (!tcc [i].IsNullable)
            IO.Error ("Illegal attempt to insert a null-value in column '{0}' of table '{1}'",
                      tcc [i].Name, dbEntity);

          insertStat.Append ("null");
        }
        else
          IO.Error ("Illegal argument {0} for assert of persistent clause {1}", arg, assertion);
      }

      insertStat.Append (");");
//Console.WriteLine ("Assert -- about to ExecuteInsert {0}", insertStat.ToString ());
      ExecuteSqlCommand (insertStat.ToString ());
    }
Ejemplo n.º 11
0
        private bool DoBuiltin(BI biId, out bool findFirstClause)
        {
            findFirstClause = false;
              //Console.WriteLine ("DoBuiltin case {0} current goal \n{1}", biId, goalNode.Term);

              Term term = goalNode.Term;
              Term t0, t1, t2, t3;
              TermSet ts;
              int n, y, m, d, h, s;
              int arity;
              string functor;
              char ch;
              bool result;

              switch (biId)
              {
            case BI.cut:  // !
              varStack.DisableChoices(term.VarNo);
              break;

            case BI.fail:
              return false;

            case BI.or:
              PrologIO.Error("Serious error -- or-operator (;) not handled properly"); // should not occur
              return false;

            case BI.consult: // individual file or list of files
              t0 = term.Arg(0);

              if (t0.IsList)
              {
            int lines = 0;
            int files = 0;

            while (t0.Arity == 2)
            {
              string fName = Utils.FileNameFromTerm(t0.Arg(0), ".pl");
              if (fName == null) return false;
              lines += ps.Consult(fName);
              files++;
              t0 = t0.Arg(1);
            }
            if (files > 1) PrologIO.Message("Grand total is {0} lines", lines);
            ps.ResolveIndices();
            break;
              }
              if (t0.IsAtom || t0.IsString)
              {
            string fName = Utils.FileNameFromTerm(t0, ".pl");
            if (fName == null) return false;
            PrologIO.Write("--- Consulting {0} ... ", fName);
            ps.Consult(fName);
            PrologIO.WriteLine("{0} lines read", parser.LineCount);
            ps.ResolveIndices();
            break;
              }
              return PrologIO.Error("Not a valid file name: '{0}'", t0);

            case BI.asserta:
              ps.Assert(term.Arg(0), true); // true: at beginning
              break;

            case BI.assert:
            case BI.assertz:
              ps.Assert(term.Arg(0), false);
              break;

            case BI.retract:
              if (ps.Retract(term.Arg(0), varStack, null))
            currentCp.ClauseNode = retractClause;
              else
              {
            CanBacktrack(true);
            return false;
              }
              break;

            case BI.retractall: // retractall
              if (!ps.RetractAll(term.Arg(0), varStack)) return false;
              break;

            case BI.spy: // leash modes [call, exit, redo, fail]
            case BI.nospy:
              result = true;
              t0 = term.Arg(0);

              if (term.Arity == 2) t3 = term.Arg(1); else t3 = null; // leash list

              if (t0.Functor == "/" && t0.Arity == 2 && (t1 = t0.Arg(0)).IsAtom && (t2 = t0.Arg(1)).IsInteger)
            result = ps.SetSpy(term.Functor == "spy", t1.Functor, t2.ExprValue.AsInteger, t3);
              else if (t0.Arity == 0)
            result = ps.SetSpy(term.Functor == "spy", t0.Functor, -1, t3);

              if (!result) return false;

              if (!debug)
              {
            debug = true;
            PrologIO.Message("Debugging switched on");
              }
              break;

            case BI.nospyall:
              ps.SetNoSpyAll();
              break;

            case BI.verbose:
              PrologIO.Verbose = true;
              break;

            case BI.noverbose:
            case BI.silent:
              PrologIO.Verbose = false;
              break;

            case BI.trace:
            case BI.notrace:
              SetSwitch("Tracing", ref trace, term.Functor == "trace");
              if (trace) debug = true;
              reporting = debug || xmlTrace;
              break;

            case BI.debug:
            case BI.nodebug:
              SetSwitch("Debugging", ref debug, term.Functor == "debug");
              reporting = debug || xmlTrace;
              break;

            case BI.setof_init:
              term.Arg(0).Unify(new ObjectTerm(new TermSet(DupMode.dupIgnore)), varStack);
              break;

            case BI.setof_add:
              ts = (TermSet)((ObjectTerm)term.Arg(0).Value).Value;
              t1 = term.Arg(1);
              if (t1.IsVar) return false;
              t2 = t1.CleanUp();
              ts.Insert(t2);
              break;

            case BI.setof_exit:
              ts = (TermSet)((ObjectTerm)term.Arg(0).Value).Value;
              if (ts.Count == 0) return false; // setof must fail if the Generator yields no matches
              term.Arg(1).Unify(ts.ToList(), varStack);
              break;

            case BI.current_op_init:
              int pr = -1;
              string op = null;
              OType ot = OType.noop;
              OType ot1;

              t0 = term.Arg(0);
              if (t0.IsInteger)
            pr = Convert.ToInt16(t0.ExprValue.AsInteger);
              else if (!t0.IsVar)
            return false;

              t1 = term.Arg(1);
              if (t1.IsAtom)
            try
            {
              ot = (OType)Enum.Parse(typeof(OType), t1.Functor);
            }
            catch
            {
              return false;
            }
              else if (!t1.IsVar)
            return false;

              t2 = term.Arg(2);
              if (t2.IsAtom)
            op = t2.Functor;
              else if (!t2.IsVar)
            return false;

              ArrayList ta = parser.TerminalList;
              ArrayList oa = new ArrayList();
              int pr1;
              string op1;

              for (int i = 0; i < ta.Count; i++) // expand all operator descriptors into array oa
              {
            if (ta[i] is OperatorDescr)
            {
              OperatorDescr od = (OperatorDescr)ta[i];

              if (od.IsDefinedAsPrefix(out op1, out pr1, out ot1) &&
                   (pr == -1 || pr == pr1) && (op == null || op == op1) && (ot == OType.noop || ot == ot1))
                oa.Add(new OpRec(pr1, ot1, op1));
              if (od.IsDefinedAsInfix(out op1, out pr1, out ot1) &&
                   (pr == -1 || pr == pr1) && (op == null || op == op1) && (ot == OType.noop || ot == ot1))
                oa.Add(new OpRec(pr1, ot1, op1));
              if (od.IsDefinedAsPostfix(out op1, out pr1, out ot1) &&
                   (pr == -1 || pr == pr1) && (op == null || op == op1) && (ot == OType.noop || ot == ot1))
                oa.Add(new OpRec(pr1, ot1, op1));
            }
              }

              if (oa.Count == 0) return false;

              term.Arg(3).Bind(new ObjectTerm(oa)); // bind the operator array to the penultimate argument
              term.Arg(4).Bind(new Term((oa.Count - 1).ToString(), FType.number)); // bind the array count to the last argument
              break;

            case BI.next_op: // '$next_op'(S, I, P, F, N)
              t0 = term.Arg(0);
              oa = (ArrayList)((ObjectTerm)term.Arg(0).Value).Value;
              t1 = term.Arg(1); // index of current entry in oa
              n = t1.ExprValue.AsInteger;
              OpRec oprec = (OpRec)oa[n];
              term.Arg(1).Functor = (--n).ToString();
              term.Arg(2).Unify(new Term(oprec.Pr.ToString(), FType.number), varStack);
              term.Arg(3).Unify(new Term(oprec.Fx, FType.atom), varStack);
              term.Arg(4).Unify(new Term(oprec.Op, FType.atom), varStack);
              break;

            case BI.version: // version(V, R)
              if (!term.Arg(0).Unify(new Term(Utils.MakeAtom(VERSION), FType.atom), varStack)) return false;
              if (!term.Arg(1).Unify(new Term(Utils.MakeAtom(RELEASE), FType.atom), varStack)) return false;
              break;

            case BI.halt:
            case BI.quit:
            case BI.abort:
              halted = true;
              break;

            case BI.length: // length(L, N)
              t0 = term.Arg(0);
              if (t0.HasValue)
              {
            if (!t0.IsList) return false;
            n = 0;
            while (t0.Arity == 2)
            {
              n++;
              t0 = t0.Arg(1);
            }
            if (!term.Arg(1).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              }
              else // create a list with N elements
              {
            t1 = term.Arg(1);
            if (!t1.HasValue) return false;
            arity = Convert.ToInt16(t1.ExprValue.AsInteger);
            Term[] args = new Term[arity];
            t1 = Term.NULLLIST; // []
            for (int i = 0; i < arity; i++)
              t1 = new Term(Parser.DOT, new Term(), t1, FType.comp, OType.xfy, 100);
            t0.Unify(t1, varStack);
              }
              break;

            case BI.sort: // sort(L, S)
              t0 = term.Arg(0);
              t1 = term.Arg(1);

              if (t0.IsList)
              {
            if (!(t1.IsList || t1.IsVar)) return false;

            TermSet tlist = new TermSet(t0);
            tlist.Sort();

            if (!t1.Unify(tlist.ToList(), varStack)) return false;
              }
              else
            return false;
              break;

            case BI.functor: // functor(T, F, N)
              t0 = term.Arg(0);
              if (t0.HasValue)
              {
            if (!term.Arg(1).Unify(new Term(t0.Functor, 0), varStack)) return false;
            if (!term.Arg(2).Unify(new Term(t0.Arity.ToString(), FType.number), varStack)) return false;
            break;
              }
              else // Term (t0) is unbound
              {
            t1 = term.Arg(1);
            if (t1.HasValue) functor = t1.Functor; else return false;

            t2 = term.Arg(2);
            if (t2.HasValue) arity = Convert.ToInt16(t2.ExprValue.AsInteger); else return false;

            Term[] args = new Term[arity];
            for (int i = 0; i < arity; i++) args[i] = new Term();
            if (arity == 0)
              t2 = (functor == Parser.DOT) ? Term.NULLLIST : new Term(functor);
            else if (arity > 2)
              t2 = new Term(functor, args);
            else
            {
              OperatorDescr od = Parser.GetOperatorDescr(functor);

              if (od == null)
                t2 = new Term(functor, args);
              else if (arity == 1 &&
                        (od.IsDefinedAsPrefix(out functor, out pr, out ot) ||
                         od.IsDefinedAsPostfix(out functor, out pr, out ot)))
                t2 = new Term(functor, od, args, ot, pr);
              else if (arity == 2 && od.IsDefinedAsInfix(out functor, out pr, out ot))
                t2 = new Term(functor, od, args, ot, pr);
              else
                t2 = new Term(functor, args);
            }
            if (!t0.Unify(t2, varStack)) return false;
            break;
              }

            case BI.arg: // arg( N, Term, A)
              t1 = term.Arg(1);
              n = term.Arg(1).NArgs;
              if (n <= 0) return false;
              n = Convert.ToInt32(term.Arg(0).ExprValue.AsInteger);
              Term arg = t1.Arg(n - 1); // N is 1-based
              if (arg == null || !arg.Unify(term.Arg(2), varStack)) return false;
              break;

            case BI.abolish: // abolish( X/N)
              t0 = term.Arg(0);
              result = true;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg(0).IsAtom && t0.Arg(1).IsInteger)
            result = ps.Abolish(t0.Arg(0).Functor, t0.Arg(1).Functor);
              else
            result = false;
              if (!result) return false;
              break;

            case BI.gensym: // gensym( X)
              t0 = new Term("v" + gensymInt++, 0);

              if (t0.Unify(term.Arg(0), varStack))
            break;
              else
            return false;

            case BI.var:
              if (term.Arg(0).IsVar) break;
              return false;

            case BI.nonvar:
              if (term.Arg(0).IsNonVar) break;
              return false;

            case BI.atom_:
              if (term.Arg(0).IsAtom) break;
              return false;

            case BI.atomic:
              if (term.Arg(0).IsAtomic) break;
              return false;

            case BI.integer:
              if (term.Arg(0).IsInteger) break;
              return false;

            case BI.float_:
              if (term.Arg(0).IsFloat) break;
              return false;

            case BI.number:
              if (term.Arg(0).IsNumber) break;
              return false;

            case BI.compound:
              if (term.Arg(0).IsCompound) break;
              return false;

            case BI.list:
              if (term.Arg(0).IsList) break;
              return false;

            case BI.string_:
              if (term.Arg(0).IsString) break;
              return false;

            case BI.isdatetime:
              if (term.Arg(0).IsDateTime) break;
              return false;

            case BI.istimespan:
              if (term.Arg(0).IsTimeSpan) break;
              return false;

            case BI.is_: // X is Y
              t0 = new Term(term.Arg(1).ExprValue);
              if (term.Arg(0).Unify(t0, varStack)) break;
              return false;

            case BI.ne_uni: // X \= Y
              if (term.Arg(0).Unify(term.Arg(1), varStack)) return false;
              break;

            case BI.eq_num: // X =:=
              if (term.Arg(0).ExprValue.AsNumber == term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.ne_num: // X =\= Y
              if (term.Arg(0).ExprValue.AsNumber != term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.lt_num:  // X < Y
              if (term.Arg(0).ExprValue.AsNumber < term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.le_num: // X =< Y
              if (term.Arg(0).ExprValue.AsNumber <= term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.gt_num: // X > Y
              if (term.Arg(0).ExprValue.AsNumber > term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.ge_num: // X >= Y
              if (term.Arg(0).ExprValue.AsNumber >= term.Arg(1).ExprValue.AsNumber) break;
              return false;

            case BI.eq_str: // X == Y
              if (term.Arg(0).CompareTo(term.Arg(1)) == 0) break;
              return false;

            case BI.ne_str: // X \== Y
              if (term.Arg(0).CompareTo(term.Arg(1)) != 0) break;
              return false;

            case BI.lt_ord: // X @< Y
              if (term.Arg(0).CompareTo(term.Arg(1)) < 0) break;
              return false;

            case BI.le_ord: // X @=< Y
              if (term.Arg(0).CompareTo(term.Arg(1)) <= 0) break;
              return false;

            case BI.gt_ord: // X @> Y
              if (term.Arg(0).CompareTo(term.Arg(1)) > 0) break;
              return false;

            case BI.ge_ord: // X @>= Y
              if (term.Arg(0).CompareTo(term.Arg(1)) >= 0) break;
              return false;

            case BI.univ: // X =.. Y
              t0 = term.Arg(0);
              if (t0.HasValue) // create a list representation of the lhs and unify that with the rhs
              {
            functor = t0.Functor;
            arity = t0.Arity;
            Term[] args = new Term[arity];
            t1 = Term.NULLLIST; // []
            for (int i = arity; i > 0; i--) t1 = new Term(Parser.DOT, t0.Arg(i - 1), t1, FType.comp, OType.xfy, 100); // [arg1, arg2, ...]
            t1 = new Term(Parser.DOT, new Term(functor), t1, FType.comp, OType.yfx, 0); // [functor, arg1, arg2, ...]
            if (!t1.Unify(term.Arg(1), varStack)) return false;
            break;
              }
              else // Term is unbound. Create a function representation of the list rhs, and bind that to the lhs
              {
            t1 = term.Arg(1);
            if (!t1.HasValue || !t1.IsList) return false;
            if (t1.Arg(0) == null) return false; // empty list
            functor = t1.Arg(0).Functor;
            // convert rest of list to arguments: calculate arity first
            t1 = t1.Arg(1);
            arity = 0;
            t2 = t1;
            while (t2.Arity == 2)
            {
              arity++;
              t2 = t2.Arg(1);
            }
            // create arguments
            Term[] args = new Term[arity];
            for (int i = 0; i < arity; i++)
            {
              args[i] = t1.Arg(0);
              t1 = t1.Arg(1);
            }
            t1 = new Term(functor, args);
            t0.Unify(t1, varStack);
            break;
              }

            case BI.unifiable: // X can be unified with Y, but without variable bindings
              if (!term.Arg(0).Unifiable(term.Arg(1), varStack)) return false;
              break;

            case BI.see: // see(X)
              currentInputName = Utils.FileNameFromTerm(term.Arg(0), ".pl");
              if (currentInputName == null) return false;
              currentInputReader = new StreamReader(currentInputName);
              Console.SetIn(currentInputReader);
              break;

            /*
                case BI.see: // see(X)  // ORIGINAL VERSION, INTENDED FOR USE WITH read/1
                    try
                    {
                      currentInputName = null;
                      while (seeParserThread.IsAlive)
                      {
                        Utils.WriteLine ("Waiting to abort");
                        seeParserThread.Interrupt ();
                        seeParserThread.Abort ();
                      }
                    }
                    catch {}
                  currentInputName = Utils.FileNameFromTerm (term.Arg (0), ".pl");
                  if (currentInputName == null) return false;
               Utils.WriteLine ("File {0}", currentInputName);
                  Monitor.Enter (TermMonitor);
               Utils.WriteLine ("see: Monitor entered");
                  seeParserRun = new ThreadStart (RunSeeParser);
                  seeParserThread = new Thread (seeParserRun);
                  seeParserThread.IsBackground = true; // nodig?
                  seeParserThread.Start ();
               Utils.WriteLine ("see: Thread started");
                  break;
            */
            case BI.read: // read(X)
              try
              {
            if (currentInputName == null) return false;
            // Monitor.Enter () was issued in see (F)
            Monitor.Pulse(TermMonitor);
            if (Monitor.Wait(TermMonitor, 2000))
            {
              t0 = term.Arg(0);
              if (!t0.Unify(parser/*seeParser*/.ReadTerm, varStack)) return false;
            }
            else // time-out
            {
              Utils.WriteLine("read: Monitor.Wait () timed out");
              return false;
            }
              }
              finally
              {
            if (parser/*seeParser*/.AtEndOfInput)
            {
              currentInputName = null;
              try { Monitor.Exit(TermMonitor); }
              catch { }
            }
              }
              break;

            case BI.get0: // get0(C): any character
              n = (char)Console.ReadKey().KeyChar;
              if (!term.Arg(0).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              break;

            case BI.get: // get( C): skip non-printables
              do
              {
            n = Console.ReadKey().KeyChar;
            if (!Char.IsControl((char)n)) break; // break if printable
              } while (true);
              if (!term.Arg(0).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              break;

            case BI.seek: // seek( N, C)
              n = Convert.ToInt32(term.Arg(0).ExprValue.AsInteger);
              if (!parser/*seeParser*/.StreamInChar(n, out ch)) return false;
              if (!term.Arg(0).Unify(new Term(ch.ToString()), varStack)) return false;
              break;

            case BI.seen: // seen
              currentInputName = null;
              if (seeParserThread != null) seeParserThread.Abort();
              seeParserThread = null;  // implicitly set by Abort?
              break;

            case BI.tell: // tell( F)
              if (!Globals.SetCurrentOutput(Utils.FileNameFromTerm(term.Arg(0), ".pl"))) return false;
              break;

            case BI.write: // write( X)
              PrologIO.PrologPrint(term.Arg(0));
              break;

            case BI.writeq: // writeq( X)
              PrologIO.PrologPrint(term.Arg(0).ToStringQ());
              break;

            case BI.writeln: // writeln( X)
              PrologIO.PrologPrint(term.Arg(0));
              PrologIO.PrologPrint(Environment.NewLine);
              break;

            case BI.put: // put( C)
              n = Convert.ToInt32(term.Arg(0).ExprValue.AsInteger);
              PrologIO.PrologPrint(((char)n).ToString());
              break;

            case BI.nl:
              PrologIO.PrologPrint(Environment.NewLine);
              break;

            case BI.tab: // tab( +N)
              ////////////////////////// testen op Int
              n = Convert.ToInt32(term.Arg(0).ExprValue.AsInteger);
              PrologIO.PrologPrint(new String(' ', n));
              break;

            case BI.display: // like writeln ( X), but always to standard ouput
              Globals.SetStandardOutput();
              PrologIO.PrologPrint(term.Arg(0));
              PrologIO.PrologPrint(Environment.NewLine);
              Globals.RevertToCurrentOutput();
              break;

            case BI.told:
              Globals.TryCloseCurrentOutput();
              break;

            case BI.atom_chars: // name( ?A, ?L)
            case BI.name: // name( ?A, ?L)
              t1 = term.Arg(1);
              if (t1.HasValue)
              {
            FType fType;
            string a;

            if (t1.IsList)
            {
              StringBuilder sb = new StringBuilder();
              while (t1.Arity == 2)
              {
                t2 = t1.Arg(0);
                if (!t2.IsInteger) return false;
                sb.Append((char)t2.ExprValue.AsInteger);
                t1 = t1.Arg(1);
              }
              a = Utils.MakeAtom_ic(sb.ToString(), true, out fType);
              if (!term.Arg(0).Unify(new Term(a, fType), varStack)) return false;
            }
            else if (t1.IsString)
            {
              a = Utils.MakeAtom_ic(t1.Functor, true, out fType);
              if (!term.Arg(0).Unify(new Term(a, fType), varStack)) return false;
            }
            else
              return false;
              }
              else // create a list containing A's character codes
              {
            t0 = term.Arg(0);
            if (!t0.IsAtomic) return false;
            char[] chars = t0.Functor.ToCharArray();
            t0 = Term.NULLLIST; // []
            for (int i = chars.Length - 1; i >= 0; i--)
            {
              t2 = new Term(((int)chars[i]).ToString(), FType.number);
              t0 = new Term(Parser.DOT, t2, t0, FType.comp, OType.xfy, 100);
            }
            t1.Unify(t0, varStack);
              }
              break;

            case BI.expand_term: // expand_term( +(P-->Q), -R)
              t0 = term.Arg(0); // P-->Q
              t1 = term.Arg(1); // R
              Term head = t0.Arg(0);
              TermNode body = t0.Arg(1).ToDCG(ref head);
              t2 = new Term(new ClauseNode(head, body)).CleanUp();
              if (!t1.Unify(t2, varStack)) return false;
              break;

            case BI.numbervars: // numbervars(+X, +B, -E)
              t0 = term.Arg(0);
              t1 = term.Arg(1);
              t2 = term.Arg(2);
              if (!t1.IsInteger || t2.HasValue) return false;
              int k = (int)t1.ExprValue.AsInteger;
              t0.NumberVars(ref k, varStack);
              t2.Unify(new Term(k.ToString(), FType.number), varStack);
              break;

            case BI.writef0: // writef( S, L)
              t0 = term.Arg(0);
              t1 = term.Arg(1);
              if (!t0.IsString) return false;
              ArrayList al = new ArrayList();
              while (t1.Arity == 2)
              {
            t2 = t1.Arg(0);
            string a = null;
            if (t2.Functor == "tree" && t2.Arity == 1)
              a = t2.Arg(0).ToTree();
            else if (t2.Functor == "raw" && t2.Arity == 1)
              a = t2.Arg(0).ToRaw();
            al.Add((a == null) ? t2.ToString() : a);
            t1 = t1.Arg(1);
              }
              PrologIO.WriteLine(t0.Functor, al.ToArray());
              break;

            case BI.writef: // writef( S)
              t0 = term.Arg(0);
              if (!t0.IsString) return false;
              PrologIO.WriteLine(t0.Functor);
              break;

            case BI.username: // username( X)
              if (!term.Arg(0).Unify(new Term(Environment.UserName), varStack)) return false;
              break;

            case BI.shell: // shell( X [,Args])
              t0 = term.Arg(0);
              if (!(t0.IsString || t0.IsAtom))
            return false;
              else if (term.Arity == 1)
            Process.Start(t0.Functor);
              else if (!((t1 = term.Arg(1)).IsString || t1.IsAtom))
            return false;
              else
            Process.Start(t0.Functor, Utils.Dequoted(t1.Functor));
              break;

            /*
            namespace MyProcessSample
            {
            /// <summary>
            /// Shell for the sample.
            /// </summary>
            class MyProcess
            {
                // These are the Win32 error code for file not found or access denied.
                const int ERROR_FILE_NOT_FOUND =2;
                const int ERROR_ACCESS_DENIED = 5;

                /// <summary>
                /// Prints a file with a .doc extension.
                /// </summary>
                void PrintDoc()
                {
                    Process myProcess = new Process();

                    try
                    {
                        // Get the path that stores user documents.
                        string myDocumentsPath =
                            Environment.GetFolderPath(Environment.SpecialFolder.Personal);

                        myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc";
                        myProcess.StartInfo.Verb = "Print";
                        myProcess.StartInfo.CreateNoWindow = true;
                        myProcess.Start();
                    }
                    catch (Win32Exception e)
                    {
                        if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                        {
                            Console.WriteLine(e.Message + ". Check the path.");
                        }

                        else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
                        {
                            // Note that if your word processor might generate exceptions
                            // such as this, which are handled first.
                            Console.WriteLine(e.Message +
                                ". You do not have permission to print this file.");
                        }
                    }
                }
            */

            case BI.predicatePN: // predicate( +P/N)
              t0 = term.Arg(0);
              result = true;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg(0).IsAtom && t0.Arg(1).IsInteger)
            result = ps.IsPredicate(t0.Arg(0).Functor, (int)t0.Arg(1).ExprValue.AsInteger);
              else
            result = false;
              if (!result) return false;
              break;

            case BI.predicateX: // predicate( +T)
              t0 = term.Arg(0);
              if (t0.IsVar || !ps.IsPredicate(t0.Functor, t0.NArgs)) return false;
              break;

            #if persistent
            case BI.retract_where: // retract(X) where Y -- for persistent predicates only. No backtracking, delete the whole shebang in one go
              t0 = term.Arg (0).Arg (0); // X
              if (!ps.IsPersistent (t0)) IO.Error ("retract/1: 'where' not allowed on non-persistent predicate {0}", t0.KbKey);
              t1 = term.Arg (1); // Y
              if (!ps.Retract (t0, varStack, t1)) return false;
              break;

            case BI.persistent: // persistent( +P/N)
              t0 = term.Arg (0);
              result = true;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg (0).IsAtom && t0.Arg (1).IsInteger)
            result = ps.IsPersistent (t0.Arg (0).Functor, (int)t0.Arg (1).ExprValue.AsInteger);
              else
            result = false;
              if (!result) return false;
              break;

            case BI.whereXY: // X where Y
              t0 = term.Arg (0); // X
              if (!ps.IsPersistent (t0)) IO.Error ("'where' not allowed on non-persistent predicate {0}", t0.KbKey);
              t1 = term.Arg (1); // Y
              // insert the goal in the argument into the current goalNode
              goalNode = t0.ToGoalList ().Append (goalNode.NextNode);
              goalNode.FindPredicateDefinition (ps, t1);
              return true;

            case BI.persistent_info: // persistent_info( X/N, L) -- give a list with a description of each column
              t0 = term.Arg (0);
              t1 = term.Arg (1);
              t2 = null;

              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg (0).IsAtom && t0.Arg (1).IsInteger)
            t2 = ps.PersistentInfo (t0.Arg (0).Functor, t0.Arg (1).Functor);
              else
            return false;

              if (!t1.Unify (t2, varStack)) return false; // t1 is output-arg
              break;

            case BI.persistent_uncache: // persistent_uncache( X/N) -- empty persistent predicate X/N's cache
              t0 = term.Arg (0);
              t1 = term.Arg (1);
              PredicateDescr pd;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg (0).IsAtom && t0.Arg (1).IsInteger)
            pd = ps [Term.Key (t0.Arg (0).Functor, t0.Arg (1).Functor)];
              else
            return false;
              if (pd == null || !(pd is PersistentPredDescr))
              {
            IO.Warning ("Predicate '{0}/{1}' was not declared as persistent", t0.Arg (0).Functor, t0.Arg (1).Functor);
            return false;
              }
              ((PersistentPredDescr)pd).InvalidateCache ();
              break;
            #endif

            case BI.ground: // ground( +T)
              if (!term.Arg(0).IsGround()) return false;
              break;

            case BI.today: // date( ?Y, ?M, ?D)
              y = DateTime.Today.Year;
              m = DateTime.Today.Month;
              d = DateTime.Today.Day;
              if (!term.Arg(0).Unify(new Term(y.ToString(), FType.number), varStack)) return false;
              if (!term.Arg(1).Unify(new Term(m.ToString(), FType.number), varStack)) return false;
              if (!term.Arg(2).Unify(new Term(d.ToString(), FType.number), varStack)) return false;
              break;

            case BI.now: // time( ?H, ?M, ?S)
              h = DateTime.Now.Hour;
              m = DateTime.Now.Minute;
              s = DateTime.Now.Second;
              if (!term.Arg(0).Unify(new Term(h.ToString(), FType.number), varStack)) return false;
              if (!term.Arg(1).Unify(new Term(m.ToString(), FType.number), varStack)) return false;
              if (!term.Arg(2).Unify(new Term(s.ToString(), FType.number), varStack)) return false;
              break;

            case BI.validdate: // validdate( +Y, +M, +D)
              t0 = term.Arg(0);
              if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
              t1 = term.Arg(1);
              if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
              t2 = term.Arg(2);
              if (t2.IsInteger) d = (int)t2.ExprValue.AsInteger; else return false;
              try { new DateTime(y, m, d); }
              catch { return false; }
              break;

            case BI.validtime: // validtime( +H, +M, +S)
              t0 = term.Arg(0);
              if (t0.IsInteger) h = (int)t0.ExprValue.AsInteger; else return false;
              t1 = term.Arg(1);
              if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
              t2 = term.Arg(2);
              if (t2.IsInteger) s = (int)t2.ExprValue.AsInteger; else return false;
              try { new DateTime(2000, 1, 1, h, m, s); }
              catch { return false; }
              break;

            case BI.dayname: // dayname( +Y, +M, +D, ?N)
              DayOfWeek dow;
              t0 = term.Arg(0);
              if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
              t1 = term.Arg(1);
              if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
              t2 = term.Arg(2);
              if (t2.IsInteger) d = (int)t2.ExprValue.AsInteger; else return false;
              try { dow = new DateTime(y, m, d).DayOfWeek; }
              catch { return false; }
              if (!term.Arg(3).Unify(new Term(dow.ToString("G"), FType.text), varStack)) return false;
              break;

            case BI.dayofweek: // dayofweek( +Y, +M, +D, ?N)
              t0 = term.Arg(0);
              if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
              t1 = term.Arg(1);
              if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
              t2 = term.Arg(2);
              if (t2.IsInteger) d = (int)t2.ExprValue.AsInteger; else return false;
              try { n = (int)new DateTime(y, m, d).DayOfWeek; }
              catch { return false; }
              if (!term.Arg(3).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              break;

            case BI.dayofyear: // dayofyear( +Y, +M, +D, ?N)
              t0 = term.Arg(0);
              if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
              t1 = term.Arg(1);
              if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
              t2 = term.Arg(2);
              if (t2.IsInteger) d = (int)t2.ExprValue.AsInteger; else return false;
              try { n = (int)new DateTime(y, m, d).DayOfYear; }
              catch { return false; }
              if (!term.Arg(3).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              break;

            case BI.leapyear: // leapyear( +Y)
              t0 = term.Arg(0);
              if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
              if (!DateTime.IsLeapYear(y)) return false;
              break;

            case BI.weekno: // weekno(+Y, +M, +D, ?N) // week number of date Y-M-D, or current week number
              if (term.Arity == 4)
              {
            t0 = term.Arg(0);
            if (t0.IsInteger) y = (int)t0.ExprValue.AsInteger; else return false;
            t1 = term.Arg(1);
            if (t1.IsInteger) m = (int)t1.ExprValue.AsInteger; else return false;
            t2 = term.Arg(2);
            if (t2.IsInteger) d = (int)t2.ExprValue.AsInteger; else return false;
            try { n = Utils.WeekNo(new DateTime(y, m, d)); }
            catch { return false; } // invalid date
              }
              else
            n = Utils.WeekNo(DateTime.Today);
              if (!term.Arg(term.Arity - 1).Unify(new Term(n.ToString(), FType.number), varStack)) return false;
              break;
            case BI.prob:
              if (!term.Arg(0).Unify(new Term(new Prolog.Term.NumberValue(new decimal(resProb))), varStack)) return false;
              break;
            case BI.probdebuglevel:
              t0 = term.Arg(0);
              if (t0.IsInteger) probDebugLevel = (int)t0.ExprValue.AsInteger;
              PrologIO.WriteLine("Probability debug level:" + probDebugLevel);
              break;
            case BI.probtimedecay:
              t0 = term.Arg(0);
              if (!t0.IsString || t0.Functor == null) return false;
              timeDecayEnabled = ("yes".Equals(t0.Functor.ToLower()) || "y".Equals(t0.Functor.ToLower()));
              PrologIO.WriteLine("Probability time decay:" + timeDecayEnabled);
              break;
            case BI.xml_term: // xml_term( [C,] ?X, ?P) converts between XML and Prolog representation
              string x;
              Term ss = null;
              bool settings = (term.Arity == 3);

              if (settings)
              {
            ss = term.Arg(0);
            t0 = term.Arg(1);
            t1 = term.Arg(2);
              }
              else
              {
            t0 = term.Arg(0);
            t1 = term.Arg(1);
              }

              if (t0.HasValue)
              {
            x = t0.Functor;
            bool inFile = (t0.Arity == 1 && x == "see");  // is it the name of a source file containing the XML structure?
            bool outFile = (t0.Arity == 1 && x == "tell"); // ... or the name of a destination file containing the XML structure?
            if (inFile || outFile)
            {
              x = Utils.FileNameFromTerm(t0.Arg(0), ".xml");
              if (x == null) return false;
            }
            if (outFile)
            {
              if (!t1.HasValue) return false;

              if (!Node.TermToXml(ss, t1, ref x)) return false;
            }
            else
            {
              t2 = Node.XmlToTerm(x, inFile);
              //Console.WriteLine (t2.ToTree ());
              if (!t1.Unify(t2, varStack)) return false;
            }
            break;
              }
              else if (t1.HasValue)
              {
            x = null;
            if (!Node.TermToXml(ss, t1, ref x)) return false;
            t2 = new Term(x, FType.text);
            if (!t0.Unify(t2, varStack)) return false;
            break;
              }
              else
            return false;

            case BI.listing: // listing
              if (!ps.ListAll(null, -1, false, true)) return false; // i.e. no predefined, all user
              break;

            case BI.listingXN: // listing( X/N)
              t0 = term.Arg(0);
              result = true;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg(0).IsAtom && t0.Arg(1).IsInteger)
            result = ps.ListAll(t0.Arg(0).Functor, t0.Arg(1).ExprValue.AsInteger, false, true);
              else
            result = false;
              if (!result) return false;
              break;

            case BI.listingX: // listing( X) -- list all predicates X/N (i.e. for each N)
              t0 = term.Arg(0);
              if (!t0.IsAtom) return false;
              if (!ps.ListAll(t0.Functor, -1, false, true)) return false;
              break;

            case BI.listing0: // listing0
              if (!ps.ListAll(null, -1, true, false)) return false; // i.e. no user, all predefined
              break;

            case BI.listing0XN: // listing0( X/N)
              t0 = term.Arg(0);
              result = true;
              if (t0.Functor == "/" && t0.Arity == 2 && t0.Arg(0).IsAtom && t0.Arg(1).IsInteger)
            result = ps.ListAll(t0.Arg(0).Functor, t0.Arg(1).ExprValue.AsInteger, true, false);
              else
            result = false;
              if (!result) return false;
              break;

            case BI.listing0X: // listing0( X)
              t0 = term.Arg(0);
              if (!t0.IsAtom) return false;
              if (!ps.ListAll(t0.Functor, -1, true, false)) return false;
              break;

            case BI.pp_defines: // pp_defines( X) -- preprocessor symbol definitions -- mainly useful for debugging in nested calls
              t0 = term.Arg(0);
              if (!t0.IsVar) return false;
              t1 = Term.NULLLIST; // []
              //Console.WriteLine ("Parser.PpSymbols.count = {0}", Parser.PpSymbols.Count);
              foreach (DictionaryEntry de in Parser.PpSymbols)
            t1 = new Term(Utils.MakeAtom(de.Key as string), new Term(), t1, FType.comp, OType.xfy, 100);
              t0.Unify(t1, varStack);
              break;

            case BI.undefineds:
              ps.FindUndefineds();
              break;

            case BI.copy_term: // copy_term( X, Y)
              if (!term.Arg(1).Unify(term.Arg(0).CleanCopy(), varStack)) return false;
              break;

            case BI.undef_pred_action: // undef_pred_action( X/1, A)
              t0 = term.Arg(0);
              t1 = term.Arg(1);
              t2 = new Term(Parser.COMMA, t0, t1, FType.comp, OType.xfy, 1000);
              if (!ps.SetUndefPredAction(t2, false)) return false;
              break;

            case BI.clearall: // clearall
              ReadBuiltinPredicates();
              break;

            case BI.spypoints: // spypoints
              ps.ShowSpypoints();
              break;

            case BI.clause: // clause(Head,Body)
              t0 = term.Arg(0); // head
              t1 = term.Arg(1); // body

              if (!t0.HasValue)
              {
            // head is not instantiated --> no predicate specified
            // changed from Console.Writeln to general warning LI 2009/10/07
            PrologIO.Warning("ERROR: Arguments are not sufficiently instantiated for clause/2.");
            return false;
              }

              // find predicate
              PredicateDescr pdsc = ps[t0.KbKey];
              if (pdsc == null) return false;

              // get current clause for it
              TermNode tn = ((lastCp != null) && (lastCp is ClauseChoicePoint)) ?
            ((ClauseChoicePoint)lastCp).NextClause : pdsc.GetClauseList(t0, null);

              if (tn == null) return false;

              // prepare for redo //maybe only needed if further tn clause existent?
              varStack.Push(new ClauseChoicePoint(goalNode, goalNode.NextClause, tn.NextClause));
              findFirstClause = true;

              // unify t0 with the head and t1 with the clause
              if (!tn.Term.Unify(t0, varStack))
            return false;

              TermNode tm = tn.NextNode;

              if ((tm == null) || (tm.BuiltinId != BI.none))
              {
            // No body for the clause. Either a fact (unify t1 with TRUE) or a buildin predicate.
            if (!t1.Unify(new Term(Utils.MakeAtom(tm == null ? "true" : "builtin predicate"),
                FType.atom), varStack)) return false;
              }
              else if (tm.NextNode == null)
              {
            // body consists of one single goal.
            if (!tm.Term.Unify(t1, varStack)) return false;
              }
              else
              {
            t0 = t0.TermSeq(tm);

            if (!t0.Unify(t1, varStack)) return false;
              }
              break;

            case BI.member: // member( X, L, Rest)
              if (!(t0 = term.Arg(0)).HasValue || !(t1 = term.Arg(1)).IsList) return false;

              result = false;

              while (t1.Arity == 2)
              {
            if (result = t0.Unify(t1.Arg(0), varStack)) break;

            t1 = t1.Arg(1);
              }
              currentCp.Kill(); // no backtracking to follow -> remove the choicepoint for the alternative clauses
              if (!result) return false;
              break;

            // BACKTRACKING VERSION
            //          while (t1.Arity == 2)
            //          {
            //            if (result = t0.Unify (t1.Arg (0), varStack))
            //            {
            //              if ((t0 = t1.Arg (1)).Arity == 0) // empty list
            //                currentCp.Kill (); // no backtracking to follow -> remove the choicepoint for the alternative clauses
            //              else
            //                term.Arg (2).Bind (t0);  // set Rest to remainder of list (for backtracking)
            //
            //              break;
            //            }
            //            t1 = t1.Arg (1);
            //          }

            case BI.append: // append( [_|_], [_|_], L)
              if (!(t0 = term.Arg(0)).IsList || !(t1 = term.Arg(1)).IsList) return false;

              currentCp.Kill(); // no backtracking to follow -> remove the choicepoint for the alternative clauses

              if (!term.Arg(2).Unify(t0.AppendList(t1), varStack)) return false;
              break;

            case BI.match_regex: // regex( +Source, +Pattern, ?Result, ?Options)
              if (!((t0 = term.Arg(0)).IsString || t0.IsAtom) || !((t1 = term.Arg(1)).IsString || !t1.IsAtom)) return false;

              Match[] matches = Utils.FindRegexMatches(t0.Functor, t1.Functor, false);

              if (matches.Length == 0) return false;

              //foreach (Match mt in matches) Console.WriteLine ("index {0} match {1}", mt.Index, mt.Value);

              t2 = Term.NULLLIST; // []
              bool asAtom = true;

              for (int i = matches.Length - 1; i >= 0; i--)
            t2 = new Term(Parser.DOT, Term.MakeMatchTerm(matches[i], asAtom), t2, FType.comp, OType.xfy, 100);

              if (!term.Arg(2).Unify(t2, varStack)) return false;
              break;

            case BI.xmltrace: // xmltrace( X) -- send the execution tree of the next query to file X
              // Must be the first goal of a query, in order to avoid problems that
              // arise when it gets involved in backtracking.
              if (xmlTrace)
            PrologIO.Error("Execution trace is already being logged in {0}", xmlFile);
              else if (!(t0 = term.Arg(0)).IsAtom && !t0.IsString)
            PrologIO.Error("Not a valid file name: '{0}'", t0);
              else if (!firstGoal)
              {
            PrologIO.WriteLine("{0}*** {1}/1/2 must be the first goal of the query -- ignored{0}",
                          Environment.NewLine, term.Functor);
            break;
              }
              else if ((xmlFile = Utils.FileNameFromTerm(t0, ".xml")) == null)
            return false;

              n = (term.Arity == 2) ? term.Arg(1).ExprValue.AsInteger : INF;

              if (n < 1) PrologIO.Error("Maximum number of elements must exceed 0");

              XmlTraceOpen(term.Functor, n);
              break;

            case BI.numcols: // numcols( N) -- number of columns in the DOS-box
            #if mswindows
              if (!term.Arg(0).Unify(new Term(Utils.NumCols.ToString(), FType.number), varStack)) return false;
              break;
            #else
              return false;
            #endif

            case BI.userroles:
            #if mswindows
              WindowsIdentity ident = WindowsIdentity.GetCurrent();
              WindowsPrincipal principal = new WindowsPrincipal(ident);
              //bool admin = principal.IsInRole (WindowsBuiltInRole.Administrator);
              PrologIO.Message("{0} belongs to: ", principal.Identity.Name.ToString());

              Array wbirFields = Enum.GetValues(typeof(WindowsBuiltInRole));

              foreach (object roleName in wbirFields)
              {
            try
            {
              PrologIO.Message("{0}? {1}.", roleName, principal.IsInRole((WindowsBuiltInRole)roleName));
            }
            catch (Exception)
            {
              PrologIO.Message("Could not obtain role for RID {0}", roleName);
            }
              }
            #else
              PrologIO.Error ("userroles only available if compiler symbol mswindows is defined");
            #endif
              break;

            case BI.statistics: // statistics( X, [MSec,_]) // this version actually only returns the current time
              t1 = term.Arg(1).Arg(0);
              long time = ClockTicksMSecs();
              if (!t1.Unify(new Term(time.ToString(), FType.number), varStack)) return false;
              break;

            case BI.environment: // environment( X, Y) -- unify Y with atom value of environment variable X
              t0 = term.Arg(0);
              if (!t0.IsAtom) return false;
              string es;

              switch (t0.Functor.ToLower())
              {
            case "applicationdata":
              es = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
              break;
            case "localapplicationdata":
              es = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
              break;
            case "cookies":
              es = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
              break;
            case "desktopdirectory":
              es = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
              break;
            case "internetcache":
              es = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
              break;
            case "programfiles":
              es = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
              break;
            case "startup":
              es = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
              break;
            case "commandline":
              es = Environment.CommandLine;
              break;
            case "currentdirectory":
              es = Environment.CurrentDirectory;
              break;
            case "machinename":
              es = Environment.MachineName;
              break;
            case "newline":
              es = Environment.NewLine;
              break;
            case "osversion":
              es = Environment.OSVersion.ToString();
              break;
            case "stacktrace":
              es = Environment.StackTrace;
              break;
            case "systemdirectory":
              es = Environment.SystemDirectory;
              break;
            case "tickcount":
              es = Environment.TickCount.ToString();
              break;
            case "userdomainname":
              es = Environment.UserDomainName;
              break;
            case "userinteractive":
              es = Environment.UserInteractive.ToString();
              break;
            case "username":
              es = Environment.UserName;
              break;
            case "version":
              es = Environment.Version.ToString();
              break;
            case "workingset":
              es = Environment.WorkingSet.ToString();
              break;
            default:
              return false;
              }
              if (!term.Arg(1).Unify(new Term(es), varStack)) return false;
              break;

            case BI.query_timeout:
              t0 = term.Arg(0);
              if (t0.IsVar)
              {
            t0.Unify(new Term(queryTimeout.ToString(), FType.number), varStack);

            break;
              }
              if (!t0.IsInteger || (queryTimeout = t0.ExprValue.AsInteger) < 0) return false;
              break;

            case BI.inc_counter: // inc_counter( N) -- value of N is increased at each redo!!
              t0 = term.Arg(0);
              if (!t0.IsInteger) return false;
              t0.Functor = (t0.ExprValue.AsInteger + 1).ToString();
              break;

            //        case BI.vvv:
            //          PredicateDescr p = ps [Term.Key ("value", 2)];
            //          p.DumpClauseList ();
            //          break;

            default:
              return PrologIO.Error("Undefined built-in: {0}", term);
              }

              goalNode = goalNode.NextNode;

              if (reporting) // advance the goalList until a non-spypoint is encountered. Show spy(-exit)-info.
              {
            TermNode sp = null;

            while (goalNode is SpyPoint)
            {
              sp = ((SpyPoint)goalNode).SaveGoal;
              if (Debugger(SpyPort.exit, sp, null, false, 1)) break;
              goalNode = sp.NextNode;
            }
              }

              findFirstClause = true;

              return true;
        }
Ejemplo n.º 12
0
    public bool Unify(Term t, VarStack varStack, bool occurCheck)
    {
      if (isUnified) return ULink.Unify(t, varStack, occurCheck);
      if (t.isUnified) return Unify(t.ULink, varStack, occurCheck);
      //Interpreter.UCount++;

      if (hasValue)
      {
        if (t.hasValue)
        {
          if (_functor.Equals(t._functor) && arity == t.Arity) // => two cuts will unify as well
          {
            for (int i = 0; i < arity; i++) if (!args[i].Unify(t.Arg(i), varStack, occurCheck)) return false;

            return true;
          }
          else
            return false;
        }
        // t is an unbound variable (add occur check here if deemed necessary)
        t.Bind(this);
        varStack.Push(t);
      }
      else // 'this' is an unbound variable. Add occur check here if deemed necessary
      {
        this.Bind(t);
        varStack.Push(this);
      }

      return true;
    }
Ejemplo n.º 13
0
    private static void DCGGoal(Term t, ref TermNode body, ref Term remainder, ref bool embedded)
    {
      Term temp;

      if (t.IsString || t is Cut)
        body.Append(t);
      else if (t.Functor == Parser.CURL)
      {
        while (t.Arity == 2)
        {
          body.Append(t.Arg(0));
          t = t.Arg(1);
          embedded = true;
        }
      }
      else if (t.IsList)
      {
        temp = new Term();
        if (t == NULLLIST) t = temp; else t.SetRightmostArg(temp);

        if (embedded)
        {
          body.Append(new Term(Parser.EQ, remainder, t, FType.comp, OType.xfx, 700));
          embedded = false;
        }
        else
          remainder.Bind(t); // in this case, nothing is appended to body, which may be left empty (e.g. t-->[x])

        remainder = temp;
      }
      else if (t.IsAtom || t.IsCompound)
      {
        t = new DCGTerm(t, ref remainder);
        body.Append(t);
      }
      else if (t.IsVar)
        PrologIO.Error("Variable not allowed in DCG-clause: {0}", t.VarName());
      else
        PrologIO.Error("Illegal term in DCG-clause: {0}", t);
    }
Ejemplo n.º 14
0
        // xmlTerm = xml( [<pi>?], element (...))
        /// Conversion of a Prolog Term to an XML-structure (in a string or in a file)
        public static bool TermToXml(Term settings, Term xmlTerm, ref string fileNameOrXmlString)
        {
            // get settings
              bool isXChars = true; // not used
              bool isFormat = true;
              bool isRemPrf = false; // not used
              bool isIndent = true;
              bool isDtdChk = false; // not used
              Encoding encoding = Encoding.UTF8;

              while (settings != null && settings != Term.NULLLIST) // traverse ...
              {
            Term e = settings.Arg(0);
            string setting = e.Functor;
            string value; // value of setting
            if (e.Arity == 1) value = e.Arg(0).Functor; else continue;
            switch (setting)
            {
              case "extended_characters": // Use the extended character entities for XHTML (default true)
            isXChars = (value == "true");
            break;
              case "format": // Strip layouts when no character data appears between elements.
            isFormat = (value == "true");
            break;
              case "remove_attribute_prefixes": // Remove namespace prefixes from attributes when it
            // is the same as the prefix of the parent element
            isRemPrf = (value == "true");
            break;
              case "indent": // Indent the element content (2 spaces)
            isIndent = (value == "true");
            break;
              case "encoding": // Encoding to appear in XML-declaration
            //encoding = Encoding.GetEncoding (value); // Strings, such as "utf-8", should work, but don't
            encoding = Encoding.GetEncoding(Convert.ToInt32(value)); // 65001 = "utf-8"
            break;
              case "check_dtd": // Read the referenced DTD
            isDtdChk = (value == "true");
            break;
            }
            settings = settings.Arg(1);
              }

              bool result = false;
              XmlTextWriter xwr = null;
              StringWriter sw = new StringWriter();

              try
              {
            Term t0 = xmlTerm.Arg(0);

            if (fileNameOrXmlString == null) // return flat XmlString
            {
              xwr = new XmlTextWriter(sw);
              xwr.Formatting = Formatting.None;
            }
            else // write to file
            {
              xwr = new XmlTextWriter(fileNameOrXmlString, encoding);
              xwr.Formatting = isFormat ? Formatting.Indented : Formatting.None;
              xwr.Indentation = 2;
              xwr.IndentChar = ' '; // default
              xwr.Namespaces = true;
              ContentTermToXml(xwr, t0);
            }
            result = ElementTermToXml(xwr, xmlTerm.Arg(1)); // top-level element
              }
              finally
              {
            if (fileNameOrXmlString == null)
              fileNameOrXmlString = sw.ToString();
            else
              xwr.Close();
              }
              return result;
        }
Ejemplo n.º 15
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
      }
    }
Ejemplo n.º 16
0
    protected ClauseNode GetSelectablePersistentData (Term t, Term where)
    {
      ListDictionary varNrs = new ListDictionary ();
      ProcParamCollection ppc = (ProcParamCollection)MetaDataCollection;
      bool first;
      int  varNo;

      if (ppc.Count != t.Arity)
        IO.Error ("Number of parameters ({0}) for stored procedure '{1}' does not match the arity of persistent predicate {2}/{3}",
                  ppc.Count, dbEntity, name, arity);
      // get all arguments from t and construct the corresponding SELECT-statement.
      // The arguments must be either atomic or var, but may not be compound (apart from date(...))

      StringBuilder fetchStat = new StringBuilder ("SELECT NULL");

      for (int i = inputParamCount; i < ppc.Count; i++) fetchStat.AppendFormat (", {0}", ppc [i].Name); // output parameters only

      fetchStat.Append (" FROM ");
      int keyStart = fetchStat.Length; // Dataset key, to test whether the query result has already been executed before
      fetchStat.Append (dbEntity);

      // append stored procedure input parameters (if any)

      if (inputParamCount > 0)
      {
        fetchStat.Append (" (");
        varNo = 0;
        first = true;

        for (int i = 0; i < inputParamCount; i++)
        {
          if (first) first = false; else fetchStat.Append (", ");

          Term tt = t.Arg (varNo++);

          if (tt.IsAtom || tt.IsString)
            fetchStat.Append ("'" + tt.Functor + "'");
          else if (tt.IsNumber)
            fetchStat.Append (tt.Functor);
          else
            IO.Error ("Illegal stored procedure input parameter {0} in persistent term {1}", tt, t);
        }
        fetchStat.Append (")");
      }

      // construct a condition in a where-clause for each instantiated (output) argument of t

      varNo = 0; // t.Arg (0) is first output parameter (if any)
      first = true;

      for (int colNo = inputParamCount; colNo < ppc.Count; colNo++)
      {
        string colName = ppc [colNo].Name;
        Term   tt = t.Arg (varNo);

        if (tt.IsVar)
        {
          object prevColNo;
          // check whether value occurred as argument before (anonymous vars always have a unique varNo)
          if ((prevColNo = varNrs [tt.VarNo]) != null) // ... yes
            fetchStat.AppendFormat ("{0} {1}={2}", Conj (ref first), ppc [(int)prevColNo].Name, colName);
          varNrs [tt.VarNo] = colNo; // save for comparison with next argument(s)
        }
        else if (!(tt.IsAtom || tt.IsString || tt.IsNumber))
         IO.Error ("Illegal argument {0} in persistent term {1}", tt, t);

        varNo++;
      }

      // add the extra where-clause (if any)

      if (where != null)
      {
        // Copy the ref-table of column numers into a ref-table of corresponding column names
        ListDictionary varNames = new ListDictionary (); // ... in-situ modifications of Collection-values is not possible
        foreach (DictionaryEntry de in varNrs)
          varNames [(int)de.Key] = ppc [(int)de.Value].Name;
        fetchStat.AppendFormat ("{0} {1}", Conj (ref first), where.ToStringSql (varNames));
      }

      fetchStat.Append (";");
//Console.WriteLine ("ProcedurePredDescr.GetPersistentData -- fetchStat = \"{0}\"", fetchStat);
      DataRowCollection drc = GetResultsetForSelect (fetchStat.ToString (), keyStart); // get the data

      return (drc == null || drc.Count == 0) ? null : new PersistentClauseNode (drc, 0, this);
    }
Ejemplo n.º 17
0
        // process an element content, i.e. a list
        private static bool ContentTermToXml(XmlTextWriter xwr, Term list)
        {
            while (list != Term.NULLLIST) // traverse ...
              {
            Term e = list.Arg(0);
            string type = e.Functor;

            switch (type)
            {
              case "xmldecl":
            xwr.WriteStartDocument(true);
            break;
              case "element":
            if (!ElementTermToXml(xwr, e)) return false;
            break;
              case "text":
            xwr.WriteString(e.Arg(0).Functor);
            break;
              case "cdata":
            xwr.WriteCData(e.Arg(0).Functor);
            break;
              case "comment":
            xwr.WriteComment(e.Arg(0).Functor);
            break;
              case "instructions":
            xwr.WriteProcessingInstruction(e.Arg(0).Functor, e.Arg(1).ToString());
            break;
              default:
            PrologIO.Error("ContentTermToXml -- unrecognized type '{0}'", type);
            break;
            }
            list = list.Arg(1);
              }
              return true;
        }