Ejemplo n.º 1
0
 /// <summary>
 /// Initialize from given metatadata, plus first, restFn, rest.
 /// </summary>
 /// <param name="meta">The metadata to attach</param>
 /// <param name="first">The first of the sequence.</param>
 /// <param name="restFn">The function to generate the next value.</param>
 /// <param name="rest">The rest of the sequence..</param>
 FnSeq(IPersistentMap meta, object first, IFn restFn, ISeq rest)
     : base(meta)
 {
     _first = first;
     _restFn = restFn;
     _rest = rest;
 }
Ejemplo n.º 2
0
 public object reduce(IFn f, object start)
 {
     object ret = f.invoke(start, _array[_off]);
     for (int x = _off + 1; x < _end; x++)
         ret = f.invoke(ret, _array[x]);
     return ret;
 }
Ejemplo n.º 3
0
        public static Delegate Create(Type delegateType, IFn fn)
        {
            MethodInfo invokeMI = delegateType.GetMethod("Invoke");
            Type returnType = invokeMI.ReturnType;

            ParameterInfo[] delParams = invokeMI.GetParameters();

            List<ParameterExpression> parms = new List<ParameterExpression>();
            List<Expression> callArgs = new List<Expression>();

            foreach (ParameterInfo pi in delParams)
            {
                ParameterExpression pe = Expression.Parameter(pi.ParameterType, pi.Name);
                parms.Add(pe);
                callArgs.Add(MaybeBox(pe));
            }

            Expression call =                    
                Expression.Call(
                    Expression.Constant(fn),
                    Compiler.Methods_IFn_invoke[parms.Count], 
                    callArgs);

            Expression body =  returnType == typeof(void)
                ? (Expression)Expression.Block(call,Expression.Default(typeof(void)))
                : (Expression)Expression.Convert(call, returnType);

            LambdaExpression lambda = Expression.Lambda(delegateType, body, true, parms);

            return lambda.Compile();
        }
Ejemplo n.º 4
0
        public IFn tap()
        {
            if (_tap != null)
            throw new InvalidOperationException("Stream already tapped");

            return _tap = makeTap(_xform, _src);
        }
Ejemplo n.º 5
0
        public void UpdateSettings(DataSource_ClojureFunction_Config sender)
        {
            minXVal = sender.MinX;
            maxXVal = sender.MaxX;
            minYVal = sender.MinY;
            maxYVal = sender.MaxY;
            precision = sender.Precision;

            clojureFunctionText1 = sender.ClojureFunction1.Trim();
            clojureFunctionText2 = sender.ClojureFunction2.Trim();

            clojureFunction1 = null;
            clojureFunction2 = null;

            if (clojureFunctionText1 != "")
            {
                try { clojureFunction1 = (IFn)ClojureEngine.EvalRaw("(fn [x y z t] " + clojureFunctionText1 + ")"); }
                catch { }
            }

            if (clojureFunctionText2 != "")
            {
                try { clojureFunction2 = (IFn)ClojureEngine.EvalRaw("(fn [x y z t] " + clojureFunctionText2 + ")"); }
                catch { }
            }
        }
Ejemplo n.º 6
0
 public Future(IFn fn)
 {
     // TODO: Use a cached thread pool when agents have one.
     _t = new Thread(new ParameterizedThreadStart(ComputeFuture));
     _t.Name = "Future";
     _t.Start(fn);
 }
Ejemplo n.º 7
0
 private Iterate(IPersistentMap meta, IFn f, Object prevSeed, Object seed, ISeq next)
     :base(meta)
 {
     _f = f;
     _prevSeed = prevSeed;
     _seed = seed;
     _next = next;
 }
Ejemplo n.º 8
0
 public object deref()
 {
     if (_fn != null)
     {
         _val = _fn.invoke();
         _fn = null;
     }
     return _val;
 }
