OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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", "Name of the lattice to be affected by selections", "UNNAMED_USE");
51 itsAttr[FULL] = Attributes::makeBool("FULL", "If true, all elements are selected");
52 itsAttr[CLEAR] = Attributes::makeBool("CLEAR", "If true, all selections are cleared");
54 "RANGE", "Range to be considered for selection (default: full range)");
55 itsAttr[CLASS] =
56 Attributes::makeString("CLASS", "Name of class to be selected (default: all classes)");
58 "TYPE", "The type name of elements to be selected (default: all types)");
60 "PATTERN", "Regular expression for matching names (default: all names)");
61
63}
64
65Select::Select(const std::string& name, Select* parent) : Action(name, parent) {}
66
68
69Select* Select::clone(const std::string& name) { return new Select(name, this); }
70
72 // Find beam sequence or table definition.
73 const std::string name = Attributes::getString(itsAttr[LINE]);
74
75 if (Object* obj = OpalData::getInstance()->find(name)) {
76 if (BeamSequence* line = dynamic_cast<BeamSequence*>(obj)) {
77 select(*line->fetchLine());
78 } else if (Table* table = dynamic_cast<Table*>(obj)) {
79 select(*table->getLine());
80 } else {
81 throw OpalException(
82 "Select::execute()", "You cannot do a \"SELECT\" on \"" + name
83 + "\", it is neither a line nor a table.");
84 }
85 } else {
86 throw OpalException("Select::execute()", "Object \"" + name + "\" not found.");
87 }
88}
89
90void Select::select(const Beamline& bl) {
91 if (Attributes::getBool(itsAttr[FULL])) {
92 // Select all positions.
93 Flagger flagger(bl, true);
94 flagger.execute();
95 if (Options::info) {
96 *gmsg << level2 << "\nAll elements selected.\n" << endl;
97 }
98 } else if (Attributes::getBool(itsAttr[CLEAR])) {
99 // Deselect all selections.
100 Flagger flagger(bl, false);
101 flagger.execute();
102 if (Options::info) {
103 *gmsg << level2 << "\nAll elements de-selected.\n" << endl;
104 }
105 } else {
106 Selector sel(
109 sel.execute();
110
111 if (Options::info) {
112 int count = sel.getCount();
113 if (count == 0) {
114 *gmsg << level2 << "No elements";
115 } else if (count == 1) {
116 *gmsg << level2 << "\n1 element";
117 } else {
118 *gmsg << level2 << '\n' << count << " elements";
119 }
120 *gmsg << level2 << " selected.\n" << endl;
121 }
122 }
123}
Inform * gmsg
Definition changes.cpp:7
@ SIZE
Definition IndexMap.cpp:179
Inform * gmsg
Definition changes.cpp:7
The base class for all OPAL actions.
Definition Action.h:29
The base class for all OPAL beam lines and sequences.
An abstract sequence of beam line components.
Definition Beamline.h:34
void execute() override
Apply the algorithm to the top-level beamline.
Set/reset all selection flags in a beam line built from FlaggedElmPtr.
Definition Flagger.h:30
The base class for all OPAL objects.
Definition Object.h:45
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:169
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:210
static OpalData * getInstance()
Definition OpalData.cpp:193
virtual ~Select()
Definition Select.cpp:67
virtual void execute()
Execute the command.
Definition Select.cpp:71
void select(const Beamline &)
Definition Select.cpp:90
virtual Select * clone(const std::string &name)
Make clone.
Definition Select.cpp:69
Select()
Exemplar constructor.
Definition Select.cpp:45
int getCount() const
Return the count of selected elements.
Definition Selector.cpp:79
virtual void execute()
Execute the selection.
Definition Selector.cpp:48
The base class for all OPAL tables.
Definition Table.h:41
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