| [cabb46] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [cabb46] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * LoadAction.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: May 8, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [cabb46] | 21 | 
 | 
|---|
| [ad011c] | 22 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 23 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [78d5b2] | 24 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| [765f16] | 25 | #include "Parser/FormatParserInterface.hpp"
 | 
|---|
| [cabb46] | 26 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| [78d5b2] | 27 | #include "Parser/FormatParser_Parameters.hpp"
 | 
|---|
| [cabb46] | 28 | #include "molecule.hpp"
 | 
|---|
| [42127c] | 29 | #include "MoleculeListClass.hpp"
 | 
|---|
| [cabb46] | 30 | #include "World.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | #include <iostream>
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | #include <boost/filesystem/fstream.hpp>
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include "LoadAction.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
| [ce7fdc] | 38 | using namespace MoleCuilder;
 | 
|---|
 | 39 | 
 | 
|---|
| [cabb46] | 40 | // and construct the stuff
 | 
|---|
 | 41 | #include "LoadAction.def"
 | 
|---|
 | 42 | #include "Action_impl_pre.hpp"
 | 
|---|
 | 43 | /** =========== define the function ====================== */
 | 
|---|
 | 44 | Action::state_ptr MoleculeLoadAction::performCall() {
 | 
|---|
 | 45 |   // parsing file if present
 | 
|---|
 | 46 |   if (!boost::filesystem::exists(params.filename)) {
 | 
|---|
| [47d041] | 47 |     LOG(1, "Specified input file " << params.filename << " not found.");
 | 
|---|
| [78d5b2] | 48 |     return Action::failure;
 | 
|---|
| [cabb46] | 49 |   } else {
 | 
|---|
| [47d041] | 50 |     LOG(1, "Specified input file found, parsing ... ");
 | 
|---|
| [cabb46] | 51 | 
 | 
|---|
 | 52 |     // extract suffix
 | 
|---|
 | 53 |     std::string FilenameSuffix;
 | 
|---|
 | 54 |     std::string FilenamePrefix;
 | 
|---|
 | 55 |     if (params.filename.has_filename()) {
 | 
|---|
 | 56 |       // get suffix
 | 
|---|
| [632a52] | 57 | #if BOOST_VERSION >= 104600
 | 
|---|
 | 58 |       FilenameSuffix = params.filename.extension().string().substr(1); // remove the prefixed "."
 | 
|---|
 | 59 |       FilenamePrefix = params.filename.stem().string();
 | 
|---|
 | 60 | #else
 | 
|---|
| [cabb46] | 61 |       FilenameSuffix = params.filename.extension().substr(1); // remove the prefixed "."
 | 
|---|
 | 62 |       FilenamePrefix = params.filename.stem();
 | 
|---|
| [632a52] | 63 | #endif
 | 
|---|
| [cabb46] | 64 |     } else {
 | 
|---|
| [47d041] | 65 |       ELOG(1, "Input file does not have a suffix, cannot recognize format.");
 | 
|---|
| [cabb46] | 66 |       return Action::failure;
 | 
|---|
 | 67 |     }
 | 
|---|
 | 68 | 
 | 
|---|
| [78d5b2] | 69 |     // get undo state for parser
 | 
|---|
 | 70 |     enum ParserTypes type = FormatParserStorage::getInstance().getTypeFromSuffix(FilenameSuffix);
 | 
|---|
| [ebb87c] | 71 |     ASSERT(type != ParserTypes_end,
 | 
|---|
 | 72 |         "MoleculeLoadAction::performCall() - unknown file suffix "+FilenameSuffix+".");
 | 
|---|
| [78d5b2] | 73 |     FormatParser_Parameters *ParserParams = FormatParserStorage::getInstance().get(type).parameters;
 | 
|---|
 | 74 |     if (ParserParams != NULL)
 | 
|---|
 | 75 |       ParserParams = ParserParams->clone();
 | 
|---|
 | 76 |     else
 | 
|---|
 | 77 |       ParserParams = NULL;
 | 
|---|
 | 78 | 
 | 
|---|
| [cabb46] | 79 |     // parse the file
 | 
|---|
 | 80 |     boost::filesystem::ifstream input;
 | 
|---|
 | 81 |     input.open(params.filename);
 | 
|---|
| [73916f] | 82 |     FormatParserStorage::getInstance().load(input, FilenameSuffix);
 | 
|---|
| [cabb46] | 83 |     input.close();
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |     // set file name of last molecule
 | 
|---|
 | 86 |     MoleculeList::const_reverse_iterator iter = World::getInstance().getMolecules()->ListOfMolecules.rbegin();
 | 
|---|
 | 87 |     (*iter)->SetNameFromFilename(FilenamePrefix.c_str());
 | 
|---|
| [47d041] | 88 |     LOG(0, "Chemical formula is " << (*iter)->getFormula());
 | 
|---|
| [78d5b2] | 89 | 
 | 
|---|
 | 90 |     return Action::state_ptr(
 | 
|---|
 | 91 |         new MoleculeLoadState((*iter)->getId(),FilenamePrefix,FilenameSuffix,boost::shared_ptr<FormatParser_Parameters>(ParserParams),params)
 | 
|---|
 | 92 |     );
 | 
|---|
| [cabb46] | 93 |   }
 | 
|---|
 | 94 | }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | Action::state_ptr MoleculeLoadAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| [78d5b2] | 97 |   MoleculeLoadState *state = assert_cast<MoleculeLoadState*>(_state.get());
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |   // remove loaded molecule and its atoms
 | 
