Swi-cs-pl - A CSharp class library to connect .NET languages with SWI-Prolog
PlQuery Constructor (goal)
SwiPlCs interface ► SbsSW.SwiPlCs ► PlQuery ► PlQuery(String)
With this constructor a query is created from a string.
Uppercase parameters are interpreted a variables but can't be nested in sub terms. If you need a variable in a nested term use PlQuery(String, PlTermV). See the examples for details.
Declaration Syntax
Parameters
- goal (String)
- A string for a prolog query
Remarks
Muddy Waters sang:"I'am build for comfort, I ain't build for speed"
Examples
CopyC#
public void queryStringForeach() { string mm = "abc"; PlQuery q = new PlQuery("member(A, [a,b,c])"); int i = 0; foreach (PlTermV s in q.Solutions) { Assert.AreEqual(mm[i++].ToString(), s[0].ToString()); } // or with named variables i = 0; foreach (PlQueryVariables s in q.SolutionVariables) { Assert.AreEqual(mm[i++].ToString(), s["A"].ToString()); } }
This sample shows a query with two variables.
CopyC#
public void queryString2() { PlQuery q = new PlQuery("append(A, B, [a,b,c])"); Assert.IsTrue(q.NextSolution()); Assert.AreEqual("[]", q.Args[0].ToString()); Assert.AreEqual("[a,b,c]", q.Args[1].ToString()); Assert.IsTrue(q.NextSolution()); Assert.AreEqual("[a]", q.Args[0].ToString()); Assert.AreEqual("[b,c]", q.Args[1].ToString()); }
And the same with named variables.
CopyC#
public void queryStringNamed() { PlQuery q = new PlQuery("append(A, B, [a,b,c])"); Assert.IsTrue(q.NextSolution()); Assert.AreEqual("[]", q.Variables["A"].ToString()); Assert.AreEqual("[a,b,c]", q.Variables["B"].ToString()); Assert.IsTrue(q.NextSolution()); Assert.AreEqual("[a]", q.Variables["A"].ToString()); Assert.AreEqual("[b,c]", q.Variables["B"].ToString()); }
This sample shows what happens if the argument vector is used with compound terms.
CopyC#
public void PlCallQueryCompound_string() { string[] mm = { "comp(aa,aa1)", "comp(aa,aa2)", "comp(aa,aa3)" }; build_pred(); // create: test(comp(X,Y)) :- member(Z,[1,2,3]), atomic_list_concat([X,Z],Y). PlQuery q = new PlQuery("test(comp(aa,X))"); int i = 0; foreach (PlTermV s in q.Solutions) { Assert.AreEqual(mm[i++].ToString(), s[0].ToString()); } }
And here how to get the results with named variables with compound terms.
CopyC#
public void PlCallQueryCompoundNamed_string() { string[] mm = { "aa1", "aa2", "aa3" }; build_pred(); // create: test(comp(X,Y)) :- member(Z,[1,2,3]), atomic_list_concat([X,Z],Y). PlQuery q = new PlQuery("test(comp(aa,X))"); int i = 0; foreach (PlQueryVariables v in q.SolutionVariables) { Assert.AreEqual(mm[i++].ToString(), v["X"].ToString()); } }