Add ActionCode primitive type. Fix constraint checking.
This commit is contained in:
parent
0785b9218e
commit
59de61d0a3
11 changed files with 256 additions and 82 deletions
|
|
@ -21,7 +21,7 @@ def render_class_diagram(state, model, prefix_ids=""):
|
|||
is_abstract = False
|
||||
slot = model_od.get_slot(class_node, "abstract")
|
||||
if slot != None:
|
||||
is_abstract = od.read_primitive_value(bottom, slot, model_od.type_model)
|
||||
is_abstract, _ = od.read_primitive_value(bottom, slot, model_od.type_model)
|
||||
|
||||
if is_abstract:
|
||||
output += f"\nabstract class \"{name}\" as {make_id(class_node)}"
|
||||
|
|
@ -97,7 +97,7 @@ def render_object_diagram(state, m, mm, render_attributes=True, prefix_ids=""):
|
|||
for attr_name, attr_edge in attributes:
|
||||
slot = m_od.get_slot(obj_node, attr_name)
|
||||
if slot != None:
|
||||
output += f"\n{attr_name} => {json.dumps(od.read_primitive_value(bottom, slot, mm))}"
|
||||
output += f"\n{attr_name} => {json.dumps(od.read_primitive_value(bottom, slot, mm)[0])}"
|
||||
output += '\n}'
|
||||
|
||||
output += '\n'
|
||||
|
|
|
|||
|
|
@ -24,11 +24,13 @@ _NL: /(\r?\n[\t ]*)+/
|
|||
literal: INT
|
||||
| STR
|
||||
| BOOL
|
||||
| CODE
|
||||
|
||||
INT: /[0-9]+/
|
||||
STR: /"[^"]*"/
|
||||
| /'[^']*'/
|
||||
BOOL: "True" | "False"
|
||||
CODE: /`[^`]*`/
|
||||
|
||||
object: [IDENTIFIER] ":" IDENTIFIER [link_spec] _NL [_INDENT slot+ _DEDENT]
|
||||
link_spec: "(" IDENTIFIER "->" IDENTIFIER ")"
|
||||
|
|
@ -46,6 +48,12 @@ class TreeIndenter(Indenter):
|
|||
|
||||
parser = Lark(grammar, parser='lalr', postlex=TreeIndenter())
|
||||
|
||||
# internal use only
|
||||
# just a dumb wrapper to distinguish between code and string
|
||||
class _Code:
|
||||
def __init__(self, code):
|
||||
self.code = code
|
||||
|
||||
# given a concrete syntax text string, and a meta-model, parses the CS
|
||||
def parse_od(state, cs_text, mm):
|
||||
tree = parser.parse(cs_text)
|
||||
|
|
@ -70,7 +78,10 @@ def parse_od(state, cs_text, mm):
|
|||
return token == "True"
|
||||
|
||||
def STR(self, token):
|
||||
return str(token[1:-1]) # strip the ""
|
||||
return str(token[1:-1]) # strip the "" or ''
|
||||
|
||||
def CODE(self, token):
|
||||
return _Code(str(token[1:-1])) # strip the ``
|
||||
|
||||
def literal(self, el):
|
||||
return el[0]
|
||||
|
|
@ -111,9 +122,12 @@ def parse_od(state, cs_text, mm):
|
|||
tgt = od.create_integer_value(value_name, value)
|
||||
elif isinstance(value, str):
|
||||
tgt = od.create_string_value(value_name, value)
|
||||
elif isinstance(value, _Code):
|
||||
tgt = od.create_actioncode_value(value_name, value.code)
|
||||
else:
|
||||
raise Exception("Unimplemented type "+value)
|
||||
od.create_slot(attr_name, obj_name, tgt)
|
||||
|
||||
return obj_name
|
||||
|
||||
t = T(visit_tokens=True).transform(tree)
|
||||
|
|
|
|||
|
|
@ -4,13 +4,15 @@ from services import od
|
|||
from services.bottom.V0 import Bottom
|
||||
import json
|
||||
|
||||
def display_value(val: any):
|
||||
if isinstance(val, str):
|
||||
def display_value(val: any, type_name: str):
|
||||
if type_name == "ActionCode":
|
||||
return '`'+val+'`'
|
||||
elif type_name == "String":
|
||||
return '"'+val+'"'
|
||||
elif isinstance(val, int) or isinstance(val, bool):
|
||||
elif type_name == "Integer" or type_name == "Boolean":
|
||||
return str(val)
|
||||
else:
|
||||
raise Exception("don't know how to display value" + str(val))
|
||||
raise Exception("don't know how to display value" + type_name)
|
||||
|
||||
def render_od(state, m_id, mm_id, hide_names=True):
|
||||
bottom = Bottom(state)
|
||||
|
|
@ -25,8 +27,8 @@ def render_od(state, m_id, mm_id, hide_names=True):
|
|||
def write_attributes(object_node):
|
||||
o = ""
|
||||
for attr_name, slot_node in m_od.get_slots(object_node):
|
||||
value = m_od.read_slot(slot_node)
|
||||
o += f" {attr_name} = {display_value(value)}\n"
|
||||
value, type_name = m_od.read_slot(slot_node)
|
||||
o += f" {attr_name} = {display_value(value, type_name)}\n"
|
||||
return o
|
||||
|
||||
for class_name, objects in m_od.get_all_objects().items():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue