|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +import threading |
| 6 | +import signal |
| 7 | +from FSM_algorithm import UserIO, ComportamentalFANSpace, \ |
| 8 | + ComportamentalFANSObservation, ComportamentalFANetwork, \ |
| 9 | + Diagnosis, Diagnosticator |
| 10 | + |
| 11 | + |
| 12 | +task_result = None |
| 13 | +out_file = None |
| 14 | + |
| 15 | + |
| 16 | +def term_program(signal, frame): |
| 17 | + global task_result |
| 18 | + global out_file |
| 19 | + UserIO.write_result(task=task_result, |
| 20 | + out_file=out_file, |
| 21 | + early_terminition=True) |
| 22 | + sys.exit(0) |
| 23 | + |
| 24 | + |
| 25 | +def execute(args, input_read): |
| 26 | + global task_result |
| 27 | + options = { |
| 28 | + 1: ComportamentalFANSpace, |
| 29 | + 2: ComportamentalFANSObservation, |
| 30 | + } |
| 31 | + valid_result = True |
| 32 | + if isinstance(input_read, ComportamentalFANetwork): |
| 33 | + task_result = options[args.type[0]](input_read) |
| 34 | + task_result.build(args.obs_list) |
| 35 | + elif isinstance(input_read, ComportamentalFANSpace) \ |
| 36 | + or isinstance(input_read, Diagnosticator) \ |
| 37 | + or isinstance(input_read, ComportamentalFANSObservation): |
| 38 | + task_result = input_read |
| 39 | + if args.type[0] == 2 and args.diagnosis: |
| 40 | + if task_result.is_correct(): |
| 41 | + task_result = Diagnosis(task_result.space_states, args.obs_list) |
| 42 | + task_result.diagnosis() |
| 43 | + else: |
| 44 | + print('Not valid observation', file=sys.stderr) |
| 45 | + valid_result = False |
| 46 | + elif args.type[0] == 1 and args.diagnosis: |
| 47 | + if isinstance(input_read, Diagnosticator): |
| 48 | + if args.obs_list: |
| 49 | + task_result.linear_diagnosis(args.obs_list) |
| 50 | + elif task_result.is_correct(): |
| 51 | + task_result = Diagnosticator(task_result.space_states) |
| 52 | + task_result.build() |
| 53 | + if args.obs_list: |
| 54 | + task_result.linear_diagnosis(args.obs_list) |
| 55 | + |
| 56 | + if valid_result: |
| 57 | + UserIO.write_result(task_result, args.out_file[0]) |
| 58 | + |
| 59 | + |
| 60 | +def main(): |
| 61 | + desc = 'A program to execute different computation on regex described \ |
| 62 | + with finite state machines.' |
| 63 | + argParser = argparse.ArgumentParser(description=desc) |
| 64 | + argGroup = argParser.add_argument_group(title='Command list') |
| 65 | + argGroup.add_argument('-t', '--type', dest='type', required=True, nargs=1, |
| 66 | + type=int, choices=[1, 2], |
| 67 | + help='The task to accomplish. \n\t\t1 - \ |
| 68 | + Compute the Comportamental FA Network Space \n\t\t2 \ |
| 69 | + - Compute the CFANS relative to an observation') |
| 70 | + argGroup.add_argument('--json', dest='json', nargs=1, |
| 71 | + type=argparse.FileType('r'), |
| 72 | + help='File containing the ComportamentalFANetwork') |
| 73 | + argGroup.add_argument('--bin', dest='bin', nargs=1, |
| 74 | + type=argparse.FileType('rb'), |
| 75 | + help='File containing the binary structure') |
| 76 | + argGroup.add_argument('-o', '--out-file', dest='out_file', nargs=1, |
| 77 | + type=argparse.FileType('w+'), required=True, |
| 78 | + help='File to output results') |
| 79 | + argGroup.add_argument('-O', '--obs-list', dest='obs_list', action='append', |
| 80 | + help='List of observations. Use -Oo1 -Oo2 if the \ |
| 81 | + observation is o1 followed by o2.') |
| 82 | + argGroup.add_argument('-d', '--diagnosis', dest='diagnosis', |
| 83 | + action='store_true', |
| 84 | + help='State that the diagnosis must be computed') |
| 85 | + argGroup.add_argument('-T', '--max-time', dest='max_time', |
| 86 | + type=float, nargs=1, |
| 87 | + help='Maximum execution time in seconds') |
| 88 | + |
| 89 | + args = argParser.parse_args() |
| 90 | + if args.json and args.bin \ |
| 91 | + or not args.json and not args.bin: |
| 92 | + print('ERROR: use either json or bin option') |
| 93 | + sys.exit(1) |
| 94 | + elif args.json: |
| 95 | + lines = [line.strip() for line in args.json[0]] |
| 96 | + input_read = UserIO.read_json(''.join(line for line in lines)) |
| 97 | + |
| 98 | + if not input_read.check(): |
| 99 | + print('The input describe a malformatted ComportamentalFANetwork', |
| 100 | + file=sys.stderr) |
| 101 | + sys.exit(1) |
| 102 | + elif args.bin: |
| 103 | + input_read = UserIO.read_binary(args.bin[0]) |
| 104 | + |
| 105 | + global out_file |
| 106 | + out_file = args.out_file[0] |
| 107 | + signal.signal(signal.SIGINT, term_program) |
| 108 | + if args.max_time: |
| 109 | + t = threading.Thread(target=execute, |
| 110 | + args=[args, input_read], |
| 111 | + daemon=True) |
| 112 | + t.start() |
| 113 | + t.join(args.max_time[0]) |
| 114 | + if t.is_alive(): |
| 115 | + UserIO.write_result(task=task_result, |
| 116 | + out_file=args.out_file[0], |
| 117 | + early_terminition=True) |
| 118 | + else: |
| 119 | + execute(args, input_read) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + main() |
0 commit comments