Skip to content

Commit

Permalink
registry saved in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheukting committed Apr 19, 2021
1 parent bee8580 commit 731eb1d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
36 changes: 25 additions & 11 deletions terminusdb_client/woqlquery/example_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,54 @@

# from woql_schema import WOQLSchema, Document, Property, WOQLObject

my_schema = WOQLSchema()


class Employee(Document):
# properties = [AddressOf, ContactNum, ManagedBy, Title, TeamMemberOf]
pass
schema = my_schema


class Address(WOQLObject):
# properties = [Postcode, StreetName, StreetNum, TownCity]
pass
schema = my_schema


class Team(Enums):
"""This is Team"""

value_set = {"IT", "Marketing"}
schema = my_schema


class AddressOf(Property):
domain = [Employee]
prop_range = [Address]

domain = {Employee}
prop_range = {Address}
schema = my_schema

print(Team.__name__)

my_schema = WOQLSchema()

print(my_schema)
my_schema.commit(None)
my_other_schema = my_schema.copy()
# my_other_schema = WOQLSchema()


class Title(Property):
domain = [Employee]
prop_range = ["xsd:string"]
domain = {Employee}
prop_range = {"xsd:string"}
schema = my_other_schema


my_other_schema = WOQLSchema()
class Intern(Employee):
schema = my_other_schema


# print("Intern")
# print(Intern.properties)

my_other_schema.commit(None)

# for item in my_other_schema.all_prop():
# print(item)

print(Team)
61 changes: 43 additions & 18 deletions terminusdb_client/woqlquery/woql_schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
from copy import deepcopy

from ..woqlclient.woqlClient import WOQLClient


class WOQLClass(type):
def __init__(cls, name, bases, nmspc):
super().__init__(name, bases, nmspc)
# cls.registry holds all subclasses
if not hasattr(cls, "registry"):
cls.registry = set()
cls.registry.add(cls)
cls.registry -= set(bases) # Remove base classes
# if not hasattr(cls, "registry"):
# cls.registry = set()
# cls.registry.add(cls)
# cls.registry -= set(bases) # Remove base classes

if cls.schema is not None:
if hasattr(cls, "domain"):
if not hasattr(cls.schema, "property"):
cls.schema.property = set()
cls.schema.property.add(cls)
# cls.schema.property -= set(bases) # Remove base classes

if hasattr(cls, "properties"):
if not hasattr(cls.schema, "object"):
cls.schema.object = set()
cls.schema.object.add(cls)
# cls.schema.object -= set(bases) # Remove base classes

if hasattr(cls, "domain"):
for item in cls.domain:
Expand All @@ -19,16 +34,22 @@ def __init__(cls, name, bases, nmspc):
item.domain.add(cls)

# Metamethods, called on class objects:
def __iter__(cls):
return iter(cls.registry)

# def __iter__(cls):
# if cls.schema is not None and hasattr(cls.schema, "registry"):
# return iter(cls.schema.registry)
#
def __str__(cls):
if cls in cls.registry:
return cls.__name__
return cls.__name__ + ": " + ", ".join([sc.__name__ for sc in cls])
# if cls.schema is not None and hasattr(cls.schema, "registry"):
# if cls in cls.schema.registry:
# return cls.__name__
# return cls.__name__ + ": " + ", ".join([sc.__name__ for sc in cls])
if hasattr(cls, "value_set"):
return cls.__name__ + ": " + ", ".join([val for val in cls.value_set])
return cls.__name__


class WOQLObject(metaclass=WOQLClass):
schema = None
properties = set()

def __init__(self):
Expand All @@ -44,20 +65,24 @@ class Enums(WOQLObject):


class Property(metaclass=WOQLClass):
schema = None
domain = set()
prop_range = set()

def __init__(cls, name, bases, nmspc):
super().__init__(name, bases, nmspc)
for item in cls.domain:
item.properties.append(cls)
print("add domain:", cls.__name__)
cardinality = None


class WOQLSchema:
def __init__(self):
pass

def commit(self, client: WOQLClient):
for item in [WOQLObject, Property]:
print(item)
pass

def all_obj(self):
return iter(self.object)

def all_prop(self):
return iter(self.property)

def copy(self):
return deepcopy(self)

0 comments on commit 731eb1d

Please sign in to comment.