/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * SetRandomNumbersEngine.cpp * * Created on: Jan 01, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "CodePatterns/Log.hpp" #include "molecule.hpp" #include "CodePatterns/Verbose.hpp" #include "World.hpp" #include "RandomNumbers/RandomNumberEngine.hpp" #include "RandomNumbers/RandomNumberEngine_Parameters.hpp" #include "RandomNumbers/RandomNumberEngineFactory.hpp" #include #include #include #include "SetRandomNumbersEngineAction.hpp" using namespace MoleCuilder; // and construct the stuff #include "SetRandomNumbersEngineAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ Action::state_ptr CommandSetRandomNumbersEngineAction::performCall() { // obtain information getParametersfromValueStorage(); // note down old type std::string oldtype = RandomNumberEngineFactory::getInstance().getCurrentTypeName(); LOG(1, "Current engine is " << oldtype); // set the new default RandomNumberEngineFactory::getInstance().setCurrentType(params.engine_type); LOG(0, "STATUS: Engine of random number generator is now: " << RandomNumberEngineFactory::getInstance().getCurrentTypeName()); // note down old parameters RandomNumberEngine_Parameters *oldparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); // set each parameter (that is not -1); { std::stringstream input(params.parameters); RandomNumberEngine_Parameters *currentparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); input >> *currentparameters; // add new values on top if (!currentparameters->isDefault()) { LOG(1, "Changing prototype's parameters."); RandomNumberEngineFactory::getInstance().manipulatePrototype(*currentparameters); } delete currentparameters; } { RandomNumberEngine_Parameters *currentparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); LOG(1, "Its parameters are: " << *currentparameters); delete currentparameters; } std::stringstream output; output << *oldparameters; CommandSetRandomNumbersEngineState *newstate = new CommandSetRandomNumbersEngineState(oldtype,output.str(),params); delete oldparameters; return Action::state_ptr(newstate); } Action::state_ptr CommandSetRandomNumbersEngineAction::performUndo(Action::state_ptr _state) { CommandSetRandomNumbersEngineState *state = assert_cast(_state.get()); // note down old type for redo std::string newtype = RandomNumberEngineFactory::getInstance().getCurrentTypeName(); LOG(1, "Newly set engine was " << newtype); // note down old parameters for redo RandomNumberEngine_Parameters *newparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); // set the new default RandomNumberEngineFactory::getInstance().setCurrentType(state->old_engine_type); LOG(0, "STATUS: Engine of random number generator is again: " << RandomNumberEngineFactory::getInstance().getCurrentTypeName()); // set each parameter back (that is not -1); { std::stringstream input(state->old_parameters); RandomNumberEngine_Parameters *currentparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); input >> *currentparameters; if (!currentparameters->isDefault()) { LOG(1, "Changing prototype's parameters."); RandomNumberEngineFactory::getInstance().manipulatePrototype(*currentparameters); } delete currentparameters; } { RandomNumberEngine_Parameters *currentparameters = RandomNumberEngineFactory::getInstance().getPrototype().getParameterSet(); LOG(1, "Its parameters are: " << *currentparameters); delete currentparameters; } std::stringstream output; output << *newparameters; CommandSetRandomNumbersEngineState *newstate = new CommandSetRandomNumbersEngineState(newtype,output.str(),params); delete newparameters; return Action::state_ptr(newstate); } Action::state_ptr CommandSetRandomNumbersEngineAction::performRedo(Action::state_ptr _state){ return performUndo(_state); } bool CommandSetRandomNumbersEngineAction::canUndo() { return true; } bool CommandSetRandomNumbersEngineAction::shouldUndo() { return true; } /** =========== end of function ====================== */