Skip to content

Commit

Permalink
Merge pull request #56 from DARPA-ASKEM/feature/decapodes-reset
Browse files Browse the repository at this point in the history
Add decapode reset message handler
  • Loading branch information
mwdchang authored Jan 10, 2024
2 parents 02a0d01 + 6b1ec3b commit 0797cf1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
33 changes: 26 additions & 7 deletions src/askem_beaker/contexts/decapodes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, beaker_kernel: "LLMKernel", subkernel: "BaseSubkernel", confi

async def setup(self, config, parent_header):
self.config = config
var_names = list(config.keys())

def fetch_model(model_id):
meta_url = f"{os.environ['DATA_SERVICE_URL']}/models/{model_id}"
Expand All @@ -40,16 +39,16 @@ def fetch_model(model_id):
model = json.dumps(response.json()["model"])
return model

load_commands = [
'%s = parse_json_acset(SummationDecapode{Symbol, Symbol, Symbol},"""%s""")' % (var_name, fetch_model(decapode_id))
for var_name, decapode_id in config.items()
]
variables = {
var_name: fetch_model(decapode_id) for var_name, decapode_id in config.items()
}

command = "\n".join(
[
self.get_code("setup"),
"decapode = @decapode begin end",
*load_commands
self.get_code("load_model", {
"variables": variables,
}),
]
)
print(f"Running command:\n-------\n{command}\n---------")
Expand Down Expand Up @@ -220,3 +219,23 @@ async def save_amr_request(self, message):
self.beaker_kernel.send_response(
"iopub", "save_model_response", content, parent_header=message.header
)

@intercept()
async def reset_request(self, message):
content = message.content

model_name = content.get("model_name", self.target)
reset_code = self.get_code("reset", {
"var_name": model_name,
})
reset_result = await self.execute(reset_code)

content = {
"success": True,
"executed_code": reset_result["parent"].content["code"],
}

self.beaker_kernel.send_response(
"iopub", "reset_response", content, parent_header=message.header
)
await self.send_decapodes_preview_message(parent_header=message.header)
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ACSets, Decapodes, SyntacticModels
import HTTP, JSON3, DisplayAs
_amr = JSON3.read(String(HTTP.get("{{ model_url }}").body), SyntacticModels.ASKEMDecapodes.ASKEMDecaExpr)
{{ var_name|default("model") }} = Decapodes.SummationDecapode(_amr.model)
Dict(["var_name" => "{{ var_name|default("model") }}"]) |> DisplayAs.unlimited JSON3.write # TODO: Fix 'Unable to parse result' false error
{% for var_name, definition in variables.items() %}
{{ var_name }} = parse_json_acset(SummationDecapode{Symbol, Symbol, Symbol},"""{{ definition }}""")
_model_reset_cache[:{{ var_name }}] = {{ var_name }}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ var_name|default("decapode") }} = (haskey(_model_reset_cache, :{{ var_name|default("decapode") }}) ? _model_reset_cache[:{{ var_name|default("decapode") }}] : @decapode begin end)
3 changes: 3 additions & 0 deletions src/askem_beaker/contexts/decapodes/procedures/julia/setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ using ACSets
using Catlab, Catlab.Graphics
using CombinatorialSpaces
using Decapodes

_model_reset_cache = Dict()
decapode = @decapode begin end

0 comments on commit 0797cf1

Please sign in to comment.