Esempio n. 1
0
        public bool PrologQuery(String query, PlTermV parameters)
        {
            PlQuery q = new PlQuery(query, parameters);
            bool    x = q.NextSolution();

            return(x);
        }
Esempio n. 2
0
        public static int ToFieldLayout(string named, string arg1, object o, Type t, PlTerm term, bool childs, bool addNames)
        {
            MemberInfo[] tGetFields = GetStructFormat(t);

            int     len = tGetFields.Length;
            PlTermV tv  = NewPlTermV(len + 1);

            tv[0].UnifyAtom(arg1);
            int tvi = 1;

            for (int i = 0; i < len; i++)
            {
                object v = GetMemberValue(tGetFields[i], o);
                if (v is IList)
                {
                    v.GetType();
                }
                tv[tvi++].FromObject((v));
            }
            if (true)
            {
                return(PlSucceedOrFail(term.Unify(PlC(named, tv))));
            }
            uint termTermRef = term.TermRef;

            uint temp = libpl.PL_new_term_ref();

            libpl.PL_cons_functor_v(temp,
                                    libpl.PL_new_functor(libpl.PL_new_atom(named), tv.Size),
                                    tv.A0);
            return(libpl.PL_unify(termTermRef, temp));
        }
Esempio n. 3
0
 /// <summary>
 /// Sets the enumerator to its initial position, which is before the first element in the collection.
 /// </summary>
 /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.
 ///                 </exception><filterpriority>2</filterpriority>
 public void Reset()
 {
     Dispose();
     //fframe = libpl.PL_open_foreign_frame();
     termV   = new PlTermV(2);
     plQuery = new PlQuery(_dictionary._module, _dictionary._getvalue, termV);
 }
Esempio n. 4
0
        //operator char *(void);
        /// <inheritdoc />
        /// <summary>
        /// The exception is translated into a message as produced by print_message/2. The character data is stored in a ring.
        /// </summary>
        /// <returns>A textual description of the Exception</returns>
        override public string ToString()
        {
            if (!PlEngine.IsInitialized)
            {
                return("A PlException was thrown but it can't formatted because PlEngine is not Initialized.");
            }

            string strRet = "[ERROR: Failed to generate message.  Internal error]\n";

            using (new PlFrame())
            {
#if USE_PRINT_MESSAGE
                PlTermV av = new PlTermV(2);

                av[0] = PlTerm.PlCompound("print_message", new PlTermV(new PlTerm("error"), new PlTerm(_exTerm.TermRef)));
                PlQuery q = new PlQuery("$write_on_string", av);
                if (q.NextSolution())
                {
                    strRet = (string)av[1];
                }
                q.Free();
#else
                var av = new PlTermV(2);
                av[0] = new PlTerm(_exTerm.TermRef);
                using (var q = new PlQuery("$messages", "message_to_string", av))
                {
                    if (q.NextSolution())
                    {
                        strRet = av[1].ToString();
                    }
                }
#endif
            }
            return(strRet);
        }
Esempio n. 5
0
        public void PlTerm_PlCompound_termV()
        {
            var tv = new PlTermV(new PlTerm("a"), new PlTerm("b"), new PlTerm("c"));
            var t  = PlTerm.PlCompound("foo", tv);

            Assert.AreEqual("foo(a,b,c)", t.ToString());
        }
