├── LICENSE ├── README.adoc ├── helpers.py ├── intervals.py ├── logic_changing.py ├── rdb_section_extract.py ├── rdbextract.py ├── sel_logic_count.py └── sel_logic_functions.py /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = rdb-tool 2 | Tool for manipulating Schweitzer Engineering Laboratories (SEL) relay database files (.rdb) files. Intended to allow automated processing and interoperability with protection relay files. 3 | 4 | This is a work in progress. It seems to work moderately well for iterating across large folder structures and quantities of RDB files in a fairly short amount of time. 5 | 6 | Feedback most welcome. 7 | 8 | == Installation 9 | 10 | pip3 install named_constants 11 | -------------------------------------------------------------------------------- /helpers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import re 4 | from collections import OrderedDict 5 | from functools import partial 6 | 7 | def flatten(l): 8 | # flatten list of lists by 1 9 | return [item for sublist in l for item in sublist] 10 | 11 | def unique(l): 12 | # unique items in a list 13 | return list(set(l)) 14 | 15 | def remove_empty(mylist): 16 | return [l for l in mylist if l] 17 | 18 | def hasNumbers(inputString): 19 | return bool(re.search(r'\d', inputString)) 20 | 21 | # https://stackoverflow.com/questions/45128959/python-replace-multiple-strings-while-supporting-backreferences 22 | 23 | def multiple_replace(text, repldict): 24 | # split the dict into two lists because we need the order to be reliable 25 | keys, repls = zip(*repldict.items()) 26 | 27 | # generate a regex pattern from the keys, putting each key in a named group 28 | # so that we can find out which one of them matched. 29 | # groups are named "_" where is the index of the corresponding 30 | # replacement text in the list above 31 | pattern = '|'.join('(?P<_{}>{})'.format(i, k) for i, k in enumerate(keys)) 32 | 33 | def repl(match): 34 | # find out which key matched. We know that exactly one of the keys has 35 | # matched, so it's the only named group with a value other than None. 36 | group_name = next(name for name, value in match.groupdict().items() 37 | if value is not None) 38 | group_index = int(group_name[1:]) 39 | 40 | # now that we know which group matched, we can retrieve the 41 | # corresponding replacement text 42 | repl_text = repls[group_index] 43 | 44 | # now we'll manually search for backreferences in the 45 | # replacement text and substitute them 46 | def repl_backreference(m): 47 | reference_index = int(m.group(1)) 48 | 49 | # return the corresponding group's value from the original match 50 | # +1 because regex starts counting at 1 51 | return match.group(group_index + reference_index + 1) 52 | 53 | return re.sub(r'\\(\d+)', repl_backreference, repl_text) 54 | 55 | return re.sub(pattern, repl, text) 56 | 57 | # https://stackoverflow.com/questions/45128959/python-replace-multiple-strings-while-supporting-backreferences 58 | #https://stackoverflow.com/questions/6116978/how-to-replace-multiple-substrings-of-a-string 59 | 60 | def build_replacer(cases): 61 | ordered_cases = OrderedDict(cases.items()) 62 | replacements = {} 63 | 64 | leading_groups = 0 65 | for pattern, replacement in ordered_cases.items(): 66 | leading_groups += 1 67 | 68 | # leading_groups is now the absolute position of the root group (back-references should be relative to this) 69 | group_index = leading_groups 70 | replacement = absolute_backreference(replacement, group_index) 71 | replacements[group_index] = replacement 72 | 73 | # This pattern contains N subgroups (determine by compiling pattern) 74 | subgroups = re.compile(pattern).groups 75 | leading_groups += subgroups 76 | 77 | catch_all = "|".join("({})".format(p) for p in ordered_cases) 78 | pattern = re.compile(catch_all) 79 | 80 | def replacer(match): 81 | replacement_pattern = replacements[match.lastindex] 82 | return match.expand(replacement_pattern) 83 | 84 | return partial(pattern.sub, replacer) 85 | 86 | def absolute_backreference(text, n): 87 | ref_pat = re.compile(r"\\([0-99])") 88 | 89 | def replacer(match): 90 | return "\\{}".format(int(match.group(1)) + n) 91 | 92 | return ref_pat.sub(replacer, text) 93 | 94 | def multireplace(text, repldict, prefix='', suffix=''): 95 | new_repldict = {k:prefix + v + suffix for (k,v) in repldict.items()} 96 | replacer = build_replacer(new_repldict) # must not be regex 97 | return replacer(text) 98 | 99 | #pattern_to_replacement = {'&&': 'and', '!([a-zA-Z_]+)': r'not \1'} 100 | #replacer = build_replacer(pattern_to_replacement) 101 | #print(replacer("!this.exists()")) -------------------------------------------------------------------------------- /intervals.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # https://stack, overflow.com/questions/2154249/identify-groups-of-continuous-numbers-in-a-list 4 | def as_range(iterable): # not sure how to do this part elegantly 5 | l = list(iterable) 6 | if len(l) > 1: 7 | return '{0}-{1}'.format(l[0], l[-1]) 8 | else: 9 | return '{0}'.format(l[0]) 10 | 11 | return ','.join(as_range(g) for _, g in groupby(numberlist, key=lambda n, c=count(): n-next(c))) 12 | 13 | def get_interval_range(data): 14 | from itertools import groupby 15 | from operator import itemgetter 16 | 17 | ranges =[] 18 | for key, group in groupby(enumerate(data), lambda i: i[0] - i[1]): 19 | group = list(map(itemgetter(1), group)) 20 | group = list(map(int,group)) 21 | ranges.append((group[0],group[-1])) 22 | return ranges 23 | 24 | def provide_string_range(rng): 25 | output = [] 26 | for lims in rng: 27 | #print(lims) 28 | if lims[0] == lims[1]: 29 | output.append(str(lims[0])) 30 | else: 31 | output.append(str(lims[0])+'-'+str(lims[1])) 32 | return ", ".join(output) -------------------------------------------------------------------------------- /logic_changing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | 5 | This tool allows various useful operations to occur 6 | 7 | """ 8 | 9 | from __future__ import absolute_import 10 | 11 | import csv 12 | import re 13 | 14 | from io import StringIO 15 | from difflib import Differ 16 | from more_itertools import unique_everseen 17 | 18 | from colorama import Fore, Back, Style 19 | import lxml.etree as ET 20 | 21 | import helpers 22 | import sel_logic_count 23 | import sel_logic_functions 24 | 25 | 26 | ERR_START = Fore.RED + Back.LIGHTBLACK_EX + Style.BRIGHT 27 | ERR_END = Fore.RESET + Back.RESET + Style.RESET_ALL 28 | 29 | aliases = """ 30 | PMV25,C79OI1,HV CB Open Interval -- 5 seconds 31 | PMV26,C79OI2,LV CB Open Interval -- 15 seconds 32 | PMV27,C79RCD,Auto Reclose Reclaim Time -- 15 seconds 33 | PMV28,C79MRCD,Manual Close Reclaim Time -- 10 seconds 34 | PMV29,C79CLSD,Auto Reclose Close Supervision Time -- 1 second 35 | PMV30,C79CFD,Close Failure Time -- 180 ms 36 | PSV29,C79RI,Auto Reclose Initiate -- IN106 37 | PSV30,C79RIS,Recloser Initiate Supervision 38 | PSV31,C79DTL,Drive To Lockout Conditions 39 | PSV32,C79CLS1,HV Close Supervision Condition 40 | PSV35,C79CLS2,LV Close Supervision Condition 41 | PSV36,C3POTX,Transformer Three Pole Open 42 | """ 43 | 44 | logic = """ 45 | ## ## AUTO RECLOSE LOGIC. CONSULT WITH TRANSPOWER BEFORE USING ## ## 46 | # TIMER SETTINGS 47 | PMV25 := 250.000000 # HV CB OPEN INTERVAL 48 | PMV26 := 750.000000 # LV CB OPEN INTERVAL 49 | PMV27 := 750.000000 # AUTO RECLOSE RECLAIM TIME 50 | PMV28 := 500.000000 # MANUAL CLOSE RECLAIM TIME 51 | PMV29 := 50.000000 # AUTO RECLOSE CLOSE SUPERVISION TIME 52 | PMV30 := 9.000000 # CLOSE FAILURE TIME 53 | # LOGIC SETTINGS 54 | PSV29 := IN106 # AUTO RECLOSE INITIATE 55 | PSV30 := 52CLS AND 52CLU AND PLT15 # RECLOSER INITIATE SUPERVISION 56 | PSV31 := TRIP OR IN107 OR IN201 # DRIVE TO LOCKOUT CONDITIONS 57 | PSV32 := 594P1 # HV CLOSE SUPERVISION CONDITION 58 | PSV35 := 595P1 # LV CLOSE SUPERVISION CONDITION 59 | # 60 | # FIXED AUTO RECLOSE LOGIC PAST THIS POINT. DO NOT MODIFY # 61 | PSV36 := NOT (52CLS AND 52CLU) # THREE POLE OPEN FOR BOTH TRANSFORMER BREAKERS 62 | PCT16PU := 0.000000 # HV CB OPEN INTERVAL TIMER 63 | PCT16DO := PMV25 64 | PCT16IN := PLT21 AND R_TRIG PSV36 65 | PCT17PU := 0.000000 # LV CB OPEN INTERVAL TIMER 66 | PCT17DO := PMV26 67 | PCT17IN := PLT21 AND R_TRIG PSV36 68 | PCT18PU := 0.000000 # RECLOSE INITIATE SUPERVISION TIMER. 69 | PCT18DO := 15.000000 70 | PCT18IN := R_TRIG PLT21 71 | PCT19PU := 0.000000 # AUTO RECLOSE RECLAIM TIMER 72 | PCT19DO := PMV27 73 | PCT19IN := PLT21 AND R_TRIG 52CLU 74 | PCT20PU := PMV28 # MANUAL RECLAIM TIMER 75 | PCT20DO := 0.000000 76 | PCT20IN := PLT22 AND NOT PSV31 AND PSV30 77 | PCT21PU := PMV30 # CB CLOSE FAILURE TIMER 78 | PCT21DO := 0.000000 79 | PCT21IN := PLT15 AND (PCT05Q AND NOT 52CLS OR PCT07Q AND NOT 52CLU) 80 | PCT22PU := PMV29 # HV CLOSE SUPERVISION TIMER 81 | PCT22DO := 0.000000 82 | PCT22IN := PLT23 83 | PCT23PU := PMV29 # LV CLOSE SUPERVISION TIMER 84 | PCT23DO := 0.000000 85 | PCT23IN := PLT24 86 | # RESET STATE # 87 | PLT20S := PLT22 AND R_TRIG PCT20Q OR PLT21 AND F_TRIG PCT19Q 88 | PLT20R := R_TRIG PLT21 OR R_TRIG PLT22 89 | # CYCLE STATE # 90 | PLT21S := PLT20 AND PSV29 AND PSV30 91 | PLT21R := R_TRIG PLT20 OR R_TRIG PLT22 92 | # LOCKOUT STATE # 93 | PLT22S := PSV31 OR (PLT20 AND F_TRIG PSV30) OR (F_TRIG PCT18Q AND NOT PSV36) OR PCT21Q OR PCT22Q OR PCT23Q OR F_TRIG PLT15 OR PFRTEX 94 | PLT22R := R_TRIG PCT20Q 95 | # AUTO RECLOSE CLOSE COMMANDS # 96 | PLT23S := F_TRIG PCT16Q AND NOT PSV32 # HV RECLOSE SUPERVISION 97 | PLT23R := R_TRIG PSV32 OR R_TRIG PCT22Q 98 | PSV33 := (PLT21 AND F_TRIG PCT16Q AND PSV32) OR (PLT21 AND PLT23 AND R_TRIG PSV32) # HV CB CLOSE COMMAND 99 | PLT24S := F_TRIG PCT17Q AND NOT PSV35 # LV RECLOSE SUPERVISION 100 | PLT24R := R_TRIG PSV35 OR R_TRIG PCT23Q 101 | PSV34 := (PLT21 AND F_TRIG PCT17Q AND PSV35) OR (PLT21 AND PLT24 AND R_TRIG PSV35) # LV CB CLOSE COMMAND 102 | ## END OF AUTO RECLOSE LOGIC ## 103 | """ 104 | 105 | class Line: 106 | """ an SEL logic line """ 107 | 108 | def __init__(self, text, parent=None): 109 | self.parent = parent 110 | self.text = text 111 | self.type = None 112 | self.raw_text = sel_logic_count.removeComment(text).strip() 113 | COMMENTS = re.compile(r'^.*?(#+.*$)') 114 | comment = COMMENTS.findall(self.text) 115 | self.comment = '' if not comment else '' + comment[0].strip() 116 | self.update() 117 | 118 | def getLineNum(self): 119 | return self.parent.lines.index(self) 120 | 121 | def replace(self, first, second, dummyRun=False, etype='equation',): 122 | new = None 123 | old = self.raw_text 124 | if etype == 'equation': 125 | new = self.raw_text.replace(first, second) 126 | if not dummyRun: 127 | self.raw_text = new 128 | self.update() 129 | return old != new 130 | else: 131 | print('Error!') 132 | 133 | def replace_line(self, raw, comment=None, keepComment=False): 134 | if not keepComment: 135 | self.comment = comment 136 | self.raw_text = raw 137 | self.type = None 138 | self.update() 139 | 140 | def find(self, regex): 141 | searchpos = re.findall(regex, self.raw_text) 142 | if searchpos: 143 | return self 144 | else: 145 | return None 146 | 147 | def update(self): 148 | if self.text.startswith('#'): 149 | self.type = 'comment' 150 | self.text = str(self) 151 | 152 | def pretty_print(self, withAliases=False): 153 | if self.type == 'comment': 154 | result = '{:<8} {}'.format(Fore.BLUE + str(self.getLineNum()), 155 | Fore.RESET + self.raw_text.strip() + 156 | Fore.GREEN + Style.DIM + self.comment + 157 | Fore.RESET + Style.RESET_ALL) 158 | else: 159 | elems = sel_logic_count.countElementsUsed(self.text) - 1 160 | result = '{:<8} {:>8} {}'.format(Fore.BLUE + str(self.getLineNum()), 161 | Fore.LIGHTCYAN_EX + str(elems), 162 | Fore.WHITE + self.raw_text + 163 | Fore.GREEN + Style.DIM + ' ' + self.comment + 164 | Fore.RESET + Style.RESET_ALL) 165 | if withAliases and self.parent: 166 | from_to = {k:v[0] for (k,v) in self.parent.aliases.items()} 167 | result = helpers.multireplace(result, from_to, 168 | prefix=Fore.MAGENTA, suffix=Fore.WHITE) 169 | 170 | return result 171 | 172 | def __str__(self): 173 | if len(self.raw_text) > 0: 174 | return self.raw_text + ' ' + self.comment 175 | else: 176 | return self.comment 177 | 178 | class LogicLines: 179 | def __init__(self, text, aliases): 180 | self.text = text 181 | self.lines = [] 182 | self.aliases = self.get_aliases(aliases) 183 | self.makeLines() 184 | 185 | def makeLines(self): 186 | all_lines = (self.text.strip()).split('\n') 187 | for idx, l in enumerate(all_lines): 188 | self.lines.append(Line(l, parent=self)) 189 | 190 | def addLine(self, text): 191 | self.lines.append(Line(text, parent=self)) 192 | self.text += '\n' + text 193 | 194 | def insertLine(self, n, text, comment=''): 195 | self.lines.insert(n, Line(text, parent=self)) 196 | self.text += '\n' + text 197 | self.comment = '' 198 | self.updateLines() 199 | 200 | def deleteLine(self, line): 201 | self.lines.remove(line) 202 | 203 | def deleteLineByIndex(self, n): 204 | del self.lines[n] 205 | 206 | def getDefinitions(self, df): 207 | """ 208 | returns the line where an element is defined 209 | e.g. ASV034:= (ALT21 AND F_TRIG ASV038) 210 | would be the line object returned if df = 'ASV034' 211 | Multiple definitions will result in multiple line 212 | objects being returned 213 | """ 214 | result = [] 215 | for l in self.lines: 216 | dfn = l.raw_text.split(':=')[0].strip() 217 | #print(dfn) 218 | if dfn == df: 219 | result.append(l) 220 | return result 221 | 222 | def get_aliases(self, aliasCSV): 223 | if aliasCSV: 224 | reader = csv.reader(aliasCSV.strip().splitlines(), delimiter=',') 225 | al = {} 226 | for row in reader: 227 | al[row[0]] = [row[1], row[2]] 228 | return al 229 | else: 230 | return None 231 | 232 | def update_aliases(self, arr): 233 | update_dict = dict(arr) 234 | new_dict = {} 235 | # done this way to ensure no sequential replacement issues 236 | for k, v in self.aliases.items(): 237 | new_dict[helpers.multiple_replace(k, update_dict)] = v 238 | self.aliases = new_dict 239 | 240 | def add_alias(self, rwb, alias, description): 241 | self.aliases[rwb] = [alias, description] 242 | 243 | def print_aliases(self): 244 | print(Fore.YELLOW + Style.BRIGHT + 'Aliases' + Fore.RESET + Style.RESET_ALL) 245 | for k, v in self.aliases.items(): 246 | print('{:>9} {} ➔ {:<20} {}'.format(Fore.WHITE + k, Fore.RESET + Fore.LIGHTRED_EX, 247 | Fore.RESET + Fore.MAGENTA + v[0], 248 | Fore.GREEN + Style.DIM + v[1] + Style.RESET_ALL, 249 | Fore.RESET + Style.RESET_ALL)) 250 | 251 | def getTypeDefinitions(self, df): 252 | """ 253 | Returns all definitions of a particular type. 254 | e.g. ASV will return the values and the line objects for the 255 | following definitions if used in the text: 256 | ASV001 := blah 257 | ASV003 := blah 258 | ASV030 := blah 259 | """ 260 | 261 | etypes = sel_logic_count.RDBOperatorsConst.TYPES[df] 262 | search_regex = {} 263 | for var in etypes: 264 | result = sel_logic_count.getRawVariableFromTo(var) 265 | search_regex[result[0]] = result[1] 266 | 267 | substrs = sorted(search_regex, key=len, reverse=True) 268 | regex_search = re.compile('|'.join(substrs)) 269 | 270 | replacer = helpers.build_replacer(search_regex) 271 | 272 | result = [] 273 | result_lines = [] 274 | for l in self.lines: 275 | candidate = l.raw_text.split(':=')[0].strip() 276 | dfn = regex_search.findall(candidate) 277 | if dfn: 278 | result.append(replacer(candidate)) 279 | result_lines.append(l) 280 | 281 | return [result, result_lines] 282 | 283 | def replace(self, first, second): 284 | replacements = [] 285 | for l in self.lines: 286 | result = l.replace(first, second) 287 | if result: 288 | replacements.append(l.getLineNum()) 289 | self.updateLines() 290 | return replacements 291 | 292 | def multireplace(self, repldict): 293 | replacer = helpers.build_replacer(repldict) # must not be regex 294 | for l in self.lines: 295 | result = replacer(l.raw_text) 296 | l.replace_line(result, keepComment=True) 297 | 298 | def find(self, regex): 299 | results = [] 300 | for l in self.lines: 301 | result = l.find(regex) 302 | if result != None: 303 | results.append(result) 304 | return results 305 | 306 | def getNextVar(self, name, min=None, max=None, qty=1, skipUsed=True): 307 | # name = 'ASV' etc. 308 | 309 | if min == None: 310 | min = sel_logic_count.RDBOperatorsConst.LIMITS[name][0] 311 | 312 | if max == None: 313 | max = sel_logic_count.RDBOperatorsConst.LIMITS[name][1] 314 | 315 | self.updateLines() # ensure lines are up-to-date 316 | 317 | # this does the heavy lifting 318 | used = [] 319 | if skipUsed: 320 | used = sel_logic_count.get_logic_usage(self.text)[1] 321 | return sel_logic_count.find_unused_logic(name, used, provideRaw=True, lowestAllowed=min, highestAllowed=max)[0:qty] 322 | 323 | def updateLines(self): 324 | self.text = str(self) 325 | 326 | def pretty_print(self, withAliases=False): 327 | line_text = '' 328 | for l in self.lines: 329 | line_text += l.pretty_print(withAliases) + '\n' 330 | 331 | return line_text + '\n' + '\n' + str(sel_logic_count.calc_logic_usage(str(self))) 332 | 333 | def __str__(self): 334 | line_text = '' 335 | for l in self.lines: 336 | line_text += str(l) + '\n' 337 | return line_text 338 | 339 | class LogicManipulator: 340 | 341 | def __init__(self, text, aliases): 342 | self.l = LogicLines(text, aliases) 343 | self.history = [] 344 | 345 | def change_type(self, e, to, onlyIfDefined=False): 346 | # onlyIfDefined= TODO: Not implemented yet 347 | 348 | items = sel_logic_functions.makeLogicItems(e) 349 | # this might just be generic changes 350 | result = {} 351 | for var_to_change in items: 352 | things_to_change = sel_logic_functions.change_type_vals(var_to_change, to) 353 | # a (from, to) tuple 354 | changes = list(zip(things_to_change[0], 355 | things_to_change[1])) 356 | self.history.append(changes) 357 | self.l.update_aliases(changes) 358 | for c in changes: 359 | lchange = self.l.replace(c[0], c[1]) 360 | if lchange != []: 361 | result[c] = lchange 362 | return result 363 | 364 | def convert_timers(self, e, from_type, to_type, asv_min=1, asv_max=256): 365 | items = sel_logic_functions.makeLogicItems(e) 366 | 367 | for t in items: 368 | self.convert_timer(t, from_type, to_type, asv_min=asv_min, asv_max=asv_max) 369 | 370 | def convert_timer(self, e, from_type, to_type, asv_min=1, asv_max=256): 371 | FREQUENCY = 50 372 | """ 373 | from_type --> to_type 374 | PCT PST 375 | PST PCT 376 | PCT AST 377 | AST PCT 378 | """ 379 | number_regex = re.compile(r'[A-Z]+([0-9]+)[A-Z]*') 380 | num = number_regex.findall(e)[0] 381 | 382 | if from_type == 'PCT' and to_type == 'AST': 383 | vals = sel_logic_functions.getInstVals(e) 384 | pu = [x for x in vals if x.endswith('PU')][0] 385 | do = [x for x in vals if x.endswith('DO')][0] 386 | inp = [x for x in vals if x.endswith('IN')][0] 387 | q = [x for x in vals if x.endswith('Q')][0] 388 | 389 | pu_def = self.l.getDefinitions(pu)[0] 390 | do_def = self.l.getDefinitions(do)[0] 391 | in_def = self.l.getDefinitions(inp)[0] 392 | 393 | # TODO: FIXME check if there is more than 1, if so error out as a minimum 394 | 395 | try: 396 | pu_time = float(pu_def.raw_text.split(':=')[1].strip()) 397 | except ValueError: 398 | pu_time = pu_def.raw_text.split(':=')[1].strip() 399 | 400 | try: 401 | do_time = float(do_def.raw_text.split(':=')[1].strip()) 402 | except ValueError: 403 | do_time = do_def.raw_text.split(':=')[1].strip() 404 | 405 | in_val = in_def.raw_text.split(':=')[1].strip() 406 | 407 | print('Timer Info: PU: {} DO: {} IN: {}'.format(pu_time,do_time,in_val)) 408 | 409 | if pu_time != 0 and do_time != 0: 410 | print(ERR_START + 'This will not end well, cannot replace timers with both PU and DO != zero, doing nothing' + 411 | ERR_END) 412 | 413 | elif pu_time != 0 and do_time == 0: 414 | """ 415 | pickup only 416 | 417 | PCT01PU:= 5 AST01PT:= 5/50 418 | PCT01DO:= 0 AST01R:= NOT(X) 419 | PCT01IN:= X AST01IN:= X 420 | Output == PCT01Q Output == AST01Q 421 | """ 422 | if isinstance(pu_time, float): 423 | pu_def.replace_line('{}{}{} := {:-f}'.format('AST', num, 'PT', 424 | round(pu_time/FREQUENCY,5)), 425 | pu_def.comment) 426 | # Sometimes we set the time to a math variable, not a number! 427 | else: 428 | print(ERR_START + 429 | 'Need to _manually_ adjust {} time from cycles to seconds'.format(pu_time) + 430 | ERR_END) 431 | # TODO: Recursive search for a numeric or convertible value. Complicated. 432 | pu_def.replace_line('{}{}{} := {}'.format('AST', num, 'PT', 433 | pu_time), 434 | pu_def.comment) 435 | 436 | do_def.replace_line('{}{}{} := {}'.format('AST', num, 'R', 437 | 'NOT(' + in_val + ')'), 438 | do_def.comment) 439 | in_def.replace_line('{}{}{} := {}'.format('AST', num, 'IN', 440 | in_val), 441 | in_def.comment) 442 | self.l.replace(q, 'AST' + num + 'Q') 443 | 444 | elif pu_time == 0 and do_time != 0: 445 | """ 446 | dropoff only 447 | PCT01PU:= 25 AST01PT:= 25/50 * NOT AFRTEXP 448 | PCT01DO:= 0 AST01R:= X 449 | PCT01IN:= X AST01IN:= NOT(X) 450 | Output == PCT01Q Output == NOT(AST01Q) 451 | """ 452 | if isinstance(do_time, float): 453 | pu_def.replace_line('{}{}{} := {:-f} * NOT AFRTEXP'.format('AST', num, 'PT', 454 | round(do_time/FREQUENCY,5)), 455 | pu_def.comment) 456 | # Sometimes we set the time to a math variable, not a number! 457 | else: 458 | print(ERR_START + 459 | 'Need to _manually_ adjust {} time from cycles to seconds'.format(pu_time) + 460 | ERR_END) 461 | # TODO: Recursive search for a numeric or convertible value. Complicated. 462 | pu_def.replace_line('{}{}{} := {} * NOT AFRTEXP'.format('AST', num, 'PT', 463 | do_time), 464 | pu_def.comment) 465 | 466 | do_def.replace_line('{}{}{} := {}'.format('AST', num, 'R', 467 | in_val), 468 | do_def.comment) 469 | in_def.replace_line('{}{}{} := {}'.format('AST', num, 'IN', 470 | 'NOT' + '(' + in_val + ')'), 471 | in_def.comment) 472 | self.l.replace(q, 'NOT' + ' (' + 'AST' + num + 'Q' + ')') 473 | 474 | """ 475 | We now end up with expressions like: 476 | F_TRIG NOT(AST18Q) or R_TRIG NOT(AST18Q) 477 | which is invalid and we need another ASV allocation so that after AST18IN we have: 478 | ASVxx := NOT(ASTQ18Q) 479 | which we then replace 480 | F_TRIG ASVxx and R_TRIG ASVxx 481 | """ 482 | AST_DROPOFF_NEW_LINE_REQUIRED_REGEX = re.compile(r'((?:R|F)_TRIG )NOT \(? ?(' + 'AST' + str(num) + 'Q)(' + r')\)?') 483 | new_lines_required = self.l.find(AST_DROPOFF_NEW_LINE_REQUIRED_REGEX) 484 | 485 | if new_lines_required: 486 | next_asv = self.l.getNextVar('ASV', min=asv_min) 487 | self.l.insertLine(in_def.getLineNum()+1, next_asv[0] + ' := ' + 'NOT' + ' ' + 'AST' + str(num) + 'Q') 488 | for l in new_lines_required: 489 | if next_asv: 490 | #print(l) 491 | l.replace_line(re.sub(AST_DROPOFF_NEW_LINE_REQUIRED_REGEX, r'\1' + next_asv[0] + r'\3', l.raw_text), 492 | l.comment) 493 | else: 494 | print(ERR_START + 495 | 'No more ASVs available :-(') 496 | 497 | 498 | 499 | def reorder_type(self, type, startNum): 500 | """ 501 | e.g. PSV, 1, [5] 502 | will reorder PSV01, 02, 03, 04, 06, 07 ... 503 | and return a dict of substitutions 504 | could add excludes 505 | l.change_type('PLT', 'a') # PLT PLT15 PLT05-15 PLT5-15 result output 506 | l.change_type('PSV', 'a') # PSV PLT15 PLT05-15 PLT5-15 result output 507 | l.change_type('PMV', 'a') # PMV PLT15 PLT05-15 PLT5-15 result output 508 | """ 509 | 510 | things_to_replace = list(unique_everseen(self.l.getTypeDefinitions(type)[0])) 511 | if things_to_replace == None: 512 | print(ERR_START + 513 | 'Nothing to replace in reorder_type for {}.'.format(type) + 514 | ERR_END) 515 | new_things = self.l.getNextVar(type, min=startNum, max=None, 516 | qty=len(things_to_replace), 517 | skipUsed=False) 518 | 519 | from_to = zip(things_to_replace, new_things) 520 | 521 | self.history.append(list(zip(things_to_replace, new_things))) 522 | 523 | replacement_dict = {} 524 | 525 | for k in from_to: 526 | 527 | from_val = k[0] 528 | to_val = k[1] 529 | from_val_e = sel_logic_functions.getInstVals(from_val) 530 | to_val_e = sel_logic_functions.getInstVals(to_val) 531 | 532 | individual_replacements = zip(from_val_e, to_val_e) 533 | for ir in individual_replacements: 534 | replacement_dict[ir[0]] = ir[1] 535 | 536 | self.l.multireplace(replacement_dict) 537 | self.l.update_aliases(list(replacement_dict.items())) 538 | 539 | def substitute_aliases(d): 540 | # accepts a dict 541 | pass 542 | 543 | def __str__(self): 544 | return str(self.l) 545 | 546 | def update_svg(a, b, h): 547 | 548 | new_dict = {} 549 | for k1, v1 in a.items(): 550 | for k2, v2 in b.items(): 551 | if v1 == v2: 552 | new_dict[k1] = k2 553 | 554 | h.append(new_dict) 555 | 556 | #xml = ET.parse('/media/mulhollandd/KINGSTON/standard-designs/transformer-protection/SEL487E-3_Transformer_Protection_Settings/setting_guide/media/autoreclose_logic_diagram.svg') 557 | xml = ET.parse(r'F:\standard-designs\transformer-protection\SEL487E-3_Transformer_Protection_Settings\setting_guide\media\autoreclose_logic_diagram.svg') 558 | 559 | svg = xml.getroot() 560 | a = svg.findall(".//{http://www.w3.org/2000/svg}tspan") 561 | 562 | for k in a: 563 | the_text = str(k.text) 564 | for thing in h: 565 | the_dict = dict(thing) 566 | the_text = helpers.multireplace(the_text, the_dict) 567 | if k.text != None and the_text != None: 568 | k.text = the_text 569 | 570 | out = ET.tostring(svg, encoding='utf-8', pretty_print=True) 571 | 572 | with open('test.svg', 'wb') as f: 573 | f.write(out) 574 | 575 | l = LogicManipulator(logic, aliases) 576 | 577 | first_aliases = l.l.aliases 578 | 579 | print(l.l.pretty_print()) 580 | 581 | l.change_type('PLT', 'a') # Change to automation 582 | l.change_type('PSV', 'a') # Change to automation 583 | l.change_type('PMV', 'a') # Change to automation 584 | l.convert_timers('PCT16-23', 'PCT', 'AST', asv_min=30) # Convert DO and PU only timers 585 | 586 | print(l.l.pretty_print()) 587 | 588 | 589 | l.reorder_type('ALT', 10) # Reordering, minimum = 10 for the future! 590 | l.reorder_type('ASV', 30) # Reordering, minimum = 30 for the future! 591 | l.reorder_type('AMV', 30) # Reordering, minimum = 30 for the future! 592 | l.reorder_type('AST', 10) # Reordering, minimum = 10 for the future! 593 | 594 | l.l.add_alias('ASV040','C79CLHV','ARecl Close of HV CB') 595 | l.l.add_alias('ASV041','C79CLLV','ARecl Close of LV CB') 596 | 597 | print(l.l.pretty_print(withAliases=True)) # pretty print 598 | 599 | print(l.l) 600 | 601 | second_aliases = l.l.aliases 602 | 603 | update_svg(first_aliases, second_aliases, l.history) 604 | 605 | 606 | 607 | 608 | 609 | """ 610 | d = Differ() 611 | old_text = str(l.l).split('\n') 612 | #l.convert_timer('PCT20', 'PCT', 'AST') 613 | #l.convert_timer('PCT18', 'PCT', 'AST', asv_min=30) 614 | 615 | new_text = str(l.l).split('\n') 616 | result = list(d.compare(old_text, new_text)) 617 | result = [l for l in result if l[0:1] in '-+'] 618 | from pprint import pprint 619 | pprint(result) 620 | 621 | print(l.l.pretty_print()) 622 | """ 623 | 624 | # TODO: Used logic should be able to be based on definitions only to distinguish protection and automation logic 625 | 626 | 627 | 628 | 629 | -------------------------------------------------------------------------------- /rdb_section_extract.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | 5 | This tool extracts a pile of settings based on the hierachy of Quickset 6 | 7 | """ 8 | 9 | import collections 10 | import os 11 | import re 12 | 13 | import olefile 14 | 15 | import sel_logic_count 16 | 17 | LINE_INFO = ['Lines Used (w/ comment lines)', 'Lines Used (w/o comment lines)'] 18 | 19 | LOGIC_INFO = [ 'PSV', 'PMV', 'PLT', 'PCT', 'PST', 'PCN', 20 | 'ASV', 'AMV', 'ALT', 'AST', 'ACN'] 21 | 22 | TOTAL_SEL_PROTECTION_LINES = 250 23 | TOTAL_SEL_AUTOMATION_LINES = 1000 24 | 25 | 26 | 27 | # this probably needs to be expanded 28 | SEL_FILES_TO_GROUP = { 29 | 'G': ['SET_G1'], 30 | 'G1': ['SET_S1.TXT', 'SET_L1.TXT', 'SET_1.TXT'], # Groups 31 | 'G2': ['SET_S2.TXT', 'SET_L2.TXT', 'SET_2.TXT'], 32 | 'G3': ['SET_S3.TXT', 'SET_L3.TXT', 'SET_3.TXT'], 33 | 'G4': ['SET_S4.TXT', 'SET_L4.TXT', 'SET_4.TXT'], 34 | 'G5': ['SET_S5.TXT', 'SET_L5.TXT', 'SET_5.TXT'], 35 | 'G6': ['SET_S6.TXT', 'SET_L6.TXT', 'SET_6.TXT'], 36 | 37 | 'P1': ['SET_P1.TXT'], # Ports 38 | 'P2': ['SET_P2.TXT'], 39 | 'P3': ['SET_P3.TXT'], 40 | 'P5': ['SET_P5.TXT'], 41 | 'PF': ['SET_PF.TXT'], # Front Port 42 | 'P87': ['SET_P87.TXT'], # Differential Port Settings 43 | 44 | 'A1': ['SET_A1.TXT'], # Automation 45 | 'A2': ['SET_A2.TXT'], 46 | 'A3': ['SET_A3.TXT'], 47 | 'A4': ['SET_A4.TXT'], 48 | 'A5': ['SET_A5.TXT'], 49 | 'A6': ['SET_A6.TXT'], 50 | 'A7': ['SET_A7.TXT'], 51 | 'A8': ['SET_A8.TXT'], 52 | 'A9': ['SET_A9.TXT'], 53 | 'A10': ['SET_A10.TXT'], 54 | 55 | 'L1': ['SET_L1.TXT'], # Protection Logic 56 | 'L2': ['SET_L2.TXT'], 57 | 'L3': ['SET_L3.TXT'], 58 | 'L4': ['SET_L4.TXT'], 59 | 'L5': ['SET_L5.TXT'], 60 | 'L6': ['SET_L6.TXT'], 61 | 'L7': ['SET_L7.TXT'], 62 | 'L8': ['SET_L8.TXT'], 63 | 'L9': ['SET_L9.TXT'], 64 | 65 | 66 | 'B1': ['SET_B1.TXT'], # Bay Control information 67 | 68 | 'D1': ['SET_D1.TXT'], # DNP 69 | 'D2': ['SET_D2.TXT'], 70 | 'D3': ['SET_D3.TXT'], 71 | 'D4': ['SET_D4.TXT'], 72 | 'D5': ['SET_D5.TXT'], 73 | 74 | 'F1': ['SET_F1.TXT'], # Front Panel 75 | 'M1': ['SET_M1.TXT'], # CB Monitoring 76 | 'N1': ['SET_N1.TXT'], # Notes 77 | 'O1': ['SET_O1.TXT'], # Outputs 78 | 'R1': ['SET_R1.TXT'], # SER 79 | 'T1': ['SET_R1.TXT'], # Aliases 80 | 81 | } 82 | 83 | def process_file(filepath, args, settingsName=None): 84 | rdb_info = get_ole_data(filepath, settingsName=settingsName) 85 | return extract_parameters(filepath, rdb_info, args) 86 | 87 | def get_ole_data(filepath,settingsName=None): 88 | data = [] 89 | listdir = [] 90 | try: 91 | ole = olefile.OleFileIO(filepath) 92 | listdir = ole.listdir() 93 | if settingsName: 94 | listdir = [l for l in listdir if l[1]==settingsName] 95 | for direntry in listdir: 96 | data.append([direntry, ole.openstream(direntry).getvalue()]) 97 | except: 98 | print('Failed to read streams in file: ' + filepath) 99 | return data 100 | 101 | def extract_parameters(filepath, rdb_info, txtfile): 102 | fn = os.path.basename(filepath) 103 | parameter_info=[] 104 | 105 | for stream in rdb_info: 106 | settings_name = str(stream[0][1]) 107 | stream_name = str(stream[0][-1]).upper() 108 | if stream_name in SEL_FILES_TO_GROUP[txtfile]: 109 | return [settings_name, stream[1].decode('utf-8')] 110 | 111 | def get_sel_setting(text): 112 | setting_expression = re.compile(r'^([A-Z0-9_]+),\"(.*)\"(?:\r\n|\x1c\r\n)', flags=re.MULTILINE) 113 | return re.findall(setting_expression, text) 114 | 115 | def format_logic(d): 116 | # get logic report 117 | if isinstance(d, str): 118 | raw_results = collections.OrderedDict() 119 | for k, v in d.items(): 120 | raw_results[k] = sel_logic_count.calc_usage_raw(v) 121 | return raw_results 122 | else: 123 | return d 124 | 125 | def make_table_data(raw_results): 126 | table_data = [] 127 | 128 | for row_name in LINE_INFO + LOGIC_INFO: 129 | table_row = [row_name] 130 | for k, v in raw_results.items(): 131 | if row_name in v: 132 | table_row.append(v[row_name]) 133 | table_data.append(table_row) 134 | 135 | return table_data 136 | 137 | def sum_logic_usage_multiple_groups(d, group_title='Group', settings_name=None, automation=None, total=None): 138 | """ 139 | d is a dictionary with the group number as the key 140 | and the protection logic as the values 141 | This is processed and an Asciidoc table is produced 142 | """ 143 | 144 | columns = 3*len(d) + 1 145 | 146 | # get logic report 147 | table_data = make_table_data(format_logic(d)) 148 | 149 | no_groups = len(d) 150 | info = [] 151 | 152 | # Anchor 153 | info.append('[#overall_logic_usage]') 154 | 155 | # Title 156 | if settings_name: 157 | keys = ', '.join([str(ky)[1:2] for ky in d.keys()]) 158 | info.append('.`{}` Logic Usage in Setting Groups {}'.format(settings_name.upper(), keys)) 159 | 160 | # Column Definitions 161 | info.append('[cols="1*<.^,{}"]'.format(','.join(['1*>.^,1*^.^,1*>.^'] * no_groups))) 162 | 163 | info.append('|===') 164 | 165 | # Group Title 166 | info.append('h|') 167 | for group in d.keys(): 168 | info.append('3+^.^h| '.format(no_groups) + 169 | '{} {}'.format(group_title, group[1:])) 170 | 171 | info.append('') 172 | info.append(str(columns)+'+^.^h| Protection Usage') 173 | info.append('') 174 | 175 | # Overall line information 176 | for k in table_data: 177 | if k[0] in LINE_INFO: 178 | pr = ('h| {}').format(k[0]).ljust(50) 179 | for gd in k[1:]: 180 | pr += '3+^.^| {} / {} '.format(gd, TOTAL_SEL_PROTECTION_LINES).ljust(20) 181 | info.append(pr) 182 | 183 | # Capacity free from relay STA S command 184 | sta_s_info = ['Free protection settings capacity (%)', 'Free protection execution capacity (%)'] 185 | 186 | for s in sta_s_info: 187 | pr = ('h| {} ').format(s).ljust(50) 188 | for gd in range(no_groups): 189 | pr += '3+^.^| #??# '.ljust(20) 190 | info.append(pr) 191 | 192 | info.append('') 193 | if d and not total: 194 | info.append(str(columns)+'+^.^h| Variable Usage for Protection Logic') 195 | elif total and automation: 196 | info.append(str(columns)+'+^.^h| Variable Usage for Protection and Automation Logic') 197 | info.append('') 198 | 199 | info.append('h| Variable ' + 200 | ' '.join(['h| Used h| Free % h| Available']*no_groups)) 201 | info.append('') 202 | 203 | if total: 204 | table_data = make_table_data(format_logic(total)) 205 | 206 | for k in table_data: 207 | if k[0] in LOGIC_INFO: 208 | pr = ('h| `{}`'.format(k[0])).ljust(13) 209 | for gd in k[1:]: 210 | fstr = '| {:>12} | {:<7.0%} | {:<30}' 211 | pr += fstr.format('{} / {}'.format(gd['qty'], gd['total']), 212 | gd['free_pu'], 213 | '[small]#{}#'.format(gd['available_detail'])) 214 | info.append(pr) 215 | 216 | if automation: 217 | info.append('') 218 | info.append(str(columns)+'+^.^h| Automation Usage') 219 | info.append('') 220 | 221 | # Group Title 222 | info.append('h|') 223 | for group in d.keys(): 224 | info.append('3+^.^h| '.format(no_groups) + 225 | '{} {}'.format(group_title, group[1:])) 226 | 227 | questions = ['3+^.^| #??# '] * no_groups 228 | 229 | info.append('{:<50} {}'.format('h| Free automation settings storage capacity (%)', ''.join(questions))) 230 | info.append('{:<50} {}'.format('h| Free automation execution availability (%)', ''.join(questions))) 231 | info.append('{:<50} {}'.format('h| Automation peak execution cycle time (ms)', ''.join(questions))) 232 | info.append('{:<50} {}'.format('h| Automation average execution cycle time (ms)', ''.join(questions))) 233 | 234 | table_data = make_table_data(format_logic(automation)) 235 | 236 | # Overall line information 237 | for k in table_data: 238 | if k[0] in LINE_INFO: 239 | pr = ('h| {} ').format(k[0]).ljust(51) 240 | for gd in k[1:]: 241 | pr += str(no_groups * 3) + '+^.^| {} / {} '.format(gd, TOTAL_SEL_AUTOMATION_LINES).ljust(20) 242 | info.append(pr) 243 | 244 | info.append('|===') 245 | 246 | return('\n'.join(info)) 247 | 248 | def get_logic(filepath, *names, settingsName=None): 249 | logics = {} 250 | for name in names: 251 | [settings_name, output] = process_file(filepath, name, settingsName) 252 | lines = get_sel_setting(output) 253 | result = [] 254 | for settings in lines: 255 | result.append(settings[1]) 256 | logic_text = "\n".join(result) 257 | logics[name] = logic_text 258 | return logics 259 | 260 | def get_logic_total(path, groups, includeAutomation=True, settings_name=None): 261 | # get logic for number of protection 262 | groups_new = ['L' + str(g) for g in groups] 263 | protection = get_logic(path, *groups_new, settingsName=settings_name) 264 | 265 | automation_arr = [] 266 | if includeAutomation: 267 | for block in range(1,10+1): 268 | #print(get_logic(path, 'A' + str(block))) 269 | automation_arr.append(get_logic(path, 'A' + str(block), settingsName=settings_name)['A' + str(block)]) 270 | automation = '\n'.join(automation_arr) 271 | return [protection, automation] 272 | 273 | return [protection] 274 | 275 | def plogic_used(filepath, group_prefix, settings_name, *nums): 276 | 277 | logics = get_logic(filepath, *nums) 278 | 279 | if len(nums) == 1: 280 | return sel_logic_count.calc_logic_usage(logics[nums[0]]) 281 | else: 282 | return sum_logic_usage_multiple_groups(logics, group_prefix, settings_name) 283 | 284 | def pa_logic_used(filepath, group_prefix, settings_name, *nums): 285 | 286 | logics = get_logic_total(filepath, nums, includeAutomation=True, settings_name=settings_name) 287 | LINES = ['Lines Used (w/ comment lines)', 'Lines Used (w/o comment lines)'] 288 | 289 | automation = sel_logic_count.calc_usage_raw(logics[1]) 290 | automation = {k:v for (k,v) in automation.items() if k in LINES} 291 | automation = {'A': automation} 292 | 293 | protection = {} 294 | total = {} 295 | 296 | for group in nums: 297 | # print(group) 298 | pg = sel_logic_count.calc_usage_raw(logics[0]['L' + str(group)]) 299 | protection['L' + str(group)] = {k:v for (k,v) in pg.items() if k in LINES} 300 | 301 | tg = sel_logic_count.calc_usage_raw(logics[0]['L' + str(group)] + '\n' + logics[1]) 302 | total['L' + str(group)] = {k:v for (k,v) in tg.items() if k not in LINES} 303 | 304 | #print('p',protection, 'a', automation, 't', total) 305 | print(sum_logic_usage_multiple_groups(protection, group_prefix, settings_name, automation, total)) 306 | 307 | """ 308 | if len(nums) == 1: 309 | return sel_logic_count.calc_logic_usage(logics[nums[0]]) 310 | else: 311 | return sum_logic_usage_multiple_groups(logics, group_prefix, settings_name) 312 | """ 313 | 314 | 315 | if __name__ == '__main__': 316 | 317 | """ 318 | path = r'F:\standard-designs\transformer-protection\SEL487E-3_Transformer_Protection_Settings\settings\SEL-487E-3.rdb' 319 | output = process_file(path, 'F1') 320 | 321 | 322 | k = get_sel_setting(output) 323 | result = [] 324 | 325 | 326 | for item in k: 327 | val = item[1] 328 | cnt = sel_logic_count.countElementsUsed(val) 329 | result.append(('{: >3}').format(str(cnt)) + ' | ' + item[0] + ' ::= ' + val) 330 | 331 | result = sorted(result, key=lambda x: int((x.split('|'))[0].strip())) 332 | 333 | print(result) 334 | 335 | for k in result: 336 | # print('x', k) 337 | print(int((k.split('|'))[0].strip()), k) 338 | 339 | """ 340 | 341 | """output = process_file('/media/mulhollandd/KINGSTON/standard-designs/transformer-protection/SEL487E-3_Transformer_Protection_Settings/settings/SEL-487E-3.rdb', 'L1') 342 | 343 | #k = get_stream_parameter('',output) 344 | k = get_sel_setting(output) 345 | result = [] 346 | for val in k: 347 | result.append(val[1]) 348 | logic_text = "\n".join(result) 349 | 350 | print(sel_logic_count.calc_logic_usage(logic_text))""" 351 | 352 | #plogic_used('/home/mulhollandd/Downloads/SEL487E-3_Transformer_Protection_Settings_v14Aug2017.000.002/settings/SEL-487E-3.rdb', 1) 353 | 354 | 355 | #path = '/media/mulhollandd/KINGSTON/standard-designs/transformer-protection/SEL487E-3_Transformer_Protection_Settings/settings/SEL-487E-3.rdb' 356 | #path = r'G:\standard-designs\transformer-protection\SEL487E-3_Transformer_Protection_Settings\settings\SEL-487E-3.rdb' 357 | path = r'F:\standard-designs\transformer-protection\SEL487E-3_Transformer_Protection_Settings\settings\SEL-487E-3.rdb' 358 | path = r'/media/mulhollandd/KINGSTON/standard-designs/capacitor-protection/SEL487E-3_Capacitor_Protection_Settings/settings/SEL-487E-3.rdb' 359 | #path = '/home/mulhollandd/Downloads/junk/SEL-487E-3.rdb' 360 | #print(plogic_used(path, 'Application', 1, 2)) 361 | 362 | 363 | #print(get_logic_total(path, [1,2])) 364 | 365 | 366 | #print(pa_logic_used(path, 'Application', 1, 2)) 367 | #print(plogic_used(path, 'Application', 'Blah', 'L1', 'L2')) 368 | pa_logic_used(path, 'Application', 'TYP123_DStarNE', '1') 369 | 370 | #output = process_file(path, 'R1') 371 | #print(output) 372 | 373 | 374 | 375 | 376 | #print(output) 377 | """ 378 | ser_points_and_aliases = {} 379 | for counter in range(1, 250+1): 380 | num = str(counter) 381 | #SITM70,"TRIPT"\x1c\r\nSNAME70,"TRIPT"\x1c\r\nSSET70,"Asserted"\x1c\r\nSCLR70,"Deasserted"\x1c\r\nSHMI70,"N" 382 | match = re.compile(r'SITM' + num + r',"([A-Z0-9_]*)"\x1c\r\nSNAME' + num + r',"([A-Za-z0-9_]+)*"\x1c\r\nSSET' + num + ',"(.*)"\x1c\r\nSCLR'+ num + ',"(.*)"\x1c\r\nSHMI' + num + r',"([A-Z0-9_]+)*"', flags=re.MULTILINE) 383 | result = match.findall('\n'.join(output)) 384 | rwb = result[0][0] 385 | aliases = result[0][1] 386 | alias_set = result[0][2] 387 | alias_clear = result[0][3] 388 | hmi_alarm = result[0][4] 389 | 390 | ser_points_and_aliases[rwb] = [aliases, alias_set, alias_clear, hmi_alarm] 391 | 392 | print(rwb, [aliases, alias_set, alias_clear, hmi_alarm]) 393 | 394 | 395 | output = process_file(path, 'P1') 396 | 397 | protection = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6'] 398 | automation = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10'] 399 | 400 | for logic in protection + automation 401 | output = process_file(path, 'P1') 402 | output = process_file(path, 'P1') 403 | output = process_file(path, 'P1') 404 | output = process_file(path, 'P1') 405 | output = process_file(path, 'P1') 406 | """ 407 | 408 | 409 | #for k in output: 410 | # print(k) 411 | 412 | # SITM248,"PST07Q" SNAME248,"PST07Q" SSET248,"Asserted" SCLR248,"Deasserted" SHMI248,"N" 413 | # 414 | 415 | # tool to remove protection and automation aliases which are unused. 416 | 417 | 418 | -------------------------------------------------------------------------------- /rdbextract.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | rdbextract.py 5 | A tool to browse SEL AcSELerator Quickset RDB files to extract parameter 6 | information intended for bulk processing. 7 | 8 | Usage defined by running with option -h. 9 | 10 | This tool can be run from the IDLE prompt using the start def, e.g. 11 | start('-h') or start('start.rdb G1:50P1P') 12 | 13 | Installation instructions (for Python 3): 14 | - pip install tablib olefile 15 | 16 | TODO: 17 | - include settings group which parameter is used in: 18 | code like this could be used: 19 | print "used in: " 20 | print re.findall('^' + SEL_SETTING_NAME + ",\"" + 21 | SEL_EXPRESSION + sys.argv[1] + SEL_EXPRESSION 22 | + "\"" + SEL_SETTING_EOL, 23 | rdat, flags=re.MULTILINE) 24 | - sorting options on display and dump output? 25 | - sort out guessing of Transpower standard design version 26 | - sort out handling of protection and automation logic in 400 series 27 | """ 28 | 29 | import sys 30 | import os 31 | import argparse 32 | import fnmatch 33 | import glob 34 | import re 35 | 36 | from itertools import zip_longest 37 | from pathlib import Path 38 | 39 | import tablib 40 | import olefile 41 | 42 | from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE 43 | 44 | __version__ = "GratefulDead" 45 | 46 | RDB_EXTENSION = 'RDB' 47 | BASE_PATH = os.path.dirname(os.path.realpath(__file__)) 48 | PARAMETER_SEPARATOR = ':' 49 | 50 | SEL_EXPRESSION = r'[\w :+/\\()!,.\-_\\*#]*' 51 | SEL_SETTING_EOL = r'\x1c\r\n' 52 | # these seem to be the options 53 | # this needs to be verified 54 | SEL_SETTING_EOL = r'(\r\n|\x1c\r\n)' 55 | # presently using this as covers all cases. 56 | # this seems to work differently to others: SEL-421-4 LineProt Std Rev01.rdb 57 | SEL_SETTING_EOL = r'' 58 | SEL_SETTING_NAME = r'[\w _]*' 59 | SEL_FID_EXPRESSION='^FID=([\w :+/\\()!,.\-_\\*]{10,})\r\n' 60 | SEL_PARTNO_EXPRESSION='^PARTNO=([\w :+/\\()!,.\-_\\*]{10,})\r\n' 61 | SEL_BFID_EXPRESSION='^BFID=([\w :+/\\()!,.\-_\\*]{10,})\r\n' 62 | 63 | OUTPUT_FILE_NAME = "output" 64 | NOT_FOUND = 'Not Found' 65 | 66 | # this probably needs to be expanded 67 | SEL_FILES_TO_GROUP = { 68 | 'G': ['SET_G1'], 69 | 'G1': ['SET_S1.TXT', 'SET_L1.TXT', 'SET_1.TXT'], # Groups 70 | 'G2': ['SET_S2.TXT', 'SET_L2.TXT', 'SET_2.TXT'], 71 | 'G3': ['SET_S3.TXT', 'SET_L3.TXT', 'SET_3.TXT'], 72 | 'G4': ['SET_S4.TXT', 'SET_L4.TXT', 'SET_4.TXT'], 73 | 'G5': ['SET_S5.TXT', 'SET_L5.TXT', 'SET_5.TXT'], 74 | 'G6': ['SET_S6.TXT', 'SET_L6.TXT', 'SET_6.TXT'], 75 | 76 | 'P1': ['SET_P1.TXT'], # Ports 77 | 'P2': ['SET_P2.TXT'], 78 | 'P3': ['SET_P3.TXT'], 79 | 'P5': ['SET_P5.TXT'], 80 | 'PF': ['SET_PF.TXT'], # Front Port 81 | 'P87': ['SET_P87.TXT'], # Differential Port Settings 82 | 83 | 'A1': ['SET_A1.TXT'], # Automation 84 | 'A2': ['SET_A2.TXT'], 85 | 'A3': ['SET_A3.TXT'], 86 | 'A4': ['SET_A4.TXT'], 87 | 'A5': ['SET_A5.TXT'], 88 | 'A6': ['SET_A6.TXT'], 89 | 'A7': ['SET_A7.TXT'], 90 | 'A8': ['SET_A8.TXT'], 91 | 'A9': ['SET_A9.TXT'], 92 | 'A10': ['SET_A10.TXT'], 93 | 94 | 'L1': ['SET_L1.TXT'], # Protection Logic 95 | 'L2': ['SET_L2.TXT'], 96 | 'L3': ['SET_L3.TXT'], 97 | 'L4': ['SET_L4.TXT'], 98 | 'L5': ['SET_L5.TXT'], 99 | 'L6': ['SET_L6.TXT'], 100 | 'L7': ['SET_L7.TXT'], 101 | 'L8': ['SET_L8.TXT'], 102 | 'L9': ['SET_L9.TXT'], 103 | 104 | 105 | 'B1': ['SET_B1.TXT'], # Bay Control information 106 | 107 | 'D1': ['SET_D1.TXT'], # DNP 108 | 'D2': ['SET_D2.TXT'], 109 | 'D3': ['SET_D3.TXT'], 110 | 'D4': ['SET_D4.TXT'], 111 | 'D5': ['SET_D5.TXT'], 112 | 113 | 'F1': ['SET_F1.TXT'], # Front Panel 114 | 'M1': ['SET_M1.TXT'], # CB Monitoring 115 | 'N1': ['SET_N1.TXT'], # Notes 116 | 'O1': ['SET_O1.TXT'], # Outputs 117 | 'R1': ['SET_R1.TXT'], # SER 118 | 'T1': ['SET_R1.TXT'], # Aliases 119 | 120 | } 121 | 122 | OUTPUT_HEADERS = ['RDB File','Name','Setting File','Setting Name','Val'] 123 | 124 | def main(arg=None): 125 | parser = argparse.ArgumentParser( 126 | description='Process individual or multiple RDB files and produce summary'\ 127 | ' of results as a csv or xls file.', 128 | epilog='Enjoy. Bug reports and feature requests welcome. Feel free to build a GUI :-)', 129 | prefix_chars='-/') 130 | 131 | parser.add_argument('-o', choices=['csv','xlsx'], 132 | help='Produce output as either comma separated values (csv) or as'\ 133 | ' a Micro$oft Excel .xls spreadsheet. If no output provided then'\ 134 | ' output is to the screen.') 135 | # ' '.join(opts.dmp) 1 136 | parser.add_argument('path', metavar='PATH|FILE', nargs='+', 137 | help='Go recursively go through path PATH. Redundant if FILE'\ 138 | ' with extension .rdb is used. When recursively called, only'\ 139 | ' searches for files with:' + RDB_EXTENSION + '. Globbing is'\ 140 | ' allowed with the * and ? characters.') 141 | 142 | parser.add_argument('-c', '--console', action="store_true", 143 | help='Show output to console') 144 | 145 | # Not implemented yet 146 | #parser.add_argument('-d', '--design', action="store_true", 147 | # help='Attempt to determine Transpower standard design version and' \ 148 | # ' include this information in output') 149 | 150 | parser.add_argument('-s', '--settings', metavar='G:S', type=str, nargs='+', 151 | help='Settings in the form of G:S where G is the group'\ 152 | ' and S is the SEL variable name. If G: is omitted the search' \ 153 | ' goes through all groups. Otherwise G should be the '\ 154 | ' group of interest. S should be the setting name ' \ 155 | ' e.g. OUT201.' \ 156 | ' Examples: G1:50P1P or G2:50P1P or 50P1P' \ 157 | ' '\ 158 | ' You can also get port settings using P:S' 159 | ' Note: Applying a group for a non-grouped setting is unnecessary'\ 160 | ' and will prevent you from receiving results.'\ 161 | ' '\ 162 | ' Special arguments include: FID') 163 | 164 | parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__) 165 | 166 | if arg == None: 167 | args = parser.parse_args() 168 | else: 169 | args = parser.parse_args(arg.split()) 170 | 171 | files_to_do = return_file_paths([' '.join(args.path)], RDB_EXTENSION) 172 | 173 | if files_to_do != []: 174 | process_rdb_files(files_to_do, args) 175 | else: 176 | print('Found nothing to do for path: ' + args.path[0]) 177 | sys.exit() 178 | os.system("Pause") 179 | 180 | def findfiles(which, where='.'): 181 | '''Returns list of filenames from `where` path matched by 'which' 182 | shell pattern. Matching is case-insensitive.''' 183 | 184 | # TODO: recursive param with walk() filtering 185 | rule = re.compile(fnmatch.translate(which), re.IGNORECASE) 186 | return [os.path.join(where, name) for name in os.listdir(where) if rule.match(name)] 187 | 188 | def return_file_paths(args_path, file_extension): 189 | 190 | return findfiles('*.' + file_extension.lower(), args_path[0].replace('"', '')) 191 | 192 | """fpath = args_path[0].replace(r'"', '') 193 | 194 | if fpath == ".": 195 | files = list(Path('.').glob('**/*.' + file_extension.lower())) 196 | 197 | else: 198 | files = Path(fpath).resolve().glob('*.rdb') 199 | 200 | return [str(f) for f in files] 201 | """ 202 | 203 | def grouper(iterable, n, fillvalue=None): 204 | args = [iter(iterable)] * n 205 | return zip_longest(*args, fillvalue=fillvalue) 206 | 207 | def process_rdb_files(files_to_do, args): 208 | parameter_info = [] 209 | 210 | for filename in files_to_do: 211 | # print filename 212 | rdb_info = get_ole_data(filename) 213 | new_data = extract_parameters(filename, rdb_info, args) 214 | parameter_info += new_data 215 | 216 | data = tablib.Dataset(headers=['filename'] + args.settings) 217 | 218 | # group output data by parameter 219 | grouped = grouper(parameter_info, len(args.settings), '') 220 | 221 | for file_data in grouped: 222 | data.append([file_data[0][0]] + [k[-1] for k in file_data]) 223 | 224 | # don't overwrite existing file 225 | name = OUTPUT_FILE_NAME 226 | if args.o == 'csv' or args.o == 'xlsx': 227 | # this is stupid and klunky but hey 228 | while os.path.exists(name + '.csv') or os.path.exists(name + '.xlsx'): 229 | name += '_' 230 | 231 | # write data 232 | if args.o == None: 233 | pass 234 | elif args.o == 'csv': 235 | with open(name + '.csv', 'wb') as output: 236 | output.write(data.csv) 237 | elif args.o == 'xlsx': 238 | with open(name + '.xlsx', 'wb') as output: 239 | output.write(data.xlsx) 240 | 241 | if args.console == True: 242 | display_info(parameter_info) 243 | 244 | def get_ole_data(filename): 245 | data = [] 246 | try: 247 | ole = olefile.OleFileIO(filename) 248 | listdir = ole.listdir() 249 | for direntry in listdir: 250 | data.append([direntry, ole.openstream(direntry).getvalue()]) 251 | except: 252 | print('Failed to read streams in file: ' + filename) 253 | return data 254 | 255 | def fix_string(text): 256 | return re.sub(ILLEGAL_CHARACTERS_RE, '', text) 257 | 258 | def extract_parameters(filename, rdb_info, args): 259 | fn = os.path.basename(filename) 260 | parameter_info=[] 261 | 262 | parameter_list = [] 263 | for k in args.settings: 264 | parameter_list.append(k.replace(r'"', '')) #.translate(None, '\"')) 265 | 266 | # iterate across all parameters the user specified 267 | for parameter in parameter_list: 268 | category_file_list = None 269 | search_parameter = '' 270 | 271 | # is it a parameter associated wtih a group? 272 | if parameter.find(PARAMETER_SEPARATOR) != -1: 273 | category_file_list = \ 274 | SEL_FILES_TO_GROUP[(parameter.split(PARAMETER_SEPARATOR))[0]] 275 | search_parameter = parameter.split(PARAMETER_SEPARATOR)[1] 276 | else: 277 | search_parameter = parameter 278 | 279 | # print search_parameter 280 | # iterate over stream in rdb file 281 | for stream in rdb_info: 282 | return_value = [] 283 | settings_name = str(stream[0][1]) 284 | stream_name = str(stream[0][-1]).upper() 285 | 286 | # lookup for group to file to restrict examination 287 | # parameters are always: 288 | # Relays > Setting Name > Settings Files 289 | # so length is always at least 3 290 | if len(stream[0]) >= 3 and \ 291 | (category_file_list == None \ 292 | or stream[0][-1].upper() in category_file_list): 293 | 294 | if search_parameter == 'FID': 295 | try: 296 | return_value = extract_fid(stream[1].decode('ascii', errors="ignore")) 297 | except: 298 | return_value = "Unable to decode rdb file" 299 | elif search_parameter == 'BFID': 300 | try: 301 | return_value = extract_bfid(stream[1].decode('ascii', errors="ignore")) 302 | except: 303 | return_value = "Unable to decode rdb file" 304 | 305 | elif search_parameter == 'PARTNO': 306 | try: 307 | return_value = extract_partno(stream[1].decode('ascii', errors="ignore")) 308 | except: 309 | return_value = "Unable to decode rdb file" 310 | 311 | else: 312 | try: 313 | return_value = get_stream_parameter(search_parameter, 314 | stream[1].decode('ascii', errors="ignore")) 315 | except: 316 | return_value = "Unable to decode rdb file" 317 | 318 | if return_value != []: 319 | parameter_info.append([fn, settings_name, \ 320 | stream_name, search_parameter, return_value[0]]) 321 | break 322 | 323 | else: 324 | parameter_info.append([fn, 'NA',\ 325 | "N/A", search_parameter, NOT_FOUND]) 326 | 327 | return parameter_info 328 | 329 | def extract_fid(stream): 330 | # FIDs look like this for example: 331 | return re.findall(SEL_FID_EXPRESSION, \ 332 | stream, flags=re.MULTILINE) 333 | 334 | def extract_bfid(stream): 335 | # FIDs look like this for example: 336 | return re.findall(SEL_BFID_EXPRESSION, \ 337 | stream, flags=re.MULTILINE) 338 | 339 | def extract_partno(stream): 340 | # PARTNOs look like this for example: 341 | return re.findall(SEL_PARTNO_EXPRESSION, \ 342 | stream, flags=re.MULTILINE) 343 | 344 | 345 | def get_stream_parameter(parameter, stream): 346 | return re.findall('^' + parameter + \ 347 | ",\"(" + SEL_EXPRESSION + ")\"" + \ 348 | SEL_SETTING_EOL, \ 349 | stream, flags=re.MULTILINE) 350 | 351 | def display_info(parameter_info): 352 | lengths = [] 353 | # first pass to determine column widths: 354 | for line in parameter_info: 355 | for index,element in enumerate(line): 356 | try: 357 | lengths[index] = max(lengths[index], len(element)) 358 | except IndexError: 359 | lengths.append(len(element)) 360 | 361 | parameter_info.insert(0,OUTPUT_HEADERS) 362 | # now display in columns 363 | for line in parameter_info: 364 | display_line = '' 365 | for index,element in enumerate(line): 366 | display_line += element.ljust(lengths[index]+2,' ') 367 | print(display_line) 368 | 369 | if __name__ == '__main__': 370 | if len(sys.argv) == 1 : 371 | # main(r'-o xlsx --console "in\SEL-421-4 LineProt Std Rev01.rdb" --settings "RID TID G1:81D1P 81D1T 81D2P 81D2T TR FID"') 372 | # main(r'-o xlsx --console "in\SEL-421-4 LineProt Std Rev01.rdb" --settings "TR"') 373 | #main(r'-o xlsx "in/other/SEL-351S-6-R5 Standard Rev01 (2).rdb" --settings "RID TID SID G1:81D1P G1:81D1T 81D2P 81D2T TR FID"') 374 | # main(r'-o xlsx "W:\Education\Current\Stationware Dump\20150511\SI" --settings "RID TID SID G1:81D1P G1:81D1D 81D2P 81D2D TR FID"') 375 | # main(r'-o xlsx "in/other/SEL-351S-6-R5 Standard Rev01 (2).rdb" --settings "RID TID SID G1:81D1P G1:81D1T 81D2P 81D2T TR FID"') 376 | # main(r'-o xlsx "W:\Education\Current\Stationware Dump\20150511\SI" --settings "RID TID SID G1:81D1P G1:81D1D 81D2P 81D2D TR FID"') 377 | # main(r'-o xlsx "W:\Education\Current\Stationware Dump\20150511\" --settings "RID TID G1:51P1P G1:51P1TD G1:51P1C TR FID"') 378 | # W:\Education\Current\Stationware Dump 379 | #main(r'-o xlsx "/media/mulhollandd/KINGSTON/stationware/rdb" --settings "RID TID FID"') 380 | main(r'-o xlsx "/media/mulhollandd/KINGSTON/stationware/rdb" --settings "RID TID SID FID PARTNO BFID"') 381 | 382 | else: 383 | main() 384 | -------------------------------------------------------------------------------- /sel_logic_count.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | 5 | This tool simply counts logic used and spare logic and the number of elements used in a SELogic equation. 6 | 7 | """ 8 | import re 9 | 10 | from named_constants import Constants 11 | from helpers import flatten, unique, remove_empty 12 | from intervals import get_interval_range, provide_string_range 13 | 14 | class RDBOperatorsConst(Constants): 15 | """ 16 | Information on variable types for ""SEL-400 series logic and 17 | regexes for processing of equations 18 | """ 19 | TYPES = {'PSV': ['PSVxx'], 20 | 'PMV': ['PMVxx'], 21 | 'PLT': ['PLTxx$S$', 'PLTxx$R$', 'PLTxx'], 22 | 'PCT': ['PCTxx$IN$', 'PCTxx$PU$', 'PCTxx$DO$', 'PCTxx$Q$'], 23 | 'PST': ['PSTxx$IN$', 'PSTxx$PT$', 'PSTxx$R$', 'PSTxx$ET$', 'PSTxx$Q$'], 24 | 'PCN': ['PCNxx$IN$', 'PCNxx$PV$', 'PCNxx$R$', 'PCNxx$CV$', 'PCNxx$Q$'], 25 | 'ASV': ['ASVxxx'], 26 | 'AMV': ['AMVxxx'], 27 | 'ALT': ['ALTxx$S$', 'ALTxx$R$', 'ALTxx'], 28 | 'AST': ['ASTxx$IN$', 'ASTxx$PT$', 'ASTxx$R$', 'ASTxx$ET$', 'ASTxx$Q$'], 29 | 'ACN': ['ACNxx$IN$', 'ACNxx$PV$', 'ACNxx$R$', 'ACNxx$CV$', 'ACNxx$Q$'] 30 | } 31 | 32 | LIMITS = {'PSV': [1, 64], 33 | 'PMV': [1, 64], 34 | 'PLT': [1, 32], 35 | 'PCT': [1, 32], 36 | 'PST': [1, 32], 37 | 'PCN': [1, 32], 38 | 'ASV': [1, 256], 39 | 'AMV': [1, 256], 40 | 'ALT': [1, 32], 41 | 'AST': [1, 32], 42 | 'ACN': [1, 32] 43 | } 44 | 45 | COMMENTS = re.compile(r'#.*') 46 | OPERATORS = re.compile(r'\+|\*|/') 47 | BOOL_OPERATORS = re.compile(r'\<|\>') 48 | EQUALITY_CHECK = r'=' 49 | EQUATION_DEF = r'^\:\=$' 50 | NUMBERS = re.compile(r'^-?[0-9]*\.?[0-9]*$') 51 | LOGICAL_OPERATORS = re.compile(r'AND|NOT|OR|R_TRIG|F_TRIG') 52 | BRACKETS = re.compile(r'\(|\)') 53 | FUNCTIONS_RAW = ['ABS', 'ASIN', 'ACOS', 'CEIL', 'COS', 'EXP', 'FLOOR', 'LN', 'LOG', 'SIN', 'SQRT'] 54 | FUNCTIONS = re.compile('|'.join([x + r'\(' for x in FUNCTIONS_RAW])) 55 | 56 | def comp_sub(regex, list, replacement=''): 57 | """ carry out substitution on all elements in list """ 58 | return [re.sub(regex, replacement, p) for p in list] 59 | 60 | def comp_count(list, regex): 61 | """ combine array elements to a string and return counts of regex in the string """ 62 | input_data = ' '.join(list) 63 | result = re.findall(regex, input_data) 64 | return len(result) 65 | 66 | def getRawVariableFromTo(rdb_type): 67 | """ 68 | Take a value from RDBOperatorsConst.TYPES (e.g. PSV$xx$ or PCT$xx$PU) 69 | 1. Replace xx with regex to identify the number, PSV[0-9]{2} 70 | 2. Remove the $ signs any suffix (remove the $ in $xx$ portion) <1> 71 | 3. Remove the $xx$ portion entirely and replace with the first capture group. 72 | 73 | So given an input of PSV$xx$ this function returns: 74 | ['PSV([0-9]{2})', 'PSV\\1'] or 75 | ['PCT([0-9]{2})PU', 'PCT\\1'] 76 | 77 | """ 78 | number_regex = r'([0-9]{' + str(len(re.findall('x+', rdb_type)[0])) + '})' 79 | opWithRegex = re.sub('x+', number_regex, rdb_type) 80 | opWithRegexLessDollars = re.sub('\$', '', opWithRegex) 81 | # remove $xx$ portion 82 | opWithRegexLessDollarsBit = re.sub('\$.*\$', '', rdb_type) 83 | 84 | # replace with capture group 85 | newValue = re.sub('x+', r'\\1', opWithRegexLessDollarsBit) 86 | 87 | return [opWithRegexLessDollars, newValue] 88 | 89 | def getVariableRegex(variable_info): 90 | """ 91 | Given a variable in the form of PCT$xx$PU or PSV$xx$ return a regex of PSV([0-9]{2}) 92 | """ 93 | number_regex = r'([0-9]{' + str(len(re.findall('x+', variable_info)[0])) + '})' 94 | opWithRegex = re.sub('x+', number_regex, variable_info) 95 | # remove suffix or supplementary variable information 96 | opWithRegexLessDollarsBit = re.sub(r'\$.*\$', '', opWithRegex) 97 | return opWithRegexLessDollarsBit 98 | 99 | def removeComment(eqn): 100 | """ 101 | Remove comments from an equation received as a string 102 | """ 103 | return re.sub(RDBOperatorsConst.COMMENTS,'', eqn.strip()) 104 | 105 | def eqnTokenise(eqn): 106 | """ 107 | Tokenise an equation by splitting on whitespace 108 | """ 109 | return eqn.split(" ") 110 | 111 | def getEquationElements(eqnArr, keepNumbers=False, removeEmpty=True, uniqueOnly=True, sorted=True): 112 | """ 113 | Remove cruft from equations: 114 | * Remove operators 115 | * Remove equation definition 116 | * Remove logical operators 117 | * Remove equality checks 118 | * Optionally remove numbers 119 | """ 120 | 121 | notElements = [RDBOperatorsConst.OPERATORS, 122 | RDBOperatorsConst.EQUATION_DEF, 123 | RDBOperatorsConst.LOGICAL_OPERATORS, 124 | RDBOperatorsConst.BOOL_OPERATORS, 125 | RDBOperatorsConst.FUNCTIONS, 126 | RDBOperatorsConst.BRACKETS, 127 | RDBOperatorsConst.EQUALITY_CHECK] 128 | 129 | if not keepNumbers: 130 | notElements.append(RDBOperatorsConst.NUMBERS) 131 | 132 | for removal in notElements: 133 | eqnArr = comp_sub(removal, eqnArr) 134 | 135 | if removeEmpty: 136 | eqnArr = remove_empty(eqnArr) 137 | 138 | if uniqueOnly: 139 | eqnArr = unique(eqnArr) 140 | 141 | if sorted: 142 | eqnArr.sort() 143 | 144 | return eqnArr 145 | 146 | def getSimpleLogicElements(arrEqnElements, uniqueOnly=True, sorted=True): 147 | """ 148 | Given an array containing SEL logic variables, reduce them to the raw elements and 149 | return an array of them. Anything additional in the array should not be affected 150 | """ 151 | 152 | # substitute all element types 153 | for key, value in RDBOperatorsConst.TYPES.items(): 154 | for ident in value: 155 | # get regex 156 | [variableRegex, variableNameAndNumOnly] = getRawVariableFromTo(ident) 157 | # substitute 158 | arrEqnElements = comp_sub(variableRegex, arrEqnElements, variableNameAndNumOnly) 159 | 160 | if uniqueOnly: 161 | #print(arrEqnElements) 162 | arrEqnElements = unique(arrEqnElements) 163 | 164 | if sorted: 165 | arrEqnElements.sort() 166 | 167 | return arrEqnElements 168 | 169 | def getEqnResidual(eqnArray, removeEmpty=True): 170 | """ 171 | Given an array of RDB elements remove all known SELogic equation operators 172 | """ 173 | # substitute all element types 174 | for key, value in RDBOperatorsConst.TYPES.items(): 175 | variableName = getVariableRegex(value[0]) 176 | eqnArray = comp_sub(variableName, eqnArray, '') 177 | 178 | if removeEmpty: 179 | eqnArray = remove_empty(eqnArray) 180 | 181 | return eqnArray 182 | 183 | def getEqnArrayOfArraysResidual(arr, removeEmpty=True): 184 | # TODO: I can remove this? 185 | # Given an array of arrays ([lines] and [elements in equations]) 186 | # Return the residual elements after removing operators and SELogic items 187 | return getEqnResidual(flatten(arr),removeEmpty) 188 | 189 | def getLineComponents(eqn, keepNumbers=False, uniqueOnly=True): 190 | """ 191 | For a given line return three arrays: 192 | [line containing logic types and RWBs] [only logic] [only RWBs] 193 | """ 194 | raw_line = eqnTokenise(removeComment(eqn)) 195 | # remove operators, brackets etc. TODO: Is ABS(x) more expensive than x? I don't take this into account 196 | raw_line = getEquationElements(raw_line, keepNumbers, uniqueOnly=uniqueOnly) 197 | eqn_elements = getSimpleLogicElements(raw_line, uniqueOnly=uniqueOnly) 198 | 199 | residual_elements = getEqnResidual(eqn_elements) 200 | 201 | logic_elements = [x for x in eqn_elements if x not in residual_elements] 202 | 203 | return [eqn_elements, logic_elements, residual_elements] 204 | 205 | def countElementsUsed(eqn): 206 | """ 207 | Counts the amount of logic used in an equation 208 | Can handle the use of functions 209 | """ 210 | elements = getLineComponents(eqn, keepNumbers=True, uniqueOnly=False) 211 | functions = re.findall(RDBOperatorsConst.FUNCTIONS, eqn) 212 | return len(elements[0]) + len(functions) 213 | 214 | def get_logic_usage(ltext): 215 | 216 | results = {} 217 | 218 | logic_lines = ltext.strip().split("\n") 219 | results['LINES'] = len(logic_lines) 220 | 221 | # lines without comments 222 | lines_less_comments = [removeComment(l) for l in logic_lines] 223 | 224 | # lines without blank or comment lines 225 | lines_nb_nc = remove_empty(lines_less_comments) 226 | results['LINES_UNCOMMENTED'] = len(lines_nb_nc) 227 | 228 | tokenised_lines = [[l.split(" ")] for l in lines_less_comments] 229 | 230 | raw_line_elements = [] 231 | logic_elements = [] 232 | residual_elements = [] 233 | 234 | for line in logic_lines: 235 | [int_eqn_elements, int_logic_elements, int_residual_elements] = getLineComponents(line,keepNumbers=False) 236 | 237 | raw_line_elements.append(int_eqn_elements) 238 | logic_elements.append(int_logic_elements) 239 | residual_elements.append(int_residual_elements) 240 | 241 | # We are only interested in totals, so flatten the array and take only unique values 242 | logic_elements = unique(flatten(logic_elements)) 243 | residual_elements = unique(flatten(residual_elements)) 244 | 245 | logic_elements.sort() 246 | residual_elements.sort() 247 | 248 | for key, value in RDBOperatorsConst.TYPES.items(): 249 | search_string = getVariableRegex(value[0]) 250 | count = comp_count(logic_elements, search_string) 251 | results[key] = str(count) 252 | 253 | return [results, logic_elements, residual_elements] 254 | 255 | def make_limits(name, min=False, max=False): 256 | """ 257 | for a given variable, e.g. PSV return a full list 258 | within capability of SEL-400 series logic 259 | """ 260 | if name in RDBOperatorsConst.LIMITS: 261 | 262 | if min and not max: 263 | lims = list(range(min, RDBOperatorsConst.LIMITS[name][1]+1)) 264 | elif max and not min: 265 | lims = list(range(RDBOperatorsConst.LIMITS[name][0]+1,max)) 266 | elif min and max: 267 | lims = [min, max] 268 | else: 269 | lims = RDBOperatorsConst.LIMITS[name] 270 | 271 | rng = list(range(lims[0], lims[1]+1)) 272 | 273 | max_chars = str(len(str(lims[1]))) 274 | return [name + ('{:0>' + max_chars + '}').format(str(r)) for r in rng] 275 | 276 | def find_unused_logic(type, used, provideRaw=False, lowestAllowed=None, highestAllowed=None): 277 | # TODO: FIXME: lowestAllowed only works with provideRaw 278 | full_list = make_limits(type) 279 | if full_list != None: 280 | unused = [l for l in full_list if l not in used] 281 | regex_only_numbers = re.compile(r'^' + type) 282 | only_numbers = [int(re.sub(regex_only_numbers,'',val)) for val in unused] 283 | rng = get_interval_range(only_numbers) 284 | 285 | if provideRaw == False: 286 | return provide_string_range(rng) 287 | else: 288 | if lowestAllowed and not highestAllowed: 289 | return [u for u in unused if int(re.findall(r'[A-Z]+([0-9]+)[A-Z]*', u)[0]) >= lowestAllowed] 290 | elif not lowestAllowed and highestAllowed: 291 | return [u for u in unused if int(re.findall(r'[A-Z]+([0-9]+)[A-Z]*', u)[0]) <= highestAllowed] 292 | elif lowestAllowed and highestAllowed: 293 | return [u for u in unused if lowestAllowed <= int(re.findall(r'[A-Z]+([0-9]+)[A-Z]*', u)[0]) <= highestAllowed] 294 | else: 295 | return unused 296 | else: 297 | return '' 298 | 299 | def calc_logic_usage(logic_text): 300 | """ 301 | Calculate the logic usage. 302 | Just I think a calculation of the number of "residual elements" 303 | Where Numbers also count 304 | """ 305 | returnval = '' 306 | r = get_logic_usage(logic_text) 307 | [usage_info, logic_used, residue] = r 308 | 309 | returnval += 'Lines Used (w/ comment lines): ' + str(usage_info['LINES']) + '\n' 310 | returnval += 'Lines Used (w/o comment lines): ' + str(usage_info['LINES_UNCOMMENTED']) + '\n' 311 | 312 | del usage_info['LINES'] 313 | del usage_info['LINES_UNCOMMENTED'] 314 | 315 | for operator_type, used_qty in usage_info.items(): 316 | total = RDBOperatorsConst.LIMITS[operator_type][1] 317 | returnval += ' '.join([operator_type, 318 | 'Used:', '{:>3}'.format(used_qty), 319 | '/' ' {:<4}'.format(RDBOperatorsConst.LIMITS[operator_type][1]), 320 | 'Unused: {:<4.0%} Available: '.format(1-int(used_qty)/total) + 321 | find_unused_logic(operator_type, logic_used), '\n']) 322 | 323 | return returnval 324 | 325 | def calc_usage_raw(logic_texts): 326 | """ 327 | Calculate the logic usage. 328 | Just I think a calculation of the number of "residual elements" 329 | Where Numbers also count 330 | """ 331 | 332 | usage_sum = {} 333 | 334 | r = get_logic_usage(logic_texts) 335 | [usage_info, logic_used, residue] = r 336 | 337 | usage_sum['Lines Used (w/ comment lines)'] = usage_info['LINES'] 338 | usage_sum['Lines Used (w/o comment lines)'] = usage_info['LINES_UNCOMMENTED'] 339 | 340 | del usage_info['LINES'] 341 | del usage_info['LINES_UNCOMMENTED'] 342 | 343 | for operator_type, used_qty in usage_info.items(): 344 | total = RDBOperatorsConst.LIMITS[operator_type][1] 345 | usage_sum[operator_type] = {} 346 | usage_sum[operator_type]['qty'] = used_qty 347 | usage_sum[operator_type]['total'] = RDBOperatorsConst.LIMITS[operator_type][1] 348 | usage_sum[operator_type]['free_pu'] = (1-int(used_qty)/total) 349 | usage_sum[operator_type]['available_detail'] = find_unused_logic(operator_type, logic_used) 350 | 351 | return usage_sum 352 | 353 | """ 354 | 1. Make sure quickset is displaying non-aliased values 355 | 2. Copy and paste logic below into the string logic_text between the quotes, (at start of lines) 356 | 3. Copy and paste protection logic and then automation logic without blank lines (but then total lines is incorrect) 357 | 4. Run. 358 | """ 359 | 360 | logic_text = """ 361 | APP_SEL := 1.000000 # BASE APPLICATION NUMBER AS PER SETTING GUIDE 362 | ## ## CUSTOM OC/EF, VOLTAGE AND OVERLOAD ELEMENTS FOR DESIGNER MODIFICATION## ## 363 | #PHASE INST/DEFT LEVEL 1 SETTINGS (CUSTOM P1). OC(DEFT)LV1 OR OC(DEFT)MV1 (FOR ICTS). THIS ELEMENT CAN BE CHANGED TO OC(VCON,DEFT)LV1 BY CHANGING THE TORQUE CONTROL AS PER THE SETTING GUIDE 364 | C67UP1P := 999.000000 # PICKUP, AMPS SECONDARY 365 | C67UP1D := 999.000000 # DELAY, CYCLES 366 | C67UP1C := 1 # TORQUE CONTROL 367 | #PHASE INST/DEFT LEVEL 2 SETTINGS (CUSTOM P2). OC(DIREC, DEFT)LV1 OR OC(DEFT)MV1 ELEMENT FOR ICT 368 | C67UP2P := 999.000000 # PICKUP, AMPS SECONDARY 369 | C67UP2D := 999.000000 # DELAY, CYCLES 370 | C67UP2C := (((APP_SEL <> 3.000000 AND UF32P) OR (APP_SEL = 5.000000 AND WF32P)) AND NOT PSV22) OR (APP_SEL = 3.000000) # TORQUE CONTROL 371 | #PHASE INST/DEFT LEVEL 3 SETTINGS (CUSTOM P3). SPARE OC(DEFT)LV1 OR OC(DEFT)MV1 (FOR ICTS) ELEMENT FOR CUSTOM USE. THIS ELEMENT CAN BE VOLTAGE CONTROLLED BY CHANGING THE TORQUE CONTROL AS PER THE SETTING GUIDE 372 | C67UP3P := 999.000000 # PICKUP, AMPS SECONDARY 373 | C67UP3D := 999.000000 # DELAY, CYCLES 374 | C67UP3C := 1 # TORQUE CONTROL 375 | #PHASE INST/DEFT LEVEL 4 SETTINGS (CUSTOM P4). OC(SOTF). THIS IS THE PICKUP FOR SOTF ELEMENTS 376 | C50USTF := 999.000000 # PICKUP, AMPS SECONDARY 377 | #GROUND INST/DEFT LEVEL 1 SETTINGS (OPERATES FROM IGS) (CUSTOM G1). EF(DEFT)HV1 FOR YD SUPPLY BANK TRANSFORMERS AND INTERCONNECTING TRANSFORMER APPLICATIONS ONLY 378 | C67SG1P := 999.000000 # PICKUP, AMPS SECONDARY 379 | C67SG1D := 999.000000 # DELAY, CYCLES 380 | C67SG1C := 1 # TORQUE CONTROL 381 | #NEUTRAL INST/DEFT LEVEL 1 SETTINGS (OPERATES FROM IY2) (CUSTOM N1). EF(ALARM)1 FOR SUPPLY BANK TRANSFORMERS ONLY 382 | C67UN1P := 999.000000 # PICKUP, AMPS SECONDARY 383 | C67UN1D := 6000.000000 # DELAY, CYCLES 384 | C67UN1C := 1 # TORQUE CONTROL 385 | #GROUND INST/DEFT LEVEL 1 SETTINGS (OPERATES FROM IGU)(CUSTOM G1). EF(DEFT)LV1 AND EF(SOTF)LV1 (PICKUP ONLY) FOR SUPPLY BANK TRANSFORMERS ONLY 386 | C67UG1P := 999.000000 # PICKUP, AMPS SECONDARY 387 | C67UG1D := 999.000000 # DELAY, CYCLES 388 | C67UG1C := 1 # TORQUE CONTROL 389 | #VOLTAGE INST/DEFT LEVEL 1 SETTINGS (OPERATES FROM VPV)(CUSTOM V1). HV VT HEALTHY OVERVOLTAGE ELEMENT FOR SUPPLY BANK TRANSFORMERS ONLY 390 | C59VV1P := 999.000000 # PICKUP, VOLTS SECONDARY 391 | #VOLTAGE INST/DEFT LEVEL 2 SETTINGS (OPERATES FROM VPZ)(CUSTOM V2). LV/MV (FOR ICT) VT HEALTHY OVERVOLTAGE ELEMENT 392 | C59ZV2P := 999.000000 # PICKUP, VOLTS SECONDARY 393 | # 394 | ## ## USER CUSTOMISABLE TIMERS (SEE SETTING GUIDE) ## ## 395 | PCT01PU := 0.000000 # S TERMINAL CB DROP OFF TIMER FOR REMOTE OPEN AND LAT 396 | PCT01DO := 25.000000 397 | PCT01IN := OCS OR IN203 OR (APP_SEL <> 3.000000 AND IN201) 398 | PCT02PU := 0.000000 # T TERMINAL CB DROP OFF TIMER FOR REMOTE OPEN AND LAT 399 | PCT02DO := 25.000000 400 | PCT02IN := (APP_SEL = 4.000000 AND (OCT OR IN202 OR IN201)) 401 | PCT03PU := 0.000000 # U TERMINAL CB DROP OFF TIMER FOR REMOTE OPEN AND LAT 402 | PCT03DO := 25.000000 403 | PCT03IN := OCU OR IN201 OR ((APP_SEL <> 3.000000 OR APP_SEL <> 4.000000) AND IN203) 404 | PCT04PU := 0.000000 # W TERMINAL CB DROP OFF TIMER FOR REMOTE OPEN AND LAT 405 | PCT04DO := 25.000000 406 | PCT04IN := APP_SEL = 5.000000 AND (OCU OR IN201 OR IN203) 407 | PCT05PU := 0.000000 # CB S CLOSE DROPOFF TIMER 408 | PCT05DO := 10.000000 409 | PCT05IN := CLSS 410 | PCT06PU := 0.000000 # CB T CLOSE DROPOFF TIMER 411 | PCT06DO := 10.000000 412 | PCT06IN := CLST 413 | PCT07PU := 0.000000 # CB U CLOSE DROPOFF TIMER 414 | PCT07DO := 10.000000 415 | PCT07IN := CLSU 416 | PCT08PU := 0.000000 # CB W CLOSE DROPOFF TIMER 417 | PCT08DO := 10.000000 418 | PCT08IN := CLSW 419 | PST01PT := 100.000000 # TX TEMP(MAIN) TRIP 420 | PST01R := NOT (IN208) 421 | PST01IN := IN208 422 | PST02PT := 100.000000 # TEMP(ET)/TEMP(LV) TRIP 423 | PST02R := NOT (IN210) 424 | PST02IN := IN210 425 | PST03PT := 250.000000 # HV VT SUPERVISION LOGIC 426 | PST03R := NOT (APP_SEL <> 3.000000 AND (NOT (VAVFM > C59VV1P AND VBVFM > C59VV1P AND VCVFM > C59VV1P) AND (52CLS OR (APP_SEL = 4.000000 AND 52CLT)))) 427 | PST03IN := APP_SEL <> 3.000000 AND (NOT (VAVFM > C59VV1P AND VBVFM > C59VV1P AND VCVFM > C59VV1P) AND (52CLS OR (APP_SEL = 4.000000 AND 52CLT))) 428 | PST04PT := 250.000000 # LV/MV (FOR ICTS) VT SUPERVISION LOGIC 429 | PST04R := NOT (NOT (VAZFM > C59ZV2P AND VBZFM > C59ZV2P AND VCZFM > C59ZV2P) AND (52CLU OR (APP_SEL = 5.000000 AND 52CLW))) 430 | PST04IN := NOT (VAZFM > C59ZV2P AND VBZFM > C59ZV2P AND VCZFM > C59ZV2P) AND (52CLU OR (APP_SEL = 5.000000 AND 52CLW)) 431 | PCT13PU := 0.000000 # LV/MV (FOR ICTS) VT VOLTAGE CONTROL PROTECTION 432 | PCT13DO := 250.000000 433 | PCT13IN := APP_SEL <> 3.000000 AND (VAZFM > C59ZV2P AND VBZFM > C59ZV2P AND VCZFM > C59ZV2P) 434 | PCT14PU := 0.000000 # RESERVED FOR LV VOLTAGE CONTROL LATCHING LOGIC 435 | PCT14DO := 0.000000 436 | PCT14IN := 0 437 | PST05PT := 20.000000 # 400MS DELAY (20 CYCLES). STAGE 2 TRIP OF HV CB 438 | PST05R := NOT (APP_SEL <> 3.000000 AND (C49TRP OR REF50T2 OR C67UG1T)) 439 | PST05IN := APP_SEL <> 3.000000 AND (C49TRP OR REF50T2 OR C67UG1T) 440 | PST06PT := 20.000000 # 400 MS DELAY (20 CYCLES). STAGE 2 TRIP OF MV/LV CB. INTERCONNECTING TRANSFORMER APPLICATIONS ONLY 441 | PST06R := NOT (APP_SEL = 3.000000 AND C67SG1T) 442 | PST06IN := APP_SEL = 3.000000 AND C67SG1T 443 | PCT17PU := 0.000000 # SPARE. AVAILABLE FOR GENERAL USE 444 | PCT17DO := 0.000000 445 | PCT17IN := 0 446 | PCT18PU := 0.000000 # SPARE. AVAILABLE FOR GENERAL USE 447 | PCT18DO := 0.000000 448 | PCT18IN := 0 449 | # 450 | ## ## USER CUSTOMISABLE LATCHES FOR CONTROL (SEE SETTING GUIDE) ## ## 451 | PLT01S := 0 # SPARE. AVAILABLE FOR GENERAL USE 452 | PLT01R := 0 # SPARE. AVAILABLE FOR GENERAL USE 453 | PLT02S := 0 # SPARE. AVAILABLE FOR GENERAL USE 454 | PLT02R := 0 # SPARE. AVAILABLE FOR GENERAL USE 455 | # 456 | ## ## INTERMEDIATE VARIABLE ## ## 457 | PSV01 := PST01Q # TEMP(MAIN) TRIP OPERATED INTERMEDIATE VARIABLE 458 | PSV02 := PST02Q # TEMP(ET) TRIP OR TEMP(LV)TRIP OPERATED INTERMEDIATE VARIABLE 459 | PSV03 := REFF1 # RESTRICTED EARTH FAULT 1 PROTECTION OPERATED INTERMEDIATE VARIABLE USED FOR SCADA MAP 460 | PSV04 := REFF2 # RESTRICTED EARTH FAULT 2 PROTECTION OPERATED INTERMEDIATE VARIABLE USED FOR SCADA MAP 461 | PSV05 := REF50T1 # EF(DEFT,NEUT)HV1 PROTECTION OPERATED INTERMEDIATE VARIALBE USED FOR SCADA MAP 462 | PSV06 := REF50T2 # EF(DEFT,NEUT)LV1 PROTECTION OPERATED INTERMEDIATE VARIALBE USED FOR SCADA MAP 463 | PSV07 := C67UP1T # OC(DEFT)LV1 / OC(VCON,DEFT)LV1 / OC(DEFT)MV1 PROTECTION OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 464 | PSV08 := (PCT19Q OR PCT25Q) AND PCT26Q # OC(SOTF) PROTECTION OPERATED. INTERMEDIATE VARIABLE 465 | PSV09 := C67UP2T # OC(DIREC,DEFT)LV PROTECTION OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 466 | PSV10 := C67UP3T # SPARE OC(DEFT)LV1 ELEMENT OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 467 | PSV11 := C67SG1T # EF(DEFT)HV1 PROTECTION OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 468 | PSV12 := C67UN1T # EF(ALARM)1 OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 469 | PSV13 := C67UG1T # EF(DEFT)LV1 PROTECTION OPERATED. INTERMEDIATE VARIABLE USED FOR SCADA MAP 470 | PSV14 := FBFS # TERMINAL S BREAKER FAIL PROTECTION OPERATED INTERMEDIATE VARIABLE USED FOR SCADA MAP 471 | PSV15 := FBFT # TERMINAL T BREAKER FAIL PROTECTION OPERATED INTERMEDIATE VARIABLE USED FOR SCADA MAP 472 | PSV16 := FBFU # TERMINAL U BREAKER FAIL PROTECTION OPERATED INTERMEDIATE VARIABLE USED FOR SCADA MAP 473 | PSV17 := FBFW # TERMINAL W BREAKER FAIL PROTECTION OPERATED INTERMEDIATE VARABLE USED FOR SCADA MAP 474 | PSV18 := PST05Q OR (APP_SEL = 3.000000 AND PST06Q) # TWO STAGE PROTECTION ELEMENT OPERATED 475 | PSV21 := APP_SEL <> 3.000000 AND (LOPV OR PST03Q) # HV VT SUPERVISION INTERMEDIATE VARIABLE 476 | PSV22 := LOPZ OR PST04Q # LV/MV (FOR ICTS) VT SUPERVISION INTERMEDIATE VARIABLE 477 | PSV23 := PTP_OK # PTP TIME SYNC OK. INTERMEDIATE VARIABLE USED FOR SCADA MAP 478 | PSV24 := BUBCWAL # CB U WEAR EXCESSIVE. INTERMEDIATE VARIABLE USE FOR SCADA MAP 479 | PSV25 := 0 # SPARE. AVAILABLE FOR GENERAL USE 480 | PSV26 := 0 # SPARE. AVAILABLE FOR GENERAL USE 481 | PSV27 := 0 # SPARE. AVAILABLE FOR GENERAL USE 482 | PSV28 := 0 # SPARE. AVAILABLE FOR GENERAL USE 483 | # 484 | ## ## FIXED LOGIC - CHANGES TO LOGIC BEYOND THIS POINT GENERALLY NOT REQUIRED ## ## 485 | ## CUSTOM OC/EF ELEMENTS AND CUSTOM OVERVOLTAGE ELEMENT PICKUPS ## 486 | PSV20 := (APP_SEL <> 5.000000 AND (IAUFM > C67UP1P OR IBUFM > C67UP1P OR ICUFM > C67UP1P) AND C67UP1C) 487 | C67UP1 := PSV20 OR (APP_SEL = 5.000000 AND (IAUWFM > C67UP1P OR IBUWFM > C67UP1P OR ICUWFM > C67UP1P) AND C67UP1C) # OC(DEFT)LV1 / OC(DEFT)MV1 488 | PSV29 := APP_SEL <> 5.000000 AND (IAUFM > C67UP2P OR IBUFM > C67UP2P OR ICUFM > C67UP2P) AND C67UP2C 489 | C67UP2 := PSV29 OR (APP_SEL = 5.000000 AND (IAUWFM > C67UP2P OR IBUWFM > C67UP2P OR ICUWFM > C67UP2P) AND C67UP2C) # OC(DIREC,DEFT)LV1" 490 | PSV30 := APP_SEL <> 5.000000 AND ((IAUFM > C67UP3P OR IBUFM > C67UP3P OR ICUFM > C67UP3P) AND C67UP3C) 491 | C67UP3 := PSV30 AND APP_SEL = 5.000000 AND ((IAUWFM > C67UP3P OR IBUWFM > C67UP3P OR ICUWFM > C67UP3P) AND C67UP3C) # SPARE OC(DEFT)LV1 492 | C67SG1 := (APP_SEL <> 4.000000 AND (3I0SM > C67SG1P) AND C67SG1C) OR (APP_SEL = 4.000000 AND ((3I0STM > C67SG1P) AND C67SG1C)) # EF(DEFT)HV1 493 | C67UN1 := (IY2FM > C67UN1P) AND C67UN1C # EF(ALARM)1 494 | C67UG1 := ((APP_SEL <> 5.000000 AND 3I0UM > C67UG1P) OR (APP_SEL = 5.000000 AND 3I0UWM > C67UG1P)) AND C67UG1C # EF(DEFT)LV1 495 | ## CUSTOM SOTF ELEMENT, CUSTOM OC/EF ELEMENTS AND CUSTOM VOLTAGE ELEMENTS TIME DELAYED TRIPS ## 496 | PCT19PU := 15.000000 # SOTF ARMED 300MS AFTER CIRCUIT BREAKER S OR T (IF HV 1.5 CB) OPENED, SOTF ENABLED FOR 200MS AFTER CIRCUIT BREAKER S CLOSED 497 | PCT19DO := 10.000000 498 | PCT19IN := NOT (52CLS OR (APP_SEL = 4.000000 AND 52CLT)) 499 | PCT25PU := 15.000000 # SOTF ARMED 300MS AFTER CIRCUIT BREAKER U (AND W WHERE DUAL LV INCOMERS EXIST) OPENED, SOTF ENABLED FOR 200MS AFTER THEY ARE CLOSED 500 | PCT25DO := 17.500000 501 | PCT25IN := NOT (52CLU OR (APP_SEL = 5.000000 AND 52CLW)) 502 | PCT26PU := 7.500000 # DELAY, CYCLES. 150MS SOTF OPERATE DELAY 503 | PCT26DO := 0.000000 504 | PSV31 := APP_SEL <> 5.000000 AND (IAUFM > C50USTF OR IBUFM > C50USTF OR ICUFM > C50USTF OR ((APP_SEL <> 3.000000) AND 3I0UM > C67UG1P)) 505 | PCT26IN := PSV31 OR (APP_SEL = 5.000000 AND (IAUWFM > C50USTF OR IBUWFM > C50USTF OR ICUWFM > C50USTF OR 3I0UWM > C67UG1P)) 506 | PCT27PU := C67UP1D 507 | PCT27DO := 0.000000 508 | PCT27IN := C67UP1 509 | PCT28PU := C67UP2D 510 | PCT28DO := 0.000000 511 | PCT28IN := C67UP2 512 | PCT29PU := C67UP3D 513 | PCT29DO := 0.000000 514 | PCT29IN := C67UP3 515 | PCT30PU := C67SG1D 516 | PCT30DO := 0.000000 517 | PCT30IN := C67SG1 518 | PCT31PU := C67UN1D 519 | PCT31DO := 0.000000 520 | PCT31IN := C67UN1 521 | PCT32PU := C67UG1D 522 | PCT32DO := 0.000000 523 | PCT32IN := C67UG1 524 | ## CUSTOM OL(ET,NER)LV1 ELEMENT LOGIC NOT FOR CASUAL MODIFICATION ## 525 | # ENSURE THAT AMV001 AMV002 AMV003 AMV004 ARE SET CORRECTLY IN AUTOMATION 1 526 | C49TRP := IY2FM <= C49_IR # C = IM <= IR 527 | C49_TH := IY2FM * IY2FM * C49_DT - C49__K * C49TRP # D = IM*IM*DT-K*C 528 | PMV24 := PMV21 # X = B 529 | PMV21 := PMV21 + C49_TH # B = B+D 530 | PMV22 := PMV22 + (C49_TH - (PMV21 - PMV24)) # S = S + (D - (B-X)) 531 | PMV24 := PMV21 # X = B 532 | PMV21 := PMV21 + PMV22 # B = B+S 533 | PMV22 := PMV22 - (PMV21 - PMV24) # S = S - (B-X) 534 | C49TRP := (0.000000 < PMV21) AND NOT LB01 # TEST IF J/R < 0 OR LB01 = 1 RESET THERMAL STATE TO ZERO REQUEST 535 | PMV21 := PMV21 * C49TRP # FORCE J/R B TO 0 IF J/R<0 OR LB01 = 1 536 | PMV22 := PMV22 * C49TRP # FORCE J/R S TO 0 IF J/R<0 OR LB01 = 1 537 | C49TRP := (PMV21 > C49__M) AND NOT AFRTEXP AND C67UN1 # TEST FOR TRIP AND BLOCK TRIP UNTIL AMVS ARE INITIALISED AND APPLY CURRENT GUARD 538 | C49_TH := PMV21 / C49__M * 100.000000 # THERMAL STATE AS PERCENT 539 | ## LOCK BUTTON LOGIC ## 540 | PLT03S := PST07Q AND NOT FP_LOCK 541 | PLT03R := PB12PUL AND FP_LOCK 542 | PST07PT := 150.000000 543 | PST07R := NOT (PB12) 544 | PST07IN := PB12 # LOCK/UNLOCK 545 | # FRONT PANEL INTERMEDIATE VARIABLES 546 | PSV32 := (APP_SEL <> 1.000000 AND APP_SEL <> 6.000000 AND C67SG1T) OR ((APP_SEL = 2.000000 OR APP_SEL = 4.000000 OR APP_SEL = 5.000000) AND REF50T1) # HV SYSTEM FAULT FRONT PANEL INTERMEDIATE VARIABLE 547 | PSV33 := C67UP1T OR (APP_SEL <> 3.000000 AND (51T01 OR 51T02 OR REF50T2 OR 67UG1T OR C67UP3T OR C49TRP)) # MV/LV SYSTEM FAULT FRONT PANEL INTERMEDIATE VARIABLE 548 | PSV34 := (PHA_U AND TRIPU) OR ((APP_SEL = 3.000000 OR APP_SEL = 5.000000) AND PHA_W AND TRIPW) # RED PH FRONT PANEL INDICATION INTERMEDIATE VARIABLE 549 | PSV35 := (PHB_U AND TRIPU) OR ((APP_SEL = 3.000000 OR APP_SEL = 5.000000) AND PHB_W AND TRIPW) # YELLOW PH FRONT PANEL INDICATION INTERMEDIATE VARIABLE 550 | PSV36 := (PHC_U AND TRIPU) OR ((APP_SEL = 3.000000 OR APP_SEL = 5.000000) AND PHC_W AND TRIPW) # BLUE PH FRONT PANEL INDICATION INTERMEDIATE VARIABLE 551 | PSV37 := (APP_SEL <> 1.000000 AND APP_SEL <> 6.000000) AND (REFF1 OR C67SG1T) OR (APP_SEL <> 3.000000 AND (REFF2 OR C67UG1T OR REF50T2 OR C49TRP)) # EARTH FAULT FRONT PANEL INDICATION INTERMEDIATE VARIABLE 1 552 | PSV38 := ((APP_SEL = 1.000000 OR APP_SEL = 6.000000) AND 591P1T) OR (APP_SEL = 3.000000 AND (592P1T OR 593P1T)) # EARTH FAULT FRONT PANEL INDICATION INTERMEDIATE VARIABLE 2 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | """ 613 | 614 | logic_text_2 = """ 615 | ## ## AUTO RECLOSE LOGIC. CONSULT WITH TRANSPOWER BEFORE USING ## ## 616 | # TIMER SETTINGS 617 | C79OI1 := 250.000000 # HV CB OPEN INTERVAL 618 | C79OI2 := 750.000000 # LV CB OPEN INTERVAL 619 | C79RCD := 750.000000 # AUTO RECLOSE RECLAIM TIME 620 | C79MRCD := 500.000000 # MANUAL CLOSE RECLAIM TIME 621 | C79CLSD := 50.000000 # AUTO RECLOSE CLOSE SUPERVISION TIME 622 | C79CFD := 9.000000 # CLOSE FAILURE TIME 623 | # LOGIC SETTINGS 624 | C79RI := IN106 # AUTO RECLOSE INITIATE 625 | C79RIS := 52CLS AND 52CLU AND PLT15 # RECLOSER INITIATE SUPERVISION 626 | C79DTL := TRIP OR IN107 OR IN201 # DRIVE TO LOCKOUT CONDITIONS 627 | C79CLS1 := 594P1 # HV CLOSE SUPERVISION CONDITION 628 | C79CLS2 := 595P1 # LV CLOSE SUPERVISION CONDITION 629 | # 630 | # FIXED AUTO RECLOSE LOGIC PAST THIS POINT. DO NOT MODIFY # 631 | C3POTX := NOT (52CLS AND 52CLU) # THREE POLE OPEN FOR BOTH TRANSFORMER BREAKERS 632 | PLT15S := R_TRIG RB16 633 | PLT15R := R_TRIG RB15 634 | PCT16PU := 0.000000 # HV CB OPEN INTERVAL TIMER 635 | PCT16DO := C79OI1 636 | PCT16IN := PLT21 AND R_TRIG C3POTX 637 | PCT17PU := 0.000000 # LV CB OPEN INTERVAL TIMER 638 | PCT17DO := C79OI2 639 | PCT17IN := PLT21 AND R_TRIG C3POTX 640 | PCT18PU := 0.000000 # RECLOSE INITIATE SUPERVISION TIMER. 641 | PCT18DO := 15.000000 642 | PCT18IN := R_TRIG PLT21 643 | PCT19PU := 0.000000 # AUTO RECLOSE RECLAIM TIMER 644 | PCT19DO := C79RCD 645 | PCT19IN := PLT21 AND R_TRIG 52CLU 646 | PCT20PU := C79MRCD # MANUAL RECLAIM TIMER 647 | PCT20DO := 0.000000 648 | PCT20IN := PLT22 AND NOT C79DTL AND C79RIS 649 | PCT21PU := C79CFD # CB CLOSE FAILURE TIMER 650 | PCT21DO := 1.000000 651 | PCT21IN := PLT15 AND (PCT05Q AND NOT 52CLS OR PCT07Q AND NOT 52CLU) 652 | PCT22PU := C79CLSD # HV CLOSE SUPERVISION TIMER 653 | PCT22DO := 0.000000 654 | PCT22IN := PLT23 655 | PCT23PU := C79CLSD # LV CLOSE SUPERVISION TIMER 656 | PCT23DO := 0.000000 657 | PCT23IN := PLT24 658 | # RESET STATE # 659 | PLT20S := PLT22 AND R_TRIG PCT20Q OR PLT21 AND F_TRIG PCT19Q 660 | PLT20R := R_TRIG PLT21 OR R_TRIG PLT22 661 | # CYCLE STATE # 662 | PLT21S := PLT20 AND C79RI AND C79RIS 663 | PLT21R := R_TRIG PLT20 OR R_TRIG PLT22 664 | # LOCKOUT STATE # 665 | PLT22S := C79DTL OR (PLT20 AND F_TRIG C79RIS) OR (F_TRIG PCT18Q AND NOT C3POTX) OR PCT21Q OR PCT22Q OR PCT23Q OR F_TRIG PLT15 OR PFRTEX 666 | PLT22R := R_TRIG PCT20Q 667 | # AUTO RECLOSE CLOSE COMMANDS # 668 | PLT23S := F_TRIG PCT16Q AND NOT C79CLS1 # HV RECLOSE SUPERVISION 669 | PLT23R := R_TRIG C79CLS1 OR R_TRIG PCT22Q 670 | PSV33 := (PLT21 AND F_TRIG PCT16Q AND C79CLS1) OR (PLT21 AND PLT23 AND R_TRIG C79CLS1) # HV CB CLOSE COMMAND 671 | PLT24S := F_TRIG PCT17Q AND NOT C79CLS2 # LV RECLOSE SUPERVISION 672 | PLT24R := R_TRIG C79CLS2 OR R_TRIG PCT23Q 673 | PSV34 := (PLT21 AND F_TRIG PCT17Q AND C79CLS2) OR (PLT21 AND PLT24 AND R_TRIG C79CLS2) # LV CB CLOSE COMMAND 674 | ## END OF AUTO RECLOSE LOGIC ## 675 | """ 676 | 677 | if __name__ == "__main__": 678 | #pass 679 | calc_logic_usage(logic_text_2) 680 | print(calc_usage_raw(logic_text_2)) 681 | #e = "PSVxx = (PHA_U AND TRIPU) OR ((PMV63 = 3 OR PMV63 = 5) PHA_W AND TRIPW)" 682 | e2 = "(PSV44 OR PSV45 OR ((PMV64 = 2.000000 OR PMV64 = 4.000000 OR PMV64 = 5.000000) AND REF50T1)) AND R_TRIG TRIP # GROUND FAULT" 683 | 684 | #print(countElementsUsed(e)) 685 | print(countElementsUsed(e2)) 686 | 687 | #calc_logic_usage(e) 688 | #print(len(getLineComponents(e, keepNumbers=True)[0][1])) 689 | 690 | """ 691 | Use paired parentheses to control the execution order of operations in a SELOGIC 692 | control equation. Use as many as 14 nested sets of parentheses in each SELOGIC 693 | control equation. The relay calculates the result of the operation on the innermost 694 | pair of parentheses first and then uses this result with the remaining operations. 695 | Table 13.17 is a truth table for an example operation that illustrates how paren- 696 | theses can affect equation evaluation. 697 | 698 | Could also add a check for this. 699 | """ 700 | -------------------------------------------------------------------------------- /sel_logic_functions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import re 4 | 5 | import helpers 6 | import sel_logic_count 7 | 8 | def getInstVals(name): 9 | """ For an instantiated variable type, get the usage 10 | so PLT13 returns PLT13S and PLT13R 11 | """ 12 | type = name[0:3] # SEL variables all have a type 3 chars long 13 | num = name[3:] # and a value which is the remainder 14 | 15 | type_inst = sel_logic_count.RDBOperatorsConst.TYPES[type] 16 | types = [re.sub(r'x+', num, e).replace('$', '') for e in type_inst] 17 | return types 18 | 19 | def change_type_vals(e, to): 20 | valsToChange = getInstVals(e) 21 | 22 | newVals = [] 23 | if to.lower() in ['p', 'prot', 'protection']: 24 | newVals = ['P' + n[1:] for n in valsToChange] 25 | # TODO: Update to handle moving from the many AMV/ASVs to the few PMV/PSVs 26 | elif to.lower() in ['a', 'auto', 'automation']: 27 | newVals = ['A' + n[1:] for n in valsToChange] 28 | # TODO: Check this 29 | if newVals[0][0:3] in ['ASV','AMV']: 30 | newVals = [n[0:3] + '0' + n[3:] for n in newVals if n[0:3] in ['ASV','AMV']] 31 | #print(newVals) 32 | else: 33 | print("Error") 34 | 35 | return [valsToChange, newVals] 36 | 37 | def makeLogicItems(e): 38 | find_lots = re.compile(r'^([A-Z]+)([0-9]{1,3})-([0-9]{1,3})$') 39 | find_digits = helpers.hasNumbers(e) 40 | 41 | result = find_lots.findall(e) 42 | 43 | items = None 44 | if result: 45 | items = sel_logic_count.make_limits(result[0][0], 46 | int(result[0][1]), 47 | int(result[0][2])) 48 | elif find_digits: 49 | items = [e] 50 | else: 51 | items = sel_logic_count.make_limits(e) 52 | return items --------------------------------------------------------------------------------