diff --git a/alib2data/src/Api.cpp b/alib2data/src/Api.cpp index 1639aec82b78229976547a052d1e8cec1a6dbee8..cfaefbde0e27358462ebd73bbca9fe319aa9442f 100644 --- a/alib2data/src/Api.cpp +++ b/alib2data/src/Api.cpp @@ -19,6 +19,7 @@ const exception::ExceptionFromXMLParser FromXMLParsers::exceptionParser; const alib::ObjectFromXMLParser FromXMLParsers::objectParser; const container::ContainerFromXMLParser FromXMLParsers::containerParser; const primitive::PrimitiveFromXMLParser FromXMLParsers::primitiveParser; +const tree::TreeFromXMLParser FromXMLParsers::treeParser; const label::LabelToXMLComposer ToXMLComposers::labelComposer; const alphabet::SymbolToXMLComposer ToXMLComposers::symbolComposer; @@ -30,6 +31,7 @@ const alib::ObjectToXMLComposer ToXMLComposers::objectComposer; const exception::ExceptionToXMLComposer ToXMLComposers::exceptionComposer; const container::ContainerToXMLComposer ToXMLComposers::containerComposer; const primitive::PrimitiveToXMLComposer ToXMLComposers::primitiveComposer; +const tree::TreeToXMLComposer ToXMLComposers::treeComposer; const ToXMLComposers ToXMLComposers::toXMLComposers; @@ -97,6 +99,7 @@ const std::string Names::PRIMITIVE_INTEGER = "Integer"; const std::string Names::PRIMITIVE_CHARACTER = "Character"; const std::string Names::PRIMITIVE_UNSIGNED = "Unsigned"; const std::string Names::PRIMITIVE_BOOL = "Bool"; +const std::string Names::TREE_RANKED_TREE = "RankedTree"; Void api<Void>::parse(std::list<sax::Token>& input) { return FromXMLParsers::objectParser.parseVoid(input); @@ -1004,6 +1007,30 @@ std::list<sax::Token> api<bool>::compose(bool data) { return ToXMLComposers::primitiveComposer.compose(data); } +tree::Tree api<tree::Tree>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::treeParser.parseTree(input); +} + +bool api<tree::Tree>::first(const std::list<sax::Token>& input) { + return FromXMLParsers::treeParser.first(input); +} + +std::list<sax::Token> api<tree::Tree>::compose(const tree::Tree& data) { + return ToXMLComposers::treeComposer.compose(data); +} + +tree::RankedTree api<tree::RankedTree>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::treeParser.parseRankedTree(input); +} + +bool api<tree::RankedTree>::first(const std::list<sax::Token>& input) { + return sax::FromXMLParserHelper::isToken(input, sax::Token::TokenType::START_ELEMENT, Names::TREE_RANKED_TREE); +} + +std::list<sax::Token> api<tree::RankedTree>::compose(const tree::RankedTree& data) { + return ToXMLComposers::treeComposer.compose(data); +} + void ToXMLComposers::Visit(void* data, const Void& voidObject) const { *((std::list<sax::Token>*) data) = std::move(api<Void>::compose(voidObject)); } @@ -1244,4 +1271,8 @@ void ToXMLComposers::Visit(void* data, const primitive::Character& primitive) co *((std::list<sax::Token>*) data) = std::move(api<primitive::Character>::compose(primitive)); } +void ToXMLComposers::Visit(void* data, const tree::RankedTree& tree) const { + *((std::list<sax::Token>*) data) = std::move(api<tree::RankedTree>::compose(tree)); +} + } /* namespace alib */ diff --git a/alib2data/src/Api.hpp b/alib2data/src/Api.hpp index 18d041ce2f7ee0a94fa4bbdb66c723ba5e3ee6da..a02227336d5f433066b0c4dc086d4ce3ffe3cdaa 100644 --- a/alib2data/src/Api.hpp +++ b/alib2data/src/Api.hpp @@ -17,6 +17,7 @@ #include "object/ObjectFromXMLParser.h" #include "exception/ExceptionFromXMLParser.h" #include "primitive/PrimitiveFromXMLParser.h" +#include "tree/TreeFromXMLParser.h" #include "label/LabelToXMLComposer.h" #include "alphabet/SymbolToXMLComposer.h" @@ -27,6 +28,7 @@ #include "object/ObjectToXMLComposer.h" #include "exception/ExceptionToXMLComposer.h" #include "primitive/PrimitiveToXMLComposer.h" +#include "tree/TreeToXMLComposer.h" #include "object/ObjectBase.h" @@ -98,6 +100,7 @@ public: const static std::string PRIMITIVE_CHARACTER; const static std::string PRIMITIVE_UNSIGNED; const static std::string PRIMITIVE_BOOL; + const static std::string TREE_RANKED_TREE; }; @@ -654,6 +657,20 @@ struct api<primitive::Character> { static std::list<sax::Token> compose(const primitive::Character& data); }; +template<> +struct api<tree::Tree> { + static tree::Tree parse(std::list<sax::Token>& input); + static bool first(const std::list<sax::Token>& input); + static std::list<sax::Token> compose(const tree::Tree& data); +}; + +template<> +struct api<tree::RankedTree> { + static tree::RankedTree parse(std::list<sax::Token>& input); + static bool first(const std::list<sax::Token>& input); + static std::list<sax::Token> compose(const tree::RankedTree& data); +}; + template<> struct api<char> { static char parse(std::list<sax::Token>& input); @@ -706,6 +723,7 @@ public: static const ObjectFromXMLParser objectParser; static const container::ContainerFromXMLParser containerParser; static const primitive::PrimitiveFromXMLParser primitiveParser; + static const tree::TreeFromXMLParser treeParser; }; @@ -780,6 +798,8 @@ class ToXMLComposers : public VisitableObjectBase::const_visitor_type { void Visit(void*, const primitive::Integer& primitive) const; void Visit(void*, const primitive::Character& primitive) const; + void Visit(void*, const tree::RankedTree& tree) const; + public: static const label::LabelToXMLComposer labelComposer; static const alphabet::SymbolToXMLComposer symbolComposer; @@ -791,6 +811,7 @@ public: static const ObjectToXMLComposer objectComposer; static const container::ContainerToXMLComposer containerComposer; static const primitive::PrimitiveToXMLComposer primitiveComposer; + static const tree::TreeToXMLComposer treeComposer; static const ToXMLComposers toXMLComposers; }; diff --git a/alib2data/src/object/ObjectBase.h b/alib2data/src/object/ObjectBase.h index b9b8014be623d382ccd93b582f8c9bbf0cb6d178..b34fbb265d1d57ad4f2b64e7a0d975bf2fca7f83 100644 --- a/alib2data/src/object/ObjectBase.h +++ b/alib2data/src/object/ObjectBase.h @@ -122,6 +122,10 @@ class Character; } +namespace tree { +class RankedTree; +} + namespace alib { class ObjectBase; @@ -136,7 +140,8 @@ typedef std::acceptor_base<ObjectBase, string::Epsilon, string::LinearString, string::CyclicString, alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol, alphabet::RankedSymbol, alphabet::BarSymbol, alphabet::RankedBarSymbol, alphabet::SubtreeWildcardSymbol, alphabet::SymbolPairSymbol, alphabet::SymbolSetSymbol, alphabet::UniqueSymbol, container::ObjectsSet, container::ObjectsVector, container::ObjectsPair, container::ObjectsMap, - primitive::String, primitive::Integer, primitive::Character + primitive::String, primitive::Integer, primitive::Character, + tree::RankedTree > VisitableObjectBase; class ObjectBase : @@ -151,7 +156,8 @@ class ObjectBase : string::Epsilon, string::LinearString, string::CyclicString, alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol, alphabet::RankedSymbol, alphabet::BarSymbol, alphabet::RankedBarSymbol, alphabet::SubtreeWildcardSymbol, alphabet::SymbolPairSymbol, alphabet::SymbolSetSymbol, alphabet::UniqueSymbol, container::ObjectsSet, container::ObjectsVector, container::ObjectsPair, container::ObjectsMap, - primitive::String, primitive::Integer, primitive::Character + primitive::String, primitive::Integer, primitive::Character, + tree::RankedTree >, public VisitableObjectBase { }; diff --git a/alib2data/src/tree/RankedNode.cpp b/alib2data/src/tree/RankedNode.cpp deleted file mode 100644 index 894899895f3d55a5573fcb01b2b88df020ab67bf..0000000000000000000000000000000000000000 --- a/alib2data/src/tree/RankedNode.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * State.cpp - * - * Created on: Mar 26, 2013 - * Author: Martin Zak - */ - -#include "State.h" -#include "../../label/Label.h" - -namespace automaton { - -State::State(const label::Label& name) : name(name) { -} - -State::State(label::Label&& name) : name(std::move(name)) { -} - -State::State(int number) : name(label::labelFrom(number)) { -} - -State::State(char character) : name(label::labelFrom(character)) { -} - -State::State(const std::string& name) : name(label::labelFrom(name)) { -} - -const label::Label& State::getName() const { - return name; -} - -bool State::operator < (const State& other) const { - return name < other.name; -} - -bool State::operator == (const State& other) const { - return name == other.name; -} - -bool State::operator != (const State& other) const{ - return !(*this == other); -} - -std::ostream& operator<<(std::ostream& out, const State& state) { - out << "(State " << state.name << ")"; - return out; -} - -State::operator std::string () const { - return (std::string) name; -} - -} /* namespace automaton */ diff --git a/alib2data/src/tree/RankedTree/RankedNode.cpp b/alib2data/src/tree/RankedTree/RankedNode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cb41ff1b962f34c823c89c7803f7d5e82257d6e --- /dev/null +++ b/alib2data/src/tree/RankedTree/RankedNode.cpp @@ -0,0 +1,76 @@ +/* + * RankedNode.cpp + * + * Created on: Nov 29, 2014 + * Author: Stepan Plachy + */ + +#include "RankedNode.h" +#include "../TreeException.h" + +namespace tree { + +RankedNode::RankedNode(const alphabet::RankedSymbol& symbol, std::vector<RankedNode *> & children) : symbol(symbol), parent(NULL), children(children) { + if((int) children.size() != symbol.getRank().getData()) throw new TreeException("Number of children doesn't match the rank of the symbol"); + for (std::vector<RankedNode *>::iterator it = children.begin(); it != children.end(); ++it) { + (*it) -> parent = this; + } +} + +const alphabet::RankedSymbol& RankedNode::getSymbol() const { + return symbol; +} + +void RankedNode::setSymbol(alphabet::RankedSymbol & symbol) { + this -> symbol = symbol; +} + +const std::vector<RankedNode *> & RankedNode::getChildren() const { + return children; +} + + +bool RankedNode::operator < (const RankedNode & other) const { + return compare(other) == -1; +} + +bool RankedNode::operator == (const RankedNode & other) const { + return compare(other) == 0; +} + +bool RankedNode::operator != (const RankedNode & other) const{ + return !(*this == other); +} + +int RankedNode::compare(const RankedNode & other) const { + if (symbol < other.symbol) return -1; + else if (symbol == other.symbol) { + std::vector<RankedNode *>::const_iterator i = children.begin(); + std::vector<RankedNode *>::const_iterator j = other.children.begin(); + while (i != this -> children.end() && j != other.children.end()) { + int result = (*i) -> compare(**j); + if (result != 0) return result; + } + if (i == children.end() && j == other.children.end()) return 0; + else if (j == other.children.end()) return 1; + else return -1; + } + else return 1; +} + +std::ostream& operator<<(std::ostream& out, const RankedNode& node) { + out << "(Ranked node " + << " symbol = " << node.symbol + << " children = {"; + for (std::vector<RankedNode *>::const_iterator it = node.children.begin(); it != node.children.end(); ++it) { + out << *it; + } + out << "})"; + return out; +} + +RankedNode::operator std::string () const { + return (std::string) symbol; +} + +} /* namespace tree */ diff --git a/alib2data/src/tree/RankedNode.h b/alib2data/src/tree/RankedTree/RankedNode.h similarity index 55% rename from alib2data/src/tree/RankedNode.h rename to alib2data/src/tree/RankedTree/RankedNode.h index 27ba426575713ca372570ad165a8a0b674281b3e..141e33a8277d333f05e194da6f9212ef04f1aa8b 100644 --- a/alib2data/src/tree/RankedNode.h +++ b/alib2data/src/tree/RankedTree/RankedNode.h @@ -10,8 +10,10 @@ #include <string> #include <ostream> +#include <vector> -//#include "../../label/Label.h" +#include "../../label/Label.h" +#include "../../alphabet/RankedSymbol.h" namespace tree { @@ -21,14 +23,23 @@ namespace tree { class RankedNode { private: alphabet::RankedSymbol symbol; + RankedNode * parent; + std::vector<RankedNode *> children; public: - explicit RankedNode(const label::Label& name); + explicit RankedNode(const alphabet::RankedSymbol& symbol, std::vector<RankedNode *> & children); const alphabet::RankedSymbol& getSymbol() const; + void setSymbol(alphabet::RankedSymbol& symbol); + RankedNode * getParent() const; + const std::vector<RankedNode *> & getChildren() const; + + void switchSubtree(RankedNode * other); + RankedNode * replaceSubtree(RankedNode * replacement); bool operator < (const RankedNode& other) const; bool operator == (const RankedNode& other) const; bool operator != (const RankedNode& other) const; + int compare (const RankedNode& other) const; friend std::ostream& operator<<(std::ostream&, const RankedNode&); diff --git a/alib2data/src/tree/RankedTree/RankedTree.cpp b/alib2data/src/tree/RankedTree/RankedTree.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9793f351742c87ca0cb806c77313e36bfff679e7 --- /dev/null +++ b/alib2data/src/tree/RankedTree/RankedTree.cpp @@ -0,0 +1,81 @@ +/* + * RankedTree.cpp + * + * Created on: Nov 29, 2014 + * Author: Stepan Plachy + */ + +#include "RankedTree.h" +#include "../TreeException.h" +#include <ostream> +#include <sstream> + +namespace tree { + +RankedTree::RankedTree(std::set<alphabet::RankedSymbol> rankedAlphabet, RankedNode * root) : rankedAlphabet(rankedAlphabet), root(root) { + +} + +TreeBase* RankedTree::clone() const { //////////////////// + return new RankedTree(*this); +} + +TreeBase* RankedTree::plunder() && { + return new RankedTree(std::move(*this)); +} +/* +bool DFA::removeInputSymbol(const alphabet::Symbol& symbol) { + for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); + transition++) { + if (transition->first.second == symbol) + throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used."); + } + + return inputAlphabet.erase(symbol); + +} +*/ +bool RankedTree::operator==(const ObjectBase& other) const { + return other == *this; +} + +bool RankedTree::operator<(const ObjectBase& other) const { + return other > *this; +} + +bool RankedTree::operator>(const ObjectBase& other) const { + return other < *this; +} + +bool RankedTree::operator==(const RankedTree & other) const { + return this -> rankedAlphabet == other.rankedAlphabet && *(this -> root) == *(other.root); +} + +bool RankedTree::operator<(const RankedTree & other) const { + if (this -> rankedAlphabet < other.rankedAlphabet) return true; + else if (this -> rankedAlphabet < other.rankedAlphabet) return *(this -> root) == *(other.root); + else return false; +} + +void RankedTree::operator>>(std::ostream& out) const { + out << "(Ranked tree" + << " rankedAlphabet = " << rankedAlphabet + << " root = " << *root + << ")"; +} + +int RankedTree::compare(const RankedTree & other) const { + if (this -> rankedAlphabet == other.rankedAlphabet) { + return this -> root -> compare(*other.root); + } + else if (this -> rankedAlphabet < other.rankedAlphabet) return -1; + else return 1; +} + +RankedTree::operator std::string () const { + std::stringstream ss; + ss << *this; + return ss.str(); +} + +} /* namespace automaton */ diff --git a/alib2data/src/tree/RankedTree/RankedTree.h b/alib2data/src/tree/RankedTree/RankedTree.h new file mode 100644 index 0000000000000000000000000000000000000000..28e56f782609104bfdc19aa5ad249ba4dc68c469 --- /dev/null +++ b/alib2data/src/tree/RankedTree/RankedTree.h @@ -0,0 +1,69 @@ +/* + * RankedTree.h + * + * Created on: Nov 29, 2014 + * Author: Stepan Plachy + */ + +#ifndef RANKEDTREE_H_ +#define RANKEDTREE_H_ + +#include "../TreeBase.h" +#include "RankedNode.h" +#include "../../alphabet/RankedSymbol.h" +#include "../../std/set.hpp" + +namespace tree { + +/** + * Represents Ranked Tree. + */ +class RankedTree : public std::acceptor<RankedTree, VisitableTreeBase, std::acceptor<RankedTree, alib::VisitableObjectBase, TreeBase> > { +protected: + std::set<alphabet::RankedSymbol> rankedAlphabet; + RankedNode * root; +public: + explicit RankedTree(std::set<alphabet::RankedSymbol> rankedAlphabet, RankedNode * root); + + virtual TreeBase* clone() const; + + virtual TreeBase* plunder() &&; + + /** + * @return tree root + */ + const RankedNode * getRoot() const {return root;} + + const std::set<alphabet::RankedSymbol> & getRankedAlphabet() const {return rankedAlphabet;} + + /** + * @copydoc Automaton::removeInputSymbol(const Symbol&) + */ +// virtual bool removeInputSymbol(const alphabet::Symbol& symbol); ////////////////// + + virtual bool operator<(const alib::ObjectBase& other) const; + virtual bool operator==(const alib::ObjectBase& other) const; + virtual bool operator>(const alib::ObjectBase& other) const; + + virtual bool operator==(const RankedTree& other) const; + virtual bool operator<(const RankedTree& other) const; + + virtual void operator>>(std::ostream& os) const; + + virtual int compare(const ObjectBase& other) const { + return -other.compare(*this); + } + + virtual int compare(const RankedTree & other) const; + + virtual operator std::string() const; + + virtual int selfTypeId() const { + return typeId<RankedTree>(); + //return 0; + } +}; + +} /* namespace tree */ + +#endif /* RANKEDTREE_H_ */ diff --git a/alib2data/src/tree/Tree.cpp b/alib2data/src/tree/Tree.cpp index f9d7bedfb5ca16d7204846000fe14782faad4db6..9eb3b4fbb035fc0361ffbff994c2bdc0dfe9d62f 100644 --- a/alib2data/src/tree/Tree.cpp +++ b/alib2data/src/tree/Tree.cpp @@ -12,21 +12,6 @@ #include "TreeException.h" namespace tree { -/* -State createUniqueState(const State& base, const std::set<State>& other) { - label::NextLabel nextLabelCreator; - - label::Label nextLabel = base.getName(); - - int i = 0; - do { - nextLabel = nextLabelCreator.nextLabel(nextLabel); - if(other.count(State(nextLabel)) == 0) - return State(nextLabel); - } while(++i < INT_MAX); - throw AutomatonException("Could not create unique state with base name " + (std::string) base.getName() + "." ); -} -*/ } /* namespace tree */ diff --git a/alib2data/src/tree/Tree.h b/alib2data/src/tree/Tree.h index 3e7ba21b763dd31c59643b7bc45c55f945f69c87..cdd4333f1bbc6931ed8b4c1cbb1897e375af7f9b 100644 --- a/alib2data/src/tree/Tree.h +++ b/alib2data/src/tree/Tree.h @@ -12,10 +12,6 @@ #include "TreeBase.h" #include "../common/wrapper.hpp" -//#include "common/State.h" -//#include <set> -//#include <string> - namespace tree { /** @@ -23,8 +19,6 @@ namespace tree { */ typedef alib::wrapper<TreeBase> Tree; -//State createUniqueState(const State& base, const std::set<State>& other); - } /* namespace tree */ #endif /* Tree_H_ */ diff --git a/alib2data/src/tree/TreeFromXMLParser.cpp b/alib2data/src/tree/TreeFromXMLParser.cpp index 2b201820d550f7cb00c89f04adde1fe466c46e32..999f190534d02a98fc18211f343b7dad8d99c850 100644 --- a/alib2data/src/tree/TreeFromXMLParser.cpp +++ b/alib2data/src/tree/TreeFromXMLParser.cpp @@ -8,13 +8,64 @@ #include "TreeFromXMLParser.h" #include "../sax/ParserException.h" -#include "../alphabet/BlankSymbol.h" -#include "../label/Label.h" #include "../Api.hpp" namespace tree { + Tree TreeFromXMLParser::parseTree(std::list<sax::Token> &input) const { + return parseTree(input, std::set<FEATURES>({FEATURES::RANKED_TREE})); + } + + Tree TreeFromXMLParser::parseTree(std::list<sax::Token>& input, const std::set<FEATURES>& features) const { + if(alib::api<RankedTree>::first(input)) { + if(!features.count(FEATURES::RANKED_TREE)) throw exception::AlibException(); + return Tree(parseRankedTree(input)); + } else + throw sax::ParserException(sax::Token("Tree / RankedTree", sax::Token::TokenType::START_ELEMENT), input.front()); + } + + RankedTree TreeFromXMLParser::parseRankedTree(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::TREE_RANKED_TREE); + + std::set<alphabet::RankedSymbol> rankedAlphabet = parseRankedAlphabet(input); + RankedNode * root = parseRankedNode(input); + + RankedTree tree(rankedAlphabet, root); + + popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::TREE_RANKED_TREE); + return tree; + } + + std::set<alphabet::RankedSymbol> TreeFromXMLParser::parseRankedAlphabet(std::list<sax::Token> &input) const { + std::set<alphabet::RankedSymbol> rankedSymbols; + popToken(input, sax::Token::TokenType::START_ELEMENT, "rankedAlphabet"); + while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { + rankedSymbols.insert(alib::api<alphabet::RankedSymbol>::parse(input)); + } + popToken(input, sax::Token::TokenType::END_ELEMENT, "rankedAlphabet"); + return rankedSymbols; + } + + RankedNode * TreeFromXMLParser::parseRankedNode(std::list<sax::Token> &input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "rankedNode"); + alphabet::RankedSymbol symbol = alib::api<alphabet::RankedSymbol>::parse(input); + int rank = symbol.getRank().getData(); + std::vector<RankedNode *> children(rank); + while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { + children.push_back(parseRankedNode(input)); + } + popToken(input, sax::Token::TokenType::END_ELEMENT, "rankedNode"); + return new RankedNode(symbol, children); + } + + bool TreeFromXMLParser::first(const std::list<sax::Token>& input) const { + if (alib::api<RankedTree>::first(input)) { + return true; + } else { + return false; + } + } } /* namespace tree */ diff --git a/alib2data/src/tree/TreeFromXMLParser.h b/alib2data/src/tree/TreeFromXMLParser.h index e66e8dd7dc9204b101f28eb1787dd78e4d19a9e7..d24fcc514110e2c79d86e88d0f13378e252dc0ce 100644 --- a/alib2data/src/tree/TreeFromXMLParser.h +++ b/alib2data/src/tree/TreeFromXMLParser.h @@ -9,7 +9,9 @@ #define TREE_FROM_XML_PARSER_H_ #include "../sax/FromXMLParserHelper.h" -//#include "FSM/DFA.h" +#include "Tree.h" +#include "TreeFeatures.h" +#include "RankedTree/RankedTree.h" #include <list> #include <set> @@ -30,6 +32,14 @@ namespace tree { */ class TreeFromXMLParser : public sax::FromXMLParserHelper { + RankedNode * parseRankedNode(std::list<sax::Token> &input) const; + std::set<alphabet::RankedSymbol> parseRankedAlphabet(std::list<sax::Token> &input) const; + + Tree parseTree(std::list<sax::Token>& input) const; + Tree parseTree(std::list<sax::Token>& input, const std::set<FEATURES>& features) const; + + RankedTree parseRankedTree(std::list<sax::Token>& input) const; + template<typename T> friend class alib::api; public: bool first(const std::list<sax::Token>& input) const; diff --git a/alib2data/src/tree/TreeToXMLComposer.cpp b/alib2data/src/tree/TreeToXMLComposer.cpp index 93187ca16fef0836506539eef9ca4df38901ce32..75afcf57d4ca39582914ec9a545d0c79a091de6c 100644 --- a/alib2data/src/tree/TreeToXMLComposer.cpp +++ b/alib2data/src/tree/TreeToXMLComposer.cpp @@ -44,6 +44,14 @@ void AutomatonToXMLComposer::composeTransitionTo(std::list<sax::Token>& out, con } */ +void TreeToXMLComposer::composeInputRankedAlphabet(std::list<sax::Token>& out, const std::set<alphabet::RankedSymbol>& symbols) const { + out.push_back(sax::Token("inputRankedAlphabet", sax::Token::TokenType::START_ELEMENT)); + for (const auto& symbol : symbols) { + out.splice(out.end(), alib::api<alphabet::RankedSymbol>::compose(symbol)); + } + out.push_back(sax::Token("inputRankedAlphabet", sax::Token::TokenType::END_ELEMENT)); +} + std::list<sax::Token> TreeToXMLComposer::compose(const TreeBase& tree) const { std::list<sax::Token> out; tree.Accept((void*) &out, alib::ToXMLComposers::toXMLComposers); @@ -55,6 +63,28 @@ std::list<sax::Token> TreeToXMLComposer::compose(const Tree& tree) const { tree.getData().Accept((void*) &out, alib::ToXMLComposers::toXMLComposers); return out; } + +std::list<sax::Token> TreeToXMLComposer::compose(const RankedTree& tree) const { + std::list<sax::Token> out; + out.push_back(sax::Token(alib::Names::TREE_RANKED_TREE, sax::Token::TokenType::START_ELEMENT)); + + composeInputRankedAlphabet(out, tree.getRankedAlphabet()); + composeNode(out, *tree.getRoot()); + + out.push_back(sax::Token(alib::Names::TREE_RANKED_TREE, sax::Token::TokenType::END_ELEMENT)); + return out; +} + +void TreeToXMLComposer::composeNode(std::list<sax::Token>& out, const RankedNode& node) const { + out.push_back(sax::Token("rankedNode", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::api<alphabet::RankedSymbol>::compose(node.getSymbol())); + for(const auto& child : node.getChildren()) { + composeNode(out, *child); + } + + out.push_back(sax::Token("rankedNode", sax::Token::TokenType::END_ELEMENT)); +} + /* std::list<sax::Token> AutomatonToXMLComposer::compose(const DFA& automaton) const { std::list<sax::Token> out; diff --git a/alib2data/src/tree/TreeToXMLComposer.h b/alib2data/src/tree/TreeToXMLComposer.h index 34680ec5e67de71e32f26385d420e3cde80784fe..751c806a54e93254fc2d71888327444c7ad2cf14 100644 --- a/alib2data/src/tree/TreeToXMLComposer.h +++ b/alib2data/src/tree/TreeToXMLComposer.h @@ -11,7 +11,7 @@ #include <string> #include <list> #include "Tree.h" -//#include "FSM/EpsilonNFA.h" +#include "RankedTree/RankedTree.h" #include "../sax/Token.h" namespace alib { @@ -27,12 +27,8 @@ namespace tree { * This class contains methods to print XML representation of tree to the output stream. */ class TreeToXMLComposer { -// void composeStates(std::list<sax::Token>&, const std::set<State>& states) const; - -// void composeTransitions(std::list<sax::Token>&, const EpsilonNFA& automaton) const; - -// void composeTransitionTo(std::list<sax::Token>&, const State& state) const; - + + void composeInputRankedAlphabet(std::list<sax::Token>& out, const std::set<alphabet::RankedSymbol>& symbols) const; /** * Prints XML representation of Tree to the output stream. * @param tree tree to print @@ -42,8 +38,9 @@ class TreeToXMLComposer { std::list<sax::Token> compose(const Tree& tree) const; + std::list<sax::Token> compose(const RankedTree& tree) const; -// std::list<sax::Token> compose(const State& state) const; + void composeNode(std::list<sax::Token>& out, const RankedNode& node) const; template<typename T> friend class alib::api; }; diff --git a/alib2data/src/tree/Trees/RankedTree.cpp b/alib2data/src/tree/Trees/RankedTree.cpp deleted file mode 100644 index 5037f60424a77019b10fddc891111be355808c71..0000000000000000000000000000000000000000 --- a/alib2data/src/tree/Trees/RankedTree.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - * DFA.cpp - * - * Created on: Mar 25, 2013 - * Author: Jan Travnicek - */ - -#include "DFA.h" -#include "../../std/set.hpp" -#include "../../std/map.hpp" -#include "../AutomatonException.h" -#include <ostream> -#include <sstream> - -namespace automaton { - -DFA::DFA(const State& initialState) : SingleInitialState(initialState) { - -} - -AutomatonBase* DFA::clone() const { - return new DFA(*this); -} - -AutomatonBase* DFA::plunder() && { - return new DFA(std::move(*this)); -} - -bool DFA::removeState(const State& state) { - if (initialState == state) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); - } - - if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); - } - - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (t->first.first == state || t->second == state) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - } - - return states.erase(state); -} - -bool DFA::removeInputSymbol(const alphabet::Symbol& symbol) { - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->first.second == symbol) - throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used."); - } - - return inputAlphabet.erase(symbol); - -} - -bool DFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { - if (states.find(from) == states.end()) - throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); - - if (inputAlphabet.find(input) == inputAlphabet.end()) - throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); - - if (states.find(to) == states.end()) - throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); - - std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); - - if (transitions.find(key) != transitions.end()) { - if(transitions.find(key)->second == to) - return false; - else - throw AutomatonException( - "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + "\") -> \"" + (std::string) to.getName() - + "\" already exists."); - } - - transitions.insert(std::make_pair(key, to)); - return true; -} - -bool DFA::removeTransition(const State& from, const alphabet::Symbol& input, const State& to) { - std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); - - if (transitions.find(key) == transitions.end()) - return false; - - if(transitions.find(key)->second != to) - throw AutomatonException( - "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input - + "\") -> \"" + (std::string) to.getName() + "\" doesn't exist."); - - transitions.erase(key); - return true; -} - -const std::map<std::pair<State, alphabet::Symbol>, State>& DFA::getTransitions() const { - return transitions; -} - -std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsFromState(const State& from) const { - if( states.find(from) == states.end()) - throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); - - std::map<std::pair<State, alphabet::Symbol>, State> transitionsFromState; - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->first.first == from) { - transitionsFromState.insert(std::make_pair(transition->first, transition->second)); - } - } - - return transitionsFromState; -} - -std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsToState(const State& to) const { - if( states.find(to) == states.end()) - throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); - - std::map<std::pair<State, alphabet::Symbol>, State> transitionsToState; - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->second == to) { - transitionsToState.insert(std::make_pair(transition->first, transition->second)); - } - } - - return transitionsToState; -} - -bool DFA::isTotal() const { - return transitions.size() == inputAlphabet.size() * states.size(); -} - -bool DFA::operator==(const ObjectBase& other) const { - return other == *this; -} - -bool DFA::operator<(const ObjectBase& other) const { - return other > *this; -} - -bool DFA::operator>(const ObjectBase& other) const { - return other < *this; -} - -bool DFA::operator==(const DFA& other) const { - return this->states == other.states && this->inputAlphabet == other.inputAlphabet && this->initialState == other.initialState && this->finalStates == other.finalStates && this->transitions == other.transitions; -} - -bool DFA::operator<(const DFA& other) const { - return std::tie(states, inputAlphabet, initialState, finalStates, transitions) < std::tie(other.states, other.inputAlphabet, other.initialState, other.finalStates, other.transitions); -} - -void DFA::operator>>(std::ostream& out) const { - out << "(DFA" - << " states = " << states - << " inputAlphabet = " << inputAlphabet - << " initialState = " << initialState - << " finalStates = " << finalStates - << " transitions = " << transitions - << ")"; -} - -DFA::operator std::string () const { - std::stringstream ss; - ss << *this; - return ss.str(); -} - -} /* namespace automaton */ diff --git a/alib2data/src/tree/Trees/RankedTree.h b/alib2data/src/tree/Trees/RankedTree.h deleted file mode 100644 index 530643fa48bd144a3508f110f430d6d7a1b1865d..0000000000000000000000000000000000000000 --- a/alib2data/src/tree/Trees/RankedTree.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * RankedTree.h - * - * Created on: Mar 25, 2013 - * Author: Jan Travnicek - */ - -#ifndef RANKEDTREE_H_ -#define RANKEDTREE_H_ - -#include <map> -#include "../TreeBase.h" -#include "../common/SingleInitialState.h" -#include "../common/InputAlphabet.h" -#include "../../alphabet/Symbol.h" - -namespace automaton { - -/** - * Represents Finite Automaton. - * Can store nondeterministic finite automaton without epsilon transitions. - */ -class DFA : public std::acceptor<DFA, VisitableAutomatonBase, std::acceptor<DFA, alib::VisitableObjectBase, AutomatonBase> >, public SingleInitialState, public InputAlphabet { -protected: - std::map<std::pair<State, alphabet::Symbol>, State> transitions; -public: - explicit DFA(const State& initialState); - - virtual AutomatonBase* clone() const; - - virtual AutomatonBase* plunder() &&; - - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual bool removeState(const State& state); - - /** - * @copydoc Automaton::removeInputSymbol(const Symbol&) - */ - virtual bool removeInputSymbol(const alphabet::Symbol& symbol); - - /** - * Adds transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton - */ - bool addTransition(const State& current, const alphabet::Symbol& input, const State& next); - - /** - * Removes transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. - */ - bool removeTransition(const State& current, const alphabet::Symbol& input, const State& next); - - /** - * @return automaton transitions - */ - const std::map<std::pair<State, alphabet::Symbol>, State>& getTransitions() const; - - /** - * @return automaton transitions from state - */ - std::map<std::pair<State, alphabet::Symbol>, State> getTransitionsFromState(const State& from) const; - - /** - * @return automaton transitions to state - */ - std::map<std::pair<State, alphabet::Symbol>, State> getTransitionsToState(const State& from) const; - - /** - * Determines whether DFA is total deterministic - * FA is deterministic if and only if - * \li \c it is deterministic. (Trivial here) - * \li \c size of transition function \forall states and \forall input symbols \delta (from state, input symbol) = 1 - * @return true when automaton is total deterministic, false otherwise - */ - bool isTotal() const; - - virtual bool operator<(const alib::ObjectBase& other) const; - virtual bool operator==(const alib::ObjectBase& other) const; - virtual bool operator>(const alib::ObjectBase& other) const; - - virtual bool operator==(const DFA& other) const; - virtual bool operator<(const DFA& other) const; - - virtual void operator>>(std::ostream& os) const; - - virtual operator std::string() const; - - virtual int selfTypeId() const { - return typeId<DFA>(); - } -}; - -} /* namespace automaton */ - -#endif /* DFA_H_ */ diff --git a/alib2data/src/tree/Trees/UnrankedTree.cpp b/alib2data/src/tree/Trees/UnrankedTree.cpp deleted file mode 100644 index 5037f60424a77019b10fddc891111be355808c71..0000000000000000000000000000000000000000 --- a/alib2data/src/tree/Trees/UnrankedTree.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - * DFA.cpp - * - * Created on: Mar 25, 2013 - * Author: Jan Travnicek - */ - -#include "DFA.h" -#include "../../std/set.hpp" -#include "../../std/map.hpp" -#include "../AutomatonException.h" -#include <ostream> -#include <sstream> - -namespace automaton { - -DFA::DFA(const State& initialState) : SingleInitialState(initialState) { - -} - -AutomatonBase* DFA::clone() const { - return new DFA(*this); -} - -AutomatonBase* DFA::plunder() && { - return new DFA(std::move(*this)); -} - -bool DFA::removeState(const State& state) { - if (initialState == state) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); - } - - if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); - } - - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (t->first.first == state || t->second == state) - throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); - } - - return states.erase(state); -} - -bool DFA::removeInputSymbol(const alphabet::Symbol& symbol) { - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->first.second == symbol) - throw AutomatonException("Input symbol \"" + (std::string) symbol + "\" is used."); - } - - return inputAlphabet.erase(symbol); - -} - -bool DFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { - if (states.find(from) == states.end()) - throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); - - if (inputAlphabet.find(input) == inputAlphabet.end()) - throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); - - if (states.find(to) == states.end()) - throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); - - std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); - - if (transitions.find(key) != transitions.end()) { - if(transitions.find(key)->second == to) - return false; - else - throw AutomatonException( - "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + "\") -> \"" + (std::string) to.getName() - + "\" already exists."); - } - - transitions.insert(std::make_pair(key, to)); - return true; -} - -bool DFA::removeTransition(const State& from, const alphabet::Symbol& input, const State& to) { - std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); - - if (transitions.find(key) == transitions.end()) - return false; - - if(transitions.find(key)->second != to) - throw AutomatonException( - "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input - + "\") -> \"" + (std::string) to.getName() + "\" doesn't exist."); - - transitions.erase(key); - return true; -} - -const std::map<std::pair<State, alphabet::Symbol>, State>& DFA::getTransitions() const { - return transitions; -} - -std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsFromState(const State& from) const { - if( states.find(from) == states.end()) - throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); - - std::map<std::pair<State, alphabet::Symbol>, State> transitionsFromState; - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->first.first == from) { - transitionsFromState.insert(std::make_pair(transition->first, transition->second)); - } - } - - return transitionsFromState; -} - -std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsToState(const State& to) const { - if( states.find(to) == states.end()) - throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); - - std::map<std::pair<State, alphabet::Symbol>, State> transitionsToState; - for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (transition->second == to) { - transitionsToState.insert(std::make_pair(transition->first, transition->second)); - } - } - - return transitionsToState; -} - -bool DFA::isTotal() const { - return transitions.size() == inputAlphabet.size() * states.size(); -} - -bool DFA::operator==(const ObjectBase& other) const { - return other == *this; -} - -bool DFA::operator<(const ObjectBase& other) const { - return other > *this; -} - -bool DFA::operator>(const ObjectBase& other) const { - return other < *this; -} - -bool DFA::operator==(const DFA& other) const { - return this->states == other.states && this->inputAlphabet == other.inputAlphabet && this->initialState == other.initialState && this->finalStates == other.finalStates && this->transitions == other.transitions; -} - -bool DFA::operator<(const DFA& other) const { - return std::tie(states, inputAlphabet, initialState, finalStates, transitions) < std::tie(other.states, other.inputAlphabet, other.initialState, other.finalStates, other.transitions); -} - -void DFA::operator>>(std::ostream& out) const { - out << "(DFA" - << " states = " << states - << " inputAlphabet = " << inputAlphabet - << " initialState = " << initialState - << " finalStates = " << finalStates - << " transitions = " << transitions - << ")"; -} - -DFA::operator std::string () const { - std::stringstream ss; - ss << *this; - return ss.str(); -} - -} /* namespace automaton */ diff --git a/alib2data/src/tree/Trees/UnrankedTree.h b/alib2data/src/tree/Trees/UnrankedTree.h deleted file mode 100644 index cd5344e6f35027535661c4acb6b036b6146ef4ae..0000000000000000000000000000000000000000 --- a/alib2data/src/tree/Trees/UnrankedTree.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * DFA.h - * - * Created on: Mar 25, 2013 - * Author: Jan Travnicek - */ - -#ifndef DFA_H_ -#define DFA_H_ - -#include <map> -#include "../AutomatonBase.h" -#include "../common/SingleInitialState.h" -#include "../common/InputAlphabet.h" -#include "../../alphabet/Symbol.h" - -namespace automaton { - -/** - * Represents Finite Automaton. - * Can store nondeterministic finite automaton without epsilon transitions. - */ -class DFA : public std::acceptor<DFA, VisitableAutomatonBase, std::acceptor<DFA, alib::VisitableObjectBase, AutomatonBase> >, public SingleInitialState, public InputAlphabet { -protected: - std::map<std::pair<State, alphabet::Symbol>, State> transitions; -public: - explicit DFA(const State& initialState); - - virtual AutomatonBase* clone() const; - - virtual AutomatonBase* plunder() &&; - - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual bool removeState(const State& state); - - /** - * @copydoc Automaton::removeInputSymbol(const Symbol&) - */ - virtual bool removeInputSymbol(const alphabet::Symbol& symbol); - - /** - * Adds transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton - */ - bool addTransition(const State& current, const alphabet::Symbol& input, const State& next); - - /** - * Removes transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. - */ - bool removeTransition(const State& current, const alphabet::Symbol& input, const State& next); - - /** - * @return automaton transitions - */ - const std::map<std::pair<State, alphabet::Symbol>, State>& getTransitions() const; - - /** - * @return automaton transitions from state - */ - std::map<std::pair<State, alphabet::Symbol>, State> getTransitionsFromState(const State& from) const; - - /** - * @return automaton transitions to state - */ - std::map<std::pair<State, alphabet::Symbol>, State> getTransitionsToState(const State& from) const; - - /** - * Determines whether DFA is total deterministic - * FA is deterministic if and only if - * \li \c it is deterministic. (Trivial here) - * \li \c size of transition function \forall states and \forall input symbols \delta (from state, input symbol) = 1 - * @return true when automaton is total deterministic, false otherwise - */ - bool isTotal() const; - - virtual bool operator<(const alib::ObjectBase& other) const; - virtual bool operator==(const alib::ObjectBase& other) const; - virtual bool operator>(const alib::ObjectBase& other) const; - - virtual bool operator==(const DFA& other) const; - virtual bool operator<(const DFA& other) const; - - virtual void operator>>(std::ostream& os) const; - - virtual operator std::string() const; - - virtual int selfTypeId() const { - return typeId<DFA>(); - } -}; - -} /* namespace automaton */ - -#endif /* DFA_H_ */