public SimulationGraph(StorageLocation location, DateTime closeOfBusinessDate) { Context = new Context(location, closeOfBusinessDate); var rootNodeKey = new ModelKey("Root", typeof(DummyModel)); var rootNode = new SimulationNode(rootNodeKey); rootNode.Model = Model.Create <DummyModel>("Root", null); _nodes[rootNodeKey] = rootNode; _nodeStack.Push(rootNode); }
public T RegisterModel <T>(string identifier) where T : Model { var modelKey = new ModelKey(identifier, typeof(T)); SimulationNode node; if (!_nodes.TryGetValue(modelKey, out node)) { var model = _factory.CreateModel(typeof(T), identifier, this) as T; node = AddModelToGraph(modelKey, model); } _nodeStack.Peek().AddRequisite(node); return((T)node.Model); }
private SimulationNode AddModelToGraph(ModelKey key, Model model) { var node = new SimulationNode(key) { Model = model }; _nodes[key] = node; _nodeStack.Push(node); // add children here model.Initialise(this); _nodeStack.Pop(); return(node); }
private T GetModel <T>(Type modelType, string identifier, Simulation simulation) where T : class { var key = new ModelKey(identifier, modelType); Model model = null; if (!_models.TryGetValue(key, out model)) { try { model = Activator.CreateInstance(modelType, new object[] { identifier, simulation }) as Model; } catch (Exception) { throw new Exception("Model constructor not available"); } } return(model as T); }
public T RegisterFactor <T>(string identifier) where T : Factor { // if the factor already exists, then return it var factorKey = new FactorKey(identifier, typeof(T)); SimulationNode node; ModelKey modelKey; if (!_modelLookup.TryGetValue(factorKey, out modelKey)) { // otherwise create. Note that creating the factor always creates the corresponding model var factor = _factory.CreateFactor(typeof(T), identifier, this) as T; var model = factor.Model; modelKey = new ModelKey(identifier, model.GetType()); node = AddModelToGraph(modelKey, model); node.Factor = factor; _modelLookup[factorKey] = modelKey; } else { node = _nodes[modelKey]; } _nodeStack.Peek().AddRequisite(node); return((T)node.Factor); }
public SimulationNode(ModelKey key) { this.Key = key; }