Skip to content

Commit

Permalink
additional sql tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Dec 3, 2024
1 parent 9669c53 commit b3b603d
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 161 deletions.
187 changes: 183 additions & 4 deletions server/models/ontology.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from dataclasses import dataclass, field
from enum import StrEnum

from server.models.file_metadata import (
FileMetadata,
)


@dataclass(kw_only=True)
class Literal:
Expand All @@ -21,6 +17,27 @@ class Literal:
datatype: str = ""
language: str = ""

def to_dict(self):
return {
"value": self.value,
"datatype": self.datatype,
"language": self.language,
}

@classmethod
def from_dict(cls, data):
if "value" not in data:
raise ValueError("value is required")
if "datatype" not in data:
data["datatype"] = ""
if "language" not in data:
data["language"] = ""
return cls(
value=data["value"],
datatype=data["datatype"],
language=data["language"],
)


class NamedNodeType(StrEnum):
"""
Expand Down Expand Up @@ -62,6 +79,49 @@ class NamedNode:
description: list[Literal]
is_deprecated: bool

def to_dict(self):
return {
"belongs_to": self.belongs_to,
"type": self.type,
"full_uri": self.full_uri,
"label": [
label.to_dict() for label in self.label
],
"description": [
desc.to_dict() for desc in self.description
],
"is_deprecated": self.is_deprecated,
}

@classmethod
def from_dict(cls, data):
if "belongs_to" not in data:
raise ValueError("belongs_to is required")
if "type" not in data:
raise ValueError("type is required")
if "full_uri" not in data:
raise ValueError("full_uri is required")
if "label" not in data:
raise ValueError("label is required")
if "description" not in data:
raise ValueError("description is required")
if "is_deprecated" not in data:
data["is_deprecated"] = False
return cls(
belongs_to=data["belongs_to"],
type=data["type"],
full_uri=data["full_uri"],
label=[
Literal.from_dict(label)
for label in data["label"]
],
description=[
Literal.from_dict(desc)
for desc in data["description"]
],
is_deprecated=data["is_deprecated"],
)


@dataclass(kw_only=True)
class Individual(NamedNode):
Expand All @@ -81,6 +141,29 @@ def __post_init__(self):
f"Individual must have type {NamedNodeType.INDIVIDUAL}"
)

def to_dict(self):
return {
**super().to_dict(),
"type": self.type,
}

@classmethod
def from_dict(cls, data):
return cls(
belongs_to=data["belongs_to"],
type=data["type"],
full_uri=data["full_uri"],
label=[
Literal.from_dict(label)
for label in data["label"]
],
description=[
Literal.from_dict(desc)
for desc in data["description"]
],
is_deprecated=data["is_deprecated"],
)


@dataclass(kw_only=True)
class Class(NamedNode):
Expand All @@ -102,6 +185,30 @@ def __post_init__(self):
f"Class must have type {NamedNodeType.CLASS}"
)

def to_dict(self):
return {
**super().to_dict(),
"super_classes": self.super_classes,
}

@classmethod
def from_dict(cls, data):
return cls(
belongs_to=data["belongs_to"],
type=data["type"],
full_uri=data["full_uri"],
label=[
Literal.from_dict(label)
for label in data["label"]
],
description=[
Literal.from_dict(desc)
for desc in data["description"]
],
is_deprecated=data["is_deprecated"],
super_classes=data["super_classes"],
)


@dataclass(kw_only=True)
class Property(NamedNode):
Expand All @@ -127,6 +234,34 @@ def __post_init__(self):
f"Property must have type {NamedNodeType.PROPERTY}"
)

def to_dict(self):
return {
**super().to_dict(),
"property_type": self.property_type,
"range": self.range,
"domain": self.domain,
}

@classmethod
def from_dict(cls, data):
return cls(
belongs_to=data["belongs_to"],
type=data["type"],
full_uri=data["full_uri"],
label=[
Literal.from_dict(label)
for label in data["label"]
],
description=[
Literal.from_dict(desc)
for desc in data["description"]
],
is_deprecated=data["is_deprecated"],
property_type=data["property_type"],
range=data["range"],
domain=data["domain"],
)


@dataclass(kw_only=True)
class Ontology:
Expand All @@ -151,3 +286,47 @@ class Ontology:
individuals: list[Individual]
properties: list[Property]
all_named_nodes: list[NamedNode]

def to_dict(self):
return {
"uuid": self.uuid,
"file_uuid": self.file_uuid,
"name": self.name,
"classes": [
cls.to_dict() for cls in self.classes
],
"individuals": [
ind.to_dict() for ind in self.individuals
],
"properties": [
prop.to_dict() for prop in self.properties
],
"all_named_nodes": [
node.to_dict()
for node in self.all_named_nodes
],
}

@classmethod
def from_dict(cls, data):
return cls(
uuid=data["uuid"],
file_uuid=data["file_uuid"],
name=data["name"],
classes=[
Class.from_dict(cls)
for cls in data["classes"]
],
individuals=[
Individual.from_dict(ind)
for ind in data["individuals"]
],
properties=[
Property.from_dict(prop)
for prop in data["properties"]
],
all_named_nodes=[
NamedNode.from_dict(node)
for node in data["all_named_nodes"]
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def get_ontologies(
...

def create_ontology(
self, name: str, file_metadata: FileMetadata
self, name: str, content: bytes
) -> Ontology:
"""
Create an ontology
Parameters:
name (str): Ontology name
file_metadata (FileMetadata): Ontology file metadata
content (bytes): Ontology content
"""
...

Expand Down
4 changes: 4 additions & 0 deletions server/services/core/sqlite_db_service/tables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from server.services.core.sqlite_db_service.tables.file_metadata import (
FileMetadataTable,
)
from server.services.core.sqlite_db_service.tables.ontology import (
OntologyTable,
)
from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceMetadataTable,
)
Expand All @@ -12,4 +15,5 @@
"ConfigTable",
"WorkspaceMetadataTable",
"FileMetadataTable",
"OntologyTable",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import ForeignKey, String
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from server.services.core.sqlite_db_service.base import (
Expand Down
16 changes: 16 additions & 0 deletions server/services/core/sqlite_db_service/tables/ontology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from server.services.core.sqlite_db_service.base import (
Base,
)


class OntologyTable(Base):
__tablename__ = "ontology"

uuid: Mapped[str] = mapped_column(
String, primary_key=True
)
name: Mapped[str] = mapped_column(String)
json_file_uuid: Mapped[str] = mapped_column(String)
Loading

0 comments on commit b3b603d

Please sign in to comment.