Example #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);
        }
Example #2
0
 /// <summary>
 /// Creates an ISO standard Prolog error term expressing the expected type and actual term that does not satisfy this type.
 /// </summary>
 /// <param name="expected">The type which was expected</param>
 /// <param name="actual">The actual term</param>
 public SwiPrologTypeException(string expected, SwiPrologTerm actual)
     : base(
         SwiPrologTerm.Compound("error",
                                new SwiPrologTermVector(SwiPrologTerm.Compound("type_error",
                                                                               new SwiPrologTermVector(new SwiPrologTerm(expected), actual)),
                                                        SwiPrologTerm.Variable())
                                ))
 {
 }
Example #3
0
 /// <inheritdoc />
 public SwiPrologException(string message, Exception innerException)
     : base(message, innerException)
 {
     if (null == innerException)
     {
         throw new ArgumentNullException("innerException");
     }
     _messagePl = message + "; innerExeption:" + innerException.Message;
     _exTerm    = new SwiPrologTerm(message);
 }
Example #4
0
 // ISerializable Constructor
 /// <inheritdoc />
 protected SwiPrologException(SerializationInfo info, StreamingContext context)
     : base(info, context)
 {
     if (info == null)
     {
         throw new ArgumentNullException("info");
     }
     _messagePl = (string)info.GetValue("_messagePl", typeof(string));
     _exTerm    = (SwiPrologTerm)info.GetValue("_exTerm", typeof(SwiPrologTerm));
 }
Example #5
0
#pragma warning disable 1573
        /// <inheritdoc cref="CallQuery(System.String)" />
        /// <summary>As <see cref="CallQuery(string)"/> but executed in the named module.</summary>
        /// <param name="module">The modulename in which the query is executed</param>
        public static SwiPrologTerm CallQuery(string module, string goal)
        {
            SwiPrologTerm retVal;

            using (var q = new SwiPrologQuery(module, goal))
            {
                // find the variable or throw an exception
                SwiPrologTerm?t = null;
                if (q.Variables.Count == 1)
                {
                    t = new SwiPrologTerm(q.Variables[0].Value.TermRef);
                }
                else
                {
                    for (int i = 0; i < q._av.Size; i++)
                    {
                        if (!q._av[i].IsVariable)
                        {
                            continue;
                        }
                        if (t == null)
                        {
                            t = new SwiPrologTerm(q._av[i].TermRef);
                        }
                        else
                        {
                            throw new ArgumentException("More than one Variable in " + goal);
                        }
                    }
                }
                if (t == null)
                {
                    throw new ArgumentException("No Variable found in " + goal);
                }

                if (q.NextSolution())
                {
                    retVal = (SwiPrologTerm)t;
                }
                else
                {
                    retVal = new SwiPrologTerm();    // null
                }
                q.Free(false);
            }
            return(retVal);
        }
Example #6
0
        public IEnumerable <SwiPrologQueryResult> Query(SwiPrologTerm query)
        {
            //return Query(query.ToString());
            var count = query.Arity;
            var array = new SwiPrologTerm[count];

            for (int index = 0; index < count; ++index)
            {
                array[index] = query[index + 1];
            }
            using (var q = new SwiPrologQuery(module, query.Name, new SwiPrologTermVector(array)))
            {
                foreach (SwiPrologQueryResult v in q.SolutionVariables)
                {
                    yield return(v);
                }
            }
        }
Example #7
0
 public SwiPrologTerm List(SwiPrologTerm head, SwiPrologTerm tail)
 {
     return(SwiPrologTerm.List(head, tail));
 }
Example #8
0
 public SwiPrologTerm Compound(string functor, params SwiPrologTerm[] args)
 {
     return(SwiPrologTerm.Compound(functor, new SwiPrologTermVector(args)));
 }
Example #9
0
 public SwiPrologTerm Variable()
 {
     return(SwiPrologTerm.Variable());
 }
Example #10
0
 internal SwiPrologQueryVariable(string name, SwiPrologTerm val)
 {
     Name  = name;
     Value = val;
 }
Example #11
0
 public bool Call(SwiPrologTerm goal)
 {
     return(Call(goal.ToString()));
 }
Example #12
0
 /// <inheritdoc cref="SwiPrologException" />
 public SwiPrologDomainException(SwiPrologTerm term)
     : base(term)
 {
 }
Example #13
0
 /// <inheritdoc />
 public SwiPrologTypeException(SwiPrologTerm term)
     : base(term)
 {
 }
Example #14
0
 /// <summary>
 /// <para>To catch a exception thrown by prolog</para>
 /// <para>For a example see <see cref="SwiPrologException"/>.</para>
 /// </summary>
 /// <param name="term">A PlTerm containing the Prolog exception</param>
 /// <see cref="SwiPrologException"/>
 public SwiPrologException(SwiPrologTerm term)
 {
     _exTerm = new SwiPrologTerm(term.TermRef);  // If this line is deleted -> update comment in PlTern(term_ref)
 }
Example #15
0
 public SwiPrologTerm Nil()
 {
     return(SwiPrologTerm.Nil());
 }
Example #16
0
 public SwiPrologTerm String(string text)
 {
     return(SwiPrologTerm.String(text));
 }
Example #17
0
 /// <inheritdoc />
 public SwiPrologException()
 {
     _exTerm = SwiPrologTerm.Variable();
 }
Example #18
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
            }
        }
Example #19
0
 /// <inheritdoc />
 public SwiPrologException(string message)
     : base(message)
 {
     _messagePl = message;
     _exTerm    = new SwiPrologTerm(message);
 }