Esempio n. 1
0
        static IEnumerable <bool> queens3(object UnplacedQs, object SafeQs, Variable Qs)
        {
            ListPair UnplacedQsListPair = YP.getValue(UnplacedQs) as ListPair;

            if (UnplacedQsListPair != null)
            {
                Variable UnplacedQs1 = new Variable();
                Variable Q           = new Variable();
                foreach (bool l1 in selectq(Q, UnplacedQsListPair, UnplacedQs1))
                {
                    if (!(SafeQs is ListPair && hasAttack((int)Q.getValue(), (ListPair)SafeQs)))
                    {
                        foreach (bool l2 in queens3(UnplacedQs1, new ListPair(Q, SafeQs), Qs))
                        {
                            yield return(false);
                        }
                    }
                }
            }
            else
            {
                foreach (bool l1 in Qs.unify(SafeQs))
                {
                    yield return(false);
                }
            }
        }
Esempio n. 2
0
 static IEnumerable <bool> squaredRectangle(object Width, object Height)
 {
     foreach (bool l1 in YP.unify(Width, Height))
     {
         yield return(false);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// If this Variable is bound, then just call YP.unify to unify this with arg.
 /// (Note that if arg is an unbound Variable, then YP.unify will bind it to
 /// this Variable's value.)
 /// Otherwise, bind this Variable to YP.getValue(arg) and yield once.  After the
 /// yield, return this Variable to the unbound state.
 /// For more details, see http://yieldprolog.sourceforge.net/tutorial1.html
 /// </summary>
 /// <param name="arg"></param>
 /// <returns></returns>
 public IEnumerable <bool> unify(object arg)
 {
     if (!_isBound)
     {
         _value = YP.getValue(arg);
         if (_value == this)
         {
             // We are unifying this unbound variable with itself, so leave it unbound.
             yield return(false);
         }
         else
         {
             _isBound = true;
             try
             {
                 yield return(false);
             }
             finally
             {
                 // Remove the binding.
                 _isBound = false;
             }
         }
     }
     else
     {
         foreach (bool l1 in YP.unify(this, arg))
         {
             yield return(false);
         }
     }
 }
Esempio n. 4
0
        static IEnumerable <bool> attack3(object X, object N, object Arg3)
        {
            Variable Y = new Variable();

            foreach (bool l in new ListPair(Y, new Variable()).unify(Arg3))
            {
                if ((int)YP.getValue(X) == (int)Y.getValue() + (int)YP.getValue(N))
                {
                    yield return(false);
                }
                if ((int)YP.getValue(X) == (int)Y.getValue() - (int)YP.getValue(N))
                {
                    yield return(false);
                }
            }
            Variable Ys = new Variable();
            Variable N1 = new Variable();

            foreach (bool l1 in new ListPair(new Variable(), Ys).unify(Arg3))
            {
                foreach (bool l2 in N1.unify((int)YP.getValue(N) + 1))
                {
                    foreach (bool l3 in attack3(X, N1, Ys))
                    {
                        yield return(false);
                    }
                }
            }
        }
Esempio n. 5
0
        // disable warning on l1, don't see how we can
        // code this differently
        #pragma warning disable 0168, 0219

        /// <summary>
        /// For each result, unify the _freeVariables and unify bagArrayVariable with the associated bag.
        /// </summary>
        /// <param name="bagArrayVariable">this is unified with the List<object> of matches for template that
        /// corresponds to the bindings for freeVariables.  Be very careful: this does not unify with a Prolog
        /// list.</param>
        /// <returns></returns>
        public IEnumerable <bool> resultArray(Variable bagArrayVariable)
        {
            if (_freeVariables == null)
            {
                // No unbound free variables, so we only filled one bag.  If empty, bagof fails.
                if (_findallBagArray.Count > 0)
                {
                    foreach (bool l1 in bagArrayVariable.unify(_findallBagArray))
                    {
                        yield return(false);
                    }
                }
            }
            else
            {
                foreach (KeyValuePair <object[], List <object> > valuesAndBag in _bagForFreeVariables)
                {
                    foreach (bool l1 in YP.unifyArrays(_freeVariables, valuesAndBag.Key))
                    {
                        foreach (bool l2 in bagArrayVariable.unify(valuesAndBag.Value))
                        {
                            yield return(false);
                        }
                    }
                    // Debug: Should we free memory of the answers already returned?
                }
            }
        }
Esempio n. 6
0
    /// <summary>
    /// Parse goalString and yield for each Goal, setting VariableList to a list of
    /// (variableAtom = Var).
    /// </summary>
    /// <param name="goalString"></param>
    /// <param name="Goal"></param>
    /// <param name="VariableList"></param>
    /// <returns></returns>
    static IEnumerable <bool> parseGoal(string goalString, object Goal, object VariableList)
    {
        // The parser requires a newline at the end.
        YP.see(new StringReader(goalString + "\n"));
        object TermList = new Variable();

        // parseInput set TermList to a list of f(Goal, VariableList).
        foreach (bool l1 in Parser.parseInput(TermList))
        {
            // Close the input now before yielding.
            YP.seen();
            // Iterate through each member of TermList.
            for (TermList = YP.getValue(TermList);
                 TermList is Functor2 && ((Functor2)TermList)._name == Atom.DOT;
                 TermList = YP.getValue(((Functor2)TermList)._arg2))
            {
                // Unify the head of the list with f(Goal, VariableList).
                foreach (bool l2 in YP.unify
                             (((Functor2)TermList)._arg1, new Functor2(Atom.F, Goal, VariableList)))
                {
                    yield return(false);
                }
            }
            yield break;
        }
        // Close the input in case parseInput failed.
        YP.seen();
    }
Esempio n. 7
0
    public Triangle(YP pA, YP pB, YP pC)
    {
        Action <YP> Aset = (a) => {
            A = a;
        };
        Action <YP, YP> BCset = (b, c) => {
            this.B = (b.Y < c.Y) ? b : c;
            this.C = (b.Y < c.Y) ? c : b;
        };

        Action <YP, YP, YP, Action <YP>, Action <YP, YP> >
        check = (a, b, c, f1, f2) => {
            Console.WriteLine($"A is A? {a}");
            if (a.X != b.X && b.X == c.X)
            {
                Console.WriteLine("YES A {a}");
                f1(a);
                f2(b, c);
            }
            else
            {
                Console.WriteLine("NO A ...");
            }
        };

        check(pA, pB, pC, Aset, BCset);
        check(pB, pC, pA, Aset, BCset);
        check(pC, pA, pB, Aset, BCset);
    }
Esempio n. 8
0
        private static string listPairToString(Functor2 listPair)
        {
            string result = "[";

            while (true)
            {
                object head = YP.getValue(listPair._arg1);
                object tail = YP.getValue(listPair._arg2);
                if (tail == (object)Atom.NIL)
                {
                    result += head;
                    break;
                }
                else if (tail is Functor2 && ((Functor2)tail)._name == Atom.DOT)
                {
                    result  += head + ", ";
                    listPair = (Functor2)tail;
                    // Loop again.
                }
                else
                {
                    // The list is not terminated with NIL.
                    result += head + "|" + tail;
                    break;
                }
            }
            result += "]";
            return(result);
        }
Esempio n. 9
0
        public void add()
        {
            if (_freeVariables == null)
            {
                // The goal has bound the values in _template but we don't bother with _freeVariables.
                _findallBagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
            }
            else
            {
                // The goal has bound the values in _template and _freeVariables.
                // Find the entry for this set of _freeVariables values.
                object[] freeVariableValues = new object[_freeVariables.Length];
                for (int i = 0; i < _freeVariables.Length; ++i)
                {
                    freeVariableValues[i] = YP.getValue(_freeVariables[i]);
                }
                List <object> bagArray;
                if (!_bagForFreeVariables.TryGetValue(freeVariableValues, out bagArray))
                {
                    bagArray = new List <object>();
                    _bagForFreeVariables[freeVariableValues] = bagArray;
                }

                // Now copy the template and add to the bag for the freeVariables values.
                bagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
            }
        }
Esempio n. 10
0
 /// If arg is another Functor2, then succeed (yield once) if this and arg have the
 /// same name and all functor args unify, otherwise fail (don't yield).
 /// If arg is a Variable, then call its unify to unify with this.
 /// Otherwise fail (don't yield).
 public IEnumerable <bool> unify(object arg)
 {
     arg = YP.getValue(arg);
     if (arg is Functor2)
     {
         Functor2 argFunctor = (Functor2)arg;
         if (_name.Equals(argFunctor._name))
         {
             foreach (bool l1 in YP.unify(_arg1, argFunctor._arg1))
             {
                 foreach (bool l2 in YP.unify(_arg2, argFunctor._arg2))
                 {
                     yield return(false);
                 }
             }
         }
     }
     else if (arg is Variable)
     {
         foreach (bool l1 in ((Variable)arg).unify(this))
         {
             yield return(false);
         }
     }
 }
Esempio n. 11
0
        public IEnumerable <bool> retract(object Head, object Body)
        {
            Head = YP.getValue(Head);
            if (Head is Variable)
            {
                throw new PrologException("instantiation_error", "Head is an unbound variable");
            }
            object[] arguments = YP.getFunctorArgs(Head);

            // We always match Head from _allAnswers, and the Body is Atom.a("true").
            #pragma warning disable 0168, 0219
            foreach (bool l1 in YP.unify(Body, Atom.a("true")))
            {
                // The caller can assert another answer into this same predicate during yield, so we have to
                //   make a copy of the answers.
                foreach (object[] answer in _allAnswers.ToArray())
                {
                    foreach (bool l2 in YP.unifyArrays(arguments, answer))
                    {
                        _allAnswers.Remove(answer);
                        clearIndexes();
                        yield return(false);
                    }
                }
            }
            #pragma warning restore 0168, 0219
        }
Esempio n. 12
0
    void RetrieveFacts()
    {
        //Debug.Log("# of observables: " + fow.observables.Count);

        /*Transform head = transform.Find("Head");
         * Color original = head.GetComponent<Renderer>().material.GetColor("_Color"); //for debugging/showcase purposes*/
        foreach (GameObject obj in fow.observables)
        {
            if (obj == null)
            {
                continue;
            }
            Observable obs = obj.GetComponent <Observable>();
            //Debug.Log("Observable " + obs);
            foreach (ObservableFact fact in obs.observableFacts)
            {
                YP.assertFact(info.agentId, fact.getLabel(), fact.getValues());
            }
            foreach (LocationClue clue in obs.locationClues)
            {
                //head.GetComponent<Renderer>().material.SetColor("_Color", Color.green); //for debugging/showcase purposes
                agent.solver.AddLocationClue(clue);
            }
            foreach (MurderClue clue in obs.murderClues)
            {
                transform.Find("Head").GetComponent <Renderer>().material.SetColor("_Color", Color.green);
                agent.solver.AddMurderClue(clue);
            }
        }
        //StartCoroutine(stopProcessingFacts(head, original, 0.5f)); //for debugging/showcase purposes
        fow.observables.Clear();
    }
Esempio n. 13
0
 public static IEnumerable <bool> undetected(object Obj)
 {
     {
         Variable Value = new Variable();
         Variable Min   = new Variable();
         Variable Max   = new Variable();
         foreach (bool l2 in recognition(Obj, Value))
         {
             foreach (bool l3 in confidence(Obj, Min, Max))
             {
                 if (YP.lessThan(Value, Min))
                 {
                     yield return(false);
                 }
             }
         }
     }
     {
         Variable Value = new Variable();
         Variable Min   = new Variable();
         Variable Max   = new Variable();
         foreach (bool l2 in recognition(Obj, Value))
         {
             foreach (bool l3 in confidence(Obj, Min, Max))
             {
                 if (YP.greaterThan(Value, Max))
                 {
                     yield return(false);
                 }
             }
         }
     }
 }
 public void addUniqueVariables(List <Variable> variableSet)
 {
     for (int i = 0; i < _args.Length; ++i)
     {
         YP.addUniqueVariables(_args[i], variableSet);
     }
 }
Esempio n. 15
0
    static void Main(string[] args)
    {
        Console.WriteLine("Yield Prolog Shell.  Enter ctrl-Z to exit.");

        while (true)
        {
            Console.Write("?- ");
            string goalString = Console.ReadLine();
            if (goalString == null)
            {
                break;
            }
            if (goalString == "")
            {
                Console.WriteLine("Enter ctrl-Z to exit.");
                continue;
            }

            Variable Goal         = new Variable();
            Variable VariableList = new Variable();
            foreach (bool l1 in parseGoal(goalString, Goal, VariableList))
            {
                try
                {
                    bool gotMatch = false;
                    foreach (bool l2 in YP.getIterator(Goal, null))
                    {
                        gotMatch = true;
                        if (YP.getValue(VariableList) != Atom.NIL)
                        {
                            // We are showing values, so allow the user to enter ";" to display the next match.
                            writeValues(VariableList);
                            if (!Console.ReadLine().StartsWith(";"))
                            {
                                break;
                            }
                        }
                    }

                    if (gotMatch)
                    {
                        if (YP.getValue(VariableList) == Atom.NIL)
                        {
                            // We didn't show any values.  So just write true after matching (one or more times).
                            Console.WriteLine("true");
                        }
                    }
                    else
                    {
                        Console.WriteLine("fail");
                    }
                }
                catch (PrologException exception)
                {
                    Console.WriteLine(exception._term.ToString());
                }
            }
        }
    }
        /// <summary>
        /// Unify Bag with the result. This frees the internal answers, so you can only call this once.
        /// </summary>
        /// <param name="Bag"></param>
        /// <returns></returns>
        public IEnumerable <bool> result(object Bag)
        {
            object result = ListPair.make(_bagArray);

            // Try to free the memory.
            _bagArray = null;
            return(YP.unify(Bag, result));
        }
Esempio n. 17
0
 public static IEnumerable <bool> usrcmd(object UserCmd, object SysCmd)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("usrcmd_"), new object[] { UserCmd, SysCmd }))
         {
             yield return(false);
         }
     }
 }
 /// <summary>
 /// Create a PrologException where the Term is error(ErrorTerm, Message).
 /// This uses YP.makeCopy to copy the ErrorTerm and Message so that they are valid after unbinding.
 /// </summary>
 /// <param name="ErrorTerm">the error term of the error</param>
 /// <param name="Messsage">the message term of the error.  If this is a string, it is converted to an
 /// Atom so it can be used by Prolog code.
 /// Message, converted to a string, is use as the printable exception message.
 /// </param>
 public PrologException(object ErrorTerm, object Message)
     : base(YP.getValue(Message).ToString())
 {
     if (Message is string)
     {
         Message = Atom.a((string)Message);
     }
     _term = YP.makeCopy(new Functor2(Atom.a("error"), ErrorTerm, Message), new Variable.CopyStore());
 }
