Newer
Older
/*
* ExactSubtreeMatchingAutomaton.cpp
*
* Created on: 9. 2. 2014
* Author: Jan Travnicek
*/
#include "ExactSubtreeMatchingAutomaton.h"
#include <tree/ranked/PrefixRankedBarTree.h>
#include <automaton/PDA/InputDrivenNPDA.h>
#include <automaton/TA/NFTA.h>
#include <alphabet/BottomOfTheStackSymbol.h>
#include <registration/AlgoRegistration.hpp>
namespace arbology {
namespace exact {
automaton::Automaton ExactSubtreeMatchingAutomaton::construct ( const tree::Tree & pattern ) {
return dispatch ( pattern.getData ( ) );
automaton::InputDrivenNPDA < > ExactSubtreeMatchingAutomaton::construct ( const tree::PrefixRankedTree < > & pattern ) {
automaton::InputDrivenNPDA < > res ( DefaultStateType ( 0 ), DefaultSymbolType ( 'S' ) );
for ( const std::ranked_symbol < > & symbol : pattern.getAlphabet ( ) ) {
res.addInputSymbol ( DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ) );
res.setPushdownStoreOperation ( DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), std::vector < DefaultSymbolType > ( 1, DefaultSymbolType ( 'S' ) ), std::vector < DefaultSymbolType > ( ( size_t ) symbol.getRank ( ), DefaultSymbolType ( 'S' ) ) );
for ( const std::ranked_symbol < > & symbol : pattern.getAlphabet ( ) ) {
res.addTransition ( DefaultStateType ( 0 ), DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), DefaultStateType ( 0 ) );
for ( const std::ranked_symbol < > & symbol : pattern.getContent ( ) ) {
res.addState ( DefaultStateType ( i ) );
res.addTransition ( DefaultStateType ( i - 1 ), DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), DefaultStateType ( i ) );
res.addFinalState ( DefaultStateType ( i - 1 ) );
auto ExactSubtreeMatchingAutomatonPrefixRankedTree = registration::OverloadRegister < ExactSubtreeMatchingAutomaton, automaton::InputDrivenNPDA < >, tree::PrefixRankedTree < > > ( ExactSubtreeMatchingAutomaton::construct );
automaton::InputDrivenNPDA < > ExactSubtreeMatchingAutomaton::construct ( const tree::PrefixRankedBarTree < > & pattern ) {
automaton::InputDrivenNPDA < > res ( DefaultStateType ( 0 ), alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) );
res.setPushdownStoreAlphabet ( { alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ), DefaultSymbolType ( 'S' ) } );
for ( const std::ranked_symbol < > & symbol : pattern.getAlphabet ( ) ) {
res.addInputSymbol ( DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ) );
if ( pattern.getBars ( ).count ( symbol ) )
res.setPushdownStoreOperation ( DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), std::vector < DefaultSymbolType > ( 1, DefaultSymbolType ( 'S' ) ), std::vector < DefaultSymbolType > { } );
res.setPushdownStoreOperation ( DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), std::vector < DefaultSymbolType > { }, std::vector < DefaultSymbolType > ( 1, DefaultSymbolType ( 'S' ) ) );
for ( const std::ranked_symbol < > & symbol : pattern.getAlphabet ( ) ) {
res.addTransition ( DefaultStateType ( 0 ), DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), DefaultStateType ( 0 ) );
for ( const std::ranked_symbol < > & symbol : pattern.getContent ( ) ) {
res.addState ( DefaultStateType ( i ) );
res.addTransition ( DefaultStateType ( i - 1 ), DefaultSymbolType ( alphabet::RankedSymbol < > { symbol } ), DefaultStateType ( i ) );
res.addFinalState ( DefaultStateType ( i - 1 ) );
auto ExactSubtreeMatchingAutomatonPrefixRankedBarTree = registration::OverloadRegister < ExactSubtreeMatchingAutomaton, automaton::InputDrivenNPDA < >, tree::PrefixRankedBarTree < > > ( ExactSubtreeMatchingAutomaton::construct );
DefaultStateType constructRecursive ( const std::tree < std::ranked_symbol < > > & node, automaton::NFTA < > & res, int & nextState ) {
std::vector < DefaultStateType > states;
states.reserve ( ( size_t ) node.getData ( ).getRank ( ) );
for ( const std::tree < std::ranked_symbol < > > & child : node.getChildren ( ) )
states.push_back ( constructRecursive ( child, res, nextState ) );
DefaultStateType state = DefaultStateType ( nextState++ );
res.addTransition ( node.getData ( ), states, state );
automaton::NFTA < > ExactSubtreeMatchingAutomaton::construct ( const tree::RankedTree < > & pattern ) {
automaton::NFTA < > res;
res.setInputAlphabet ( pattern.getAlphabet ( ) );
res.addFinalState ( constructRecursive ( pattern.getContent ( ), res, nextState ) );
auto ExactSubtreeMatchingAutomatonRankedTree = registration::OverloadRegister < ExactSubtreeMatchingAutomaton, automaton::NFTA < >, tree::RankedTree < > > ( ExactSubtreeMatchingAutomaton::construct );
} /* namespace exact */
} /* namespace arbology */