Skip to content

Commit

Permalink
Added Graph.create_literal() and Graph.create_typed_literal()
Browse files Browse the repository at this point in the history
Also added an `Enum` with XSD datatype URIs
  • Loading branch information
namedgraph committed Nov 4, 2023
1 parent 7be5130 commit 8555349
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
10 changes: 7 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

graph1 = dataset.default_graph
resource1 = graph1.create_resource("http://example.org/subject1")
resource1.add_property(graph1.create_property("http://example.org/property1"), Literal("Object1"))
resource1.add_property(graph1.create_property("http://example.org/property1"), graph1.create_literal("Object1"))
resource1.add_property(graph1.create_property("http://example.org/property2"), graph1.create_resource("http://example.org/object"))
resource1.add_property(graph1.create_property("http://example.org/property2"), graph1.create_typed_literal(42))

graph2 = dataset.get_named_graph("http://example.org/graph2")
resource2 = graph2.create_resource()
resource2.add_property(graph2.create_property("http://example.org/property3"), Literal("Object2"))
resource2.add_property(graph2.create_property("http://example.org/property3"), graph2.create_literal("Object2"))
resource3 = graph2.create_resource("http://example.org/subject3")
resource3.add_property(graph2.create_property("http://example.org/property4"), Literal("Object3"))
resource3.add_property(graph2.create_property("http://example.org/property4"), graph2.create_literal("Object3"))

graph3 = dataset.get_named_graph("http://example.org/graph3")
graph3.add(graph1)
Expand All @@ -37,6 +38,9 @@
serialize(list(graph1.list_triples()), output, "application/n-triples") # "application/n-quads" should work
print(output.getvalue())

print("\nlen(graph1):\n")
print(len(graph1))

print("\nresource1.uri:\n")
print(resource1.uri)

Expand Down
24 changes: 23 additions & 1 deletion oxijen/model_impl/impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from oxijen.rdf_model import Resource, Property, Graph, Dataset
from oxijen.model_impl.xsd import XSD
from pyoxigraph import Store, Triple, BlankNode, NamedNode, Literal, Quad, DefaultGraph
from typing import Iterator, Union, Optional
from typing import Iterator, Union, Optional, Any

class ResourceImpl(Resource):

Expand Down Expand Up @@ -62,13 +63,34 @@ def create_resource(self, uri: Optional[str] = None) -> Resource:
def create_property(self, uri: str) -> Property:
return ResourceImpl(NamedNode(uri), self)

def create_literal(self, value: str) -> Literal:
return Literal(value) # should it be xsd:string-typed by default as per RDF 1.1?

def create_typed_literal(self, value: Any, type_uri: Optional[str] = None) -> Literal:
if type_uri is None:
match value:
case int():
type_uri = XSD.INTEGER.value
case str():
type_uri = XSD.STRING.value
case float():
type_uri = XSD.FLOAT.value
# TO-DO: support more types
case _:
raise TypeError('Unsupported type conversion')

return Literal(str(value), datatype=NamedNode(type_uri))


class GraphStoreImpl(GraphImpl):

def __init__(self, store: Store, name: Union[BlankNode, NamedNode]):
self.store = store
self.name = name

def __len__(self) -> int:
return len(list(self.list_triples()))

def list_triples(self) -> Iterator[Triple]:
quads = self.store.quads_for_pattern(None, None, None, self.name)

Expand Down
9 changes: 9 additions & 0 deletions oxijen/model_impl/xsd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum

class XSD(Enum):

NS : str = "http://www.w3.org/2001/XMLSchema#"

INTEGER = NS + "integer"
STRING = NS + "string"
FLOAT = NS + "float"
14 changes: 13 additions & 1 deletion oxijen/rdf_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Iterator, Union, Optional
from typing import Iterator, Union, Optional, Any
from pyoxigraph import Store, Triple, Quad, Literal, NamedNode, BlankNode

class Resource(ABC):
Expand Down Expand Up @@ -53,6 +53,10 @@ class Property(Resource):

class Graph(ABC):

@abstractmethod
def __len__(self) -> int:
pass

@abstractmethod
def create_resource(self, uri: Optional[str] = None) -> Resource:
pass
Expand All @@ -61,6 +65,14 @@ def create_resource(self, uri: Optional[str] = None) -> Resource:
def create_property(self, uri: str) -> Property:
pass

@abstractmethod
def create_literal(self, value: str) -> Literal:
pass

@abstractmethod
def create_typed_literal(self, value: Any, type_uri: Optional[str] = None) -> Literal:
pass

@abstractmethod
def list_triples(self) -> Iterator[Triple]:
pass
Expand Down

0 comments on commit 8555349

Please sign in to comment.