OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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"
22#include "Algorithms/Flagger.h"
24#include "Tables/Selector.h"
26#include "Utilities/Options.h"
27
28#include <iostream>
29
30extern Inform* gmsg;
31
32namespace {
33 enum {
34 LINE, // The line to be affected.
35 FULL, // If true, all elements are selected.
36 CLEAR, // If true, all selections are cleared.
37 RANGE, // The range to be considered.
38 CLASS, // The class of elements to be selected.
39 TYPE, // The type name of elements to be selected.
40 PATTERN, // The regular expression for matching names.
41 SIZE
42 };
43}
44
46 Action(SIZE, "SELECT",
47 "The \"SELECT\" sub-command selects the positions to be affected "
48 "by subsequent error sub-commands.") {
50 ("LINE",
51 "Name of the lattice to be affected by selections",
52 "UNNAMED_USE");
54 ("FULL",
55 "If true, all elements are selected");
57 ("CLEAR",
58 "If true, all selections are cleared");
60 ("RANGE",
61 "Range to be considered for selection (default: full range)");
63 ("CLASS",
64 "Name of class to be selected (default: all classes)");
66 ("TYPE",
67 "The type name of elements to be selected (default: all types)");
69 ("PATTERN",
70 "Regular expression for matching names (default: all names)");
71
73}
74
75
76Select::Select(const std::string& name, Select* parent):
77 Action(name, parent)
78{}
79
80
83
84
85Select* Select::clone(const std::string& name) {
86 return new Select(name, this);
87}
88
89
91 // Find beam sequence or table definition.
92 const std::string name = Attributes::getString(itsAttr[LINE]);
93
94 if (Object* obj = OpalData::getInstance()->find(name)) {
95 if (BeamSequence* line = dynamic_cast<BeamSequence*>(obj)) {
96 select(*line->fetchLine());
97 } else if (Table* table = dynamic_cast<Table*>(obj)) {
98 select(*table->getLine());
99 } else {
100 throw OpalException("Select::execute()",
101 "You cannot do a \"SELECT\" on \"" + name +
102 "\", it is neither a line nor a table.");
103 }
104 } else {
105 throw OpalException("Select::execute()",
106 "Object \"" + name + "\" not found.");
107 }
108}
109
110
111void Select::select(const Beamline& bl) {
112 if (Attributes::getBool(itsAttr[FULL])) {
113 // Select all positions.
114 Flagger flagger(bl, true);
115 flagger.execute();
116 if (Options::info) {
117 *gmsg << level2 << "\nAll elements selected.\n" << endl;
118 }
119 } else if (Attributes::getBool(itsAttr[CLEAR])) {
120 // Deselect all selections.
121 Flagger flagger(bl, false);
122 flagger.execute();
123 if (Options::info) {
124 *gmsg << level2 << "\nAll elements de-selected.\n" << endl;
125 }
126 } else {
127 Selector sel(bl,
132 sel.execute();
133
134 if (Options::info) {
135 int count = sel.getCount();
136 if (count == 0) {
137 *gmsg << level2 << "No elements";
138 } else if (count == 1) {
139 *gmsg << level2 << "\n1 element";
140 } else {
141 *gmsg << level2 << '\n' << count << " elements";
142 }
143 *gmsg << level2 << " selected.\n" << endl;
144 }
145 }
146}
Inform * gmsg
Definition changes.cpp:7
@ SIZE
Definition IndexMap.cpp:173
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Attribute makeRange(const std::string &name, const std::string &help)
Create a range attribute.
bool getBool(const Attribute &attr)
Return logical value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
RangeRep getRange(const Attribute &attr)
Get range value.
bool info
Info flag.
Definition Options.cpp:28
Action(int size, const char *name, const char *help)
Constructor for exemplars.
Definition Action.cpp:54
The base class for all OPAL beam lines and sequences.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:189
Object(int size, const char *name, const char *help)
Constructor for exemplars.
Definition Object.cpp:354
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
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
virtual ~Select()
Definition Select.cpp:81
virtual void execute()
Execute the command.
Definition Select.cpp:90
void select(const Beamline &)
Definition Select.cpp:111
virtual Select * clone(const std::string &name)
Make clone.
Definition Select.cpp:85
Select()
Exemplar constructor.
Definition Select.cpp:45
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.