aGrUM  0.16.0
pattern_inl.h
Go to the documentation of this file.
1 
30 namespace gum {
31  namespace prm {
32  namespace gspan {
33 
34  INLINE
35  Pattern::Pattern() : DiGraph(), __last(0) { GUM_CONSTRUCTOR(Pattern); }
36 
37  INLINE
38  Pattern::~Pattern() { GUM_DESTRUCTOR(Pattern); }
39 
40  INLINE
42  NodeId n = NodeId(size() + 1);
44  __node_map.insert(n, &l);
45  __last = &l;
46  return n;
47  }
48 
49  INLINE
51  try {
52  return *(__node_map[node]);
53  } catch (NotFound&) {
54  GUM_ERROR(NotFound, "node not found in this Pattern");
55  }
56  }
57 
58  INLINE
59  const LabelData& Pattern::label(NodeId node) const {
60  try {
61  return *(__node_map[node]);
62  } catch (NotFound&) {
63  GUM_ERROR(NotFound, "node not found in this Pattern");
64  }
65  }
66 
67  INLINE
69  if (__last) return *__last;
70 
71  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
72  }
73 
74  INLINE
75  const LabelData& Pattern::lastAdded() const {
76  if (__last) return *__last;
77 
78  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
79  }
80 
81  INLINE
83  try {
84  return *(__arc_map[Arc(i, j)].first);
85  } catch (NotFound&) {
86  GUM_ERROR(NotFound, "arc not found in this Pattern");
87  }
88  }
89 
90  INLINE
91  const LabelData& Pattern::label(NodeId i, NodeId j) const {
92  try {
93  return *(__arc_map[Arc(i, j)].first);
94  } catch (NotFound&) {
95  GUM_ERROR(NotFound, "arc not found in this Pattern");
96  }
97  }
98 
99  INLINE
100  LabelData& Pattern::label(const Arc& arc) {
101  try {
102  return *(__arc_map[arc].first);
103  } catch (NotFound&) {
104  GUM_ERROR(NotFound, "arc not found in this Pattern");
105  }
106  }
107 
108  INLINE
109  const LabelData& Pattern::label(const Arc& arc) const {
110  try {
111  return *(__arc_map[arc].first);
112  } catch (NotFound&) {
113  GUM_ERROR(NotFound, "arc not found in this Pattern");
114  }
115  }
116 
117  INLINE
119  if (!(DiGraph::exists(i) && DiGraph::exists(j))) {
120  GUM_ERROR(NotFound, "node not found in this pattern");
121  }
122 
123  EdgeCode* edge =
124  new EdgeCode(i, j, __node_map[i]->id, l.id, __node_map[j]->id);
125 
126  if ((code().codes.size() == 0)
127  || (DFSCode::validNeighbors(code().codes.back(), edge))) {
128  DiGraph::addArc(i, j);
129  __arc_map.insert(Arc(i, j), std::make_pair(&l, edge));
130  code().codes.push_back(edge);
131  } else {
132  delete edge;
134  "illegal arc considering neighborhood restriction");
135  }
136  }
137 
138  INLINE
139  bool Pattern::exists(NodeId id) const { return DiGraph::exists(id); }
140 
141  INLINE
142  bool Pattern::exists(NodeId tail, NodeId head) const {
143  return DiGraph::existsArc(tail, head);
144  }
145 
146  INLINE
147  Size Pattern::size() const { return DiGraph::size(); }
148 
149  INLINE
151 
152  INLINE
153  void Pattern::rightmostPath(std::list< NodeId >& r_path) const {
154  r_path.push_back(NodeId(size()));
155 
156  while (r_path.front() != 1) {
157  for (const auto par : parents(r_path.front())) {
158  if (par < r_path.front()) {
159  r_path.push_front(par);
160  break;
161  }
162  }
163  }
164  }
165 
166  INLINE const NodeGraphPart& Pattern::nodes() const {
167  return DiGraph::nodes();
168  }
169 
170  INLINE const ArcSet& Pattern::arcs() const { return DiGraph::arcs(); }
171 
172  INLINE
173  DFSCode& Pattern::code() { return __code; }
174 
175  INLINE
176  const DFSCode& Pattern::code() const { return __code; }
177 
178  INLINE
180  try {
181  return *(__arc_map[Arc(tail, head)].second);
182  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
183  }
184 
185  INLINE
187  try {
188  return *(__arc_map[arc].second);
189  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
190  }
191 
192  INLINE
193  const EdgeCode& Pattern::edgeCode(NodeId tail, NodeId head) const {
194  try {
195  return *(__arc_map[Arc(tail, head)].second);
196  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
197  }
198 
199  INLINE
200  const EdgeCode& Pattern::edgeCode(const Arc& arc) const {
201  try {
202  return *(__arc_map[arc].second);
203  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
204  }
205 
206  INLINE
208  EdgeCode* edge = __code.codes.back();
209  __code.codes.pop_back();
210 
211  if (edge->isForward()) {
212  __node_map.erase(edge->j);
213  __arc_map.erase(Arc(edge->i, edge->j));
214  DiGraph::eraseArc(Arc(edge->i, edge->j));
215  DiGraph::eraseNode(edge->j);
216  } else {
217  __arc_map.erase(Arc(edge->i, edge->j));
218  DiGraph::eraseArc(Arc(edge->i, edge->j));
219  }
220 
221  delete edge;
222  }
223 
224  INLINE
225  void Pattern::remove(NodeId node) {
226  if (DiGraph::parents(node).empty() && DiGraph::children(node).empty()) {
227  DiGraph::eraseNode(node);
228  __node_map.erase(node);
229  } else {
230  GUM_ERROR(OperationNotAllowed, "the given node has neighbors");
231  }
232  }
233  } /* namespace gspan */
234  } /* namespace prm */
235 } /* namespace gum */
EdgeCode & edgeCode(NodeId tail, NodeId head)
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:179
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: diGraph_inl.h:35
NodeId i
The DFS subscript of the first node in the code.
Definition: edgeCode.h:77
Inner class to handle data about labels in this interface graph.
Pattern()
Default constructor.
Definition: pattern_inl.h:35
ArcProperty< std::pair< LabelData *, EdgeCode *> > __arc_map
Mapping between edges in this Pattern and their respective LabelData.
Definition: pattern.h:213
NodeId j
The DFS subscript of the second node in the code.
Definition: edgeCode.h:80
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
DFSCode & code()
Returns the DFSCode of this Pattern.
Definition: pattern_inl.h:173
Size size() const
alias for sizeNodes
NodeId addNodeWithLabel(LabelData &l)
Insert a node with the given LabelData.
Definition: pattern_inl.h:41
Reprensent a Depth First Search coding of a graph.
Definition: DFSCode.h:53
~Pattern()
Destructor.
Definition: pattern_inl.h:38
bool exists(const NodeId id) const
alias for existsNode
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
std::vector< EdgeCode *> codes
The vector containing the EdgeCode composing this DFSCode.
Definition: DFSCode.h:90
LabelData & label(NodeId node)
Returns the LabelData assigned to node.
Definition: pattern_inl.h:50
bool empty() const
alias for emptyNodes
represent a DFS code used by gspan.
Definition: edgeCode.h:52
Size size() const
Returns the number of nodes in this Pattern.
Definition: pattern_inl.h:147
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Base class for all oriented graphs.
Definition: diGraph.h:111
NodeProperty< LabelData *> __node_map
Mapping between nodes in this Pattern and their respective LabelData.
Definition: pattern.h:209
Class for node sets in graph.
LabelData & lastAdded()
Insert a node with the given LabelData.
Definition: pattern_inl.h:68
const NodeGraphPart & nodes() const
Definition: pattern_inl.h:166
void rightmostPath(std::list< NodeId > &r_path) const
Fill r_path with the rightmost path of this Pattern. The list is supposed empty.
Definition: pattern_inl.h:153
LabelData * __last
The last LabelData added to this pattern.
Definition: pattern.h:216
const ArcSet & arcs() const
Definition: pattern_inl.h:170
Idx id
An unique identifier for this label.
void pop_back()
Remove the last EdgeCode of this pattern.
Definition: pattern_inl.h:207
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
bool isForward() const
Returns true if this EdgeCode is a forward edge.
Definition: edgeCode_inl.h:56
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
DFSCode __code
The DFSCode of this Pattern.
Definition: pattern.h:205
static bool validNeighbors(EdgeCode *e1, EdgeCode *e2)
Returns true of e2 is a valid neighbor for e1 (i.e.
Definition: DFSCode.h:142
void addArc(NodeId i, NodeId j, LabelData &l)
Add an arc to this Pattern.
Definition: pattern_inl.h:118
bool exists(NodeId id) const
Returns true if id is a node in this Pattern.
Definition: pattern_inl.h:139
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void remove(NodeId node)
Remove a node if it has no neighbors, raise an OperationNotAllowed otherwise.
Definition: pattern_inl.h:225
Size sizeArcs() const
Returns the number of arcs in this Pattern.
Definition: pattern_inl.h:150
This contains all the information we want for a node in a DFSTree.
Definition: pattern.h:73
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition: diGraph_inl.h:69
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55