From 6fda6817f40550cb15d63f6cdcee6ae8273f9840 Mon Sep 17 00:00:00 2001 From: Alexey Fedoseev Date: Wed, 24 Apr 2024 11:52:00 +0300 Subject: [PATCH] new transitions interface --- cyberiadamlpp.cpp | 56 +++++++++++++++++++++++++++++++++++++---------- cyberiadamlpp.h | 6 +++-- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/cyberiadamlpp.cpp b/cyberiadamlpp.cpp index fae9d31..a285cb2 100644 --- a/cyberiadamlpp.cpp +++ b/cyberiadamlpp.cpp @@ -682,7 +682,17 @@ void ElementCollection::add_element(Element* e) { CYB_ASSERT(e); CYB_ASSERT(e->get_parent() == this); - children.push_back(e); + if (e->get_type() == elementTransition) { + children.push_back(e); + } else { + ElementList::iterator i; + for (i = children.begin(); i != children.end(); i++) { + if ((*i)->get_type() == elementTransition) { + break; + } + } + children.insert(i, e); + } } void ElementCollection::add_first_element(Element* e) @@ -1080,6 +1090,8 @@ CyberiadaEdge* Transition::to_edge() const std::ostream& Transition::dump(std::ostream& os) const { Element::dump(os); + os << ", source: '" << source->get_id() << "'"; + os << ", target: '" << target->get_id() << "'"; if (has_action()) { os << ", action: {"; os << action; @@ -1312,30 +1324,35 @@ ChoicePseudostate* Document::new_choice(ElementCollection* _parent, const ID& _i return choice; } -Transition* Document::new_transition(ElementCollection* _parent, Element* source, Element* target, +Transition* Document::new_transition(StateMachine* sm, Element* source, Element* target, const Action& action, const Polyline& pl, const Point& sp, const Point& tp, const Point& label, const Color& c) { - check_parent_element(_parent); + check_parent_element(sm); + check_transition_element(source); + check_transition_element(target); + check_transition_action(action); - Transition* t = new Transition(_parent, generate_vertex_id(_parent), source, target, - action, pl, sp, tp, label, c); - _parent->add_element(t); + Transition* t = new Transition(sm, generate_transition_id(source->get_id(), target->get_id()), + source, target, action, pl, sp, tp, label, c); + sm->add_element(t); return t; } -Transition* Document::new_transition(ElementCollection* _parent, const ID& _id, Element* source, Element* target, +Transition* Document::new_transition(StateMachine* sm, const ID& _id, Element* source, Element* target, const Action& action, const Polyline& pl, const Point& sp, const Point& tp, const Point& label, const Color& c) { - check_parent_element(_parent); + check_parent_element(sm); + check_transition_element(source); + check_transition_element(target); check_id_uniqueness(_id); + check_transition_action(action); - Transition* t = new Transition(_parent, _id, source, target, - action, pl, sp, tp, label, c); - _parent->add_element(t); + Transition* t = new Transition(sm, _id, source, target, action, pl, sp, tp, label, c); + sm->add_element(t); return t; } @@ -1375,6 +1392,23 @@ void Document::check_single_initial(const ElementCollection* _parent) const } } +void Document::check_transition_action(const Action& action) const +{ + if (action.get_type() != actionTransition) { + throw ParametersException("Transitions cannot contain entry/exit activities"); + } +} + +void Document::check_transition_element(const Element* element) const +{ + if (!element) { + throw ParametersException("Empty element"); + } + if (element->get_type() == elementRoot || element->get_type() == elementSM) { + throw ParametersException("Bad element for transition"); + } +} + void Document::import_nodes_recursively(ElementCollection* collection, CyberiadaNode* nodes) { CYB_ASSERT(collection); diff --git a/cyberiadamlpp.h b/cyberiadamlpp.h index 96199d2..7b8aa9b 100644 --- a/cyberiadamlpp.h +++ b/cyberiadamlpp.h @@ -558,11 +558,11 @@ namespace Cyberiada { const Rect& r = Rect(), const Color& color = Color()); ChoicePseudostate* new_choice(ElementCollection* parent, const ID& id, const Name& name, const Rect& r = Rect(), const Color& color = Color()); - Transition* new_transition(ElementCollection* parent, Element* source, Element* target, + Transition* new_transition(StateMachine* sm, Element* source, Element* target, const Action& action, const Polyline& pl = Polyline(), const Point& sp = Point(), const Point& tp = Point(), const Point& label = Point(), const Color& color = Color()); - Transition* new_transition(ElementCollection* parent, const ID& id, Element* source, Element* target, + Transition* new_transition(StateMachine* sm, const ID& id, Element* source, Element* target, const Action& action, const Polyline& pl = Polyline(), const Point& sp = Point(), const Point& tp = Point(), const Point& label = Point(), const Color& color = Color()); @@ -594,6 +594,8 @@ namespace Cyberiada { void check_parent_element(const ElementCollection* parent) const; void check_id_uniqueness(const ID& id) const; void check_single_initial(const ElementCollection* parent) const; + void check_transition_element(const Element* element) const; + void check_transition_action(const Action& action) const; String format; DocumentMetainformation metainfo;