Skip to content

Commit

Permalink
#40: improve statement visiting
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanzwicknagl committed Dec 21, 2023
1 parent ef1e9d3 commit 748c8ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/src/viasp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import textwrap
import webbrowser
import importlib.metadata
from clingo.script import enable_python

from viasp import Control
from viasp.server import startup
Expand Down Expand Up @@ -84,6 +85,7 @@ def start():
options = [str(models)]

backend_url = f"{DEFAULT_BACKEND_PROTOCOL}://{host}:{port}"
enable_python()
ctl = Control(options, viasp_backend_url=backend_url)
for path in paths:
ctl.load(path)
Expand Down
34 changes: 28 additions & 6 deletions backend/src/viasp/asp/reify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from typing import Dict, List, Tuple, Iterable, Set, Collection, Any, Union, Sequence
from typing import Dict, List, Tuple, Iterable, Set, Collection, Any, Union, Sequence, cast

import clingo
import networkx as nx
Expand Down Expand Up @@ -311,11 +311,33 @@ def get_body_aggregate_elements(self, body: Sequence[AST]) -> List[AST]:
return body_aggregate_elements

def visit_ShowTerm(self, showTerm: AST):
new_rule = ast.Rule(
showTerm.location,
ast.Literal(showTerm.location, ast.Sign.NoSign, showTerm.term),
showTerm.body)
parse_string(place_ast_at_location(new_rule), lambda rule: self.visit(rule))
if (hasattr(showTerm, "location") and isinstance(showTerm.location, ast.Location)
and hasattr(showTerm, "term") and isinstance(showTerm.term, AST)
and hasattr(showTerm, "body") and isinstance(showTerm.body, Sequence)
and all(isinstance(elem, AST) for elem in showTerm.body)):
new_head = ast.Literal(
showTerm.location,
ast.Sign.NoSign,
ast.SymbolicAtom(
showTerm.term
)
)
self.visit(
ast.Rule(
showTerm.location,
new_head,
cast(Sequence, showTerm.body))
)
else:
print(f"Plan B for ShowTerm: {showTerm}", flush=True)
new_rule = ast.Rule(
cast(ast.Location, showTerm.location),
ast.Literal(
cast(ast.Location, showTerm.location),
ast.Sign.NoSign,
cast(AST, showTerm.term)),
cast(Sequence, showTerm.body))
parse_string(place_ast_at_location(new_rule), lambda rule: self.visit(rule))


def visit_Minimize(self, minimize: Minimize):
Expand Down

0 comments on commit 748c8ee

Please sign in to comment.