Пример #1
0
        public override void OnReadFromXmlNode(IXmlCodeReader serializer, XmlNode node)
        {
            base.OnReadFromXmlNode(serializer, node);
            PropertyId      = XmlUtil.GetAttributeUInt(node, XMLATTR_PropertyId);
            PropertyClassId = XmlUtil.GetAttributeUInt(node, XmlTags.XMLATT_ClassID);
            XmlObjectReader reader = (XmlObjectReader)serializer;

            if (PropertyClassId == 0)
            {
                PropertyClassId = reader.ObjectList.ClassId;
            }
            ClassPointer root = ClassPointer.CreateClassPointer(PropertyClassId, reader.ObjectList.Project);

            PropertyClass mc = root.GetCustomPropertyById(PropertyId);

            if (mc == null)
            {
                MathNode.LogError(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                "Property {0} not found in class {1}", PropertyId, root.ClassId));
            }
            else
            {
                _property = mc;
            }
        }
Пример #2
0
        void _timerX_Tick(object sender, EventArgs e)
        {
            if (_timerX != null)
            {
                _timerX.Enabled = false;
                _timerX         = null;
            }
            string src = string.Empty;
            string tgt = string.Empty;

            try
            {
                src = _htmlFile;
                tgt = Path.Combine(_projectFolder, Path.GetFileName(_htmlFile));
                File.Copy(src, tgt, true);
                src = Path.Combine(Path.GetDirectoryName(_htmlFile), string.Format(CultureInfo.InvariantCulture, "{0}.css", Path.GetFileNameWithoutExtension(_htmlFile)));
                if (File.Exists(src))
                {
                    tgt = Path.Combine(_projectFolder, Path.GetFileName(src));
                    File.Copy(src, tgt, true);
                }
            }
            catch (Exception err)
            {
                MathNode.LogError(string.Format(CultureInfo.InvariantCulture, "Error copying from [{0}] to [{1}]. {2}", src, tgt, err.Message));
            }
            int p = _htmlFile.LastIndexOf('.');

            if (p > 0)
            {
                src = string.Format(CultureInfo.InvariantCulture, "{0}.css", _htmlFile.Substring(0, p));
                if (File.Exists(src))
                {
                    tgt = Path.Combine(_projectFolder, Path.GetFileName(src));
                    try
                    {
                        File.Copy(src, tgt, true);
                    }
                    catch (Exception err)
                    {
                        MathNode.LogError(string.Format(CultureInfo.InvariantCulture, "Error copying from [{0}] to [{1}]. {2}", src, tgt, err.Message));
                    }
                }
            }
        }
Пример #3
0
        public override void OnReadFromXmlNode(IXmlCodeReader serializer, XmlNode node)
        {
            base.OnReadFromXmlNode(serializer, node);
            MethodId = XmlUtil.GetAttributeUInt(node, XmlTags.XMLATT_handlerId);
            XmlObjectReader reader = (XmlObjectReader)serializer;
            ClassPointer    root   = reader.ObjectList.RootPointer as ClassPointer;
            MethodClass     mc     = root.GetCustomMethodById(MethodId);

            if (mc == null)
            {
                MathNode.LogError(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                "Method {0} not found in class {1}", MethodId, root.ClassId));
            }
            else
            {
                _method = mc;
            }
        }
