import argparse import csv from pathlib import Path from ee import tools from ee.part import Part, load_db from ee.tools import mk_dirs def work(in_path: Path, work_dir: Path): in_parts = load_db(in_path) print_header = False tools.mk_dirs(work_dir) with (work_dir / "facts.dl").open("w") as f: print(".decl part(uri:symbol, ref:symbol)", file=f) print(".input part", file=f) print("", file=f) print(".decl fact(part_uri:symbol, key:symbol, value:symbol)", file=f) print(".input fact", file=f) part_count = 0 fact_count = 0 in_dir = work_dir / "in" mk_dirs(in_dir) with (in_dir / "part.facts").open("w") as part_f: with (in_dir / "fact.facts").open("w") as fact_f: part_csv = csv.writer(part_f, dialect="excel-tab") if print_header: part_csv.writerow(["uri", "ref"]) fact_csv = csv.writer(fact_f, dialect="excel-tab") if print_header: fact_csv.writerow(["part_uri", "key", "value"]) for xml in load_db(in_path).iterparts(): p = Part(xml) sch_ref = p.get_only_schematic_reference() part_csv.writerow([p.uri, sch_ref.referenceProp if sch_ref else None]) part_count += 1 for fact in p.get_facts(): fact_csv.writerow([p.uri, fact.keyProp, fact.valueProp]) fact_count += 1 print("Loaded clauses: {} parts, {} facts".format(part_count, fact_count)) parser = argparse.ArgumentParser() parser.add_argument("--in", dest="in_path", required=True, metavar="PART DB") parser.add_argument("--work", required=True, metavar="DIR") args = parser.parse_args() work(Path(args.in_path), Path(args.work))