public PrologCLR() { _singleInstance = this; ClientModule = null; ClientPrefix = "cli_"; SetupProlog(); }
/// <summary> /// Gets or sets the element with the specified key. /// </summary> /// <returns> /// The element with the specified key. /// </returns> /// <param name="key">The key of the element to get or set. /// </param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null. /// </exception><exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="key"/> is not found. /// </exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only. /// </exception> public TValue this[TKey key] { get { TValue tvalue = default(TValue); InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_getvalue, KeyToTerm(key), PlTerm.PlVar()); bool res = PlCall(_module, _getvalue, new PlTermV(newPlTermV)); if (res) { tvalue = (TValue)PrologCLR.CastTerm(newPlTermV.Arg(1), valueType); } else { // tvalue = default(TValue); } }); return(tvalue); } set { Remove(key); Add(new KeyValuePair <TKey, TValue>(key, value)); } }
public static void SetupProlog1() { PrologCLR.ConsoleTrace("geting lib path"); CLASSPATH = java.lang.System.getProperty("java.class.path"); ConsoleTrace("GOT lib path"); string CLASSPATH0 = Environment.GetEnvironmentVariable("CLASSPATH"); if (String.IsNullOrEmpty(CLASSPATH)) { CLASSPATH = CLASSPATH0; } string jplcp = clasPathOf(new jpl.JPL()); if (!JplDisabled) { CLASSPATH = IKVMHome + "/SWIJPL.dll" + ";" + IKVMHome + "/SWIJPL.jar;" + CLASSPATH0; } ConsoleWriteLine("CLASSPATH=" + CLASSPATH); if (CLASSPATH != null) { Environment.SetEnvironmentVariable("CLASSPATH", CLASSPATH); java.lang.System.setProperty("java.class.path", CLASSPATH); } java.lang.System.setProperty("java.library.path", libpath); }
private static string PlStringFormat(string text, params object[] ps) { RegisterCurrentThread(); try { if (ps != null && ps.Length > 0) { for (int i = 0; i < ps.Length; i++) { var o = ps[i]; if (o == null) { ps[i] = "NULL"; } else if (o is Exception) { ps[i] = PrologCLR.ExceptionString((Exception)o); } } text = String.Format(text, ps); } } catch (Exception) { } DeregisterThread(Thread.CurrentThread); return(text); }
//#pragma unsafe public override object CallPrologFast(object[] paramz) { //lock (oneEvtHandlerAtATime) { try { string module = Key.Module ?? "user"; PrologEvents++; if (!knownDefined && !PrologCLR.IsDefined(module, Key.Name, PrologArity)) { //PrologCLR.Warn("Undefined Event Handler {0}:{1}/{2}", module, Key.Name, PrologArity); return(null); } knownDefined = true; return(PrologCLR.CallProlog(this, module, Key.Name, PrologArity, Key.Origin, paramz, ReturnType, true)); } catch (AccessViolationException e) { PrologCLR.Warn("CallProlog: {0} ex: {1}", this, e); return(null); } catch (Exception e) { PrologCLR.Warn("CallProlog: {0} ex: {1}", this, e); return(null); } } }
public static PlTerm ValueToTerm(TValue value) { if (value.Equals(default(TValue))) { return(PlTerm.PlVar()); } return(PrologCLR.ToProlog(value)); }
public static PlTerm KeyToTerm(TKey key) { if (key.Equals(default(TValue))) { return(PlTerm.PlVar()); } return(PrologCLR.ToProlog(key)); }
public void Clear() { InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_querypred, new PlTermV(1)); PlCall(_module, _retractall, new PlTermV(newPlTermV)); }); }
public void RemoveRef() { Refs--; if (Refs == 0 && !Heaped) { PrologCLR.RemoveTaggedObject(TagName); } }
public object GetSymbol(string name) { PlTerm termout = PlTerm.PlVar(); if (!ModuleCall("GetSymbol", PlNamed(name), termout)) { return(null); } return(PrologCLR.CastTerm(termout, typeof(System.Object))); }
/// <summary> /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <returns> /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </returns> /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. /// </exception> public bool Remove(KeyValuePair <TKey, TValue> item) { bool removed = false; InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_getvalue, TermVOf(item)); removed = PlCall(_module, _retractPred, new PlTermV(newPlTermV)); }); return(removed); }
/// <summary> /// ?- closure(v(V1,V2),format('I was called with ~q ~q ...',[V1,V2]). /// </summary> /// <param name="info"></param> /// <param name="instance"></param> /// <param name="closureTerm"></param> public ClosureDelegate(EventInfo info, object instance, PlTerm clousreTerm) { Event = info; Instance = instance; SetInstanceOfDelegateType(info.EventHandlerType); PlTerm plC = PlTerm.PlVar(); PrologCLR.PlCall("system", "copy_term", new PlTermV(clousreTerm, plC)); this.closureTerm = libpl.PL_record(clousreTerm.TermRef); Event.AddEventHandler(instance, Delegate); }
public void Add(T item) { if (_assertPred == null) { throw new NotSupportedException("add " + this); } InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_querypred, new PlTermV(KeyToTerm(item))); PlCall(_module, _assertPred, new PlTermV(newPlTermV)); }); }
static public void EnsureStated() { if (PrologGenericDelegateThread == null) { PrologGenericDelegateThread = new Thread(PrologEventLoop); PrologGenericDelegateThread.Name = "PrologEventSerializer"; PrologGenericDelegateThread.TrySetApartmentState(ApartmentState.STA); PrologGenericDelegateThread.IsBackground = true; PrologCLR.RegisterThread(PrologGenericDelegateThread); PrologGenericDelegateThread.Start(); } }
/// <summary> /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. /// </exception> public void Add(KeyValuePair <TKey, TValue> item) { if (_assertPred == null) { throw new NotSupportedException("add " + this); } InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_getvalue, TermVOf(item)); PlCall(_module, _assertPred, new PlTermV(newPlTermV)); }); }
/// <summary> /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. /// </exception> public void Clear() { if (_retractall == null) { throw new NotSupportedException("clear " + this); } InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_getvalue, new PlTermV(2)); PlCall(_module, _retractall, new PlTermV(newPlTermV)); }); }
/// <summary> /// Advances the enumerator to the next element of the collection. /// </summary> /// <returns> /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection. /// </returns> /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. /// </exception><filterpriority>2</filterpriority> public bool MoveNext() { if (!plQuery.NextSolution()) { Dispose(); return(false); } nonLeft = false; PlTerm plQueryArgs = plQuery.Args[0]; currentValue = PrologCLR.CastTerm(plQueryArgs, _dictionary.keyType);; return(true); }
public void InForiegnFrame(Action action) { PrologCLR.RegisterCurrentThread(); uint fid = libpl.PL_open_foreign_frame(); try { action(); } finally { // if (fid > 0) libpl.PL_close_foreign_frame(fid); } }
public bool RemoveFrame(TrackedFrame frame) { if (CurrentTrackedFrame == frame) { PopFrame(); return(true); } else { if (PrologCLR.DebugRefs) { PrologCLR.Debug("Removing wierd frame{0}", frame); } frame.RemoveRefs(); return(false); } }
static public int install() { try { PrologCLR.IsPLWin = Type.GetType("Mono.Runtime") == null; PrologCLR.RedirectStreams = false; PrologCLR.SetupProlog(); PrologCLR.ConsoleWriteLine(typeof(Embedded).FullName + ".install suceeded"); PrologCLR.ClientReady = true; return(libpl.PL_succeed); } catch (Exception e) { PrologCLR.WriteException(e); PrologCLR.ConsoleWriteLine(typeof(Embedded).FullName + ".install failed"); return(libpl.PL_fail); } }
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))); }
public override object CallPrologFast(object[] paramz) { PrologCLR.RegisterCurrentThread(); var results = paramz; PlTerm plC = PlTerm.PlVar(); libpl.PL_recorded(closureTerm, plC.TermRef); PlTerm ctestVars = plC.Arg(0); PlTerm ctestCode = plC.Arg(1); PlTerm[] terms = PrologCLR.ToTermArray(ctestVars); int idx = terms.Length - 1; int resdex = results.Length - 1;; while (idx >= 0 && resdex >= 0) { terms[idx--].FromObject(results[resdex--]); } PrologCLR.PlCall("user", "call", new PlTermV(ctestCode, 1)); return(null); }
/// <summary> /// Gets the value associated with the specified key. /// </summary> /// <returns> /// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false. /// </returns> /// <param name="key">The key whose value to get. /// </param><param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized. /// </param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null. /// </exception> public bool TryGetValue(TKey key, out TValue value) { TValue value0 = default(TValue); bool res = false; InForiegnFrame(() => { PlTerm plTermPlVar = PlTerm.PlVar(); PlTermV newPlTermV = new PlTermV(KeyToTerm(key), plTermPlVar); res = PlCall(_module, _getvalue, newPlTermV); if (res) { value0 = (TValue)PrologCLR.CastTerm(newPlTermV[1], valueType); } else { } }); value = value0; return(res); }
//#pragma unsafe public override object CallPrologFast(object[] paramz) { //lock (oneEvtHandlerAtATime) { try { object arg1 = //Key.Origin; //makes sense for UseCallN this; PrologEvents++; if (UseCallN) { return(PrologCLR.CallProlog(this, Key.Module, "call", PrologArity, arg1, paramz, ReturnType, false)); } string module = Key.Module ?? "user"; PrologEvents++; if (!knownDefined && !PrologCLR.IsDefined(module, Key.Name, PrologArity)) { PrologCLR.Warn("Undefined Delegate Handler {0}:{1}/{2}", module, Key.Name, PrologArity); return(null); } knownDefined = true; return(PrologCLR.CallProlog(this, module, Key.Name, PrologArity, arg1, paramz, ReturnType, false)); } catch (AccessViolationException e) { PrologCLR.Warn("CallProlog: {0} ex: {1}", this, e); return(null); } catch (Exception e) { PrologCLR.Warn("CallProlog: {0} ex: {1}", this, e); return(null); } } }
public ArrayIndexEnumerator(Array value) { Type arrayType = value.GetType(); rank = arrayType.GetArrayRank(); uppers = new int[rank]; lowers = new int[rank]; idx = new int[rank]; len = 1; for (int i = 0; i < rank; i++) { int high = uppers[i] = value.GetUpperBound(i); int low = lowers[i] = value.GetLowerBound(i); if (low != 0) { PrologCLR.Error("LowerBound !=0 in " + arrayType); } int lenSize = (high - low + 1); len *= lenSize; } }
/// <summary> /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>. /// </summary> /// <returns> /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>. /// </returns> /// <param name="key">The key of the element to remove. /// </param><exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null. /// </exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only. /// </exception> public bool Remove(TKey key) { if (Keyz != null) { if (!Keyz.IsReadOnly) { return(Keyz.Remove(key)); } } if (_retractPred == null) { throw new NotSupportedException("remove " + this); } bool removed = false; InForiegnFrame(() => { PlTerm newPlTermV = PrologCLR.PlC(_getvalue, KeyToTerm(key), PlTerm.PlVar()); removed = PlCall(_module, _retractPred, new PlTermV(newPlTermV)); }); return(removed); }
public static object CallProlog(object target, string module, string name, int arity, object origin, object[] paramz, Type returnType, bool discard) { if (!ClientReady) { return(null); } return(InvokeFromC(() => { PlTermV args = NewPlTermV(arity); int fillAt = 0; if (origin != null) { args[fillAt++].FromObject(origin); } for (int i = 0; i < paramz.Length; i++) { args[fillAt++].FromObject(paramz[i]); } bool IsVoid = returnType == typeof(void); if (!IsVoid) { //args[fillAt] = PlTerm.PlVar(); } if (!PlQuery.PlCall(module, name, args)) { if (!IsVoid) { Warn("Failed Event Handler {0} failed", target); } } if (IsVoid) { return null; } object ret = PrologCLR.CastTerm(args[fillAt], returnType); return ret; }, discard)); }
static public Type ToType(PlTerm typeSpec) { return(PrologCLR.GetType(typeSpec)); }
public override sealed void Init(object self) { lock (GroupInitLock) { prologClient = prologClient ?? new PrologCLR(); if (!IsInited) { IsInited = true; prologClient.InitFromUser(); } } }
private PlTerm C(string collection) { return(PrologCLR.C(collection)); }
public bool ConsultIfExists(string file) { return(PrologCLR.InvokeFromC(() => File.Exists(file) && Consult(file), false)); }