| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * BondLengthTableAction.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: May 9, 2010
|
|---|
| 12 | * Author: heber
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | // include headers that implement a archive in simple text format
|
|---|
| 21 | #include <boost/archive/text_oarchive.hpp>
|
|---|
| 22 | #include <boost/archive/text_iarchive.hpp>
|
|---|
| 23 |
|
|---|
| 24 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 25 |
|
|---|
| 26 | #include "Graph/BondGraph.hpp"
|
|---|
| 27 | #include "config.hpp"
|
|---|
| 28 | #include "CodePatterns/Log.hpp"
|
|---|
| 29 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 30 | #include "World.hpp"
|
|---|
| 31 |
|
|---|
| 32 | #include <iostream>
|
|---|
| 33 | #include <string>
|
|---|
| 34 |
|
|---|
| 35 | #include "Actions/CommandAction/BondLengthTableAction.hpp"
|
|---|
| 36 |
|
|---|
| 37 | using namespace MoleCuilder;
|
|---|
| 38 |
|
|---|
| 39 | // and construct the stuff
|
|---|
| 40 | #include "BondLengthTableAction.def"
|
|---|
| 41 | #include "Action_impl_pre.hpp"
|
|---|
| 42 | /** =========== define the function ====================== */
|
|---|
| 43 | Action::state_ptr CommandBondLengthTableAction::performCall() {
|
|---|
| 44 | ostringstream usage;
|
|---|
| 45 |
|
|---|
| 46 | // obtain information
|
|---|
| 47 | getParametersfromValueStorage();
|
|---|
| 48 |
|
|---|
| 49 | DoLog(0) && (Log() << Verbose(0) << "Using " << params.BondGraphFileName << " as bond length table." << endl);
|
|---|
| 50 | BondGraph *&BG = World::getInstance().getBondGraph();
|
|---|
| 51 |
|
|---|
| 52 | // create undo state
|
|---|
| 53 | std::stringstream undostream;
|
|---|
| 54 | boost::archive::text_oarchive oa(undostream);
|
|---|
| 55 | oa << BG;
|
|---|
| 56 | CommandBondLengthTableState *UndoState =
|
|---|
| 57 | new CommandBondLengthTableState(
|
|---|
| 58 | undostream.str(),
|
|---|
| 59 | params
|
|---|
| 60 | );
|
|---|
| 61 |
|
|---|
| 62 | BG->CleanupBondLengthTable();
|
|---|
| 63 | if ((!params.BondGraphFileName.empty())
|
|---|
| 64 | && boost::filesystem::exists(params.BondGraphFileName)) {
|
|---|
| 65 | std::ifstream input(params.BondGraphFileName.string().c_str());
|
|---|
| 66 | if ((input.good()) && (BG->LoadBondLengthTable(input))) {
|
|---|
| 67 | DoLog(0) && (Log() << Verbose(0) << "Bond length table parsed successfully." << endl);
|
|---|
| 68 | input.close();
|
|---|
| 69 | return Action::state_ptr(UndoState);
|
|---|
| 70 | } else {
|
|---|
| 71 | DoeLog(1) && (eLog()<< Verbose(1) << "Bond length table parsing failed." << endl);
|
|---|
| 72 | input.close();
|
|---|
| 73 | }
|
|---|
| 74 | } else {
|
|---|
| 75 | DoeLog(1) && (eLog()<< Verbose(1) << "Bond length table loading failed." << endl);
|
|---|
| 76 | }
|
|---|
| 77 | // recover bond graph
|
|---|
| 78 | boost::archive::text_iarchive ia(undostream);
|
|---|
| 79 | delete BG;
|
|---|
| 80 | ia >> BG;
|
|---|
| 81 | delete UndoState;
|
|---|
| 82 | return Action::failure;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | Action::state_ptr CommandBondLengthTableAction::performUndo(Action::state_ptr _state) {
|
|---|
| 86 | CommandBondLengthTableState *state = assert_cast<CommandBondLengthTableState*>(_state.get());
|
|---|
| 87 |
|
|---|
| 88 | BondGraph *BG;
|
|---|
| 89 | std::stringstream undostream(state->undostring);
|
|---|
| 90 | boost::archive::text_iarchive ia(undostream);
|
|---|
| 91 | ia >> BG;
|
|---|
| 92 | World::getInstance().setBondGraph(BG);
|
|---|
| 93 |
|
|---|
| 94 | return Action::state_ptr(_state);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | Action::state_ptr CommandBondLengthTableAction::performRedo(Action::state_ptr _state){
|
|---|
| 98 | CommandBondLengthTableState *state = assert_cast<CommandBondLengthTableState*>(_state.get());
|
|---|
| 99 |
|
|---|
| 100 | BondGraph *&BG = World::getInstance().getBondGraph();
|
|---|
| 101 | BG->CleanupBondLengthTable();
|
|---|
| 102 | std::ifstream input(state->params.BondGraphFileName.string().c_str());
|
|---|
| 103 | if ((input.good()) && (BG->LoadBondLengthTable(input))) {
|
|---|
| 104 | DoLog(0) && (Log() << Verbose(0) << "Bond length table parsed successfully." << endl);
|
|---|
| 105 | input.close();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | return Action::state_ptr(_state);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | bool CommandBondLengthTableAction::canUndo() {
|
|---|
| 112 | return true;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | bool CommandBondLengthTableAction::shouldUndo() {
|
|---|
| 116 | return true;
|
|---|
| 117 | }
|
|---|
| 118 | /** =========== end of function ====================== */
|
|---|