BlueBrain / nexus-forge

Building and Using Knowledge Graphs made easy
https://nexus-forge.readthedocs.io
GNU Lesser General Public License v3.0
39 stars 19 forks source link

forge.from_jsonld(): Handling Types of Literal Values in JSON-LD #194

Closed tobiasschweizer closed 3 years ago

tobiasschweizer commented 3 years ago

Hi there

I have a problem regarding types of literal values in JSON-LD.

Example:

data:

grant = {
  "@context": {
    "@vocab": "http://schema.org/",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@type": "Grant",
  "name": "My grant",
  "description": "My first granted grant",
  "startDate": {
      "@type": "xsd:date",
      "@value": "2021-09-23Z"
    }
}

schema file with shape

{
  "@context": [
    "https://incf.github.io/neuroshapes/contexts/schema.json",
    {
      "this": "https://schemaorgshapes.org/dash/grant/shapes/"
    }
  ],
  "@type": "nxv:Schema",
  "@id": "https://schemaorgshapes.org/dash/grant",
  "imports": [],
  "shapes": [
    {
      "@id": "this:GrantShape",
      "@type": "sh:NodeShape",
      "label": "A grant",
      "targetClass": "schema:Grant",
      "property": [
        {
          "path": "schema:name",
          "name": "Name",
          "description": "The Grant name.",
          "datatype": "xsd:string",
          "minCount": 1,
          "maxCount": 1
        },
        {
          "path": "schema:description",
          "name": "Description",
          "description": "The Grant description",
          "datatype": "xsd:string",
          "minCount": 1,
          "maxCount": 1
        },
        {
          "path": "schema:startDate",
          "name": "Start Date",
          "description": "The Grant's start date",
          "minCount": 1,
          "maxCount": 1,
          "datatype": "xsd:date"
        }
      ]
    }
  ]
}

When doing the following, I get a validation error:


res = forge.from_jsonld(grant)

forge.validate(res)

Constraint Violation in DatatypeConstraintComponent (http://www.w3.org/ns/shacl#DatatypeConstraintComponent): Severity: sh:Violation Source Shape: [ sh:datatype xsd:date ; sh:description Literal("The Grant's start date") ; sh:maxCount Literal("1", datatype=xsd:integer) ; sh:minCount Literal("1", datatype=xsd:integer) ; sh:name Literal("Start Date") ; sh:path schema:startDate ] Focus Node: [ http://schema.org/description Literal("My first granted grant") ; http://schema.org/name Literal("My grant") ; http://schema.org/startDate [ http://schema.org/value Literal("2021-09-23Z") ; rdf:type xsd:date ] ; rdf:type http://schema.org/Grant ] Value Node: [ http://schema.org/value Literal("2021-09-23Z") ; rdf:type xsd:date ] Result Path: schema:startDate

To me, the encoding of the xsd:date seems correct in JSON-LD, see also here.

However , the message for the focus node looks a bit strange:

Value Node: [ http://schema.org/value Literal("2021-09-23Z") ; rdf:type xsd:date ]

Could it be that the @value is mistaken for the schema.org property http://schema.org/value?

I'd be grateful for any help. Thanks a lot.

tobiasschweizer commented 3 years ago

I am using nexusforge 0.6.3 and nexus-sdk 0.3.2.

tobiasschweizer commented 3 years ago

Update: I checked the resource in Nexus Fusion and got:

"http://schema.org/startDate": [
    {
      "@type": [
        "http://www.w3.org/2001/XMLSchema#date"
      ],
      "http://schema.org/value": [
        {
          "@value": "2021-09-23Z"
        }
      ]
    }
  ]

Printing the kgforge.core.resource.Resource (returned by forge.from_jsonld) gives me:

startDate:
    {
        type: xsd:date
        value: 2021-09-23Z
    }

Is there some process that removes the @ from @value and then it looks like the property http://schema.org/value?

tobiasschweizer commented 3 years ago

maybe this shouldn't happen for @value:

https://github.com/BlueBrain/nexus-forge/blob/72c871164c1cac06c08b732ceeec0a0644e99edf/kgforge/core/conversions/rdf.py#L397-L398

tobiasschweizer commented 3 years ago

I tried to avoid using @value and this is what I came up with:

{
  "@context": {
    "schema": "http://schema.org/",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "Grant": "schema:Grant",
    "name": "schema:name",
    "description": "schema:description",
    "startDate": {
      "@id": "schema:startDate",
      "@type": "xsd:date"
    }
  },
  "@type": "Grant",
  "name": "My grant",
  "description": "My first granted grant",
  "startDate": "2021-09-23Z"
}

I tried to follow the example given here.

However, now it seems to ignore the startDate property when calling forge.validate():

_validate_one False ValidationError: Validation Report Conforms: False Results (1): Constraint Violation in MinCountConstraintComponent (http://www.w3.org/ns/shacl#MinCountConstraintComponent): Severity: sh:Violation Source Shape: [ sh:datatype xsd:date ; sh:description Literal("The Grant's start date") ; sh:maxCount Literal("1", datatype=xsd:integer) ; sh:minCount Literal("1", datatype=xsd:integer) ; sh:name Literal("Start Date") ; sh:path schema:startDate ] Focus Node: [ Literal("My first granted grant") ; Literal("My grant") ; rdf:type ] Result Path: schema:startDate

When I create the resource using forge.register(), startDate is missing.

When I create the resource using Nexus Delta directly, it works (including validation against the schema created before).

MFSY commented 3 years ago

@tobiasschweizer can you try again with master branch to see if the issue is fixed ?

tobiasschweizer commented 3 years ago

@MFSY Sure Thing! I'll try this tomorrow morning. Could you instruct me how to build the library from source? I'd prefer to use a venv.

MFSY commented 3 years ago

Some info are available in the install doc:

A simple:

pip install git+https://github.com/BlueBrain/nexus-forge Should be enough.

tobiasschweizer commented 3 years ago

Great, I'll try this tomorrow morning and let you know asap if it works for me.

tobiasschweizer commented 3 years ago

@MFSY I installed nexus-forge from master and it works now as expected. Thanks a lot for fixing this! Will there be a minor release with this fix?

I still get some (debug) output from forge.from_jsonld():

@type @type @type @type

I guess it is this print: https://github.com/BlueBrain/nexus-forge/blob/a6d4e598338d23be12215c41087892c9d8046c35/kgforge/core/conversions/rdf.py#L402

MFSY commented 3 years ago

Great this work. Nice catch for the print issue.