Skip to content

Solver

pd2af.asp.solver

Clingo glue: build a control, solve, return the model + element registry.

Functions:

Name Description
solve

Solve the ASP program for input_map in mode.

solve

solve(input_map: Map, mode: TransformationMode, set_active: list[str] | None = None, set_inactive: list[str] | None = None, set_all_active: bool = False, set_all_inactive: bool = False, exclude_groups: tuple[str, ...] = (), exclude_rules: tuple[str, ...] = ()) -> tuple[FactBase, dict]

Solve the ASP program for input_map in mode.

Returns the single clingo model of derived atoms together with the clingo_id -> model_element map the build pass resolves keys through. A satisfiable program deriving no atom returns an empty fact base; a program with no answer set raises ValueError.

Source code in src/pd2af/asp/solver.py
def solve(
    input_map: momapy.core.map.Map,
    mode: pd2af.modes.TransformationMode,
    set_active: list[str] | None = None,
    set_inactive: list[str] | None = None,
    set_all_active: bool = False,
    set_all_inactive: bool = False,
    exclude_groups: tuple[str, ...] = (),
    exclude_rules: tuple[str, ...] = (),
) -> tuple[clorm.FactBase, dict]:
    """Solve the ASP program for ``input_map`` in ``mode``.

    Returns the single clingo model of derived atoms together with the
    ``clingo_id -> model_element`` map the build pass resolves keys through.
    A satisfiable program deriving no atom returns an empty fact base; a
    program with no answer set raises ``ValueError``.
    """
    clingo_id_to_model_element = {}
    language = pd2af.modes.get_language_from_map_or_model(input_map)
    control = _make_control(
        input_map.model,
        clingo_id_to_model_element,
        mode=mode,
        language=language,
        set_active=set_active,
        set_inactive=set_inactive,
        set_all_active=set_all_active,
        set_all_inactive=set_all_inactive,
        exclude_groups=exclude_groups,
        exclude_rules=exclude_rules,
    )
    control.ground([("base", [])])
    clingo_models = []
    solve_result = control.solve(
        on_model=lambda model: clingo_models.append(model.facts(atoms=True))
    )
    if not clingo_models:
        raise ValueError(
            f"the ASP program has no answer set (solve result: {solve_result}); "
            f"an unsatisfiable program cannot be built into a map"
        )
    return clingo_models[0], clingo_id_to_model_element