google / schema-dts

JSON-LD TypeScript types for Schema.org vocabulary
Apache License 2.0
889 stars 33 forks source link

Generate types for external schema #169

Open hawyar opened 2 years ago

hawyar commented 2 years ago

I am trying to generate types for the GS1 web vocabulary. I have included the context used

schema-dts-gen --ontology=https://schema.org/version/latest/schemaorg-all-https.nt --context=cc:http://creativecommons.org/ns#,cert:http://www.w3.org/ns/auth/cert#,dbp:http://dbpedia.org/property/,dc:http://purl.org/dc/elements/1.1/,dcterms:http://purl.org/dc/elements/1.1/,doap:http://usefulinc.com/ns/doap#,exif:http://www.w3.org/2003/12/exif/ns#,fn:http://www.w3.org/2005/xpath-functions#,foaf:http://xmlns.com/foaf/0.1/,geo:http://www.w3.org/2003/01/geo/wgs84_pos#,geonames:http://www.geonames.org/ontology#,gr:http://purl.org/goodrelations/v1#,gs1:https://gs1.org/voc/,log:http://www.w3.org/2000/10/swap/log#,owl:http://www.w3.org/2002/07/owl#,rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#,rdfs:http://www.w3.org/2000/01/rdf-schema#,rei:http://www.w3.org/2004/06/rei#,rsa:http://www.w3.org/ns/auth/rsa#,rss:http://purl.org/rss/1.0/,schema:http://schema.org/,sfn:http://www.w3.org/ns/sparql#,sioc:http://rdfs.org/sioc/ns#,skos:http://www.w3.org/2004/02/skos/core#,sw:http://www.w3.org/2003/06/sw-vocab-status/ns#,swrc:http://swrc.ontoware.org/ontology#,types:http://rdfs.org/sioc/types#,vann:http://purl.org/vocab/vann/,vcard:http://www.w3.org/2006/vcard/ns#,voaf:http://purl.org/vocommons/voaf#,wot:http://xmlns.com/wot/0.1/,xhtml:http://www.w3.org/1999/xhtml#,xsd:http://www.w3.org/2001/XMLSchema# > schema.ts

which results in the correct context

export type WithContext<T extends Thing> = T & {
    "@context": {
        "cc": "http://creativecommons.org/ns#";
        "cert": "http://www.w3.org/ns/auth/cert#";
        "dbp": "http://dbpedia.org/property/";
        "dc": "http://purl.org/dc/elements/1.1/";
        "dcterms": "http://purl.org/dc/elements/1.1/";
        "doap": "http://usefulinc.com/ns/doap#";
        "exif": "http://www.w3.org/2003/12/exif/ns#";
        "fn": "http://www.w3.org/2005/xpath-functions#";
        "foaf": "http://xmlns.com/foaf/0.1/";
        "geo": "http://www.w3.org/2003/01/geo/wgs84_pos#";
        "geonames": "http://www.geonames.org/ontology#";
        "gr": "http://purl.org/goodrelations/v1#";
        "gs1": "https://gs1.org/voc/";
        "log": "http://www.w3.org/2000/10/swap/log#";
        "owl": "http://www.w3.org/2002/07/owl#";
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#";
        "rei": "http://www.w3.org/2004/06/rei#";
        "rsa": "http://www.w3.org/ns/auth/rsa#";
        "rss": "http://purl.org/rss/1.0/";
        "schema": "http://schema.org/";
        "sfn": "http://www.w3.org/ns/sparql#";
        "sioc": "http://rdfs.org/sioc/ns#";
        "skos": "http://www.w3.org/2004/02/skos/core#";
        "sw": "http://www.w3.org/2003/06/sw-vocab-status/ns#";
        "swrc": "http://swrc.ontoware.org/ontology#";
        "types": "http://rdfs.org/sioc/types#";
        "vann": "http://purl.org/vocab/vann/";
        "vcard": "http://www.w3.org/2006/vcard/ns#";
        "voaf": "http://purl.org/vocommons/voaf#";
        "wot": "http://xmlns.com/wot/0.1/";
        "xhtml": "http://www.w3.org/1999/xhtml#";
        "xsd": "http://www.w3.org/2001/XMLSchema#";
    };
};

but then I am not sure how to use

const product: WithContext<Product> 

I think I am missing something here. Any feedback is appreciated

Eyas commented 2 years ago

