aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
pattern_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Inline implementation of the Pattern class.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 namespace gum {
30  namespace prm {
31  namespace gspan {
32 
33  INLINE
36  ;
37  }
38 
39  INLINE
42  ;
43  }
44 
45  INLINE
47  NodeId n = NodeId(size() + 1);
49  _node_map_.insert(n, &l);
50  _last_ = &l;
51  return n;
52  }
53 
54  INLINE
56  try {
57  return *(_node_map_[node]);
58  } catch (NotFound&) { GUM_ERROR(NotFound, "node not found in this Pattern") }
59  }
60 
61  INLINE
62  const LabelData& Pattern::label(NodeId node) const {
63  try {
64  return *(_node_map_[node]);
65  } catch (NotFound&) { GUM_ERROR(NotFound, "node not found in this Pattern") }
66  }
67 
68  INLINE
70  if (_last_) return *_last_;
71 
72  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet")
73  }
74 
75  INLINE
76  const LabelData& Pattern::lastAdded() const {
77  if (_last_) return *_last_;
78 
79  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet")
80  }
81 
82  INLINE
84  try {
85  return *(_arc_map_[Arc(i, j)].first);
86  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
87  }
88 
89  INLINE
90  const LabelData& Pattern::label(NodeId i, NodeId j) const {
91  try {
92  return *(_arc_map_[Arc(i, j)].first);
93  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
94  }
95 
96  INLINE
98  try {
99  return *(_arc_map_[arc].first);
100  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
101  }
102 
103  INLINE
104  const LabelData& Pattern::label(const Arc& arc) const {
105  try {
106  return *(_arc_map_[arc].first);
107  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in this Pattern") }
108  }
109 
110  INLINE
112  if (!(DiGraph::exists(i) && DiGraph::exists(j))) {
113  GUM_ERROR(NotFound, "node not found in this pattern")
114  }
115 
116  EdgeCode* edge = new EdgeCode(i, j, _node_map_[i]->id, l.id, _node_map_[j]->id);
117 
118  if ((code().codes.size() == 0) || (DFSCode::validNeighbors(code().codes.back(), edge))) {
119  DiGraph::addArc(i, j);
122  } else {
123  delete edge;
124  GUM_ERROR(OperationNotAllowed, "illegal arc considering neighborhood restriction")
125  }
126  }
127 
128  INLINE
129  bool Pattern::exists(NodeId id) const { return DiGraph::exists(id); }
130 
131  INLINE
133  return DiGraph::existsArc(tail, head);
134  }
135 
136  INLINE
137  Size Pattern::size() const { return DiGraph::size(); }
138 
139  INLINE
140  Size Pattern::sizeArcs() const { return DiGraph::sizeArcs(); }
141 
142  INLINE
145 
146  while (r_path.front() != 1) {
147  for (const auto par: parents(r_path.front())) {
148  if (par < r_path.front()) {
150  break;
151  }
152  }
153  }
154  }
155 
156  INLINE const NodeGraphPart& Pattern::nodes() const { return DiGraph::nodes(); }
157 
158  INLINE const ArcSet& Pattern::arcs() const { return DiGraph::arcs(); }
159 
160  INLINE
161  DFSCode& Pattern::code() { return _code_; }
162 
163  INLINE
164  const DFSCode& Pattern::code() const { return _code_; }
165 
166  INLINE
168  try {
169  return *(_arc_map_[Arc(tail, head)].second);
170  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
171  }
172 
173  INLINE
175  try {
176  return *(_arc_map_[arc].second);
177  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
178  }
179 
180  INLINE
182  try {
183  return *(_arc_map_[Arc(tail, head)].second);
184  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
185  }
186 
187  INLINE
188  const EdgeCode& Pattern::edgeCode(const Arc& arc) const {
189  try {
190  return *(_arc_map_[arc].second);
191  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern") }
192  }
193 
194  INLINE
195  void Pattern::pop_back() {
198 
199  if (edge->isForward()) {
201  _arc_map_.erase(Arc(edge->i, edge->j));
202  DiGraph::eraseArc(Arc(edge->i, edge->j));
203  DiGraph::eraseNode(edge->j);
204  } else {
205  _arc_map_.erase(Arc(edge->i, edge->j));
206  DiGraph::eraseArc(Arc(edge->i, edge->j));
207  }
208 
209  delete edge;
210  }
211 
212  INLINE
214  if (DiGraph::parents(node).empty() && DiGraph::children(node).empty()) {
217  } else {
218  GUM_ERROR(OperationNotAllowed, "the given node has neighbors")
219  }
220  }
221  } /* namespace gspan */
222  } /* namespace prm */
223 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
INLINE std::ostream & operator<<(std::ostream &out, const EdgeData< GUM_SCALAR > &data)
Print a EdgeData<GUM_SCALAR> in out.