object::Object* Simulation::buildObject(const String& name, object::Object::Type type) { // Try to find object internal object type auto desc = findObjectType(name); if (desc) { // Create parent object auto obj = buildObject(desc->baseName); // Configure obj->configure(desc->config, *this); return obj; } Log::debug("Create object '", name, "'"); // Create object with given name auto object = getPluginContext().createObject(name, *this, type); // Register module if (object) return addObject(std::move(object)); Log::warning("Unable to create object: ", name, " (unsupported by library?)"); return nullptr; }
//---------------------------------------------------------------------------- long ctkPluginFrameworkLauncher::install(const QString& symbolicName, ctkPluginContext* context) { QString pluginPath = getPluginPath(symbolicName); if (pluginPath.isEmpty()) return -1; ctkPluginContext* pc = context; if (pc == 0 && d->fwFactory == 0) { d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps); try { d->fwFactory->getFramework()->init(); pc = getPluginContext(); } catch (const ctkPluginException& exc) { qCritical() << "Failed to initialize the plug-in framework:" << exc; delete d->fwFactory; d->fwFactory = 0; return -1; } } try { return pc->installPlugin(QUrl::fromLocalFile(pluginPath))->getPluginId(); } catch (const ctkPluginException& exc) { qWarning() << "Failed to install plugin:" << exc; return -1; } }
//---------------------------------------------------------------------------- bool ctkPluginFrameworkLauncher::start(const QString& symbolicName, ctkPlugin::StartOptions options, ctkPluginContext* context) { // instantiate and start the framework if (context == 0 && d->fwFactory == 0) { d->fwFactory = new ctkPluginFrameworkFactory(d->fwProps); try { d->fwFactory->getFramework()->start(); } catch (const ctkPluginException& exc) { qCritical() << "Failed to start the plug-in framework:" << exc; delete d->fwFactory; d->fwFactory = 0; return false; } } else if (context == 0 && d->fwFactory->getFramework()->getState() != ctkPlugin::ACTIVE) { try { d->fwFactory->getFramework()->start(options); } catch (const ctkPluginException& exc) { qCritical() << "Failed to start the plug-in framework:" << exc; delete d->fwFactory; d->fwFactory = 0; return false; } } if(!symbolicName.isEmpty()) { QString pluginPath = getPluginPath(symbolicName); if (pluginPath.isEmpty()) return false; ctkPluginContext* pc = context ? context : getPluginContext(); try { pc->installPlugin(QUrl::fromLocalFile(pluginPath))->start(options); } catch (const ctkPluginException& exc) { qWarning() << "Failed to install plugin:" << exc; return false; } } return true; }
UniquePtr<program::Program> Simulation::buildProgram(StringView name) { Log::debug("Create program '", name, "'"); // Create a program auto program = getPluginContext().createProgram(name); if (program) return program; Log::warning("Unable to create program '", name, "'"); return nullptr; }
ViewPtr<module::Module> Simulation::useModule(const String& nameSrc, String storePath) { String name = nameSrc; // Find ':' in name - old definition auto pos = nameSrc.find(':'); if (pos != String::npos) { name = nameSrc.substr(0, pos); storePath = nameSrc.substr(pos + 1); } if (storePath.empty()) storePath = name; // Module exists, return the existing one if (hasModule(storePath)) return getModule(storePath); Log::debug("Loading library: ", name); // Load only library Log::debug("Create module '", name, "'"); // Create module with given name auto module = getPluginContext().createModule(name, *this); // Register module if (module) { if (storePath != name) { Log::info("Using module '", name, "' as '", storePath, "'"); } else { Log::info("Using module '", name, "'"); } return addModule(storePath, std::move(module)); } Log::warning("Unable to create module '", name, "' (unsupported by library?)"); return nullptr; }
//---------------------------------------------------------------------------- bool ctkPluginFrameworkLauncher::stop(const QString& symbolicName, ctkPlugin::StopOptions options, ctkPluginContext* context) { if (d->fwFactory == 0) return true; ctkPluginContext* pc = context ? context : getPluginContext(); if (pc == 0) { qWarning() << "No valid plug-in context available"; return false; } if(!symbolicName.isEmpty()) { QString pluginPath = getPluginPath(symbolicName); if (pluginPath.isEmpty()) return false; try { QList<QSharedPointer<ctkPlugin> > plugins = pc->getPlugins(); foreach(QSharedPointer<ctkPlugin> plugin, plugins) { if (plugin->getSymbolicName() == symbolicName) { plugin->stop(options); return true; } } qWarning() << "Plug-in" << symbolicName << "not found"; return false; } catch (const ctkPluginException& exc) { qWarning() << "Failed to stop plug-in:" << exc; return false; } }
void Simulation::configure(const config::Configuration& config) { // Resize world { auto size = config.get<SizeVector>("world-size"); if (size.getWidth() == Zero || size.getHeight() == Zero) throw config::Exception("Width or height is zero!"); setWorldSize(size); } // Time step setTimeStep(config.get<units::Time>("dt")); if (config.has("length-coefficient")) { m_converter.setLengthCoefficient(config.get<RealType>("length-coefficient")); } // Set gravity setGravity(config.get("gravity", getGravity())); // Number of iterations setIterations(config.get("iterations", getIterations())); // Background color setBackgroundColor(config.get("background", getBackgroundColor())); #if CONFIG_RENDER_TEXT_ENABLE setFontColor(config.get("text-color", getBackgroundColor().inverted())); #endif #if CONFIG_RENDER_TEXT_ENABLE setFontSize(config.get("text-size", getFontSize())); #endif #if CONFIG_RENDER_TEXT_ENABLE setSimulationTimeRender(config.get("show-simulation-time", isSimulationTimeRender())); #endif #ifdef CECE_ENABLE_RENDER setVisualized(config.get("visualized", isVisualized())); #endif // Parse plugins for (auto&& pluginConfig : config.getConfigurations("plugin")) { // Returns valid pointer or throws an exception requirePlugin(pluginConfig.get("name"))->configure(*this, pluginConfig); } // Parse parameters for (auto&& parameterConfig : config.getConfigurations("parameter")) { setParameter(parameterConfig.get("name"), units::parse(parameterConfig.get("value"))); } // Register user types for (auto&& typeConfig : config.getConfigurations("type")) { addObjectType({ typeConfig.get("name"), typeConfig.get("base"), typeConfig.toMemory() }); } // Parse init for (auto&& initConfig : config.getConfigurations("init")) { const String typeName = initConfig.has("language") ? initConfig.get("language") : initConfig.get("type"); auto initializer = getPluginContext().createInitializer(typeName); if (initializer) { // Configure initializer initializer->loadConfig(*this, initConfig); // Register initializer addInitializer(std::move(initializer)); } } // Parse modules for (auto&& moduleConfig : config.getConfigurations("module")) { // Get name auto name = moduleConfig.get("name"); if (hasModule(name)) continue; const String typeName = moduleConfig.has("language") ? moduleConfig.get("language") : moduleConfig.has("type") ? moduleConfig.get("type") : name ; auto module = getPluginContext().createModule(typeName, *this); if (module) { module->loadConfig(*this, moduleConfig); addModule(std::move(name), std::move(module)); } } // Parse programs for (auto&& programConfig : config.getConfigurations("program")) { const String typeName = programConfig.has("language") ? programConfig.get("language") : programConfig.get("type"); auto program = getPluginContext().createProgram(typeName); if (program) { // Configure program program->loadConfig(*this, programConfig); // Register program addProgram(programConfig.get("name"), std::move(program)); } } // Parse objects for (auto&& objectConfig : config.getConfigurations("object")) { // Create object auto object = buildObject( objectConfig.get("class"), objectConfig.get("type", object::Object::Type::Dynamic) ); if (object) object->configure(objectConfig, *this); } if (config.has("data-out-objects-filename")) { m_dataOutObjects = makeUnique<OutFileStream>(config.get("data-out-objects-filename")); *m_dataOutObjects << "iteration;totalTime;id;typeName;posX;posY;velX;velY\n"; } }