Exemplo n.º 1
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 (libswipl.PL_is_initialised(IntPtr.Zero, IntPtr.Zero) == 0)
            {
                return("A PlException was thrown but it can't formatted because SwiPrologEngine is not Initialized.");
            }

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

            using (new SwiPrologFrame())
            {
#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 SwiPrologTermVector(2);
                av[0] = new SwiPrologTerm(_exTerm.TermRef);
                using (var q = new SwiPrologQuery("$messages", "message_to_string", av))
                {
                    if (q.NextSolution())
                    {
                        strRet = av[1].ToString();
                    }
                }
#endif
            }
            return(strRet);
        }
Exemplo n.º 2
0
#pragma warning disable 1573
        /// <inheritdoc cref="SwiPrologQuery(string,SwiPrologTermVector)" />
        /// <summary>locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        internal SwiPrologQuery(string module, string name, SwiPrologTermVector termV)
        {
            //if (null == termV)
            //    throw new ArgumentNullException("termV");

            _av     = termV;
            _name   = name;
            _module = module;
        }
Exemplo n.º 3
0
#pragma warning disable 1573
        // Parameter 'predicate' has no matching param tag in the XML comment for 'SbsSW.SwiPlCs.PlQuery.PlCall(string, string, SbsSW.SwiPlCs.PlTermV)' (but other parameters do)
        /// <inheritdoc cref="Call(string, SwiPrologTermVector)" />
        /// <summary>As <see cref="Call(string, SwiPrologTermVector)"/> but locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public static bool Call(string module, string predicate, SwiPrologTermVector args)
        {
            bool bRet;

            using (var q = new SwiPrologQuery(module, predicate, args))
            {
                bRet = q.NextSolution();
                q.Free(false);
            }
            return(bRet);
        }
Exemplo n.º 4
0
 /// <overloads>
 /// The main purpose of the static PlCall methods is to call a prolog prove or to do some site effects.
 /// <example>
 /// <code>
 ///      Assert.IsTrue(PlQuery.PlCall("is_list", new PlTerm("[a,b,c,d]")));
 /// </code>
 /// <code>
 ///      Assert.IsTrue(PlQuery.PlCall("consult", new PlTerm("some_file_name")));
 ///      // or
 ///      Assert.IsTrue(PlQuery.PlCall("consult('some_file_name')"));
 /// </code>
 /// </example>
 /// </overloads>
 /// <inheritdoc cref="SwiPrologQuery(string,SwiPrologTermVector)" />
 /// <remarks>
 /// <para>Create a PlQuery from the arguments, generates the first solution by NextSolution() and destroys the query.</para>
 /// </remarks>
 /// <param name="predicate">defines the name of the predicate</param>
 /// <param name="args">Is a <see cref="SwiPrologTermVector"/> of arguments for the predicate</param>
 /// <returns>Return true or false as the result of NextSolution() or throw an exception.</returns>
 public static bool Call(string predicate, SwiPrologTermVector args)
 {
     return(Call(ModuleDefault, predicate, args));
 }
Exemplo n.º 5
0
 //	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>
 internal SwiPrologQuery(string name, SwiPrologTermVector termV)
     : this(ModuleDefault, name, termV)
 {
 }
Exemplo n.º 6
0
#pragma warning disable 1573
        /// <inheritdoc cref="SwiPrologQuery(string)" />
        /// <summary>locating the predicate in the named module.</summary>
        /// <param name="module">locating the predicate in the named module.</param>
        public SwiPrologQuery(string module, string goal)
        {
            if (string.IsNullOrEmpty(goal))
            {
                throw new ArgumentNullException("goal");
            }
            if (string.IsNullOrEmpty(module))
            {
                _module = ModuleDefault;
            }
            else
            {
                _module = module;
            }

            var queryString = goal;

            try
            {
                // call read_term(Term_of_query_string, [variable_names(VN)]).
                // read_term_from_atom('noun(ş,C)', T, [variable_names(Vars)]).
                // befor 2014 with redirected IO-Streams (PlQuery_Old_Kill_unused)
                var           atom          = new SwiPrologTerm("'" + goal.Replace(@"\", @"\\").Replace("'", @"\'") + "'");
                SwiPrologTerm term          = SwiPrologTerm.Variable();
                SwiPrologTerm options       = SwiPrologTerm.Variable();
                SwiPrologTerm variablenames = SwiPrologTerm.Variable();
                SwiPrologTerm l             = SwiPrologTerm.Tail(options);
                l.Append(SwiPrologTerm.Compound("variable_names", variablenames));
                l.Close();
                var args = new SwiPrologTermVector(atom, term, options);
                if (!Call(_module, "read_term_from_atom", args))
                {
                    throw new SwiPrologLibraryException("Call read_term_from_atom/3 fails! goal:" + queryString);
                }

                // set list of variables and variable_names into _queryVariables
                foreach (SwiPrologTerm t in variablenames.ToList())
                {
                    // t[0]='=' , t[1]='VN', t[2]=_G123
                    _queryVariables.Add(new SwiPrologQueryVariable(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 SwiPrologTermVector(term.Arity);
                for (int index = 0; index < term.Arity; index++)
                {
                    if (0 == libswipl.PL_get_arg(index + 1, term.TermRef, _av[index].TermRef))
                    {
                        throw new SwiPrologException("PL_get_arg in PlQuery " + term.ToString());
                    }
                }
            }
#if _DEBUG
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                Console.WriteLine(ex.Message);
            }
#endif
            finally
            {
                // NBT
            }
        }