Esempio n. 6
0
        public void UnifyListTuple()
        {
            var list = new PlTerm("[1, (a, b), 2, (c, d)]");
            int idx  = 0;

            foreach (var t in list)
            {
                //Console.WriteLine(t.ToString());	// testausgabe
                if (t.IsCompound && t.Arity == 2)
                {
                    var v = new PlTermV(2);
                    var c = PlTerm.PlCompound(",", v);
                    t.Unify(c);
                    if (idx == 0)
                    {
                        Assert.AreEqual("a", v[0].ToString(), "a");
                        Assert.AreEqual("b", v[1].ToString(), "b");
                    }
                    else if (idx == 1)
                    {
                        Assert.AreEqual("c", v[0].ToString(), "c");
                        Assert.AreEqual("d", v[1].ToString(), "d");
                    }
                    idx++;
                }
            }
        }
        /// <summary>
        /// Wygenerowanie pierwszego rozwiązania.
        /// </summary>
        private void start_button_Click(object sender, EventArgs e)
        {
            try
            {
                String sciezka = @"..\..\..\alloc.pl";
                if (!File.Exists(sciezka))
                {
                    throw new FileNotFoundException("Brak pliku: " + sciezka);
                }

                String crew        = prepareCrewPrologList();
                String flightsReqs = prepareFlightsRequirements(); //DefaultData.prepareDefaultRequirements();

                if (!PlEngine.IsInitialized)
                {
                    String[] param = { "-f", sciezka };
                    PlEngine.Initialize(param);
                }

                prologParams    = new PlTermV(3);
                prologQuery     = new PlQuery("matchTeams", prologParams);
                prologParams[1] = new PlTerm(flightsReqs);
                prologParams[2] = new PlTerm(crew);

                generateResultAndShow();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Wyjatek:\n\n" + ex.Message);
            }
        }
        protected int list_length(PlTerm list)
        {
            var listLen = PlTerm.PlVar();
            var args    = new PlTermV(list, listLen);

            Assert.IsTrue(PlQuery.PlCall("length", args));
            return((int)args[1]);
        }
Esempio n. 9
0
 public override bool Close(PlTermV a0)
 {
     if (MissingImpl())
     {
         return(false);
     }
     return(ManagedObject.Close(a0));
 }
Esempio n. 10
0
 /// <summary>
 /// Sets the enumerator to its initial position, which is before the first element in the collection.
 /// </summary>
 /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.
 ///                 </exception><filterpriority>2</filterpriority>
 public void Reset()
 {
     Dispose();
     fframe  = libpl.PL_open_foreign_frame();
     termV   = new PlTermV(1);
     plQuery = new PlQuery(_dictionary._module, _dictionary._querypred, termV);
     nonLeft = false;
 }
Esempio n. 11
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlQuery(string,PlTermV)" />
        /// <summary>locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public PlQuery(string module, string name, PlTermV termV)
        {
            //if (null == termV)
            //    throw new ArgumentNullException("termV");

            _av     = termV;
            _name   = name;
            _module = module;
        }
Esempio n. 12
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlCall(string, PlTermV)" />
        /// <summary>As <see cref="PlCall(string, PlTermV)"/> but locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public static bool PlCall(string module, string predicate, PlTermV args)
        {
            bool    bRet = false;
            PlQuery q    = new PlQuery(module, predicate, args);

            bRet = q.NextSolution();
            q.Free(false);
            return(bRet);
        }
Esempio n. 13
0
 internal static Term[] ToJPL(PlTermV args)
 {
     int UPPER = args.Size;
     Term[] target = new Term[UPPER];
     for (int i = 0; i < UPPER; i++)
     {
         target[i] = ToJPL(args[i]);
     }
     return target;
 }
Esempio n. 14
0
        private static PlTermV ToPLCSV(PlTerm[] terms)
        {
            int     size   = terms.Length;
            PlTermV target = NewPlTermV(size);

            for (int i = 0; i < size; i++)
            {
                target[i] = terms[i];
            }
            return(target);
        }
Esempio n. 15
0
        internal static Term[] ToJPL(PlTermV args)
        {
            int UPPER = args.Size;

            Term[] target = new Term[UPPER];
            for (int i = 0; i < UPPER; i++)
            {
                target[i] = ToJPL(args[i]);
            }
            return(target);
        }
Esempio n. 16
0
        public void queryNextSolutionLoop_1()
        {
            var tv = new PlTermV(new PlTerm("A"), new PlTerm("[a,b,c]"));
            var q  = new PlQuery("member", tv);
            int i  = 0;

            while (q.NextSolution())
            {
                Assert.AreEqual(_abc[i++], q.Args[0].ToString());
            }
        }
        public void t_in_out()
        {
            Delegate replaceDelegate = new DelegateParameter2(atom_replace);

            PlEngine.RegisterForeign(replaceDelegate);
            for (int i = 1; i < 10; i++)
            {
                var arg = new PlTermV(new PlTerm("test_f"), PlTerm.PlVar());
                PlQuery.PlCall("atom_replace", arg);
                Assert.AreEqual("test_xx_f", arg[1].ToString(), "atom_replace failed!");
            }
        }