Пример #4
0
 public static void LinkPorts(List <EventPortOut> ports, List <ComponentIconEvent> iconList)
 {
     foreach (EventPortOut po in ports)
     {
         bool poLinked = false;
         foreach (ComponentIconEvent cieAction in iconList)
         {
             if (po.IsActionExecuter(cieAction))
             {
                 List <EventPortIn> ins = cieAction.DestinationPorts;
                 if (ins != null && ins.Count > 0)
                 {
                     foreach (EventPortIn pi in ins)
                     {
                         if (po.Event.IsSameObjectRef(pi.Event))
                         {
                             pi.LinkedPortID         = po.PortID;
                             pi.LinkedPortInstanceID = po.PortInstanceID;
                             po.LinkedPortID         = pi.PortID;
                             po.LinkedPortInstanceID = pi.PortInstanceID;
                             poLinked = true;
                             break;
                         }
                     }
                     if (poLinked)
                     {
                         break;
                     }
                 }
             }
         }
         if (!poLinked)
         {
             MathNode.LogError(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                             "Mapping of event -> action executer not found: {0}", po.ToString()));
         }
     }
 }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc != null)
                {
                    MethodClass scopeMethod = context.Instance as MethodClass;
                    if (scopeMethod != null)
                    {
                        //this version only edit FinalActions
                        ILimnorDesignPane dp = scopeMethod.Project.GetTypedData <ILimnorDesignPane>(scopeMethod.RootPointer.ClassId);
                        if (dp != null)
                        {
                            //save the method contents
                            BranchList           bl         = scopeMethod.ActionList;
                            string               methodName = scopeMethod.MethodName;
                            ParameterClass       returnType = scopeMethod.ReturnValue;
                            List <ComponentIcon> icons      = scopeMethod.ComponentIconList;

                            //switch method contents
                            scopeMethod.ActionList = scopeMethod.FinalActions.Actions;
                            scopeMethod.SetName("FinalActions");
                            List <ComponentIcon> icons2 = new List <ComponentIcon>();
                            foreach (ComponentIcon ic in icons)
                            {
                                icons2.Add((ComponentIcon)ic.Clone());
                            }
                            foreach (ComponentIconSubscopeVariable ic in scopeMethod.FinalActions.ComponentIconList)
                            {
                                icons2.Add((ComponentIcon)ic.Clone());
                            }
                            scopeMethod.SetComponentIcons(icons2);
                            //
                            Rectangle rc = new Rectangle(Cursor.Position, new Size(32, 232));
                            if (scopeMethod.Owner == null)
                            {
                                scopeMethod.Owner = dp.Loader.GetRootId();
                            }
                            DlgMethod dlg = scopeMethod.CreateBlockScopeMethodEditor(rc, 1);
                            try
                            {
                                dlg.LoadMethod(scopeMethod, EnumParameterEditType.ReadOnly);
                                dlg.Text = string.Format(CultureInfo.InvariantCulture, "Specify final actionsfor {0}", methodName);
                                if (edSvc.ShowDialog(dlg) == DialogResult.OK)
                                {
                                    scopeMethod.FinalActions.Actions = scopeMethod.ActionList;
                                    List <ComponentIconSubscopeVariable> lst = new List <ComponentIconSubscopeVariable>();
                                    foreach (ComponentIcon ic in scopeMethod.ComponentIconList)
                                    {
                                        ComponentIconSubscopeVariable sv = ic as ComponentIconSubscopeVariable;
                                        if (sv != null)
                                        {
                                            lst.Add(sv);
                                        }
                                    }
                                    scopeMethod.FinalActions.ComponentIconList = lst;
                                    XmlObjectWriter xw = dp.Loader.Writer;
                                    xw.WriteObject(scopeMethod.FinalActions.DataXmlNode, scopeMethod.FinalActions, null);
                                    value = scopeMethod.FinalActions;
                                }
                            }
                            catch (Exception err)
                            {
                                MathNode.LogError(DesignerException.FormExceptionText(err));
                            }
                            finally
                            {
                                //restore method contents
                                scopeMethod.SetName(methodName);
                                scopeMethod.ActionList = bl;
                                scopeMethod.SetComponentIcons(icons);
                                scopeMethod.ExitEditor();
                            }
                        }
                    }
                }
            }
            return(value);
        }
        public void OnDeserialize(object context)
        {
            string err;

            _root = context as ClassPointer;
            if (_root == null)
            {
                if (context == null)
                {
                    err = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.OnDeserialize(object context): context is null. MethodId:{1},BranchId:{2},ParameterId:{3},ParameterName:{4}", this.GetType().Name, MethodId, BranchId, ParameterId, ParameterName);
                }
                else
                {
                    err = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.OnDeserialize(object context): context {1} is not a ClassPointer. MethodId:{2},BranchId:{3},ParameterId:{4},ParameterName:{5}", this.GetType().Name, context.GetType().Name, MethodId, BranchId, ParameterId, ParameterName);
                }
                MathNode.LogError(err);
            }
            else
            {
                MethodClass mc = _root.GetCustomMethodById(MethodId);
                if (mc == null)
                {
                    if (_root.ActionInstances != null)
                    {
                        IAction a;
                        if (_root.ActionInstances.TryGetValue(MethodId, out a))
                        {
                            MethodActionForeach ma = a as MethodActionForeach;
                            if (ma != null)
                            {
                                mc = ma;
                            }
                        }
                    }
                }
                if (mc == null)
                {
                    err = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.OnDeserialize(object context): method [{1},{2}] is not found in ClassPointer.  MethodId:{2},BranchId:{3},ParameterId:{4},ParameterName:{5}", this.GetType().Name, _root.ClassId, MethodId, BranchId, ParameterId, ParameterName);
                    MathNode.LogError(err);
                }
                if (mc != null)
                {
                    ActionBranch ab = mc.SearchBranchById(BranchId);
                    if (ab == null)
                    {
                        //throw new DesignerException("{0}.OnDeserialize(object context): Action Branch [{1}] not found in method [{2},{3}][{4}]", this.GetType().Name, BranchId, _root.ClassId, MethodId, mc.Name);
                        mc.AddDeserializer(this);
                    }
                    else
                    {
                        AB_SubMethodAction sma = ab as AB_SubMethodAction;
                        if (sma != null)
                        {
                            _parameter = sma.GetParameterById(ParameterId);
                        }
                        if (_parameter == null)
                        {
                            _parameter = ab.GetActionBranchParameterByName(ParameterName);
                            if (_parameter != null)
                            {
                                _parameter.ParameterID = ParameterId;
                            }
                        }
                    }
                }
            }
        }
