Skip to content

Commit ca21889

Browse files
committed
Updating deserializers
1 parent 2fa2185 commit ca21889

File tree

5 files changed

+148
-15
lines changed

5 files changed

+148
-15
lines changed

pylasu/lionweb/ast_generation.py

+90-5
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,82 @@ def _generate_from_feature(feature: Feature, classdef: ClassDef):
117117
raise ValueError()
118118

119119

120+
def _generate_constructor() -> ast.FunctionDef:
121+
return ast.FunctionDef(
122+
name="__init__",
123+
args=ast.arguments(
124+
posonlyargs=[],
125+
args=[
126+
ast.arg(arg="self", annotation=None),
127+
ast.arg(arg="id", annotation=ast.Name(id="str", ctx=ast.Load())),
128+
ast.arg(arg="concept", annotation=ast.Name(id="Concept", ctx=ast.Load())),
129+
ast.arg(arg="position", annotation=ast.Subscript(
130+
value=ast.Name(id="Optional", ctx=ast.Load()),
131+
slice=ast.Name(id="Position", ctx=ast.Load()),
132+
ctx=ast.Load()
133+
)),
134+
],
135+
kwonlyargs=[], kw_defaults=[], defaults=[]
136+
),
137+
body=[
138+
# super().__init__(id=id, position=position, concept=concept)
139+
ast.Expr(value=ast.Call(
140+
func=ast.Attribute(
141+
value=ast.Call(func=ast.Name(id='super', ctx=ast.Load()), args=[], keywords=[]),
142+
attr='__init__',
143+
ctx=ast.Load()
144+
),
145+
args=[],
146+
keywords=[
147+
ast.keyword(arg='id', value=ast.Name(id='id', ctx=ast.Load())),
148+
ast.keyword(arg='position', value=ast.Name(id='position', ctx=ast.Load())),
149+
ast.keyword(arg='concept', value=ast.Name(id='concept', ctx=ast.Load())),
150+
]
151+
)),
152+
# self.set_id(id)
153+
ast.Expr(value=ast.Call(
154+
func=ast.Attribute(value=ast.Name(id='self', ctx=ast.Load()), attr='set_id', ctx=ast.Load()),
155+
args=[ast.Name(id='id', ctx=ast.Load())],
156+
keywords=[]
157+
)),
158+
# # self._set_containment_single_value(..., ...)
159+
# ast.Expr(value=ast.Call(
160+
# func=ast.Attribute(
161+
# value=ast.Name(id='self', ctx=ast.Load()),
162+
# attr='_set_containment_single_value',
163+
# ctx=ast.Load()
164+
# ),
165+
# args=[],
166+
# keywords=[
167+
# ast.keyword(
168+
# arg='containment',
169+
# value=ast.Call(
170+
# func=ast.Attribute(
171+
# value=ast.Name(id='concept', ctx=ast.Load()),
172+
# attr='get_containment_by_name',
173+
# ctx=ast.Load()
174+
# ),
175+
# args=[ast.Constant(value='externalName')],
176+
# keywords=[]
177+
# )
178+
# ),
179+
# ast.keyword(
180+
# arg='value',
181+
# value=ast.Name(id='externalName', ctx=ast.Load())
182+
# )
183+
# ]
184+
# ))
185+
],
186+
decorator_list=[],
187+
returns=None
188+
)
189+
190+
120191
def _generate_from_concept(classifier: Concept) -> ClassDef:
121192
bases = []
122193
if classifier.get_extended_concept().id == StarLasuBaseLanguage.get_astnode(LionWebVersion.V2023_1).id:
123194
if len(classifier.get_implemented()) == 0:
124-
bases.append('Node')
195+
bases.append('ASTNode')
125196
else:
126197
bases.append(classifier.get_extended_concept().get_name())
127198
special_interfaces = {
@@ -132,6 +203,7 @@ def _generate_from_concept(classifier: Concept) -> ClassDef:
132203
'com-strumenta-StarLasu-Documentation-id': 'StarLasuDocumentation',
133204
'com-strumenta-StarLasu-BehaviorDeclaration-id': 'StarLasuBehaviorDeclaration',
134205
'com-strumenta-StarLasu-EntityDeclaration-id': 'StarLasuEntityDeclaration',
206+
'com-strumenta-StarLasu-TypeAnnotation-id': 'StarLasuTypeAnnotation',
135207
'LionCore-builtins-INamed': 'StarLasuNamed'
136208
}
137209
for i in classifier.get_implemented():
@@ -144,12 +216,14 @@ def _generate_from_concept(classifier: Concept) -> ClassDef:
144216
dataclass_decorator = ast.Name(id="dataclass", ctx=ast.Load())
145217
classdef = ast.ClassDef(classifier.get_name(), bases=bases,
146218
keywords=[],
147-
body=[ast.Pass()],
219+
body=[],
148220
decorator_list=[dataclass_decorator])
149221

150222
for feature in classifier.get_features():
151223
_generate_from_feature(feature, classdef)
152224

225+
classdef.body.append(_generate_constructor())
226+
153227
return classdef
154228

155229

