├── .gitignore ├── LICENCE.txt ├── README.md ├── images ├── g1.png ├── g2.png ├── g3.png ├── g4.png ├── g5.png ├── g6.png ├── g7.png ├── g8.png ├── g9.png └── gettingstarted.png ├── pom.xml └── src ├── main └── java │ └── com │ └── github │ └── klangfarbe │ └── statechart │ ├── Action.java │ ├── ConcurrentState.java │ ├── Condition.java │ ├── Context.java │ ├── Event.java │ ├── EventQueueEntry.java │ ├── FinalState.java │ ├── Guard.java │ ├── HierarchicalState.java │ ├── InternalTransition.java │ ├── Metadata.java │ ├── Parameter.java │ ├── PseudoState.java │ ├── State.java │ ├── StateRuntimedata.java │ ├── Statechart.java │ ├── StatechartException.java │ ├── TimeoutEvent.java │ └── Transition.java └── test └── java └── com └── github └── klangfarbe └── statechart ├── MetadataPersistanceTest.java ├── SemanticTest.java ├── TestAction.java ├── TestCharts.java ├── TestEvent.java ├── TestGuard.java └── TestParameter.java /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/java,intellij,eclipse,maven,sublimetext 3 | # Edit at https://www.gitignore.io/?templates=java,intellij,eclipse,maven,sublimetext 4 | 5 | ### Eclipse ### 6 | 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .settings/ 16 | .loadpath 17 | .recommenders 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # PyDev specific (Python IDE for Eclipse) 26 | *.pydevproject 27 | 28 | # CDT-specific (C/C++ Development Tooling) 29 | .cproject 30 | 31 | # CDT- autotools 32 | .autotools 33 | 34 | # Java annotation processor (APT) 35 | .factorypath 36 | 37 | # PDT-specific (PHP Development Tools) 38 | .buildpath 39 | 40 | # sbteclipse plugin 41 | .target 42 | 43 | # Tern plugin 44 | .tern-project 45 | 46 | # TeXlipse plugin 47 | .texlipse 48 | 49 | # STS (Spring Tool Suite) 50 | .springBeans 51 | 52 | # Code Recommenders 53 | .recommenders/ 54 | 55 | # Annotation Processing 56 | .apt_generated/ 57 | 58 | # Scala IDE specific (Scala & Java development for Eclipse) 59 | .cache-main 60 | .scala_dependencies 61 | .worksheet 62 | 63 | ### Eclipse Patch ### 64 | # Eclipse Core 65 | .project 66 | 67 | # JDT-specific (Eclipse Java Development Tools) 68 | .classpath 69 | 70 | # Annotation Processing 71 | .apt_generated 72 | 73 | .sts4-cache/ 74 | 75 | ### Intellij ### 76 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 77 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 78 | 79 | # User-specific stuff 80 | .idea/**/workspace.xml 81 | .idea/**/tasks.xml 82 | .idea/**/usage.statistics.xml 83 | .idea/**/dictionaries 84 | .idea/**/shelf 85 | 86 | # Generated files 87 | .idea/**/contentModel.xml 88 | 89 | # Sensitive or high-churn files 90 | .idea/**/dataSources/ 91 | .idea/**/dataSources.ids 92 | .idea/**/dataSources.local.xml 93 | .idea/**/sqlDataSources.xml 94 | .idea/**/dynamic.xml 95 | .idea/**/uiDesigner.xml 96 | .idea/**/dbnavigator.xml 97 | 98 | # Gradle 99 | .idea/**/gradle.xml 100 | .idea/**/libraries 101 | 102 | # Gradle and Maven with auto-import 103 | # When using Gradle or Maven with auto-import, you should exclude module files, 104 | # since they will be recreated, and may cause churn. Uncomment if using 105 | # auto-import. 106 | # .idea/modules.xml 107 | # .idea/*.iml 108 | # .idea/modules 109 | 110 | # CMake 111 | cmake-build-*/ 112 | 113 | # Mongo Explorer plugin 114 | .idea/**/mongoSettings.xml 115 | 116 | # File-based project format 117 | *.iws 118 | 119 | # IntelliJ 120 | out/ 121 | 122 | # mpeltonen/sbt-idea plugin 123 | .idea_modules/ 124 | 125 | # JIRA plugin 126 | atlassian-ide-plugin.xml 127 | 128 | # Cursive Clojure plugin 129 | .idea/replstate.xml 130 | 131 | # Crashlytics plugin (for Android Studio and IntelliJ) 132 | com_crashlytics_export_strings.xml 133 | crashlytics.properties 134 | crashlytics-build.properties 135 | fabric.properties 136 | 137 | # Editor-based Rest Client 138 | .idea/httpRequests 139 | 140 | # Android studio 3.1+ serialized cache file 141 | .idea/caches/build_file_checksums.ser 142 | 143 | ### Intellij Patch ### 144 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 145 | 146 | # *.iml 147 | # modules.xml 148 | # .idea/misc.xml 149 | # *.ipr 150 | 151 | # Sonarlint plugin 152 | .idea/sonarlint 153 | 154 | ### Java ### 155 | # Compiled class file 156 | *.class 157 | 158 | # Log file 159 | *.log 160 | 161 | # BlueJ files 162 | *.ctxt 163 | 164 | # Mobile Tools for Java (J2ME) 165 | .mtj.tmp/ 166 | 167 | # Package Files # 168 | *.jar 169 | *.war 170 | *.nar 171 | *.ear 172 | *.zip 173 | *.tar.gz 174 | *.rar 175 | 176 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 177 | hs_err_pid* 178 | 179 | ### Maven ### 180 | target/ 181 | pom.xml.tag 182 | pom.xml.releaseBackup 183 | pom.xml.versionsBackup 184 | pom.xml.next 185 | release.properties 186 | dependency-reduced-pom.xml 187 | buildNumber.properties 188 | .mvn/timing.properties 189 | .mvn/wrapper/maven-wrapper.jar 190 | 191 | ### SublimeText ### 192 | # Cache files for Sublime Text 193 | *.tmlanguage.cache 194 | *.tmPreferences.cache 195 | *.stTheme.cache 196 | 197 | # Workspace files are user-specific 198 | *.sublime-workspace 199 | 200 | # Project files should be checked into the repository, unless a significant 201 | # proportion of contributors will probably not be using Sublime Text 202 | # *.sublime-project 203 | 204 | # SFTP configuration file 205 | sftp-config.json 206 | 207 | # Package control specific files 208 | Package Control.last-run 209 | Package Control.ca-list 210 | Package Control.ca-bundle 211 | Package Control.system-ca-bundle 212 | Package Control.cache/ 213 | Package Control.ca-certs/ 214 | Package Control.merged-ca-bundle 215 | Package Control.user-ca-bundle 216 | oscrypto-ca-bundle.crt 217 | bh_unicode_properties.cache 218 | 219 | # Sublime-github package stores a github token in this file 220 | # https://packagecontrol.io/packages/sublime-github 221 | GitHub.sublime-settings 222 | 223 | # End of https://www.gitignore.io/api/java,intellij,eclipse,maven,sublimetext 224 | -------------------------------------------------------------------------------- /LICENCE.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | 504 | 505 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Table of contents:** 2 | * What is it? 3 | * Introduction to statecharts 4 | * Getting started - an example 5 | * References 6 | 7 | # What is it? 8 | 9 | This framework implements the semantics and syntax of UML statecharts in Java. It gives developers an easy to use API for integration of this diagramm-type into their own code. It is released under the GNU Lesser General Public License. 10 | 11 | ## What are statecharts? 12 | 13 | Statecharts were initially introduced by David Harel in 1987 and are based on a generalisation of the concepts of finite state machines [1]. They are directed graphs and used to describe the behaviour of an object instead of Sequence- or Collaboration-Diagrams which describe the interaction between objects. The Object Management Group added this type of diagram into the UML specification with a slightly modified semantics. The main enhancement of statecharts is the ability to use hierarchy and concurrency in the modelling: If you are new to statecharts you can read a short introduction to the syntax and semantics below. 14 | 15 | ## Why using it? Comparison to alternatives 16 | 17 | The problem with statecharts is that the semantics is quite hard to understand and difficult to implement because several elements are not able to map directly to current programming languages. Therefore the developer is in an awkward position: He can model the object behaviour in his CASE tool and then has the problem that the implementation is hard to realize and error-prone. 18 | 19 | One possibility is the usage of the state-pattern, introduced by Erich Gamma et. al [2]. But there are several disadvantages with this approach: 20 | 21 | * It just supports finite state machines (FSMs) and no hierarchy or concurrency. 22 | * The elements (like e.g. actions and transitions) are not represented as real objects. Instead they are hidden in the classes implementing a state which makes it hard to understand, change and maintain the software. 23 | * The infrastructure of the FSM and the runtime-configuration cannot be separated. Instead many instances of the FSM must occur in the memory if it should be used more than once at the time. 24 | 25 | This project addresses these issues and uses a different approach. Originally the framework was based on my diploma thesis in computer science at the University of Dortmund, Germany, and was created to run statecharts on LEGO Mindstorms [3]. Later it was optimized and is now used in server side 24/7-applications to implement application logic and several protocol layers. 26 | 27 | ## Features 28 | 29 | The framework uses version 1.5 of the UML as a basis for the implementation of the statechart semantics. The list below shows the supported elements and important features: 30 | 31 | * Very easy to use API (See documentation below for details) 32 | * Implemented in Java 33 | * Does require only a very small amount of memory 34 | * Complete object-oriented design (all elements are real objects) 35 | * Parallel usage of the statechart infrastructure. Runtime specific data is encapsulated in a small object. 36 | * Support for many elements of statecharts 37 | * Simple, hierarchical and concurrent states 38 | * Start and final states 39 | * History and deep-history pseudostates in hierarchical states 40 | * Fork- and join pseudostates for concurrent states 41 | * Segmented transitions using junction points 42 | * Transitions can cross borders of composite states (implicit entry/exit) 43 | * Entry, do and exit actions in states 44 | * Events, guards and actions for transitions 45 | * Asynchronous event queues for signal events including a thread-pool 46 | 47 | If you like this software, have any questions, feature requests or find bugs feel free to write me an email. 48 | 49 | # Introduction to statecharts 50 | 51 | If you are new to the statecharts it may be hard to understand the syntax and especially the semantics. This short introduction should help you to get into statecharts and learn how to use them. All elements which are supported by this framework are described here. The original text can be found in [3] (german only). 52 | 53 | ## States and Actions 54 | 55 | Statecharts are represented as directed graphs and therefore have vertices and (directed) edges. The vertices are the states an object can reach. Edges are changes of the state, the so-called _transitions_. States are represented through a rectangle with rounded edges and can have a name and actions. Three kind of state-actions are available: 56 | 57 | * The _entry-action_ is executed when the state is activated. 58 | * The _do-action_ is executed after finishing the entry-action. 59 | * The _exit-action_ is executed when the state is deactivated. 60 | 61 | A special type of state are the _pseudo-states_. They exists only for modelling and are not real states of the object. The statechart **must not** stay or can execute actions in these pseudostates. The main entry activated when the statechart begins the event-handling is the _start-state_ which is a pseudostate and only have outgoing transitions. The _final-state_ instead is a real state and only have incoming transitions. 62 | 63 | ![g1.png](/images/g1.png) 64 | 65 | ## Transitions 66 | 67 | ### Events 68 | 69 | Changing a state normally is triggered with events. An event is the result of an arbitrary action from the system or the environment around the system. Events are not defined in a special way. In the UML specification are four event-types described, the most important ones are: 70 | 71 | * The _signal-event_ is triggered by the surrounding environment. 72 | * The _time-event_ is triggered by the statechart itself. 73 | 74 | Every state can have a completion-transition which is a transition without an event. If the state has no such transition (all transitions are triggered by events) the object stays in this state until an appropriate event ca be handled by an outgoing transition. All events are only handled by the current active state. If the event cannot be handled by the state, the event is discarded. For the end-transition the finishing of the states exit-action is interpreted as the trigger. 75 | 76 | ### Guards and Actions 77 | 78 | A transition can have a guard and an action besides the event. The first is marked as `[guard]` the latter as `/action`. Guards are used to make sure that the transition only can trigger if the evaluation of the guard is true. Therefore it is possible to create transitions with the same event and different guards. Depending on the guard different actions can be executed or target states reached. 79 | 80 | ![g2.png](/images/g2.png) 81 | 82 | While modelling you have to make sure that the model is deterministic. So it is not allowed to add outgoing transitions to the state which can trigger both on an event. 83 | 84 | ### Time-triggered transitions 85 | 86 | The time event is marked at the transition with the keyword after `(x)`. This defines a special kind of transition, the so-called time-triggered transition. `x` is the time the source state must be active before the transition can trigger (if the optional guard evaluation is true). Counting the time begins when an state is activated. 87 | 88 | ![g3.png](/images/g3.png) 89 | 90 | ### Segmented transitions 91 | 92 | Another aspect of statecharts are the segmented transitions. They are used if you want to concatenate several transitions e.g. to execute an action with each subtransition. The other usefull commitment of segmented transitions is the modelling of branches. All subtransitions are concatenated with the junction point pseudostate which is displayed as a small black circle in the diagram. 93 | 94 | One important property of segmented transitions is that they are always atomic. Therefore the transition triggers either complete or not at all. That means before a statechange occurs the segmented transition must check if there exists a permitted path to the next real state. But there is another noteworthiness to mention: Only the first segment of the transition can contain an event as a trigger. Otherwise all segments can contain guards and actions. 95 | 96 | ![g4.png](/images/g4.png) 97 | 98 | ## Hierarchical states 99 | 100 | Complex systems often requires abstraction of an actual situation and specify the concrete behaviour in a separate step later on. In statecharts the usage of hierarchical states, also known as or-state, can be used to decompose a complex state into substates which describe the behaviour in detail. Once the object reaches this composite or-state, exactly one substate is activated automatically and only one substate is active at the time (which explains the name or-state). This interlocking implies new particularities which will be described now. 101 | 102 | ### Start- and final-states 103 | 104 | Inside an or-state it is essential to know which substate to activate when the parent state is activated and when the or-state is finished respectivly. This information is modelled with the already known start- and final-states. The existence of these pseudostates is not mandatory in every case. Why will be cleared in the next paragraph. 105 | 106 | ### Entering and leaving the hierarchical state 107 | 108 | Two types can be described how a complex state may be activated: 109 | 110 | * An incoming transition ends at the border of the or-state. In this case the existence of the start-state is mandatory. 111 | * An incoming transition ends at a substate, it crosses the border of the or-state. Therefore the start-state is not mandatory because the hierarchical state knows the first active substate by the target of the transition. 112 | 113 | Leaving a hierarchical state is analogue: 114 | 115 | * The state can be deactivated through an outgoing (end-)transition. If this transition has no trigger the finishing of the composite state is the signal to trigger the end-transition. In such a case a final-state is mandatory. 116 | * The state can be deactivated through an outgoing transition which source is a substate and target is a state outside the or-state. In such a case a final-state is not mandatory. 117 | 118 | ### State transitions 119 | 120 | Transitions within hierarchies brings up two questions we need a semantics for. At first, what happens with outgoing event-triggered transitions from the or-state? The semantics is that the object is in exactly one substate when the or-state is active. So it is nessecary that every substate can handle this event as well. To realize this semantics every event-triggered outgoing transition of the hierarchical-state is inherited by all substates. In other words, every substate handles all outgoing event-triggered transitions of its parent states which are on the path from the state to the root node of the hierarchy-tree. 121 | 122 | This semantics will cause the next question: What happens if a substate "overrides" an outgoing transition with the same event? In this case more than one transition is able to fire. That means a rule is nessecary to prioritize transitions: `t1` is an outgoing transition of the state `s1` and `s1` is a transitively reachable substate of `s2`. `t2` is an outgoing transition of `s2` and handles the same event as `t1`. In this case the inner transition `t1` has a higher priority as `t2`. This rule assures that always the lowest transition in the hierarchy tree triggers. 123 | 124 | ![g5.png](/images/g5.png) 125 | 126 | ### History states 127 | 128 | Many Systems require to know the last configuration of a complex state when it was deactivated. With this information it is possible to continue in the same configuration when reactivating the complex-state. This information is modelled using history-pseudostates in hierarchical states. History is shown as `H` or `H*` in the diagram and are categorized as follow: 129 | 130 | * The shallow history `H` stores the last active substate. 131 | * The deep history `H*` stores all active substates on the path from the node to the leaf in the hierarchy tree. 132 | 133 | One difference between classic statecharts by David Harel and UML statecharts is that in latter you must specify a start-transition from the history-state to the state which should be activated if no history is available. This is the case when the or-state has never been active before. If a history is available, the stored substate is activated. 134 | 135 | ![g6.png](/images/g6.png) 136 | 137 | ## Concurrent states 138 | 139 | The second improvement of statecharts is the possibility to model concurrency. A concurrent state must have at least two parallel active substates. These are the regions of the and-state. In the diagram these regions are separated with a dashed line. Concurrency implies new conceptual consequences which are described now. 140 | 141 | ### Regions 142 | 143 | Regions are described with hierarchical states, therefore the semantics introduced above applies here as well. Basically all regions are active as soon as the and-state is activated. They are some kind of processes which are running inside the concurrent state. 144 | 145 | It not possible that several regions are "deactivated" while others aren´t. Incoming events are always handled by all regions which means that perhaps more than one transition are able to fire. Namely up to region count transitions. Like in or-states event triggered outgoing transitions are inherited by all regions (and thereby all substates). The priority rule for fireing transitions is applied here as well. 146 | 147 | ![g7.png](/images/g7.png) 148 | 149 | ### Entering and leaving the concurrent state 150 | 151 | When entering a concurrent state all regions must be activated and vice versa. As in hierarchical states two cases can be described. 152 | 153 | * The incoming transition ends at the and-state. In this case all regions must have a start-state. 154 | * The incoming transition ends at a substate of exatly one region. Then the and-state is activated imlicit at this substate. All other regions are activated at there start-states. Therefore n-1 start-states are nessecary. 155 | 156 | Deactivating is analogue: 157 | 158 | * If all regions have activated their end-state the completion-transition can trigger. The end-states are synching the regions. 159 | * If a transition of a substate fires and the target state is outside the and-state, the concurrent state is deactivated implicit. All other regions must be deactivated immediately as well. This semantics applies also for inherited event-triggered transition. 160 | 161 | ![g8.png](/images/g8.png) 162 | 163 | ### Complex transitions 164 | 165 | Two cases of activating a concurrent-state were described above: implicit and explicit. The first all n regions need start-states where latter `n-1` start-states are needed. But what happens when `n-m` regions should be activated (where `1 < m < n`). E.g. this is nessecary if the start-states should describe the default entry behaviour and for some special case the configuration of the and-state should be different when activating. 166 | 167 | To model this behaviour complex transitions were introduced. They split the control flow. One incoming transition is split into maximum one transition per region with a target substate in this region. An analogue case can be described for leaving the and-state. A complex transition can have these two semantics: 168 | 169 | * Split one transition into at least two transitions. All regions which are not allowed for the incoming transitions are activated at their start-states automatically. 170 | * Synchronising different regions for leaving the and-state when some substates are activated. All regions which are not allowed for the outgoing transitions are deactivated automatically. 171 | 172 | The diagram provides two kind of pseudo-states for this behaviour. Both are shown as a small black bar. This bar represents the fork-state if it is a `1:n`-mapping, so one transition is split into n. If it is `n:1`-mapping, that means `n` incoming transitions are synchronised into one. This kind is called the join-state. 173 | 174 | ![g9.png](/images/g9.png) 175 | 176 | # Getting started - an example 177 | 178 | This introduction should help you to understand how to use the framework. Mainly the following steps are necessary: 179 | 180 | * Model the diagram with your favorite CASE-Tool. 181 | * If needed create a derivation of the Metadata-class to store your runtime-specific data. 182 | * Create your implementation of the used actions, guards and events. 183 | * Code your diagram using the framework classes. 184 | * Dispatch the events on the statechart. 185 | 186 | These steps are described in detail in the next sections. As a basis the following statechart is used to create the code for: 187 | 188 | ![gettingstarted.png](/images/gettingstarted.png) 189 | 190 | ## Creating your metadata, actions, events and guards 191 | 192 | After the diagram is modelled we need to recognize the runtime-specific data we are using. In this example we need to store an integer-value which is decremented every time the state `F` is activated. 193 | 194 | All your data is stored in a separate object to make sure the infrastructure of the statechart can be used by more than one thread. This object must be a derivation of the base class Metadata: 195 | 196 | ```java 197 | public class MyMetadata extends Metadata { 198 | public int value = 0; 199 | } 200 | ``` 201 | 202 | **And again the warning:** Make sure you never store data which will be modified in any way in your actions and guards because they are accessed concurrently! 203 | 204 | ### Events 205 | 206 | The next step is to create all events used in the statechart. In this example we have two events `anEvent` and `anotherEvent`. Events are represented via the base class Event. Creating your events is simply done by deriving a class. A default implementation is given so you normaly don´t need to implement something. 207 | 208 | ```java 209 | public class anEvent extends Event {} 210 | public class anotherEvent extends Event {} 211 | ``` 212 | 213 | Every event can store a string as an _id_ and can get with the `toString`-method: 214 | 215 | ```java 216 | public class anEvent extends Event { 217 | public anEvent() { 218 | super("anEvent"); 219 | } 220 | } 221 | ``` 222 | 223 | You may noticed that we did not specify the time event used in the concurrent-state. As you will see later, this is not necessary. 224 | 225 | ### Actions 226 | 227 | Transitions and states will trigger an action by invoking the `execute`-method from the interface. So for each action specified in the statechart we need a class implementing this interface and overwrite the execute-method. In our example we have the actions `SetValue`, `DecrementValue` and `print`. The implementation is pretty simple: 228 | 229 | ```java 230 | public class SetValue extends Action { 231 | private int value; 232 | 233 | public SetValue(int value) { 234 | this.value = value; 235 | } 236 | 237 | public void execute(Metadata data, Parameter parameter) { 238 | data.value = value; 239 | } 240 | } 241 | 242 | public class DecrementValue extends Action { 243 | public void execute(Metadata data, Parameter parameter) { 244 | data.value--; 245 | } 246 | } 247 | 248 | public class Print extends Action { 249 | private string value; 250 | 251 | public Print(string value) { 252 | this.value = value; 253 | } 254 | 255 | public void execute(Metadata data, Parameter parameter) { 256 | System.out.println(value); 257 | } 258 | } 259 | ``` 260 | 261 | When dispatching an event you can use the parameter class (or exactly a derivation of it) to send call-parameter to the action. Another important thing is that an action must never dispatch a synchronous call event on the statechart. This may lead to unspecified behaviour! That means never call the dispatch method from the statechart in the execute method. If you need your action to trigger an event do this using the event queue to dispatch an asynchronous signal-event. In the implementation you will use `statechart.dispatchAsynchron` instead of `statechart.dispatch`. 262 | 263 | ### Guards 264 | 265 | Guards are implemented - I think you guess it - by implementing the interface `Guard`. When using guards you must make sure two things: 266 | 267 | * The guard must never change any data in the metadata object. 268 | * The evaluation must always be a boolean expression. 269 | 270 | Our only guard in the example is a simple check of the integer value stored in the data object: 271 | 272 | ```java 273 | public class ValueEquals extends Guard { 274 | private int value; 275 | 276 | public ValueEquals(int value) { 277 | this.value = value; 278 | } 279 | 280 | public boolean check(Metadata data, Parameter parameter) { 281 | return data.value == value; 282 | } 283 | } 284 | ``` 285 | 286 | The `else`-guard is just a `meta`-guard, that means it is only shown in the diagram but not represented in the statechart implementation. Every transition with an `else`-guard does not have any kind of guard at all in the implementation. 287 | 288 | ## Building the statechart 289 | 290 | After we have created the specific elements we can start building the statechart itself. The main idea is to start with the statechart object and then create all substates in a top-down way. Every state you create will need to know its parent state in the constructor. 291 | 292 | When you allocate the statechart you can specify the number of worker threads for the `event`- and `timeout-event`-queue. The main difference is that the normal event-queue implements a fifo handling and makes sure that only one event per metadata is dispatched with the worker threads at a time while the timeout-event-queue is only internally used for implementing the timeout-event semantics. If a state is activated it will automatically trigger timeout-events for each outgoing transition associated with this kind of event and removes it from the queue when the state is deactivated. The following code represents the states of the diagram: 293 | 294 | ```java 295 | // Create the statechart and top-level states 296 | Statechart chart = new Statechart(); 297 | PseudoState state_start = new PseudoState("start", chart, PseudoState.pseudostate_start); 298 | FinalState state_final = new FinalState("final", chart); 299 | HierarchicalState state_a = new HierarchicalState("a", chart, new SetValue(10), null, null); 300 | 301 | // create substates of the or-state a 302 | State state_b = new State("b", state_a); 303 | PseudoState state_j = new PseudoState("j", state_a, PseudoState.pseudostate_junction); 304 | FinalState state_a_final = new FinalState("a_final", state_a); 305 | ConcurrentState state_c = new ConcurrentState("c", state_a); 306 | 307 | // region 1 of the and-state 308 | HierarchicalState state_c_r1 = new HierarchicalState("c_r1", state_c); 309 | PseudoState state_c_r1_start = new PseudoState("c_r1_start", state_c_r1, PseudoState.pseudostate_start); 310 | State state_d = new State("d", state_c_r1, new Print("c_r1 active"), null, new Print("c_r1 inactive")); 311 | 312 | // region 2 of the and-state 313 | HierarchicalState state_c_r2 = new HierarchicalState("c_r2", state_c, null, null, null); 314 | PseudoState state_c_r2_start = new PseudoState("c_r2_start", state_c_r2, PseudoState.pseudostate_start); 315 | State state_e = new State("e", state_c_r2, new Print("start timeout"), null, null); 316 | State state_f = new State("f", state_c_r2, new DecrementValue(), null, null); 317 | ``` 318 | 319 | If you want you can give every state a name as an identifier. This is strongly recommended because it simplifies the debugging. 320 | 321 | After all states are created we must create the transitions. It is important that you create the transitions after the states because the transition-constructor need the state-hierarchy. 322 | 323 | ```java 324 | // create the transitions 325 | new Transition(state_start, state_b); 326 | new Transition(state_b, state_c); 327 | new Transition(state_c_r1_start, state_d); 328 | new Transition(state_c_r2_start, state_e); 329 | new Transition(state_e, state_f, new TimeoutEvent(1000), new Print("Timeout")); 330 | new Transition(state_f, state_e, new AnotherEvent()); 331 | new Transition(state_c, state_j, new AnEvent()); 332 | new Transition(state_j, state_b, new ValueEquals(0)); 333 | new Transition(state_j, state_a_final); 334 | new Transition(state_a, state_final); 335 | ``` 336 | 337 | That is all you have to do to implement the statechart. The next section will explain how to use it. 338 | 339 | ## Usage 340 | 341 | The created instance of the statechart can now be used for dispatching events. It is possible to use the same statechart instance with more than one metadata object. What we need to do is create a metadata object and then trigger events. You can choose between using the event queue or not. When a new Metadata object is created you must call the start-method before dispatching events. 342 | 343 | ```java 344 | MyMetadata myData = new MyMetadata(); 345 | chart.start(myData); 346 | ``` 347 | 348 | A call of the dispatch- (and the start) method returns only if a state is reached where no outgoing transition can trigger. In our example this is the case with state `E`: You call start and the statechart runs to state `E` for this data. Then a timeout event is created internally and the `start`-method returns. When we now sleep at least 1 second `E` is de- and `F` activated. Then we trigger an event: 349 | 350 | ```java 351 | Thread.sleep(1500); 352 | chart.dispatch(myData, new AnEvent()); 353 | ``` 354 | 355 | This is all we do for dispatching events. If you want to use the event queue just call the asynchronous dispatch and start methods: 356 | 357 | ```java 358 | chart.startAsynchron(myData); 359 | chart.dispatchAsynchron(myData, new AnEvent()); 360 | ``` 361 | 362 | ## Conclusion 363 | 364 | I hope that this short introduction helps you implementing your own statecharts. If you have further questions, find bugs, have feature requests or just want to leave a comment feel free to write me an email or use the forum and tracker at the project page. 365 | 366 | # References 367 | 368 | [1] Harel, David: "Statecharts: A Visual Formalism for Complex Systems" http://www.wisdom.weizmann.ac.il/~dharel/SCANNED.PAPERS/Statecharts.pdf. In: Science of Computer Programming 8 (1987), June, Nr. 3, Pages 231-274 369 | 370 | [2] Gamma, Erich ; Helm, Richard ; Johnson, Ralph ; Vlissides, John: Design Patterns - Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. 371 | 372 | [3] Mocek, Christian: "Erweiterung des CASE-Werkzeugs DAVE um einen Code-Generator für LEGO Mindstorms" ftp://ls10-ftp.cs.uni-dortmund.de/pub/Diplom-Arbeiten/da_Christian_Mocek.pdf. Diploma-Thesis, 2006, Software-Technology, University of Dortmund, Germany. 373 | 374 | -------------------------------------------------------------------------------- /images/g1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g1.png -------------------------------------------------------------------------------- /images/g2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g2.png -------------------------------------------------------------------------------- /images/g3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g3.png -------------------------------------------------------------------------------- /images/g4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g4.png -------------------------------------------------------------------------------- /images/g5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g5.png -------------------------------------------------------------------------------- /images/g6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g6.png -------------------------------------------------------------------------------- /images/g7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g7.png -------------------------------------------------------------------------------- /images/g8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g8.png -------------------------------------------------------------------------------- /images/g9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/g9.png -------------------------------------------------------------------------------- /images/gettingstarted.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/klangfarbe/UML-Statechart-Framework-for-Java/75719ffdd77ad1ee220e258ec958aa0e89113c74/images/gettingstarted.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.github.klangfarbe 5 | statechart 6 | 1.3.0 7 | jar 8 | 9 | 10 | 11 | junit 12 | junit 13 | 4.12 14 | test 15 | 16 | 17 | 18 | 19 | package 20 | 21 | 22 | maven-compiler-plugin 23 | 24 | 1.8 25 | 1.8 26 | true 27 | UTF-8 28 | 29 | 3.8.0 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Action.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Interface for an action which can be called by a transition or state. 24 | */ 25 | public interface Action { 26 | /** 27 | * Called by the state or transition. 28 | * 29 | * @param data The runtime data object 30 | * @param param The parameter for this action 31 | */ 32 | void execute(Metadata data, Parameter param); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/ConcurrentState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.Vector; 23 | 24 | /** 25 | * The implementation of the AND composite state. 26 | */ 27 | public class ConcurrentState extends Context { 28 | // ============================================================================ 29 | // ATTRIBUTES 30 | // ============================================================================ 31 | /** 32 | * The regions of the AND-composition. 33 | */ 34 | private Vector regions = new Vector(); 35 | 36 | // ============================================================================ 37 | // METHODS 38 | // ============================================================================ 39 | 40 | /** 41 | * Creates a simple OR-composite-state without any actions. 42 | * 43 | * @throws StatechartException 44 | */ 45 | public ConcurrentState(String name, Context parent) throws StatechartException { 46 | this(name, parent, null, null, null); 47 | } 48 | 49 | /** 50 | * Creates a simple OR-composite-state with the given actions. 51 | * 52 | * @throws StatechartException 53 | */ 54 | public ConcurrentState(String name, Context parent, Action entryAction, Action doAction, Action exitAction) throws StatechartException { 55 | super(name, parent, entryAction, doAction, exitAction); 56 | } 57 | 58 | // ============================================================================ 59 | 60 | /** 61 | * Adds a new region 62 | * 63 | * @throws StatechartException 64 | */ 65 | void addRegion(HierarchicalState region) { 66 | regions.add(region); 67 | } 68 | 69 | // ============================================================================ 70 | 71 | /** 72 | * Activates the state. 73 | */ 74 | @Override 75 | boolean activate(Metadata data, Parameter parameter) { 76 | if (super.activate(data, parameter)) { 77 | StateRuntimedata statedata = data.getData(this); 78 | for (int i = 0; i < regions.size(); i++) { 79 | // check if the region is activated implicit via a incoming 80 | // transition 81 | if (!statedata.stateset.contains(regions.get(i))) { 82 | HierarchicalState h = regions.get(i); 83 | if (h.activate(data, parameter)) { 84 | h.dispatch(data, null, parameter); 85 | } 86 | } 87 | } 88 | return true; 89 | } 90 | return false; 91 | } 92 | 93 | // ============================================================================ 94 | 95 | /** 96 | * Deactivates the state and informs the substates that they deactivate too. 97 | */ 98 | @Override 99 | void deactivate(Metadata data, Parameter parameter) { 100 | data.getData(this).stateset.clear(); 101 | for (int i = 0; i < regions.size(); i++) { 102 | HierarchicalState h = regions.get(i); 103 | h.deactivate(data, parameter); 104 | } 105 | super.deactivate(data, parameter); 106 | } 107 | 108 | // ============================================================================ 109 | 110 | /** 111 | * Overrides the dispatch method from the state and takes care of delegating 112 | * the incoming event to the current state. 113 | */ 114 | @Override 115 | boolean dispatch(Metadata data, Event event, Parameter parameter) { 116 | // at least one region must dispatch the event 117 | boolean dispatched = false; 118 | 119 | StateRuntimedata statedata = data.getData(this); 120 | 121 | /* 122 | * Dispatch the event in all regions as long as this state is active. If 123 | * we don not check this, an implicit exist would be ignored by this 124 | * code. 125 | */ 126 | for (int i = 0; i < regions.size() && statedata.active; i++) { 127 | HierarchicalState h = regions.get(i); 128 | if (h.dispatch(data, event, parameter)) { 129 | dispatched = true; 130 | } 131 | } 132 | 133 | if (dispatched) { 134 | return true; 135 | } 136 | 137 | /* 138 | * Dispatch the event on this state. but make sure that all regions are 139 | * finished before we can leave this state with the final-transition. 140 | */ 141 | for (int i = 0; i < transitions.size() && statedata.active; i++) { 142 | Transition t = transitions.get(i); 143 | 144 | // If not finished, ignore all transitions without an event 145 | if (t.event == null && !finished(data)) { 146 | continue; 147 | } 148 | 149 | if (t.execute(event, data, parameter)) { 150 | return true; 151 | } 152 | } 153 | return false; 154 | } 155 | 156 | // ============================================================================ 157 | 158 | /** 159 | * Check if all the regions reached their end-states. 160 | */ 161 | private boolean finished(Metadata data) { 162 | for (int i = 0; i < regions.size(); i++) { 163 | HierarchicalState h = regions.get(i); 164 | if (!(data.getData(h).currentState instanceof FinalState)) { 165 | return false; 166 | } 167 | } 168 | return true; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Condition.java: -------------------------------------------------------------------------------- 1 | package com.github.klangfarbe.statechart; 2 | 3 | public class Condition extends PseudoState { 4 | public Condition(String name, Context parent, Guard guard, State whenTrue, State otherwise) throws StatechartException { 5 | super(name, parent, PseudoState.pseudostate_junction); 6 | new Transition(this, whenTrue, guard); 7 | new Transition(this, otherwise); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Context.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Domain of the state. Needed for setting up the hierarchy. This class must 24 | * never be instantiated directly. 25 | */ 26 | public abstract class Context extends State { 27 | // ============================================================================ 28 | // ATTRIBUTES 29 | // ============================================================================ 30 | protected PseudoState startState = null; 31 | 32 | // ============================================================================ 33 | // METHODS 34 | // ============================================================================ 35 | public Context(String name, Context parent, Action entryAction, Action doAction, Action exitAction) throws StatechartException { 36 | super(name, parent, entryAction, doAction, exitAction); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Event.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Interface for an event for the statechart. 24 | */ 25 | public abstract class Event { 26 | // ============================================================================ 27 | // ATTRIBUTES 28 | // ============================================================================ 29 | /** 30 | * The ID of the event 31 | */ 32 | private String id = new String(); 33 | 34 | // ============================================================================ 35 | // METHODS 36 | // ============================================================================ 37 | 38 | /** 39 | * Creates an event 40 | */ 41 | public Event() { 42 | } 43 | 44 | // ============================================================================ 45 | 46 | /** 47 | * Creates an event with a given id 48 | * 49 | * @param id 50 | */ 51 | public Event(String id) { 52 | this.id = id; 53 | } 54 | 55 | // ============================================================================ 56 | 57 | /** 58 | * Called by the transition to check if it should handle this event. 59 | * 60 | * @param event the event to compare 61 | * @param data The runtime data object 62 | * @param param The parameter for this action 63 | */ 64 | public boolean equals(Event event, Metadata data, Parameter param) { 65 | return event != null ? id.compareTo(event.toString()) == 0 : false; 66 | } 67 | 68 | // ============================================================================ 69 | 70 | /** 71 | * Returns the id 72 | */ 73 | @Override 74 | public String toString() { 75 | return id; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/EventQueueEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.concurrent.Delayed; 23 | import java.util.concurrent.TimeUnit; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | /** 27 | * The entry for an asynchronous dispatched event. If necessary the execution of 28 | * the event can be delayed for a specific amount of milliseconds. 29 | * 30 | * @Note This class has a natrual ordering that is inconsistant with equals. 31 | */ 32 | public class EventQueueEntry implements Runnable, Delayed { 33 | // ============================================================================ 34 | // ATTRIBUTES 35 | // ============================================================================ 36 | private static AtomicInteger idCounter = new AtomicInteger(1); 37 | private Statechart statechart = null; 38 | private State state = null; 39 | private Metadata data = null; 40 | private Event event = null; 41 | private Parameter parameter = null; 42 | private long relativeTimeout = 0; 43 | private long absoluteTimeout = 0; 44 | private long added = System.currentTimeMillis(); 45 | private Integer id; 46 | volatile boolean invalid = false; 47 | 48 | // ============================================================================ 49 | // METHODS 50 | // ============================================================================ 51 | public EventQueueEntry(Statechart statechart, State state, Metadata data, Event event, Parameter parameter, long timeout) { 52 | this.statechart = statechart; 53 | this.state = state; 54 | this.data = data; 55 | this.event = event; 56 | this.parameter = parameter; 57 | this.relativeTimeout = timeout; 58 | this.absoluteTimeout = added + relativeTimeout; 59 | this.id = idCounter.getAndIncrement(); 60 | } 61 | 62 | // ============================================================================ 63 | // Inherited by Runnable 64 | // ============================================================================ 65 | @Override 66 | public void run() { 67 | // if the state this event belongs to is not active anymore ignore it 68 | if (!invalid && data.isActive(state)) { 69 | statechart.dispatch(data, event, parameter); 70 | } 71 | statechart = null; 72 | state = null; 73 | data = null; 74 | event = null; 75 | parameter = null; 76 | } 77 | 78 | // ============================================================================ 79 | // Inherited by Delayed 80 | // ============================================================================ 81 | @Override 82 | public long getDelay(TimeUnit sourceUnit) { 83 | long currentTime = System.currentTimeMillis(); 84 | long duration = absoluteTimeout - currentTime; 85 | return duration <= 0 ? 0 : sourceUnit.convert(duration, TimeUnit.MILLISECONDS); 86 | } 87 | 88 | // ============================================================================ 89 | 90 | /* 91 | * FIXME The behaviour of this Method is a bit strange in order to handle a 92 | * bug in JDK 5. If used in JDK 6 or newer this method can be changed so 93 | * that it only compares the absoluteTimeout. 94 | */ 95 | @Override 96 | public int compareTo(Delayed d) { 97 | if (d instanceof EventQueueEntry) { 98 | EventQueueEntry entry = (EventQueueEntry) d; 99 | if (this == entry) { 100 | return 0; 101 | } else if (this.absoluteTimeout < entry.absoluteTimeout) { 102 | return -1; 103 | } else if (this.absoluteTimeout > entry.absoluteTimeout) { 104 | return 1; 105 | } else { 106 | return this.id.compareTo(entry.id); 107 | } 108 | } else { 109 | throw new ClassCastException("Cannot compare an object of " + this.getClass() + " to an object of " + d.getClass()); 110 | } 111 | } 112 | 113 | // ============================================================================ 114 | // Inherited by Object 115 | // ============================================================================ 116 | @Override 117 | public String toString() { 118 | StringBuilder builder = new StringBuilder(); 119 | builder.append("EventQueueEntry [id="); 120 | builder.append(id); 121 | builder.append(", added="); 122 | builder.append(added); 123 | builder.append(", data=("); 124 | builder.append(data); 125 | builder.append("), event="); 126 | builder.append(event); 127 | builder.append(", invalid="); 128 | builder.append(invalid); 129 | builder.append(", parameter="); 130 | builder.append(parameter); 131 | builder.append(", state="); 132 | builder.append(state); 133 | builder.append(", statechart="); 134 | builder.append(statechart); 135 | builder.append(", relativeTimeout="); 136 | builder.append(relativeTimeout); 137 | builder.append(", absoluteTimeout="); 138 | builder.append(absoluteTimeout); 139 | builder.append("]"); 140 | return builder.toString(); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/FinalState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * The final state of a statechart or substate 24 | */ 25 | public class FinalState extends State { 26 | /** 27 | * Creates a new Finalstate. 28 | * 29 | * @throws StatechartException 30 | */ 31 | public FinalState(String name, Context parent) throws StatechartException { 32 | super(name, parent, null, null, null); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Guard.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Interface for a guard which can be called by a transition. 24 | */ 25 | public interface Guard { 26 | /** 27 | * Called by the transition. 28 | * 29 | * @param data The runtime data object 30 | * @param param The parameter for this action 31 | */ 32 | boolean check(Metadata data, Parameter param); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/HierarchicalState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * The implementation of the OR composite state. 24 | */ 25 | public class HierarchicalState extends Context { 26 | // ============================================================================ 27 | // ATTRIBUTES 28 | // ============================================================================ 29 | PseudoState history = null; 30 | 31 | // ============================================================================ 32 | // METHODS 33 | // ============================================================================ 34 | 35 | /** 36 | * Creates a simple OR-composite-state without any actions. 37 | * 38 | * @throws StatechartException 39 | */ 40 | public HierarchicalState(String name, Context parent) throws StatechartException { 41 | this(name, parent, null, null, null); 42 | } 43 | 44 | // ============================================================================ 45 | 46 | /** 47 | * Creates a simple OR-composite-state with the given actions. 48 | * 49 | * @throws StatechartException 50 | */ 51 | public HierarchicalState(String name, Context parent, Action entryAction, Action doAction, Action exitAction) throws StatechartException { 52 | super(name, parent, entryAction, doAction, exitAction); 53 | 54 | if (parent instanceof ConcurrentState) { 55 | ((ConcurrentState) parent).addRegion(this); 56 | } 57 | } 58 | 59 | // ============================================================================ 60 | 61 | /** 62 | * Deactivates the state and informs the substates that they deactivate too. 63 | */ 64 | @Override 65 | void deactivate(Metadata data, Parameter parameter) { 66 | if (!data.isActive(this)) { 67 | return; 68 | } 69 | 70 | StateRuntimedata statedata = data.getData(this); 71 | 72 | if (statedata == null) { 73 | super.deactivate(data, parameter); 74 | return; 75 | } 76 | 77 | // save the history 78 | if (history != null && statedata.currentState != startState && statedata.currentState != history && !(statedata.currentState instanceof FinalState)) { 79 | history.storeHistory(data); 80 | } 81 | 82 | /* 83 | * deactivate substate 84 | */ 85 | if (statedata.currentState != null) { 86 | statedata.currentState.deactivate(data, parameter); 87 | } 88 | 89 | statedata.currentState = null; 90 | 91 | super.deactivate(data, parameter); 92 | } 93 | 94 | // ============================================================================ 95 | 96 | /** 97 | * Overrides the dispatch method from the state and takes care of delegating 98 | * the incoming event to the current state. 99 | */ 100 | @Override 101 | boolean dispatch(Metadata data, Event event, Parameter parameter) { 102 | if (!data.isActive(this)) { 103 | return false; 104 | } 105 | 106 | StateRuntimedata statedata = data.getData(this); 107 | 108 | // Use startstate on activation if available 109 | if (statedata.currentState == null && startState != null) { 110 | data.activate(startState); 111 | statedata.currentState.activate(data, parameter); 112 | } 113 | 114 | // delegate event to substate. 115 | boolean rc = false; 116 | 117 | if (statedata.currentState != null) { 118 | rc = statedata.currentState.dispatch(data, event, parameter); 119 | } 120 | 121 | /* 122 | * If the substate dispatched the event and we reached the finalstate or 123 | * this state is no longar active, trigger a new dispatch for the end 124 | * transition, otherwise return. 125 | */ 126 | if ((rc && !(statedata.currentState instanceof FinalState)) || !data.isActive(this)) { 127 | return rc; 128 | } 129 | 130 | /* 131 | * If no substate can handle the event try to find a transition on this 132 | * state which can. There are 3 possibilities: - The endstate is 133 | * reached: Call the finishing transition - Handle the event with a 134 | * transition from this state - Handle the event with a transition 135 | * inherited from the context 136 | */ 137 | for (int i = 0; i < transitions.size(); i++) { 138 | Transition t = transitions.get(i); 139 | 140 | // Filter all finishing transitions if endstate is not active 141 | if (!(statedata.currentState instanceof FinalState) && !t.hasEvent()) { 142 | continue; 143 | } 144 | 145 | /* 146 | * If the endstate is active and the transition is a finishing 147 | * transition (it has not event), trigger the transition without the 148 | * event. 149 | * 150 | * Otherwise try to trigger the transition with the event. 151 | */ 152 | if (statedata.currentState instanceof FinalState && !t.hasEvent()) { 153 | if (t.execute(null, data, parameter)) { 154 | return true; 155 | } 156 | } 157 | 158 | // try to fire the transision 159 | if (t.execute(event, data, parameter)) { 160 | return true; 161 | } 162 | } 163 | return false; 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/InternalTransition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Describes a transition which does not change the state when triggered, but 24 | * only executes the associated action. This kind of transition must have an 25 | * event as its trigger. In contrast to the normal transition the states entry 26 | * and exit actions are not triggered. 27 | */ 28 | public class InternalTransition extends Transition { 29 | // ============================================================================ 30 | // METHODS 31 | // ============================================================================ 32 | public InternalTransition(State state, Event event, Action action) { 33 | super(state, state, event, action); 34 | deactivate.clear(); 35 | activate.clear(); 36 | } 37 | 38 | // ============================================================================ 39 | 40 | public InternalTransition(State state, Event event, Guard guard, Action action) { 41 | super(state, state, event, guard, action); 42 | deactivate.clear(); 43 | activate.clear(); 44 | } 45 | 46 | // ============================================================================ 47 | 48 | @Override 49 | boolean execute(Event event, Metadata data, Parameter parameter) { 50 | // check if the event can be handled 51 | if (this.event != null && !this.event.equals(event, data, parameter)) { 52 | return false; 53 | } 54 | 55 | if (this.event != null && event == null) { 56 | return false; 57 | } 58 | 59 | if (!allowed(data, parameter)) { 60 | return false; 61 | } 62 | 63 | if (action != null) { 64 | action.execute(data, parameter); 65 | } 66 | return true; 67 | } 68 | 69 | // ============================================================================ 70 | 71 | @Override 72 | boolean allowed(Metadata data, Parameter parameter) { 73 | if (guard != null && !guard.check(data, parameter)) { 74 | return false; 75 | } 76 | return true; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Metadata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.beans.PropertyChangeListener; 23 | import java.beans.PropertyChangeSupport; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | import java.util.Set; 27 | 28 | /** 29 | * Describes runtime specific data of the statechart. The main data is the 30 | * currently active state, or in general all actives when using hierarchy. For 31 | * every active state a StateMetadata-Object is created which stores runtime 32 | * specifiv data for the state (e.g. the time since entering the state). This 33 | * object is allocated only when the state is active, otherwise it is deleted. 34 | */ 35 | public class Metadata { 36 | // ============================================================================ 37 | // ATTRIBUTES 38 | // ============================================================================ 39 | private Statechart statechart; 40 | 41 | /** 42 | * Keymap which holds the StateRuntimedata of a state 43 | */ 44 | private Map activeStates = new HashMap(); 45 | 46 | PropertyChangeSupport pcs = new PropertyChangeSupport(this); 47 | 48 | //============================================================================ 49 | // METHODS 50 | //============================================================================ 51 | 52 | /** 53 | * Creates a Metadata object. 54 | */ 55 | public Metadata() { 56 | } 57 | 58 | //============================================================================ 59 | 60 | /** 61 | * Checks whether the given state is active or not. 62 | */ 63 | public boolean isActive(State state) { 64 | if (activeStates.containsKey(state)) { 65 | return getData(state).active; 66 | } 67 | return false; 68 | } 69 | 70 | // ============================================================================ 71 | 72 | public boolean isActive(String name) throws StatechartException { 73 | return isActive(statechart.getStateByName(name)); 74 | } 75 | 76 | // ============================================================================ 77 | 78 | /** 79 | * Gets the runtime specific data of the state. 80 | * 81 | * @return The data or NULL if the state is not active 82 | */ 83 | public StateRuntimedata getData(State state) { 84 | return activeStates.get(state); 85 | } 86 | 87 | // ============================================================================ 88 | 89 | /** 90 | * Activates a state for this Metadata. If the state is not in the Hashmap it 91 | * will be added and a new StateRuntimeData is created. 92 | */ 93 | void activate(State state) { 94 | if (state instanceof Statechart) { 95 | this.statechart = (Statechart) state; 96 | } 97 | StateRuntimedata data = getData(state); 98 | if (data == null) { 99 | data = new StateRuntimedata(); 100 | activeStates.put(state, data); 101 | } 102 | 103 | data.active = true; 104 | data.currentTime = System.currentTimeMillis(); 105 | data.currentState = null; 106 | 107 | // update the context. if context is null we are at top level 108 | if (state.context != null) { 109 | data = activeStates.get(state.context); 110 | data.currentState = state; 111 | } 112 | 113 | pcs.firePropertyChange("activate", null, state); 114 | } 115 | 116 | // ============================================================================ 117 | 118 | /** 119 | * Deactivates the state and frees the allocated resources. 120 | */ 121 | void deactivate(State state) { 122 | if (activeStates.containsKey(state)) { 123 | StateRuntimedata data = getData(state); 124 | 125 | // If we store the history of a hierarchical state, keep it 126 | if (state instanceof PseudoState 127 | && (((PseudoState) state).type == PseudoState.pseudostate_deep_history 128 | || ((PseudoState) state).type == PseudoState.pseudostate_history)) { 129 | data.active = false; 130 | return; 131 | } 132 | 133 | data.timeoutEvents.clear(); 134 | data.currentState = null; 135 | data = null; 136 | activeStates.remove(state); 137 | pcs.firePropertyChange("deactivate", null, state); 138 | } 139 | } 140 | 141 | // ============================================================================ 142 | 143 | /** 144 | * Gets the runtime data for a state. The difference to the normal getData 145 | * method is, that a new StateRuntimedata is created if it don't exists in the 146 | * hashmap. If the data already exists, it is returned instead. 147 | */ 148 | StateRuntimedata createRuntimedata(State s) { 149 | StateRuntimedata data = activeStates.get(s); 150 | if (data == null) { 151 | data = new StateRuntimedata(); 152 | activeStates.put(s, data); 153 | } 154 | return data; 155 | } 156 | 157 | // ============================================================================ 158 | 159 | /** 160 | * Resets the metadata object for reuse 161 | */ 162 | public void reset() { 163 | activeStates.clear(); 164 | statechart = null; 165 | } 166 | 167 | // ============================================================================ 168 | 169 | public Set getActiveStates() { 170 | return activeStates.keySet(); 171 | } 172 | 173 | // ============================================================================ 174 | 175 | public boolean isRunning() { 176 | boolean isRunning = true; 177 | 178 | Set activeStates = getActiveStates(); 179 | for (State state : activeStates) { 180 | if (state instanceof Statechart) { 181 | continue; 182 | } 183 | if (state instanceof PseudoState) { 184 | continue; 185 | } 186 | isRunning = isRunning && !(state instanceof FinalState); 187 | } 188 | 189 | return isRunning; 190 | } 191 | 192 | // ============================================================================ 193 | 194 | public void addActivateObserver(PropertyChangeListener l) { 195 | pcs.addPropertyChangeListener("activate", l); 196 | } 197 | 198 | // ============================================================================ 199 | 200 | public void addDeactivateObserver(PropertyChangeListener l) { 201 | pcs.addPropertyChangeListener("deactivate", l); 202 | } 203 | 204 | } 205 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Parameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * Baseclass for call parameter. All Parameters must have the ability to store 24 | * the current time of the active state. This value is set by every state 25 | * itself. 26 | */ 27 | public class Parameter { 28 | /** 29 | * Creates a parameter 30 | */ 31 | public Parameter() { 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/PseudoState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.Vector; 23 | 24 | /** 25 | * Implements the Pseudostates. Note that it is not possible to stay in this 26 | * kind of state. 27 | */ 28 | public class PseudoState extends State { 29 | // ============================================================================ 30 | // ATTRIBUTES 31 | // ============================================================================ 32 | public static final int pseudostate_start = 1; 33 | public static final int pseudostate_junction = 2; 34 | public static final int pseudostate_fork = 3; 35 | public static final int pseudostate_join = 4; 36 | public static final int pseudostate_history = 5; 37 | public static final int pseudostate_deep_history = 6; 38 | 39 | /** 40 | * The type of this state 41 | */ 42 | int type; 43 | 44 | /** 45 | * Incoming transitions (needed for join) 46 | */ 47 | Vector incoming = null; 48 | 49 | // ============================================================================ 50 | // METHODS 51 | // ============================================================================ 52 | 53 | /** 54 | * Creates a pseudo state with the given type. 55 | * 56 | * @throws StatechartException 57 | */ 58 | public PseudoState(String name, Context parent, int type) throws StatechartException { 59 | super(name, parent, null, null, null); 60 | this.type = type; 61 | if (type == pseudostate_start) { 62 | if (parent.startState == null) { 63 | parent.startState = this; 64 | } else { 65 | throw new StatechartException("Parent has already a start state!"); 66 | } 67 | } else if (type == pseudostate_history || type == pseudostate_deep_history) { 68 | if (parent instanceof HierarchicalState) { 69 | HierarchicalState h = (HierarchicalState) parent; 70 | if (h.history == null) { 71 | h.history = this; 72 | } else { 73 | throw new StatechartException("Parent has already a history state!"); 74 | } 75 | } else { 76 | throw new StatechartException("Parent is not hierarchical state!"); 77 | } 78 | } 79 | } 80 | 81 | // ============================================================================ 82 | 83 | /** 84 | * Returns the type of this pseudo state. 85 | */ 86 | public int getType() { 87 | return type; 88 | } 89 | 90 | ; 91 | 92 | // ============================================================================ 93 | 94 | /** 95 | * Does a lookup if this pseudo state can be activated. This is true when 96 | * there exists a path to the next real state. 97 | */ 98 | boolean lookup(Metadata data, Parameter parameter) { 99 | // check if all incoming transitions can trigger 100 | if (type == pseudostate_join) { 101 | for (int i = 0; i < incoming.size(); i++) { 102 | Transition t = incoming.get(i); 103 | StateRuntimedata d = data.getData(t.deactivate.get(0)); 104 | if (d == null || d != null && !d.active || t.hasGuard() && !t.guard.check(data, parameter)) { 105 | return false; 106 | } 107 | } 108 | } 109 | 110 | // check if an outgoing transition can trigger 111 | for (int i = 0; i < transitions.size(); i++) { 112 | if (transitions.get(i).allowed(data, parameter)) { 113 | return true; 114 | } 115 | } 116 | return false; 117 | } 118 | 119 | // ============================================================================ 120 | 121 | /** 122 | * Activates the state and executes the next nessecary steps depending on 123 | * the type. Overwrites the method from class State. 124 | */ 125 | @Override 126 | boolean activate(Metadata data, Parameter parameter) { 127 | data.activate(this); 128 | StateRuntimedata d = data.getData(this); 129 | 130 | if (entryAction != null) { 131 | entryAction.execute(data, parameter); 132 | } 133 | 134 | if (type == pseudostate_history || type == pseudostate_deep_history) { 135 | for (int i = 0; i < d.stateset.size(); i++) { 136 | d.stateset.get(i).activate(data, parameter); 137 | } 138 | } else if (type == pseudostate_fork) { 139 | // Exclude regions from automatic activation through the and-state 140 | for (int j = 0; j < transitions.size(); j++) { 141 | Transition t = transitions.get(j); 142 | // Activate the region at it's start state if the guard fails. 143 | if (t.guard != null && !t.guard.check(data, parameter)) { 144 | continue; 145 | } 146 | 147 | for (int i = 0; i < t.activate.size(); i++) { 148 | if (i + 1 < t.activate.size() && t.activate.get(i) instanceof ConcurrentState) { 149 | ConcurrentState s = (ConcurrentState) t.activate.get(i); 150 | StateRuntimedata cd = data.createRuntimedata(s); 151 | 152 | if (!cd.stateset.contains(t.activate.get(i + 1))) { 153 | cd.stateset.add(t.activate.get(i + 1)); 154 | } 155 | } 156 | } 157 | } 158 | } else if (type == pseudostate_join) { 159 | // trigger every action of all incoming transitions 160 | } 161 | 162 | return true; 163 | } 164 | 165 | // ============================================================================ 166 | 167 | /** 168 | * Stores the active states for a history state. 169 | */ 170 | void storeHistory(Metadata data) { 171 | StateRuntimedata d = data.getData(this); 172 | if (d != null) { 173 | d.stateset.clear(); 174 | PseudoState.calculate(d.stateset, data.getData(context).currentState, data, type); 175 | } 176 | } 177 | 178 | // ============================================================================ 179 | 180 | /** 181 | * If a history type is given and the history is stored the event will be 182 | * ignored because on activation the substates are in the context the 183 | * current active state and not the history pseudostate anymore. for other 184 | * states simply call the base method. 185 | */ 186 | @Override 187 | boolean dispatch(Metadata data, Event event, Parameter parameter) { 188 | if (type == pseudostate_history || type == pseudostate_deep_history) { 189 | StateRuntimedata d = data.getData(context); 190 | if (d != null && d.currentState != null && d.currentState != this) { 191 | return d.currentState.dispatch(data, event, parameter); 192 | } 193 | } else if (type == pseudostate_fork) { 194 | // enter the state and execute the transitions 195 | for (int i = 0; i < transitions.size(); i++) { 196 | Transition t = transitions.get(i); 197 | t.execute(event, data, parameter); 198 | } 199 | return true; 200 | } 201 | return super.dispatch(data, event, parameter); 202 | } 203 | 204 | // ============================================================================ 205 | 206 | /** 207 | * Adds an incoming transition to the state. is called by the transition 208 | * constructor. 209 | */ 210 | void addIncomingTransition(Transition t) { 211 | if (incoming == null) { 212 | incoming = new Vector(); 213 | } 214 | incoming.add(t); 215 | } 216 | 217 | // ============================================================================ 218 | 219 | /** 220 | * Does a depth-first-search to get all active nodes in the hierarchy tree. 221 | */ 222 | private static void calculate(Vector history, State s, Metadata data, int type) { 223 | if (s == null) { 224 | return; 225 | } 226 | 227 | history.add(s); 228 | 229 | if (type == pseudostate_history) { 230 | return; 231 | } 232 | 233 | if (s instanceof HierarchicalState) { 234 | StateRuntimedata runtimedata = data.getData(s); 235 | State substate = runtimedata != null ? runtimedata.currentState : null; 236 | if (substate != null) { 237 | calculate(history, substate, data, type); 238 | } 239 | } 240 | /* 241 | * if (s instanceof ConcurrentState) { Vector regions = 242 | * ((ConcurrentState) s).regions; for (int i = 0; i < regions.size(); 243 | * i++) { calculate(history, (State) regions.get(i), data, type); } } 244 | */ 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/State.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.Vector; 23 | 24 | /** 25 | * Represents a simple state of the statechart. For debugging purposes each 26 | * state can have a name assigned for identification. 27 | */ 28 | public class State { 29 | // ============================================================================ 30 | // ATTRIBUTES 31 | // ============================================================================ 32 | // The entry action to execute. 33 | protected Action entryAction = null; 34 | 35 | // The do action to execute. 36 | protected Action doAction = null; 37 | 38 | // The exit action to execute. 39 | protected Action exitAction = null; 40 | 41 | // List of the associated transition objects. 42 | protected Vector transitions = new Vector(); 43 | 44 | // The context of this state. 45 | Context context = null; 46 | 47 | // The statechart this state belongs to. 48 | Statechart statechart = null; 49 | 50 | // The name of the state (for easier debugging). 51 | protected String name = null; 52 | 53 | // ============================================================================ 54 | // METHODS 55 | // ============================================================================ 56 | 57 | /** 58 | * Creates a state without any actions. 59 | * 60 | * @throws StatechartException 61 | */ 62 | public State(String name, Context parent) throws StatechartException { 63 | this(name, parent, null, null, null); 64 | } 65 | 66 | /** 67 | * Creates a state with the given actions. 68 | * 69 | * @throws StatechartException 70 | */ 71 | public State(String name, Context parent, Action entryAction, Action doAction, Action exitAction) throws StatechartException { 72 | if (parent == null && !(this instanceof Statechart)) { 73 | throw new StatechartException("Parameter parent cannot be null"); 74 | } 75 | if (name == null) { 76 | throw new StatechartException("Parameter name cannot be null"); 77 | } 78 | 79 | this.context = parent; 80 | 81 | // get the statechart, we need it for the event queues 82 | if (parent != null) { 83 | while (parent.context != null) { 84 | parent = parent.context; 85 | } 86 | if (parent instanceof Statechart) { 87 | this.statechart = (Statechart) parent; 88 | if (this.statechart.states.containsKey(name)) { 89 | throw new StatechartException("State name <" + name + "> already used! Please define a unique name."); 90 | } 91 | this.statechart.states.put(name, this); 92 | } else { 93 | throw new StatechartException("Cannot determine path to the statechart. Check the hierarchy."); 94 | } 95 | } 96 | 97 | this.name = name; 98 | this.entryAction = entryAction; 99 | this.doAction = doAction; 100 | this.exitAction = exitAction; 101 | } 102 | 103 | // ============================================================================ 104 | 105 | /** 106 | * Gets the context 107 | */ 108 | public Context getContext() { 109 | return context; 110 | } 111 | 112 | // ============================================================================ 113 | 114 | /** 115 | * Sets the entry action. If there is already an action given, it will be 116 | * destroyed first. 117 | */ 118 | public void setEntryAction(Action action) { 119 | this.entryAction = action; 120 | } 121 | 122 | // ============================================================================ 123 | 124 | /** 125 | * Sets the do action. If there is already an action given, it will be 126 | * destroyed first. 127 | */ 128 | public void setDoAction(Action action) { 129 | this.doAction = action; 130 | } 131 | 132 | // ============================================================================ 133 | 134 | /** 135 | * Sets the exit action. If there is already an action given, it will be 136 | * destroyed first. 137 | */ 138 | public void setExitAction(Action action) { 139 | this.exitAction = action; 140 | } 141 | 142 | // ============================================================================ 143 | 144 | /** 145 | * Sets the name of the state used for debugging purposes. 146 | */ 147 | public void setName(String name) { 148 | this.name = name; 149 | } 150 | 151 | // ============================================================================ 152 | 153 | /** 154 | * return the given name 155 | */ 156 | @Override 157 | public String toString() { 158 | return name == null ? "" : name; 159 | } 160 | 161 | // ============================================================================ 162 | 163 | /** 164 | * Adds a transition to the list. 165 | */ 166 | void addTransition(Transition transition) { 167 | // make sure transition with guards are checked first! 168 | if (transition.hasGuard()) { 169 | transitions.add(0, transition); 170 | } else { 171 | transitions.add(transition); 172 | } 173 | } 174 | 175 | // ============================================================================ 176 | 177 | /** 178 | * Activates the state. 179 | */ 180 | boolean activate(Metadata data, Parameter parameter) { 181 | if (!data.isActive(this)) { 182 | data.activate(this); 183 | 184 | // trigger the timout events if available 185 | for (int i = 0; i < transitions.size(); i++) { 186 | Transition t = transitions.get(i); 187 | if (t.event != null && t.event instanceof TimeoutEvent) { 188 | TimeoutEvent event = (TimeoutEvent) t.event; 189 | EventQueueEntry entry = new EventQueueEntry(statechart, this, data, event, parameter, event.getTimout()); 190 | StateRuntimedata runtimedata = data.getData(this); 191 | runtimedata.timeoutEvents.add(entry); 192 | statechart.timeoutEventQueue.add(entry); 193 | } 194 | } 195 | 196 | if (entryAction != null) { 197 | entryAction.execute(data, parameter); 198 | } 199 | 200 | if (doAction != null) { 201 | doAction.execute(data, parameter); 202 | } 203 | return true; 204 | } 205 | return false; 206 | } 207 | 208 | // ============================================================================ 209 | 210 | /** 211 | * Deactivates the state. 212 | */ 213 | void deactivate(Metadata data, Parameter parameter) { 214 | if (data.isActive(this)) { 215 | // Mark events as invalid 216 | Vector timeoutEvents = data.getData(this).timeoutEvents; 217 | if (timeoutEvents != null) { 218 | for (EventQueueEntry event : timeoutEvents) { 219 | event.invalid = true; 220 | /* 221 | * @FIXME 222 | * 223 | * @See Java-Bug ID: 6268068. Proposed fix is to use: 224 | * removeAll(Collections.singletonList(event)). 225 | * 226 | * Problem: Leads to ConcurrentModificationException. Final 227 | * fix is in JDK6 228 | */ 229 | statechart.timeoutEventQueue.remove(event); 230 | } 231 | } 232 | data.deactivate(this); 233 | if (exitAction != null) { 234 | exitAction.execute(data, parameter); 235 | } 236 | } 237 | } 238 | 239 | // ============================================================================ 240 | 241 | /** 242 | * Dispatches the given event. 243 | */ 244 | boolean dispatch(Metadata data, Event event, Parameter parameter) { 245 | for (int i = 0; i < transitions.size(); i++) { 246 | if (transitions.get(i).execute(event, data, parameter)) { 247 | return true; 248 | } 249 | } 250 | return false; 251 | } 252 | } 253 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/StateRuntimedata.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.Vector; 23 | 24 | /** 25 | * Holds the runtime specific data for a state in the statechart. 26 | */ 27 | public class StateRuntimedata { 28 | // ============================================================================ 29 | // ATTRIBUTES 30 | // ============================================================================ 31 | /** 32 | * The current time the state is activated 33 | */ 34 | public long currentTime = 0; 35 | 36 | /** 37 | * If the state is active 38 | */ 39 | public boolean active = false; 40 | 41 | /** 42 | * The current activated substate 43 | */ 44 | public State currentState = null; 45 | 46 | /** 47 | * A generic set of states, needed e.g. for history storage or region 48 | * activation 49 | */ 50 | public Vector stateset = new Vector(); 51 | 52 | /** 53 | * A set of all events which are currently in the timeoutQueue 54 | */ 55 | public Vector timeoutEvents = new Vector(); 56 | 57 | // ============================================================================ 58 | // Methods 59 | // ============================================================================ 60 | @Override 61 | public String toString() { 62 | StringBuilder builder = new StringBuilder(); 63 | builder.append("StateRuntimedata [active="); 64 | builder.append(active); 65 | builder.append(", currentState="); 66 | builder.append(currentState); 67 | builder.append(", currentTime="); 68 | builder.append(currentTime); 69 | builder.append(", stateset="); 70 | builder.append(stateset); 71 | builder.append(", timeoutEvents="); 72 | builder.append(timeoutEvents); 73 | builder.append("]"); 74 | return builder.toString(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Statechart.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.HashMap; 23 | import java.util.Vector; 24 | import java.util.concurrent.*; 25 | 26 | /** 27 | * The main entry point for using the statechart framework. Contains all 28 | * necessary methods for delegating incoming events to the substates. When 29 | * deleting the statechart all substates, actions, events, guards and transition 30 | * will be deleted automatically. 31 | */ 32 | public class Statechart extends Context implements Runnable { 33 | /** 34 | * Creates the threads for the ThreadPoolExecutor. These threads may be 35 | * daemon or non-daemon threads. 36 | */ 37 | class StatechartThreadFactory implements ThreadFactory { 38 | // ======================================================================== 39 | // ATTRIBUTES 40 | // ======================================================================== 41 | private String id; 42 | private int threadCount = 0; 43 | private boolean makeDaemonThreads = false; 44 | private ThreadGroup threadGroup = null; 45 | 46 | // ======================================================================== 47 | // METHODS 48 | // ======================================================================== 49 | public StatechartThreadFactory(String id, boolean makeDaemonThreads) { 50 | this.makeDaemonThreads = makeDaemonThreads; 51 | this.id = id; 52 | threadGroup = new ThreadGroup(id); 53 | } 54 | 55 | // ======================================================================== 56 | 57 | @Override 58 | public Thread newThread(Runnable r) { 59 | String name = null; 60 | synchronized (this) { 61 | ++threadCount; 62 | name = id + "-thread-" + threadCount; 63 | } 64 | Thread t = new Thread(threadGroup, r, name); 65 | t.setDaemon(makeDaemonThreads); 66 | return t; 67 | } 68 | } 69 | 70 | // ============================================================================ 71 | // ATTRIBUTES 72 | // ============================================================================ 73 | public static final String VERSION = "1.2.0"; 74 | private ExecutorService threadpool = null; 75 | DelayQueue timeoutEventQueue = new DelayQueue(); 76 | HashMap states = new HashMap(); 77 | 78 | // ============================================================================ 79 | // METHODS 80 | // ============================================================================ 81 | 82 | /** 83 | * Creates the Statechart with the given default and maximum number of 84 | * threads. 85 | * 86 | * @param name The name of the statechart. This must be unique for all 87 | * statecharts in the running JVM. 88 | * @param threads The maximum number of threads available in the threadpool. 89 | * @param makeDaemonThreads Specifies if the created threads should be daemon or 90 | * non-daemon threads. 91 | * @throws StatechartException 92 | */ 93 | public Statechart(String name, int threads, boolean makeDaemonThreads) throws StatechartException { 94 | super(name, null, null, null, null); 95 | // we need at least two threads for asynchronous and timeout events 96 | if (threads < 2) { 97 | threads = 2; 98 | } 99 | threadpool = Executors.newFixedThreadPool(threads, new StatechartThreadFactory(name, makeDaemonThreads)); 100 | threadpool.execute(this); 101 | } 102 | 103 | // ============================================================================ 104 | 105 | /** 106 | * Shutdown of the threadpool. The pool waits 60 seconds at most before 107 | * shutting down hard. 108 | * 109 | * @throws InterruptedException 110 | */ 111 | public synchronized void shutdown() { 112 | threadpool.shutdown(); 113 | timeoutEventQueue.clear(); 114 | // send an empty event to the queue to get back from the delay queues 115 | // take method 116 | timeoutEventQueue.add(new EventQueueEntry(this, this, null, null, null, 0)); 117 | try { 118 | threadpool.awaitTermination(60, TimeUnit.SECONDS); 119 | } catch (InterruptedException e) { 120 | // ignore and just exit the method 121 | } 122 | } 123 | 124 | // ============================================================================ 125 | 126 | /** 127 | * Initializes the Statechart in the runtime data. Sets the start state. 128 | */ 129 | public boolean start(Metadata data) { 130 | Parameter parameter = new Parameter(); 131 | return start(data, parameter); 132 | } 133 | 134 | // ============================================================================ 135 | 136 | /** 137 | * Initializes the Statechart in the runtime data. Sets the start state. 138 | */ 139 | public boolean start(Metadata data, Parameter parameter) { 140 | data.reset(); 141 | data.activate(this); 142 | data.activate(startState); 143 | return dispatch(data, null, parameter); 144 | } 145 | 146 | // ============================================================================ 147 | 148 | /** 149 | * Sets a new data object to continue from the given state. This is usually 150 | * needed for persistance to restore the state after a restart of the JVM or 151 | * if the data object was deleted sometime ago. 152 | */ 153 | public boolean restoreState(State state, Metadata data, Parameter parameter) { 154 | if (data.isActive(this)) { 155 | return false; 156 | } 157 | data.reset(); 158 | // get the path from the state to the root 159 | Vector path = new Vector(); 160 | State parent = state; 161 | do { 162 | path.add(0, parent); 163 | parent = parent.context; 164 | } while (parent != null); 165 | 166 | for (State s : path) { 167 | s.activate(data, parameter); 168 | } 169 | return true; 170 | } 171 | 172 | // ============================================================================ 173 | 174 | /** 175 | * Calls the dispatch method on the current state. 176 | */ 177 | public boolean dispatch(Metadata data, Event event) { 178 | Parameter parameter = new Parameter(); 179 | return dispatch(data, event, parameter); 180 | } 181 | 182 | // ============================================================================ 183 | 184 | /** 185 | * Overrides the dispatch method from the state and takes care of delegating 186 | * the incoming event to the current state. 187 | */ 188 | @Override 189 | public boolean dispatch(Metadata data, Event event, Parameter parameter) { 190 | boolean rc = false; 191 | synchronized (data) { 192 | State currentState = data.getData(this).currentState; 193 | rc = currentState.dispatch(data, event, parameter); 194 | 195 | // call dispatch as long as we hit states with end transitions 196 | do { 197 | currentState = data.getData(this).currentState; 198 | } while (currentState != null && currentState.dispatch(data, null, parameter)); 199 | } 200 | return rc; 201 | } 202 | 203 | // ============================================================================ 204 | 205 | /** 206 | * Initializes the Statechart in the runtime data. Sets the start state and 207 | * triggers an initial asynchronous dispatch. 208 | */ 209 | public void startAsynchron(Metadata data) { 210 | Parameter parameter = new Parameter(); 211 | startAsynchron(data, parameter); 212 | } 213 | 214 | // ============================================================================ 215 | 216 | /** 217 | * Initializes the Statechart in the runtime data. Sets the start state and 218 | * triggers an initial asynchronous dispatch. 219 | */ 220 | public void startAsynchron(Metadata data, Parameter parameter) { 221 | data.reset(); 222 | data.activate(this); 223 | data.activate(startState); 224 | dispatchAsynchron(data, null, parameter); 225 | } 226 | 227 | // ============================================================================ 228 | 229 | /** 230 | * Adds an event to the event queue. 231 | */ 232 | public void dispatchAsynchron(Metadata data, Event event) { 233 | Parameter parameter = new Parameter(); 234 | dispatchAsynchron(data, event, parameter); 235 | } 236 | 237 | // ============================================================================ 238 | 239 | /** 240 | * Adds an event to the event queue. 241 | */ 242 | public void dispatchAsynchron(Metadata data, Event event, Parameter parameter) { 243 | if (!threadpool.isShutdown()) { 244 | if (event instanceof TimeoutEvent) { 245 | timeoutEventQueue.add(new EventQueueEntry(this, this, data, event, parameter, ((TimeoutEvent) event).getTimout())); 246 | } else { 247 | threadpool.execute(new EventQueueEntry(this, this, data, event, parameter, 0)); 248 | } 249 | } 250 | } 251 | 252 | // ============================================================================ 253 | 254 | /** 255 | * Since every state must have a unique name, it is possible to get the 256 | * state object by name. 257 | * 258 | * @throws StatechartException 259 | */ 260 | public final State getStateByName(String stateName) throws StatechartException { 261 | State state = states.get(stateName); 262 | if (state == null) { 263 | throw new StatechartException("Could not find state <" + stateName + ">."); 264 | } 265 | return state; 266 | } 267 | 268 | // ============================================================================ 269 | // Inherited by Runnable 270 | // ============================================================================ 271 | 272 | /** 273 | * Dequeues elements from the timeout queue and dispatches them 274 | */ 275 | @Override 276 | public void run() { 277 | while (!threadpool.isShutdown()) { 278 | try { 279 | EventQueueEntry event = timeoutEventQueue.take(); 280 | threadpool.execute(event); 281 | } catch (InterruptedException e) { 282 | // ignore the exception. Just run the next loop and take method 283 | // if 284 | // necessary 285 | } catch (RejectedExecutionException e) { 286 | // Normally this means that the threadpool has been shutted down 287 | } 288 | } 289 | } 290 | } 291 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/StatechartException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | /** 23 | * This class describes some common exception which can occur when working with 24 | * the statechart. 25 | */ 26 | public class StatechartException extends Exception { 27 | // ============================================================================ 28 | // ATTRIBUTES 29 | // ============================================================================ 30 | private static final long serialVersionUID = 1L; 31 | 32 | // ============================================================================ 33 | // METHODS 34 | // ============================================================================ 35 | public StatechartException() { 36 | super(); 37 | } 38 | 39 | // ============================================================================ 40 | 41 | public StatechartException(String reason) { 42 | super(reason); 43 | } 44 | 45 | // ============================================================================ 46 | 47 | public StatechartException(String reason, Throwable cause) { 48 | super(reason, cause); 49 | } 50 | 51 | // ============================================================================ 52 | 53 | public StatechartException(Throwable cause) { 54 | super(cause); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/TimeoutEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.UUID; 23 | 24 | /** 25 | * Timeout event for a transition 26 | */ 27 | public class TimeoutEvent extends Event { 28 | // ============================================================================ 29 | // ATTRIBUTES 30 | // ============================================================================ 31 | /** 32 | * The timeout value in milliseconds 33 | */ 34 | private long timeout; 35 | private UUID uuid = UUID.randomUUID(); 36 | 37 | // ============================================================================ 38 | // METHODS 39 | // ============================================================================ 40 | 41 | /** 42 | * Creates a timeout event. 43 | * 44 | * @param timeout The timeout value in milliseconds 45 | */ 46 | public TimeoutEvent(long timeout) { 47 | super("TimeoutEvent"); 48 | this.timeout = timeout; 49 | } 50 | 51 | // ============================================================================ 52 | 53 | /** 54 | * Checks if the timevalue in the given parameter is larger than the one 55 | * stored in this event. 56 | * 57 | * @param event the event to compare 58 | * @param data The runtime data object 59 | * @param param The parameter for this event. Must be a TimeoutParameter type. 60 | */ 61 | @Override 62 | public boolean equals(Event event, Metadata data, Parameter param) { 63 | if (event instanceof TimeoutEvent && ((TimeoutEvent) event).uuid == this.uuid) { 64 | return true; 65 | } 66 | return false; 67 | } 68 | 69 | // ============================================================================ 70 | 71 | /** 72 | * Gets the timeout of the event. 73 | */ 74 | public long getTimout() { 75 | return timeout; 76 | } 77 | 78 | // ============================================================================ 79 | 80 | /** 81 | * Sets the current timeout 82 | */ 83 | public void setTimeout(long timeout) { 84 | this.timeout = timeout; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/github/klangfarbe/statechart/Transition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import java.util.Vector; 23 | 24 | /** 25 | * Describes a Transition between states. 26 | */ 27 | public class Transition { 28 | // ============================================================================ 29 | // ATTRIBUTES 30 | // ============================================================================ 31 | // The triggering event or 0 if no event is used. 32 | Event event = null; 33 | 34 | // The guard watching if the transition can trigger. 35 | Guard guard = null; 36 | 37 | // The action to execute when the transition triggers. 38 | Action action = null; 39 | 40 | // List of all states which must be deactivated when triggering. 41 | Vector deactivate = new Vector(); 42 | 43 | // List of all states which must be activated when triggering. 44 | Vector activate = new Vector(); 45 | 46 | // ============================================================================ 47 | // METHODS 48 | // ============================================================================ 49 | public Transition(State start, State end) { 50 | init(start, end, null, null, null); 51 | } 52 | 53 | // ============================================================================ 54 | 55 | public Transition(State start, State end, Event event) { 56 | init(start, end, event, null, null); 57 | } 58 | 59 | // ============================================================================ 60 | 61 | public Transition(State start, State end, Guard guard) { 62 | init(start, end, null, guard, null); 63 | } 64 | 65 | // ============================================================================ 66 | 67 | public Transition(State start, State end, Action action) { 68 | init(start, end, null, null, action); 69 | } 70 | 71 | // ============================================================================ 72 | 73 | public Transition(State start, State end, Event event, Guard guard) { 74 | init(start, end, event, guard, null); 75 | } 76 | 77 | // ============================================================================ 78 | 79 | public Transition(State start, State end, Event event, Action action) { 80 | init(start, end, event, null, action); 81 | } 82 | 83 | // ============================================================================ 84 | 85 | public Transition(State start, State end, Guard guard, Action action) { 86 | init(start, end, null, guard, action); 87 | } 88 | 89 | // ============================================================================ 90 | 91 | public Transition(State start, State end, Event event, Guard guard, Action action) { 92 | init(start, end, event, guard, action); 93 | } 94 | 95 | // ============================================================================ 96 | 97 | /** 98 | * Checks wether an event is associated with this transition or not. 99 | */ 100 | boolean hasEvent() { 101 | return event != null ? true : false; 102 | } 103 | 104 | // ============================================================================ 105 | 106 | /** 107 | * Checks wether a guard is associated with this transition or not. 108 | */ 109 | boolean hasGuard() { 110 | return guard != null ? true : false; 111 | } 112 | 113 | // ============================================================================ 114 | 115 | /** 116 | * Checks wether an action is associated with this transition or not. 117 | */ 118 | boolean hasAction() { 119 | return action != null ? true : false; 120 | } 121 | 122 | // ============================================================================ 123 | 124 | /** 125 | * Executes the transition and triggers the new step. 126 | */ 127 | boolean execute(Event event, Metadata data, Parameter parameter) { 128 | // check if the event can be handled 129 | if (this.event != null && !this.event.equals(event, data, parameter)) { 130 | return false; 131 | } 132 | 133 | if (this.event != null && event == null) { 134 | return false; 135 | } 136 | 137 | if (!allowed(data, parameter)) { 138 | return false; 139 | } 140 | 141 | // deactivate all states 142 | for (int i = 0; i < deactivate.size(); i++) { 143 | deactivate.get(i).deactivate(data, parameter); 144 | } 145 | 146 | // Execute exit-action 147 | if (action != null) { 148 | action.execute(data, parameter); 149 | } 150 | 151 | // Activate all new states. 152 | for (int i = 0; i < activate.size(); i++) { 153 | /* 154 | * check if we activate an concurrent state imlicit and if so make 155 | * sure adding the correct region to the list of regions to ignore 156 | * on activation. It is activated by this transition. 157 | */ 158 | if (i + 1 < activate.size() && activate.get(i) instanceof ConcurrentState) { 159 | ConcurrentState s = (ConcurrentState) activate.get(i); 160 | StateRuntimedata cd = data.createRuntimedata(s); 161 | 162 | if (!cd.stateset.contains(activate.get(i + 1))) { 163 | cd.stateset.add(activate.get(i + 1)); 164 | } 165 | } 166 | activate.get(i).activate(data, parameter); 167 | } 168 | return true; 169 | } 170 | 171 | // ============================================================================ 172 | 173 | /** 174 | * Checks if all constraints are fullfilled. To do this the whole path up to 175 | * the next real state is checked. 176 | */ 177 | boolean allowed(Metadata data, Parameter parameter) { 178 | if (guard != null && !guard.check(data, parameter)) { 179 | return false; 180 | } 181 | 182 | /* 183 | * if target is a pseudostate, call lookup to check if we do not stay in 184 | * this state. So get the last state in the activate list. end() is 185 | * behind the last element so we have to select the prior element by 186 | * using *--. 187 | */ 188 | State target = activate.lastElement(); 189 | if (target instanceof PseudoState) { 190 | return ((PseudoState) target).lookup(data, parameter); 191 | } 192 | 193 | return true; 194 | } 195 | 196 | // ============================================================================ 197 | 198 | /** 199 | * Initializes the Transition 200 | */ 201 | private void init(State start, State end, Event event, Guard guard, Action action) { 202 | this.event = event; 203 | this.guard = guard; 204 | this.action = action; 205 | Transition.calculateStateSet(start, end, deactivate, activate); 206 | start.addTransition(this); 207 | 208 | // for handling join states correctly, we need to know the incoming 209 | // transition 210 | if (end instanceof PseudoState && ((PseudoState) end).getType() == PseudoState.pseudostate_join) { 211 | ((PseudoState) end).addIncomingTransition(this); 212 | } 213 | } 214 | 215 | // ============================================================================ 216 | 217 | /** 218 | * Calculates all the states which must be deactivated and then activated 219 | * when triggering the transition. 220 | */ 221 | private static void calculateStateSet(State start, State end, Vector deactivate, Vector activate) { 222 | // temp vectors for calculating the LCA (least common ancestor) 223 | Vector a = new Vector(); 224 | Vector d = new Vector(); 225 | 226 | // get all states for possible deactivation 227 | State s = start; 228 | while (s != null) { 229 | d.add(0, s); 230 | Context context = s.context; 231 | 232 | // If context is hierarchical or concurrent state, get it as parent. 233 | if (context != null && !(context instanceof Statechart)) { 234 | s = context; 235 | } else { 236 | s = null; 237 | } 238 | } 239 | 240 | // get all states for possible activation 241 | State e = end; 242 | while (e != null) { 243 | a.add(0, e); 244 | Context context = e.context; 245 | 246 | // If context is hierarchical or concurrent state, get it as parent. 247 | if (context != null && !(context instanceof Statechart)) { 248 | e = context; 249 | } else { 250 | e = null; 251 | } 252 | } 253 | 254 | /* 255 | * Get LCA number. It is min-1 by default. Therefore we make sure that 256 | * if start equals end, we do not get the whole path up to the root node 257 | * if the state is a substate. 258 | */ 259 | int min = a.size() < d.size() ? a.size() : d.size(); 260 | int lca = min - 1; 261 | 262 | // get the LCA-State 263 | if (start != end) { 264 | // if the first entry is not equal we got the LCA 265 | for (lca = 0; lca < min; lca++) { 266 | if (a.get(lca) != d.get(lca)) { 267 | break; 268 | } 269 | } 270 | } 271 | 272 | // Fill the given vectors for the transition 273 | for (int j = lca; j < d.size(); j++) { 274 | deactivate.add(0, d.get(j)); 275 | } 276 | 277 | for (int j = lca; j < a.size(); j++) { 278 | activate.add(a.get(j)); 279 | } 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/MetadataPersistanceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import org.junit.Assert; 23 | import org.junit.Test; 24 | 25 | public class MetadataPersistanceTest { 26 | 27 | // ============================================================================ 28 | // Test persistance capability of the Metadata object 29 | // ============================================================================ 30 | @Test 31 | public void testRestoreState() throws StatechartException { 32 | Statechart chart = TestCharts.h5(); 33 | State s3 = chart.getStateByName("c"); 34 | State h1 = chart.getStateByName("p"); 35 | State h2 = chart.getStateByName("q"); 36 | 37 | TestParameter parameter = new TestParameter(); 38 | Metadata data = new Metadata(); 39 | 40 | chart.restoreState(s3, data, parameter); 41 | Assert.assertTrue(data.isActive(s3)); 42 | Assert.assertTrue(data.isActive(h2)); 43 | Assert.assertTrue(data.isActive(h1)); 44 | Assert.assertTrue(data.isActive(chart)); 45 | 46 | String result = "A:p A:q A:c"; 47 | Assert.assertEquals(result, parameter.path); 48 | 49 | // check that the restore only works one time 50 | Assert.assertEquals(false, chart.restoreState(s3, data, parameter)); 51 | 52 | chart.shutdown(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/SemanticTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | import org.junit.Assert; 23 | import org.junit.Ignore; 24 | import org.junit.Test; 25 | 26 | public class SemanticTest { 27 | 28 | void waitForFinalState(Statechart chart, Metadata data) { 29 | // Wait until the statechart reached its final state 30 | State current = null; 31 | while (current == null || !(current instanceof FinalState)) { 32 | try { 33 | Thread.sleep(100); 34 | } catch (InterruptedException e) { 35 | // Ignore this... 36 | } 37 | synchronized (data) { 38 | current = data.getData(chart).currentState; 39 | } 40 | } 41 | } 42 | 43 | @Test 44 | public void testEventQueue() throws StatechartException, InterruptedException { 45 | Statechart chart = TestCharts.t2(); 46 | 47 | TestEvent s1 = new TestEvent(1); 48 | TestEvent s2 = new TestEvent(2); 49 | TestParameter parameter = new TestParameter(); 50 | Metadata data = new Metadata(); 51 | 52 | chart.startAsynchron(data, parameter); 53 | chart.dispatchAsynchron(data, s1, parameter); 54 | chart.dispatchAsynchron(data, s2, parameter); 55 | waitForFinalState(chart, data); 56 | chart.shutdown(); 57 | Assert.assertEquals("D:start A:a D:a A:a D:a A:end", parameter.path); 58 | } 59 | 60 | @Test 61 | public void testSemantics1() throws StatechartException { 62 | Statechart chart = TestCharts.t1(); 63 | 64 | TestParameter parameter = new TestParameter(); 65 | Metadata data = new Metadata(); 66 | 67 | Assert.assertTrue(chart.start(data, parameter)); 68 | Assert.assertEquals("D:start A:a D:a A:b D:b A:end", parameter.path); 69 | chart.shutdown(); 70 | } 71 | 72 | @Test 73 | public void testSemantics2() throws StatechartException { 74 | Statechart chart = TestCharts.t2(); 75 | 76 | TestEvent s1 = new TestEvent(1); 77 | TestEvent s2 = new TestEvent(2); 78 | TestParameter parameter = new TestParameter(); 79 | Metadata data = new Metadata(); 80 | 81 | Assert.assertTrue(chart.start(data, parameter)); 82 | Assert.assertTrue(chart.dispatch(data, s1, parameter)); 83 | Assert.assertTrue(chart.dispatch(data, s2, parameter)); 84 | Assert.assertEquals("D:start A:a D:a A:a D:a A:end", parameter.path); 85 | 86 | // check if more than one signals create a longer way 87 | parameter.path = ""; 88 | 89 | Assert.assertTrue(chart.start(data, parameter)); 90 | Assert.assertTrue(chart.dispatch(data, s1, parameter)); 91 | Assert.assertTrue(chart.dispatch(data, s1, parameter)); 92 | Assert.assertTrue(chart.dispatch(data, s2, parameter)); 93 | Assert.assertEquals("D:start A:a D:a A:a D:a A:a D:a A:end", parameter.path); 94 | chart.shutdown(); 95 | } 96 | 97 | @Test 98 | public void testSemantics3() throws StatechartException { 99 | Statechart chart = TestCharts.t3(); 100 | 101 | TestEvent s1 = new TestEvent(1); 102 | TestParameter parameter = new TestParameter(); 103 | Metadata data = new Metadata(); 104 | 105 | Assert.assertTrue(chart.start(data, parameter)); 106 | Assert.assertTrue(chart.dispatch(data, s1, parameter)); 107 | Assert.assertEquals("D:start A:a D:a A:b D:b A:end", parameter.path); 108 | chart.shutdown(); 109 | } 110 | 111 | @Test 112 | public void testSemantics4() throws StatechartException, InterruptedException { 113 | Statechart chart = TestCharts.t3(); 114 | 115 | TestParameter parameter = new TestParameter(); 116 | Metadata data = new Metadata(); 117 | 118 | Assert.assertTrue(chart.start(data, parameter)); 119 | waitForFinalState(chart, data); 120 | Assert.assertEquals("D:start A:a D:a A:end", parameter.path); 121 | chart.shutdown(); 122 | } 123 | 124 | @Test 125 | public void testSemantics5() throws StatechartException { 126 | Statechart chart = TestCharts.t4(); 127 | 128 | TestParameter parameter = new TestParameter(); 129 | parameter.guardvalue = 0; 130 | Metadata data = new Metadata(); 131 | 132 | Assert.assertTrue(chart.start(data, parameter)); 133 | Assert.assertEquals("D:start A:a D:a A:end", parameter.path); 134 | chart.shutdown(); 135 | } 136 | 137 | @Test 138 | public void testSemantics6() throws StatechartException { 139 | Statechart chart = TestCharts.t4(); 140 | 141 | TestParameter parameter = new TestParameter(); 142 | parameter.guardvalue = 1; 143 | Metadata data = new Metadata(); 144 | 145 | Assert.assertTrue(chart.start(data, parameter)); 146 | Assert.assertEquals("D:start A:a D:a A:j1 D:j1 E:a1 A:b D:b A:end", parameter.path); 147 | chart.shutdown(); 148 | } 149 | 150 | @Test 151 | public void testSemantics7() throws StatechartException { 152 | Statechart chart = TestCharts.t4(); 153 | 154 | TestParameter parameter = new TestParameter(); 155 | parameter.guardvalue = 2; 156 | Metadata data = new Metadata(); 157 | 158 | Assert.assertTrue(chart.start(data, parameter)); 159 | Assert.assertEquals("D:start A:a D:a A:j1 D:j1 E:a2 A:c D:c A:j2 D:j2 E:a3 A:j3 D:j3 E:a4 A:end", parameter.path); 160 | chart.shutdown(); 161 | } 162 | 163 | @Test 164 | public void testSemantics8() throws StatechartException { 165 | Statechart chart = TestCharts.h1(); 166 | 167 | TestParameter parameter = new TestParameter(); 168 | Metadata data = new Metadata(); 169 | 170 | Assert.assertTrue(chart.start(data, parameter)); 171 | Assert.assertEquals("D:start A:p A:start p D:start p A:a D:a A:b D:b A:end p D:end p D:p A:end", parameter.path); 172 | chart.shutdown(); 173 | } 174 | 175 | @Test 176 | public void testSemantics9() throws StatechartException { 177 | Statechart chart = TestCharts.h2(); 178 | 179 | TestEvent event = new TestEvent(1); 180 | TestParameter parameter = new TestParameter(); 181 | Metadata data = new Metadata(); 182 | 183 | Assert.assertTrue(chart.start(data, parameter)); 184 | Assert.assertTrue(chart.dispatch(data, event, parameter)); 185 | Assert.assertEquals("D:start A:p A:start p D:start p A:a D:a A:b D:b A:end p D:end p D:p A:end", parameter.path); 186 | chart.shutdown(); 187 | } 188 | 189 | @Test 190 | public void testSemantics10() throws StatechartException { 191 | Statechart chart = TestCharts.h3(); 192 | 193 | TestEvent event = new TestEvent(1); 194 | TestParameter parameter = new TestParameter(); 195 | Metadata data = new Metadata(); 196 | 197 | Assert.assertTrue(chart.start(data, parameter)); 198 | Assert.assertTrue(chart.dispatch(data, event, parameter)); 199 | Assert.assertEquals("D:start A:p A:start p D:start p A:a D:a A:b D:b A:end p D:end p D:p A:end", parameter.path); 200 | chart.shutdown(); 201 | } 202 | 203 | @Test 204 | public void testSemantics11() throws StatechartException { 205 | Statechart chart = TestCharts.h3(); 206 | 207 | TestEvent event = new TestEvent(2); 208 | TestParameter parameter = new TestParameter(); 209 | Metadata data = new Metadata(); 210 | 211 | Assert.assertTrue(chart.start(data, parameter)); 212 | Assert.assertTrue(chart.dispatch(data, event, parameter)); 213 | Assert.assertEquals("D:start A:p A:start p D:start p A:a D:a D:p A:end", parameter.path); 214 | chart.shutdown(); 215 | } 216 | 217 | @Test 218 | public void testSemantics12() throws StatechartException { 219 | Statechart chart = TestCharts.h4(); 220 | 221 | TestEvent event1 = new TestEvent(1); 222 | TestEvent event2 = new TestEvent(2); 223 | TestEvent event3 = new TestEvent(3); 224 | TestParameter parameter = new TestParameter(); 225 | Metadata data = new Metadata(); 226 | 227 | Assert.assertTrue(chart.start(data, parameter)); 228 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 229 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 230 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 231 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 232 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 233 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 234 | Assert.assertTrue(chart.dispatch(data, event3, parameter)); 235 | 236 | String result = "D:start A:p A:start p D:start p U:history p A:a D:a "; 237 | result += "D:p A:p A:start p D:start p A:a D:a A:b D:b D:p A:p "; 238 | result += "A:start p D:start p A:b D:b A:a D:a D:p A:p A:start p "; 239 | result += "D:start p A:a D:a A:b D:b A:end p D:end p D:p A:end"; 240 | 241 | Assert.assertEquals(result, parameter.path); 242 | chart.shutdown(); 243 | } 244 | 245 | @Test 246 | public void testSemantics13() throws StatechartException { 247 | Statechart chart = TestCharts.h5(); 248 | 249 | TestEvent event1 = new TestEvent(1); 250 | TestEvent event2 = new TestEvent(2); 251 | TestEvent event3 = new TestEvent(3); 252 | TestEvent event4 = new TestEvent(4); 253 | TestParameter parameter = new TestParameter(); 254 | Metadata data = new Metadata(); 255 | 256 | Assert.assertTrue(chart.start(data, parameter)); 257 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 258 | Assert.assertTrue(chart.dispatch(data, event4, parameter)); 259 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 260 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 261 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 262 | Assert.assertTrue(chart.dispatch(data, event3, parameter)); 263 | 264 | String result = "D:start A:p A:start p D:start p U:history p A:a D:a "; 265 | result += "A:q A:start q D:start q A:b D:b A:c D:c D:q D:p A:p "; 266 | result += "A:start p D:start p A:q A:c D:c D:q A:a D:a A:q "; 267 | result += "A:start q D:start q A:b D:b D:q A:end p D:end p D:p A:end"; 268 | 269 | Assert.assertEquals(result, parameter.path); 270 | chart.shutdown(); 271 | } 272 | 273 | @Test 274 | public void testSemantics14() throws StatechartException { 275 | Statechart chart = TestCharts.h6(); 276 | 277 | TestParameter parameter = new TestParameter(); 278 | Metadata data = new Metadata(); 279 | 280 | Assert.assertTrue(chart.start(data, parameter)); 281 | Assert.assertEquals("D:start A:p A:q A:r D:r D:q D:p A:x A:y D:y D:x A:end", parameter.path); 282 | chart.shutdown(); 283 | } 284 | 285 | @Test 286 | public void testSemantics15() throws StatechartException { 287 | Statechart chart = TestCharts.c1(); 288 | TestParameter parameter = new TestParameter(); 289 | Metadata data = new Metadata(); 290 | 291 | Assert.assertTrue(chart.start(data, parameter)); 292 | 293 | String result = "D:start "; 294 | result += "A:p "; 295 | result += "A:p-r1 "; 296 | result += "A:start p-r1 "; 297 | result += "D:start p-r1 "; 298 | result += "A:a "; 299 | result += "A:p-r2 "; 300 | result += "A:start p-r2 "; 301 | result += "D:start p-r2 "; 302 | result += "A:b "; 303 | result += "D:a "; 304 | result += "A:end p-r1 "; 305 | result += "D:b "; 306 | result += "A:end p-r2 "; 307 | result += "D:end p-r1 "; 308 | result += "D:p-r1 "; 309 | result += "D:end p-r2 "; 310 | result += "D:p-r2 "; 311 | result += "D:p "; 312 | result += "A:end"; 313 | 314 | Assert.assertEquals(result, parameter.path); 315 | chart.shutdown(); 316 | } 317 | 318 | @Test 319 | public void testSemantics16() throws StatechartException { 320 | Statechart chart = TestCharts.c2(); 321 | 322 | TestParameter parameter = new TestParameter(); 323 | Metadata data = new Metadata(); 324 | 325 | TestEvent event1 = new TestEvent(1); 326 | TestEvent event2 = new TestEvent(2); 327 | 328 | Assert.assertTrue(chart.start(data, parameter)); 329 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 330 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 331 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 332 | 333 | String result = "D:start "; 334 | result += "A:p "; 335 | result += "A:p-r1 "; 336 | result += "A:start p-r1 "; 337 | result += "D:start p-r1 "; 338 | result += "A:a "; 339 | result += "A:p-r2 "; 340 | result += "A:start p-r2 "; 341 | result += "D:start p-r2 "; 342 | result += "A:c "; 343 | result += "D:a "; 344 | result += "A:b "; 345 | result += "D:b "; 346 | result += "A:end p-r1 "; 347 | result += "D:c "; 348 | result += "A:d "; 349 | result += "D:d "; 350 | result += "A:e "; 351 | result += "D:e "; 352 | result += "A:end p-r2 "; 353 | result += "D:end p-r1 "; 354 | result += "D:p-r1 "; 355 | result += "D:end p-r2 "; 356 | result += "D:p-r2 "; 357 | result += "D:p "; 358 | result += "A:end"; 359 | 360 | Assert.assertEquals(result, parameter.path); 361 | chart.shutdown(); 362 | } 363 | 364 | @Test 365 | public void testSemantics17() throws StatechartException { 366 | Statechart chart = TestCharts.c2(); 367 | TestParameter parameter = new TestParameter(); 368 | Metadata data = new Metadata(); 369 | TestEvent event1 = new TestEvent(1); 370 | TestEvent event2 = new TestEvent(2); 371 | 372 | Assert.assertTrue(chart.start(data, parameter)); 373 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 374 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 375 | 376 | String result = "D:start "; 377 | 378 | result += "A:p "; 379 | result += "A:p-r1 "; 380 | result += "A:start p-r1 "; 381 | result += "D:start p-r1 "; 382 | result += "A:a "; 383 | result += "A:p-r2 "; 384 | result += "A:start p-r2 "; 385 | result += "D:start p-r2 "; 386 | result += "A:c "; 387 | // c . d 388 | result += "D:c "; 389 | result += "A:d "; // a . b 390 | result += "D:a "; 391 | result += "A:b "; // d . e 392 | result += "D:d "; 393 | result += "A:e "; 394 | // b . end p-r2 395 | result += "D:b "; 396 | result += "A:end p-r1 "; // e . end p-r2 397 | result += "D:e "; 398 | result += "A:end p-r2 "; 399 | result += "D:end p-r1 "; 400 | result += "D:p-r1 "; 401 | result += "D:end p-r2 "; 402 | result += "D:p-r2 "; 403 | result += "D:p "; 404 | result += "A:end"; 405 | 406 | Assert.assertEquals(result, parameter.path); 407 | chart.shutdown(); 408 | } 409 | 410 | @Test 411 | public void testSemantics18() throws StatechartException { 412 | Statechart chart = TestCharts.c2(); 413 | TestParameter parameter = new TestParameter(); 414 | Metadata data = new Metadata(); 415 | TestEvent event1 = new TestEvent(1); 416 | TestEvent event3 = new TestEvent(3); 417 | 418 | Assert.assertTrue(chart.start(data, parameter)); 419 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 420 | Assert.assertTrue(chart.dispatch(data, event3, parameter)); 421 | 422 | String result = "D:start "; 423 | result += "A:p "; 424 | result += "A:p-r1 "; 425 | result += "A:start p-r1 "; 426 | result += "D:start p-r1 "; 427 | result += "A:a "; 428 | result += "A:p-r2 "; 429 | result += "A:start p-r2 "; 430 | result += "D:start p-r2 "; 431 | result += "A:c "; // c . d 432 | result += "D:c "; 433 | result += "A:d "; // S3 Transition 434 | result += "D:a "; 435 | result += "D:p-r1 "; 436 | result += "D:d "; 437 | result += "D:p-r2 "; 438 | result += "D:p "; 439 | result += "A:end"; 440 | 441 | Assert.assertEquals(result, parameter.path); 442 | chart.shutdown(); 443 | } 444 | 445 | public void testSemantics19() throws StatechartException { 446 | Statechart chart = TestCharts.c3(); 447 | TestParameter parameter = new TestParameter(); 448 | Metadata data = new Metadata(); 449 | 450 | Assert.assertTrue(chart.start(data, parameter)); 451 | 452 | String result = "D:start "; 453 | result += "A:p "; 454 | result += "A:p-r2 "; 455 | result += "A:start p-r2 "; 456 | result += "D:start p-r2 "; 457 | result += "A:b "; 458 | result += "A:p-r1 "; 459 | result += "A:a "; // a . end p-r1 460 | result += "D:a "; 461 | result += "A:end p-r1 "; // b . end p-r2 462 | result += "D:b "; 463 | result += "A:end p-r2 "; 464 | result += "D:end p-r1 "; 465 | result += "D:p-r1 "; 466 | result += "D:end p-r2 "; 467 | result += "D:p-r2 "; 468 | result += "D:p "; 469 | result += "A:end"; 470 | 471 | Assert.assertEquals(result, parameter.path); 472 | chart.shutdown(); 473 | } 474 | 475 | @Test 476 | public void testSemantics20() throws StatechartException { 477 | Statechart chart = TestCharts.c4(); 478 | TestParameter parameter = new TestParameter(); 479 | Metadata data = new Metadata(); 480 | 481 | Assert.assertTrue(chart.start(data, parameter)); 482 | 483 | String result = "D:start "; 484 | result += "A:p "; 485 | result += "A:p-r1 "; 486 | result += "A:start p-r1 "; 487 | result += "D:start p-r1 "; 488 | result += "A:a "; 489 | result += "A:p-r2 "; 490 | result += "A:start p-r2 "; 491 | result += "D:start p-r2 "; 492 | result += "A:b "; // a . end 493 | result += "D:a "; 494 | result += "D:p-r1 "; 495 | result += "D:b "; 496 | result += "D:p-r2 "; 497 | result += "D:p "; 498 | result += "A:end"; 499 | 500 | Assert.assertEquals(result, parameter.path); 501 | chart.shutdown(); 502 | } 503 | 504 | @Test 505 | public void testSemantics21() throws StatechartException { 506 | Statechart chart = TestCharts.c5(); 507 | TestParameter parameter = new TestParameter(); 508 | Metadata data = new Metadata(); 509 | TestEvent event1 = new TestEvent(1); 510 | TestEvent event2 = new TestEvent(2); 511 | 512 | Assert.assertTrue(chart.start(data, parameter)); 513 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 514 | Assert.assertTrue(!chart.dispatch(data, event2, parameter)); 515 | 516 | String result = "D:start "; 517 | result += "A:p "; 518 | result += "A:p-r1 "; 519 | result += "A:start p-r1 "; 520 | result += "D:start p-r1 "; 521 | result += "A:a "; 522 | result += "A:p-r2 "; 523 | result += "A:start p-r2 "; 524 | result += "D:start p-r2 "; 525 | result += "A:b "; // a . end 526 | result += "D:a "; 527 | result += "D:p-r1 "; 528 | result += "D:b "; 529 | result += "D:p-r2 "; 530 | result += "D:p "; 531 | result += "A:end"; 532 | 533 | Assert.assertEquals(result, parameter.path); 534 | chart.shutdown(); 535 | } 536 | 537 | @Test 538 | public void testSemantics22() throws StatechartException { 539 | Statechart chart = TestCharts.c5(); 540 | TestParameter parameter = new TestParameter(); 541 | Metadata data = new Metadata(); 542 | TestEvent event1 = new TestEvent(1); 543 | TestEvent event2 = new TestEvent(2); 544 | 545 | Assert.assertTrue(chart.start(data, parameter)); 546 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 547 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 548 | 549 | String result = "D:start "; 550 | result += "A:p "; 551 | result += "A:p-r1 "; 552 | result += "A:start p-r1 "; 553 | result += "D:start p-r1 "; 554 | result += "A:a "; 555 | result += "A:p-r2 "; 556 | result += "A:start p-r2 "; 557 | result += "D:start p-r2 "; 558 | result += "A:b "; // b . c 559 | result += "D:b "; 560 | result += "A:c "; // a . end 561 | result += "D:a "; 562 | result += "D:p-r1 "; 563 | result += "D:c "; 564 | result += "D:p-r2 "; 565 | result += "D:p "; 566 | result += "A:end"; 567 | 568 | Assert.assertEquals(result, parameter.path); 569 | chart.shutdown(); 570 | } 571 | 572 | @Test 573 | public void testSemantics23() throws StatechartException { 574 | Statechart chart = TestCharts.c6(); 575 | TestParameter parameter = new TestParameter(); 576 | Metadata data = new Metadata(); 577 | 578 | Assert.assertTrue(chart.start(data, parameter)); 579 | 580 | String result = "D:start "; 581 | result += "A:fork "; 582 | result += "D:fork "; 583 | result += "A:p "; 584 | result += "A:p-r1 "; 585 | result += "A:a "; 586 | result += "A:p-r2 "; 587 | result += "A:b "; 588 | result += "D:a "; 589 | result += "A:end p-r1 "; 590 | result += "D:b "; 591 | result += "A:end p-r2 "; 592 | result += "D:end p-r1 "; 593 | result += "D:p-r1 "; 594 | result += "D:end p-r2 "; 595 | result += "D:p-r2 "; 596 | result += "D:p "; 597 | result += "A:end"; 598 | 599 | Assert.assertEquals(result, parameter.path); 600 | chart.shutdown(); 601 | } 602 | 603 | @Test 604 | public void testSemantics24() throws StatechartException { 605 | Statechart chart = TestCharts.c7(); 606 | TestParameter parameter = new TestParameter(); 607 | parameter.guardvalue = 1; 608 | Metadata data = new Metadata(); 609 | 610 | Assert.assertTrue(chart.start(data, parameter)); 611 | 612 | String result = "D:start "; 613 | result += "A:fork "; 614 | result += "D:fork "; 615 | result += "A:p "; 616 | result += "A:p-r3 "; 617 | result += "A:c "; 618 | result += "A:p-r1 "; 619 | result += "A:a "; 620 | result += "A:p-r2 "; 621 | result += "A:b "; 622 | result += "D:a "; 623 | result += "A:end p-r1 "; 624 | result += "D:b "; 625 | result += "A:end p-r2 "; 626 | result += "D:c "; 627 | result += "A:end p-r3 "; 628 | result += "D:end p-r1 "; 629 | result += "D:p-r1 "; 630 | result += "D:end p-r2 "; 631 | result += "D:p-r2 "; 632 | result += "D:end p-r3 "; 633 | result += "D:p-r3 "; 634 | result += "D:p "; 635 | result += "A:end"; 636 | 637 | Assert.assertEquals(result, parameter.path); 638 | chart.shutdown(); 639 | } 640 | 641 | @Test 642 | public void testSemantics25() throws StatechartException { 643 | Statechart chart = TestCharts.c7(); 644 | TestParameter parameter = new TestParameter(); 645 | parameter.guardvalue = 0; 646 | Metadata data = new Metadata(); 647 | 648 | Assert.assertTrue(chart.start(data, parameter)); 649 | 650 | String result = "D:start "; 651 | result += "A:fork "; 652 | result += "D:fork "; 653 | result += "A:p "; 654 | result += "A:p-r3 "; 655 | result += "A:start p-r3 "; 656 | result += "D:start p-r3 "; 657 | result += "A:d "; 658 | result += "A:p-r1 "; 659 | result += "A:a "; 660 | result += "A:p-r2 "; 661 | result += "A:b "; 662 | result += "D:a "; 663 | result += "A:end p-r1 "; 664 | result += "D:b "; 665 | result += "A:end p-r2 "; 666 | result += "D:d "; 667 | result += "A:end p-r3 "; 668 | result += "D:end p-r1 "; 669 | result += "D:p-r1 "; 670 | result += "D:end p-r2 "; 671 | result += "D:p-r2 "; 672 | result += "D:end p-r3 "; 673 | result += "D:p-r3 "; 674 | result += "D:p "; 675 | result += "A:end"; 676 | 677 | Assert.assertEquals(result, parameter.path); 678 | chart.shutdown(); 679 | } 680 | 681 | @Test 682 | public void testSemantics26() throws StatechartException { 683 | Statechart chart = TestCharts.c8(); 684 | TestParameter parameter = new TestParameter(); 685 | Metadata data = new Metadata(); 686 | 687 | Assert.assertTrue(chart.start(data, parameter)); 688 | 689 | String result = "D:start "; 690 | result += "A:p "; 691 | result += "A:p-r1 "; 692 | result += "A:start p-r1 "; 693 | result += "D:start p-r1 "; 694 | result += "A:a "; 695 | result += "A:p-r2 "; 696 | result += "A:start p-r2 "; 697 | result += "D:start p-r2 "; 698 | result += "A:b "; 699 | result += "D:b "; 700 | result += "A:c "; 701 | result += "D:a "; 702 | result += "D:p-r1 "; 703 | result += "D:c "; 704 | result += "D:p-r2 "; 705 | result += "D:p "; 706 | result += "A:join "; 707 | result += "D:join "; 708 | result += "A:end"; 709 | 710 | Assert.assertEquals(result, parameter.path); 711 | chart.shutdown(); 712 | } 713 | 714 | @Test 715 | public void testSemantics27() throws InterruptedException, StatechartException { 716 | Statechart chart = TestCharts.c9(); 717 | TestParameter parameter = new TestParameter(); 718 | Metadata data = new Metadata(); 719 | 720 | Assert.assertTrue(chart.start(data, parameter)); 721 | waitForFinalState(chart, data); 722 | 723 | String result = "D:start "; 724 | result += "A:p "; 725 | result += "A:p-r1 "; 726 | result += "A:start p-r1 "; 727 | result += "D:start p-r1 "; 728 | result += "A:a "; 729 | result += "A:p-r2 "; 730 | result += "A:start p-r2 "; 731 | result += "D:start p-r2 "; 732 | result += "A:b "; 733 | result += "A:p-r3 "; 734 | result += "A:start p-r3 "; 735 | result += "D:start p-r3 "; 736 | result += "A:d "; 737 | result += "D:b "; 738 | result += "A:c "; 739 | result += "D:a "; 740 | result += "D:p-r1 "; 741 | result += "D:c "; 742 | result += "D:p-r2 "; 743 | result += "D:d "; 744 | result += "D:p-r3 "; 745 | result += "D:p "; 746 | result += "A:join "; 747 | result += "D:join "; 748 | result += "A:end"; 749 | 750 | Assert.assertEquals(result, parameter.path); 751 | chart.shutdown(); 752 | } 753 | 754 | @Test 755 | public void testSemantics28() throws StatechartException { 756 | Statechart chart = TestCharts.c10(); 757 | TestParameter parameter = new TestParameter(); 758 | Metadata data = new Metadata(); 759 | TestEvent event1 = new TestEvent(1); 760 | TestEvent event2 = new TestEvent(2); 761 | 762 | Assert.assertTrue(chart.start(data, parameter)); 763 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 764 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 765 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 766 | Assert.assertTrue(chart.dispatch(data, event1, parameter)); 767 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 768 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 769 | 770 | String result = "D:start "; 771 | result += "A:c "; 772 | result += "D:c "; 773 | // S1 774 | result += "A:p "; 775 | result += "A:p-r1 "; 776 | result += "A:start p-r1 "; 777 | result += "D:start p-r1 "; 778 | result += "A:end p-r1 "; 779 | result += "A:p-r2 "; 780 | result += "A:start p-r2 "; 781 | result += "D:start p-r2 "; 782 | result += "E:history "; 783 | result += "A:a "; // S1 784 | result += "D:a "; 785 | result += "A:b "; // S2 786 | result += "D:b "; 787 | result += "D:p-r2 "; 788 | result += "D:end p-r1 "; 789 | result += "D:p-r1 "; 790 | result += "D:p "; 791 | result += "A:c "; // S1 792 | result += "D:c "; 793 | result += "A:p "; 794 | result += "A:p-r1 "; 795 | result += "A:start p-r1 "; 796 | result += "D:start p-r1 "; 797 | result += "A:end p-r1 "; 798 | result += "A:p-r2 "; 799 | result += "A:start p-r2 "; 800 | result += "D:start p-r2 "; 801 | result += "A:b "; // S2 802 | result += "D:b "; 803 | result += "D:p-r2 "; 804 | result += "D:end p-r1 "; 805 | result += "D:p-r1 "; 806 | result += "D:p "; 807 | result += "A:c "; // S2 808 | result += "D:c "; 809 | result += "A:end"; 810 | 811 | Assert.assertEquals(result, parameter.path); 812 | chart.shutdown(); 813 | } 814 | 815 | // Checks the deep history state when the transition is made from the substate 816 | // Be aware that this does not work with a correct semantics but we make sure 817 | // that no NullPointerException occurs. See commit 575482c6 for information 818 | @Test 819 | @Ignore 820 | public void testSemantics29() throws StatechartException { 821 | Statechart chart = TestCharts.h5(); 822 | 823 | TestEvent event2 = new TestEvent(2); 824 | TestEvent event3 = new TestEvent(3); 825 | TestEvent event4 = new TestEvent(4); 826 | TestEvent event5 = new TestEvent(5); 827 | TestParameter parameter = new TestParameter(); 828 | Metadata data = new Metadata(); 829 | 830 | Assert.assertTrue(chart.start(data, parameter)); 831 | Assert.assertTrue(chart.dispatch(data, event2, parameter)); 832 | Assert.assertTrue(chart.dispatch(data, event4, parameter)); 833 | Assert.assertTrue(chart.dispatch(data, event5, parameter)); 834 | Assert.assertTrue(chart.dispatch(data, event3, parameter)); 835 | 836 | String result = "D:start A:p A:start p D:start p U:history p A:a D:a "; 837 | result += "A:q A:start q D:start q A:b D:b A:c D:c D:q D:p A:d D:d A:p "; 838 | result += "A:start p D:start p A:q A:c D:c D:q A:end p D:end p D:p A:end"; 839 | 840 | Assert.assertEquals(result, parameter.path); 841 | chart.shutdown(); 842 | } 843 | 844 | // Statechart for Issue #2 - Timeouts in concurrent states 845 | @Test 846 | public void testSemantics30() throws StatechartException { 847 | Statechart chart = TestCharts.c11(); 848 | TestParameter parameter = new TestParameter(); 849 | Metadata data = new Metadata(); 850 | Assert.assertTrue(chart.start(data, parameter)); 851 | waitForFinalState(chart, data); 852 | 853 | String result = 854 | "D:start A:p " + 855 | "A:p-r1 A:start p-r1 D:start p-r1 A:a " + 856 | "A:p-r2 A:start p-r2 D:start p-r2 A:b " + 857 | "D:b E:t2 A:end p-r2 " + 858 | "D:a E:t1 A:end p-r1 " + 859 | "D:end p-r1 D:p-r1 " + 860 | "D:end p-r2 D:p-r2 " + 861 | "D:p " + 862 | "A:end"; 863 | 864 | Assert.assertEquals(result, parameter.path); 865 | chart.shutdown(); 866 | } 867 | 868 | @Test 869 | public void testInternalTransaction() throws StatechartException { 870 | Statechart chart = TestCharts.t5(); 871 | TestParameter parameter = new TestParameter(); 872 | Metadata data = new Metadata(); 873 | Assert.assertTrue(chart.start(data, parameter)); 874 | Assert.assertTrue(chart.dispatch(data, new TestEvent(1), parameter)); 875 | Assert.assertTrue(chart.dispatch(data, new TestEvent(2), parameter)); 876 | Assert.assertTrue(chart.dispatch(data, new TestEvent(3), parameter)); 877 | 878 | String result = 879 | // event 1 880 | "D:start E:t1 A:a " + 881 | // event 2 882 | "D:a E:t2 A:a " + 883 | // event 3 884 | "E:t3 " + 885 | // event 4 886 | "D:a E:t4 A:end"; 887 | 888 | Assert.assertEquals(result, parameter.path); 889 | chart.shutdown(); 890 | } 891 | } 892 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/TestAction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | 23 | public class TestAction implements Action { 24 | public TestAction(String name, String action) { 25 | this.name = name; 26 | this.action = action; 27 | } 28 | 29 | @Override 30 | public void execute(Metadata data, Parameter param) { 31 | TestParameter parameter = (TestParameter) param; 32 | parameter.path += (parameter.path.length() != 0 ? " " : "") + action + ":" + name; 33 | }; 34 | 35 | private String name; 36 | 37 | private String action; 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/TestEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | 23 | public class TestEvent extends Event { 24 | TestEvent(int i) { 25 | this.i = i; 26 | } 27 | 28 | @Override 29 | public boolean equals(Event event, Metadata data, Parameter param) { 30 | if (event instanceof TestEvent) { 31 | TestEvent e = (TestEvent) event; 32 | if (e != null) { 33 | return e.getNumber() == i; 34 | } 35 | } 36 | return false; 37 | } 38 | 39 | int getNumber() { 40 | return i; 41 | } 42 | 43 | private int i; 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/TestGuard.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | 23 | public class TestGuard implements Guard { 24 | TestGuard(int i) { 25 | this.i = i; 26 | } 27 | 28 | @Override 29 | public boolean check(Metadata data, Parameter param) { 30 | TestParameter p = (TestParameter) param; 31 | if (p != null) { 32 | return p.guardvalue == i; 33 | } 34 | return false; 35 | }; 36 | 37 | private int i; 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/github/klangfarbe/statechart/TestParameter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * UML statechart framework (http://github.com/klangfarbe/UML-Statechart-Framework-for-Java) 3 | * 4 | * Copyright (C) 2006-2013 Christian Mocek (christian.mocek@gmail.com) 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | package com.github.klangfarbe.statechart; 21 | 22 | 23 | public class TestParameter extends Parameter { 24 | String path = new String(); 25 | int guardvalue = 0; 26 | } 27 | --------------------------------------------------------------------------------