OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Select.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Select.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Select
10 // Class for the OPAL SELECT command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:37 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "BasicActions/Select.h"
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 #include <iostream>
29 
30 
31 // Class Select
32 // ------------------------------------------------------------------------
33 
34 // The attributes of class Select.
35 
36 namespace {
37  enum {
38  LINE, // The line to be affected.
39  FULL, // If true, all elements are selected.
40  CLEAR, // If true, all selections are cleared.
41  RANGE, // The range to be considered.
42  CLASS, // The class of elements to be selected.
43  TYPE, // The type name of elements to be selected.
44  PATTERN, // The regular expression for matching names.
45  SIZE
46  };
47 }
48 
49 extern Inform *gmsg;
50 
52  Action(SIZE, "SELECT",
53  "The \"SELECT\" sub-command selects the positions to be affected "
54  "by subsequent error sub-commands.") {
56  ("LINE",
57  "Name of the lattice to be affected by selections",
58  "UNNAMED_USE");
60  ("FULL",
61  "If true, all elements are selected");
63  ("CLEAR",
64  "If true, all selections are cleared");
66  ("RANGE",
67  "Range to be considered for selection (default: full range)");
69  ("CLASS",
70  "Name of class to be selected (default: all classes)");
72  ("TYPE",
73  "The type name of elements to be selected (default: all types)");
75  ("PATTERN",
76  "Regular expression for matching names (default: all names)");
77 
79 }
80 
81 
82 Select::Select(const std::string &name, Select *parent):
83  Action(name, parent)
84 {}
85 
86 
88 {}
89 
90 
91 Select *Select::clone(const std::string &name) {
92  return new Select(name, this);
93 }
94 
95 
97  // Find beam sequence or table definition.
98  const std::string name = Attributes::getString(itsAttr[LINE]);
99 
100  if(Object *obj = OpalData::getInstance()->find(name)) {
101  if(BeamSequence *line = dynamic_cast<BeamSequence *>(obj)) {
102  select(*line->fetchLine());
103  } else if(Table *table = dynamic_cast<Table *>(obj)) {
104  select(*table->getLine());
105  } else {
106  throw OpalException("Select::execute()",
107  "You cannot do a \"SELECT\" on \"" + name +
108  "\", it is neither a line nor a table.");
109  }
110  } else {
111  throw OpalException("Select::execute()",
112  "Object \"" + name + "\" not found.");
113  }
114 }
115 
116 
117 void Select::select(const Beamline &bl) {
118  if(Attributes::getBool(itsAttr[FULL])) {
119  // Select all positions.
120  Flagger flagger(bl, true);
121  flagger.execute();
122  if(Options::info) {
123  *gmsg << level3 << "\nAll elements selected.\n" << endl;
124  }
125  } else if(Attributes::getBool(itsAttr[CLEAR])) {
126  // Deselect all selections.
127  Flagger flagger(bl, false);
128  flagger.execute();
129  if(Options::info) {
130  *gmsg << level3 << "\nAll elements de-selected.\n" << endl;
131  }
132  } else {
133  Selector sel(bl,
137  Attributes::getString(itsAttr[PATTERN]));
138  sel.execute();
139 
140  int count = sel.getCount();
141 
142  if(count == 0) {
143  *gmsg << level3 << "No elements";
144  } else if(count == 1) {
145  *gmsg << level3 << "\n1 element";
146  } else {
147  *gmsg << level3 << '\n' << count << " elements";
148  }
149 
150  *gmsg << level3 << " selected.\n" << endl;
151  }
152 }
Select()
Exemplar constructor.
Definition: Select.cpp:51
RangeRep getRange(const Attribute &attr)
Get range value.
Definition: Attributes.cpp:177
The base class for all OPAL actions.
Definition: Action.h:30
virtual void execute()
Execute the command.
Definition: Select.cpp:96
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Inform * gmsg
Definition: Main.cpp:21
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
Definition: Attributes.cpp:171
virtual ~Select()
Definition: Select.cpp:87
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool info
Info flag.
Definition: Options.cpp:8
Set selection flags for a given range in a beam line.
Definition: Selector.h:33
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
static OpalData * getInstance()
Definition: OpalData.cpp:209
void select(const Beamline &)
Definition: Select.cpp:117
virtual Select * clone(const std::string &name)
Make clone.
Definition: Select.cpp:91
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
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:37
const T * find(const T table[], const std::string &name)
Look up name.
Definition: TFind.h:34
virtual void execute()
Apply the algorithm to the top-level beamline.
The base class for all OPAL beam lines and sequences.
Definition: BeamSequence.h:32
The SELECT command.
Definition: Select.h:30
The base class for all OPAL objects.
Definition: Object.h:48
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
const std::string name
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
The base class for all OPAL tables.
Definition: Table.h:42
virtual void execute()
Execute the selection.
Definition: Selector.cpp:58
Definition: Inform.h:41
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
int getCount() const
Return the count of selected elements.
Definition: Selector.cpp:91
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307