@@ -188,11 +262,22 @@ def ast_generation(click, language: Language, output):
188262
level=0
189263
)
190264
import_node = ast.ImportFrom(
265+
module='pylasu.lwmodel',
266+
names=[ast.alias(name='ASTNode', asname=None)],
267+
level=0
268+
)
269+
import_language = ast.ImportFrom(
270+
module='lionwebpython.language',
271+
names=[ast.alias(name='Concept', asname=None)],
272+
level=0
273+
)
274+
import_model = ast.ImportFrom(
191275
module='pylasu.model',
192-
names=[ast.alias(name='Node', asname=None)],
276+
names=[ast.alias(name='Position', asname=None)],
193277
level=0
194278
)
195-
module = ast.Module(body=[import_abc, import_dataclass, import_typing, import_enum, import_starlasu, import_node],
279+
module = ast.Module(body=[import_abc, import_dataclass, import_typing, import_enum, import_starlasu, import_node,
280+
import_language, import_model],
196281
type_ignores=[])
197282

198283
for element in language.get_elements():
@@ -230,7 +315,7 @@ def ast_generation(click, language: Language, output):
230315
elif isinstance(classifier, Interface):
231316
bases = []
232317
if len(classifier.get_extended_interfaces()) == 0:
233-
bases.append("Node")
318+
bases.append("ASTNode")
234319
# bases.append("ABC")
235320

236321
classdef = ast.ClassDef(classifier.get_name(), bases=bases,

pylasu/lionweb/deserializer_generation.py

+40-10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,42 @@ def generate_concept_deserializer(concept: Concept) -> ast.FunctionDef:
102102
for f in concept.all_features():
103103
field_name = calculate_field_name(f)
104104
constructor_assignments.append(ast.keyword(arg=field_name, value=ast.Constant(value=f.get_name())))
105+
106+
return_stmt = ast.Return(
107+
value=ast.Call(
108+
func=ast.Name(id=concept.get_name(), ctx=ast.Load()),
109+
args=[],
110+
keywords=[
111+
# id=serialized_instance.id
112+
ast.keyword(
113+
arg='id',
114+
value=ast.Attribute(
115+
value=ast.Name(id='serialized_instance', ctx=ast.Load()),
116+
attr='id',
117+
ctx=ast.Load()
118+
)
119+
),
120+
# position=properties_values[classifier.get_property_by_name('position')]
121+
ast.keyword(
122+
arg='position',
123+
value=ast.Subscript(
124+
value=ast.Name(id='properties_values', ctx=ast.Load()),
125+
slice=ast.Call(
126+
func=ast.Attribute(
127+
value=ast.Name(id='classifier', ctx=ast.Load()),
128+
attr='get_property_by_name',
129+
ctx=ast.Load()
130+
),
131+
args=[ast.Constant(value='position')],
132+
keywords=[]
133+
),
134+
ctx=ast.Load()
135+
)
136+
)
137+
]
138+
)
139+
)
140+
105141
return ast.FunctionDef(
106142
name=f"_deserialize_{to_snake_case(concept.get_name())}",
107143
args=ast.arguments(
@@ -117,13 +153,7 @@ def generate_concept_deserializer(concept: Concept) -> ast.FunctionDef:
117153
defaults=[]
118154
),
119155
body=[
120-
ast.Return(
121-
value=ast.Call(
122-
func=ast.Name(id=concept.get_name(), ctx=ast.Load()),
123-
args=[],
124-
keywords=constructor_assignments
125-
)
126-
)
156+
return_stmt
127157
],
128158
decorator_list=[],
129159
returns=ast.Name(id=concept.get_name(), ctx=ast.Load())
@@ -165,8 +195,8 @@ def deserializer_generation(click, language: Language, output):
165195
level=0
166196
)
167197
import_node = ast.ImportFrom(
168-
module='pylasu.model',
169-
names=[ast.alias(name='Node', asname=None)],
198+
module='pylasu.lwmodel',
199+
names=[ast.alias(name='ASTNode', asname=None)],
170200
level=0
171201
)
172202
import_ast = ast.ImportFrom(
@@ -176,7 +206,7 @@ def deserializer_generation(click, language: Language, output):
176206
level=0
177207
)
178208
import_primitives = ast.ImportFrom(
179-
module='primitive_types',
209+
module='.primitive_types',
180210
names=[ast.alias(name=e.get_name(), asname=None) for e in language.get_elements()
181211
if isinstance(e, PrimitiveType)],
182212
level=0

pylasu/lionweb/starlasu.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from lionwebpython.language import Language, Concept
44
from lionwebpython.language.lioncore_builtins import LionCoreBuiltins
55
from lionwebpython.language.primitive_type import PrimitiveType
6+
from lionwebpython.lionweb_version import LionWebVersion
67

78
from pylasu.model import Point, Position
89

pylasu/lwmodel/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .astnode import ASTNode
2+
3+
__all__ = ["ASTNode"]

pylasu/lwmodel/astnode.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Optional
2+
3+
from lionwebpython.language import Concept
4+
from lionwebpython.model.impl.dynamic_node import DynamicNode
5+
6+
from pylasu.model import Position
7+
8+
9+
class ASTNode(DynamicNode):
10+
11+
def __init__(self, id: str, concept: Concept, position: Optional[Position]=None):
12+
super().__init__(id=id, concept=concept)
13+
if position:
14+
self.set_property_value(property=concept.get_property_by_name('position'), value=position)

0 commit comments

Comments
 (0)