Esempio n. 19
0
 public static IEnumerable <bool> cmddesc(object SysCmd, object Desc)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("cmddesc_"), new object[] { SysCmd, Desc }))
         {
             yield return(false);
         }
     }
 }
Esempio n. 20
0
 public static IEnumerable <bool> request(object Id, object UserCmd)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("request_"), new object[] { Id, UserCmd }))
         {
             yield return(false);
         }
     }
 }
Esempio n. 21
0
 public static IEnumerable <bool> objcmd(object Obj, object SysCmd)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("objcmd_"), new object[] { Obj, SysCmd }))
         {
             yield return(false);
         }
     }
 }
 /// <summary>
 /// Détruit tous les prédicats de nom et d'arité identique de la base de données de <see cref="YieldProlog"/>.
 /// A utiliser uniquement dans un nouveau <see cref="AppDomain"/>.
 /// </summary>
 public void YPretractAll()
 {
     object[] values = new object[arity];
     for (int i = 0; i < arity; i++)
     {
         values[i] = new Variable();
     }
     YP.retract(Functor.make(Atom.a(name), values));
 }
Esempio n. 23
0
 public static IEnumerable <bool> result(object SysCmd, object Out)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("result_"), new object[] { SysCmd, Out }))
         {
             yield return(false);
         }
     }
 }
