anusii / rdflib

A pure Dart package for working with RDF (resource description framework).
https://pub.dev/packages/rdflib
GNU General Public License v3.0
13 stars 2 forks source link

ConstrainedDatatype causes g.parseTurtle failure #34

Open weasdown opened 2 weeks ago

weasdown commented 2 weeks ago

If an ontology contains a property with a range whose value is a List rather than String, Graph._saveToGroups() fails on line 614 because the for loop casts the range item to a String. I tried changing this to cast to a var, but I'm not then sure how Graph.item() should handle it or how the resulting item should be saved to groups. Below are a sample ontology file to reproduce this (in .ttl and .xml, but both with extra .txt extensions to allow upload), and the Python file used to generate the ontology.

image

Ontology files to reproduce error (with extra .txt extension to allow upload): Turtle: rdflib-test.ttl.txt XML version for info: rdflib-test.xml.txt

Python used to generate ontology files:

import owlready2 as owl
import rdflib as rdf

onto_path = 'https://example.com/rdflib-test#'
onto = owl.get_ontology('https://example.com/rdflib-test')
with onto:
    class Object(owl.Thing):
        pass

    class Mass(owl.DataProperty):
        domain = [Object]
        range = [owl.ConstrainedDatatype(base_datatype=float, min_exclusive=0.0)]

    class has_mass(owl.DataProperty, owl.FunctionalProperty, Object >> Mass):
        pass

# Save the resulting ontology
file_name = 'rdflib-test'
onto.save(file=f'{file_name}.xml', format="rdfxml")  # save temporary XML that can be read by RDFlib

# Use RDFlib to parse XML ontology and save to TTL
g = rdf.Graph()  # an RDFlib graph
g.parse(f'{file_name}.xml')  # parse temporary XML into RDFlib graph

g.bind('rdflib-test', rdf.Namespace(onto_path))

g.serialize(f'{file_name}.ttl', format='turtle')  # also save TTL locally

# os.remove(f'{owl_file_name}.xml')  # delete temporary XML
weasdown commented 1 week ago

I've written a working parser for List attributes here, although I admit the code is pretty horrible so I'll definitely clean it up when I can.

weasdown commented 1 week ago

I've found that my code linked above doesn't currently handle multiple restrictions for ConstrainedDatatypes, so I'm working on that. Generally, I need to adapt the code so it uses the list brackets (( or [) to work out whether there will be deeper lists, rather than assuming it knows the structure. Once my parser is complete and tested for a variety of ontology entries, I'll add a pull request in this repo.