FST  openfst-1.8.2
OpenFst Library
mutable-fst.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Expanded FST augmented with mutators; interface class definition and
19 // mutable arc iterator interface.
20 
21 #ifndef FST_MUTABLE_FST_H_
22 #define FST_MUTABLE_FST_H_
23 
24 #include <sys/types.h>
25 
26 #include <cstddef>
27 #include <cstdint>
28 #include <istream>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include <fst/log.h>
35 #include <fstream>
36 
37 #include <fst/expanded-fst.h>
38 #include <string_view>
39 
40 
41 namespace fst {
42 
43 template <class Arc>
45 
46 // Abstract interface for an expanded FST which also supports mutation
47 // operations. To modify arcs, use MutableArcIterator.
48 template <class A>
49 class MutableFst : public ExpandedFst<A> {
50  public:
51  using Arc = A;
52  using StateId = typename Arc::StateId;
53  using Weight = typename Arc::Weight;
54 
55  virtual MutableFst<Arc> &operator=(const Fst<Arc> &fst) = 0;
56 
58  return operator=(static_cast<const Fst<Arc> &>(fst));
59  }
60 
61  // Sets the initial state.
62  virtual void SetStart(StateId) = 0;
63 
64  // Sets a state's final weight.
65  virtual void SetFinal(StateId s, Weight weight = Weight::One()) = 0;
66 
67  // Sets property bits w.r.t. mask.
68  virtual void SetProperties(uint64_t props, uint64_t mask) = 0;
69 
70  // Adds a state and returns its ID.
71  virtual StateId AddState() = 0;
72 
73  // Adds multiple states.
74  virtual void AddStates(size_t) = 0;
75 
76  // Adds an arc to state.
77  virtual void AddArc(StateId, const Arc &) = 0;
78 
79  // Adds an arc (passed by rvalue reference) to state. Allows subclasses
80  // to optionally implement move semantics. Defaults to lvalue overload.
81  virtual void AddArc(StateId state, Arc &&arc) { AddArc(state, arc); }
82 
83  // Deletes some states, preserving original StateId ordering.
84  virtual void DeleteStates(const std::vector<StateId> &) = 0;
85 
86  // Delete all states.
87  virtual void DeleteStates() = 0;
88 
89  // Delete some arcs at a given state.
90  virtual void DeleteArcs(StateId, size_t) = 0;
91 
92  // Delete all arcs at a given state.
93  virtual void DeleteArcs(StateId) = 0;
94 
95  // Optional, best effort only.
96  virtual void ReserveStates(size_t) {}
97 
98  // Optional, best effort only.
99  virtual void ReserveArcs(StateId, size_t) {}
100 
101  // Returns input label symbol table or nullptr if not specified.
102  const SymbolTable *InputSymbols() const override = 0;
103 
104  // Returns output label symbol table or nullptr if not specified.
105  const SymbolTable *OutputSymbols() const override = 0;
106 
107  // Returns input label symbol table or nullptr if not specified.
108  virtual SymbolTable *MutableInputSymbols() = 0;
109 
110  // Returns output label symbol table or nullptr if not specified.
111  virtual SymbolTable *MutableOutputSymbols() = 0;
112 
113  // Sets input label symbol table; pass nullptr to delete table.
114  virtual void SetInputSymbols(const SymbolTable *isyms) = 0;
115 
116  // Sets output label symbol table; pass nullptr to delete table.
117  virtual void SetOutputSymbols(const SymbolTable *osyms) = 0;
118 
119  // Gets a copy of this MutableFst. See Fst<>::Copy() for further doc.
120  MutableFst *Copy(bool safe = false) const override = 0;
121 
122  // Reads a MutableFst from an input stream, returning nullptr on error.
123  static MutableFst *Read(std::istream &strm, const FstReadOptions &opts) {
124  FstReadOptions ropts(opts);
125  FstHeader hdr;
126  if (ropts.header) {
127  hdr = *opts.header;
128  } else {
129  if (!hdr.Read(strm, opts.source)) return nullptr;
130  ropts.header = &hdr;
131  }
132  if (!(hdr.Properties() & kMutable)) {
133  LOG(ERROR) << "MutableFst::Read: Not a MutableFst: " << ropts.source;
134  return nullptr;
135  }
136  const auto &fst_type = hdr.FstType();
137  const auto reader = FstRegister<Arc>::GetRegister()->GetReader(fst_type);
138  if (!reader) {
139  LOG(ERROR) << "MutableFst::Read: Unknown FST type \"" << fst_type
140  << "\" (arc type = \"" << A::Type() << "\"): " << ropts.source;
141  return nullptr;
142  }
143  auto *fst = reader(strm, ropts);
144  if (!fst) return nullptr;
145  return down_cast<MutableFst *>(fst);
146  }
147 
148  // Reads a MutableFst from a file; returns nullptr on error. An empty
149  // source results in reading from standard input. If convert is true,
150  // convert to a mutable FST subclass (given by convert_type) in the case
151  // that the input FST is non-mutable.
152  static MutableFst *Read(const std::string &source, bool convert = false,
153  std::string_view convert_type = "vector") {
154  if (convert == false) {
155  if (!source.empty()) {
156  std::ifstream strm(source,
157  std::ios_base::in | std::ios_base::binary);
158  if (!strm) {
159  LOG(ERROR) << "MutableFst::Read: Can't open file: " << source;
160  return nullptr;
161  }
162  return Read(strm, FstReadOptions(source));
163  } else {
164  return Read(std::cin, FstReadOptions("standard input"));
165  }
166  } else { // Converts to 'convert_type' if not mutable.
167  std::unique_ptr<Fst<Arc>> ifst(Fst<Arc>::Read(source));
168  if (!ifst) return nullptr;
169  if (ifst->Properties(kMutable, false)) {
170  return down_cast<MutableFst *>(ifst.release());
171  } else {
172  std::unique_ptr<Fst<Arc>> ofst(Convert(*ifst, convert_type));
173  ifst.reset();
174  if (!ofst) return nullptr;
175  if (!ofst->Properties(kMutable, false)) {
176  LOG(ERROR) << "MutableFst: Bad convert type: " << convert_type;
177  }
178  return down_cast<MutableFst *>(ofst.release());
179  }
180  }
181  }
182 
183  // For generic mutuble arc iterator construction; not normally called
184  // directly by users.
185  virtual void InitMutableArcIterator(StateId s,
186  MutableArcIteratorData<Arc> *data) = 0;
187 };
188 
189 // Mutable arc iterator interface, templated on the Arc definition. This is
190 // used by mutable arc iterator specializations that are returned by the
191 // InitMutableArcIterator MutableFst method.
192 template <class Arc>
194  public:
195  // Sets current arc.
196  virtual void SetValue(const Arc &) = 0;
197 };
198 
199 template <class Arc>
200 struct MutableArcIteratorData {
201  std::unique_ptr<MutableArcIteratorBase<Arc>> base; // Specific iterator.
202 };
203 
204 // Generic mutable arc iterator, templated on the FST definition; a wrapper
205 // around a pointer to a more specific one.
206 //
207 // Here is a typical use:
208 //
209 // for (MutableArcIterator<StdFst> aiter(&fst, s);
210 // !aiter.Done();
211 // aiter.Next()) {
212 // StdArc arc = aiter.Value();
213 // arc.ilabel = 7;
214 // aiter.SetValue(arc);
215 // ...
216 // }
217 //
218 // This version requires function calls.
219 template <class FST>
221  public:
222  using Arc = typename FST::Arc;
223  using StateId = typename Arc::StateId;
224 
226  fst->InitMutableArcIterator(s, &data_);
227  }
228 
229  bool Done() const { return data_.base->Done(); }
230 
231  const Arc &Value() const { return data_.base->Value(); }
232 
233  void Next() { data_.base->Next(); }
234 
235  size_t Position() const { return data_.base->Position(); }
236 
237  void Reset() { data_.base->Reset(); }
238 
239  void Seek(size_t a) { data_.base->Seek(a); }
240 
241  void SetValue(const Arc &arc) { data_.base->SetValue(arc); }
242 
243  uint8_t Flags() const { return data_.base->Flags(); }
244 
245  void SetFlags(uint8_t flags, uint8_t mask) {
246  return data_.base->SetFlags(flags, mask);
247  }
248 
249  private:
251 
252  MutableArcIterator(const MutableArcIterator &) = delete;
254 };
255 
256 namespace internal {
257 
258 // MutableFst<A> case: abstract methods.
259 template <class Arc>
260 inline typename Arc::Weight Final(const MutableFst<Arc> &fst,
261  typename Arc::StateId s) {
262  return fst.Final(s);
263 }
264 
265 template <class Arc>
266 inline ssize_t NumArcs(const MutableFst<Arc> &fst, typename Arc::StateId s) {
267  return fst.NumArcs(s);
268 }
269 
270 template <class Arc>
271 inline ssize_t NumInputEpsilons(const MutableFst<Arc> &fst,
272  typename Arc::StateId s) {
273  return fst.NumInputEpsilons(s);
274 }
275 
276 template <class Arc>
277 inline ssize_t NumOutputEpsilons(const MutableFst<Arc> &fst,
278  typename Arc::StateId s) {
279  return fst.NumOutputEpsilons(s);
280 }
281 
282 } // namespace internal
283 
284 // A useful alias when using StdArc.
286 
287 // This is a helper class template useful for attaching a MutableFst interface
288 // to its implementation, handling reference counting and COW semantics.
289 template <class Impl, class FST = MutableFst<typename Impl::Arc>>
290 class ImplToMutableFst : public ImplToExpandedFst<Impl, FST> {
291  public:
292  using Arc = typename Impl::Arc;
293  using StateId = typename Arc::StateId;
294  using Weight = typename Arc::Weight;
295 
297 
298  void SetStart(StateId s) override {
299  MutateCheck();
300  GetMutableImpl()->SetStart(s);
301  }
302 
303  void SetFinal(StateId s, Weight weight = Weight::One()) override {
304  MutateCheck();
305  GetMutableImpl()->SetFinal(s, std::move(weight));
306  }
307 
308  void SetProperties(uint64_t props, uint64_t mask) override {
309  // Can skip mutate check if extrinsic properties don't change,
310  // since it is then safe to update all (shallow) copies
311  const auto exprops = kExtrinsicProperties & mask;
312  if (GetImpl()->Properties(exprops) != (props & exprops)) MutateCheck();
313  GetMutableImpl()->SetProperties(props, mask);
314  }
315 
316  StateId AddState() override {
317  MutateCheck();
318  return GetMutableImpl()->AddState();
319  }
320 
321  void AddStates(size_t n) override {
322  MutateCheck();
323  return GetMutableImpl()->AddStates(n);
324  }
325 
326  void AddArc(StateId s, const Arc &arc) override {
327  MutateCheck();
328  GetMutableImpl()->AddArc(s, arc);
329  }
330 
331  void AddArc(StateId s, Arc &&arc) override {
332  MutateCheck();
333  GetMutableImpl()->AddArc(s, std::forward<Arc>(arc));
334  }
335 
336  void DeleteStates(const std::vector<StateId> &dstates) override {
337  MutateCheck();
338  GetMutableImpl()->DeleteStates(dstates);
339  }
340 
341  void DeleteStates() override {
342  if (!Unique()) {
343  const auto *isymbols = GetImpl()->InputSymbols();
344  const auto *osymbols = GetImpl()->OutputSymbols();
345  SetImpl(std::make_shared<Impl>());
346  GetMutableImpl()->SetInputSymbols(isymbols);
347  GetMutableImpl()->SetOutputSymbols(osymbols);
348  } else {
349  GetMutableImpl()->DeleteStates();
350  }
351  }
352 
353  void DeleteArcs(StateId s, size_t n) override {
354  MutateCheck();
355  GetMutableImpl()->DeleteArcs(s, n);
356  }
357 
358  void DeleteArcs(StateId s) override {
359  MutateCheck();
360  GetMutableImpl()->DeleteArcs(s);
361  }
362 
363  void ReserveStates(size_t n) override {
364  MutateCheck();
365  GetMutableImpl()->ReserveStates(n);
366  }
367 
368  void ReserveArcs(StateId s, size_t n) override {
369  MutateCheck();
370  GetMutableImpl()->ReserveArcs(s, n);
371  }
372 
373  const SymbolTable *InputSymbols() const override {
374  return GetImpl()->InputSymbols();
375  }
376 
377  const SymbolTable *OutputSymbols() const override {
378  return GetImpl()->OutputSymbols();
379  }
380 
382  MutateCheck();
383  return GetMutableImpl()->InputSymbols();
384  }
385 
387  MutateCheck();
388  return GetMutableImpl()->OutputSymbols();
389  }
390 
391  void SetInputSymbols(const SymbolTable *isyms) override {
392  MutateCheck();
393  GetMutableImpl()->SetInputSymbols(isyms);
394  }
395 
396  void SetOutputSymbols(const SymbolTable *osyms) override {
397  MutateCheck();
398  GetMutableImpl()->SetOutputSymbols(osyms);
399  }
400 
401  protected:
407 
408  explicit ImplToMutableFst(std::shared_ptr<Impl> impl)
409  : ImplToExpandedFst<Impl, FST>(impl) {}
410 
412  : ImplToExpandedFst<Impl, FST>(fst, safe) {}
413 
414  void MutateCheck() {
415  if (!Unique()) SetImpl(std::make_shared<Impl>(*this));
416  }
417 };
418 
419 } // namespace fst
420 
421 #endif // FST_MUTABLE_FST_H_
void DeleteArcs(StateId s, size_t n) override
Definition: mutable-fst.h:353
void ReserveArcs(StateId s, size_t n) override
Definition: mutable-fst.h:368
bool Read(std::istream &strm, const std::string &source, bool rewind=false)
Definition: fst.cc:50
void DeleteStates() override
Definition: mutable-fst.h:341
void AddArc(StateId s, const Arc &arc) override
Definition: mutable-fst.h:326
void Convert(FarReader< Arc > &reader, FarWriter< Arc > &writer, std::string_view fst_type)
Definition: convert.h:27
constexpr uint64_t kMutable
Definition: properties.h:48
void SetStart(StateId s) override
Definition: mutable-fst.h:298
void ReserveStates(size_t n) override
Definition: mutable-fst.h:363
const FstHeader * header
Definition: fst.h:75
static MutableFst * Read(std::istream &strm, const FstReadOptions &opts)
Definition: mutable-fst.h:123
virtual uint64_t Properties(uint64_t mask, bool test) const =0
virtual SymbolTable * MutableOutputSymbols()=0
std::unique_ptr< MutableArcIteratorBase< Arc > > base
Definition: mutable-fst.h:201
void SetFinal(StateId s, Weight weight=Weight::One()) override
Definition: mutable-fst.h:303
virtual size_t NumArcs(StateId) const =0
const SymbolTable * OutputSymbols() const override
Definition: mutable-fst.h:377
static MutableFst * Read(const std::string &source, bool convert=false, std::string_view convert_type="vector")
Definition: mutable-fst.h:152
typename fst::MutableFst< Arc >::Arc Arc
Definition: mutable-fst.h:222
std::string source
Definition: fst.h:74
typename Arc::Weight Weight
Definition: fst.h:213
virtual void InitMutableArcIterator(StateId s, MutableArcIteratorData< Arc > *data)=0
const SymbolTable * InputSymbols() const override=0
virtual void SetInputSymbols(const SymbolTable *isyms)=0
#define LOG(type)
Definition: log.h:49
void SetProperties(uint64_t props, uint64_t mask) override
Definition: mutable-fst.h:308
virtual Weight Final(StateId) const =0
SymbolTable * MutableOutputSymbols() override
Definition: mutable-fst.h:386
virtual void SetStart(StateId)=0
void DeleteArcs(StateId s) override
Definition: mutable-fst.h:358
MutableFst * Copy(bool safe=false) const override=0
void SetValue(const Arc &arc)
Definition: mutable-fst.h:241
To down_cast(From *f)
Definition: compat.h:50
SymbolTable * MutableInputSymbols() override
Definition: mutable-fst.h:381
const Arc & Value() const
Definition: mutable-fst.h:231
virtual void ReserveArcs(StateId, size_t)
Definition: mutable-fst.h:99
virtual size_t NumInputEpsilons(StateId) const =0
void AddStates(size_t n) override
Definition: mutable-fst.h:321
MutableArcIterator(FST *fst, StateId s)
Definition: mutable-fst.h:225
constexpr uint64_t kExtrinsicProperties
Definition: properties.h:184
const std::string & FstType() const
Definition: fst.h:142
StateId AddState() override
Definition: mutable-fst.h:316
const SymbolTable * OutputSymbols() const override=0
void SetOutputSymbols(const SymbolTable *osyms) override
Definition: mutable-fst.h:396
void Seek(size_t a)
Definition: mutable-fst.h:239
ImplToMutableFst(const ImplToMutableFst &fst, bool safe)
Definition: mutable-fst.h:411
void SetFlags(uint8_t flags, uint8_t mask)
Definition: mutable-fst.h:245
uint8_t Flags() const
Definition: mutable-fst.h:243
virtual void SetProperties(uint64_t props, uint64_t mask)=0
void SetInputSymbols(const SymbolTable *isyms) override
Definition: mutable-fst.h:391
virtual void AddArc(StateId state, Arc &&arc)
Definition: mutable-fst.h:81
virtual void DeleteArcs(StateId, size_t)=0
MutableFst & operator=(const MutableFst &fst)
Definition: mutable-fst.h:57
void DeleteStates(const std::vector< StateId > &dstates) override
Definition: mutable-fst.h:336
typename Arc::StateId StateId
Definition: expanded-fst.h:43
virtual MutableFst< Arc > & operator=(const Fst< Arc > &fst)=0
virtual SymbolTable * MutableInputSymbols()=0
virtual void AddArc(StateId, const Arc &)=0
A Arc
Definition: fst.h:211
ImplToMutableFst(std::shared_ptr< Impl > impl)
Definition: mutable-fst.h:408
size_t Position() const
Definition: mutable-fst.h:235
virtual StateId AddState()=0
virtual void ReserveStates(size_t)
Definition: mutable-fst.h:96
virtual void SetFinal(StateId s, Weight weight=Weight::One())=0
const SymbolTable * InputSymbols() const override
Definition: mutable-fst.h:373
typename Arc::StateId StateId
Definition: fst.h:212
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
virtual size_t NumOutputEpsilons(StateId) const =0
void AddArc(StateId s, Arc &&arc) override
Definition: mutable-fst.h:331
virtual void DeleteStates()=0
uint64_t Properties() const
Definition: fst.h:150
virtual void AddStates(size_t)=0