NextSolution() public method

Provide the next solution to the query. Prolog exceptions are mapped to C# exceptions.

If the query is closed it will be opened. If the last solution was generated the query will be closed.

If an exception is thrown while parsing (open) the query the _qid is set to zero.

Is thrown if SWI-Prolog Manual PL_next_solution() returns false
public NextSolution ( ) : bool
return bool
Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
#pragma warning restore 1573
        /// <inheritdoc cref="PlCall(string, PlTermV)" />
        /// <summary>Call a goal once.</summary>
        /// <example>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("is_list([a,b,c,d])"));
        /// </code>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("consult('some_file_name')"));
        /// </code>
        /// </example>
        /// <param name="goal">The complete goal as a string</param>
        public static bool PlCall(string goal)
        {
            bool bRet;

            using (var q = new PlQuery("call", new PlTermV(new PlTerm(goal))))
            {
                bRet = q.NextSolution();
                q.Free(true);
            }
            return(bRet);
        }
Exemplo n.º 3
0
#pragma warning restore 1573
        /// <inheritdoc cref="PlCall(string, PlTermV)" />
        /// <summary>Call a goal once.</summary>
        /// <example>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("is_list([a,b,c,d])"));
        /// </code>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("consult('some_file_name')"));
        /// </code>
        /// </example>
        /// <param name="goal">The complete goal as a string</param>
        public static bool PlCall(string goal)
        {
            // TODO: change to use PlTerm(text) or PlCompound(string)
            // TODO: <weiterre tests z.b. mehrere goals/>
            bool    bRet = false;
            PlQuery q    = new PlQuery("call", new PlTermV(PlTerm.PlCompound(goal)));

            bRet = q.NextSolution();
            q.Free(true);
            return(bRet);
        }
