def test_load_generators(): from pyflies.generators import generator_names names = generator_names() assert names
def pyflies(): """ Entry point to run code generator from command line. """ class MyParser(argparse.ArgumentParser): """ Custom arugment parser for printing help message in case of error. See http://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu """ def error(self, message): sys.stderr.write('error: %s\n' % message) self.print_help() sys.exit(2) parser = MyParser(description="pyflies model translator.") parser.add_argument('model', help="Model file name") args = parser.parse_args() try: model = pyflies_mm.model_from_file(args.model) except Exception as e: print(e) return gen_names = generator_names() if not model.targets: print("No targets specified.\n" "Define one or more target specification at " "the end of the file.\n" "Installed targets are: {} \n" .format(", ".join(gen_names))) return # Check if there is generator for each target for target in model.targets: if target.name not in gen_names: line, _ = \ model.metamodel.parser.pos_to_linecol(target._position) print("Unexisting target '{}' at line {}." .format(target.name, line)) for target in model.targets: # Call generator print("Generating code for target {}(out={})..."\ .format(target.name, target.output), end="") try: generate(model, target) except Exception as e: print(str(e)) return finally: print("Done") print("Code for target platform(s) generated sucessfully.")
def pyflies(): """ Entry point to run code generator from command line. """ class MyParser(argparse.ArgumentParser): """ Custom arugment parser for printing help message in case of error. See http://stackoverflow.com/questions/4042452/display-help-message-with-python-argparse-when-script-is-called-without-any-argu """ def error(self, message): sys.stderr.write('error: %s\n' % message) self.print_help() sys.exit(2) parser = MyParser(description="pyflies model translator.") parser.add_argument('model', help="Model file name") args = parser.parse_args() try: model = pyflies_mm.model_from_file(args.model) except Exception as e: print(e) return gen_names = generator_names() if not model.targets: print("No targets specified.\n" "Define one or more target specification at " "the end of the file.\n" "Installed targets are: {} \n".format(", ".join(gen_names))) return # Check if there is generator for each target for target in model.targets: if target.name not in gen_names: line, _ = \ model.metamodel.parser.pos_to_linecol(target._position) print("Unexisting target '{}' at line {}.".format( target.name, line)) for target in model.targets: # Call generator print("Generating code for target {}(out={})..."\ .format(target.name, target.output), end="") try: generate(model, target) except Exception as e: print(str(e)) return finally: print("Done") print("Code for target platform(s) generated sucessfully.")
def on_actionGenerateCode_triggered(self): gen_names = generator_names() if not hasattr(self.current_editor, 'model'): show_error("There is no model in the editor." "Type in experiment model and save it before running" " code generation.") return model = self.current_editor.model targets = model.targets if len(targets) == 0: show_error(("No targets specified.\n" + "Define one or more target specification at " + "the end of the file.\n" + "Installed targets are: {} \n") .format(", ".join(gen_names))) return # Check if there is generator for each target for target in targets: if target.name not in gen_names: line, _ = \ model.metamodel.parser.pos_to_linecol(target._position) show_error("Unexisting target '{}' at line {}." .format(target.name, line)) for target in targets: # Call generator try: generate(model, target) except PyFliesException as e: show_error(str(e)) return show_info("Code generation", "Code for target platform(s) generated sucessfully.")
def on_actionGenerateCode_triggered(self): gen_names = generator_names() if not hasattr(self.current_editor, 'model'): show_error("There is no model in the editor." "Type in experiment model and save it before running" " code generation.") return model = self.current_editor.model targets = model.targets if len(targets) == 0: show_error( ("No targets specified.\n" + "Define one or more target specification at " + "the end of the file.\n" + "Installed targets are: {} \n").format(", ".join(gen_names))) return # Check if there is generator for each target for target in targets: if target.name not in gen_names: line, _ = \ model.metamodel.parser.pos_to_linecol(target._position) show_error("Unexisting target '{}' at line {}.".format( target.name, line)) for target in targets: # Call generator try: generate(model, target) except PyFliesException as e: show_error(str(e)) return show_info("Code generation", "Code for target platform(s) generated sucessfully.")
def pyflies_model_processor(model, metamodel): """ Validates model, evaluates condition matches in stimuli definitions, creates a map from each condition to a set of stimuli that matches and sets default values. """ # Post-processing is done for each test type for block in model.blocks: if block.__class__.__name__ == "TestType": # Check that there is a condition variable named "response" if "response" not in block.condVarNames: line, col = \ metamodel.parser.pos_to_linecol( block.condVarNames[0]._tx_position) raise TextXSemanticError( "There must be condition variable named 'response' at {}". format((line, col)), line=line, col=col) # Create default duration if not given. if block.duration is None: default_duration = metamodel['Duration']() default_duration.value = 0 default_duration._from = defaults['duration_from'] default_duration.to = defaults['duration_to'] block.duration = default_duration # Create map of condition variables to collect their values. # For each variable name a list of values will be created # indexed by the condition ordinal number. condvar_values = {} for var in block.condVarNames: condvar_values[var] = [] for c in block.conditions: # Check if proper number of condition variable values is # specified in the current condition. if len(block.condVarNames) != len(c.varValues): line, col = \ metamodel.parser.pos_to_linecol(c._tx_position) raise TextXSemanticError( "There must be {} condition variable values at {}". format(len(condvar_values), (line, col)), line=line, col=col) # Fill the map of condition variable values for this condition. for idx, var_name in enumerate(block.condVarNames): condvar_values[var_name].append(c.varValues[idx]) # Attach the map of values to the test to be used in # condition match expression evaluation and in generator # templates. block.condvar_values = condvar_values def cond_matches(idx, c, exp): """ Recursively evaluates condition match expression. """ if exp.__class__.__name__ == "EqualsExpression": return condvar_values[exp.varName][idx] == exp.varValue elif exp.__class__.__name__ == "AndExpression": val = True for op in exp.operand: val = val and cond_matches(idx, c, op) return val elif exp.__class__.__name__ == "OrExpression": val = False for op in exp.operand: val = val or cond_matches(idx, c, op) return val # This should not happen assert False # We create, for each condition, a list # of stimuli that should be presented in the condition. # This list is stimuli_for_cond accessible on each # Condition object. # For each condition we iterate trough all stimuli # definitions and evaluate condition match. If the # condition evaluates to True stimulus is included # for condition. for idx, c in enumerate(block.conditions): c.stimuli_for_cond = [] for s in block.condStimuli: exp = s.conditionMatch.expression stimuli = s.stimuli if (exp.__class__.__name__ == "FixedCondition" and exp.expression == "all") or\ (exp.__class__.__name__ == "OrdinalCondition" and idx == exp.expression - 1) or\ (exp.__class__.__name__ == "ExpressionCondition" and cond_matches(idx, c, exp.expression)): # For each stimuli create a new instance # with all params resolved stimuli_for_match = [] for stimulus in stimuli: stimuli_for_match.append( create_resolve_stimulus( stimulus, block, c, metamodel)) c.stimuli_for_cond.append(stimuli_for_match) # Find special stimuli if any (error, correct, fixation) block.error = [] block.correct = [] block.fix = [] for s in block.condStimuli: exp = s.conditionMatch.expression if exp.__class__.__name__ == "FixedCondition" and\ exp.expression in ["error", "correct", "fixation"]: stimuli = [ create_resolve_stimulus(st, block, None, metamodel) for st in s.stimuli ] if exp.expression == "error": block.error = stimuli elif exp.expression == "correct": block.correct = stimuli elif exp.expression == "fixation": block.fix = stimuli # Check targets for target in model.targets: if target.name not in generator_names(): line, col = \ metamodel.parser.pos_to_linecol(target._tx_position) raise TextXSemanticError( "Unknown target '{}' at {}. Valid targets are {}".format( target.name, (line, col), list(generator_names())), line=line, col=col)
def pyflies_model_processor(model, metamodel): """ Validates model, evaluates condition matches in stimuli definitions, creates a map from each condition to a set of stimuli that matches and sets default values. """ # Post-processing is done for each test type for e in model.blocks: if e.__class__.__name__ == "TestType": # Check that there is a condition variable named "response" if "response" not in e.conditions.varNames: line, col = \ metamodel.parser.pos_to_linecol(e.conditions._tx_position) raise TextXSemanticError( "There must be condition variable named 'response' at {}" .format((line, col)), line=line, col=col) # Default duration if e.stimuli.duration is None: default_duration = metamodel['Duration']() default_duration.value = 0 default_duration._from = defaults['duration_from'] default_duration.to = defaults['duration_to'] e.stimuli.duration = default_duration # Create map of condition variables to collect their values. condvar_values = {} for var in e.conditions.varNames: condvar_values[var] = [] for c in e.conditions.conditions: # Check if proper number of condition variable values is # specified if len(condvar_values) != len(c.varValues): line, col = \ metamodel.parser.pos_to_linecol(c._tx_position) raise TextXSemanticError( "There must be {} condition variable values at {}" .format(len(condvar_values), (line, col)), line=line, col=col) # Fill the map of condition variable values for this condition. for idx, param_name in enumerate(e.conditions.varNames): condvar_values[param_name].append(c.varValues[idx]) # Attach the map of values to the test to be used in # condition match expression evaluation and in generator # templates. e.condvar_values = condvar_values def cond_matches(idx, c, exp): """ Recursively evaluates condition match expression. """ if exp.__class__.__name__ == "EqualsExpression": return condvar_values[exp.varName][idx] == exp.varValue elif exp.__class__.__name__ == "AndExpression": val = True for op in exp.operand: val = val and cond_matches(idx, c, op) return val elif exp.__class__.__name__ == "OrExpression": val = False for op in exp.operand: val = val or cond_matches(idx, c, op) return val # This should not happen assert False # We create, for each condition, a list # of stimuli that should be presented in the condition. # This list is stimuli_for_cond accessible on each # Condition object. # For each condition we iterate trough all stimuli # definitions and evaluate condition match. If the # condition evaluates to True stimulus is included # for condition. for idx, c in enumerate(e.conditions.conditions): c.stimuli_for_cond = [] for s in e.stimuli.condStimuli: exp = s.conditionMatch.expression stimuli = s.stimuli if (exp.__class__.__name__ == "FixedCondition" and exp.expression == "all") or\ (exp.__class__.__name__ == "OrdinalCondition" and idx == exp.expression - 1) or\ (exp.__class__.__name__ == "ExpressionCondition" and cond_matches(idx, c, exp.expression)): # For each stimuli create a new instance # with all params resolved stimuli_for_match = [] for stimulus in stimuli: stimuli_for_match.append( resolve(stimulus, e, c, metamodel)) c.stimuli_for_cond.append(stimuli_for_match) # Find special stimuli if any (error, correct, fixation) e.error = [] e.correct = [] e.fix = [] for s in e.stimuli.condStimuli: exp = s.conditionMatch.expression if exp.__class__.__name__ == "FixedCondition" and\ exp.expression in ["error", "correct", "fixation"]: stimuli = [resolve(st, e, None, metamodel) for st in s.stimuli] if exp.expression == "error": e.error = stimuli elif exp.expression == "correct": e.correct = stimuli elif exp.expression == "fixation": e.fix = stimuli # Check targets for target in model.targets: if target.name not in generator_names(): line, col = \ metamodel.parser.pos_to_linecol(target._tx_position) raise TextXSemanticError( "Unknown target '{}' at {}. Valid targets are {}" .format(target.name, (line, col), list(generator_names())), line=line, col=col)