This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse ontologies and generate publishable directory structure
- Loading branch information
Fletcher91
committed
Aug 21, 2019
0 parents
commit 1bc5244
Showing
9 changed files
with
1,769 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/* | ||
.idea | ||
.rpt2* | ||
coverage/** | ||
ontologies/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "ontologies", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"clean": "rm -rf packages/", | ||
"generate": "ts-node src/index.ts", | ||
"rebuild": "yarn clean && yarn generate", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ontola/ontologies.git" | ||
}, | ||
"author": "Fletcher91 <[email protected]>", | ||
"license": "GPL-3.0-or-later", | ||
"bugs": { | ||
"url": "https://github.com/ontola/ontologies/issues" | ||
}, | ||
"homepage": "https://github.com/ontola/ontologies#readme", | ||
"dependencies": { | ||
"@types/glob": "^7.1.1", | ||
"glob": "^7.1.4", | ||
"npm-registry-client": "^8.6.0", | ||
"rdflib": "npm:[email protected]", | ||
"ts-morph": "^3.1.3", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"license": "GPL-3.0-or-later", | ||
"main": "index.js", | ||
"version": "1.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Project } from "ts-morph" | ||
import { | ||
Ontology, | ||
OntologyItem, | ||
OntologyItemPropType, | ||
} from './types' | ||
import defaults from './defaults.package.json'; | ||
|
||
const firstValue = (obj: OntologyItem, property: string): OntologyItemPropType => { | ||
if (obj && Object.prototype.hasOwnProperty.call(obj, property)) { | ||
const prop = obj[property] | ||
|
||
return Array.isArray(prop) ? prop[0] : prop | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
export async function generate(ontologies: Ontology[]) { | ||
const packages = new Project() | ||
|
||
for (const ontology of ontologies) { | ||
const packageJSON = Object.assign( | ||
{}, | ||
defaults, | ||
{ | ||
name: `@ontologies/${ontology.symbol}`, | ||
description: firstValue(ontology, 'label'), | ||
} | ||
) | ||
packages.createSourceFile( | ||
`packages/${ontology.symbol}/package.json`, | ||
JSON.stringify(packageJSON, null, 2) | ||
) | ||
} | ||
|
||
await packages.save() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { generate } from './generate' | ||
import { parse } from "./parse" | ||
|
||
console.log("Starting process") | ||
parse().then(generate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as fs from "fs" | ||
import * as path from 'path' | ||
|
||
import glob from "glob" | ||
import { | ||
parse as parseRDF, | ||
IndexedFormula, | ||
Namespace, | ||
SomeTerm, | ||
Statement, | ||
NamedNode, | ||
} from 'rdflib' | ||
import { | ||
Ontology, | ||
OntologyClass, | ||
OntologyInfo, | ||
OntologyProperty, | ||
} from './types' | ||
|
||
const rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") | ||
const rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#") | ||
const owl = Namespace("http://www.w3.org/2002/07/owl#") | ||
const classTypes: SomeTerm[] = [ | ||
rdfs("Class"), | ||
owl("Class"), | ||
]; | ||
const propertyTypes: SomeTerm[] = [ | ||
rdf("Property"), | ||
owl("ObjectProperty"), | ||
]; | ||
|
||
function parseOntology(file: string, ontologyInfo: OntologyInfo): Promise<IndexedFormula> { | ||
return new Promise((resolve) => { | ||
const ontology = new IndexedFormula() | ||
|
||
parseRDF(file, ontology, ontologyInfo.ns, "text/turtle", () => { | ||
resolve(ontology) | ||
}) | ||
}) | ||
} | ||
|
||
function getClasses(ontologyData: IndexedFormula): OntologyClass[] { | ||
const getProperty = (st: Statement, prop: NamedNode): SomeTerm[] => | ||
ontologyData.match(st.subject, prop).map(s => s.object); | ||
|
||
return ontologyData | ||
.statements | ||
.filter((st) => st.predicate === rdf("type") && classTypes.includes(st.object)) | ||
.map((property) => ({ | ||
iri: property.subject, | ||
label: getProperty(property, rdfs("label")), | ||
comment: getProperty(property, rdfs("comment")), | ||
isDefinedBy: getProperty(property, rdfs("isDefinedBy")), | ||
seeAlso: getProperty(property, rdfs("seeAlso")), | ||
subClassOf: getProperty(property, rdfs("subClassOf")).filter(term => term.termType === "NamedNode") as NamedNode[], | ||
})) | ||
} | ||
|
||
function getProperties(ontologyData: IndexedFormula): OntologyProperty[] { | ||
const getProperty = (st: Statement, prop: NamedNode): SomeTerm[] => | ||
ontologyData.match(st.subject, prop).map(s => s.object); | ||
|
||
return ontologyData | ||
.statements | ||
.filter((st) => st.predicate === rdf("type") && propertyTypes.includes(st.object)) | ||
.map((property) => ({ | ||
iri: property.subject, | ||
label: getProperty(property, rdfs("label")), | ||
comment: getProperty(property, rdfs("comment")), | ||
isDefinedBy: getProperty(property, rdfs("isDefinedBy")), | ||
seeAlso: getProperty(property, rdfs("seeAlso")), | ||
domain: getProperty(property, rdfs("domain")).filter(term => term.termType === "NamedNode") as NamedNode[], | ||
range: getProperty(property, rdfs("range")).filter(term => term.termType === "NamedNode") as NamedNode[], | ||
})) | ||
} | ||
|
||
export function parse(): Promise<Ontology[]> { | ||
return new Promise(async (resolve, reject) => { | ||
glob("./ontologies/**/index.json", async (err: Error | null, files: string[]) => { | ||
if (err) { | ||
return reject(err) | ||
} | ||
|
||
const ontologies: Ontology[] = [] | ||
|
||
for (const file of files) { | ||
const infoFile = fs.readFileSync(file, "utf8") | ||
const ontologyInfo = JSON.parse(infoFile) as OntologyInfo; | ||
const ontologyFile = fs.readFileSync(path.resolve(file, '../ontology.ttl'), "utf8") | ||
|
||
const ontologyData = await parseOntology(ontologyFile, ontologyInfo) | ||
|
||
const ontology = { | ||
name: ontologyInfo.name, | ||
ns: new NamedNode(ontologyInfo.ns), | ||
symbol: ontologyInfo.symbol, | ||
classes: getClasses(ontologyData), | ||
properties: getProperties(ontologyData), | ||
}; | ||
|
||
ontologies.push(ontology) | ||
} | ||
|
||
return resolve(ontologies) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { NamedNode, SomeTerm } from 'rdflib' | ||
|
||
export interface OntologyInfo { | ||
/** Human readable name */ | ||
name: string | ||
/** The namespace IRI */ | ||
ns: string | ||
/** Location where the ontology can be fetched */ | ||
source: string | ||
/** Preferred shortening symbol for the namespace */ | ||
symbol: string | ||
} | ||
|
||
export type OntologyItemPropType = undefined | string | SomeTerm | OntologyClass | OntologyProperty | ||
export type OntologyItemProp = OntologyItemPropType | Array<OntologyItemPropType> | ||
|
||
export interface OntologyItem { | ||
[k: string]: OntologyItemProp | ||
name?: string | ||
iri?: SomeTerm | ||
label?: SomeTerm[] | ||
comment?: SomeTerm[] | ||
isDefinedBy?: SomeTerm[] | ||
seeAlso?: SomeTerm[] | ||
} | ||
|
||
export interface Ontology extends OntologyItem { | ||
classes: OntologyClass[] | ||
name: string | ||
properties: OntologyProperty[] | ||
symbol: string | ||
ns: NamedNode | ||
} | ||
|
||
export interface OntologyProperty extends OntologyItem { | ||
domain: NamedNode[] | ||
range: NamedNode[] | ||
} | ||
|
||
export interface OntologyClass extends OntologyItem { | ||
subClassOf: NamedNode[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Basic Options */ | ||
"incremental": true, /* Enable incremental compilation */ | ||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ | ||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ | ||
"lib": [ | ||
"es2019" | ||
], | ||
// "allowJs": true, /* Allow javascript files to be compiled. */ | ||
"resolveJsonModule": true, | ||
// "checkJs": true, /* Report errors in .js files. */ | ||
// "declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | ||
// "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
// "outFile": "./", /* Concatenate and emit output to single file. */ | ||
// "outDir": "./", /* Redirect output structure to the directory. */ | ||
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ | ||
// "composite": true, /* Enable project compilation */ | ||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ | ||
// "removeComments": true, /* Do not emit comments to output. */ | ||
"noEmit": true, /* Do not emit outputs. */ | ||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */ | ||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ | ||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ | ||
|
||
/* Strict Type-Checking Options */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
|
||
/* Additional Checks */ | ||
"noUnusedLocals": true, /* Report errors on unused locals. */ | ||
"noUnusedParameters": true, /* Report errors on unused parameters. */ | ||
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ | ||
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ | ||
|
||
/* Module Resolution Options */ | ||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | ||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ | ||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ | ||
// "typeRoots": [], /* List of folders to include type definitions from. */ | ||
// "types": [], /* Type declaration files to be included in compilation. */ | ||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ | ||
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ | ||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ | ||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ | ||
} | ||
} |
Oops, something went wrong.