コード例 #1
0
ファイル: Substitution.cs プロジェクト: druzil/nggp-base
        public virtual Term GetMapping(TermVariable var)
        {
            Term result;

            if (Substitutions.TryGetValue(var, out result))
            {
                if (result is TermVariable)
                {
                    var sub = GetMapping((TermVariable)result);
                    if (sub != null)
                    {
                        Substitutions[var] = sub;
                        result = sub;
                    }
                }
                else
                {
                    var func = result as TermFunction;
                    if (func != null && func.HasVariables)
                        result = func.ApplySubstitution(this);
                }
            }

            return result;
        }
コード例 #2
0
ファイル: Substitution.cs プロジェクト: druzil/nggp-base
 public Term this[TermVariable variable]
 {
     get
     {
     return _contents[variable];
     }
     set
     {
     _contents[variable] = value;
     }
 }
コード例 #3
0
ファイル: Unifier.cs プロジェクト: druzil/nggp-base
        private static bool UnifyVariable(TermVariable var, Term x, Substitution theta)
        {
            if (theta.Contains(var))
                return UnifyTerm(theta[var], x, theta);

            TermVariable termVariable = x as TermVariable;
            if ((termVariable != null) && theta.Contains(termVariable))
                return UnifyTerm(var, theta[termVariable], theta);

            theta[var] = x;
            return true;
        }
コード例 #4
0
ファイル: Substitution.cs プロジェクト: druzil/nggp-base
 public bool Contains(TermVariable variable)
 {
     return _contents.ContainsKey(variable);
 }
コード例 #5
0
ファイル: TermFunction.cs プロジェクト: druzil/nggp-base
 protected override int CompareTo(TermVariable t)
 {
     // Obj < Func < Var
     return -1;
 }
コード例 #6
0
ファイル: TermFunction.cs プロジェクト: druzil/nggp-base
 /**
  * Checks whether the function contains references to the input variable.
  * Used for unification of a variable and a function ('occurs' check).
  *
  * @param variable The variable whose presence to check for.
  * @return True if <tt>variable</tt> appears anywhere within the function's arguments.
  */
 public bool HasVariable(TermVariable variable)
 {
     for (int i = 0; i < Arity; i++)
     {
         var termVariable = Arguments[i] as TermVariable;
         if (termVariable != null)
         {
             if (termVariable.Equals(variable))
                 return true;
         }
         else
         {
             var function = Arguments[i] as TermFunction;
             if (function != null && function.HasVariable(variable))
                 return true;
         }
     }
     return false;
 }
コード例 #7
0
ファイル: TermVariable.cs プロジェクト: druzil/nggp-base
 protected override int CompareTo(TermVariable t)
 {
     return Math.Sign(_varName - t._varName);
 }
コード例 #8
0
ファイル: VariableFact.cs プロジェクト: druzil/nggp-base
        /// <summary>
        /// Construct a variable fact from a GdlList. Note that the list <i>must</i>
        /// be a list of atoms, in other words, there cannot be any nested lists. The
        /// fact is constructed by taking the first element of the list as the fact's
        /// relation name, and every subsequent element as a column. If an atom is
        /// found to be a variable, then that column is marked as a variable.
        /// </summary>
        /// <param name="list">The list to build the fact from.</param>
        /// <returns>A variable fact representing the data from the list.</returns>
        public static Fact FromList(GdlList list)
        {
            int relName = ((GdlAtom)list[0]).GetToken();

            var terms = new Term[list.Arity];

            // Turn each element of the list into a term.
            // Make sure to turn same variables into the same term.

            bool vars = false;

            var varMap = new Dictionary<GdlVariable, TermVariable>();

            for (int i = 0; i < list.Arity; i++)
            {
                GdlExpression exp = list[i + 1];

                if ((exp is GdlVariable) == false)
                {
                    terms[i] = Term.BuildFromGdl(exp, varMap);

                    // Check to see if this term has variables in it.
                    // (But don't bother if we already know that we have variables.)
                    if (!vars && terms[i].HasVariables)
                        vars = true;
                }
                else
                {
                    var var = (GdlVariable)exp;
                    terms[i] = new TermVariable(var.GetToken());
                    vars = true;
                }

            }

            // Only return a variable fact if this actually has variables
            if (vars)
                return new VariableFact(true, relName, terms);
            return new GroundFact(relName, terms);
        }
コード例 #9
0
ファイル: Substituter.cs プロジェクト: druzil/nggp-base
        private static Term SubstituteVariable(TermVariable variable, Substitution theta)
        {
            if (!theta.Contains(variable))
                return variable;

            Term result = theta[variable];
            Term betterResult;

            while (!(betterResult = SubstituteTerm(result, theta)).Equals(result))
                result = betterResult;

            theta[variable] = result;
            return result;
        }
コード例 #10
0
ファイル: VariableRenamer.cs プロジェクト: druzil/nggp-base
        private TermVariable RenameVariable(TermVariable variable, IDictionary<TermVariable, TermVariable> renamings)
        {
            if (!renamings.ContainsKey(variable))
                //renamings[variable] = TermVariable.MakeTermVariable();
                renamings[variable] = new TermVariable(GameContainer.SymbolTable["?R" + _nextName++]);

            return renamings[variable];
        }
コード例 #11
0
ファイル: Substitution.cs プロジェクト: druzil/nggp-base
 public virtual void AddMapping(TermVariable from, Term to)
 {
     //TODO: is there a reason to clone?
     Substitutions[from] = to.Clone();
 }
コード例 #12
0
ファイル: Substitution.cs プロジェクト: druzil/nggp-base
 internal bool IsMappedTo(TermVariable a)
 {
     return Substitutions.ContainsValue(a);
 }