


swicli.pl -- SWI-Prolog 2-Way interface to .NET/MonoIntroduction
This is an overview of an interface which allows SWI-Prolog programs to dynamically create and manipulate .NET objects.
Here are some significant features of the interface and its implementation:
?- use_module(library(swicli)).
?- cli_call('System.Threading.ThreadPool','GetAvailableThreads'(X,Y),_).
X=499, Y=1000
?- cli_call('System.Environment','Version',X),cli_writeln(X). "2.0.50727.5448" X = @'C#499252128'.
== Doc root will be findable from http://code.google.com/p/opensim4opencog/wiki/SwiCLI
@see CSharp.txt
cli_load_lib(+AppDomainName, +AssemblyPartialName, +FullClassName, +StaticMethodName)cli_load_lib/4 is what was used to bootstrap SWICLI (it defined the next stage where cli_load_assembly/1) became present
remember to: export LD_LIBRARY_PATH=/development/opensim4opencog/bin:$LD_LIBRARY_PATH
in swicli.pl we called:
:- cli_load_lib('SWIProlog','Swicli.Library','Swicli.Library.Embedded','install').
cli_lib_type(-LibTypeName)
cli_load_assembly(+AssemblyPartialNameOrPath)
cli_load_assembly_uncaught(+AssemblyPartialNameOrPath)?- cli_load_assembly('Swicli.Library').
The uncaught version allows exception to come from .NET
cli_load_assembly_methods(+AssemblyPartialNameOrPath, +OnlyPrologVisible, +StringPrefixOrNull)?- cli_load_assembly_methods('Swicli.Library', @false, "cli_").
cli_add_assembly_search_path(+Path)
cli_remove_assembly_search_path(+Path)?- cli_add_assembly_search_path('c:/myproj/bin').
?- cli_remove_assembly_search_path('c:/myproj/bin').
This now makes the System assembly resolver see Assemblies in that directory
Simular to Windows: adding to %PATH% Linux: adding to $MONO_PATH
cli_non_obj(+Obj)
cli_non_null(+Obj)
cli_is_null(+Obj)
cli_null(+Obj)
cli_is_true(+Obj)
cli_true(+Obj)
cli_is_false(+Obj)
cli_false(+Obj)
cli_is_void(+Obj)
cli_void(+Obj)
cli_is_type(+Obj)
cli_is_object(+Obj)
cli_is_tagged_object(+Obj)
cli_is_ref(+Obj)
cli_memb(O, F, X)
cli_is_type(+Impl, ?Type)
cli_subclass(+Subclass, +Superclass)
cli_typespec(+ClazzSpec, -Value)
cli_add_tag(+RefObj, +TagString)
cli_remove_tag(+TagString)
cli_to_ref(+Obj, +Ref)15 ?- cli_to_ref(sbyte(127),O),cli_get_type(O,T),cli_writeln(O is T). "127"is"System.SByte" O = @'C#283319280', T = @'C#283324332'. 16 ?- cli_to_ref(long(127),O),cli_get_type(O,T),cli_writeln(O is T). "127"is"System.Int64" O = @'C#283345876', T = @'C#283345868'. 17 ?- cli_to_ref(ulong(127),O),cli_get_type(O,T),cli_writeln(O is T). "127"is"System.UInt64" O = @'C#283346772', T = @'C#283346760'. 15 ?- cli_to_ref(sbyte(127),O),cli_get_type(O,T),cli_writeln(O is T). "127"is"System.SByte" O = @'C#283319280', T = @'C#283324332'. 16 ?- cli_to_ref(long(127),O),cli_get_type(O,T),cli_writeln(O is T). "127"is"System.Int64" O = @'C#283345876', T = @'C#283345868'. 18 ?- cli_to_ref(343434127,O),cli_get_type(O,T),cli_writeln(O is T). "343434127"is"System.Int32" O = @'C#281925284', T = @'C#281925280'. 19 ?- cli_to_ref(3434341271,O),cli_get_type(O,T),cli_writeln(O is T). "3434341271"is"System.UInt64" O = @'C#281926616', T = @'C#283346760'. 21 ?- cli_to_ref(343434127111,O),cli_get_type(O,T),cli_writeln(O is T). "343434127111"is"System.UInt64" O = @'C#281930092', T = @'C#283346760'. 28 ?- cli_to_ref(34343412711111111111111111111111111111,O),cli_get_type(O,T),cli_writeln(O is T). "34343412711111111111111111111111111111"is"java.math.BigInteger" O = @'C#281813796', T = @'C#281810860'.
cli_to_immediate(+Ref, -Immediate)
cli_cast(+Value, +ClazzSpec, -Ref)
cli_cast_immediate(+Value, +ClazzSpec, -Immediate)?- cli_cast(1,'double',X).
X = @'C#568261440'.
?- cli_cast(1,'System.DayOfWeek',X).
X = @'C#568269000'.
?- cli_cast_immediate(1,'System.DayOfWeek',X).
X = enum('DayOfWeek', 'Monday').
?- cli_cast_immediate(1.0,'System.DayOfWeek',X).
X = enum('DayOfWeek', 'Monday').
?- cli_cast_immediate(1.01,'System.DayOfWeek',X).
ERROR: Having time of it convcerting 1.01 to System.DayOfWeek why System.ArgumentException: Requested value '1.01' was not found.
cli_tracker_begin(-Tracker)
cli_tracker_free(+Tracker)
cli_free(+RefObject)
cli_heap(+RefObject)
cli_with_gc(+Call)
cli_with_lock(+Lock, +Call)
cli_lock_enter(+LockObj)
cli_lock_exit(+LockObj)
cli_write(+Obj)
cli_writeln(+Obj)
to_string(+Obj, -String)
cli_to_str(+Obj, -String)
cli_to_str_raw(+Obj, -String)
cli_java_to_string(+Obj, -Value)
cli_halt
cli_halt(+Obj)
cli_throw(+Ex)
cli_break(+Ex)
cli_col(+Col, -Elem)
cli_enumerator_element(+Enumer, -Elem)
cli_iterator_element(+Iter, -Elem)
?- cli_new('System.Collections.Generic.List'('System.String'),[int],[10],Obj).
Obj = @'C#516939544'.
?- cli_get($Obj,'Count',Out).
Out = 0.
?- cli_call($Obj,'Add'("foo"),Out).
Out = @void.
?- cli_call($Obj,'Add'("bar"),Out).
Out = @void.
?- cli_get($Out,'Count',Out).
Out = 2.
?- cli_col($Obj,E).
E = "foo" ;
E = "bar" ;
false.
cli_col_add(+Col, +Item)
cli_col_contains(+Col, +Item)
cli_col_remove(+Col, +Item)
cli_col_removeall(+Col)
cli_col_size(+Col, ?Count)
cli_new_prolog_collection(+PredImpl, +ElementType, -PBD)
cli_make_list(+Obj, +Arg2, +Arg3)
cli_new_list_1(+Obj, +Arg2, +Arg3)
cli_sublist(+Mask, +List)
cli_array_to_list(+Obj, +Arg2)
cli_array_to_term(+ArrayValue, -Value)
cli_array_to_termlist(+ArrayValue, -Value)
cli_term_to_array(+ArrayValue, -Value)
cli_array_to_term_args(+Array, -Term)
cli_map(Map, ?Key, ?Value)
cli_map_add(+Map, +Key, +Value)
cli_map_set(+Map, +Key, +Value)
cli_map_remove(+Map, +Key)
cli_map_remove(+Map, ?Key, ?Value)
cli_map_removeall(+Map)
cli_map_size(+Map, -Count)
cli_preserve(TF, :Call)
member_elipse(Ele, Elipse)?- member_elipse(E,{a,b,c}).
E = a ;
E = b ;
E = c.
cli_to_data(+Ref, -Term)?- cli_cast("Yellow",'System.Drawing.Color',C),cli_to_data(C,D),writeq(D).
["R"=255,"G"=255,"B"=0,"A"=255,"IsKnownColor"= @true,"IsEmpty"= @false,"IsNamedColor"= @true,"IsSystemColor"= @false,"Name"="Yellow"]
C = @'C#802963000',
D = ["R"=255, "G"=255, "B"=0, "A"=255, "IsKnownColor"= @true, "IsEmpty"= @false, "IsNamedColor"= @true, "IsSystemColor"= @ ..., ... = ...].
cli_unify(OE, PE)
cli_new(+ClassNameWithParams, -Result)
cli_new(+ClazzSpec, +Params, -Result)
cli_new(+ClazzSpec, +MemberSpec, +Params, -Result)?- cli_load_assembly('IKVM.OpenJDK.Core')
?- cli_new('java.lang.Long'(long),[44],Out),cli_to_str(Out,Str).
same as..
?- cli_new('java.lang.Long',[long],[44],Out),cli_to_str(Out,Str).
arity 4 exists to specify generic types
?- cli_new('System.Int64',[int],[44],Out),cli_to_str(Out,Str).
?- cli_new('System.Text.StringBuilder',[string],["hi there"],Out),cli_to_str(Out,Str).
?- cli_new('System.Int32'(int),[44],Out),cli_to_str(Out,Str).
ClazzSpec can be:
if ClazzSpec is an object (non-array) type or descriptor and Params is a list of values or references, then Result is the result of an invocation of that type's most specifically-typed constructor to whose respective formal parameters the actual Params are assignable (and assigned)
if ClazzSpec is an array type or descriptor and Params is a list of values or references, each of which is (independently) assignable to the array element type, then Result is a new array of as many elements as Params has members, initialised with the respective members of Params;
if ClazzSpec is an array type or descriptor and Params is a non-negative integer N, then Result is a new array of that type, with N elements, each initialised to CLR's appropriate default value for the type;
If Result is {Term} then we attempt to convert a new PlTerm instance to a corresponding term; this is of little obvious use here, but is consistent with cli_call/4 and cli_get/3
Make a "new string[32]" and get it's length.
?- cli_new(array(string),[int],[32],O),cli_get(O,'Length',L).
cli_call(+ClazzOrInstance, +CallTerm, -Result)
cli_call(+ClazzOrInstance, +MethodSpec, +Params, -Result)
cli_call_raw(+ClazzOrInstance, +MethodSpec, +Params, -Result)
cli_raise_event_handler(+ClazzOrInstance, +MemberSpec, +Params, -Result)MethodSpec should be:
Params should be:
CallTerm should be:
finally, an attempt will be made to unify Result with the returned result
cli_lib_call(+CallTerm, -Result)finally, an attempt will be made to unify Result with the returned result
cli_set(+Obj, +NameValueParis:list)
cli_get(+Obj, +NameValueParis:list)
cli_get(+ClazzOrInstance, +MemberSpec, -Value)
cli_set(+ClazzOrInstance, +MemberSpec, +Value)
cli_get_raw(+ClazzOrInstance, +MemberSpec, -Value)
cli_set_raw(+ClazzOrInstance, +MemberSpec, +Value)
cli_set_property(+ClazzOrInstance, +MemberSpec, +IndexValues, +Value)
cli_get_property(+ClazzOrInstance, +MemberSpec, +IndexValues, -Value)MemberSpec can be:
IndexValues can be:
Value:
cli_new_event_waiter(+ClazzOrInstance, +MemberSpec, -WaitOn)
cli_add_event_waiter(+WaitOn, +ClazzOrInstance, +MemberSpec, -NewWaitOn)
cli_block_until_event(+WaitOn, +Time, +Lambda)
cli_block_until_event(+WaitOn, +MaxTime, +TestVarsCode, -ExitCode)
cli_add_event_handler(+Term1, +Arity, +IntPtrControl, Pred)
cli_add_event_handler(+ClazzOrInstance, +MemberSpec, +PrologPred)
cli_remove_event_handler(+ClazzOrInstance, +MemberSpec, +PrologPred)
cli_hide(+Pred)
cli_notrace(+Call) is nondet
cli_class_from_type(+Value, -Value)
cli_find_class(+ClazzName, -ClazzObject)
cli_find_type(+ClazzSpec, +ClassRef)
cli_get_class(+Value, -Value)
cli_get_classname(+Value, -Value)
cli_get_type(+Value, -Value)
cli_get_type_fullname(+Value, -Value)
cli_type_from_class(+Value, -Value)
cli_new_delegate_term(+TypeFi, +PrologPred, +BooleanSaveKey, -Delegate)
cli_getterm(+ValueCol, +Value, -Value)
cli_new_array(+ClazzSpec, +Rank, -Value)
cli_new_delegate(+DelegateClass, +PrologPred, -Value)
cli_is_layout(+MemberSpec)
cli_add_layout(+ClazzSpec, +MemberSpec)
cli_add_layout(+ClazzSpec, +MemberSpec, +ToSpec)
cli_add_recomposer(+ClazzSpec, +MemberSpec, +Obj2r, +R2obj)
cli_find_constructor(+ClazzSpec, +MemberSpec, -Method)
cli_find_method(+ClazzOrInstance, +MemberSpec, -Method)
cli_member_doc(+Memb, +Doc, +Xml)
cli_members(+ClazzOrInstance, -Members)
cli_add_shorttype(+Short, +Long)
cli_props_for_type(+ClazzSpec, +MemberSpecs)
cli_new_array(Arg1, Arg2, Arg3)
cli_array_fill(Arg1, Arg2)
cli_array_fill_values(Arg1, Arg2)
cli_special_unify(Arg1, Arg2)
cli_fmt(Arg1, Arg2, Arg3)
cli_intern(Arg1, Arg2, Arg3)
cli_memb(Arg1, Arg2)
cli_to_data(Arg1, Arg2, Arg3)
cli_new_prolog_dictionary(Arg1, Arg2, Arg3, Arg4)
cli_set_element(Arg1, Arg2, Arg3)
cli_add_foreign_methods(Arg1, Arg2, Arg3)
cli_array_to_length(Arg1, Arg2)
cli_get_symbol(Arg1, Arg2, Arg3)
cli_add_element(Arg1, Arg2)
cli_debug(Arg1)
cli_expanded(Arg1, Arg2)
module_functor(Arg1, Arg2, Arg3, Arg4)
cli_link_swiplcs(Arg1)
cli_make_default(Arg1, Arg2)
cli_subproperty(Arg1, Arg2)
cli_demo(Arg1, Arg2)
cli_expand(Arg1, Arg2)
cli_set_hook(Arg1, Arg2, Arg3)
cli_get_hook(Arg1, Arg2, Arg3)
cli_fmt(Arg1, Arg2)
cli_is_defined(Arg1, Arg2)
cli_eval(Arg1, Arg2, Arg3)
cli_eval_hook(Arg1, Arg2, Arg3)
cli_interned(Arg1, Arg2, Arg3)
cli_test_array_to_term1(-Value)
cli_test_array_to_term2(-Value)
cli_test_opt(+Incoming, ?REFInt32Outbound)
cli_test_opt(+Incoming, +StringOptionalstr, ?REFInt32Outbound)
cli_test_out(+Incoming, ?REFInt32Outbound)
cli_test_pbc(+Pred, +Counted)
cli_test_pbct(+Pred, +Counted)
cli_test_pbd(+Pred, +Counted)
cli_test_pbdt(+Pred, +Counted)
cli_test_ref(+Incoming, ?REFInt32Outbound)
cli_test_ref(+Incoming, ?REFStringOptionalstr, ?REFInt32Outbound)
cli_test_var_arg(?REFInt32Outbound, +ArrayOfInt32Incoming)