Esempio n. 18
0
        public virtual int Call0(ContextHandle handle, PlTermV termV)
        {
            bool res  = handle.Call(termV);
            bool more = handle.HasMore();

            if (more)
            {
                libpl.PL_retry(handle.Handle);
                return(res ? 3 : 0);
            }
            return(res ? 1 : 0);
        }
Esempio n. 19
0
 void AssertWord(string word)
 {
     using (PlFrame fr = new PlFrame())
     {
         PlTermV av = new PlTermV(1);
         av[0] = PlTerm.PlCompound("word", new PlTermV(new PlTerm(word)));
         using (PlQuery q = new PlQuery("assert", av))
         {
             q.NextSolution();
         }
     }
 }
Esempio n. 20
0
        void AssertWord2(string word)
        {
            PlFrame fr = new PlFrame();
            PlTermV av = new PlTermV(1);

            av[0] = PlTerm.PlCompound("word", new PlTermV(new PlTerm(word)));
            PlQuery q = new PlQuery("assert", av);

            q.NextSolution();
            q.Dispose();   // IMPORTANT ! never forget to free the query before the PlFrame is closed
            fr.Dispose();
        }
Esempio n. 21
0
        // funkcja dzieki ktorej stawiamy pionek
        public bool spacingPawns(int index, List <int> listOfPawnsOpponent, List <OvalShapeExtended> listOfOvalShapes, Image textureOfPawn)
        {
            #region Konwersje list pionow do stringow
            String listPawns         = convertListToString(listOfPawns);
            String listOpponentPawns = convertListToString(listOfPawnsOpponent);


            #endregion

            PlTermV para = new PlTermV(4);
            para[0] = new PlTerm(index);
            para[1] = new PlTerm(listPawns);
            para[3] = new PlTerm(listOpponentPawns);


            PlQuery q = new PlQuery("dodaj_pion", para);
            q.NextSolution();



            if (para[1] == para[2])
            {
                return(false);
            }
            else
            {
                var list = para[2].ToString().Replace("[", "").Replace("]", "").Split(',');
                listOfPawns.Clear();
                foreach (var x in list)
                {
                    listOfPawns.Add(Convert.ToInt32(x));
                }

                foreach (var x in list)
                {
                    listOfOvalShapes[Convert.ToInt32(x)].BackgroundImage = textureOfPawn;
                }



                bool win;
                q   = new PlQuery(string.Format("wygrywajace({0})", para[2]));
                win = q.NextSolution();

                if (win)
                {
                    playerWin = true;
                }

                return(true);
            }
        }
Esempio n. 22
0
        public void queryNextSolutionLoop_arg_term_codelist()
        {
            var av = new PlTermV(2);

            av[1] = PlTerm.PlCodeList("abc");
            var q = new PlQuery("member", av);
            int i = 0;

            while (q.NextSolution())
            {
                Assert.AreEqual(_abc[i++][0], (int)av[0]);
            }
        }
Esempio n. 23
0
        private static PlTermV ToPLCSV1(PlTerm a0, PlTerm[] terms)
        {
            int     size   = terms.Length;
            PlTermV target = NewPlTermV(size + 1);
            int     to     = 1;

            target[0] = a0;
            for (int i = 0; i < size; i++)
            {
                target[to++] = terms[i];
            }
            return(target);
        }
Esempio n. 24
0
        private static string ToCSString(PlTermV termV)
        {
            int s = termV.Size;

            //var a0= termV.A0;
            PlTerm v0 = termV[0];
            PlTerm v1 = termV[1];

            PlQuery.PlCall("write", new PlTermV(v0));
            PlQuery.PlCall("nl");
            PlQuery.PlCall("writeq", new PlTermV(v1));
            PlQuery.PlCall("nl");
            return("");
        }
