aGrUM  0.14.2
pattern_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 namespace gum {
28  namespace prm {
29  namespace gspan {
30 
31  INLINE
32  Pattern::Pattern() : DiGraph(), __last(0) { GUM_CONSTRUCTOR(Pattern); }
33 
34  INLINE
35  Pattern::~Pattern() { GUM_DESTRUCTOR(Pattern); }
36 
37  INLINE
39  NodeId n = NodeId(size() + 1);
41  __node_map.insert(n, &l);
42  __last = &l;
43  return n;
44  }
45 
46  INLINE
48  try {
49  return *(__node_map[node]);
50  } catch (NotFound&) {
51  GUM_ERROR(NotFound, "node not found in this Pattern");
52  }
53  }
54 
55  INLINE
56  const LabelData& Pattern::label(NodeId node) const {
57  try {
58  return *(__node_map[node]);
59  } catch (NotFound&) {
60  GUM_ERROR(NotFound, "node not found in this Pattern");
61  }
62  }
63 
64  INLINE
66  if (__last) return *__last;
67 
68  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
69  }
70 
71  INLINE
72  const LabelData& Pattern::lastAdded() const {
73  if (__last) return *__last;
74 
75  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
76  }
77 
78  INLINE
80  try {
81  return *(__arc_map[Arc(i, j)].first);
82  } catch (NotFound&) {
83  GUM_ERROR(NotFound, "arc not found in this Pattern");
84  }
85  }
86 
87  INLINE
88  const LabelData& Pattern::label(NodeId i, NodeId j) const {
89  try {
90  return *(__arc_map[Arc(i, j)].first);
91  } catch (NotFound&) {
92  GUM_ERROR(NotFound, "arc not found in this Pattern");
93  }
94  }
95 
96  INLINE
97  LabelData& Pattern::label(const Arc& arc) {
98  try {
99  return *(__arc_map[arc].first);
100  } catch (NotFound&) {
101  GUM_ERROR(NotFound, "arc not found in this Pattern");
102  }
103  }
104 
105  INLINE
106  const LabelData& Pattern::label(const Arc& arc) const {
107  try {
108  return *(__arc_map[arc].first);
109  } catch (NotFound&) {
110  GUM_ERROR(NotFound, "arc not found in this Pattern");
111  }
112  }
113 
114  INLINE
116  if (!(DiGraph::exists(i) && DiGraph::exists(j))) {
117  GUM_ERROR(NotFound, "node not found in this pattern");
118  }
119 
120  EdgeCode* edge =
121  new EdgeCode(i, j, __node_map[i]->id, l.id, __node_map[j]->id);
122 
123  if ((code().codes.size() == 0)
124  || (DFSCode::validNeighbors(code().codes.back(), edge))) {
125  DiGraph::addArc(i, j);
126  __arc_map.insert(Arc(i, j), std::make_pair(&l, edge));
127  code().codes.push_back(edge);
128  } else {
129  delete edge;
131  "illegal arc considering neighborhood restriction");
132  }
133  }
134 
135  INLINE
136  bool Pattern::exists(NodeId id) const { return DiGraph::exists(id); }
137 
138  INLINE
139  bool Pattern::exists(NodeId tail, NodeId head) const {
140  return DiGraph::existsArc(tail, head);
141  }
142 
143  INLINE
144  Size Pattern::size() const { return DiGraph::size(); }
145 
146  INLINE
148 
149  INLINE
150  void Pattern::rightmostPath(std::list< NodeId >& r_path) const {
151  r_path.push_back(NodeId(size()));
152 
153  while (r_path.front() != 1) {
154  for (const auto par : parents(r_path.front())) {
155  if (par < r_path.front()) {
156  r_path.push_front(par);
157  break;
158  }
159  }
160  }
161  }
162 
163  INLINE const NodeGraphPart& Pattern::nodes() const {
164  return DiGraph::nodes();
165  }
166 
167  INLINE const ArcSet& Pattern::arcs() const { return DiGraph::arcs(); }
168 
169  INLINE
170  DFSCode& Pattern::code() { return __code; }
171 
172  INLINE
173  const DFSCode& Pattern::code() const { return __code; }
174 
175  INLINE
177  try {
178  return *(__arc_map[Arc(tail, head)].second);
179  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
180  }
181 
182  INLINE
184  try {
185  return *(__arc_map[arc].second);
186  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
187  }
188 
189  INLINE
190  const EdgeCode& Pattern::edgeCode(NodeId tail, NodeId head) const {
191  try {
192  return *(__arc_map[Arc(tail, head)].second);
193  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
194  }
195 
196  INLINE
197  const EdgeCode& Pattern::edgeCode(const Arc& arc) const {
198  try {
199  return *(__arc_map[arc].second);
200  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
201  }
202 
203  INLINE
205  EdgeCode* edge = __code.codes.back();
206  __code.codes.pop_back();
207 
208  if (edge->isForward()) {
209  __node_map.erase(edge->j);
210  __arc_map.erase(Arc(edge->i, edge->j));
211  DiGraph::eraseArc(Arc(edge->i, edge->j));
212  DiGraph::eraseNode(edge->j);
213  } else {
214  __arc_map.erase(Arc(edge->i, edge->j));
215  DiGraph::eraseArc(Arc(edge->i, edge->j));
216  }
217 
218  delete edge;
219  }
220 
221  INLINE
222  void Pattern::remove(NodeId node) {
223  if (DiGraph::parents(node).empty() && DiGraph::children(node).empty()) {
224  DiGraph::eraseNode(node);
225  __node_map.erase(node);
226  } else {
227  GUM_ERROR(OperationNotAllowed, "the given node has neighbors");
228  }
229  }
230  } /* namespace gspan */
231  } /* namespace prm */
232 } /* namespace gum */
EdgeCode & edgeCode(NodeId tail, NodeId head)
Returns the EdgeCode of an edge of this Pattern.
Definition: pattern_inl.h:176
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:32
NodeId i
The DFS subscript of the first node in the code.
Definition: edgeCode.h:74
Inner class to handle data about labels in this interface graph.
Pattern()
Default constructor.
Definition: pattern_inl.h:32
ArcProperty< std::pair< LabelData *, EdgeCode *> > __arc_map
Mapping between edges in this Pattern and their respective LabelData.
Definition: pattern.h:210
NodeId j
The DFS subscript of the second node in the code.
Definition: edgeCode.h:77
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
DFSCode & code()
Returns the DFSCode of this Pattern.
Definition: pattern_inl.h:170
Size size() const
alias for sizeNodes
NodeId addNodeWithLabel(LabelData &l)
Insert a node with the given LabelData.
Definition: pattern_inl.h:38
Reprensent a Depth First Search coding of a graph.
Definition: DFSCode.h:50
~Pattern()
Destructor.
Definition: pattern_inl.h:35
bool exists(const NodeId id) const
alias for existsNode
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::vector< EdgeCode *> codes
The vector containing the EdgeCode composing this DFSCode.
Definition: DFSCode.h:87
LabelData & label(NodeId node)
Returns the LabelData assigned to node.
Definition: pattern_inl.h:47
bool empty() const
alias for emptyNodes
represent a DFS code used by gspan.
Definition: edgeCode.h:49
Size size() const
Returns the number of nodes in this Pattern.
Definition: pattern_inl.h:144
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:108
NodeProperty< LabelData *> __node_map
Mapping between nodes in this Pattern and their respective LabelData.
Definition: pattern.h:206
Class for node sets in graph.
LabelData & lastAdded()
Insert a node with the given LabelData.
Definition: pattern_inl.h:65
const NodeGraphPart & nodes() const
Definition: pattern_inl.h:163
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:150
LabelData * __last
The last LabelData added to this pattern.
Definition: pattern.h:213
const ArcSet & arcs() const
Definition: pattern_inl.h:167
Idx id
An unique identifier for this label.
void pop_back()
Remove the last EdgeCode of this pattern.
Definition: pattern_inl.h:204
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:53
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
DFSCode __code
The DFSCode of this Pattern.
Definition: pattern.h:202
static bool validNeighbors(EdgeCode *e1, EdgeCode *e2)
Returns true of e2 is a valid neighbor for e1 (i.e.
Definition: DFSCode.h:139
void addArc(NodeId i, NodeId j, LabelData &l)
Add an arc to this Pattern.
Definition: pattern_inl.h:115
bool exists(NodeId id) const
Returns true if id is a node in this Pattern.
Definition: pattern_inl.h:136
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void remove(NodeId node)
Remove a node if it has no neighbors, raise an OperationNotAllowed otherwise.
Definition: pattern_inl.h:222
Size sizeArcs() const
Returns the number of arcs in this Pattern.
Definition: pattern_inl.h:147
This contains all the information we want for a node in a DFSTree.
Definition: pattern.h:70
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:66
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52