def main(argv, result_handler=None): import argparse parser = argparse.ArgumentParser() parser.add_argument('filename', metavar='MODEL', type=str, help='input ProbLog model') parser.add_argument('--format', choices=('dot', 'pl', 'cnf', 'svg', 'internal'), default=None, help='output format') parser.add_argument('--break-cycles', action='store_true', help='perform cycle breaking') parser.add_argument('--transform-nnf', action='store_true', help='transform to NNF') parser.add_argument('--keep-all', action='store_true', help='also output deterministic nodes') parser.add_argument('--keep-duplicates', action='store_true', help='don\'t eliminate duplicate literals') parser.add_argument('--any-order', action='store_true', help='allow reordering nodes') parser.add_argument('--hide-builtins', action='store_true', help='hide deterministic part based on builtins') parser.add_argument('--propagate-evidence', action='store_true', help='propagate evidence') parser.add_argument('--propagate-weights', action='store_true', help='propagate evidence') parser.add_argument( '--compact', action='store_true', help='allow compact model (may remove some predicates)') parser.add_argument('--noninterpretable', action='store_true') parser.add_argument('--web', action='store_true', help=argparse.SUPPRESS) parser.add_argument('--verbose', '-v', action='count', default=0, help='Verbose output') parser.add_argument('-o', '--output', type=str, help='output file', default=None) parser.add_argument( '-a', '--arg', dest='args', action='append', help='Pass additional arguments to the cmd_args builtin.') args = parser.parse_args(argv) outformat = args.format outfile = sys.stdout if args.output: outfile = open(args.output, 'w') if outformat is None: outformat = os.path.splitext(args.output)[1][1:] if outformat == 'cnf' and not args.break_cycles: print( 'Warning: CNF output requires cycle-breaking; cycle breaking enabled.', file=sys.stderr) if args.transform_nnf: target = LogicNNF elif args.break_cycles or outformat == 'cnf': target = LogicDAG else: target = LogicFormula if args.propagate_weights: semiring = SemiringLogProbability() else: semiring = None if args.web: print_result = print_result_json else: print_result = print_result_standard try: gp = target.createFrom(PrologFile(args.filename, parser=DefaultPrologParser( ExtendedPrologFactory())), label_all=not args.noninterpretable, avoid_name_clash=not args.compact, keep_order=not args.any_order, keep_all=args.keep_all, keep_duplicates=args.keep_duplicates, hide_builtins=args.hide_builtins, propagate_evidence=args.propagate_evidence, propagate_weights=semiring, args=args.args) if outformat == 'pl': rc = print_result((True, gp.to_prolog()), output=outfile) elif outformat == 'dot': rc = print_result((True, gp.to_dot()), output=outfile) elif outformat == 'svg': dot = gp.to_dot() tmpfile = mktempfile('.dot') with open(tmpfile, 'w') as f: print(dot, file=f) svg = subprocess_check_output(['dot', tmpfile, '-Tsvg']) rc = print_result((True, svg), output=outfile) elif outformat == 'cnf': cnfnames = False if args.verbose > 0: cnfnames = True rc = print_result( (True, CNF.createFrom(gp).to_dimacs(names=cnfnames)), output=outfile) elif outformat == 'internal': rc = print_result((True, str(gp)), output=outfile) else: rc = print_result((True, gp.to_prolog()), output=outfile) except Exception as err: import traceback err.trace = traceback.format_exc() rc = print_result((False, err)) if args.output: outfile.close() if rc: sys.exit(rc)
def string_to_clause(term_string, engine=None, database=None, **kwargs): parser = PrologParser(ExtendedPrologFactory()) res = parser.parseString(unquote(term_string)) return res[0]