Example #1
0
void ModelFactory::createModel(Coordinator& coordinator,
                               vpz::AtomicModel* model,
                               const std::string& dynamics,
                               const std::vector < std::string >& conditions,
                               const std::string& observable)
{
    const vpz::Dynamic& dyn = mDynamics.get(dynamics);

    const SimulatorMap& result(coordinator.modellist());
    if (result.find(model) != result.end()) {
        throw utils::InternalError(fmt(_(
                "The model '%1%' already exist in coordinator")) %
            model->getName());
    }

    Simulator* sim = new Simulator(model);
    coordinator.addModel(model, sim);

    value::Map initValues;
    if (not conditions.empty()) {
        for (std::vector < std::string >::const_iterator it =
             conditions.begin(); it != conditions.end(); ++it) {
	    const vpz::Condition& cnd(mExperiment.conditions().get(*it));
	    value::MapValue vl;
	    cnd.fillWithFirstValues(vl);

	    for (value::MapValue::const_iterator itv = vl.begin();
		 itv != vl.end(); ++itv) {

                if (initValues.exist(itv->first)) {
                    initValues.value().clear();
                    throw utils::InternalError(fmt(_(
                            "Multiples condition with the same init port " \
                            "name '%1%'")) % itv->first);
                }
                initValues.add(itv->first, itv->second);
            }

            vl.clear();
	}
    }

    try {
        sim->addDynamics(attachDynamics(coordinator, sim, dyn, initValues));
    } catch(const std::exception& /*e*/) {
        initValues.value().clear();
        throw;
    }

    initValues.value().clear();

    if (not observable.empty()) {
        vpz::Observable& ob(mExperiment.views().observables().get(observable));
        const vpz::ObservablePortList& lst(ob.observableportlist());

        for (vpz::ObservablePortList::const_iterator it = lst.begin();
             it != lst.end(); ++it) {
            const vpz::ViewNameList& vnlst(it->second.viewnamelist());
            for (vpz::ViewNameList::const_iterator jt = vnlst.begin();
                 jt != vnlst.end(); ++jt) {

                View* view = coordinator.getView(*jt);

                if (not view) {
                    throw utils::InternalError(fmt(_(
                                "The view '%1%' is unknow of coordinator "
                                "view list")) % *jt);
                }

                view->addObservable(sim, it->first,
                                    coordinator.getCurrentTime());
            }
        }
    }

    InternalEvent* evt = sim->init(coordinator.getCurrentTime());
    if (evt) {
        coordinator.eventtable().putInternalEvent(evt);
    }
}