Esempio n. 1
0
        public void LoadApplet(string filename)
        {
            Reset(null, EventArgs.Empty);
            CurrentFilename = filename;
            SetCaption(filename);
            applet = MycroParser.InstantiateFromFile <Applet>(filename, null);

            // Create the membranes and their containing receptors:
            List <Membrane> membraneList = new List <Membrane>();

            membraneList.Add(Program.Skin);

            VisualizerController.View.StartDrop     = true;
            VisualizerController.View.ShowMembranes = false;                            // don't show membranes while we're loading the applet.  Causes problems because the model isn't fully loaded!
            // Skin is the the root membrane.  It has no siblings.
            DeserializeMembranes(applet.MembranesDef.Membranes[0], Program.Skin, membraneList);
            VisualizerController.View.StartDrop     = false;
            VisualizerController.View.ShowMembranes = true;

            // Create the carriers if they exist.

            if (applet.CarriersDef != null)
            {
                applet.CarriersDef.Carriers.ForEach(c =>
                {
                    ISemanticTypeStruct protocol = Program.Skin.SemanticTypeSystem.GetSemanticTypeStruct(c.Protocol);
                    dynamic signal = Program.Skin.SemanticTypeSystem.Create(c.Protocol);
                    Type t         = signal.GetType();

                    c.Attributes.ForEach(attr =>
                    {
                        PropertyInfo pi      = t.GetProperty(attr.Name);
                        TypeConverter tcFrom = TypeDescriptor.GetConverter(pi.PropertyType);

                        if (tcFrom.CanConvertFrom(typeof(string)))
                        {
                            object val = tcFrom.ConvertFromInvariantString(attr.Value);
                            pi.SetValue(signal, val);
                        }
                        else
                        {
                            throw new ApplicationException("Cannot convert string to type " + t.Name);
                        }
                    });

                    // TODO: Carriers need to specify into which membrane they are placed.
                    Membrane inMembrane = membraneList.SingleOrDefault(m => m.Name == c.Membrane);

                    if (inMembrane != null)
                    {
                        inMembrane.CreateCarrier(Program.Skin["DropReceptor"].Instance, protocol, signal);
                    }
                    else
                    {
                        // TODO: Inform user that carrier is not associated with a defined membrane.
                    }
                });
            }

            // When we're all done, recreate the connections because the membrane permeability might have changed.
            // TODO: Should this be an event the visualizer picks up on?
            VisualizerController.View.UpdateConnections();

            // Inform all receptors that the system is fully initialized.
            Program.Skin.EndSystemInit();
        }