From 163f7bd1f595afaa2f445bafb95d15304396d9f4 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Tue, 4 Oct 2016 14:31:32 +0200 Subject: Public release. --- CHANGELOG | 4 ++++ LICENSE | 27 +++++++++++++++++++++++++++ README | 38 ++++++++++++++++++++++++++++++++++++++ tina_converter.py | 44 +++++++++++++++++++++++++++++++------------- 4 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 CHANGELOG create mode 100644 LICENSE create mode 100644 README diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..ad3b735 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,4 @@ +VERSION 2 (Oct. 4 2016): + No longer require place names to follow one another (i.e. having only + places 0, 2, 99121) is now authorized. Before, if you had place 2, you + had to have both place 0 and 1. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bc1aa70 --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2016, Nathanaƫl Sensfelder +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of zero-of-one nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README b/README new file mode 100644 index 0000000..9716046 --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +## HOW TO USE ################################################################## +* You need the following: +- A command line interface. +- Python 3. +- The ".net" file corresponding to your petri file (see associated section). +- A translation table (see associated section). + +python3 tina_converter.py NET_FILE TRANSLATION_TABLE OUTPUT_FILE + +Example: +python3 tina_converter.py question1.net translation_table.txt out.rdp + +## HOW TO GET THE .NET FILE #################################################### +Having modeled your Petri net under Tina (http://projects.laas.fr/tina/), you +can go in the "edit" menu and select "textify". You should be presented with +a textual representation of your Petri net. You should then be able to create +the ".net" file using the "File" menu, by selecting "save as". + +## HOW TO BUILD YOUR TRANSLATION TABLE ######################################### +The translation table is a text file in which each line represents an entry. + +#### REPRESENTING A CONDITION +The syntax is as follows: +whatever_you_put_in_your_petri_net::THE_ACTUAL_VARIABLE::THE_OPERATOR::THE_VALUE +THE_OPERATOR should be included in {=,<,>,>=,<=}, or that field left empty +(which is apparently valid syntax, but I know nothing of the semantics). +THE_VALUE must be a positive integer (i.e. THE_VALUE >= 0). For 'boolean' +variables, you must use 1 for 'true' and 0 for 'false'. +Example: +noAction::ACTION_EN_COURS::=::0 + +#### REPRESENTING AN ACTION WITH NO PARAMETER +The syntax is as follows: +whatever_you_put_in_your_petri_net::THE_ACTUAL_NAME + +#### REPRESENTING AN ACTION WITH PARAMETERS +The syntax is as follows: +whatever_you_put_in_your_petri_net::THE_ACTUAL_NAME::THE_VALUE diff --git a/tina_converter.py b/tina_converter.py index 2a1b483..f51d50e 100755 --- a/tina_converter.py +++ b/tina_converter.py @@ -1,9 +1,10 @@ #!/usr/bin/env python3 # vim:set et sw=2 ts=2 tw=80: - +# tina_converter VERSION 2 import argparse import re +## 'CONSTANTS' ################################################################ TINA_TRANSITION_REGEX = re.compile('tr t([0-9]+) : {(.*)} \[0,w\[ (.*) -> (.*)') TINA_PLACE_W_TOKEN_REGEX = re.compile('pl p([0-9]+) : .* \(([0-9]+)\)') TINA_PLACE_TOKEN_REGEX = re.compile('pl p([0-9]+) : .*') @@ -46,6 +47,8 @@ ACTION['GO_NEXT_WP'] = (25, False) ACTION['ETALON_CAPT_LUMIERE'] = (26, False) ACTION['ENVOI_BT'] = (18, True) +## FUNCTIONS ################################################################## + def parse_translation_table (tt_file): tt = dict() for line in tt_file: @@ -147,8 +150,19 @@ def parse_translation_table (tt_file): return tt -def handle_link (link): - return (link if '*' in link else (link + '*1')).replace('p', 'X') +def handle_link (link, pa): + if ('*' in link): + data = link.split('*') + + if (data[0] not in pa): + pa[data[0]] = len(pa) + + return ('X' + str(pa[link]) + '*' + data[1]) + else: + if (link not in pa): + pa[link] = len(pa) + + return ('X' + str(pa[link]) + '*1') def handle_condition (cond, tt): if (cond not in tt): @@ -184,7 +198,7 @@ def handle_action (act, tt): return (str(a_id) + ',' + str(a_val)) -def handle_transition (transition_id, label, inputs, outputs, tt): +def handle_transition (transition_id, label, inputs, outputs, tt, pa): label = label.split('/') inputs = inputs.split(' ') outputs = outputs.split(' ') @@ -206,14 +220,14 @@ def handle_transition (transition_id, label, inputs, outputs, tt): exit(-1) result = 't' + transition_id + ':' - result += ','.join([handle_link(input_link) for input_link in inputs]) + result += ','.join([handle_link(input_link, pa) for input_link in inputs]) if (len(conditions) == 0): result += ';?' else: result += ';' + '/'.join([handle_condition(cond, tt) for cond in conditions]) - result += ';' + ','.join([handle_link(output_link) for output_link in outputs]) + result += ';' + ','.join([handle_link(output_link, pa) for output_link in outputs]) if (len(actions) == 0): result += ';?' @@ -228,7 +242,8 @@ def handle_transition (transition_id, label, inputs, outputs, tt): def convert_tina_net (net_file, tt): first_line = "1,0" result = "" - places = dict() + tokens_at = dict() + places_aliases = dict() for line in net_file: line = line.replace('\r','').replace('\n','') @@ -245,7 +260,8 @@ def convert_tina_net (net_file, tt): matched.group(2), matched.group(3), matched.group(4), - tt + tt, + places_aliases ) ) continue @@ -253,13 +269,13 @@ def convert_tina_net (net_file, tt): matched = TINA_PLACE_W_TOKEN_REGEX.search(line) if (matched): - places[int(matched.group(1))] = matched.group(2) + tokens_at[places_aliases['p' + matched.group(1)]] = matched.group(2) continue matched = TINA_PLACE_TOKEN_REGEX.search(line) if (matched): - places[int(matched.group(1))] = str(0) + tokens_at[places_aliases['p' + matched.group(1)]] = '0' continue matched = TINA_NET_REGEX.search(line) @@ -272,9 +288,9 @@ def convert_tina_net (net_file, tt): exit(-1) first_line = ( - str(len(places) + 1) + str(len(tokens_at) + 1) + ',' - + ','.join([places[i] for i in range(0,len(places))]) + + ','.join([('0' if i not in tokens_at else tokens_at[i]) for i in range(0, len(tokens_at))]) ) + ',0' return (first_line + result) @@ -301,5 +317,7 @@ args = parser.parse_args() translation_table = parse_translation_table(args.translation_table) converted_tn = convert_tina_net(args.net_file, translation_table) +args.output_file.write(converted_tn) +args.output_file.close() #print("Result:") -print(converted_tn) +#print(converted_tn) -- cgit v1.2.3-70-g09d2