Пример #7
0
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc != null)
                {
                    ExceptionHandlerList.PropertyDescriptorExceptionHandler pd = context.PropertyDescriptor as ExceptionHandlerList.PropertyDescriptorExceptionHandler;
                    if (pd == null)
                    {
                        throw new DesignerException("Property descriptor [{0}] is not a PropertyDescriptorExceptionHandler", context.PropertyDescriptor == null ? "null" : context.PropertyDescriptor.GetType().FullName);
                    }
                    ExceptionHandler     eh       = pd.Handler;
                    BranchList           branches = eh.ActionList;
                    ExceptionHandlerList ehs      = context.Instance as ExceptionHandlerList;
                    if (ehs != null)
                    {
                        MethodClass scopeMethod = ehs.OwnerMethod;
                        if (scopeMethod != null)
                        {
                            if (branches == null)
                            {
                                branches = new BranchList(eh, new List <ActionBranch>());
                            }

                            ILimnorDesignPane dp = scopeMethod.Project.GetTypedData <ILimnorDesignPane>(scopeMethod.RootPointer.ClassId);
                            if (dp != null)
                            {
                                //save the method contents
                                BranchList           bl         = scopeMethod.ActionList;
                                string               methodName = scopeMethod.MethodName;
                                ParameterClass       returnType = scopeMethod.ReturnValue;
                                List <ComponentIcon> icons      = scopeMethod.ComponentIconList;

                                //switch method contents
                                scopeMethod.ActionList = branches;
                                scopeMethod.SetName(string.Format(CultureInfo.InvariantCulture, "HandlerFor{0}", eh.ExceptionType.Name));
                                scopeMethod.ReturnValue = null;
                                List <ComponentIcon> icons2 = new List <ComponentIcon>();
                                foreach (ComponentIcon ic in icons)
                                {
                                    icons2.Add((ComponentIcon)ic.Clone());
                                }
                                foreach (ComponentIconSubscopeVariable ic in eh.ComponentIconList)
                                {
                                    icons2.Add((ComponentIcon)ic.Clone());
                                }
                                icons2.Add(eh.ExceptionObject);
                                scopeMethod.SetComponentIcons(icons2);
                                //
                                Rectangle rc = new Rectangle(Cursor.Position, new Size(32, 232));
                                if (scopeMethod.Owner == null)
                                {
                                    scopeMethod.Owner = dp.Loader.GetRootId();
                                }
                                DlgMethod dlg = scopeMethod.CreateBlockScopeMethodEditor(rc, eh.ExceptionType.MemberId);
                                try
                                {
                                    dlg.LoadMethod(scopeMethod, EnumParameterEditType.ReadOnly);
                                    dlg.Text = string.Format(CultureInfo.InvariantCulture, "Create exception handler for {0}", eh.ExceptionType.Name);
                                    if (edSvc.ShowDialog(dlg) == DialogResult.OK)
                                    {
                                        bool delete = false;
                                        eh.ActionList = scopeMethod.ActionList;
                                        if (eh.ActionList.Count == 0)
                                        {
                                            if (MessageBox.Show(string.Format(CultureInfo.CurrentUICulture, "Do you want to remove this exception [{0}]?", eh.ExceptionType.Name), "Capture exception", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                            {
                                                delete = true;
                                            }
                                        }
                                        if (delete)
                                        {
                                            scopeMethod.RemoveExceptionCapture(eh);
                                        }
                                        else
                                        {
                                            List <ComponentIconSubscopeVariable> lst = new List <ComponentIconSubscopeVariable>();
                                            foreach (ComponentIcon ic in scopeMethod.ComponentIconList)
                                            {
                                                ComponentIconSubscopeVariable sv = ic as ComponentIconSubscopeVariable;
                                                if (sv != null)
                                                {
                                                    lst.Add(sv);
                                                }
                                            }
                                            eh.ComponentIconList = lst;
                                            scopeMethod.RootPointer.SaveMethod(scopeMethod, eh);
                                        }
                                    }
                                }
                                catch (Exception err)
                                {
                                    MathNode.LogError(DesignerException.FormExceptionText(err));
                                }
                                finally
                                {
                                    //restore method contents
                                    scopeMethod.SetName(methodName);
                                    scopeMethod.ActionList  = bl;
                                    scopeMethod.ReturnValue = returnType;
                                    scopeMethod.SetComponentIcons(icons);
                                    scopeMethod.ExitEditor();
                                }
                            }
                        }
                    }
                }
            }
            return(value);
        }
        /// <summary>
        /// this control is already added to Parent.Controls.
        /// 1. remove invalid inports
        /// 2. add missed EventPortIn
        /// </summary>
        /// <param name="viewer"></param>
        public override void Initialize(EventPathData eventData)
        {
            ClassPointer       root = this.ClassPointer.RootPointer;
            List <EventAction> ehs  = root.EventHandlers;

            if (ehs != null && ehs.Count > 0)
            {
                if (DestinationPorts == null)
                {
                    DestinationPorts = new List <EventPortIn>();
                }
                else
                {
                    //remove invalid inport
                    List <EventPortIn> invalidInports = new List <EventPortIn>();
                    foreach (EventPortIn pi in DestinationPorts)
                    {
                        bool bFound = false;
                        foreach (EventAction ea in ehs)
                        {
                            if (pi.Event.IsSameObjectRef(ea.Event))
                            {
                                if (pi.Event.IsSameObjectRef(ea.Event))
                                {
                                    if (ea.TaskIDList != null && ea.TaskIDList.Count > 0)
                                    {
                                        foreach (TaskID tid in ea.TaskIDList)
                                        {
                                            if (tid.IsEmbedded)
                                            {
                                                HandlerMethodID hid = tid as HandlerMethodID;
                                                if (hid != null)
                                                {
                                                    if (hid.ActionId == this.MethodId)
                                                    {
                                                        bFound = true;
                                                        break;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                IAction a = root.GetActionInstance(tid.ActionId);                                                //only public actions in map
                                                if (a != null)
                                                {
                                                    CustomMethodPointer cmp = a.ActionMethod as CustomMethodPointer;
                                                    if (cmp != null)
                                                    {
                                                        if (cmp.MemberId == this.MethodId)
                                                        {
                                                            bFound = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (bFound)
                                {
                                    break;
                                }
                            }
                        }
                        if (!bFound)
                        {
                            invalidInports.Add(pi);
                        }
                    }
                    if (invalidInports.Count > 0)
                    {
                        foreach (EventPortIn pi in invalidInports)
                        {
                            DestinationPorts.Remove(pi);
                        }
                    }
                }
                //add missed EventPortIn
                foreach (EventAction ea in ehs)
                {
                    if (ea.TaskIDList != null && ea.TaskIDList.Count > 0)
                    {
                        foreach (TaskID tid in ea.TaskIDList)
                        {
                            HandlerMethodID hid = tid as HandlerMethodID;
                            if (hid != null)
                            {
                                if (hid.ActionId == this.MethodId)
                                {
                                    bool bFound = false;
                                    foreach (EventPortIn pi in DestinationPorts)
                                    {
                                        if (pi.Event.IsSameObjectRef(ea.Event))
                                        {
                                            bFound = true;
                                            break;
                                        }
                                    }
                                    if (!bFound)
                                    {
                                        EventPortIn pi = new EventPortIn(this);

                                        pi.Event = ea.Event;
                                        double x, y;
                                        ComponentIconEvent.CreateRandomPoint(Width + ComponentIconEvent.PortSize, out x, out y);
                                        pi.Location = new Point((int)(Center.X + x), (int)(Center.Y + y));
                                        pi.SetLoaded();
                                        pi.SaveLocation();
                                        DestinationPorts.Add(pi);
                                    }
                                }
                            }
                            else
                            {
                                //it is a root scope action
                                IAction a = root.GetActionInstance(tid.ActionId);
                                if (a == null)
                                {
                                    MathNode.LogError(string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                                    "Action [{0}] not found", tid));
                                }
                                else
                                {
                                    CustomMethodPointer cmp = a.ActionMethod as CustomMethodPointer;
                                    if (cmp != null)
                                    {
                                        if (cmp.MemberId == this.MethodId)
                                        {
                                            bool bFound = false;
                                            foreach (EventPortIn pi in DestinationPorts)
                                            {
                                                if (pi.Event.IsSameObjectRef(ea.Event))
                                                {
                                                    bFound = true;
                                                    break;
                                                }
                                            }
                                            if (!bFound)
                                            {
                                                EventPortIn pi = new EventPortIn(this);
                                                pi.Event = ea.Event;
                                                double x, y;
                                                ComponentIconEvent.CreateRandomPoint(Width + ComponentIconEvent.PortSize, out x, out y);
                                                pi.Location = new Point((int)(Center.X + x), (int)(Center.Y + y));
                                                pi.SetLoaded();
                                                pi.SaveLocation();
                                                DestinationPorts.Add(pi);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #9
0
        public bool ExportPhpScriptCode(StringCollection jsCode, StringCollection methodCode, JsMethodCompiler data)
        {
            bool bRet = false;

            MathNode.Trace("BranchList.ExportPhpScriptCode. Method {0}, action blocks {1}================", _method.Name, this.Count);
            //create code threads
            List <ActionBranch> independentThreads;

            if (_independentThreads == null)
            {
                independentThreads = FindoutActionThreads(true);
            }
            else
            {
                independentThreads = _independentThreads;
            }
            IsMultiThreads = (independentThreads.Count > 1);
            //the case of Count == 0 (empty method) is handled by MethodClass.ExportCode
            if (independentThreads.Count > 0)
            {
                int k0 = 0;                // main thread index
                this.ActionThreads.Clear();
                foreach (ActionBranch a in independentThreads)
                {
                    _threads.Add(a.BranchId, a);
                }
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (independentThreads[k].IsMainThread)
                    {
                        k0 = k;
                        break;
                    }
                }
                if (k0 == 0)
                {
                    independentThreads[0].IsMainThread = true;
                }
                this.MainThreadId = independentThreads[k0].BranchId;
                List <UInt32> usedBranches = new List <uint>();
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    independentThreads[k].IsMainThread = (k == k0);
                    independentThreads[k].SetIsMainThreadForSubBranches(usedBranches);
                }
                //javascript does not support threading
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (k != k0)
                    {
                        ActionBranch ab = independentThreads[k];
                        ab.IsMainThread = false;
                        ////method contents: a single thread
                        ab.ExportPhpScriptCode(null, null, jsCode, methodCode, data);
                    }
                }
                //main thread code
                bRet = independentThreads[k0].ExportPhpScriptCode(null, null, jsCode, methodCode, data);
                //
                if (_method == null)
                {
                    MathNode.LogError("method is null");
                }
                else                 //
                {
                    if (_method.ReturnValue != null)
                    {
                        if (!typeof(void).Equals(_method.ReturnValue.ObjectType))
                        {
                            //check whether all branches ends with a method return statement
                            if (!independentThreads[k0].AllBranchesEndWithMethodReturnStatement())
                            {
                                methodCode.Add("return ");
                                methodCode.Add(ValueTypeUtil.GetDefaultPhpScriptValueByType(_method.ReturnValue.ObjectType));
                                methodCode.Add(";\r\n");
                            }
                        }
                    }
                }
            }
            MathNode.Trace("End of BranchList.ExportPhpScriptCode. Method {0}================", _method.Name);
            return(bRet);
        }
Пример #10
0
        public bool ExportJavaScriptCode(StringCollection jsCode, StringCollection methodCode, JsMethodCompiler data)
        {
            bool bRet = false;

            MathNode.Trace("BranchList.ExportJavaScriptCode. Method {0}, action blocks {1}================", _method.Name, this.Count);
            //create code threads
            List <ActionBranch> independentThreads;

            if (_independentThreads == null)
            {
                independentThreads = FindoutActionThreads(true);
            }
            else
            {
                independentThreads = _independentThreads;
            }
            IsMultiThreads = (independentThreads.Count > 1);
            //the case of Count == 0 (empty method) is handled by MethodClass.ExportCode
            if (independentThreads.Count > 0)
            {
                int k0 = 0;                // main thread index
                this.ActionThreads.Clear();
                foreach (ActionBranch a in independentThreads)
                {
                    _threads.Add(a.BranchId, a);
                }
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (independentThreads[k].IsMainThread)
                    {
                        k0 = k;
                        break;
                    }
                }
                if (k0 == 0)
                {
                    independentThreads[0].IsMainThread = true;
                }
                this.MainThreadId = independentThreads[k0].BranchId;
                List <UInt32> usedBranches = new List <uint>();
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    independentThreads[k].IsMainThread = (k == k0);
                    independentThreads[k].SetIsMainThreadForSubBranches(usedBranches);
                }
                //javascript does not support threading
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (k != k0)
                    {
                        ActionBranch ab = independentThreads[k];
                        ab.IsMainThread = false;
                        ////method contents: a single thread
                        ab.ExportJavaScriptCode(null, null, jsCode, methodCode, data);
                    }
                }
                //main thread code
                bRet = independentThreads[k0].ExportJavaScriptCode(null, null, jsCode, methodCode, data);
                //
                if (_method == null)
                {
                    MathNode.LogError("method is null");
                }
            }
            MathNode.Trace("End of BranchList.ExportJavaScriptCode. Method {0}================", _method.Name);
            return(bRet);
        }
Пример #11
0
        /// <summary>
        /// multi-thread support:
        ///
        /// thread_{branch id}(object data)
        /// {
        ///     object[] ps = (object[])data;
        ///     type1 var1 = ps[0];
        ///     type2 var2 = ps[1];
        ///     ...
        ///     {branch code}
        /// }
        /// </summary>
        /// <param name="compiler"></param>
        /// <param name="method"></param>
        /// <returns>true:all barnches of the main thread have method return or goto; false: at least one branch of the main thread does not have method return or goto</returns>
        public bool ExportCode(ILimnorCodeCompiler compiler, CodeMemberMethod method, CodeStatementCollection statements)
        {
            bool bRet = false;

            MathNode.Trace("BranchList.ExportCode. Method {0}, action blocks {1}================", method.Name, this.Count);
            //create code threads
            List <ActionBranch> independentThreads;

            if (_independentThreads == null)
            {
                independentThreads = FindoutActionThreads(true);
            }
            else
            {
                independentThreads = _independentThreads;
            }
            IsMultiThreads = (independentThreads.Count > 1);
            //the case of Count == 0 (empty method) is handled by MethodClass.ExportCode
            if (independentThreads.Count > 0)
            {
                int k0 = 0;                // main thread index
                this.ActionThreads.Clear();
                foreach (ActionBranch a in independentThreads)
                {
                    _threads.Add(a.BranchId, a);
                }
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (independentThreads[k].IsMainThread)
                    {
                        k0 = k;
                        break;
                    }
                }
                if (k0 == 0)
                {
                    independentThreads[0].IsMainThread = true;
                }
                this.MainThreadId = independentThreads[k0].BranchId;
                List <UInt32> usedBranches = new List <uint>();
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    independentThreads[k].IsMainThread = (k == k0);
                    independentThreads[k].SetIsMainThreadForSubBranches(usedBranches);
                }
                //generate additional threads
                CodeExpression[] obs = null;
                CodeExpression[] ps  = null;
                for (int k = 0; k < independentThreads.Count; k++)
                {
                    if (k != k0)
                    {
                        ActionBranch ab = independentThreads[k];
                        ab.IsMainThread = false;
                        //method for this additional thread
                        CodeMemberMethod mt = new CodeMemberMethod();
                        mt.Name = "thread_" + ab.BranchId.ToString("x");
                        compiler.TypeDeclaration.Members.Add(mt);
                        mt.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data"));
                        //use an object[] to hold the method parameters which is passed to the thread method via the data parameter
                        string pas = "******" + Guid.NewGuid().GetHashCode().ToString("x");
                        mt.Statements.Add(new CodeVariableDeclarationStatement(typeof(object[]), pas,
                                                                               new CodeCastExpression(typeof(object[]), new CodeArgumentReferenceExpression("data"))));
                        //create variables named after the method parameters and assign values to them
                        //so that the actions within the method can use them as if they are method parameters.
                        for (int i = 0; i < _method.ParameterCount; i++)
                        {
                            mt.Statements.Add(new CodeVariableDeclarationStatement(_method.Parameters[i].TypeString,
                                                                                   _method.Parameters[i].Name,
                                                                                   new CodeCastExpression(_method.Parameters[i].TypeString,
                                                                                                          new CodeArrayIndexerExpression(new CodeVariableReferenceExpression(pas),
                                                                                                                                         new CodePrimitiveExpression(i)))));
                        }
                        //method contents: a single thread
                        ab.ExportThreadCode(compiler, mt, mt.Statements);
                        //
                        if (compiler.Debug)
                        {
                            mt.Statements.Add(new CodeExpressionStatement(
                                                  new CodeMethodInvokeExpression(
                                                      new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), LimnorDebugger.Debugger), "ThreadFinished"
                                                      //,new CodePrimitiveExpression(ab.BranchId)
                                                      )
                                                  )
                                              );
                        }
                        //code to launch this thread
                        if (obs == null)
                        {
                            //create an object[] to hold all method parameters to be passed into the thread method
                            obs = new CodeExpression[_method.ParameterCount];
                            for (int i = 0; i < _method.ParameterCount; i++)
                            {
                                obs[i] = new CodeArgumentReferenceExpression(_method.Parameters[i].Name);
                            }
                        }
                        //parameters for launch the thread
                        ps  = new CodeExpression[2];
                        pas = "******" + Guid.NewGuid().GetHashCode().ToString("x");
                        statements.Add(
                            new CodeVariableDeclarationStatement(typeof(object[]), pas, new CodeArrayCreateExpression(typeof(object), obs)));
                        //delegate to the thread method
                        ps[0] = new CodeDelegateCreateExpression(new CodeTypeReference(typeof(System.Threading.WaitCallback)),
                                                                 new CodeThisReferenceExpression(), mt.Name);
                        //parameter to the thread method
                        ps[1] = new CodeVariableReferenceExpression(pas);
                        //queue the thread
                        statements.Add(new CodeMethodInvokeExpression(
                                           new CodeTypeReferenceExpression(typeof(System.Threading.ThreadPool)),
                                           "QueueUserWorkItem", ps));
                    }
                }
                //main thread code
                bRet = independentThreads[k0].ExportThreadCode(compiler, method, statements);
                //
                if (_method == null)
                {
                    MathNode.LogError("method is null");
                }
                else                 //
                {
                    if (_method.ReturnValue != null)
                    {
                        if (!typeof(void).Equals(_method.ReturnValue.ObjectType))
                        {
                            //check whether all branches ends with a method return statement
                            if (!independentThreads[k0].AllBranchesEndWithMethodReturnStatement())
                            {
                                CodeExpression pe;
                                if (_method.ReturnValue.ObjectType.IsArray)
                                {
                                    pe = new CodePrimitiveExpression(null);
                                }
                                else
                                {
                                    pe = ObjectCreationCodeGen.ObjectCreationCode(VPLUtil.GetDefaultValue(_method.ReturnValue.ObjectType));
                                }
                                statements.Add(new CodeMethodReturnStatement(pe));
                            }
                        }
                    }
                }
            }
            MathNode.Trace("End of BranchList.ExportCode. Method {0}================", method.Name);
            return(bRet);
        }