def run_clingo(
    draco_query: List[str],
    constants: Dict[str, str] = None,
    files: List[str] = None,
    relax_hard=False,
    silence_warnings=False,
    debug=False,
) -> Tuple[str, str]:
    """
    Run draco and return stderr and stdout
    """

    # default args
    files = files or DRACO_LP
    if relax_hard and "hard-integrity.lp" in files:
        files.remove("hard-integrity.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
    )

    program = "\n".join(draco_query)
    file_names = [os.path.join(DRACO_LP_DIR, f) for f in files]
    asp_program = b"\n".join(map(load_file, file_names)) + program.encode("utf8")

    if debug:
        with tempfile.NamedTemporaryFile(mode="w", delete=False) as fd:
            fd.write(program)

            logger.info('Debug ASP with "clingo %s %s"', " ".join(file_names), fd.name)

    stdout, stderr = proc.communicate(asp_program)

    return (stderr, stdout)
def run_clingo ( draco_query : List [ str ] , constants : Dict [ str , str ] = None , files : List [ str ] = None , relax_hard = False , silence_warnings = False , debug = False , ) -> Tuple [ str , str ] : files = files or DRACO_LP if relax_hard and "hard-integrity.lp" in files : files . remove ( "hard-integrity.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 ) program = "\n" . join ( draco_query ) file_names = [ os . path . join ( DRACO_LP_DIR , f ) for f in files ] asp_program = b"\n" . join ( map ( load_file , file_names ) ) + program . encode ( "utf8" ) if debug : with tempfile . NamedTemporaryFile ( mode = "w" , delete = False ) as fd : fd . write ( program ) logger . info ( 'Debug ASP with "clingo %s %s"' , " " . join ( file_names ) , fd . name ) stdout , stderr = proc . communicate ( asp_program ) return ( stderr , stdout )
Run draco and return stderr and stdout