-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move ModelBase and loader from bdd-dsl
- move base class for models with ID and types from bdd-dsl - add ModelLoader and AttrLoaderProtocol to manage functions for loading attributes depending on application context - add attr loader function for Python module attributes - add import function that uses the loaded attributes
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 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,51 @@ | ||
# SPDX-License-Identifier: MPL-2.0 | ||
from typing import Any, Dict, Optional, Protocol | ||
from rdflib import URIRef, Graph, RDF | ||
|
||
|
||
class ModelBase(object): | ||
"""All models should have an URI as ID and types""" | ||
|
||
id: URIRef | ||
types: set[URIRef] | ||
_attributes: Dict[URIRef, Any] | ||
|
||
def __init__(self, graph: Graph, node_id: URIRef) -> None: | ||
self.id = node_id | ||
self.types = set() | ||
for type_id in graph.objects(subject=node_id, predicate=RDF.type): | ||
assert isinstance(type_id, URIRef) | ||
self.types.add(type_id) | ||
|
||
assert len(self.types) > 0 | ||
self._attributes = {} | ||
|
||
def has_attr(self, key: URIRef) -> bool: | ||
return key in self._attributes | ||
|
||
def set_attr(self, key: URIRef, val: Any) -> None: | ||
self._attributes[key] = val | ||
|
||
def get_attr(self, key: URIRef) -> Optional[Any]: | ||
if key not in self._attributes: | ||
return None | ||
|
||
return self._attributes[key] | ||
|
||
|
||
class AttrLoaderProtocol(Protocol): | ||
def __call__(self, graph: Graph, model: ModelBase, **kwargs: Any) -> None: ... | ||
|
||
|
||
class ModelLoader(object): | ||
_loaders: list[AttrLoaderProtocol] | ||
|
||
def __init__(self) -> None: | ||
self._loaders = [] | ||
|
||
def register(self, loader: AttrLoaderProtocol) -> None: | ||
self._loaders.append(loader) | ||
|
||
def load_attributes(self, graph: Graph, model: ModelBase, **kwargs: Any): | ||
for loader in self._loaders: | ||
loader(graph=graph, model=model, **kwargs) |
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