Esempio n. 25
0
        public void queryNextSolutionLoop_arg_term_StringList()
        {
            var av = new PlTermV(2);

            av[1] = new PlTerm("[a,b,c]");

            var q = new PlQuery("member", av);
            int i = 0;

            while (q.NextSolution())
            {
                Assert.AreEqual(_abc[i++], av[0].ToString());
            }
        }
Esempio n. 26
0
        public static int FooThree(PlTerm a0, int arity, IntPtr control)
        {
            var handle = control;
            FRG fc     = (FRG)(libpl.PL_foreign_control(control));

            switch (fc)
            {
            case FRG.PL_FIRST_CALL:
            {
                var  v    = NondetContextHandle.ObtainHandle(control);
                var  tv   = new PlTermV(a0, arity);
                bool res  = v.Setup(tv);
                bool more = v.HasMore();
                if (more)
                {
                    libpl.PL_retry(v.Handle);
                    return(res ? 3 : 0);
                }
                return(res ? 1 : 0);
            } break;

            case FRG.PL_REDO:
            {
                var  v    = NondetContextHandle.FindHandle(control);
                bool res  = v.Call(new PlTermV(a0, arity));
                bool more = v.HasMore();
                if (more)
                {
                    libpl.PL_retry(v.Handle);
                    return(res ? 3 : 0);
                }
                return(res ? 1 : 0);
            } break;

            case FRG.PL_CUTTED:
            {
                var  v   = NondetContextHandle.FindHandle(control);
                bool res = v.Close(new PlTermV(a0, arity));
                NondetContextHandle.ReleaseHandle(v);
                return(res ? 1 : 0);
            } break;

            default:
            {
                throw new PlException("no frg");
                return(libpl.PL_fail);
            }
            break;
            }
        }
Esempio n. 27
0
        public static bool DoStuff(PlTermV term1)
        {
            int    arity  = term1.Size;
            PlTerm term_X = term1[arity - 2];
            PlTerm term_Y = term1[arity - 1];

            for (int i = 0; i < arity - 2; i++)
            {
                Console.WriteLine(i.ToString() + ' ' + term1[i]);
            }

            term_X.Unify(new PlTerm(12));
            term_Y.Unify(new PlTerm(15));
            return(true);
        }
        public static bool my_concat_atom(PlTermV term1)
        {
            System.Diagnostics.Debug.Print("my_concat_atom called with term - {0}", term1.ToString());

            int    arity   = term1.Size;
            string sRet    = "";
            PlTerm termOut = term1[arity - 1];

            for (int i = 0; i < arity - 1; i++)
            {
                sRet += term1[i].ToString();
            }
            termOut.Unify(sRet);
            return(true);
        }
Esempio n. 29
0
 public static PlTerm[] ToTermArray(IEnumerable <PlTerm> enumerable)
 {
     if (enumerable is PlTerm[])
     {
         return((PlTerm[])enumerable);
     }
     if (enumerable is PlTermV)
     {
         PlTermV tv = (PlTermV)enumerable;
         return(tv.ToArray());
     }
     if (enumerable is PlTerm)
     {
         // I guess IsList makes a copy
         PlTerm tlist = (PlTerm)enumerable;
         if (tlist.IsVar)
         {
             return new PlTerm[] { tlist }
         }
         ;
         if (tlist.IsList)
         {
             enumerable = tlist.Copy();
         }
         if (tlist.Name == "{}")
         {
             var t     = tlist.Arg(0);
             var terms = new List <PlTerm>();
             while (t.Arity == 2)
             {
                 terms.Add(t.Arg(0));
                 t = t.Arg(1);
             }
             // last Item
             terms.Add(t);
             return(terms.ToArray());
         }
         if (tlist.IsAtomic)
         {
             if (tlist.IsAtom && tlist.Name == "[]")
             {
                 return(new PlTerm[0]);
             }
             return(new PlTerm[] { tlist });
         }
     }
     return(enumerable.ToArray());
 }