Ejemplo n.º 9
0
 /// Construct a multifunction.
 /// </summary>
 /// <param name="name">The name</param>
 /// <param name="dispatchFn">The dispatch function.</param>
 /// <param name="defaultDispatchVal">The default dispatch value.</param>
 /// <param name="hierarchy">The hierarchy for this multifunction</param>
 public MultiFn(string name, IFn dispatchFn, object defaultDispatchVal, IRef hierarchy)
 {
     _name = name;
     _dispatchFn = dispatchFn;
     _defaultDispatchVal = defaultDispatchVal;
     _methodTable = PersistentHashMap.EMPTY;
     _methodCache = MethodTable;
     _preferTable = PersistentHashMap.EMPTY;
     _hierarchy = hierarchy;
     _cachedHierarchy = null;
 }
Ejemplo n.º 10
0
 public object reduce(IFn f, object start)
 {
     object ret = f.invoke(start, _array[_off]);
     if (RT.isReduced(ret))
         return ret;
     for (int x = _off + 1; x < _end; x++)
     {
         ret = f.invoke(ret, _array[x]);
         if (RT.isReduced(ret))
             return ((IDeref)ret).deref();
     }
     return ret;
 }
Ejemplo n.º 11
0
 public object swap(IFn f)
 {
     for (; ; )
     {
         object v = deref();
         object newv = f.invoke(v);
         Validate(newv);
         if (_state.CompareAndSet(v, newv))
         {
             NotifyWatches(v,newv);
             return newv;
         }
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Invoke an <see cref="IFn">IFn</see> on a value to validate it.
        /// </summary>
        /// <param name="vf">The <see cref="IFn">IFn</see> to invoke.</param>
        /// <param name="val">The value to validate.</param>
        /// <remarks>Uneventful return marks a successful validation.  
        /// To indicate a failed validation, the validation function should return <value>false</value> or throw an exception.
        /// <para>This appears in multiple places.  Should find it a common home?</para></remarks>
        protected internal static void Validate(IFn vf, object val)
        {
            if (vf == null)
                return;

            bool ret = false;

            try
            {
               ret = RT.booleanCast(vf.invoke(val));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Invalid reference state", e);
            }

            if ( ! ret )
                throw new InvalidOperationException("Invalid reference state");
        }
Ejemplo n.º 13
0
        public static void AddClojureDropDownBox(string labelText, Var variable, PersistentVector vals, IFn func)
        {
            string variableSymbolName = null;

            if(variable != null)
                variableSymbolName = variable.sym.Name;

            Label label = new Label();
            label.AutoSize = true;
            label.Text = labelText;

            Dictionary<string, object> labelCodeAssoc = new Dictionary<string, object>();

            ComboBox comboBox = new ComboBox();
            for (int i = 0; i < vals.Count; i += 2)
            {
                labelCodeAssoc.Add(vals[i].ToString(), vals[i + 1]);
                comboBox.Items.Add(vals[i]);
            }

            comboBox.SelectedIndex = 0;
            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox.SelectedIndexChanged += (s, e) =>
                {
                    if (comboBox.SelectedItem == null)
                        return;

                    object code = labelCodeAssoc[comboBox.SelectedItem.ToString()];

                     if(variableSymbolName != null)
                    {
                        string newVal = code.ToString();
                        ClojureEngine.Eval("(def " + variableSymbolName + " " + newVal + ")");
                    }

                    if (func != null)
                        ClojureEngine.Log(func.invoke());
                };

            self.AddNewClojureControls(new Control[] { label, comboBox });
        }
Ejemplo n.º 14
0
 public static IEnumerator create(IFn xform, IEnumerator source)
 {
     return(new TransformerEnumerator(xform, source, false));
 }
Ejemplo n.º 15
0
 public static object ApplyToHelper(IFn fn, ISeq argList)
 {
     switch (RT.BoundedLength(argList, 20))
     {
         case 0:
             argList = null;
             return fn.invoke();
         case 1:
             return fn.invoke(Util.Ret1(argList.first(),argList=null));
         case 2:
             return fn.invoke(argList.first()
                     , Util.Ret1((argList = argList.next()).first(),argList = null)
             );
         case 3:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 4:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 5:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 6:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 7:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 8:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 9:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 10:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 11:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 12:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 13:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 14:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 15:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 16:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 17:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 18:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 19:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         case 20:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , Util.Ret1((argList = argList.next()).first(), argList = null)
             );
         default:
             return fn.invoke(argList.first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , (argList = argList.next()).first()
                     , RT.SeqToArray<object>(Util.Ret1(argList.next(),argList=null)));
     }
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Create an encapsulated message to an agent.
 /// </summary>
 /// <param name="agent">The agent the message is for.</param>
 /// <param name="fn">The function to compute the new value.</param>
 /// <param name="args">Additional arguments (in addition to the current state).</param>
 /// <param name="solo">Execute on its own thread?</param>
 public Action(Agent agent, IFn fn, ISeq args, bool solo)
 {
     _agent = agent;
     _fn = fn;
     _args = args;
     _solo = solo;
 }
Ejemplo n.º 17
0
 public void setErrorHandler(IFn f)
 {
     _errorHandler = f;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Construct one.
 /// </summary>
 /// <param name="fn">The function to invoke.</param>
 /// <param name="args">The arguments to invoke the function on.</param>
 public CFn(IFn fn, ISeq args)
 {
     _fn   = fn;
     _args = args;
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Send a message to the agent.
        /// </summary>
        /// <param name="fn">The function to be called on the current state and the supplied arguments.</param>
        /// <param name="args">The extra arguments to the function.</param>
        /// <param name="solo"><value>true</value> means execute on its own thread (send-off); 
        /// <value>false</value> means use a thread pool thread (send).</param>
        /// <returns>This agent.</returns>
        public object dispatch(IFn fn, ISeq args, Boolean solo)
        {
            if ( _errors != null )
                throw new Exception("Agent has errors", (Exception)RT.first(_errors));
            Action action = new Action(this,fn,args,solo);
            DispatchAction(action);

            return this;
        }
Ejemplo n.º 20
0
 public DocumentService(IGetUserService getUserService, IRepository <Document> repository, IFn fn)
 {
     _getUserService = getUserService;
     _repository     = repository;
     _fn             = fn;
 }
Ejemplo n.º 21
0
 public Stream(IFn xform, Stream src)
 {
     _src   = src.tap();
     _xform = xform;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Sets the validator.
 /// </summary>
 /// <param name="vf">The new validtor</param>
 /// <remarks>The current value must validate in order for this validator to be accepted.  If not, an exception will be thrown.</remarks>
 public virtual void setValidator(IFn vf)
 {
     Validate(vf, deref());
     _validator = vf;
 }
Ejemplo n.º 23
0
 public Stream(IFn src)
 {
     _src   = src;
     _xform = null;
 }
Ejemplo n.º 24
0
 static IFn makeTap(IFn xform, IFn src)
 {
     return(new Tapper(xform, src));
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Construct a delay for a function.
 /// </summary>
 /// <param name="fn">The function to delay.</param>
 public Delay(IFn fn)
 {
     _fn  = fn;
     _val = null;
 }
Ejemplo n.º 26
0
 public object alter(IFn fn, ISeq args)
 {
     set(fn.applyTo(RT.cons(deref(), args)));
     return(this);
 }
Ejemplo n.º 27
0
 public object reduce(IFn f)
 {
     throw new InvalidOperationException();
 }
Ejemplo n.º 28
0
        public static object read(PushbackTextReader r,
                                  bool eofIsError,
                                  object eofValue,
                                  bool isRecursive,
                                  Object opts)
        {
            try
            {
                for (; ;)
                {
                    int ch = r.Read();

                    while (isWhitespace(ch))
                    {
                        ch = r.Read();
                    }

                    if (ch == -1)
                    {
                        if (eofIsError)
                        {
                            throw new EndOfStreamException("EOF while reading");
                        }
                        return(eofValue);
                    }

                    if (Char.IsDigit((char)ch))
                    {
                        object n = readNumber(r, (char)ch);
                        return(RT.suppressRead() ? null : n);
                    }

                    IFn macroFn = getMacro(ch);
                    if (macroFn != null)
                    {
                        object ret = macroFn.invoke(r, (char)ch, opts);
                        if (RT.suppressRead())
                        {
                            return(null);
                        }
                        // no op macros return the reader
                        if (ret == r)
                        {
                            continue;
                        }
                        return(ret);
                    }

                    if (ch == '+' || ch == '-')
                    {
                        int ch2 = r.Read();
                        if (Char.IsDigit((char)ch2))
                        {
                            Unread(r, ch2);
                            object n = readNumber(r, (char)ch);
                            return(RT.suppressRead() ? null : n);
                        }
                        Unread(r, ch2);
                    }

                    //string token = readToken(r, (char)ch);
                    //return RT.suppressRead() ? null : interpretToken(token);
                    string rawToken;
                    string token;
                    string mask;
                    bool   eofSeen;
                    readToken(r, (char)ch, true, out rawToken, out token, out mask, out eofSeen);
                    if (eofSeen)
                    {
                        if (eofIsError)
                        {
                            throw new EndOfStreamException("EOF while reading symbol");
                        }
                        return(eofValue);
                    }
                    return(RT.suppressRead() ? null : InterpretToken(rawToken, token, mask));
                }
            }
            catch (Exception e)
            {
                if (isRecursive)
                {
                    throw;
                }

                LineNumberingTextReader lntr = r as LineNumberingTextReader;
                if (lntr == null)
                {
                    throw;
                }

                throw new ReaderException(lntr.LineNumber, lntr.ColumnNumber, e);
            }
        }
Ejemplo n.º 29
0
 void CommuteRoot(IFn fn)
 {
     object newRoot = fn.invoke(_root);
     Validate(getValidator(), newRoot);
     object oldRoot = _root;
     _root = newRoot;
     ++_rev;
     NotifyWatches(oldRoot, newRoot);
 }
Ejemplo n.º 30
0
 public IntervalData(IFn _f, int _interval, object _key)
 {
     f        = _f;
     interval = _interval;
     key      = _key;
 }
Ejemplo n.º 31
0
 public object alterRoot(IFn fn, ISeq args)
 {
     object newRoot = fn.applyTo(RT.cons(_root, args));
     Validate(getValidator(), newRoot);
     object oldroot = _root;
     _root = newRoot;
     ++_rev;
     NotifyWatches(oldroot,newRoot);
     return newRoot;
 }
Ejemplo n.º 32
0
        object Run(IFn fn)
        {
            // TODO: Define an overload called on ThreadStartDelegate or something equivalent.

            bool          done   = false;
            object        ret    = null;
            List <Ref>    locked = new List <Ref>();
            List <Notify> notify = new List <Notify>();

            for (int i = 0; !done && i < RetryLimit; i++)
            {
                try
                {
                    GetReadPoint();
                    if (i == 0)
                    {
                        _startPoint = _readPoint;
                        _startTime  = Environment.TickCount;
                    }

                    _info = new Info(RUNNING, _startPoint);
                    ret   = fn.invoke();

                    // make sure no one has killed us before this point,
                    // and can't from now on
                    if (_info.Status.compareAndSet(RUNNING, COMMITTING))
                    {
                        foreach (KeyValuePair <Ref, List <CFn> > pair in _commutes)
                        {
                            Ref r = pair.Key;
                            if (_sets.Contains(r))
                            {
                                continue;
                            }

                            bool wasEnsured = _ensures.Contains(r);
                            // can't upgrade read lock, so release
                            ReleaseIfEnsured(r);
                            TryWriteLock(r);
                            locked.Add(r);

                            if (wasEnsured && r.CurrentValPoint() > _readPoint)
                            {
                                throw _retryex;
                            }

                            Info refinfo = r.TInfo;
                            if (refinfo != null && refinfo != _info && refinfo.IsRunning)
                            {
                                if (!Barge(refinfo))
                                {
                                    throw _retryex;
                                }
                            }
                            object val = r.TryGetVal();
                            _vals[r] = val;
                            foreach (CFn f in pair.Value)
                            {
                                _vals[r] = f.Fn.applyTo(RT.cons(_vals[r], f.Args));
                            }
                        }
                        foreach (Ref r in _sets)
                        {
                            TryWriteLock(r);
                            locked.Add(r);
                        }
                        // validate and enqueue notifications
                        foreach (KeyValuePair <Ref, object> pair in _vals)
                        {
                            Ref r = pair.Key;
                            r.Validate(pair.Value);
                        }

                        // at this point, all values calced, all refs to be written locked
                        // no more client code to be called
                        long commitPoint = GetCommitPoint();
                        foreach (KeyValuePair <Ref, object> pair in _vals)
                        {
                            Ref    r      = pair.Key;
                            object oldval = r.TryGetVal();
                            object newval = pair.Value;

                            r.SetValue(newval, commitPoint);
                            if (r.getWatches().count() > 0)
                            {
                                notify.Add(new Notify(r, oldval, newval));
                            }
                        }

                        done = true;
                        _info.Status.set(COMMITTED);
                    }
                }
                catch (RetryEx)
                {
                    // eat this so we retry rather than fall out
                }
                catch (Exception ex)
                {
                    if (ContainsNestedRetryEx(ex))
                    {
                        // Wrapped exception, eat it.
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    for (int k = locked.Count - 1; k >= 0; --k)
                    {
                        locked[k].ExitWriteLock();
                    }
                    locked.Clear();
                    foreach (Ref r in _ensures)
                    {
                        r.ExitReadLock();
                    }
                    _ensures.Clear();
                    Stop(done ? COMMITTED : RETRY);
                    try
                    {
                        if (done) // re-dispatch out of transaction
                        {
                            foreach (Notify n in notify)
                            {
                                n._ref.NotifyWatches(n._oldval, n._newval);
                            }
                            foreach (Agent.Action action in _actions)
                            {
                                Agent.DispatchAction(action);
                            }
                        }
                    }
                    finally
                    {
                        notify.Clear();
                        _actions.Clear();
                    }
                }
            }
            if (!done)
            {
                throw new InvalidOperationException("Transaction failed after reaching retry limit");
            }
            return(ret);
        }
Ejemplo n.º 33
0
 public void Reset()
 {
     idx       = -1;
     predicate = null;
 }
Ejemplo n.º 34
0
 public Seqer(IFn tap)
 {
     _tap = tap;
 }
Ejemplo n.º 35
0
 public IRef addWatch(object key, IFn callback)
 {
     _watches = _watches.assoc(key, callback);
     return(this);
 }
Ejemplo n.º 36
0
 public object reduce(IFn f)
 {
     if (_oa != null)
     {
         object ret = _oa[_i];
         for (int x = _i + 1; x < _oa.Length; x++)
             ret = f.invoke(ret, _oa[x]);
         return ret;
     }
     object ret1 = _ilist[_i];    // JAVA 1112 wraps in RT.prepRet
     for (int x = _i + 1; x < _ilist.Count; x++)
         ret1 = f.invoke(ret1, _ilist[x]);       // JAVA 1112 wraps in RT.prepRet
     return ret1;
 }
Ejemplo n.º 37
0
 static ISeq makeSequence(IFn tap)
 {
     return(RT.seq(new LazySeq(new Seqer(tap))));
 }
Ejemplo n.º 38
0
 public object kvreduce(IFn f, object init)
 {
     int step = 0;
     for (int i = 0; i < _cnt; i += step)
     {
         object[] array = ArrayFor(i);
         for (int j = 0; j < array.Length; j++)
         {
             init = f.invoke(init, j + i, array[j]);
             if (RT.isReduced(init))
                 return ((IDeref)init).deref();
         }
         step = array.Length;
     }
     return init;
 }
Ejemplo n.º 39
0
 /// <summary>
 /// Construct a delay for a function.
 /// </summary>
 /// <param name="fn">The function to delay.</param>
 public Delay(IFn fn)
 {
     _fn        = fn;
     _val       = null;
     _exception = null;
 }
Ejemplo n.º 40
0
 public object reduce(IFn f, object start)
 {
     int step = 0;
     for (int i = 0; i < _cnt; i += step)
     {
         Object[] array = ArrayFor(i);
         for (int j = 0; j < array.Length; ++j)
         {
             start = f.invoke(start, array[j]);
             if (RT.isReduced(start))
                 return ((IDeref)start).deref();
         }
         step = array.Length;
     }
     return start;
 }
Ejemplo n.º 41
0
        public static object ApplyToHelper(IFn fn, ISeq argList)
        {
            switch (RT.BoundedLength(argList, 20))
            {
            case 0:
                argList = null;
                return(fn.invoke());

            case 1:
                return(fn.invoke(Util.Ret1(argList.first(), argList = null)));

            case 2:
                return(fn.invoke(argList.first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 3:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 4:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 5:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 6:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 7:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 8:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 9:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 10:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 11:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 12:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 13:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 14:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 15:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 16:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 17:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 18:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 19:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            case 20:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , Util.Ret1((argList = argList.next()).first(), argList = null)
                                 ));

            default:
                return(fn.invoke(argList.first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , (argList = argList.next()).first()
                                 , RT.SeqToArray <object>(Util.Ret1(argList.next(), argList = null))));
            }
        }
Ejemplo n.º 42
0
 public object alter(IFn fn, ISeq args)
 {
     set(fn.applyTo(RT.cons(deref(), args)));
     return this;
 }
Ejemplo n.º 43
0
 /// <summary>
 /// Sets the validator.
 /// </summary>
 /// <param name="vf">The new validtor</param>
 /// <remarks>The current value must validate in order for this validator to be accepted.  If not, an exception will be thrown.</remarks>
 public virtual void setValidator(IFn vf)
 {
     Validate(vf, deref());
     _validator = vf;
 }
Ejemplo n.º 44
0
 /// <summary>
 /// Sets the validator.
 /// </summary>
 /// <param name="vf">The new validtor</param>
 public override void setValidator(IFn vf)
 {
     if (hasRoot())
         Validate(vf, _root);
     _validator = vf;
 }
Ejemplo n.º 45
0
        /// <summary>
        /// Send a message to the agent.
        /// </summary>
        /// <param name="fn">The function to be called on the current state and the supplied arguments.</param>
        /// <param name="args">The extra arguments to the function.</param>
        /// <param name="solo"><value>true</value> means execute on its own thread (send-off); 
        /// <value>false</value> means use a thread pool thread (send).</param>
        /// <returns>This agent.</returns>
        public object dispatch(IFn fn, ISeq args, Boolean solo)
        {
            Exception error = getError();
            if (error != null)
                throw new Exception("Agent is failed, needs restart", error);
            Action action = new Action(this,fn,args,solo);
            DispatchAction(action);

            return this;
        }
Ejemplo n.º 46
0
 public IRef addWatch(Agent watcher, IFn action, bool sendOff)
 {
     _watchers = _watchers.assoc(watcher, new object[] { action, sendOff });
      return this;
 }
Ejemplo n.º 47
0
 public object commute(IFn fn, ISeq args)
 {
     return(LockingTransaction.GetEx().DoCommute(this, fn, args));
 }
Ejemplo n.º 48
0
 public void setErrorHandler(IFn f)
 {
     _errorHandler = f;
 }
Ejemplo n.º 49
0
        public object alter(IFn fn, ISeq args)
        {
            LockingTransaction t = LockingTransaction.GetEx();

            return(t.DoSet(this, fn.applyTo(RT.cons(t.DoGet(this), args))));
        }
Ejemplo n.º 50
0
 public Task35(IFn fn)
 {
     _fn = fn;
 }
Ejemplo n.º 51
0
 public Tapper(IFn xform, IFn src)
 {
     _xform = xform;
     _src   = src;
 }
Ejemplo n.º 52
0
 public object swap(IFn f, Object x, Object y, ISeq args)
 {
     for (; ; )
     {
         object v = deref();
         object newv = f.applyTo(RT.listStar(v, x, y, args));
         Validate(newv);
         if (_state.CompareAndSet(v, newv))
         {
             NotifyWatches(v, newv);
             return newv;
         }
     }
 }
Ejemplo n.º 53
0
 TransformerEnumerator(IFn xform, IEnumerator sourceEnum, bool multi)
 {
     _sourceEnum = sourceEnum;
     _xf         = (IFn)xform.invoke(new TransformHelper(o => _buffer = _buffer.Add(o)));
     _multi      = multi;
 }