CopyInstantiation() public static method

Recopy the term to replace bound variables with their values and alpha convert any unbound variables. This has the effect of removing interference between this term and the copy should one or the other have its bindings changed (either through unification or backtracking).
public static CopyInstantiation ( object term ) : object
term object
return object
Ejemplo n.º 1
0
            public override bool MoveNext()
            {
                retry:

                // First, try the next child of the current parent.
                if (childIndex >= 0)
                {
                    Current = parentEnumerator.Current.Children[childIndex--];
                    this.variable.Value = Term.CopyInstantiation(Current.Key);
                    return true;
                }

                // Ran out of children on the current parent.
                if (parentEnumerator.MoveNext())
                {
                    // ReSharper disable once PossibleNullReferenceException
                    if (parentEnumerator.Current.IsExclusive)
                        throw new ELNodeExclusionException(
                            "Non-exclusive query of an exclusive node",
                            parentEnumerator.Current,
                            this.variable);

                    childIndex = parentEnumerator.Current.Children.Count - 1;

                    goto retry;
                }
                this.variable.ForciblyUnbind();
                return false;
            }
Ejemplo n.º 2
0
        public IEnumerable <CutState> Retract(Structure head, object body)
        {
            var  entries = GetEntriesListForUpdate();
            bool gotOne  = true;

            while (gotOne && entries.Count > 0)
            {
                gotOne = false;
                for (int i = 0; !gotOne && i < entries.Count; i++)
                {
                    var entry = entries[i];
                    // Have to recopy the rule just in case it's being used in a pending subgoal.
                    // If it is, then the subgoal will see a modified version of the rule.
                    var rule = (Structure)Term.CopyInstantiation(new Structure(Symbol.Implication, entry.Head, entry.Body));
#pragma warning disable 168
                    // ReSharper disable UnusedVariable
                    foreach (var ignore1 in Term.Unify(head, rule.Argument(0)))
                    {
                        foreach (var ignore2 in Term.Unify(body, rule.Argument(1)))
                        // ReSharper restore UnusedVariable
#pragma warning restore 168
                        {
                            gotOne = true;
                            entries.RemoveAt(i);
                            yield return(CutState.Continue);

                            entries = GetEntriesListForUpdate();
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private object TermExpansion(object unexpanded)
        {
            if (CheckForPredicateInfoInThisKB(term_expansion, 2) == null && Global.CheckForPredicateInfoInThisKB(term_expansion, 2) == null)
                // Don't bother if not defined.
                return unexpanded;

            // Attempt to expand it
            var expanded = new LogicVariable(expansion);
            // Try this KB
            if (CheckForPredicateInfoInThisKB(term_expansion, 2) != null)
                // ReSharper disable UnusedVariable
#pragma warning disable 0168
                foreach (var ignore in Prove(new Structure(term_expansion, unexpanded, expanded)))
#pragma warning restore 0168
                {
                    return Term.CopyInstantiation(expanded);
                }

            // Try the global KB
            if (this != Global && Global.CheckForPredicateInfoInThisKB(term_expansion, 2) != null)
#pragma warning disable 0168
                foreach (var ignore in Global.Prove(new Structure(term_expansion, unexpanded, expanded)))
#pragma warning restore 0168
                // ReSharper restore UnusedVariable
                {
                    return Term.CopyInstantiation(expanded);
                }


            // Expansion failed, so use unexpanded version.
            return unexpanded;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// True if the specified goal is provable within this KnowledgeBase.
 /// </summary>
 /// <param name="result">Value of variable to return</param>
 /// <param name="goal">Goal to attempt to prove</param>
 /// <param name="throwOnFailure">If true, SolveFor will throw a GoalException if the goal fails</param>
 /// <param name="thisValue">Value to give ot the indexical $this during execution</param>
 /// <returns>Success</returns>
 public object SolveFor(LogicVariable result, object goal, object thisValue, bool throwOnFailure = true)
 {
     if (this.IsTrue(Term.Structurify(goal, "Argument to SolveFor() should be a valid Prolog goal."), thisValue))
         return Term.CopyInstantiation(result);
     if (throwOnFailure)
         throw new GoalException(goal, "Goal is unsatisfiable");
     return null;
 }
Ejemplo n.º 5
0
 public override bool MoveNext()
 {
     if (this.childIndex < parentNode.Children.Count)
     {
         Current = parentNode.Children[this.childIndex++];
         this.variable.Value = Term.CopyInstantiation(Current.Key);
         return true;
     }
     this.variable.ForciblyUnbind();
     return false;
 }
Ejemplo n.º 6
0
 public static ELNode UpdateStructure(Structure term, KnowledgeBase knowledgeBase)
 {
     if (term.Functor == Symbol.Slash)
     {
         if (term.Arity == 1)
             return knowledgeBase.ELRoot.StoreNonExclusive(Term.CopyInstantiation(term.Argument(0)));
         return Update(term.Argument(0), knowledgeBase).StoreNonExclusive(Term.CopyInstantiation(term.Argument(1)));
     }
     if (term.Functor == Symbol.Colon)
     {
         return Update(term.Argument(0), knowledgeBase).StoreExclusive(Term.CopyInstantiation(term.Argument(1)), true); 
     }
     throw new Exception("Malformed EL assertion: "+ISOPrologWriter.WriteToString(term));
 }
Ejemplo n.º 7
0
 public override bool MoveNext()
 {
     if (this.variable.IsBound)
     {
         // We've already been through it once, so unbind the variable and fail.
         this.variable.ForciblyUnbind();
         return false;
     }
     
     // This is our first time through, so bind the variable and succeed.
     Current = child;
     this.variable.Value = Term.CopyInstantiation(child.Key);
     return true;
 }
Ejemplo n.º 8
0
 public override bool MoveNext()
 {
     while (parentEnumerator.MoveNext())
     {
         if (parentEnumerator.Current.IsNonExclusive)
         {
             throw new ELNodeExclusionException("Exclusive query of an non-exclusive node", parentEnumerator.Current, this.variable);
         }
         if (parentEnumerator.Current.Children.Count > 0)
         {
             Current = parentEnumerator.Current.Children[0];
             this.variable.Value = Term.CopyInstantiation(Current.Key);
             return true;
         }
     }
     this.variable.ForciblyUnbind();
     return false;
 }
Ejemplo n.º 9
0
        public IEnumerable <CutState> FindClauses(Structure head, object body)
        {
            foreach (var entry in Entries)
            {
                var rule =
                    (Structure)Term.CopyInstantiation(new Structure(Symbol.Implication, entry.Head, entry.Body));
#pragma warning disable 414, 168, 219
                // ReSharper disable UnusedVariable
                foreach (var ignore1 in Term.Unify(rule.Argument(0), head))
                {
                    foreach (var ignroe2 in Term.Unify(rule.Argument(1), body))
#pragma warning restore 414, 168, 219
                    {
                        // ReSharper restore UnusedVariable
                        yield return(CutState.Continue);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        internal static IEnumerable <CutState> SetImplementation(object[] args, PrologContext context)
        {
            if (args.Length != 2)
            {
                throw new ArgumentCountException("set", args, new object[] { "Variable", "NewValue" });
            }

            object value = Term.CopyInstantiation(args[1]);

            if (value is LogicVariable)
            {
                throw new UninstantiatedVariableException((LogicVariable)args[1], "Value argument should be a data object, not an uninstantiated (unbound) variable.");
            }
            var functor = Term.Deref(args[0]) as Symbol;

            if (functor == null)
            {
                throw new ArgumentTypeException("set", "functor", args[0], typeof(Symbol));
            }

            List <KnowledgeBaseEntry> entries = context.KnowledgeBase.EntryListForStoring(new PredicateIndicator(functor, 1));

            switch (entries.Count)
            {
            case 0:
                entries.Add(new KnowledgeBaseVariable(value));
                return(CutStateSequencer.Succeed());

            case 1:
                var v = entries[0] as KnowledgeBaseVariable;
                if (v == null)
                {
                    throw new ArgumentException("Functor is not a variable; it has another entry defined for it.");
                }
                v.CurrentValue = value;
                return(CutStateSequencer.Succeed());

            default:
                throw new ArgumentException("Functor is not a variable; it has multiple entries defined for it.");
            }
        }