Skip to content

Commit

Permalink
Merge pull request #22 from krr-up/nico/convert
Browse files Browse the repository at this point in the history
Nico/convert
  • Loading branch information
nrueh authored Jul 4, 2024
2 parents 2b9db1c + 2e4dd8d commit 6ff39c7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/coom/city-bike.coom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// City Bike example
// Provided by denkbares GmbH
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024). To appear.
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024).

product {
Color color
Expand Down
2 changes: 1 addition & 1 deletion examples/coom/kids-bike.coom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Kids Bike example
// Provided by denkbares GmbH
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024). To appear.
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024).

product {
Color color
Expand Down
2 changes: 1 addition & 1 deletion examples/coom/travel-bike-simplified.coom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Simplified Travel Bike example
// Provided by denkbares GmbH
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024). To appear.
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024).

product {
num .#/l 0-200 totalVolume
Expand Down
2 changes: 1 addition & 1 deletion examples/coom/travel-bike.coom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Travel Bike example
// Provided by denkbares GmbH
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024). To appear.
// see Baumeister et al.: Towards Industrial-scale Product Configuration (2024).

product {
num .#/g 1-10000 totalWeight
Expand Down
27 changes: 14 additions & 13 deletions src/coomsuite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
SOLVERS = ["clingo", "fclingo"]


def convert_instance(coom_file: str, output_dir: Optional[str] = None) -> str: # nocoverage
def convert_instance(coom_file: str, outdir: Optional[str] = None) -> str: # nocoverage
"""
Converts a COOM instance into ASP
Args:
coom_file (str): COOM file .coom
output_dir (str, optional): Name of the output directory, by default the same of coom_file is used
"""
output_dir = "" if output_dir is None else output_dir
filename = splitext(basename(coom_file))[0] + "-coom.lp"
output_lp_file = join(output_dir, filename)

input_stream = FileStream(coom_file, encoding="utf-8")
asp_instance = run_antlr4_visitor(input_stream)
asp_instance = [f"coom_{a}" if a != "" else a for a in asp_instance]

with open(output_lp_file, "w", encoding="utf8") as f:
f.write("\n".join(asp_instance))
f.write("\n")
log.info("ASP file saved in %s", output_lp_file)
return output_lp_file
asp_instance = "\n".join([f"coom_{a}" if a != "" else a for a in run_antlr4_visitor(input_stream)])

if outdir is not None:
filename = splitext(basename(coom_file))[0] + "-coom.lp"
output_lp_file = join(outdir, filename)

with open(output_lp_file, "w", encoding="utf8") as f:
f.write(asp_instance)
f.write("\n")
log.info("ASP file saved in %s", output_lp_file)
return output_lp_file

return asp_instance
6 changes: 4 additions & 2 deletions src/coomsuite/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def main():
# log.error("error")

if args.command == "convert":
output_lp_file = convert_instance(args.input, args.output)
log.info("ASP file saved in %s", output_lp_file)
asp_instance = convert_instance(args.input, args.output)
if args.output is None:
print(asp_instance)

elif args.command == "solve":
log.info("Converting and solving COOM file %s", args.input)
with TemporaryDirectory() as temp_dir:
Expand Down
11 changes: 5 additions & 6 deletions src/coomsuite/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ def get(levels: list[tuple[str, int]], name: str) -> Optional[int]:
parser_convert.add_argument(
"input",
type=str,
help="Input the COOM file corresponding to the instance.",
help="""
Input the COOM file corresponding to the instance. Converted instance is printed to console by default.
Specify output directory with '--output' to save .""",
)
parser_convert.add_argument(
"--output",
"-o",
type=str,
help="Path to output directory. Same directory as input by default.",
default=None,
help="Path to output directory. (Optional)",
)

# -------------
Expand All @@ -90,10 +93,6 @@ def get(levels: list[tuple[str, int]], name: str) -> Optional[int]:
)
parser_solve.add_argument("--solver", "-s", type=str, help="Set solver", choices=SOLVERS, default="clingo")

# parser_solve.add_argument(
# "--profile", "-p", type=str, help="Set COOM profile", choices=COOM_PROFILES, default="all"
# )

parser_solve.add_argument(
"--output", "-o", type=str, help="Set console output format", choices=["asp", "coom"], default="asp"
)
Expand Down

0 comments on commit 6ff39c7

Please sign in to comment.