public static bool cliToStrRaw(PlTerm obj, PlTerm str) { try { if (!str.IsVar) { var plvar = PlTerm.PlVar(); return(cliToStrRaw(obj, plvar) && SpecialUnify(str, plvar)); } if (obj.IsString) { return(str.Unify(obj)); } if (obj.IsVar) { return(str.Unify((string)obj)); } object o = GetInstance(obj); if (o == null) { return(str.FromObject("" + obj)); } return(str.FromObject(ToString(o))); } catch (Exception e) { Warn("cliToString: {0}", e); object o = GetInstance(obj); if (o == null) { return(str.FromObject("" + obj)); } return(str.FromObject(ToString(o))); } }
static public bool cliJavaToString(PlTerm paramIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliJavaToString(paramIn, plvar) && SpecialUnify(valueOut, plvar)); } object getInstance = GetInstance(paramIn); if (getInstance == null) { return(valueOut.Unify(PlTerm.PlString("null"))); } #if USE_IKVM object val = getInstance as java.lang.Object; if (val == null) { Class c = ikvm.runtime.Util.getClassFromObject(getInstance); string s = (string)c.getMethod("toString", new Class[0]).invoke(getInstance, ZERO_OBJECTS); return(valueOut.Unify(PlTerm.PlString(s))); } return(valueOut.Unify(PlTerm.PlString(val.toString()))); #else object val = getInstance; return(valueOut.Unify(PlTerm.PlString(val.ToString()))); #endif }
public static int ToFieldLayout(string named, string arg1, object o, Type t, PlTerm term, bool childs, bool addNames) { MemberInfo[] tGetFields = GetStructFormat(t); int len = tGetFields.Length; PlTermV tv = NewPlTermV(len + 1); tv[0].UnifyAtom(arg1); int tvi = 1; for (int i = 0; i < len; i++) { object v = GetMemberValue(tGetFields[i], o); if (v is IList) { v.GetType(); } tv[tvi++].FromObject((v)); } if (true) { return(PlSucceedOrFail(term.Unify(PlC(named, tv)))); } uint termTermRef = term.TermRef; uint temp = libpl.PL_new_term_ref(); libpl.PL_cons_functor_v(temp, libpl.PL_new_functor(libpl.PL_new_atom(named), tv.Size), tv.A0); return(libpl.PL_unify(termTermRef, temp)); }
static public bool cliMemberDoc(PlTerm membIn, PlTerm docOut, PlTerm xmlOut) { var mi = GetInstance(membIn) as MemberInfo; if (mi != null) { XElement xmls = GetDocString(mi); return(xmlOut.Unify(ToProlog(xmls)) && docOut.Unify(PlTerm.PlString(xmls == null ? "" : xmls.InnerXml))); } return(true); }
static public bool cliGetClassname(PlTerm valueIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliGetClassname(valueIn, plvar) && SpecialUnify(valueOut, plvar)); } Type val = CastTerm(valueIn, typeof(Type)) as Type; if (val == null) { return(false); } #if USE_IKVM return(valueOut.Unify(val.getName())); #else return(valueOut.Unify(val.GetType().Name)); #endif }
private static int ToVMNumber(object o, PlTerm term) { if (o is int) { return(libpl.PL_unify_integer(term.TermRef, (int)Convert.ToInt32(o))); } if (PreserveObjectType) { return(PlSucceedOrFail(UnifyTagged(o, term))); } // signed types if (o is short || o is sbyte) { return(libpl.PL_unify_integer(term.TermRef, (int)Convert.ToInt32(o))); } if (o is long) { return(libpl.PL_unify_integer(term.TermRef, (long)Convert.ToInt64(o))); } if (o is decimal || o is Single || o is float || o is double) { return(libpl.PL_unify_float(term.TermRef, (double)Convert.ToDouble(o))); } // unsigned types if (o is ushort || o is byte) { return(libpl.PL_unify_integer(term.TermRef, (int)Convert.ToInt32(o))); } if (o is UInt32) { return(libpl.PL_unify_integer(term.TermRef, (long)Convert.ToInt64(o))); } // potentually too big?! if (o is ulong) { ulong u64 = (ulong)o; if (u64 <= Int64.MaxValue) { return(libpl.PL_unify_integer(term.TermRef, (long)Convert.ToInt64(o))); } return(PlSucceedOrFail(term.Unify(u64))); //return libpl.PL_unify_float(term.TermRef, (double)Convert.ToDouble(o)); } if (o is IntPtr) { return(libpl.PL_unify_intptr(term.TermRef, (IntPtr)o)); } if (o is UIntPtr) { return(libpl.PL_unify_intptr(term.TermRef, (IntPtr)o)); } return(-1); }
public static bool unifyArrayToTerm(Array value, PlTerm valueOut) { int len = value.Length; Type arrayType = value.GetType(); var termv = NewPlTermV(len); int rank = arrayType.GetArrayRank(); if (rank != 1) { var indexesv = new PlTermV(rank); for (int i = 0; i < rank; i++) { indexesv[i].Put(value.GetLength(i)); } var idxIter = new ArrayIndexEnumerator(value); int putAt = 0; while (idxIter.MoveNext()) { bool pf = termv[putAt++].FromObject((value.GetValue(idxIter.Current))); if (!pf) { return(false); } } return /// array/3 (valueOut.Unify(PlC("array", typeToSpec(arrayType), PlC("indexes", indexesv), PlC("values", termv)))); } else { for (int i = 0; i < len; i++) { bool pf = termv[i].FromObject((value.GetValue(i))); if (!pf) { return(false); } } return(valueOut.Unify(PlC("array", typeToSpec(arrayType.GetElementType()), PlC("values", termv)))); } }
public static bool cliArrayToTermlist(PlTerm arrayValue, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliArrayToTermlist(arrayValue, plvar) && SpecialUnify(valueOut, plvar)); } object getInstance = GetInstance(arrayValue); if (getInstance == null) { return(valueOut.Unify(PLNULL)); } Array value = GetArrayValue(getInstance); if (value == null) { Error("Cant find array from {0} as {1}", arrayValue, getInstance.GetType()); return(false); } Type arrayType = value.GetType(); if (arrayType.GetArrayRank() != 1) { Error("Non rank==1 " + arrayType); } int len = value.Length; var termv = ATOM_NIL; for (int i = len - 1; i >= 0; i--) { termv = PlC(".", ToProlog((value.GetValue(i))), termv); } //Type et = value.GetType().GetElementType(); return(valueOut.Unify(termv)); }
static public bool cliTypeToFullname(PlTerm valueIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliTypeToFullname(valueIn, plvar) && SpecialUnify(valueOut, plvar)); } Type val = CastTerm(valueIn, typeof(Type)) as Type; if (val == null) { return(false); } return(valueOut.Unify(val.FullName)); }
static public bool cliGetterm(PlTerm valueCol, PlTerm valueIn, PlTerm valueOut) { List <object> objs; if (valueCol.IsVar) { objs = new List <object>(); valueCol.FromObject(objs); } else { objs = (List <object>)CastTerm(valueCol, typeof(ICollection)); } if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliGetterm(valueCol, valueIn, plvar) && SpecialUnify(valueOut, plvar)); } if (IsTaggedObject(valueIn)) { object val = GetInstance(valueIn); int index = objs.IndexOf(val); if (index < 0) { index = objs.Count; objs.Add(val); var type = val.GetType(); if (type.IsArray) { return(valueIn.Unify(valueOut)); } return(ToFieldLayout("object", typeToName(type), val, type, valueOut, false, false) != libpl.PL_fail); } } return(valueIn.Unify(valueOut)); }
static public bool cliPropsForType(PlTerm clazzSpec, PlTerm memberSpecList) { Type type = GetType(clazzSpec); var props = GetPropsForTypes(type); var value = props.Key.ToArray(); int len = value.Length; var termv = ATOM_NIL; for (int i = len - 1; i >= 0; i--) { termv = PlC(".", ToProlog((value[i].Name)), termv); } var value2 = props.Value.ToArray(); len = value2.Length; for (int i = len - 1; i >= 0; i--) { termv = PlC(".", ToProlog((value2[i].Name)), termv); } return memberSpecList.Unify(termv); }
public object Eval(object obj) { PlTerm termin = PlTerm.PlVar(); if (obj is PlTerm) { termin.Unify((PlTerm)obj); } else { termin.FromObject(obj); } PlTerm termout = PlTerm.PlVar(); if (!ModuleCall("Eval", termin, termout)) { return(null); } return(PrologCLR.CastTerm(termout, typeof(System.Object))); }
static public bool cliPropsForType(PlTerm clazzSpec, PlTerm memberSpecList) { Type type = GetType(clazzSpec); var props = GetPropsForTypes(type); var value = props.Key.ToArray(); int len = value.Length; var termv = ATOM_NIL; for (int i = len - 1; i >= 0; i--) { termv = PlC(".", ToProlog((value[i].Name)), termv); } var value2 = props.Value.ToArray(); len = value2.Length; for (int i = len - 1; i >= 0; i--) { termv = PlC(".", ToProlog((value2[i].Name)), termv); } return(memberSpecList.Unify(termv)); }
public static bool cliArrayToTerm(PlTerm arrayValue, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return(cliArrayToTerm(arrayValue, plvar) && SpecialUnify(valueOut, plvar)); } object getInstance = GetInstance(arrayValue); if (getInstance == null) { return(valueOut.Unify(PLNULL)); } Array value = GetArrayValue(getInstance); if (value == null) { Error("Cant find array from {0} as {1}", arrayValue, getInstance.GetType()); return(false); } return(unifyArrayToTerm(value, valueOut)); }
static public bool cliJavaToString(PlTerm paramIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return cliJavaToString(paramIn, plvar) && SpecialUnify(valueOut, plvar); } object getInstance = GetInstance(paramIn); if (getInstance == null) return valueOut.Unify(PlTerm.PlString("null")); #if USE_IKVM object val = getInstance as java.lang.Object; if (val == null) { Class c = ikvm.runtime.Util.getClassFromObject(getInstance); string s = (string)c.getMethod("toString", new Class[0]).invoke(getInstance, ZERO_OBJECTS); return valueOut.Unify(PlTerm.PlString(s)); } return valueOut.Unify(PlTerm.PlString(val.toString())); #else object val = getInstance; return valueOut.Unify(PlTerm.PlString(val.ToString())); #endif }
static public bool cliMembers(PlTerm clazzOrInstance, PlTerm membersSpecListOut) { object getInstance; Type c; if (!GetInstanceAndType(clazzOrInstance, out getInstance, out c)) { return(false); } MemberInfo[] members = c.GetMembers(BindingFlagsALL); List <PlTerm> list = new List <PlTerm>(); string cname = c.Name; List <MemberInfo> exclude = new List <MemberInfo>(); int ordinal = 0; foreach (var info in c.GetFields(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetProperties(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetMethods(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetConstructors(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetEvents(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } foreach (MemberInfo info in members) { break; try { if (exclude.Contains(info)) { continue; } } catch (Exception e) { Debug("Warn exclude.Contains {0}: {1}", info, e); continue; } AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } return(membersSpecListOut.Unify(ToPlList(list.ToArray()))); }
static public bool cliGetClassname(PlTerm valueIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return cliGetClassname(valueIn, plvar) && SpecialUnify(valueOut, plvar); } Type val = CastTerm(valueIn, typeof(Type)) as Type; if (val == null) return false; #if USE_IKVM return valueOut.Unify(val.getName()); #else return valueOut.Unify(val.GetType().Name); #endif }
public static bool cliToStrRaw(PlTerm obj, PlTerm str) { try { if (!str.IsVar) { var plvar = PlTerm.PlVar(); return cliToStrRaw(obj, plvar) && SpecialUnify(str, plvar); } if (obj.IsString) return str.Unify(obj); if (obj.IsVar) return str.Unify((string)obj); object o = GetInstance(obj); if (o == null) return str.FromObject("" + obj); return str.FromObject(ToString(o)); } catch (Exception e) { Warn("cliToString: {0}", e); object o = GetInstance(obj); if (o == null) return str.FromObject("" + obj); return str.FromObject(ToString(o)); } }
public static bool cliMembers(PlTerm clazzOrInstance, PlTerm membersSpecListOut) { object getInstance; Type c; if (!GetInstanceAndType(clazzOrInstance, out getInstance, out c)) return false; MemberInfo[] members = c.GetMembers(BindingFlagsALL); List<PlTerm> list = new List<PlTerm>(); string cname = c.Name; List<MemberInfo> exclude = new List<MemberInfo>(); int ordinal = 0; foreach (var info in c.GetFields(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetProperties(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetMethods(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetConstructors(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } ordinal = 0; foreach (var info in c.GetEvents(BindingFlagsALL)) { AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } foreach (MemberInfo info in members) { break; try { if (exclude.Contains(info)) continue; } catch (Exception e) { Embedded.Debug("Warn exclude.Contains {0}: {1}", info, e); continue; } AddMemberToList(info, list, cname, ordinal++); exclude.Add(info); } return membersSpecListOut.Unify(ToPlArray(list.ToArray())); }
static public bool cliTypeToFullname(PlTerm valueIn, PlTerm valueOut) { if (!valueOut.IsVar) { var plvar = PlTerm.PlVar(); return cliTypeToFullname(valueIn, plvar) && SpecialUnify(valueOut, plvar); } Type val = CastTerm(valueIn, typeof(Type)) as Type; if (val == null) return false; return valueOut.Unify(val.FullName); }
public static bool cliMemberDoc(PlTerm membIn, PlTerm docOut, PlTerm xmlOut) { var mi = GetInstance(membIn) as MemberInfo; if (mi != null) { XElement xmls = GetDocString(mi); return xmlOut.Unify(ToProlog(xmls)) && docOut.Unify(PlTerm.PlString(xmls == null ? "" : xmls.InnerXml)); } return true; }
static public bool cliTypeToTypespec(PlTerm clazzSpec, PlTerm valueOut) { return valueOut.Unify(typeToSpec(GetType(clazzSpec))); }
static public bool cliTypeToTypespec(PlTerm clazzSpec, PlTerm valueOut) { return(valueOut.Unify(typeToSpec(GetType(clazzSpec)))); }
private static bool SpecialUnify(PlTerm valueOut, PlTerm plvar) { bool b = valueOut.Unify(plvar); if (b) return true; object obj1 = GetInstance(plvar); if (ReferenceEquals(obj1, null)) { return false; } Type t1 = obj1.GetType(); object obj2 = CastTerm(valueOut, t1); if (ReferenceEquals(obj2, null)) { return false; } Type t2 = obj2.GetType(); if (obj1.Equals(obj2)) { return true; } if (t1 == t2) { return false; } return false; }
public static int UnifyToPrologImmediate(object o, PlTerm term) { uint TermRef = term.TermRef; if (o is PlTerm) { return libpl.PL_unify(TermRef, ((PlTerm)o).TermRef); } if (o is string) { string s = (string)o; switch (VMStringsAsAtoms) { case libpl.CVT_STRING: { try { return libpl.PL_unify_string_chars(TermRef, (string)o); } catch (Exception) { return UnifyAtom(TermRef, s); } } case libpl.CVT_ATOM: try { return libpl.PL_unify_atom_chars(TermRef, (string)o); } catch (Exception) { return UnifyAtom(TermRef, s); } case libpl.CVT_LIST: return libpl.PL_unify_list_chars(TermRef, (string)o); default: Warn("UNKNOWN VMStringsAsAtoms {0}", VMStringsAsAtoms); return libpl.PL_fail; } } if (o == null) { return AddTagged(TermRef, "null"); } if (o is Type || o is Type) { if (true) { //lock (ToFromConvertLock) { var tag = object_to_tag(o); AddTagged(TermRef, tag); return libpl.PL_succeed; } } return PlSucceedOrFail(term.Unify(typeToSpec((Type)o))); } Type t = o.GetType(); if (t == typeof(void)) { return AddTagged(TermRef, "void"); } if (o is ValueType) { if (o is bool) { bool tf = (bool)o; return AddTagged(TermRef, tf ? "true" : "false"); } if (o is char) { try { char ch = (char)o; string cs = new string(ch, 1); switch (VMStringsAsAtoms) { case libpl.CVT_STRING: return libpl.PL_unify_atom_chars(TermRef, cs); case libpl.CVT_ATOM: return libpl.PL_unify_atom_chars(TermRef, cs); case libpl.CVT_LIST: return libpl.PL_unify_integer(TermRef, (int)ch); default: Warn("UNKNOWN VMStringsAsAtoms {0}", VMStringsAsAtoms); return libpl.PL_fail; } } catch (Exception e) { Warn("@TODO unmappable errors? {0} type {1}", o, t); // } } if (t.IsEnum) { int res = FromEnum(TermRef, o, t); ///term.ToString(); return res; } if (t.IsPrimitive) { try { int res = ToVMNumber(o, term); if (res == libpl.PL_succeed) return res; if (res == libpl.PL_fail) return res; if (res != -1) { // Warn("@TODO Missing code for ToVmNumber? " + o + " type " + t); return res; } if (t.IsPrimitive) { Warn("@TODO Missing code for primitive? {0} type {1}", o, t); } } catch (Exception e) { Warn("@TODO unmappable errors? {0} type {1}", o, t); } } } lock (FunctorToLayout) { PrologTermLayout layout; if (TypeToLayout.TryGetValue(t, out layout)) { MemberInfo[] tGetFields = layout.FieldInfos;// GetStructFormat(t); int len = tGetFields.Length; PlTermV tv = NewPlTermV(len); for (int i = 0; i < len; i++) { object v = GetMemberValue(tGetFields[i], o); tv[i].FromObject((v)); } return PlSucceedOrFail(term.Unify(PlC(layout.Name, tv))); } } lock (FunctorToRecomposer) { PrologTermRecomposer layout = GetTypeMap(t, TypeToRecomposer); if (layout != null) { lock (ToFromConvertLock) { var tag = object_to_tag(o); uint newref = libpl.PL_new_term_refs(2); AddTagged(newref, tag); PlTerm into = new PlTerm(newref); PlTerm outto = new PlTerm(newref + 1); var ret = PlQuery.PlCall(layout.module, layout.obj2r, new PlTermV(into, outto)); if (ret) { return term.Unify(outto) ? libpl.PL_succeed : libpl.PL_fail; } } } } if (o is IList) { } if (IsStructRecomposable(t)) { return ToFieldLayout("struct", typeToName(t), o, t, term, false, false); } if (o is EventArgs) { return ToFieldLayout("event", typeToName(t), o, t, term, false, false); } if (t.IsArray) { Array al = (Array) o; if (false && al.Length > 0 && al.Length < 1024) { Type et = t.GetElementType(); object firstNonNull = null; foreach (var ele in al) { if (ele!=null) { firstNonNull = ele; } } var needMake = firstNonNull != null; if (needMake) { PlTerm newVar = PlTerm.PlVar(); needMake = NeedsToMakeRef(firstNonNull, newVar); } if (!needMake) { return PlSucceedOrFail(unifyArrayToTerm(al, term)); } } } return PlObject(TermRef, o); }