OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
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"
21 #include "AbstractObjects/Object.h"
22 #include "AbstractObjects/Table.h"
24 #include "Elements/OpalElement.h"
25 #include "Utilities/Options.h"
27 #include <iostream>
28 
29 
30 Selector::Selector(const Beamline &bl, const RangeRep &range,
31  const std::string &clsName, const std::string &typName,
32  const std::string &pattern):
33  RangeSelector(bl, range),
34  itsClass(0), itsType(typName), itsPattern(0), itsCount(0) {
35  if(! clsName.empty() && (itsClass = Element::find(clsName)) == 0) {
36  if(Options::warn) {
37  std::cerr << "\n### Warning ### Unknown class name \""
38  << clsName << "\"; will select all classes.\n" << std::endl;
39  }
40  }
41 
42  if(! pattern.empty()) {
43  itsPattern = new RegularExpression(pattern);
44  }
45 }
46 
47 
49  delete itsPattern;
50 }
51 
52 
54  itsCount = 0;
56 }
57 
58 
60  // Skip elements which are not in range.
61  if(itsRange.isActive()) {
62  const std::string &name = fep.getElement()->getName();
63  if(name[0] != '[') {
64  bool set = true;
65  OpalElement &elem = dynamic_cast<OpalElement &>(*Element::find(name));
66 
67  // If class exists and element is not class member, then skip.
68  if(itsClass != 0 && ! elem.isTreeMember(itsClass)) set = false;
69 
70  // If pattern does exists and element name does not match, then skip.
71  if(itsPattern != 0 && ! itsPattern->match(name)) set = false;
72 
73  // If type name is not blank and element type is different, then skip.
74  if(! itsType.empty() && itsType != elem.getTypeName()) set = false;
75 
76  // The current element matches all conditions.
77  if(set) {
78  fep.setSelectionFlag(true);
79  ++itsCount;
80  }
81  }
82  }
83 }
84 
85 
86 int Selector::getCount() const {
87  return itsCount;
88 }
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
const std::string name
bool warn
Warn flag.
Definition: Options.cpp:33
static Element * find(const std::string &name)
Find named Element.
Definition: Element.cpp:45
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition: Object.cpp:291
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
bool isActive() const
Test for active range.
Definition: RangeRep.cpp:65
virtual const std::string & getName() const
Get element name.
An abstract sequence of beam line components.
Definition: Beamline.h:34
ElementBase * getElement() const
Get the element pointer.
Definition: ElmPtr.h:58
A section of a beam line.
Definition: FlaggedElmPtr.h:36
void setSelectionFlag(bool flag) const
Get selection flag.
const std::string getTypeName() const
Return the element's type name.
virtual void execute()
Execute the algorithm.
RangeRep itsRange
Working data for range.
Definition: RangeSelector.h:55
virtual void handleElement(const FlaggedElmPtr &)
The operation to be done for elements.
Definition: Selector.cpp:59
int getCount() const
Return the count of selected elements.
Definition: Selector.cpp:86
virtual ~Selector()
Definition: Selector.cpp:48
virtual void execute()
Execute the selection.
Definition: Selector.cpp:53
const std::string itsType
Definition: Selector.h:63
int itsCount
Definition: Selector.h:69
const Element * itsClass
Definition: Selector.h:60
const RegularExpression * itsPattern
Definition: Selector.h:66
A regular expression.
bool match(const std::string &s) const
Match a string against the pattern.