Exemplo n.º 1
0
void Program::loadConfig(simulator::Simulation& simulation, const config::Configuration& config)
{
    if (config.has("filename"))
    {
        m_source.initFile(config.get("filename"));
    }
    else
    {
        m_source.initSource(config.getContent());
    }

    // Store function pointer
    m_call = m_source.getFunction("__call__");

    // Fallback
    if (!m_call)
        m_call = m_source.getFunction("call");

    if (!m_call)
        throw InvalidArgumentException("Python program doesn't have '__call__' or 'call' function");
}
Exemplo n.º 2
0
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";
    }
}