public void t_callback_with_subQuery() { Delegate d = new DelegateParameter1(assert_fact); PlEngine.RegisterForeign(d); Assert.IsTrue(PlQuery.PlCall("assert( (test(Result) :- member(X,[1,2,3]), assert_fact(X), findall(E, fact(E), Result)) )")); int count = 1; var q = new PlQuery("test(L)"); foreach (PlTermV s in q.Solutions) { switch (count) { case 1: Assert.AreEqual("[1]", s[0].ToString()); break; case 2: Assert.AreEqual("[1,2]", s[0].ToString()); break; case 3: Assert.AreEqual("[1,2,3]", s[0].ToString()); break; } //Console.WriteLine(s[0].ToString()); count++; } }
public static void RegisterPLCSForeigns() { CreatorThread = Thread.CurrentThread; RegisterMainThread(); ShortNameType = new Dictionary <string, Type>(); ShortNameType["string"] = typeof(String); ShortNameType["object"] = typeof(Object); ShortNameType["sbyte"] = typeof(sbyte); // libpl.PL_agc_hook(new AtomGCHook(Tracker_FreeAtom)); //ShortNameType = new PrologBackedDictionary<string, Type>(null, "shortTypeName"); //PlEngine.RegisterForeign(null, "cliFindClass", 2, new DelegateParameter2(PrologCli.cliFindClass), PlForeignSwitches.None); PlEngine.RegisterForeign(ExportModule, "cli_load_assembly", 1, new DelegateParameter1(PrologCLR.cliLoadAssembly), PlForeignSwitches.None); if (VerboseStartup) { ConsoleWriteLine("RegisterPLCSForeigns"); } InternMethod(null, "cwl", typeof(Console).GetMethod("WriteLine", ONE_STRING)); Type t = typeof(PrologCLR); InternMethod(ExportModule, "cli_load_assembly_methods", t.GetMethod("cliLoadAssemblyMethods")); InternMethod(t.GetMethod("cliAddAssemblySearchPath"), "cli_"); InternMethod(t.GetMethod("cliRemoveAssemblySearchPath"), "cli_"); AddForeignMethods(t, false, "cli_"); RegisterJPLForeigns(); if (VerboseStartup) { ConsoleWriteLine("done RegisterPLCSForeigns"); } }
public static void Ex3() { if (!PlEngine.IsInitialized) { String[] param = { "-q" }; // suppressing informational and banner messages PlEngine.Initialize(param); // Test 1 //PlQuery.PlCall("assert(father(martin, inka))"); //Console.WriteLine(PlQuery.PlCall("father(martin, inkal)")); Del myComp = DoStuff; PlEngine.RegisterForeign(null, "mmm", 5, myComp); Console.WriteLine(PlQuery.PlCall("mmm(3, 6, 9, X, Y)")); using (PlQuery q = new PlQuery("mmm(3, 6, 9, X, Y)")) { foreach (PlQueryVariables v in q.SolutionVariables) { Console.WriteLine(v["X"].ToString()); Console.WriteLine(v["Y"].ToString()); } } PlEngine.PlCleanup(); } }
public static void LoadNonDetExamples() { Fn015.Register(); const PlForeignSwitches Nondeterministic = PlForeignSwitches.Nondeterministic; PlEngine.RegisterForeign(null, "foo2", 2, new DelegateParameterBacktrack2(FooTwo), Nondeterministic); PlEngine.RegisterForeign(null, "foo3", 3, new DelegateParameterBacktrackVarArgs(FooThree), Nondeterministic | PlForeignSwitches.VarArgs); }
public void t_succeed_and_fail() { Delegate d = new DelegateParameter1(odd); PlEngine.RegisterForeign(d); Assert.IsFalse(PlQuery.PlCall("odd(as)")); Assert.IsFalse(PlQuery.PlCall("odd(4)")); Assert.IsTrue(PlQuery.PlCall("odd(5)")); }
private static void RegisterJPLForeigns() { // backup old jpl.pl and copy over it if (!JplDisabled) { SafelyRun(() => { if (File.Exists(IKVMHome + "/jpl_for_ikvm.phps")) { if (!File.Exists(SwiHomeDir + "/library/jpl.pl.old")) { File.Copy(SwiHomeDir + "/library/jpl.pl", SwiHomeDir + "/library/jpl.pl.old", true); } File.Copy(IKVMHome + "/jpl_for_ikvm.phps", SwiHomeDir + "/library/jpl.pl", true); } }); } PlEngine.RegisterForeign("swicli", "link_swiplcs", 1, new DelegateParameter1(link_swiplcs), PlForeignSwitches.None); //JplSafeNativeMethods.install(); JplSafeNativeMethodsCalled = true; //DoQuery(new Query("ensure_loaded(library(jpl)).")); /* * * * jpl_jlist_demo :- * jpl_new( 'javax.swing.JFrame', ['modules'], F), * jpl_new( 'javax.swing.DefaultListModel', [], DLM), * jpl_new( 'javax.swing.JList', [DLM], L), * jpl_call( F, getContentPane, [], CP), * jpl_call( CP, add, [L], _), * ( current_module( M), * jpl_call( DLM, addElement, [M], _), * fail * ; true * ), * jpl_call( F, pack, [], _), * jpl_call( F, getHeight, [], H), * jpl_call( F, setSize, [150,H], _), * jpl_call( F, setVisible, [@(true)], _). * * * % this directive runs the above demo * * :- jpl_jlist_demo. * */ return; //we dont need to really do this PlCall("use_module(library(jpl))."); PlAssert("jpl0 :- jpl_new( 'java.lang.String', ['hi'], DLM),writeln(DLM)"); PlAssert("jpl1 :- jpl_new( 'javax.swing.DefaultListModel', [], DLM),writeln(DLM)"); }
public static void InternMethod(MethodInfo m, PrologVisible pm) { pm.Name = ComputeName(pm, m); if (pm.DelegateType != null) { ExportedMethodInfos.Add(m); PlEngine.RegisterForeign(pm.ModuleName, pm.Name, pm.Arity, pm.Delegate, pm.ForeignSwitches); return; } InternMethod(pm.ModuleName, pm.Name, m); }
public void t_creating_a_list() { Delegate d = new DelegateParameter1(create_list); PlEngine.RegisterForeign(d); for (int i = 1; i < 10; i++) { PlTerm t = PlQuery.PlCallQuery("create_list(L)"); Assert.AreEqual("[a,b,c]", t.ToString(), "create_list failed!"); } }
//[Ignore] //[TestMethod] public void t_backtrack() { Delegate d = new DelegateParameterBacktrack(my_member); PlEngine.RegisterForeign(d); for (int i = 1; i < 10; i++) { PlTerm t = PlQuery.PlCallQuery("my_member(X, [a,b,c])"); Assert.AreEqual("abc", t.ToString(), "my_concat_atom failed!"); } }
public void t_modifying_a_list() { Delegate d = new DelegateParameter2(modify_list); PlEngine.RegisterForeign(d); for (int i = 1; i < 10; i++) { PlTerm t = PlQuery.PlCallQuery("modify_list([a,b,c], L)"); Assert.AreEqual("[aa,bb,cc]", t.ToString(), "modify_list failed!"); } }
public void t_in_out() { Delegate replaceDelegate = new DelegateParameter2(atom_replace); PlEngine.RegisterForeign(replaceDelegate); for (int i = 1; i < 10; i++) { var arg = new PlTermV(new PlTerm("test_f"), PlTerm.PlVar()); PlQuery.PlCall("atom_replace", arg); Assert.AreEqual("test_xx_f", arg[1].ToString(), "atom_replace failed!"); } }
public void t_varargs_single() { // ReSharper disable once CSharpWarnings::CS0618 Delegate d = new DelegateParameterVarArgs(my_concat_atom); PlEngine.RegisterForeign("my_concat_atom", 4, d); for (int i = 1; i < 10; i++) { PlTerm t = PlQuery.PlCallQuery("my_concat_atom(a,b,c, X)"); Assert.AreEqual("abc", t.ToString(), "my_concat_atom failed!"); } }
public void t_compound_term() { Delegate d = new DelegateParameter3(compound_term); PlEngine.RegisterForeign(d); PlTerm t = PlQuery.PlCallQuery("compound_term(test(P, schmerz, arm), 1, PART)"); Assert.IsTrue(t.IsVar, "1 create_list failed!"); t = PlQuery.PlCallQuery("compound_term(test(P, schmerz, arm), 2, PART)"); Assert.AreEqual("schmerz", t.ToString(), "2 create_list failed!"); t = PlQuery.PlCallQuery("compound_term(test(P, schmerz, arm), 3, PART)"); Assert.AreEqual("arm", t.ToString(), "3 create_list failed!"); }
public void t_varargs() { Delegate d = new DelegateParameterVarArgs(my_concat_atom); PlEngine.RegisterForeign("my_concat_atom", 4, d); PlEngine.RegisterForeign("my_concat_atom", 7, d); for (int i = 1; i < 10; i++) { PlTerm t = PlQuery.PlCallQuery("my_concat_atom(a,b,c, X)"); Assert.AreEqual("abc", t.ToString(), "my_concat_atom failed!"); t = PlQuery.PlCallQuery("my_concat_atom(a,b,c,d,e,f, X)"); Assert.AreEqual("abcdef", t.ToString(), "my_concat_atom failed!"); } }
private void InitializeCallBack() { List <string> rulesToRemove = new List <string> { "callPariJoueur(_,_)", "callJouerCarte(_,_)", "callPlayManche", "callPlayManche2", "callPlayManche3", "callJoueurPioche", "callJoueurPioche2", "callPlayerPari", "callPlayerPari2", "callPlayTour(_)", "callPlayTour2(_)", "callPlayerJoue(_)" }; foreach (var item in rulesToRemove) { Debug.WriteLine( PlQuery.PlCall("retractall(" + item + ").")); } List <Delegate> collbacks = new List <Delegate> { new DelegateParameter2(callPariJoueur), new DelegateParameter2(callJouerCarte), new DelegateParameter0(callPlayManche), new DelegateParameter0(callPlayManche2), new DelegateParameter0(callPlayManche3), new DelegateParameter0(callJoueurPioche), new DelegateParameter0(callJoueurPioche2), new DelegateParameter0(callPlayerPari), new DelegateParameter0(callPlayerPari2), new DelegateParameter1(callPlayTour), new DelegateParameter1(callPlayTour2), new DelegateParameter1(callPlayerJoue), }; foreach (var item in collbacks) { PlEngine.RegisterForeign(item); // stock les delegates pour pas qu'ils soient bouffé par le garbage mesDelegate.Add(item); } }
public static void InternMethod(string module, string pn, MethodInfo minfo, object defaultInstanceWhenMissing) { if (minfo == null) { return; } if (!minfo.IsStatic && defaultInstanceWhenMissing == null) { throw new NotSupportedException(string.Format( "Interning a staic method without a target {0}:{1} -> {2}", module, pn, minfo)); } ExportedMethodInfos.Add(minfo); Type type = minfo.DeclaringType; pn = pn ?? (type.Name + "." + minfo.Name); if (ForceJanCase) { var pn2 = ToPrologCase(pn); if (pn2 != pn) { pn = pn2; } } ParameterInfo[] ps = minfo.GetParameters(); Type rt = minfo.ReturnType; int paramlen = ps.Length; bool nonvoid = rt != typeof(void); bool isbool = rt == typeof(bool); bool hasReturnValue = nonvoid && !isbool; bool isStatic = minfo.IsStatic; bool isVanilla = true; int maxOptionals = 0; foreach (ParameterInfo info in ps) { if (info.ParameterType != typeof(PlTerm)) { isVanilla = false; } if (IsOptionalParam(info)) { isVanilla = false; maxOptionals++; } } if (isbool && isStatic) { if (isVanilla) { RegisterInfo(pn, paramlen, minfo); Delegate d = null; switch (paramlen) { case 0: { d = new DelegateParameter0(() => (bool)InvokeCaught(minfo, null, ZERO_OBJECTS)); PlEngine.RegisterForeign(module, pn, paramlen, d, PlForeignSwitches.None); return; } case 1: PlEngine.RegisterForeign(module, pn, paramlen, new DelegateParameter1( (p1) => (bool)InvokeCaught(minfo, null, new object[] { p1 })), PlForeignSwitches.None); return; case 2: PlEngine.RegisterForeign(module, pn, paramlen, new DelegateParameter2( (p1, p2) => (bool)InvokeCaught(minfo, null, new object[] { p1, p2 })), PlForeignSwitches.None); return; case 3: PlEngine.RegisterForeign(module, pn, paramlen, new DelegateParameter3( (p1, p2, p3) => (bool)InvokeCaught(minfo, null, new object[] { p1, p2, p3 })), PlForeignSwitches.None); return; case 4: PlEngine.RegisterForeign(module, pn, paramlen, new DelegateParameter4( (p1, p2, p3, p4) => (bool)InvokeCaught(minfo, null, new object[] { p1, p2, p3, p4 })), PlForeignSwitches.None); return; case -5: // use the default please PlEngine.RegisterForeign(module, pn, paramlen, new DelegateParameter5( (p1, p2, p3, p4, p5) => (bool)InvokeCaught(minfo, null, new object[] { p1, p2, p3, p4, p5 })), PlForeignSwitches.None); return; default: break; } } } int plarity = paramlen + (hasReturnValue ? 1 : 0) + (isStatic ? 0 : 1); RegisterInfo(pn, plarity, minfo); DelegateParameterVarArgs del = GetDelV(minfo, type, nonvoid, isbool, isStatic, plarity, defaultInstanceWhenMissing); PlEngine.RegisterForeign(module, pn, plarity, del, PlForeignSwitches.VarArgs); while (maxOptionals > 0) { RegisterInfo(pn, plarity - maxOptionals, minfo); del = GetDelV(minfo, type, nonvoid, isbool, isStatic, plarity - maxOptionals, defaultInstanceWhenMissing); PlEngine.RegisterForeign(module, pn, plarity - maxOptionals, del, PlForeignSwitches.VarArgs); maxOptionals--; } }