-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimental: first pass at unified model transformations
- Loading branch information
Showing
4 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from abc import abstractmethod | ||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
from linkml_runtime import SchemaView | ||
from linkml_runtime.linkml_model import SlotDefinition, ClassDefinitionName | ||
|
||
|
||
@dataclass | ||
class ModelTransformer: | ||
""" | ||
Base class for all model transformers | ||
""" | ||
schemaview: SchemaView = None | ||
|
||
@abstractmethod | ||
def transform( | ||
self, tgt_schema_name: str = None, top_class: ClassDefinitionName = None | ||
) -> Any: | ||
raise NotImplementedError | ||
|
||
def unfold_slot_range(self, slot: SlotDefinition): | ||
union_ranges = [] | ||
for x in slot.any_of + slot.exactly_one_of: | ||
if x.range: | ||
union_ranges.append(x.range) | ||
if not union_ranges: | ||
union_ranges.append(slot.range) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from abc import ABC | ||
from dataclasses import dataclass | ||
from typing import Optional, List, Union | ||
|
||
from pydantic import BaseModel | ||
|
||
from linkml.transformers.model_transformer import ModelTransformer | ||
|
||
|
||
NAME = str | ||
|
||
|
||
class TargetLanguageProperties(BaseModel): | ||
""" | ||
Properties for a target language | ||
""" | ||
multiple_inheritance: Optional[bool] = None | ||
ranges_as_unions: Optional[bool] = None | ||
element_name_regex: Optional[str] = None | ||
reserved_words: Optional[List[str]] = None | ||
|
||
|
||
class ProgramElement(BaseModel, ABC): | ||
""" | ||
A base element in the object-oriented program model. | ||
Each element must have a name. This should uniquely | ||
identify within the context of the element type, and | ||
in the scope of a module | ||
""" | ||
name: NAME | ||
doc: Optional[str] = None | ||
|
||
|
||
class ProgramPackage(ProgramElement): | ||
pass | ||
|
||
|
||
class ProgramModule(ProgramElement): | ||
pass | ||
|
||
|
||
RANGE = Union["DisjunctiveRangeExpression", "AtomicRangeExpression"] | ||
|
||
class RangeExpression(BaseModel, ABC): | ||
""" | ||
A range expression | ||
""" | ||
optional: Optional[bool] = None | ||
|
||
|
||
class DisjunctiveRangeExpression(RangeExpression): | ||
""" | ||
A disjunctive range expression | ||
""" | ||
ranges: List[RANGE] | ||
|
||
|
||
class AtomicRangeExpression(RangeExpression): | ||
""" | ||
An atomic range expression | ||
""" | ||
range: NAME | ||
package: Optional[NAME] = None | ||
|
||
|
||
class ProgramField(ProgramElement): | ||
""" | ||
A field (attribute, slot) in a class. | ||
""" | ||
allowed_values: Optional[RANGE] = None | ||
default_expression: Optional[str] = None | ||
|
||
|
||
|
||
class ProgramClass(ProgramElement): | ||
""" | ||
A class in the object-oriented program model. | ||
""" | ||
is_a: Optional[NAME] = None | ||
mixins: Optional[List[NAME]] = None | ||
class_attributes: Optional[List[ProgramField]] = None | ||
fields: Optional[List[ProgramField]] = None | ||
|
||
|
||
|
||
class ProgramEnum(ProgramElement): | ||
pass | ||
|
||
|
||
@dataclass | ||
class ObjectOrientedModelTransformer(ModelTransformer): | ||
""" | ||
Transforms the source schema into an object-oriented model | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters