|
| 1 | +#!/usr/bin/python3 |
| 2 | +# |
| 3 | +# CV is a framework for continuous verification. |
| 4 | +# |
| 5 | +# Copyright (c) 2018-2023 ISP RAS (http://www.ispras.ru) |
| 6 | +# Ivannikov Institute for System Programming of the Russian Academy of Sciences |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +""" |
| 22 | +This script provides functionality to filter a given witnesses. |
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +from components.mea import * |
| 28 | + |
| 29 | + |
| 30 | +def _create_config(options=None, conversion_function="", comparison_function=""): |
| 31 | + additional_mf = [] |
| 32 | + is_debug = False |
| 33 | + if options: |
| 34 | + conversion_function = options.conversion |
| 35 | + comparison_function = options.comparison |
| 36 | + additional_mf = options.mf or [] |
| 37 | + is_debug = options.debug |
| 38 | + return { |
| 39 | + COMPONENT_MEA: { |
| 40 | + TAG_COMPARISON_FUNCTION: comparison_function, |
| 41 | + TAG_CONVERSION_FUNCTION: conversion_function, |
| 42 | + TAG_CONVERSION_FUNCTION_ARGUMENTS: { |
| 43 | + TAG_ADDITIONAL_MODEL_FUNCTIONS: additional_mf |
| 44 | + }, |
| 45 | + TAG_DEBUG: is_debug, |
| 46 | + TAG_CLEAN: False, |
| 47 | + TAG_UNZIP: False, |
| 48 | + TAG_DRY_RUN: False, |
| 49 | + TAG_SOURCE_DIR: None |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +def _parse_cmdline() -> tuple: |
| 55 | + parser = argparse.ArgumentParser() |
| 56 | + parser.add_argument("-d", "--directory", help="directory with witnesses to be filtered", |
| 57 | + required=True) |
| 58 | + parser.add_argument("--conversion", help="conversion function", |
| 59 | + default=DEFAULT_CONVERSION_FUNCTION) |
| 60 | + parser.add_argument("--comparison", help="comparison function", |
| 61 | + default=DEFAULT_COMPARISON_FUNCTION) |
| 62 | + parser.add_argument("--additional-model-functions", dest='mf', nargs='+', |
| 63 | + help="additional model functions, separated by whitespace") |
| 64 | + parser.add_argument('--debug', action='store_true') |
| 65 | + options = parser.parse_args() |
| 66 | + |
| 67 | + witnesses = glob.glob(os.path.join(options.directory, f"witness.*{GRAPHML_EXTENSION}")) |
| 68 | + return witnesses, _create_config(options) |
| 69 | + |
| 70 | + |
| 71 | +def execute_filtering(witnesses: list, config=None, conversion_function="", |
| 72 | + comparison_function="") -> list: |
| 73 | + """ |
| 74 | + Filter the given violation witnesses. |
| 75 | + """ |
| 76 | + if not config: |
| 77 | + config = _create_config(conversion_function=conversion_function, |
| 78 | + comparison_function=comparison_function) |
| 79 | + script_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir) |
| 80 | + install_dir = os.path.abspath(os.path.join(script_dir, DEFAULT_INSTALL_DIR)) |
| 81 | + |
| 82 | + if not os.path.exists(install_dir): |
| 83 | + install_dir = os.path.abspath(os.path.join(os.pardir, DEFAULT_INSTALL_DIR)) |
| 84 | + mea = MEA(config, witnesses, install_dir) |
| 85 | + mea.logger.debug(f"Received {len(witnesses)} witnesses") |
| 86 | + processed_witnesses = mea.filter() |
| 87 | + mea.logger.debug(f"Number of unique witnesses is {len(processed_witnesses)}") |
| 88 | + return processed_witnesses |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + m_witnesses, m_config = _parse_cmdline() |
| 93 | + for witness in execute_filtering(m_witnesses, m_config): |
| 94 | + print(f"Unique witness '{witness}'") |
0 commit comments