diff --git a/cyberiadamlpp.cpp b/cyberiadamlpp.cpp index f9e1d77..09b126f 100644 --- a/cyberiadamlpp.cpp +++ b/cyberiadamlpp.cpp @@ -1278,6 +1278,34 @@ FinalState* Document::new_final(ElementCollection* _parent, const ID& _id, const return fin; } +ChoicePseudostate* Document::new_choice(ElementCollection* _parent, const Rect& r, const Color& c) +{ + check_parent_element(_parent); + + ChoicePseudostate* choice = new ChoicePseudostate(_parent, generate_vertex_id(_parent), r, c); + _parent->add_element(choice); + return choice; +} + +ChoicePseudostate* Document::new_choice(ElementCollection* _parent, const Name& _name, const Rect& r, const Color& c) +{ + check_parent_element(_parent); + + ChoicePseudostate* choice = new ChoicePseudostate(_parent, generate_vertex_id(_parent), _name, r, c); + _parent->add_element(choice); + return choice; +} + +ChoicePseudostate* Document::new_choice(ElementCollection* _parent, const ID& _id, const Name& _name, const Rect& r, const Color& c) +{ + check_parent_element(_parent); + check_id_uniqueness(_id); + + ChoicePseudostate* choice = new ChoicePseudostate(_parent, generate_vertex_id(_parent), _name, r, c); + _parent->add_element(choice); + return choice; +} + void Document::check_cyberiada_error(int res, const String& msg) const { switch (res) { diff --git a/cyberiadamlpp.h b/cyberiadamlpp.h index df844b0..7c268e1 100644 --- a/cyberiadamlpp.h +++ b/cyberiadamlpp.h @@ -550,6 +550,12 @@ namespace Cyberiada { FinalState* new_final(ElementCollection* parent, const Point& point = Point()); FinalState* new_final(ElementCollection* parent, const Name& name, const Point& point = Point()); FinalState* new_final(ElementCollection* parent, const ID& id, const Name& name, const Point& point = Point()); + ChoicePseudostate* new_choice(ElementCollection* parent, + const Rect& r = Rect(), const Color& color = Color()); + ChoicePseudostate* new_choice(ElementCollection* parent, const Name& name, + 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()); void load(const String& path, DocumentFormat f = formatDetect); void save(const String& path, DocumentFormat f = formatCyberiada10) const; diff --git a/tests/11-choice.cpp b/tests/11-choice.cpp new file mode 100644 index 0000000..3c5b616 --- /dev/null +++ b/tests/11-choice.cpp @@ -0,0 +1,52 @@ +/* ----------------------------------------------------------------------------- + * The Cyberiada GraphML C++ library implemention + * + * The test + * + * Copyright (C) 2024 Alexey Fedoseev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/ + * ----------------------------------------------------------------------------- */ + +#include +#include "cyberiadamlpp.h" +#include "testutils.h" + +using namespace Cyberiada; +using namespace std; + +int main(int argc, char** argv) +{ + Document d; + + StateMachine* sm = d.new_state_machine("SM"); + + d.new_choice(sm); + try { + // check id uniqueness + d.new_choice(sm, "n0", "test"); + } catch (const Cyberiada::ParametersException&){ + } + + State* parent = d.new_state(sm, "State"); + d.new_choice(parent, "Local choice"); + + try { + cout << d << endl; + d.save(string(argv[0]) + ".graphml", formatCyberiada10); + } catch (const Cyberiada::Exception&) { + return 1; + } + return 0; +} diff --git a/tests/11-choice.test-output.graphml b/tests/11-choice.test-output.graphml new file mode 100644 index 0000000..309d758 --- /dev/null +++ b/tests/11-choice.test-output.graphml @@ -0,0 +1,50 @@ + + + Cyberiada-GraphML-1.0 + + + + + + + + + + + + + + + + + + + + + + SM + + formal + CGML_META + standardVersion/ 1.0 + +transitionOrder/ transitionFirst + +eventPropagation/ block + + + + + choice + + + State + + + choice + Local choice + + + + + diff --git a/tests/11-output.txt b/tests/11-output.txt new file mode 100644 index 0000000..d0d04eb --- /dev/null +++ b/tests/11-output.txt @@ -0,0 +1 @@ +Document: {id: '', name: '', format: 'Cyberiada-GraphML-1.0', meta: {standard version: '1.0', transition order: transition first, event propagation: block events}, elements: {State Machine: {id: 'G0', name: 'SM', elements: {Choice: {id: 'n0'}, Composite State: {id: 'n1', name: 'State', elements: {Choice: {id: 'n1::n0', name: 'Local choice'}}}}}}