1 | /*
|
---|
2 | * SubgraphDissectionAction.cpp
|
---|
3 | *
|
---|
4 | * Created on: May 9, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "Helpers/MemDebug.hpp"
|
---|
14 |
|
---|
15 | #include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
|
---|
16 | #include "Actions/ActionRegistry.hpp"
|
---|
17 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
18 |
|
---|
19 | #include "atom.hpp"
|
---|
20 | #include "bond.hpp"
|
---|
21 | #include "bondgraph.hpp"
|
---|
22 | #include "config.hpp"
|
---|
23 | #include "Helpers/Log.hpp"
|
---|
24 | #include "molecule.hpp"
|
---|
25 | #include "stackclass.hpp"
|
---|
26 | #include "World.hpp"
|
---|
27 |
|
---|
28 | #include <iostream>
|
---|
29 | #include <string>
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 |
|
---|
33 | #include "UIElements/UIFactory.hpp"
|
---|
34 | #include "UIElements/Dialog.hpp"
|
---|
35 | #include "Actions/ValueStorage.hpp"
|
---|
36 |
|
---|
37 | const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
|
---|
38 |
|
---|
39 | FragmentationSubgraphDissectionAction::FragmentationSubgraphDissectionAction() :
|
---|
40 | Action(NAME)
|
---|
41 | {}
|
---|
42 |
|
---|
43 | FragmentationSubgraphDissectionAction::~FragmentationSubgraphDissectionAction()
|
---|
44 | {}
|
---|
45 |
|
---|
46 | void FragmentationSubgraphDissection() {
|
---|
47 | ActionRegistry::getInstance().getActionByName(FragmentationSubgraphDissectionAction::NAME)->call(Action::NonInteractive);
|
---|
48 | };
|
---|
49 |
|
---|
50 | Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
|
---|
51 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
52 |
|
---|
53 | dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
|
---|
54 |
|
---|
55 | return dialog;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
|
---|
60 | DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
|
---|
61 | // @TODO rather do the dissection afterwards
|
---|
62 | MoleculeListClass *molecules = World::getInstance().getMolecules();
|
---|
63 | config * const configuration = World::getInstance().getConfig();
|
---|
64 |
|
---|
65 | // 0a. remove all present molecules
|
---|
66 | vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
|
---|
67 | for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
|
---|
68 | molecules->erase(*MolRunner);
|
---|
69 | World::getInstance().destroyMolecule(*MolRunner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | // 0b. remove all bonds and construct a molecule with all atoms
|
---|
73 | molecule *mol = World::getInstance().createMolecule();
|
---|
74 | vector <atom *> allatoms = World::getInstance().getAllAtoms();
|
---|
75 | for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
|
---|
76 | for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
|
---|
77 | delete(*BondRunner);
|
---|
78 | mol->AddAtom(*AtomRunner);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // 1. create the bond structure of the single molecule
|
---|
82 | if (configuration->BG != NULL) {
|
---|
83 | if (!configuration->BG->ConstructBondGraph(mol)) {
|
---|
84 | World::getInstance().destroyMolecule(mol);
|
---|
85 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
|
---|
86 | return Action::failure;
|
---|
87 | }
|
---|
88 | } else {
|
---|
89 | DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
|
---|
90 | return Action::failure;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // 2. scan for connected subgraphs
|
---|
94 | MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
|
---|
95 | class StackClass<bond *> *BackEdgeStack = NULL;
|
---|
96 | Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
|
---|
97 | delete(BackEdgeStack);
|
---|
98 | if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
|
---|
99 | World::getInstance().destroyMolecule(mol);
|
---|
100 | DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
|
---|
101 | return Action::failure;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
|
---|
105 | // the original one as parsed in)
|
---|
106 | // TODO: Optimize this, when molecules just contain pointer list of global atoms!
|
---|
107 |
|
---|
108 | // 4a. create array of molecules to fill
|
---|
109 | const int MolCount = Subgraphs->next->Count();
|
---|
110 | char number[MAXSTRINGSIZE];
|
---|
111 | molecule **moleculelist = new molecule *[MolCount];
|
---|
112 | MoleculeLeafClass *MolecularWalker = Subgraphs;
|
---|
113 | for (int i=0;i<MolCount;i++) {
|
---|
114 | MolecularWalker = MolecularWalker->next;
|
---|
115 | moleculelist[i] = World::getInstance().createMolecule();
|
---|
116 | moleculelist[i]->ActiveFlag = true;
|
---|
117 | strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
|
---|
118 | if (MolCount > 1) {
|
---|
119 | sprintf(number, "-%d", i+1);
|
---|
120 | strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
|
---|
121 | }
|
---|
122 | DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
|
---|
123 | for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
|
---|
124 | DoLog(1) && (Log() << Verbose(1) << **iter << endl);
|
---|
125 | }
|
---|
126 | molecules->insert(moleculelist[i]);
|
---|
127 | }
|
---|
128 |
|
---|
129 | // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
|
---|
130 | int FragmentCounter = 0;
|
---|
131 | map<int, atom *> AtomToFragmentMap;
|
---|
132 | MolecularWalker = Subgraphs;
|
---|
133 | while (MolecularWalker->next != NULL) {
|
---|
134 | MolecularWalker = MolecularWalker->next;
|
---|
135 | for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
|
---|
136 | atom * Walker = *iter;
|
---|
137 | DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
|
---|
138 | MolecularWalker->Leaf->erase(iter);
|
---|
139 | moleculelist[FragmentCounter]->AddAtom(Walker); // counting starts at 1
|
---|
140 | }
|
---|
141 | FragmentCounter++;
|
---|
142 | }
|
---|
143 | // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
|
---|
144 | // 4d. destroy the original molecule
|
---|
145 | for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
|
---|
146 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
147 | World::getInstance().destroyMolecule(mol);
|
---|
148 |
|
---|
149 | // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
|
---|
150 | // TODO: check whether this is really not needed anymore
|
---|
151 | // 4e. free Leafs
|
---|
152 | MolecularWalker = Subgraphs;
|
---|
153 | while (MolecularWalker->next != NULL) {
|
---|
154 | MolecularWalker = MolecularWalker->next;
|
---|
155 | delete(MolecularWalker->previous);
|
---|
156 | }
|
---|
157 | delete(MolecularWalker);
|
---|
158 | delete[](moleculelist);
|
---|
159 | DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
|
---|
160 |
|
---|
161 | return Action::success;
|
---|
162 | }
|
---|
163 |
|
---|
164 | Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
|
---|
165 | // ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
|
---|
166 |
|
---|
167 | return Action::failure;
|
---|
168 | // string newName = state->mol->getName();
|
---|
169 | // state->mol->setName(state->lastName);
|
---|
170 | //
|
---|
171 | // return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
|
---|
172 | }
|
---|
173 |
|
---|
174 | Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
|
---|
175 | return Action::failure;
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool FragmentationSubgraphDissectionAction::canUndo() {
|
---|
179 | return false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool FragmentationSubgraphDissectionAction::shouldUndo() {
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | const string FragmentationSubgraphDissectionAction::getName() {
|
---|
187 | return NAME;
|
---|
188 | }
|
---|