def count_violations_memoized(processed_specs: Dict[str, Dict], task: Task): key = task.to_asp() if key not in processed_specs: violations = count_violations(task) if violations is not None: processed_specs[key] = violations return processed_specs[key]
def run_draco(task: Task, constants: Dict[str, str] = None, files: List[str] = None, silence_warnings=False, debug=False) -> Tuple[str, str]: ''' Run draco and return stderr and stdout ''' # default args files = files or DRACO_LP constants = constants or {} options = ['--outf=2', '--quiet=1,2,2'] if silence_warnings: options.append('--warn=no-atom-undefined') for name, value in constants.items(): options.append(f'-c {name}={value}') cmd = ['clingo'] + options logger.debug('Command: %s', ' '.join(cmd)) proc = subprocess.Popen( args=cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) task_program = task.to_asp() file_names = [os.path.join(DRACO_LP_DIR, f) for f in files] asp_program = b'\n'.join(map(load_file, file_names)) + task_program.encode('utf8') if debug: with tempfile.NamedTemporaryFile(mode='w', delete=False) as fd: fd.write(task_program) logger.info('Debug ASP with "clingo %s %s"', ' '.join(file_names), fd.name) stdout, stderr = proc.communicate(asp_program) return (stderr, stdout)