| 1 | /*
 | 
|---|
| 2 |  * DepthFirstSearchAction.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: May 9, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Actions/FragmentationAction/DepthFirstSearchAction.hpp"
 | 
|---|
| 9 | #include "CommandLineParser.hpp"
 | 
|---|
| 10 | #include "atom.hpp"
 | 
|---|
| 11 | #include "config.hpp"
 | 
|---|
| 12 | #include "log.hpp"
 | 
|---|
| 13 | #include "molecule.hpp"
 | 
|---|
| 14 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 15 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| 16 | #include "stackclass.hpp"
 | 
|---|
| 17 | #include "verbose.hpp"
 | 
|---|
| 18 | #include "World.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <iostream>
 | 
|---|
| 21 | #include <string>
 | 
|---|
| 22 | 
 | 
|---|
| 23 | using namespace std;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 26 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 27 | #include "Actions/MapOfActions.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | const char FragmentationDepthFirstSearchAction::NAME[] = "depth-first-search";
 | 
|---|
| 30 | 
 | 
|---|
| 31 | FragmentationDepthFirstSearchAction::FragmentationDepthFirstSearchAction() :
 | 
|---|
| 32 |   Action(NAME)
 | 
|---|
| 33 | {}
 | 
|---|
| 34 | 
 | 
|---|
| 35 | FragmentationDepthFirstSearchAction::~FragmentationDepthFirstSearchAction()
 | 
|---|
| 36 | {}
 | 
|---|
| 37 | 
 | 
|---|
| 38 | Action::state_ptr FragmentationDepthFirstSearchAction::performCall() {
 | 
|---|
| 39 |   Dialog *dialog = UIFactory::getInstance().makeDialog();
 | 
|---|
| 40 |   double distance;
 | 
|---|
| 41 | 
 | 
|---|
| 42 |   dialog->queryDouble(NAME, &distance, MapOfActions::getInstance().getDescription(NAME));
 | 
|---|
| 43 | 
 | 
|---|
| 44 |   if(dialog->display()) {
 | 
|---|
| 45 |     DoLog(1) && (Log() << Verbose(1) << "Depth-First-Search Analysis." << endl);
 | 
|---|
| 46 |     molecule * const mol = World::getInstance().getMolecule(MoleculeById(0));
 | 
|---|
| 47 |     MoleculeLeafClass *Subgraphs = NULL;      // list of subgraphs from DFS analysis
 | 
|---|
| 48 |     int *MinimumRingSize = new int[mol->AtomCount];
 | 
|---|
| 49 |     atom ***ListOfLocalAtoms = NULL;
 | 
|---|
| 50 |     class StackClass<bond *> *BackEdgeStack = NULL;
 | 
|---|
| 51 |     class StackClass<bond *> *LocalBackEdgeStack = NULL;
 | 
|---|
| 52 |     mol->CreateAdjacencyList(distance, World::getInstance().getConfig()->GetIsAngstroem(), &BondGraph::CovalentMinMaxDistance, NULL);
 | 
|---|
| 53 |     Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
 | 
|---|
| 54 |     if (Subgraphs != NULL) {
 | 
|---|
| 55 |       int FragmentCounter = 0;
 | 
|---|
| 56 |       while (Subgraphs->next != NULL) {
 | 
|---|
| 57 |         Subgraphs = Subgraphs->next;
 | 
|---|
| 58 |         Subgraphs->FillBondStructureFromReference(mol, FragmentCounter, ListOfLocalAtoms, false);  // we want to keep the created ListOfLocalAtoms
 | 
|---|
| 59 |         LocalBackEdgeStack = new StackClass<bond *> (Subgraphs->Leaf->BondCount);
 | 
|---|
| 60 |         Subgraphs->Leaf->PickLocalBackEdges(ListOfLocalAtoms[FragmentCounter], BackEdgeStack, LocalBackEdgeStack);
 | 
|---|
| 61 |         Subgraphs->Leaf->CyclicStructureAnalysis(LocalBackEdgeStack, MinimumRingSize);
 | 
|---|
| 62 |         delete(LocalBackEdgeStack);
 | 
|---|
| 63 |         delete(Subgraphs->previous);
 | 
|---|
| 64 |         FragmentCounter++;
 | 
|---|
| 65 |       }
 | 
|---|
| 66 |       delete(Subgraphs);
 | 
|---|
| 67 |       for (int i=0;i<FragmentCounter;i++)
 | 
|---|
| 68 |         Free(&ListOfLocalAtoms[i]);
 | 
|---|
| 69 |       Free(&ListOfLocalAtoms);
 | 
|---|
| 70 |     }
 | 
|---|
| 71 |     delete(BackEdgeStack);
 | 
|---|
| 72 |     delete[](MinimumRingSize);
 | 
|---|
| 73 |     delete dialog;
 | 
|---|
| 74 |     return Action::success;
 | 
|---|
| 75 |   } else {
 | 
|---|
| 76 |     delete dialog;
 | 
|---|
| 77 |     return Action::failure;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | Action::state_ptr FragmentationDepthFirstSearchAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 82 | //  ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
 | 
|---|
| 83 | 
 | 
|---|
| 84 |   return Action::failure;
 | 
|---|
| 85 | //  string newName = state->mol->getName();
 | 
|---|
| 86 | //  state->mol->setName(state->lastName);
 | 
|---|
| 87 | //
 | 
|---|
| 88 | //  return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | Action::state_ptr FragmentationDepthFirstSearchAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 92 |   return Action::failure;
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | bool FragmentationDepthFirstSearchAction::canUndo() {
 | 
|---|
| 96 |   return false;
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | bool FragmentationDepthFirstSearchAction::shouldUndo() {
 | 
|---|
| 100 |   return false;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | const string FragmentationDepthFirstSearchAction::getName() {
 | 
|---|
| 104 |   return NAME;
 | 
|---|
| 105 | }
 | 
|---|