OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Selector.cpp
Go to the documentation of this file.
1//
2// Class Selector
3// Set selection flags for a given range in a beam line.
4//
5// Copyright (c) 200x - 2020, 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 "Tables/Selector.h"
19#include <iostream>
26#include "Utilities/Options.h"
28
30 const Beamline& bl, const RangeRep& range, const std::string& clsName,
31 const std::string& typName, const std::string& pattern)
32 : RangeSelector(bl, range), itsClass(0), itsType(typName), itsPattern(0), itsCount(0) {
33 if (!clsName.empty() && (itsClass = Element::find(clsName)) == 0) {
34 if (Options::warn) {
35 std::cerr << "\n### Warning ### Unknown class name \"" << clsName
36 << "\"; will select all classes.\n"
37 << std::endl;
38 }
39 }
40
41 if (!pattern.empty()) {
42 itsPattern = new RegularExpression(pattern);
43 }
44}
45
47
52
54 // Skip elements which are not in range.
55 if (itsRange.isActive()) {
56 const std::string& name = fep.getElement()->getName();
57 if (name[0] != '[') {
58 bool set = true;
59 OpalElement& elem = dynamic_cast<OpalElement&>(*Element::find(name));
60
61 // If class exists and element is not class member, then skip.
62 if (itsClass != 0 && !elem.isTreeMember(itsClass)) set = false;
63
64 // If pattern does exists and element name does not match, then skip.
65 if (itsPattern != 0 && !itsPattern->match(name)) set = false;
66
67 // If type name is not blank and element type is different, then skip.
68 if (!itsType.empty() && itsType != elem.getTypeName()) set = false;
69
70 // The current element matches all conditions.
71 if (set) {
72 fep.setSelectionFlag(true);
73 ++itsCount;
74 }
75 }
76 }
77}
78
79int Selector::getCount() const { return itsCount; }
An abstract sequence of beam line components.
Definition Beamline.h:34
virtual const std::string & getName() const
Get element name.
static Element * find(const std::string &name)
Find named Element.
Definition Element.cpp:39
ElementBase * getElement() const
Get the element pointer.
Definition ElmPtr.h:56
A section of a beam line.
void setSelectionFlag(bool flag) const
Get selection flag.
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition Object.cpp:271
const std::string getTypeName() const
Return the element's type name.
Representation of a range within a beam line or sequence.
Definition RangeRep.h:33
bool isActive() const
Test for active range.
Definition RangeRep.cpp:52
virtual void execute()
Execute the algorithm.
RangeRep itsRange
Working data for range.
A regular expression.
bool match(const std::string &s) const
Match a string against the pattern.
virtual void handleElement(const FlaggedElmPtr &)
The operation to be done for elements.
Definition Selector.cpp:53
int getCount() const
Return the count of selected elements.
Definition Selector.cpp:79
virtual ~Selector()
Definition Selector.cpp:46
virtual void execute()
Execute the selection.
Definition Selector.cpp:48
const std::string itsType
Definition Selector.h:60
int itsCount
Definition Selector.h:66
const Element * itsClass
Definition Selector.h:57
const RegularExpression * itsPattern
Definition Selector.h:63
bool warn
Warn flag.
Definition Options.cpp:33