Exemplo n.º 4
0
#pragma warning disable 1573
        /// <inheritdoc cref="PlCallQuery(System.String)" />
        /// <summary>As <see cref="PlCallQuery(string)"/> but executed in the named module.</summary>
        /// <param name="module">The modulename in which the query is executed</param>
        public static PlTerm PlCallQuery(string module, string goal)
        {
            PlTerm retVal;

            using (var q = new PlQuery(module, goal))
            {
                // find the variable or throw an exception
                PlTerm?t = null;
                if (q.Variables.Count == 1)
                {
                    t = new PlTerm(q.Variables[0].Value.TermRef);
                }
                else
                {
                    for (int i = 0; i < q._av.Size; i++)
                    {
                        if (!q._av[i].IsVar)
                        {
                            continue;
                        }
                        if (t == null)
                        {
                            t = new PlTerm(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 = (PlTerm)t;
                }
                else
                {
                    retVal = new PlTerm();    // null
                }
                q.Free(false);
            }
            return(retVal);
        }
Exemplo n.º 5
0
 //Load prolog file from hard disk
 public void Load_file(string s)
 {
     s = s.Replace("\\", "//");
     s = "consult('" + s + "')";
     string query = s.Replace("\\", "//");
     //string[] p = { "-q", "-f", query };
     //PlEngine.Initialize(p);
     try
     {
         PlQuery q = new PlQuery(query);
         Assert.IsTrue(q.NextSolution());
     }
     catch (SbsSW.SwiPlCs.Exceptions.PlException e)
     {
         System.Windows.Forms.MessageBox.Show(e.ToString(), "Error");
     }
 }
 public void SetValue(ICollectionRequester requester, string name, object value)
 {
     PrologCLR.InvokeFromC(
         () =>
             {
                 if (IsEmpty(Setter)) return false;
                 var plVar = PlTerm.PlVar();
                 var query = new PlQuery(Module, Setter,
                                         new PlTermV(PrologCLR.ToProlog(requester), PlTerm.PlString(NameSpace), PlTerm.PlString(name),
                                                     PrologCLR.ToProlog(value)));
                 while (query.NextSolution())
                 {
                     
                 }
                 return true;
             }, DiscardFrames);
 }
 public IEnumerable<string> SettingNames(ICollectionRequester requester, int depth)
 {
     return PrologCLR.InvokeFromC(
         () =>
             {
                 if (IsEmpty(KeyGetter)) return null;
                 List<string> names = new List<string>();
                 var plVar = PlTerm.PlVar();
                 var query = new PlQuery(Module, KeyGetter,
                                         new PlTermV(PrologCLR.ToProlog(requester), PlTerm.PlString(NameSpace),
                                                     plVar));
                 while (query.NextSolution())
                 {
                     string res = (string) query.Args[2];
                     if (!names.Contains(res)) names.Add(res);
                 }
                 return names.ToArray();
             }, DiscardFrames);
 }        
Exemplo n.º 8
0
        //TODO: <umstellen auf PlQuery(string)/>
        /// <summary>
        /// <para>NOTE:will be changed in the near future.</para>
        /// return the solution of a query which is called once by call
        /// Throw an ArgumentException if there is no or more than one variable in the goal
        /// </summary>
        /// <param name="goal">a goal with *one* variable</param>
        /// <returns>the bound variable of the first solution</returns>
        public static PlTerm PlCallQuery(string goal)
        {
            PlTerm  retVal;
            PlQuery q = new PlQuery(goal);

            {
                // find the variable or throw an exception
                PlTerm?t = null;
                for (int i = 0; i < q._av.Size; i++)
                {
                    if (q._av[i].IsVar)
                    {
                        if ((object)t == null)
                        {
                            t = new PlTerm(q._av[i].TermRef);
                        }
                        else
                        {
                            throw new ArgumentException("More than one Variable in " + goal);
                        }
                    }
                }
                if ((object)t == null)
                {
                    throw new ArgumentException("No Variable found in " + goal);
                }

                if (q.NextSolution())
                {
                    retVal = (PlTerm)t;
                }
                else
                {
                    retVal = new PlTerm();    // null
                }
            }
            q.Free(false);
            return(retVal);
        }
 public ICollection GetGroup(ICollectionRequester requester, string name)
 {
     return PrologCLR.InvokeFromC(
         () =>
             {
                 if (IsEmpty(Getter)) return null;
                 var plVar = PlTerm.PlVar();
                 List<object> results = new List<object>();
                 var query = new PlQuery(Module, Getter,
                                         new PlTermV(PrologCLR.ToProlog(requester), PlTerm.PlString(NameSpace), PlTerm.PlString(name),
                                                     plVar));
                 while (query.NextSolution())
                 {
                     object res = PrologCLR.GetInstance(query.Args[3]);
                     if (!results.Contains(res)) results.Add(res);
                 }
                 return results.Count == 0 ? null : results;
             }, DiscardFrames);
 }
Exemplo n.º 10
0
 public void SetValue(ICollectionRequester requester, string name, object value)
 {
     PrologCLR.InvokeFromC(
         () =>
         {
             PlTerm callback = AllCallbacks.Args[1];
             if (IsEmpty(callback)) return false;
             var plVar = PlTerm.PlVar();
             var query = new PlQuery("call",
                                     new PlTermV(callback, PlTerm.PlString(NameSpace), PlTerm.PlString(name),
                                                 PrologCLR.ToProlog(value)));
             return query.NextSolution();
         }, DiscardFrames);
 }
Exemplo n.º 11
0
        //TODO: <umstellen auf PlQuery(string)/>
        /// <summary>
        /// <para>NOTE:will be changed in the near future.</para>
        /// return the solution of a query which is called once by call
        /// Throw an ArgumentException if there is no or more than one variable in the goal
        /// </summary>
        /// <param name="goal">a goal with *one* variable</param>
        /// <returns>the bound variable of the first solution</returns>
        public static PlTerm PlCallQuery(string goal)
        {
            PlTerm retVal;
            PlQuery q = new PlQuery(goal);
            {
                // find the variable or throw an exception
                PlTerm ? t = null;
                for (int i = 0; i < q._av.Size; i++)
                {
                    if (q._av[i].IsVar)
                    {
                        if ((object)t == null)
                        {
                            t = new PlTerm(q._av[i].TermRef);
                        }
                        else
                            throw new ArgumentException("More than one Variable in " + goal);
                    }
                }
                if ((object)t == null)
                    throw new ArgumentException("No Variable found in " + goal);

                if (q.NextSolution())
                {
                    retVal = (PlTerm)t;
                }
                else
                    retVal = new PlTerm();    // null
            }
            q.Free(false);
            return retVal;
        }
Exemplo n.º 12
0
#pragma warning restore 1573
        /// <inheritdoc cref="PlCall(string, PlTermV)" />
        /// <summary>Call a goal once.</summary>
        /// <example>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("is_list([a,b,c,d])"));
        /// </code>
        /// <code>
        ///      Assert.IsTrue(PlQuery.PlCall("consult('some_file_name')"));
        /// </code>
        /// </example>
        /// <param name="goal">The complete goal as a string</param>
        public static bool PlCall(string goal)
        {
            // TODO: change to use PlTerm(text) or PlCompound(string)
            // TODO: <weiterre tests z.b. mehrere goals/>
            bool bRet = false;
            PlQuery q = new PlQuery("call", new PlTermV(PlTerm.PlCompound(goal)));
            bRet = q.NextSolution();
            q.Free(true);
            return bRet;
        }
Exemplo n.º 13
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;
        }