Esempio n. 30
0
        private int getPointsManchePlayer(PlTerm player)
        {
            int result;

            using (PlFrame fr = new PlFrame())
            {
                PlTerm  nbPoint = new PlTerm("NbPoint");
                PlTermV terms   = new PlTermV(player, nbPoint);
                using (PlQuery query = new PlQuery("pointManchePlayer", terms))
                {
                    string nbPari = query.Solutions.First()[1].ToString();
                    result = int.Parse(nbPari);
                }
            }
            return(result);
        }
Esempio n. 31
0
        //operator char *(void);
        /// <inheritdoc />
        /// <summary>
        /// The exception is translated into a message as produced by print_message/2. The character data is stored in a ring.
        /// </summary>
        /// <returns></returns>
        override public string ToString()
        {
            if (_messagePl != null)
            {
                return(GetType() + ": " + _messagePl);
            }

            if (!PlEngine.IsInitialized)
            {
                return("A PlException was thrown but it can't formatted because PlEngine is not Initialized.");
            }

            string strRet = "[ERROR: Failed to generate message.  Internal error]\n";

            if (libpl.NoToString)
            {
                return("[ERROR: Failed to generate message.  NoToString presently]\n");
            }
            using (PlFrame fr = new PlFrame())
            {
#if USE_PRINT_MESSAGE
                PlTermV av = new PlTermV(2);

                av[0] = PlTerm.PlCompound("print_message", new PlTermV(new PlTerm("error"), new PlTerm(_exTerm.TermRef)));
                PlQuery q = new PlQuery("$write_on_string", av);
                if (q.NextSolution())
                {
                    strRet = (string)av[1];
                }
                q.Free();
#else
                PlTermV av = new PlTermV(2);
                av[0] = new PlTerm(_exTerm.TermRef);
                PlQuery q = new PlQuery("$messages", "message_to_string", av);
                if (q.NextSolution())
                {
                    strRet = av[1].ToString();
                }
                //q.Free();
                return(strRet);

                q.Dispose();
#endif
            }
            return(strRet);
        }
Esempio n. 32
0
 public override bool Close(PlTermV a0)
 {
     if (MissingImpl()) return false;
     return ManagedObject.Close(a0);
 }
Esempio n. 33
0
 public override bool Setup(PlTermV a0)
 {
     if (MissingImpl()) return false;
     return ManagedObject.Setup(a0);
 }
Esempio n. 34
0
        public static int PL_chars_to_term(string chars, uint term)
        {
            PlTermV termV = new PlTermV(2);
            PL_unify(termV[0].TermRef, term);
            PL_put_atom(termV[0].TermRef, PL_new_atom(chars));
            return PrologCLR.PlCall(null, "term_to_atom", termV)?1:0;
            return SafeNativeMethods.PL_chars_to_term(chars, term);
		}
Esempio n. 35
0
 public virtual int Call0(ContextHandle handle, PlTermV termV)
 {
     bool res = handle.Call(termV);
     bool more = handle.HasMore();
     if (more)
     {
         libpl.PL_retry(handle.Handle);
         return res ? 3 : 0;
     }
     return res ? 1 : 0;
 }
Esempio n. 36
0
 public virtual NDCCH New(ContextHandle handle, PlTermV a0)
 {
     handle.Setup(a0);
     return (NDCCH)handle.ManagedObject;
 }
Esempio n. 37
0
        private static string ToCSString(PlTermV termV)
        {
            int s = termV.Size;

            //var a0= termV.A0;
            PlTerm v0 = termV[0];
            PlTerm v1 = termV[1];
            PlQuery.PlCall("write", new PlTermV(v0));
            PlQuery.PlCall("nl");
            PlQuery.PlCall("writeq", new PlTermV(v1));
            PlQuery.PlCall("nl");
            return "";
        }
Esempio n. 38
0
 internal static jpl.fli.term_t ToFLI(PlTermV args)
 {
     return ToFLI(args.A0);
 }
