Twig Class Reference

The Twig class is used for pattern matching to sections in a UAPNode tree. More...

#include <Twig.hpp>

List of all members.

Public Member Functions

 Twig ()
 Creates a new Twig.
 Twig (std::string twig_str, bool has_ele_name=false)
 Creates a new Twig from an old one.
bool fromString (std::string trig_str, bool has_ele_name=false)
 Parses a string and converts it into a Twig.
std::string toString ()
 Translates from a Twig to the equivalent string.
std::string toNodeString (bool include_target_attrib=false)
 Translates the node part of a Twig to the equivalent string.
UAPNodegetLocalSubNode (UAPNode *root, bool create=false)
 Finds a subnode that matches a Twig.
UAPNodegetSubNode (UAPNode *root)
 Finds a subnode that matches a Twig.
NodeVec getSubNodes (UAPNode *root)
 Finds all subnodes that matches a Twig.
bool matchNameAndPrefix (UAPNode *named_node)
 Match name and prefix from a Twig to the name and prefix attributes in a UAPNode.
bool operator== (Twig t)
 Test to see if two Twigs are identical.

Static Public Member Functions

static UAPNodegetLocalSubNode (UAPNode *root, std::string twig_str, bool create=false)
 Finds a subnode that matches a Twig.
static NodeVec getSubNodes (UAPNode *root, std::string twig_str)
 Finds all subnodes that matches a Twig.


Detailed Description

The Twig class is used for pattern matching to sections in a UAPNode tree.

Example: A string (in what is called "bracket" notation): "p1.p2.q[quadrupole:k(a=1,b)@err]" When translated into a Twig the string looks like: .target_attribute = "err" .name = "q" .prefix[0] = "p1" .prefix[1] = "p2" .has_brackets = true .node_vec[0] .name = "quadrupole" .node_vec[1] .name = "k" .attrib[0] .name = "a" .value = "1" .attrib[1] .name = "b" .value = "" And, for example, this Twig will match to the following portion of a UAPNode tree: <xxx name="q"> |- <quadrupole ttt="yyy"> |- <k a="1" b="zzz">

In this example the <k> node is called the "base" node of the twig, and the <xxx> node is called the "head" node.

The above string is equivalent to the string (in "bracketless" notation): "*(name = q,prefix = p1.p2):quadrupole:k(a=1,b)%err" When translated into a Twig this string looks like .target_attribute = "err" .has_brackets = false .node_vec[0] .name = "*" .attrib[0] .name = "name" .value = "q" .attrib[1] .name = "prefix" .value = "p1.p2" .node_vec[1] .name = "quadrupole" .node_vec[2] .name = "k" .attrib[0] .name = "a" .value = "1" .attrib[1] .name = "b" .value = ""

Some examples:

   Input      Prefix            name   node_vec
   ---------- ---------------   ----   ---------
  "A...B.Z[]" ["A", ".", "B"]   "Z"    None
  "A...Z[]"   ["A", "."]        "Z"    None
  ".Z[]"      [""]              "Z"    None
  "Z[]"       []                "Z"    None
  "X:Y:Z"     []                ""     ["X", "Y", "Z"]

Note that something in "bracket" notation can always be converted into "bracketless" but the reverse is not always possible.

Note: The difference between Twig("a[]", true) and Twig("a", true) is that the first Twig has .has_brackets = true and the second has .has_brackets = false.

Note: When a component is potentially confusing (has internal ".", ":", etc.), quotation Marks can be used. Example: q[quadrupole:(k(a="1.3")] beam:'{http://cern.ch/madx}madx':energy

Definition at line 184 of file Twig.hpp.


Constructor & Destructor Documentation

Twig::Twig ( std::string  twig_str,
bool  has_ele_name = false 
)

Creates a new Twig from an old one.

See the fromString method for more details.

Definition at line 131 of file Twig.cpp.


Member Function Documentation

bool Twig::fromString ( std::string  trig_str,
bool  has_ele_name = false 
)

Parses a string and converts it into a Twig.

The parsing will handle strings that are or are not in bracket notation. For example: twig_str = "q1[quadrupole:k(n=1)@err]" If the string does not have brackets "[...]", or ":", etc. like: twig_str = "q" Then it is ambiguous whether "q" is an element name or an attribute name.

Parameters:
twig_str Input string.
has_ele_name If True then an ambiguous name is interpreted as an element name.
Returns:
True if there are no syntax problems. False otherwise.

Definition at line 145 of file Twig.cpp.

UAPNode * Twig::getLocalSubNode ( UAPNode root,
std::string  twig_str,
bool  create = false 
) [static]

Finds a subnode that matches a Twig.

The root node is assumed to match .name and the root's children are searched for a match to .node_vec[0]. When a match to .node_vec[0] is found, the children of the matching node are searched for a match to .node_vec[1], etc.

Parameters:
root Root node for searching.
twig_str String equivalent of a string.
create If true then create subnode if not found.
Returns:
Pointer to the subnode. NULL if not found nor created.

Definition at line 515 of file Twig.cpp.

UAPNode * Twig::getLocalSubNode ( UAPNode root,
bool  create = false 
)

Finds a subnode that matches a Twig.

The root node is assumed to match .name and the root's children are searched for a match to .node_vec[0]. When a match to .node_vec[0] is found, the children of the matching node are searched for a match to .node_vec[1], etc. This method assumes that there are no multiple matches to .node_vec[i].

Parameters:
root root node for searching.
create If true then create subnode if not found.
Returns:
Pointer to the subnode. NULL if not found nor created.

Definition at line 499 of file Twig.cpp.

UAPNode * Twig::getSubNode ( UAPNode root  ) 

Finds a subnode that matches a Twig.

Parameters:
root root node for searching.
Returns:
Pointer to the subnode. NULL if not found nor created.

Definition at line 614 of file Twig.cpp.

NodeVec Twig::getSubNodes ( UAPNode root  ) 

Finds all subnodes that matches a Twig.

Parameters:
root root node for searching.
Returns:
Vec of subnodes.

Definition at line 538 of file Twig.cpp.

NodeVec Twig::getSubNodes ( UAPNode root,
std::string  twig_str 
) [static]

Finds all subnodes that matches a Twig.

Parameters:
root root node for searching.
twig_str String equivalent of a string.
Returns:
Vec of subnodes.

Definition at line 526 of file Twig.cpp.

bool Twig::matchNameAndPrefix ( UAPNode named_node  ) 

Match name and prefix from a Twig to the name and prefix attributes in a UAPNode.

That is, the node is of the form: <xxx name="..." prefix="...">. Note: The twig name and prefix may contain "*" and "%" wild card characters.

Parameters:
named_node Node with a name attribute and possibly a prefix attribute.
Returns:
True if there is a match. Note: If the node does not have a name attribute or the Twig name is "" then False is returned.

Definition at line 661 of file Twig.cpp.

bool Twig::operator== ( Twig  t  ) 

Test to see if two Twigs are identical.

Returns:
True if the Twigs are identical

Definition at line 780 of file Twig.cpp.

string Twig::toNodeString ( bool  include_target_attrib = false  ) 

Translates the node part of a Twig to the equivalent string.

Parameters:
include_target_attrib If true then append "@target_attribute"
Returns:
Output string.

Definition at line 447 of file Twig.cpp.

string Twig::toString (  ) 

Translates from a Twig to the equivalent string.

Returns:
Output string.

Definition at line 466 of file Twig.cpp.


Generated on Fri Feb 12 15:47:57 2010 for Universal Accelerator Parser by  doxygen 1.5.7