aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
pattern_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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
35 
36  INLINE
38 
39  INLINE
41  NodeId n = NodeId(size() + 1);
43  node_map__.insert(n, &l);
44  last__ = &l;
45  return n;
46  }
47 
48  INLINE
50  try {
51  return *(node_map__[node]);
52  } catch (NotFound&) {
53  GUM_ERROR(NotFound, "node not found in this Pattern");
54  }
55  }
56 
57  INLINE
58  const LabelData& Pattern::label(NodeId node) const {
59  try {
60  return *(node_map__[node]);
61  } catch (NotFound&) {
62  GUM_ERROR(NotFound, "node not found in this Pattern");
63  }
64  }
65 
66  INLINE
68  if (last__) return *last__;
69 
70  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
71  }
72 
73  INLINE
74  const LabelData& Pattern::lastAdded() const {
75  if (last__) return *last__;
76 
77  GUM_ERROR(OperationNotAllowed, "there are no LabelData yet");
78  }
79 
80  INLINE
82  try {
83  return *(arc_map__[Arc(i, j)].first);
84  } catch (NotFound&) {
85  GUM_ERROR(NotFound, "arc not found in this Pattern");
86  }
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&) {
94  GUM_ERROR(NotFound, "arc not found in this Pattern");
95  }
96  }
97 
98  INLINE
100  try {
101  return *(arc_map__[arc].first);
102  } catch (NotFound&) {
103  GUM_ERROR(NotFound, "arc not found in this Pattern");
104  }
105  }
106 
107  INLINE
108  const LabelData& Pattern::label(const Arc& arc) const {
109  try {
110  return *(arc_map__[arc].first);
111  } catch (NotFound&) {
112  GUM_ERROR(NotFound, "arc not found in this Pattern");
113  }
114  }
115 
116  INLINE
118  if (!(DiGraph::exists(i) && DiGraph::exists(j))) {
119  GUM_ERROR(NotFound, "node not found in this pattern");
120  }
121 
122  EdgeCode* edge
123  = new EdgeCode(i, j, node_map__[i]->id, l.id, node_map__[j]->id);
124 
125  if ((code().codes.size() == 0)
126  || (DFSCode::validNeighbors(code().codes.back(), edge))) {
127  DiGraph::addArc(i, j);
130  } else {
131  delete edge;
133  "illegal arc considering neighborhood restriction");
134  }
135  }
136 
137  INLINE
138  bool Pattern::exists(NodeId id) const { return DiGraph::exists(id); }
139 
140  INLINE
142  return DiGraph::existsArc(tail, head);
143  }
144 
145  INLINE
146  Size Pattern::size() const { return DiGraph::size(); }
147 
148  INLINE
149  Size Pattern::sizeArcs() const { return DiGraph::sizeArcs(); }
150 
151  INLINE
154 
155  while (r_path.front() != 1) {
156  for (const auto par: parents(r_path.front())) {
157  if (par < r_path.front()) {
159  break;
160  }
161  }
162  }
163  }
164 
165  INLINE const NodeGraphPart& Pattern::nodes() const {
166  return DiGraph::nodes();
167  }
168 
169  INLINE const ArcSet& Pattern::arcs() const { return DiGraph::arcs(); }
170 
171  INLINE
172  DFSCode& Pattern::code() { return code__; }
173 
174  INLINE
175  const DFSCode& Pattern::code() const { return code__; }
176 
177  INLINE
179  try {
180  return *(arc_map__[Arc(tail, head)].second);
181  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
182  }
183 
184  INLINE
186  try {
187  return *(arc_map__[arc].second);
188  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
189  }
190 
191  INLINE
193  try {
194  return *(arc_map__[Arc(tail, head)].second);
195  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
196  }
197 
198  INLINE
199  const EdgeCode& Pattern::edgeCode(const Arc& arc) const {
200  try {
201  return *(arc_map__[arc].second);
202  } catch (NotFound&) { GUM_ERROR(NotFound, "arc not found in Pattern"); }
203  }
204 
205  INLINE
206  void Pattern::pop_back() {
209 
210  if (edge->isForward()) {
212  arc_map__.erase(Arc(edge->i, edge->j));
213  DiGraph::eraseArc(Arc(edge->i, edge->j));
214  DiGraph::eraseNode(edge->j);
215  } else {
216  arc_map__.erase(Arc(edge->i, edge->j));
217  DiGraph::eraseArc(Arc(edge->i, edge->j));
218  }
219 
220  delete edge;
221  }
222 
223  INLINE
225  if (DiGraph::parents(node).empty() && DiGraph::children(node).empty()) {
228  } else {
229  GUM_ERROR(OperationNotAllowed, "the given node has neighbors");
230  }
231  }
232  } /* namespace gspan */
233  } /* namespace prm */
234 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
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.