Esempio n. 39
0
 public static bool DoQuery(string m, string f, PlTermV args)
 {
     if (JplDisabled) return PlCall(m, f, args);
     Query q;
     try
     {
         q = new Query(InModule(m, new Compound(f, ToJPL(args))));
     }
     catch (Exception e)
     {
         WriteException(e);
         return false;
     }
     return DoQuery(q);
 }
Esempio n. 40
0
 public abstract bool Close(PlTermV a0);
Esempio n. 41
0
 public virtual bool Close(ContextHandle handle, PlTermV a0)
 {
     bool res = handle.Close(a0);
     return res;
 }
Esempio n. 42
0
        public static bool PlCall(string m, string f, PlTermV args)
        {
            try
            {
                if (!JplDisabled)
                {
#if USE_IKVM
                    return DoQuery(m, f, args);
#endif
                }
                if (PlCsDisabled)
                {
                    WriteDebug("Disabled PlCall " + f);
                    return false;
                }
                return PlQuery.PlCall(m, f, args);
            }
            catch (Exception e)
            {
                WriteException(e);
                throw e;
            }
        }
Esempio n. 43
0
 public override bool Setup(PlTermV a0)
 {
     return Call(a0);
 }
Esempio n. 44
0
        public override bool Call(PlTermV a0)
        {
            bool success = false;
            try
            {
                for (int i = 0; i < a0.Size; i++)
                {
                    if (a0[i].Unify(start))
                    {
                        success = true;
                    }
                }
            }
            finally
            {
                start++;
            }
            return success;

        }
Esempio n. 45
0
 public abstract bool Call(PlTermV a0);
Esempio n. 46
0
 public static PlTerm PlC(string decl, PlTermV termV)
 {
     return PlTerm.PlCompound(decl, termV);
 }
Esempio n. 47
0
        public static bool cliBlockUntilEvent(PlTerm blockOn, PlTerm maxTime, PlTerm testVarsCode, PlTerm exitCode)
        {
            WaitUntilDelegateList list = null;
            object getInstance1 = GetInstance(blockOn);
            var wud = getInstance1 as WaitUntilDelegate;
            if (wud == null)
            {
                if (!(getInstance1 is WaitUntilDelegateList)) return Embedded.Error("Not an instance of WaitUntilDelegate: " + blockOn);
                list = getInstance1 as WaitUntilDelegateList;
            }
            else
            {
                list = wud.parent;
            }

            var timeSpan = TimeSpan.FromDays(3650);
            if (maxTime.IsInteger)
            {
                timeSpan = TimeSpan.FromMilliseconds(maxTime.intValue());
            }
            else if (!maxTime.IsVar)
            {
                timeSpan = (TimeSpan) CastTerm(maxTime, typeof (TimeSpan));
            }

            DateTime expireyTime = DateTime.Now.Add(timeSpan);
            while (DateTime.Now < expireyTime)
            {
                var results = list.WaitOne(timeSpan, out wud);
                if (results == null)
                {
                    return exitCode.UnifyAtom("time_limit_exceeded");
                }
                PlTerm copyTo = PlTerm.PlVar();
                PlTermV newPlTermV = new PlTermV(testVarsCode, copyTo);
                PlCall("system", "copy_term", newPlTermV);
                PlTerm ctestVars = copyTo.Arg(0);
                PlTerm ctestCode = copyTo.Arg(1);
                PlTerm[] terms = ToTermArray(ctestVars);
                int idx = terms.Length - 1;
                int resdex = results.Length - 1;
                while (idx >= 0 && resdex >= 0)
                {
                    terms[idx--].FromObject(results[resdex--]);
                }
                try
                {
                    if (PlCall("user", "call", new PlTermV(ctestCode)))
                        return UnifyToProlog(PlCall(null, "=", newPlTermV), exitCode) != 0;
                }
                finally
                {
                    list.Reset();
                }
            }
            return exitCode.UnifyAtom("time_limit_exceeded");
        }
Esempio n. 48
0
 public abstract bool Setup(PlTermV a0);
