OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Select.cpp
Go to the documentation of this file.
1 //
2 // Class Select
3 // The class for OPAL SELECT command.
4 //
5 // Copyright (c) 2000 - 2021, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // This file is part of OPAL.
9 //
10 // OPAL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17 //
18 #include "BasicActions/Select.h"
19 
22 #include "AbstractObjects/Table.h"
23 #include "Algorithms/Flagger.h"
24 #include "Attributes/Attributes.h"
25 #include "Tables/Selector.h"
27 #include "Utilities/Options.h"
28 
29 #include <iostream>
30 
31 extern Inform* gmsg;
32 
33 namespace {
34  enum {
35  LINE, // The line to be affected.
36  FULL, // If true, all elements are selected.
37  CLEAR, // If true, all selections are cleared.
38  RANGE, // The range to be considered.
39  CLASS, // The class of elements to be selected.
40  TYPE, // The type name of elements to be selected.
41  PATTERN, // The regular expression for matching names.
42  SIZE
43  };
44 }
45 
47  Action(SIZE, "SELECT",
48  "The \"SELECT\" sub-command selects the positions to be affected "
49  "by subsequent error sub-commands.") {
51  ("LINE",
52  "Name of the lattice to be affected by selections",
53  "UNNAMED_USE");
55  ("FULL",
56  "If true, all elements are selected");
58  ("CLEAR",
59  "If true, all selections are cleared");
61  ("RANGE",
62  "Range to be considered for selection (default: full range)");
64  ("CLASS",
65  "Name of class to be selected (default: all classes)");
67  ("TYPE",
68  "The type name of elements to be selected (default: all types)");
70  ("PATTERN",
71  "Regular expression for matching names (default: all names)");
72 
74 }
75 
76 
77 Select::Select(const std::string& name, Select* parent):
78  Action(name, parent)
79 {}
80 
81 
83 {}
84 
85 
86 Select* Select::clone(const std::string& name) {
87  return new Select(name, this);
88 }
89 
90 
92  // Find beam sequence or table definition.
93  const std::string name = Attributes::getString(itsAttr[LINE]);
94 
95  if (Object* obj = OpalData::getInstance()->find(name)) {
96  if (BeamSequence* line = dynamic_cast<BeamSequence*>(obj)) {
97  select(*line->fetchLine());
98  } else if (Table* table = dynamic_cast<Table*>(obj)) {
99  select(*table->getLine());
100  } else {
101  throw OpalException("Select::execute()",
102  "You cannot do a \"SELECT\" on \"" + name +
103  "\", it is neither a line nor a table.");
104  }
105  } else {
106  throw OpalException("Select::execute()",
107  "Object \"" + name + "\" not found.");
108  }
109 }
110 
111 
112 void Select::select(const Beamline& bl) {
113  if (Attributes::getBool(itsAttr[FULL])) {
114  // Select all positions.
115  Flagger flagger(bl, true);
116  flagger.execute();
117  if (Options::info) {
118  *gmsg << level2 << "\nAll elements selected.\n" << endl;
119  }
120  } else if (Attributes::getBool(itsAttr[CLEAR])) {
121  // Deselect all selections.
122  Flagger flagger(bl, false);
123  flagger.execute();
124  if (Options::info) {
125  *gmsg << level2 << "\nAll elements de-selected.\n" << endl;
126  }
127  } else {
128  Selector sel(bl,
132  Attributes::getString(itsAttr[PATTERN]));
133  sel.execute();
134 
135  if (Options::info) {
136  int count = sel.getCount();
137  if (count == 0) {
138  *gmsg << level2 << "No elements";
139  } else if (count == 1) {
140  *gmsg << level2 << "\n1 element";
141  } else {
142  *gmsg << level2 << '\n' << count << " elements";
143  }
144  *gmsg << level2 << " selected.\n" << endl;
145  }
146  }
147 }
@ SIZE
Definition: IndexMap.cpp:174
Inform * gmsg
Definition: Main.cpp:62
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Inform & level2(Inform &inf)
Definition: Inform.cpp:46
const std::string name
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:90
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
Definition: Attributes.cpp:206
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:100
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
RangeRep getRange(const Attribute &attr)
Get range value.
Definition: Attributes.cpp:212
bool info
Info flag.
Definition: Options.cpp:28
The base class for all OPAL actions.
Definition: Action.h:30
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
The base class for all OPAL objects.
Definition: Object.h:48
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
static OpalData * getInstance()
Definition: OpalData.cpp:195
The base class for all OPAL tables.
Definition: Table.h:42
Definition: Select.h:26
virtual ~Select()
Definition: Select.cpp:82
virtual void execute()
Execute the command.
Definition: Select.cpp:91
void select(const Beamline &)
Definition: Select.cpp:112
virtual Select * clone(const std::string &name)
Make clone.
Definition: Select.cpp:86
Select()
Exemplar constructor.
Definition: Select.cpp:46
virtual void execute()
Apply the algorithm to the top-level beamline.
Set/reset all selection flags in a beam line built from FlaggedElmPtr.
Definition: Flagger.h:31
An abstract sequence of beam line components.
Definition: Beamline.h:34
int getCount() const
Return the count of selected elements.
Definition: Selector.cpp:86
virtual void execute()
Execute the selection.
Definition: Selector.cpp:53
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Definition: Inform.h:42