def _instantiate(self, metamod_term): """Instantiate the extension on a given metamodule""" # Build the extended strategy language grammar module grammar_modterm = self.makeSlangGrammar(metamod_term) grammar_mod = maude.downModule(grammar_modterm) if grammar_mod is None: show_error('Cannot use grammar module' + (f': {str(grammar_modterm)}' if args.verbose else '.')) return None # Build the extended metalevel module for the extended language metaslang_modterm = self.makeMetaSlang(metamod_term) metaslang_mod = maude.downModule(metaslang_modterm) if metaslang_mod is None: show_error('Cannot use extended metalevel' + (f': {str(metaslang_modterm)}' if args.verbose else '.')) return None # Get the stratParse and transform operators to parse and convert # the strategies to the standard language module_kind = metaslang_mod.findSort('Module').kind() term_kind = metaslang_mod.findSort('Term').kind() strat_kind = metaslang_mod.findSort('Strategy').kind() nat_kind = metaslang_mod.findSort('Nat').kind() stratParse = metaslang_mod.findSymbol('stratParse', [module_kind, term_kind], strat_kind) transform = metaslang_mod.findSymbol('transform', [strat_kind, nat_kind], strat_kind) zero = metaslang_mod.parseTerm('0', nat_kind) strategy_kind = grammar_mod.findSort('@Strategy@').kind() if any(symb is None for symb in [stratParse, transform, zero, strategy_kind]): show_error('Missing requirements in the strategy extension module.') return None # The metarepresentation of the grammar module is in the extension term, # but the instance need it as a term of the extended metalevel grammar_modterm_in_metaslang = metaslang_mod.parseTerm(str(grammar_modterm)) return SlangExtInstance(grammar_mod, grammar_modterm_in_metaslang, metaslang_mod, stratParse, transform, strategy_kind, zero)
def __init__(self, initial, strategy=None, filename=None, module=None, metamodule=None, opaque=(), biased_matchrew=True, already_loaded=False, single_use=False): """ Generate a Maude model for model-checking. :param initial: Initial term :type initial: str or maude.Term :param strategy: Strategy to control rewriting :type strategy: str or maude.StrategyExpression or None :param filename: Name of the file to be loaded :type filename: str or None :param module: Module where to model check :type module: str or maude.Module or None :param metamodule: Metarepresentation of the module where to model check (parsed in module) :type metamodule: str or maude.Term or None :param opaque: List of opaque strategies :type opaque: list of str :param biased_matchrew: Whether the biased matchrew feature is enabled :param biased_matchrew: bool :param already_loaded: Whether the file should not be loaded again :param already_loaded: bool :param single_use: Whether a single use of the model with the graph method is intended. Otherwise, graphs will be cached between calls to check :param single_use: bool """ # File name self.filename = filename needs_loading = not isinstance(module, maude.Module) \ and not isinstance(initial, maude.Term) \ and not already_loaded if needs_loading: if self.filename is None: raise ValueError( 'filename must not be empty if not already loaded') else: maude.load(self.filename) # Module if module is None: if isinstance(initial, maude.Term) and metamodule is None: self.module = initial.symbol().getModule() else: self.module = maude.getCurrentModule() self.module_str = str(self.module) elif isinstance(module, str): self.module = maude.getModule(module) self.module_str = module elif isinstance(module, maude.Module): self.module = module self.module_str = str(module) else: raise TypeError( f"unexpected type '{type(module).__name__}' for module") # Metamodule if isinstance(metamodule, str): self.metamodule = self.module.parseTerm(metamodule) self.module = maude.downModule(metamodule) elif isinstance(metamodule, maude.Term): self.metamodule = metamodule self.module = maude.downModule(metamodule) else: self.metamodule = None # Initial term if isinstance(initial, str): self.initial = self.module.parseTerm(initial) self.initial_str = initial elif isinstance(initial, maude.Term): self.initial = initial self.initial_str = str(initial) else: raise TypeError( f"unexpected type '{type(module).__name__}' for term") # Strategy expression if isinstance(strategy, str): self.strategy = self.module.parseStrategy(strategy) self.strategy_str = strategy elif isinstance(strategy, maude.StrategyExpression): self.strategy = strategy self.strategy_str = str(strategy) else: self.strategy = None self.strategy_str = None # Opaque strategies and biased_matchrew self.opaque = opaque self.biased_matchrew = biased_matchrew # Build the parser self.parser = _formulae.Parser() self.parser.set_module(self.module, metamodule=self.metamodule) # Look for the Formula sort formula_sort = self.module.findSort('Formula') if formula_sort is None: raise ValueError( 'the given module is not prepared for model checking') self.formula_kind = self.module.findSort('Formula').kind() # Graphs (wrapped or not) if self.strategy is None: self.graph = maude.RewriteGraph(self.initial) else: self.graph = maude.StrategyRewriteGraph(self.initial, self.strategy, self.opaque, self.biased_matchrew) self.wgraphs = {} self.single_use = single_use
def parse_initial_data(args): """Inits Maude and parse common initial data of a model-checking problem""" maude.init(advise=args.advise) data = InitialData() # Checks whether the file exists data.filename = find_maude_file(args.file) if data.filename is None: usermsgs.print_error('No such file.') return None if not maude.load(args.file): usermsgs.print_error('Error loading file') return None # Loads the module if args.module is None: data.module = maude.getCurrentModule() if data.module is None: usermsgs.print_error('No last module.') return None else: data.module = maude.getModule(args.module) if data.module is None: usermsgs.print_error(f'Module {args.module} does not exist.') return None # Loads a metamodule (if required) if args.metamodule is not None: mt = data.module.parseTerm(args.metamodule) if mt is None: usermsgs.print_error('Bad parse for metamodule term.') return None data.metamodule = mt data.module = maude.downModule(mt) if data.module is None: usermsgs.print_error('Bad metamodule.') return None # Parse the initial term data.term = data.module.parseTerm(args.initial) if data.term is None: usermsgs.print_error('Bad parse for initial term') return None # Parse the strategy if args.strategy is not None: data.strategy = data.module.parseStrategy(args.strategy) if data.strategy is None: usermsgs.print_error('Bad parse for strategy') return None else: data.strategy = None # Opaque strategies and full matchrew data.opaque = [] if args.opaque == '' else args.opaque.split(',') data.full_matchrew = args.full_matchrew return data
if not os.path.isfile(args.file): show_error('Cannot find Maude file: ' + args.file) sys.exit(1) maude.load(args.extension) maude.load(args.file) # The module where the extended strategies will be used... base_module = maude.getCurrentModule() if args.module is None else maude.getModule(args.module) # ...unless a metamodule is specified if args.metamodule is not None: metamodule = args.metamodule metamod_term = base_module.parseTerm(metamodule) module = maude.downModule(metamod_term) metamodule = str(metamod_term) elif args.extmodule is None: metamodule = f"upModule('{str(base_module)}, true)" module = base_module # Load the strategy language extension extension = SlangExtension(args.view) if not extension.load(): sys.exit(1) # The subject module can be a standard module or a extended one if args.extmodule is None: instance = extension.instantiate(metamodule)
# Escape line breaks and other to make this a Maude string membs = membs.replace('\n', '\\n').replace('"', '\\"').replace('\t', ' ') # Construct the module where to model check membrane systems maude.load('memparse') membrane_external = maude.getModule('MEMBRANE-EXTERNAL') module_term = membrane_external.parseTerm(f'makeMMCModule("{membs}", {"false" if args.priority == "weak" else "true"}, {args.objbound})') nrewrites = module_term.reduce() if args.verbose: print(f'Rewriting model generated from membrane specification ({nrewrites} rewrites)') # The rewrite theory for the membrane system as a Module object mmc_module = maude.downModule(module_term) if mmc_module is None: print('Error when computing the Maude module for the membrane system.') sys.exit(1) # Parse the initial membrane t = mmc_module.parseTerm(args.membrane) s = mmc_module.parseStrategy('%mcomp%') if t is None: print('Bad parse for initial membrane.') sys.exit(1) # Execute the model checkers model = MaudeModel(t, strategy=s, filename='memparse.maude',