private void evaluate_click(object sender, System.EventArgs e) { try { Bindings bindings = new Bindings().bind("E", textBox1.Text + '.'); session.connect(); /* This will connect if neccessary. */ QueryAnswer answer = session.executeQuery("evaluate(E,R)", bindings); PBTerm result = answer.getValue("R"); if (result != null) { textBox2.AppendText(textBox1.Text + " = " + result + Environment.NewLine); textBox1.Clear(); } else { textBox2.AppendText("Error: " + answer.getError() + Environment.NewLine); } } catch (System.Exception ex) { textBox2.AppendText("Error when querying Prolog Server: " + ex.Message + Environment.NewLine); Console.Error.WriteLine(ex); Console.Error.Write(ex.StackTrace); Console.Error.Flush(); } }
// QueryAnswer constructor /// <summary> Returns the value of the specified variable or <code>null</code> if the /// variable is not bound. /// /// </summary> /// <param name="name">the name of the variable /// </param> /// <returns> the value of the variable as a {@link se.sics.prologbeans.PBTerm} /// or <code>null</code> if the variable is not bound /// </returns> public override PBTerm getValue(string name) { if (!bound) { if (hasValues) { // [PM] 4.1.3 No longer rely on answer being a PBListCell // copy all the new bindings into Bindings PBTerm list; // [PM] 4.1.3 for type checking only. Barfs if not a proper // list. (FIXME: redundant) answer.length(); for (list = answer; list.ListCell; list = list.getArgument(2)) { PBTerm bindTerm = list.getArgument(1); if (bindTerm.Name.Equals("=")) { bind(bindTerm.getArgument(1).Name, bindTerm.getArgument(2)); } } // assert list.isEmptyList(); } bound = true; } return(base.getValue(name)); }
/// <summary> Creates a new <code>QueryAnswer</code> instance with the specified /// information. /// /// </summary> /// <param name="answer"> /// a {@link se.sics.prologbeans.PBTerm} value representing the /// Prolog response /// </param> /// <param name="bindings">the variable bindings for the query to which this is an answer /// </param> public QueryAnswer(PBTerm answer, Bindings bindings) : base(bindings) { this.answer = answer; // *** FIXME: this test is a bit contrived. // [PM] 4.2.1 FIXME: No need to check instanceof. hasValues = answer.ListCell && answer is PBListCell; }
/* * private static bool compare(PBString pbstr, String str) * { * PBList pbl = pbstr; * int i; * for (i = 0; i < str.Length && pbl.Length != 0; i++) { * int c = pbl.getArgument(1).intValue(); * if (c != str[i]) { * return false; * } else { * pbl = (PBList)(pbl.getArgument(2)); * } * } * if (i < str.Length || pbl.Length != 0) { * return false; * } * return true; * } */ private static bool listcompare(PBTerm pbl, String str) { int i; for (i = 0; i < str.Length && pbl.length() != 0; i++) { long c = pbl.getArgument(1).intValue(); if (c != str[i]) { return(false); } else { pbl = pbl.getArgument(2); } } if (i < str.Length || pbl.length() != 0) { return(false); } return(true); }
private static bool listcompare(PBTerm t1, PBTerm t2) { int i; if (t1.length() != t2.length()) { return(false); } for (i = 0; t1.length() != 0; i++) { long c1 = t1.getArgument(1).intValue(); long c2 = t2.getArgument(1).intValue(); if (c1 != c2) { return(false); } else { t1 = t1.getArgument(2); t2 = t2.getArgument(2); } } return(true); }
private static bool listcompare(PBTerm t1, PBTerm t2) { int i; if (t1.length() != t2.length()) { return false; } for (i = 0; t1.length() != 0; i++) { long c1 = t1.getArgument(1).intValue(); long c2 = t2.getArgument(1).intValue(); if (c1 != c2) { return false; } else { t1 = t1.getArgument(2); t2 = t2.getArgument(2); } } return true; }
/* private static bool compare(PBString pbstr, String str) { PBList pbl = pbstr; int i; for (i = 0; i < str.Length && pbl.Length != 0; i++) { int c = pbl.getArgument(1).intValue(); if (c != str[i]) { return false; } else { pbl = (PBList)(pbl.getArgument(2)); } } if (i < str.Length || pbl.Length != 0) { return false; } return true; } */ private static bool listcompare(PBTerm pbl, String str) { int i; for (i = 0; i < str.Length && pbl.length() != 0; i++) { long c = pbl.getArgument(1).intValue(); if (c != str[i]) { return false; } else { pbl = pbl.getArgument(2); } } if (i < str.Length || pbl.length() != 0) { return false; } return true; }
public static void Main(System.String[] args) { // Startup the prolog and show its err output! int test = 1; PrologSession session = null; PBTest evalTest = new PBTest(); evalTest.prolog = args[0]; try { // SupportClass.ThreadClass t = new SupportClass.ThreadClass(new ThreadStart(evalTest.Run)); Thread t = new Thread(new ThreadStart(evalTest.Run)); t.IsBackground = true; t.Start(); // Get the port from the SICStus process (and fail if port is an error value) int port = evalTest.Port; if (port <= 0) { evalTest.fail("could not start sicstus", test); } session = new PrologSession(); System.Console.Error.WriteLine("DBG: setting port; port==" + port); // session.Port = port; // PrologBeans.NET.c# -version session.setPort(port); // PrologBeans.NET.j# -version { System.Console.Error.WriteLine("DBG: setting timeout=0"); // session.Timeout = 0; // PrologBeans.NET.c# -version session.setTimeout(0); // PrologBeans.NET.j# -version } // Test 1. - evaluation! Bindings bindings = new Bindings().bind("E", "10+20."); session.connect(); /* This will connect if neccessary. */ QueryAnswer answer = session.executeQuery("evaluate(E,R)", bindings); PBTerm result = answer.getValue("R"); if (result != null) { if (result.intValue() == 30) { success("10+20=" + result, test++); } else { evalTest.fail("Execution failed: " + result, test); } } else { evalTest.fail("Error " + answer.getError(), test); } // Test 2 - list reverse! bindings = new Bindings().bind("E", "reverse"); answer = session.executeQuery("reverse(E,R)", bindings); result = answer.getValue("R"); if (result != null) { // if ("esrever".Equals(result.ToString())) if (listcompare(result, "esrever")) { success("rev(reverse) -> " + result, test++); } else { evalTest.fail("Execution failed: " + result, test); } } else { evalTest.fail("Error " + answer.getError(), test); } // Test 2b - SPRM 13863 transfer lists of small integers PBTerm NIL = PBTerm.makeAtom("[]"); PBTerm eTerm = PBTerm.makeTerm(PBTerm.makeTerm((int)127), PBTerm.makeTerm(PBTerm.makeTerm((int)128), PBTerm.makeTerm(PBTerm.makeTerm((int)129), NIL))); PBTerm rTerm = PBTerm.makeTerm(PBTerm.makeTerm((int)129), PBTerm.makeTerm(PBTerm.makeTerm((int)128), PBTerm.makeTerm(PBTerm.makeTerm((int)127), NIL))); bindings = new Bindings().bind("E", eTerm); answer = session.executeQuery("reverse(E,R)", bindings); result = answer.getValue("R"); if (result != null) { if (listcompare(result, rTerm)) { success("rev(" + eTerm + ") -> " + result, test++); } else { evalTest.fail("Execution failed: " + "rev(" + eTerm + ") -> " + result, test); } } else { evalTest.fail("Error " + answer.getError(), test); } // Test 3 - show developers // [PD] 3.12.3 Test non-ascii character in query name answer = session.executeQuery("devel\x00f6pers(Dev)"); result = answer.getValue("Dev"); if (result != null) { // if (result.ProperList) // PrologBeans.NET.c# -version if (result.isProperList()) // PrologBeans.NET.j# -version { PBTerm list = result; if (list.length() == 4 && "Joakim".Equals(list.head().ToString()) && "Niclas".Equals(list.tail().head().ToString()) && "Per".Equals(list.tail().tail().head().ToString()) && // [PD] 3.12.2 Do not use non-ASCII literals // "едц≈ƒ÷".Equals(list.getTermAt(4).ToString())) "\u00e5\u00e4\u00f6\u00c5\u00c4\u00d6".Equals(list.tail().tail().tail().head().ToString())) { // [PD] 3.12.3 Test non-ascii character in query name success("devel\x00f6pers -> " + result, test++); } else { evalTest.fail("Execution failed: " + result, test); } } else { evalTest.fail("Execution failed: " + result, test); } } else { evalTest.fail("Error " + answer.getError(), test); } // Test 4 - send and receive a complex string-list String str = "foo\u1267bar"; bindings = new Bindings().bind("L1", str); answer = session.executeQuery("send_receive(L1,L2)", bindings); result = answer.getValue("L2"); if (result != null) { // if (((PBString)result).equals(str)) { if (listcompare(result, str)) { success("send_receive(" + str + ") -> " + result, test++); } else { evalTest.fail("Execution failed: " + result, test); } } else { evalTest.fail("Error " + answer.getError(), test); } // Test 5 - send and receive a very large atom int stringLength = 100000; String longStr = new String('x', stringLength); bindings = new Bindings().bind("L1", longStr); answer = session.executeQuery("send_receive(L1,L2)", bindings); result = answer.getValue("L2"); if (result != null) { if (result.getString().Equals(longStr)) { success("OK (" + stringLength + " characters)", test++); } else { evalTest.fail("Execution failed: " + stringLength + " characters", test); } } else { evalTest.fail("Error " + answer.getError() + ", " + stringLength + " characters", test); } // Test 6. Attributed variables bindings = new Bindings(); bindings.bind("N", 1); bindings.bind("M", 5); answer = session.executeQuery("newVar(X,N,M)", bindings); result = answer.getValue("X"); if (result != null) { if (result.isVariable()) { success("OK", test++); } else { evalTest.fail("Execution failed: " + stringLength + " characters", test); } } else { evalTest.fail("Error " + answer.getError() + ", " + stringLength + " characters", test); } // Test 7. shutdown server... session.executeQuery("shutdown"); if (!evalTest.waitForShutdown()) { evalTest.fail("shutdown", test++); } else { success("shutdown", test++); } } catch (System.Exception e) { if (error == 0) { Console.Error.WriteLine("PBTest.Main caught an exception."); System.Console.Error.WriteLine(e); // SupportClass.WriteStackTrace(e, Console.Error); Console.Error.WriteLine(e.ToString()); Console.Error.Flush(); evalTest.fail("Exception " + e.Message, test); } } finally { if (session != null) { session.disconnect(); } evalTest.shutdown(); System.Environment.Exit(error); } }