public TermNode(BaseTerm term, PredicateDescr predDescr, int level)
 {
     this.term = term;
     this.predDescr = predDescr;
     this.level = level;
 }
            // 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;
            }
Exemple #3
0
    private void FindUndefined(SortedList sd, PredicateDescr pd)
    {
#if persistent
      if (pd is PersistentPredDescr) return;
#endif

      ClauseNode clause = pd.GetClauseList(null, null);
      Term clauseHead;
      TermNode clauseTerm;

      while (clause != null) // iterate over all clauses of this predicate
      {
        clauseHead = clause.Term;
        clauseTerm = clause.NextNode;

        while (clauseTerm != null) // non-facts only. Iterate over all clauseTerm-terms t of this clause
        {
          if (clauseTerm.BuiltinId == BI.none && clauseTerm.PredDescr == null) sd[clauseTerm.Term.Index] = null;

          clauseTerm = clauseTerm.NextNode;
        }
        clause = clause.NextClause;
      }
      return;
    }
 public TermNode(BaseTerm term, PredicateDescr predDescr)
 {
     this.predDescr = predDescr;
     this.term = term;
 }
Exemple #5
0
    private void ResolveIndex(PredicateDescr pd)
    {
#if persistent
      if (pd is PersistentPredDescr) return; // does not have a clauseTerm
#endif

      ClauseNode clause = pd.GetClauseList(null, null);

      while (clause != null) // iterate over all clauses of this predicate. ClauseNode.Term contains predicate clauseHead
      {
        Term clauseHead = clause.Term; // clause = clauseHead :- clauseTerm*
        TermNode clauseTerm = clause.NextNode;

        while (clauseTerm != null) // non-facts only. Iterate over all clauseTerm-terms t of this clause
        {
          if (clauseTerm.BuiltinId == BI.none) clauseTerm.PredDescr = this[clauseTerm.Term.KbKey];
          // builtins (>=0) are handled differently (in Execute ())

          clauseTerm = clauseTerm.NextNode;
        }
        clause = clause.NextClause;
      }
      return;
    }
Exemple #6
0
    private bool ListClause(PredicateDescr pd, string functor, int arity, int seqno)
    {
      ClauseNode clause = null;
      string details;

#if persistent
      if (pd is PersistentPredDescr)
      {
        details = "persistent, details: " + pd.DefiningFile;
      }
      else
#endif
      {
        if ((clause = pd.GetClauseList(null, null)) == null) return false;

        details = "source: " + pd.DefiningFile;
      }

#if arg1index
      if (pd.IsFirstArgIndexed) details += "; arg1-indexed (jump points marked with '.')";
#endif

      Console.WriteLine("\n{0}/{1}: ({2}) {3}", functor, arity,
                         details,
                         ((seqno == 1) ? "" : ("(" + seqno.ToString() + ")")));

      while (clause != null)
      {
        TermNode next;

#if arg1index
        // prefix a clause that is pointed to by first-argument indexing with '.'
        Console.Write (" {0}{1}", (pd.IsFirstArgMarked (clause))?".":" ", clause.Term);
#else
        Console.Write("  {0}", clause.Term);
#endif

        if ((next = clause.NextNode) != null)
        {
          BI builtinId = next.BuiltinId;
          Console.Write(" :-\n{0}", (builtinId == BI.none) ? next.ToString() : builtinId.ToString());
        }
        Console.WriteLine(".");
        clause = clause.NextClause;
      }

      return true;
    }
Exemple #7
0
    private PredicateDescr SetClauseList(PredEnum predType, string f, int a, ClauseNode c)
    {
      string key = Term.Key(f, a);
      PredicateDescr pd = this[key];

      if (pd == null)
      {
        switch (predType)
        {
#if persistent
          case PredEnum.table:
            this [key] = pd = new TablePredDescr (Globals.ConsultModuleName, Globals.ConsultFileName, f, a, c);
            break;
          case PredEnum.selproc:
            this [key] = pd = new ProcedurePredDescr (Globals.ConsultModuleName, Globals.ConsultFileName, f, a, c, true);
            break;
          case PredEnum.execproc:
            this [key] = pd = new ProcedurePredDescr (Globals.ConsultModuleName, Globals.ConsultFileName, f, a, c, false);
            break;
#endif
          default:
          	if (f.Contains("::"))
            {
            	String[] parts = Regex.Split(f, "::");
                double prob = Double.Parse(parts[0], Globals.CI);
                long timestamp = 0L;
                string clauseText = "";
                if (parts.Length == 3)
                {
                    if ("T".Equals(parts[1]))
                    {
                        timestamp = DateTime.Now.Ticks;
                    }
                    else
                    {
                        timestamp = DateTime.Now.Ticks + long.Parse(parts[1]) *10000000;
                    }
                    clauseText = parts[2];
                }
                else
                {
                    clauseText = parts[1];
                }
                Parser np = new Parser(null);
                np.StreamIn = clauseText + ".";
                Term goalTerm = np.QueryNode.Term;
                key = Term.Key(goalTerm.Functor, goalTerm.Arity);
                ClauseNode cn = new ClauseNode(goalTerm, null, prob, timestamp);
                if (this[key] == null)
                {
                    this[key] = pd = new PredicateDescr(Globals.ConsultModuleName, Globals.ConsultFileName, goalTerm.Functor, goalTerm.Arity, cn);
                }
                else
                {
                    pd = this[key];
                    pd.AppendToClauseList(cn);
                }
            }
            else
            {
                this[key] = pd = new PredicateDescr(Globals.ConsultModuleName, Globals.ConsultFileName, f, a, c);
            }
            break;
        }
      }
      else
        pd.SetClauseListHead(c);

      pd.AdjustClauseListEnd();

      return pd;
    }
Exemple #8
0
 public PersistentClauseNode(DataRowCollection drc, int clauseNo, PredicateDescr pd)
     : base(null, null)
 {
     dataRowCollection = drc;
       rowNo      = clauseNo;
       rowNoMax   = drc.Count-1;
       nextClause = null;
       term   = DataRowAsTerm (rowNo);
       predDescr  = pd;
 }
Exemple #9
0
        // whereTerm != null : for persistent predicates only
        // put the predicate definition (if found) into the TermNode if it is not already there
        public bool FindPredicateDefinition(PredicateStorage predicateStorage, Term whereTerm)
        {
            if (predDescr == null) predDescr = predicateStorage[term.KbKey];

              if (predDescr == null) return false;

            #if arg1index
              Term arg;

              if (predDescr.IsFirstArgIndexed)
              {
            if ((arg = term.Arg (0)).IsVar)
              nextClause = predDescr.FirstArgVarClause ();
            else // not a variable
            {
              nextClause = predDescr.FirstArgNonvarClause (arg.Functor);
              // 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; // "fail."
            }

            if (nextClause == null)
              nextClause = predDescr.GetClauseList (term, whereTerm); // GetClauseList: in PredicateStorage
              }
              else // not indexed
            #endif
              nextClause = predDescr.GetClauseList(term, whereTerm); // GetClauseList: in PredicateStorage

              return true;
        }