Esempio n. 24
0
 public static IEnumerable <bool> confidence(object Obj, object Min, object Max)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("confidence_"), new object[] { Obj, Min, Max }))
         {
             yield return(false);
         }
     }
 }
Esempio n. 25
0
 public static IEnumerable <bool> recognition(object Obj, object Value)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("recognition_"), new object[] { Obj, Value }))
         {
             yield return(false);
         }
     }
 }
Esempio n. 26
0
 public static IEnumerable <bool> action(object Obj, object Status, object Cmd)
 {
     {
         foreach (bool l2 in YP.matchDynamic(Atom.a("action_"), new object[] { Obj, Status, Cmd }))
         {
             yield return(false);
         }
     }
 }
 public object makeCopy(Variable.CopyStore copyStore)
 {
     object[] argsCopy = new object[_args.Length];
     for (int i = 0; i < _args.Length; ++i)
     {
         argsCopy[i] = YP.makeCopy(_args[i], copyStore);
     }
     return(new Functor(_name, argsCopy));
 }
Esempio n. 28
0
 public bool termEqual(object term)
 {
     term = YP.getValue(term);
     if (term is Functor1)
     {
         Functor1 termFunctor = (Functor1)term;
         return(_name.Equals(termFunctor._name) && YP.termEqual(_arg1, termFunctor._arg1));
     }
     return(false);
 }
Esempio n. 29
0
        public bool lessThan(Functor1 functor)
        {
            // Do the equal check first since it is faster.
            if (!_name.Equals(functor._name))
            {
                return(_name.lessThan(functor._name));
            }

            return(YP.termLessThan(_arg1, functor._arg1));
        }
Esempio n. 30
0
    static IEnumerable <bool> makeList(object First, object Second, object List)
    {
        ListPair list1  = new ListPair(Second, Atom.NIL);
        ListPair result = new ListPair(First, list1);

        foreach (bool l1 in YP.unify(List, result))
        {
            yield return(false);
        }
    }