Esempio n. 49
0
        public static int FooThree(PlTerm a0, int arity, IntPtr control)
        {
            var handle = control;
            FRG fc = (FRG)(libpl.PL_foreign_control(control));

            switch (fc)
            {
                case FRG.PL_FIRST_CALL:
                    {
                        var v = NondetContextHandle.ObtainHandle(control);
                        var tv = new PlTermV(a0, arity);
                        bool res = v.Setup(tv);
                        bool more = v.HasMore();
                        if (more)
                        {
                            libpl.PL_retry(v.Handle);
                            return res ? 3 : 0;
                        }
                        return res ? 1 : 0;
                    } break;
                case FRG.PL_REDO:
                    {
                        var v = NondetContextHandle.FindHandle(control);
                        bool res = v.Call(new PlTermV(a0, arity));
                        bool more = v.HasMore();
                        if (more)
                        {
                            libpl.PL_retry(v.Handle);
                            return res ? 3 : 0;
                        }
                        return res ? 1 : 0;
                    } break;
                case FRG.PL_CUTTED:
                    {
                        var v = NondetContextHandle.FindHandle(control);
                        bool res = v.Close(new PlTermV(a0, arity));
                        NondetContextHandle.ReleaseHandle(v);
                        return res ? 1 : 0;
                    } break;
                default:
                    {
                        throw new PlException("no frg");
                        return libpl.PL_fail;
                    }
                    break;
            }
        }
Esempio n. 50
0
 private bool ModuleCall0(string s, PlTermV termV)
 {
     return PlQuery.PlCall(ClientModule, ClientPrefix + s, termV);
 }
Esempio n. 51
0
 public override bool Close(PlTermV a0)
 {
     end = start + 1;
     return true;
 }
Esempio n. 52
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlQuery(string)" />
        /// <summary>locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public PlQuery(string module, string goal)
        {
            _module = module;
            _query_string = goal;
            //_query_string = "(" + _query_string + ")";
            if (!_query_string.EndsWith(".", StringComparison.Ordinal))
                _query_string += ".";
            _query_string += Environment.NewLine;

            // redirect read stream
            DelegateStreamReadFunction old_read_function = PlEngine._function_read;
            DelegateStreamReadFunction rf = new DelegateStreamReadFunction(Sread);
            if (Embedded.RedirectStreams) PlEngine.SetStreamFunctionRead(PlStreamType.Input, rf);

            try
            {
                // call read_term(Term_of_query_string, [variable_names(VN)]).
                PlTerm term = PlTerm.PlVar();
                PlTerm option_list = PlTerm.PlVar();
                PlTerm variablenames_list = PlTerm.PlVar();
                PlTerm l = PlTerm.PlTail(option_list);
                l.Append(PlTerm.PlCompound("variable_names", variablenames_list));
                l.Close();
                PlTermV args = new PlTermV(term, option_list);
                // NOTE: read/1 needs a dot ('.') at the end
                if (!PlQuery.PlCall("read_term", args))
                    throw new PlLibException("PlCall read_term fails! goal:" + _query_string);

                // restore stream function
                if (Embedded.RedirectStreams) PlEngine.SetStreamFunctionRead(PlStreamType.Input, old_read_function);

                // set list of variables and variable_names into _queryVariables
                foreach (PlTerm t in variablenames_list.ToList())
                {
                    // t[0]='=' , t[1]='VN', t[2]=_G123
                    _queryVariables.Add(new PlQueryVar(t[1].ToString(), t[2]));
                }

                // Build the query
                _name = term.Name;

                // is ok e.g. for listing/0.
                // Check.Require(term.Arity > 0, "PlQuery(PlTerm t): t.Arity must be greater than 0."); 
                _av = new PlTermV(term.Arity);
                for (int index = 0; index < term.Arity; index++)
                {
                    if (0 == libpl.PL_get_arg(index + 1, term.TermRef, _av[index].TermRef))
                        throw new PlException("PL_get_arg in PlQuery " + term.ToString());
                }
            }
#if _DEBUG
            catch (Exception ex)
            {
                PrologCLR.ConsoleTrace(ex.Message);
            }
#endif
            finally
            {
                // NBT
            }
        }
Esempio n. 53
0
 public abstract bool Close(PlTermV termV);