|---|
 | 100 |   molecule *mol = World::getInstance().getMolecule(MoleculeById(state->molId));
 | 
|---|
 | 101 |   for(molecule::iterator iter = mol->begin(); !mol->empty(); iter = mol->begin())
 | 
|---|
 | 102 |     World::getInstance().destroyAtom(*iter);
 | 
|---|
 | 103 |   //World::getInstance().destroyMolecule(mol);
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |   // undo changes to FormatParser
 | 
|---|
 | 106 |   enum ParserTypes type = FormatParserStorage::getInstance().getTypeFromSuffix(state->FilenameSuffix);
 | 
|---|
 | 107 |   FormatParser_Parameters *ParserParams = FormatParserStorage::getInstance().get(type).parameters;
 | 
|---|
 | 108 |   if (ParserParams != NULL)
 | 
|---|
 | 109 |     ParserParams->makeClone(*state->ParserParameters);
 | 
|---|
| [cabb46] | 110 | 
 | 
|---|
| [78d5b2] | 111 |   return Action::state_ptr(_state);
 | 
|---|
| [cabb46] | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | Action::state_ptr MoleculeLoadAction::performRedo(Action::state_ptr _state){
 | 
|---|
| [78d5b2] | 115 |   MoleculeLoadState *state = assert_cast<MoleculeLoadState*>(_state.get());
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 |   // get undo state for parser
 | 
|---|
 | 118 |   enum ParserTypes type = FormatParserStorage::getInstance().getTypeFromSuffix(state->FilenameSuffix);
 | 
|---|
 | 119 |   FormatParser_Parameters *ParserParams = FormatParserStorage::getInstance().get(type).parameters;
 | 
|---|
 | 120 |   if (ParserParams != NULL)
 | 
|---|
 | 121 |     ParserParams = ParserParams->clone();
 | 
|---|
 | 122 |   else
 | 
|---|
 | 123 |     ParserParams = NULL;
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   // parse the file
 | 
|---|
 | 126 |   boost::filesystem::ifstream input;
 | 
|---|
 | 127 |   input.open(state->params.filename);
 | 
|---|
 | 128 |   FormatParserStorage::getInstance().load(input, state->FilenameSuffix);
 | 
|---|
 | 129 |   input.close();
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   // set file name of last molecule
 | 
|---|
 | 132 |   MoleculeList::const_reverse_iterator iter = World::getInstance().getMolecules()->ListOfMolecules.rbegin();
 | 
|---|
 | 133 |   (*iter)->SetNameFromFilename(state->FilenamePrefix.c_str());
 | 
|---|
 | 134 |   (*iter)->setId(state->molId);
 | 
|---|
| [47d041] | 135 |   LOG(0, "Chemical formula is " << (*iter)->getFormula());
 | 
|---|
| [78d5b2] | 136 | 
 | 
|---|
 | 137 |   return Action::state_ptr(
 | 
|---|
 | 138 |       new MoleculeLoadState((*iter)->getId(),state->FilenamePrefix,state->FilenameSuffix,boost::shared_ptr<FormatParser_Parameters>(ParserParams),params)
 | 
|---|
 | 139 |   );
 | 
|---|
| [cabb46] | 140 | }
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 | bool MoleculeLoadAction::canUndo() {
 | 
|---|
| [e69c87] | 143 |   return true;
 | 
|---|
| [cabb46] | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | bool MoleculeLoadAction::shouldUndo() {
 | 
|---|
| [e69c87] | 147 |   return true;
 | 
|---|
| [cabb46] | 148 | }
 | 
|---|
 | 149 | /** =========== end of function ====================== */
 | 
|---|