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; }
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; }