Esempio n. 54
0
#pragma warning restore 1573



        //	public PlQuery(const char *Name, const PlTermV &av)
        /// <summary>
        /// Create a query where name defines the name of the predicate and av the argument vector. 
        /// The arity is deduced from av. The predicate is located in the Prolog module user.
        /// </summary>
        /// <example>
        ///     <para>This sample shows a query with a compound term as an argument.</para>
        ///     <code source="..\..\TestSwiPl\PlQuery.cs" region="PlCallQueryCompound_termv_doc" />
        ///  </example>
        /// <param name="name">the name of the predicate</param>
        /// <param name="termV">the argument vector containing the parameters</param>
        public PlQuery(string name, PlTermV termV)
            : this("user", name, termV)
        {
        }
Esempio n. 55
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlQuery(string,PlTermV)" />
        /// <summary>locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public PlQuery(string module, string name, PlTermV termV)
        {
            //if (null == termV)
            //    throw new ArgumentNullException("termV");

            _av = termV;
            _name = name;
            _module = module;
        }
Esempio n. 56
0
 public int BackrackImpl(PlTerm a0, int arity, IntPtr control)
 {
     FRG fc = (FRG) (libpl.PL_foreign_control(control));
     ContextHandle handle;
     switch (fc)
     {
         case FRG.PL_FIRST_CALL:
             {
                 handle = NondetContextHandle.ObtainHandle(control, Clone());
                 var av = new PlTermV(a0, arity);
                 handle.ManagedObject = New(handle, av);
                 return Call0(handle, av);
             }
             break;
         case FRG.PL_REDO:
             {
                 handle = NondetContextHandle.FindHandle(control);
                 return Call0(handle, new PlTermV(a0, arity));
             }
             break;
         case FRG.PL_CUTTED:
             {
                 handle = NondetContextHandle.FindHandle(control);
                 var av = new PlTermV(a0, arity);
                 bool res = Close(handle, av);
                 NondetContextHandle.ReleaseHandle(handle);
                 return res ? 1 : 0;
             }
             break;
         default:
             {
                 throw new PlException("no frg");
                 return libpl.PL_fail;
             }
             break;
     }
 }
Esempio n. 57
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlCall(string, PlTermV)" />
        /// <summary>As <see cref="PlCall(string, PlTermV)"/> but locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public static bool PlCall(string module, string predicate, PlTermV args)
        {
            bool bRet = false;
            PlQuery q = new PlQuery(module, predicate, args);
            bRet = q.NextSolution();
            q.Free(false);
            return bRet;
        }
Esempio n. 58
0
		//operator char *(void);
        /// <inheritdoc />
        /// <summary>
        /// The exception is translated into a message as produced by print_message/2. The character data is stored in a ring.
        /// </summary>
        /// <returns></returns>
		override public string ToString()
		{
            if (_messagePl != null) return GetType() + ": " + _messagePl;

            if (!PlEngine.IsInitialized)
                return "A PlException was thrown but it can't formatted because PlEngine is not Initialized.";

			string strRet = "[ERROR: Failed to generate message.  Internal error]\n";
            if (libpl.NoToString) return "[ERROR: Failed to generate message.  NoToString presently]\n";
            using (PlFrame fr = new PlFrame())
            {

#if USE_PRINT_MESSAGE
				PlTermV av = new PlTermV(2);

                av[0] = PlTerm.PlCompound("print_message", new PlTermV(new PlTerm("error"), new PlTerm( _exTerm.TermRef)));
				PlQuery q = new PlQuery("$write_on_string", av);
				if ( q.NextSolution() )
					strRet = (string)av[1];
				q.Free();
#else
                PlTermV av = new PlTermV(2);
                av[0] = new PlTerm(_exTerm.TermRef);
                PlQuery q = new PlQuery("$messages", "message_to_string", av);
                if (q.NextSolution())
                    strRet = av[1].ToString();
                //q.Free();
                return strRet;
                q.Dispose();
#endif
            }
			return strRet;
		}
Esempio n. 59
0
 public abstract bool Call(PlTermV termV);