Skip to content

Commit

Permalink
Added support for language-tagged Literals
Browse files Browse the repository at this point in the history
  • Loading branch information
namedgraph committed Nov 4, 2023
1 parent 8555349 commit 2317eb4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.DS_Store
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

Expand Down
24 changes: 16 additions & 8 deletions oxijen/model_impl/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,26 @@ 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_literal(self, value: str, language: Optional[str] = None) -> Literal:
return Literal(value, language=language) # 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:
def create_typed_literal(self, value: Any, datatype: Optional[Union[str, NamedNode]] = None) -> Literal:
if datatype is None:
match value:
case int():
type_uri = XSD.INTEGER.value
datatype = NamedNode(XSD.INTEGER.value)
case str():
type_uri = XSD.STRING.value
datatype = NamedNode(XSD.STRING.value)
case float():
type_uri = XSD.FLOAT.value
datatype = NamedNode(XSD.FLOAT.value)
# TO-DO: support more types
case _:
raise TypeError('Unsupported type conversion')
else:
if type(datatype) is str:
datatype = NamedNode(datatype)

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


class GraphStoreImpl(GraphImpl):
Expand All @@ -105,6 +108,11 @@ def add(self, triples: Union[Iterator[Triple], 'Graph']) -> 'Graph':

return self

def remove_all(self) -> 'Graph':
self.store.remove_graph(self.name)

return self


class DatasetStoreImpl(Dataset):

Expand Down
8 changes: 6 additions & 2 deletions oxijen/rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ def create_property(self, uri: str) -> Property:
pass

@abstractmethod
def create_literal(self, value: str) -> Literal:
def create_literal(self, value: str, language: Optional[str] = None) -> Literal:
pass

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

@abstractmethod
Expand All @@ -81,6 +81,10 @@ def list_triples(self) -> Iterator[Triple]:
def add(self, triples: Union[Iterator[Triple], 'Graph']) -> 'Graph':
pass

@abstractmethod
def remove_all(self ) -> 'Graph':
pass


class GraphFactory:

Expand Down

0 comments on commit 2317eb4

Please sign in to comment.