Hmm. You'll need a few things to do this:

  1. Point --ontology to a NTriple format version of this schema. You might find a converter that transforms "GS1 Web vocabulary in RDF Turtle" to .nt

  2. GS1 appears to extend OWL, and the base type in owl, "Thing". Needs to be included as part of the ontology you pass. I had plans on supporting passing multiple ontologies but we didn't have an immediate need yet

I vaguely remember running into some issues last time I looked at passing the owl definitions because they utilized a richer set of RDF, including anonymous lists, etc, which this parser didn't understand. But I might be mixing this up.

Let me know how it goes. You can try to get something locally by converting .tt or JSON-LD schema definitions of both into .nt, saving both to disk and concatenating both files.

I'll try to set some time aside in the next few days to reproduce myself.

Eyas commented 2 years ago

When it works, btw, you would see the properties of Product include "gs1:additionalProductDescription" etc

hawyar commented 2 years ago

Thanks for the lead. I have both schema definitions in .nt format (https://github.com/hawyar/gs1.nt). Now using gs1.nt for the --ontology throws this error which I think is similar to what you mentioned.

Caught Error on end: Error: ParseError: Error: Unexpected URL http://gs1.org/voc/ with no room for 'name'. while parsing line http://gs1.org/voc/,http://purl.org/dc/elements/1.1/description,"The GS1 RDF vocabulary, described using W3C RDF Schema and the Web Ontology Language.".
Original Stack:
Error: Unexpected URL http://gs1.org/voc/ with no room for 'name'.
    at Function.Parse (file:///Users/hawyar/Personal/gs1-schema/node_modules/schema-dts-gen/dist/src/triples/types.js:88:19)
    at subject (file:///Users/hawyar/Personal/gs1-schema/node_modules/schema-dts-gen/dist/src/triples/reader.js:32:20)
    at process (file:///Users/hawyar/Personal/gs1-schema/node_modules/schema-dts-gen/dist/src/triples/reader.js:172:26)
    at process.next (<anonymous>)
    at IncomingMessage.<anonymous> (file:///Users/hawyar/Personal/gs1-schema/node_modules/schema-dts-gen/dist/src/triples/reader.js:119:28)
    at IncomingMessage.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Is the richer set of RDF in the GS1 schema definition the main issue here? I would be happy to dedicate some time for this.

Eyas commented 2 years ago

I thought a richer set of RDF would be present in OWL (which exercises blank nodes), but in this case it appears that schema-dts might not need to care about this.

The parse error here is due to references to <http://gs1.org/voc/>. To refer to a node in a "shortened" way (e.g. via context), it typically needs a hash part or trailing path (after last /). schema-dts should probably be more open-minded, and support a "Full URL-only" node, which can only be referenced using a complete URL.

In this specific case these are meta-properties that likely won't affect your output TS anyway.

Once that is done, you'll still see another issue: gs1 uses http://www.w3.org/2002/07/owl# to describe types and properties of objects. We need better awareness that an OWL class behaves the same(ish) as an rdfa class.

Eyas commented 2 years ago

I'm looking into fixing this, but FWIW gs1's generated schema seems to be invalid. E.g.

MeasurementPrecisionCode-LESS_THAN is a sub-type of MeasurementPrecisionTypeCode, but no such class exists (it's a typo, supposed to be MeasurementPrecisionCode.

So until gs1 fixes their code, you'll always need manual modifications on top of the generated .nt file.

hawyar commented 2 years ago

I am having difficulty understating the rdfa and owl class clash here.

I can fix the typo in the generated .nt file. Is that whyMeasurementPrecisionCode-LESS_THAN is listed in the spec but is not in https://www.gs1.org/voc/MeasurementPrecisionCode ?

Thank you

Eyas commented 2 years ago

That's likely. And yeah fixing the typo for MeasurementPrecisionCode will help. There are a few others that are also broken. I'll try to regenerate a list.

I am having difficulty understating the rdfa and owl class clash here.

Can you elaborate?

hawyar commented 2 years ago

No worries, just a knowledge gap on my part. I was trying to understand "We need better awareness that an OWL class behaves the same(ish) as an rdfa class"

How did you catch the typo?

Eyas commented 2 years ago

I ran schema-dts-gen 1.1.0 (or the equivalent local version I was using) against the .nt you provided, and every time it complained about an error I noted it

Eyas commented 2 years ago

I also commented on consensusnetworks/healthnet#104 that the context you're using there is not quite right (yet).