Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RankedNonlinearPattern.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
27
28namespace tree {
29
30template < class SymbolType = DefaultSymbolType >
31class RankedNonlinearPattern;
32
33} /* namespace tree */
34
35
36#include <ext/iostream>
37#include <ext/algorithm>
38
39#include <alib/string>
40#include <alib/set>
41#include <alib/tree>
42
43#include <core/components.hpp>
45
46#include <tree/TreeException.h>
48
49#include <core/normalize.hpp>
51
52#include "../unranked/UnrankedNonlinearPattern.h"
53
54namespace tree {
55
56class GeneralAlphabet;
57class SubtreeWildcard;
58class NonlinearAlphabet;
59
73template < class SymbolType >
74class RankedNonlinearPattern final : public core::Components < RankedNonlinearPattern < SymbolType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, std::tuple < GeneralAlphabet, NonlinearAlphabet >, common::ranked_symbol < SymbolType >, component::Value, SubtreeWildcard > {
79
87 void checkAlphabet ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
88
96 void checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const;
97
98public:
108
117
125
132
139 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
140 }
141
148 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
149 }
150
157 this->template accessComponent < GeneralAlphabet > ( ).add ( symbols );
158 }
159
166 return this->template accessComponent < SubtreeWildcard > ( ).get ( );
167 }
168
175 return std::move ( this->template accessComponent < SubtreeWildcard > ( ).get ( ) );
176 }
177
184 return this->template accessComponent < NonlinearAlphabet > ( ).get ( );
185 }
186
193 return std::move ( this->template accessComponent < NonlinearAlphabet > ( ).get ( ) );
194 }
195
202
208 ext::tree < common::ranked_symbol < SymbolType > > && getContent ( ) &&;
209
217 void setTree ( ext::tree < common::ranked_symbol < SymbolType > > pattern );
218
226 auto operator <=> ( const RankedNonlinearPattern & other ) const {
227 return std::tie ( m_content, getAlphabet(), getSubtreeWildcard(), getNonlinearVariables() ) <=> std::tie ( other.m_content, other.getAlphabet(), other.getSubtreeWildcard(), getNonlinearVariables() );
228 }
229
237 bool operator == ( const RankedNonlinearPattern & other ) const {
238 return std::tie ( m_content, getAlphabet(), getSubtreeWildcard(), getNonlinearVariables() ) == std::tie ( other.m_content, other.getAlphabet(), other.getSubtreeWildcard(), getNonlinearVariables() );
239 }
240
249 friend ext::ostream & operator << ( ext::ostream & out, const RankedNonlinearPattern & instance ) {
250 out << "(RankedNonlinearPattern";
251 out << " alphabet = " << instance.getAlphabet ( );
252 out << " content = " << instance.getContent ( );
253 out << " nonlinearVariables = " << instance.getNonlinearVariables ( );
254 out << " subtreeWildcard = " << instance.getSubtreeWildcard ( );
255 out << ")";
256 return out;
257 }
258
264 void nicePrint ( ext::ostream & os ) const;
265};
266
267template < class SymbolType >
268RankedNonlinearPattern < SymbolType >::RankedNonlinearPattern ( common::ranked_symbol < SymbolType > subtreeWildcard, ext::set < common::ranked_symbol < SymbolType > > nonlinearVariables, ext::set < common::ranked_symbol < SymbolType > > alphabet, ext::tree < common::ranked_symbol < SymbolType > > pattern ) : core::Components < RankedNonlinearPattern, ext::set < common::ranked_symbol < SymbolType > >, component::Set, std::tuple < GeneralAlphabet, NonlinearAlphabet >, common::ranked_symbol < SymbolType >, component::Value, SubtreeWildcard > ( std::move ( alphabet ), std::move ( nonlinearVariables ), std::move ( subtreeWildcard ) ), m_content ( std::move ( pattern ) ) {
269 checkAlphabet ( m_content );
270 checkArities ( m_content );
271}
272
273template < class SymbolType >
274RankedNonlinearPattern < SymbolType >::RankedNonlinearPattern ( common::ranked_symbol < SymbolType > subtreeWildcard, ext::set < common::ranked_symbol < SymbolType > > nonlinearVariables, ext::tree < common::ranked_symbol < SymbolType > > pattern ) : RankedNonlinearPattern ( subtreeWildcard, nonlinearVariables, ext::set < common::ranked_symbol < SymbolType > > ( pattern.prefix_begin ( ), pattern.prefix_end ( ) ) + nonlinearVariables + ext::set < common::ranked_symbol < SymbolType > > { subtreeWildcard }, pattern ) {
275}
276
277template < class SymbolType >
279}
280
281template < class SymbolType >
282RankedNonlinearPattern < SymbolType >::RankedNonlinearPattern ( const UnrankedNonlinearPattern < SymbolType > & other ) : RankedNonlinearPattern ( common::ranked_symbol < SymbolType > ( other.getSubtreeWildcard ( ), 0 ), TreeAuxiliary::unrankedToRanked < SymbolType > ( other.getContent ( ) ) ) {
283}
284
285template < class SymbolType >
287 return m_content;
288}
289
290template < class SymbolType >
292 return std::move ( m_content );
293}
294
295template < class SymbolType >
297 ext::set < common::ranked_symbol < SymbolType > > minimalAlphabet ( data.prefix_begin ( ), data.prefix_end ( ) );
298 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const common::ranked_symbol < SymbolType > & ) {
299 throw TreeException ( "Input symbols not in the alphabet." );
300 } ) );
301}
302
303template < class SymbolType >
304void RankedNonlinearPattern < SymbolType >::checkArities ( const ext::tree < common::ranked_symbol < SymbolType > > & data ) const {
305 if ( data.getData ( ).getRank ( ) != data.getChildren ( ).size ( ) )
306 throw exception::CommonException ( "Invalid rank." );
307
308 for ( const ext::tree < common::ranked_symbol < SymbolType > > & child : data )
309 checkArities ( child );
310}
311
312template < class SymbolType >
314 checkAlphabet ( pattern );
315 checkArities ( pattern );
316
317 this->m_content = std::move ( pattern );
318}
319
320template < class SymbolType >
322 m_content.nicePrint ( os );
323}
324
325} /* namespace tree */
326
327namespace core {
328
334template < class SymbolType >
335class SetConstraint< tree::RankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::GeneralAlphabet > {
336public:
346 const ext::tree < common::ranked_symbol < SymbolType > > & m_content = pattern.getContent ( );
347 return std::find(m_content.prefix_begin(), m_content.prefix_end(), symbol) != m_content.prefix_end() || pattern.template accessComponent < tree::SubtreeWildcard > ( ).get ( ) == symbol || pattern.template accessComponent < tree::NonlinearAlphabet > ( ).get ( ).count ( symbol );
348 }
349
359 return true;
360 }
361
369 }
370};
371
377template < class SymbolType >
378class SetConstraint< tree::RankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::NonlinearAlphabet > {
379public:
389 return false;
390 }
391
401 return pattern.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
402}
403
413 if( symbol.getRank() != 0 )
414 throw tree::TreeException ( "Nonlinear variable symbol has nonzero arity" );
415
416 if ( pattern.template accessComponent < tree::SubtreeWildcard > ( ).get ( ) == symbol )
417 throw tree::TreeException ( "Symbol " + ext::to_string ( symbol ) + "cannot be set as nonlinear variable since it is already subtree wildcard" );
418 }
419};
420
426template < class SymbolType >
427class ElementConstraint< tree::RankedNonlinearPattern < SymbolType >, common::ranked_symbol < SymbolType >, tree::SubtreeWildcard > {
428public:
438 return pattern.template accessComponent < tree::GeneralAlphabet > ( ).get ( ).count ( symbol );
439 }
440
450 if( symbol.getRank() != 0 )
451 throw tree::TreeException ( "SubtreeWildcard symbol has nonzero arity" );
452
453 if ( pattern.template accessComponent < tree::NonlinearAlphabet > ( ).get ( ).count ( symbol ) )
454 throw tree::TreeException ( "Symbol " + ext::to_string ( symbol ) + "cannot be set as subtree wildcard since it is already nonlinear variable" );
455 }
456};
457
463template < class SymbolType >
464struct normalize < tree::RankedNonlinearPattern < SymbolType > > {
466 common::ranked_symbol < DefaultSymbolType > wildcard = alphabet::SymbolNormalize::normalizeRankedSymbol ( std::move ( value ).getSubtreeWildcard ( ) );
467 ext::set < common::ranked_symbol < DefaultSymbolType > > nonlinearAlphabet = alphabet::SymbolNormalize::normalizeRankedAlphabet ( std::move ( value ).getNonlinearVariables ( ) );
470
471 return tree::RankedNonlinearPattern < > ( std::move ( wildcard ), std::move ( nonlinearAlphabet ), std::move ( alphabet ), std::move ( content ) );
472 }
473};
474
475} /* namespace core */
476
477extern template class tree::RankedNonlinearPattern < >;
478
static common::ranked_symbol< DefaultSymbolType > normalizeRankedSymbol(common::ranked_symbol< SymbolType > &&symbol)
Definition: SymbolNormalize.h:81
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
Definition: ranked_symbol.hpp:20
const size_t & getRank() const &
Definition: ranked_symbol.hpp:83
Definition: components.hpp:181
static bool available(const tree::RankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedNonlinearPattern.h:437
static void valid(const tree::RankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedNonlinearPattern.h:449
Definition: components.hpp:25
static bool available(const tree::RankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedNonlinearPattern.h:400
static void valid(const tree::RankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedNonlinearPattern.h:412
static bool used(const tree::RankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: RankedNonlinearPattern.h:388
static void valid(const tree::RankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: RankedNonlinearPattern.h:368
static bool used(const tree::RankedNonlinearPattern< SymbolType > &pattern, const common::ranked_symbol< SymbolType > &symbol)
Definition: RankedNonlinearPattern.h:345
static bool available(const tree::RankedNonlinearPattern< SymbolType > &, const common::ranked_symbol< SymbolType > &)
Definition: RankedNonlinearPattern.h:358
Definition: setComponents.hpp:26
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Output iterator calling a callback function on assignment.
Definition: iterator.hpp:923
Definition: ostream.h:14
Definition: set.hpp:44
Class introducing a tree with interface trying to be close to the interface of standard library conta...
Definition: tree.hpp:52
const_prefix_iterator prefix_begin() const
Getter of the prefix iterator to the root node.
Definition: tree.hpp:904
const_prefix_iterator prefix_end() const
Getter of the prefix iterator one after the last node in the prefix traversal.
Definition: tree.hpp:914
Nonlinear tree pattern represented in its natural representation. The representation is so called ran...
Definition: RankedNonlinearPattern.h:74
ext::set< common::ranked_symbol< SymbolType > > && getAlphabet() &&
Definition: RankedNonlinearPattern.h:147
const ext::set< common::ranked_symbol< SymbolType > > & getNonlinearVariables() const &
Definition: RankedNonlinearPattern.h:183
friend ext::ostream & operator<<(ext::ostream &out, const RankedNonlinearPattern &instance)
Definition: RankedNonlinearPattern.h:249
void setTree(ext::tree< common::ranked_symbol< SymbolType > > pattern)
Definition: RankedNonlinearPattern.h:313
common::ranked_symbol< SymbolType > && getSubtreeWildcard() &&
Definition: RankedNonlinearPattern.h:174
void nicePrint(ext::ostream &os) const
Definition: RankedNonlinearPattern.h:321
const common::ranked_symbol< SymbolType > & getSubtreeWildcard() const &
Definition: RankedNonlinearPattern.h:165
RankedNonlinearPattern(common::ranked_symbol< SymbolType > subtreeWildcard, ext::set< common::ranked_symbol< SymbolType > > nonlinearVariables, ext::set< common::ranked_symbol< SymbolType > > alphabet, ext::tree< common::ranked_symbol< SymbolType > > pattern)
Creates a new instance of the pattern with concrete alphabet, content, wildcard, and nonlinear variab...
Definition: RankedNonlinearPattern.h:268
void extendAlphabet(const ext::set< common::ranked_symbol< SymbolType > > &symbols)
Definition: RankedNonlinearPattern.h:156
bool operator==(const RankedNonlinearPattern &other) const
Definition: RankedNonlinearPattern.h:237
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: RankedNonlinearPattern.h:138
const ext::tree< common::ranked_symbol< SymbolType > > & getContent() const &
Definition: RankedNonlinearPattern.h:286
ext::set< common::ranked_symbol< SymbolType > > && getNonlinearVariables() &&
Definition: RankedNonlinearPattern.h:192
Definition: TreeException.h:15
static ext::tree< common::ranked_symbol< DefaultSymbolType > > normalizeRankedTree(ext::tree< common::ranked_symbol< SymbolType > > &&tree)
Definition: TreeNormalize.h:40
Nonlinear tree pattern represented in its natural representation. The representation is so called unr...
Definition: UnrankedNonlinearPattern.h:75
Definition: BarSymbol.cpp:12
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: Permutation.hpp:18
Definition: normalize.hpp:10
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: BackwardOccurrenceTest.h:17
static tree::RankedNonlinearPattern< > eval(tree::RankedNonlinearPattern< SymbolType > &&value)
Definition: RankedNonlinearPattern.h:465
Definition: normalize.hpp:13