aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
pattern_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
48
49namespace gum {
50 namespace prm {
51 namespace gspan {
52
53 INLINE
55 GUM_CONSTRUCTOR(Pattern);
56 ;
57 }
58
59 INLINE
61 GUM_DESTRUCTOR(Pattern);
62 ;
63 }
64
65 INLINE
67 NodeId n = NodeId(size() + 1);
69 _node_map_.insert(n, &l);
70 _last_ = &l;
71 return n;
72 }
73
74 INLINE
76 try {
77 return *(_node_map_[node]);
78 } catch (NotFound const&) { GUM_ERROR(NotFound, "node not found in this Pattern") }
79 }
80
81 INLINE
82 const LabelData& Pattern::label(NodeId node) const {
83 try {
84 return *(_node_map_[node]);
85 } catch (NotFound const&) { GUM_ERROR(NotFound, "node not found in this Pattern") }
86 }
87
88 INLINE
90 if (_last_) return *_last_;
91
92 GUM_ERROR(OperationNotAllowed, "there are no LabelData yet")
93 }
94
95 INLINE
97 if (_last_) return *_last_;
98
99 GUM_ERROR(OperationNotAllowed, "there are no LabelData yet")
100 }
101
102 INLINE
104 try {
105 return *(_arc_map_[Arc(i, j)].first);
106 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
107 }
108
109 INLINE
111 try {
112 return *(_arc_map_[Arc(i, j)].first);
113 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
114 }
115
116 INLINE
118 try {
119 return *(_arc_map_[arc].first);
120 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
121 }
122
123 INLINE
124 const LabelData& Pattern::label(const Arc& arc) const {
125 try {
126 return *(_arc_map_[arc].first);
127 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
128 }
129
130 INLINE
132 if (!(DiGraph::exists(i) && DiGraph::exists(j))) {
133 GUM_ERROR(NotFound, "node not found in this pattern")
134 }
135
136 EdgeCode* edge = new EdgeCode(i, j, _node_map_[i]->id, l.id, _node_map_[j]->id);
137
138 if ((code().codes.size() == 0) || (DFSCode::validNeighbors(code().codes.back(), edge))) {
139 DiGraph::addArc(i, j);
140 _arc_map_.insert(Arc(i, j), std::make_pair(&l, edge));
141 code().codes.push_back(edge);
142 } else {
143 delete edge;
144 GUM_ERROR(OperationNotAllowed, "illegal arc considering neighborhood restriction")
145 }
146 }
147
148 INLINE
149 bool Pattern::exists(NodeId id) const { return DiGraph::exists(id); }
150
151 INLINE
152 bool Pattern::exists(NodeId tail, NodeId head) const {
153 return DiGraph::existsArc(tail, head);
154 }
155
156 INLINE
157 Size Pattern::size() const { return DiGraph::size(); }
158
159 INLINE
161
162 INLINE
163 void Pattern::rightmostPath(std::list< NodeId >& r_path) const {
164 r_path.push_back(NodeId(size()));
165
166 while (r_path.front() != 1) {
167 for (const auto par: parents(r_path.front())) {
168 if (par < r_path.front()) {
169 r_path.push_front(par);
170 break;
171 }
172 }
173 }
174 }
175
176 INLINE const NodeGraphPart& Pattern::nodes() const { return DiGraph::nodes(); }
177
178 INLINE const ArcSet& Pattern::arcs() const { return DiGraph::arcs(); }
179
180 INLINE
182
183 INLINE
184 const DFSCode& Pattern::code() const { return _code_; }
185
186 INLINE
188 try {
189 return *(_arc_map_[Arc(tail, head)].second);
190 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
191 }
192
193 INLINE
195 try {
196 return *(_arc_map_[arc].second);
197 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
198 }
199
200 INLINE
201 const EdgeCode& Pattern::edgeCode(NodeId tail, NodeId head) const {
202 try {
203 return *(_arc_map_[Arc(tail, head)].second);
204 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
205 }
206
207 INLINE
208 const EdgeCode& Pattern::edgeCode(const Arc& arc) const {
209 try {
210 return *(_arc_map_[arc].second);
211 } catch (NotFound const&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
212 }
213
214 INLINE
216 EdgeCode* edge = _code_.codes.back();
217 _code_.codes.pop_back();
218
219 if (edge->isForward()) {
220 _node_map_.erase(edge->j);
221 _arc_map_.erase(Arc(edge->i, edge->j));
222 DiGraph::eraseArc(Arc(edge->i, edge->j));
223 DiGraph::eraseNode(edge->j);
224 } else {
225 _arc_map_.erase(Arc(edge->i, edge->j));
226 DiGraph::eraseArc(Arc(edge->i, edge->j));
227 }
228
229 delete edge;
230 }
231
232 INLINE
234 if (DiGraph::parents(node).empty() && DiGraph::children(node).empty()) {
235 DiGraph::eraseNode(node);
236 _node_map_.erase(node);
237 } else {
238 GUM_ERROR(OperationNotAllowed, "the given node has neighbors")
239 }
240 }
241 } /* namespace gspan */
242 } /* namespace prm */
243} /* namespace gum */
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
The base class for all directed edges.
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
Definition diGraph_inl.h:79
DiGraph(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
Definition diGraph.cpp:69
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition diGraph_inl.h:55
Size size() const
alias for sizeNodes
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool exists(const NodeId id) const
alias for existsNode
bool empty() const
alias for emptyNodes
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
Reprensent a Depth First Search coding of a graph.
Definition DFSCode.h:72
std::vector< EdgeCode * > codes
The vector containing the EdgeCode composing this DFSCode.
Definition DFSCode.h:109
static bool validNeighbors(EdgeCode *e1, EdgeCode *e2)
Returns true of e2 is a valid neighbor for e1 (i.e.
Definition DFSCode.h:161
void rightmostPath(std::list< NodeId > &r_path) const
Fill r_path with the rightmost path of this Pattern. The list is supposed empty.
const ArcSet & arcs() const
NodeProperty< LabelData * > _node_map_
Mapping between nodes in this Pattern and their respective LabelData.
Definition pattern.h:226
LabelData * _last_
The last LabelData added to this pattern.
Definition pattern.h:233
NodeId addNodeWithLabel(LabelData &l)
Insert a node with the given LabelData.
Definition pattern_inl.h:66
Size size() const
Returns the number of nodes in this Pattern.
void pop_back()
Remove the last EdgeCode of this pattern.
DFSCode & code()
Returns the DFSCode of this Pattern.
ArcProperty< std::pair< LabelData *, EdgeCode * > > _arc_map_
Mapping between edges in this Pattern and their respective LabelData.
Definition pattern.h:230
Size sizeArcs() const
Returns the number of arcs in this Pattern.
DFSCode _code_
The DFSCode of this Pattern.
Definition pattern.h:222
Pattern()
Default constructor.
Definition pattern_inl.h:54
void remove(NodeId node)
Remove a node if it has no neighbors, raise an OperationNotAllowed otherwise.
void addArc(NodeId i, NodeId j, LabelData &l)
Add an arc to this Pattern.
bool exists(NodeId id) const
Returns true if id is a node in this Pattern.
const NodeGraphPart & nodes() const
LabelData & lastAdded()
Insert a node with the given LabelData.
Definition pattern_inl.h:89
LabelData & label(NodeId node)
Returns the LabelData assigned to node.
Definition pattern_inl.h:75
EdgeCode & edgeCode(NodeId tail, NodeId head)
Returns the EdgeCode of an edge of this Pattern.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< Arc > ArcSet
Some typdefs and define for shortcuts ...
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
represent a DFS code used by gspan.
Definition edgeCode.h:72
NodeId i
The DFS subscript of the first node in the code.
Definition edgeCode.h:97
bool isForward() const
Returns true if this EdgeCode is a forward edge.
NodeId j
The DFS subscript of the second node in the code.
Definition edgeCode.h:100
Inner class to handle data about labels in this interface graph.
Idx id
An unique identifier for this label.