From 85553498162459cb82437ff4e23d27efbf758566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Jusevi=C4=8Dius?= Date: Sat, 4 Nov 2023 14:43:41 +0100 Subject: [PATCH] Added `Graph.create_literal()` and `Graph.create_typed_literal()` Also added an `Enum` with XSD datatype URIs --- example.py | 10 +++++++--- oxijen/model_impl/impl.py | 24 +++++++++++++++++++++++- oxijen/model_impl/xsd.py | 9 +++++++++ oxijen/rdf_model.py | 14 +++++++++++++- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 oxijen/model_impl/xsd.py diff --git a/example.py b/example.py index a96f800..a28f326 100644 --- a/example.py +++ b/example.py @@ -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) @@ -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) diff --git a/oxijen/model_impl/impl.py b/oxijen/model_impl/impl.py index 6503353..0acbc1c 100644 --- a/oxijen/model_impl/impl.py +++ b/oxijen/model_impl/impl.py @@ -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): @@ -62,6 +63,24 @@ 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): @@ -69,6 +88,9 @@ 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) diff --git a/oxijen/model_impl/xsd.py b/oxijen/model_impl/xsd.py new file mode 100644 index 0000000..0300bba --- /dev/null +++ b/oxijen/model_impl/xsd.py @@ -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" \ No newline at end of file diff --git a/oxijen/rdf_model.py b/oxijen/rdf_model.py index eea2b7c..cf076db 100644 --- a/oxijen/rdf_model.py +++ b/oxijen/rdf_model.py @@ -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): @@ -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 @@ -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