OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Selector.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Selector.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Selector
10 // This class sets selected selection flags in a USE object.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2001/08/13 15:25:22 $
15 // $Author: jowett $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Tables/Selector.h"
23 #include "AbstractObjects/Object.h"
24 #include "AbstractObjects/Table.h"
26 #include "Elements/OpalElement.h"
27 #include "Utilities/Options.h"
29 #include <iostream>
30 
31 
32 // Class Selector
33 // ------------------------------------------------------------------------
34 
35 Selector::Selector(const Beamline &bl, const RangeRep &range,
36  const std::string &clsName, const std::string &typName,
37  const std::string &pattern):
38  RangeSelector(bl, range),
39  itsClass(0), itsType(typName), itsPattern(0), itsCount(0) {
40  if(! clsName.empty() && (itsClass = Element::find(clsName)) == 0) {
41  if(Options::warn) {
42  std::cerr << "\n### Warning ### Unknown class name \""
43  << clsName << "\"; will select all classes.\n" << std::endl;
44  }
45  }
46 
47  if(! pattern.empty()) {
48  itsPattern = new RegularExpression(pattern);
49  }
50 }
51 
52 
54  delete itsPattern;
55 }
56 
57 
59  itsCount = 0;
61 }
62 
63 
65  // Skip elements which are not in range.
66  if(itsRange.isActive()) {
67  const std::string &name = fep.getElement()->getName();
68  if(name[0] != '[') {
69  bool set = true;
70  OpalElement &elem = dynamic_cast<OpalElement &>(*Element::find(name));
71 
72  // If class exists and element is not class member, then skip.
73  if(itsClass != 0 && ! elem.isTreeMember(itsClass)) set = false;
74 
75  // If pattern does exists and element name does not match, then skip.
76  if(itsPattern != 0 && ! itsPattern->match(name)) set = false;
77 
78  // If type name is not blank and element type is different, then skip.
79  if(! itsType.empty() && itsType != elem.getTypeName()) set = false;
80 
81  // The current element matches all conditions.
82  if(set) {
83  fep.setSelectionFlag(true);
84  ++itsCount;
85  }
86  }
87  }
88 }
89 
90 
91 int Selector::getCount() const {
92  return itsCount;
93 }
const std::string getTypeName() const
Return the element&#39;s type name.
const Element * itsClass
Definition: Selector.h:65
virtual ~Selector()
Definition: Selector.cpp:53
void setSelectionFlag(bool flag) const
Get selection flag.
A regular expression.
An abstract visitor which calls the pure virtual method.
Definition: RangeSelector.h:32
virtual const std::string & getName() const
Get element name.
Definition: ElementBase.cpp:95
RangeRep itsRange
Working data for range.
Definition: RangeSelector.h:58
bool warn
Warn flag.
Definition: Options.cpp:10
const RegularExpression * itsPattern
Definition: Selector.h:71
bool isActive() const
Test for active range.
Definition: RangeRep.cpp:66
const std::string itsType
Definition: Selector.h:68
Base class for all beam line elements.
Definition: OpalElement.h:41
bool match(const std::string &s) const
Match a string against the pattern.
Representation of a range within a beam line or sequence.
Definition: RangeRep.h:34
An abstract sequence of beam line components.
Definition: Beamline.h:37
virtual void handleElement(const FlaggedElmPtr &)
The operation to be done for elements.
Definition: Selector.cpp:64
static Element * find(const std::string &name)
Find named Element.
Definition: Element.cpp:37
const std::string name
bool isTreeMember(const Object *subTree) const
Test for tree membership.
Definition: Object.cpp:294
virtual void execute()
Execute the selection.
Definition: Selector.cpp:58
ElementBase * getElement() const
Get the element pointer.
Definition: ElmPtr.h:58
int getCount() const
Return the count of selected elements.
Definition: Selector.cpp:91
int itsCount
Definition: Selector.h:74
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
virtual void execute()
Execute the algorithm.
A section of a beam line.
Definition: FlaggedElmPtr.h:36