Commit a88125fa authored by Andrei Popescu's avatar Andrei Popescu
Browse files

new version under development in jsonProcessor.ts

- will recursively check the schema
- will take all information from a config.json file
parent fc97bef5
Pipeline #54668 passed with stage
in 28 seconds
......@@ -19,9 +19,6 @@
rdfs:comment "An electrical measurement from a system that uses three phase alternating current."@en;
vann:preferredNamespaceUri "https://w3id.org/sdm/terms/allOf#";
dcterms:creator <https://pietercolpaert.be/#me>, <https://www.linkedin.com/in/andrei-popescu/>.
<https://pietercolpaert.be/#me> rdf:type foaf:Person;
foaf:mbox "mailto:pieter.colpaert@imec.be";
foaf:name "Pieter Colpaert".
sdm:ElectricalMeasurment rdf:type rdfs:Class.
sdm:refDevice rdf:type rdf:Property;
rdfs:label "Relationship. Device(s) used to obtain the measurement."@en;
......
{
"jsonObjects": {
"sdm:ElectricalMeasurment": "allOf",
"sdm:RefDevice": "refDevice",
"sdm:RefTargetDevice": "refTargetDevice",
"sdm:ActiveEnergyImport": "activeEnergyImport",
"sdm:ReactiveEnergyImport": "reactiveEnergyImport",
"sdm:ApparentEnergyImport": "apparentEnergyImport",
"sdm:ActiveEnergyExport": "activeEnergyExport",
"sdm:ReactiveEnergyExport": "reactiveEnergyExport",
"sdm:ApparentEnergyExport": "apparentEnergyExport",
"sdm:ActivePower": "activePower",
"sdm:ReactivePower": "reactivePower",
"sdm:ApparentPower": "apparentPower",
"sdm:PowerFactor": "powerFactor",
"sdm:DisplacementPowerFactor": "displacementPowerFactor",
"sdm:Current": "current",
"sdm:PhaseVoltage": "phaseVoltage",
"sdm:PhaseToPhaseVoltage": "phaseToPhaseVoltage",
"sdm:ThdVoltage": "thdVoltage",
"sdm:ThdCurrent": "thdCurrent"
},
"sources": {
"./SmartDataModels/dataModel.json": "sdm:ElectricalMeasurment"
},
......@@ -40,9 +63,10 @@
"car": "schema:car"
},
"nestedClasses": {
"creators": {
"creator1": "https://pietercolpaert.be/#me",
"creator2": "https://www.linkedin.com/in/andrei-popescu/"
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ var N3 = require('n3');
var DataFactory = N3.DataFactory;
var namedNode = DataFactory.namedNode, literal = DataFactory.literal, defaultGraph = DataFactory.defaultGraph, quad = DataFactory.quad;
var GbfsPattern = /** @class */ (function () {
//config = require('./configs/config-smartdatamodels.json');
// Constructors
function GbfsPattern(source, mainObj) {
// Attributes
......@@ -16,8 +17,7 @@ var GbfsPattern = /** @class */ (function () {
this.map = new Map();
this.creator1 = 'https://pietercolpaert.be/#me';
this.creator2 = 'https://www.linkedin.com/in/andrei-popescu/';
//config = require('./configs/config-gbfs.json');
this.config = require('./configs/config-smartdatamodels.json');
this.config = require('./configs/config-gbfs.json');
this.jsonSource = source; // Needed when creating a ShaclShape object
this.jsonSchema = require(source);
this.mainObject = mainObj;
......@@ -42,8 +42,8 @@ var GbfsPattern = /** @class */ (function () {
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal(this.creator1, 'foaf:mbox', 'mailto:pieter.colpaert@imec.be'));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal(this.creator1, 'foaf:name', 'Pieter Colpaert'));
// Create a ShaclShape object and insert the first entries
this.shaclFileText = this.shaclFileText + this.shape.getShaclRoot();
this.shaclFileText = this.shaclFileText + this.shape.getShaclTargetClass() + '\n';
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclRoot();
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclTargetClass() + '\n';
};
/** Creates and writes quads for the main object's properties,
* by checking if new terms are encountered (against a map of terms).
......@@ -117,7 +117,7 @@ var GbfsPattern = /** @class */ (function () {
this.writer.addQuad(rdfTools_1.RDFTools.node_node_node('gbfs:' + term, 'rdf:type', 'rdf:Property')); // Add the property and its label
if (termDescription != undefined)
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal('gbfs:' + term, 'rdfs:label', termDescription.toString()));
var newClassName = this.capitalizeFirstLetter(term); // Since it is an object/array, we give it a new class as a range
var newClassName = rdfTools_1.RDFTools.capitalizeFirstLetter(term); // Since it is an object/array, we give it a new class as a range
this.writer.addQuad(rdfTools_1.RDFTools.node_node_node('gbfs:' + term, 'rdfs:range', 'gbfs:' + newClassName));
// Add the new classes to a hiddenClasses array; these will be explored by this function in a second stage.
hiddenClasses = hiddenClasses.concat('gbfs:' + newClassName);
......@@ -221,43 +221,42 @@ var GbfsPattern = /** @class */ (function () {
if (shaclTools_1.ShaclTools.isRequired(term)) {
// If the type is primitive
if (termType == 'boolean' || termType == 'string' || termType == 'number') {
this.shaclFileText = this.shaclFileText + this.shape.getShaclTypedRequiredProperty(term, rdfTools_1.RDFTools.getXsdType(termType)) + '\n';
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclTypedRequiredProperty(term, rdfTools_1.RDFTools.getXsdType(termType)) + '\n';
}
else {
this.shaclFileText = this.shaclFileText + this.shape.getShaclRequiredProperty(term) + '\n';
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclRequiredProperty(term) + '\n';
}
}
else { // Else the property is not required
// If the type is primitive
if (termType == 'boolean' || termType == 'string' || termType == 'number') {
this.shaclFileText = this.shaclFileText + this.shape.getShaclTypedProperty(term, rdfTools_1.RDFTools.getXsdType(termType)) + '\n';
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclTypedProperty(term, rdfTools_1.RDFTools.getXsdType(termType)) + '\n';
}
else {
this.shaclFileText = this.shaclFileText + this.shape.getShaclProperty(term) + '\n';
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclProperty(term) + '\n';
}
}
}
return hiddenClasses;
};
GbfsPattern.prototype.writeTurtle = function () {
var _this = this;
/*
writeTurtle (){
// Write the content of the writer in the .ttl
this.writer.end(function (error, result) { return _this.fs.writeFile("build/" + _this.fileName + ".ttl", result, function (err) {
this.writer.end((error:any, result:any) => this.fs.writeFile(`build/${this.fileName}.ttl`, result, (err:any) => {
// throws an error, you could also catch it here
if (err)
throw err;
if (err) throw err;
// success case, the file was saved
console.log('Turtle saved!');
}); });
};
GbfsPattern.prototype.writeShacl = function () {
console.log('Turtle saved!');}));
}
writeShacl (){
// Write the Shacl shape on file
this.fs.writeFileSync("build/" + this.fileName + "shacl.ttl", this.shaclFileText, function (err) {
if (err) {
this.fs.writeFileSync(`build/${this.fileName}shacl.ttl`, this.shaclFileText , function(err:any){
if(err){
return console.log("error");
}
});
};
}
*/
/** returns the properties of the main object which are required. Useful in the shaclshape class in order to create the shacl shape */
GbfsPattern.prototype.getRequiredProperties = function () {
var requiredMap = new Map();
......@@ -427,12 +426,18 @@ var GbfsPattern = /** @class */ (function () {
}
}
};
GbfsPattern.prototype.getFileName = function () {
return this.fileName;
};
GbfsPattern.prototype.getWriter = function () {
return this.writer;
};
GbfsPattern.prototype.getShaclFileText = function () {
return this.shaclFileText;
};
GbfsPattern.prototype.setMainObject = function (mainObject) {
this.mainObject = mainObject;
};
GbfsPattern.prototype.capitalizeFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
return GbfsPattern;
}());
exports.GbfsPattern = GbfsPattern;
......@@ -40,8 +40,8 @@ export class GbfsPattern {
// ShaclShape
shape: any;
fileName: any;
//config = require('./configs/config-gbfs.json');
config = require('./configs/config-smartdatamodels.json');
config = require('./configs/config-gbfs.json');
//config = require('./configs/config-smartdatamodels.json');
// Constructors
......@@ -71,8 +71,8 @@ export class GbfsPattern {
this.writer.addQuad(RDFTools.node_node_literal(this.creator1, 'foaf:mbox', 'mailto:pieter.colpaert@imec.be'));
this.writer.addQuad(RDFTools.node_node_literal(this.creator1, 'foaf:name', 'Pieter Colpaert'));
// Create a ShaclShape object and insert the first entries
this.shaclFileText = this.shaclFileText+this.shape.getShaclRoot();
this.shaclFileText = this.shaclFileText+this.shape.getShaclTargetClass()+'\n';
this.shaclFileText = this.shaclFileText+ ShaclTools.getShaclRoot();
this.shaclFileText = this.shaclFileText+ ShaclTools.getShaclTargetClass()+'\n';
}
/** Creates and writes quads for the main object's properties,
......@@ -157,7 +157,7 @@ export class GbfsPattern {
if( termDescription != undefined )
this.writer.addQuad(RDFTools.node_node_literal('gbfs:'+term, 'rdfs:label', termDescription.toString()));
const newClassName = this.capitalizeFirstLetter(term); // Since it is an object/array, we give it a new class as a range
const newClassName = RDFTools.capitalizeFirstLetter(term); // Since it is an object/array, we give it a new class as a range
this.writer.addQuad(RDFTools.node_node_node('gbfs:'+term, 'rdfs:range', 'gbfs:'+newClassName));
// Add the new classes to a hiddenClasses array; these will be explored by this function in a second stage.
......@@ -269,18 +269,18 @@ export class GbfsPattern {
if (ShaclTools.isRequired(term)){
// If the type is primitive
if (termType == 'boolean' || termType == 'string' || termType == 'number') {
this.shaclFileText = this.shaclFileText+this.shape.getShaclTypedRequiredProperty(term, RDFTools.getXsdType(termType))+'\n';
this.shaclFileText = this.shaclFileText+ShaclTools.getShaclTypedRequiredProperty(term, RDFTools.getXsdType(termType))+'\n';
}
else{
this.shaclFileText = this.shaclFileText+this.shape.getShaclRequiredProperty(term)+'\n';
this.shaclFileText = this.shaclFileText+ShaclTools.getShaclRequiredProperty(term)+'\n';
}
}
else{ // Else the property is not required
// If the type is primitive
if (termType == 'boolean' || termType == 'string' || termType == 'number') {
this.shaclFileText = this.shaclFileText+this.shape.getShaclTypedProperty(term, RDFTools.getXsdType(termType))+'\n'; }
this.shaclFileText = this.shaclFileText+ShaclTools.getShaclTypedProperty(term, RDFTools.getXsdType(termType))+'\n'; }
else{
this.shaclFileText = this.shaclFileText+this.shape.getShaclProperty(term)+'\n';
this.shaclFileText = this.shaclFileText+ShaclTools.getShaclProperty(term)+'\n';
}
}
}
......@@ -288,6 +288,7 @@ export class GbfsPattern {
return hiddenClasses;
}
/*
writeTurtle (){
// Write the content of the writer in the .ttl
this.writer.end((error:any, result:any) => this.fs.writeFile(`build/${this.fileName}.ttl`, result, (err:any) => {
......@@ -304,6 +305,8 @@ export class GbfsPattern {
}
});
}
*/
/** returns the properties of the main object which are required. Useful in the shaclshape class in order to create the shacl shape */
getRequiredProperties () {
let requiredMap = new Map<string, string>();
......@@ -483,12 +486,20 @@ export class GbfsPattern {
}
}
getFileName(){
return this.fileName;
}
getWriter(){
return this.writer;
}
getShaclFileText(){
return this.shaclFileText;
}
setMainObject(mainObject: string){
this.mainObject= mainObject;
}
capitalizeFirstLetter(string:string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
import { RDFTools } from "./rdfTools";
const N3 = require('n3');
const { DataFactory } = N3;
const { namedNode, literal, defaultGraph, quad } = DataFactory;
export class JsonProcessor {
// From config.json, we get:
// prefixes, terms, , schema_objects(in the main)
static config = require('./configs/config-smartdatamodel.json');
static jsonSource: any;
static jsonSchema: any;
static mainObject: any;
static mainJsonObject: any;
static fileName: any;
static rdf_json_objects = new Map<string, string>();
static termMap = new Map<string, string>();
static writer: any;
static path: any;
static initialise ( source:string, mainObj: string ){
// Getting configuration elements
for( let object in this.config.jsonObjects){
this.rdf_json_objects.set(object, this.config.rdf_json[object]);
}
for( let object in this.config.terms){
this.termMap.set(object, this.config.terms[object]);
}
this.writer = new N3.Writer({prefixes:this.config.prefixes});
// Setting up basic info
this.jsonSource = source; // Needed when creating a ShaclShape object
this.jsonSchema = require(source);
this.mainObject = mainObj;
this.mainJsonObject = this.getJsonObject(this.mainObject);
this.fileName = mainObj;
// Set path (TODO: set from confi.json)
this.path = this.jsonSchema[this.mainJsonObject];
}
static callParseJsonRecursive(path){
let depth = 0;
let hiddenClasses = this.parseJsonRecursive(this.writer, depth, path);
//path ?
return hiddenClasses; // these will be modified
}
static parseJsonRecursive (writer, depth, path){
if (depth > 2){ // base case
return
}
else{
for (const prop in properties){
if (this.termMap.has(prop)){
return // base case
}
else{
// Base cases
if(pattern4):
this.writer.addQuad(RDFTools.node_node_node('sdm:'+term, 'rdf:type', 'rdf:Property')); // Add the property and its label
return
if(pattern5):
writer.write
return
if(pattern6):
writer.write
return
// Recursive calls
if(pattern1 or patter 2):
hiddenClasses = hiddenClasses.concat('sdm:'+newClassName);
writer.write
return
}
}
}
}
static getJsonObject (mainObject: string){
for(let entry of Array.from(this.rdf_json_objects.entries())){
const key = entry[0];
const value = entry[1];
if( key == mainObject){
return this.rdf_json_objects.get(key);
}
}
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ var i = 0;
for (var _i = 0, _a = Array.from(schema_object); _i < _a.length; _i++) {
var _b = _a[_i], schema = _b[0], object = _b[1];
i += 1;
//const smdPattern = new SMDPattern(schema, object);
var smdPattern = new smdPattern_1.SMDPattern(schema, object);
rdfTools_1.RDFTools.initialise(smdPattern.getFileName());
shaclTools_1.ShaclTools.initialise(smdPattern.getFileName(), smdPattern.getRequiredProperties(), smdPattern.jsonSource, smdPattern.mainObject);
......@@ -32,3 +33,22 @@ for (var _i = 0, _a = Array.from(schema_object); _i < _a.length; _i++) {
rdfTools_1.RDFTools.writeTurtle(smdPattern.getWriter());
shaclTools_1.ShaclTools.writeShacl(smdPattern.getFileName(), smdPattern.getShaclFileText());
}
/* main Function for recursive jsonProcessor
for schema, object in schema_object{
smdPatter = new smdPattern(schema, object);
RDFTools.initialise(smdPattern.getFileName());
smdPattern.basicsToQuads();
hiddenClasses = JsonProcessor.callParseJsonRecursive(); //this method will need to recursively call the parse method.
}
*/
......@@ -13,30 +13,47 @@ for( let object in config.sources){
schema_object.set(object, config.sources[object]);
}
let hiddenClasses = []
let i = 0;
for (let [schema,object] of Array.from(schema_object)){
i +=1;
//const smdPattern = new SMDPattern(schema, object);
const smdPattern = new SMDPattern(schema, object);
RDFTools.initialise(smdPattern.getFileName());
RDFTools.initialise(smdPattern.getFileName());
ShaclTools.initialise(smdPattern.getFileName(), smdPattern.getRequiredProperties(), smdPattern.jsonSource, smdPattern.mainObject );
smdPattern.basicsToQuads();
hiddenClasses = smdPattern.propertiesToRDF(0);
// New classes might be have been added as range value for some properties. It is now time to explore those classes,
// e.g. "per_km_pricing" in system_pricing.json
for (const cls of hiddenClasses){
smdPattern.setMainObject(cls);
console.log("main object", cls);
smdPattern.propertiesToRDF(1);
}
RDFTools.writeTurtle(smdPattern.getWriter());
ShaclTools.writeShacl(smdPattern.getFileName(), smdPattern.getShaclFileText(),);
}
/* main Function for recursive jsonProcessor
for schema, object in schema_object{
smdPatter = new smdPattern(schema, object);
RDFTools.initialise(smdPattern.getFileName());
smdPattern.basicsToQuads();
hiddenClasses = JsonProcessor.callParseJsonRecursive(); //this method will need to recursively call the parse method.
}
}
\ No newline at end of file
*/
\ No newline at end of file
......@@ -9,9 +9,6 @@ const N3 = require('n3');
const { DataFactory } = N3;
const { namedNode, literal, defaultGraph, quad } = DataFactory;
/**
* This class is contains auxiliary methods and tools for Pattern clas (e.g. smdPattern, gbfsPattern,...)
* to write Turtle
......
......@@ -5,17 +5,6 @@ exports.ShaclTools = void 0;
var ShaclTools = /** @class */ (function () {
function ShaclTools() {
}
/*
// Constructors
constructor (required: Map<string, string>, source: string, mainObj: string) {
this.jsonSchema = require(source);
this.requiredProperties = required;
console.log("passed object",mainObj);
console.log("main target",this.getShaclTarget(mainObj));
}*/
ShaclTools.initialise = function (filename, required, source, mainObj) {
this.fileName = filename;
this.targetClass = ShaclTools.getShaclTarget(mainObj);
......
......@@ -18,10 +18,16 @@ var SMDPattern = /** @class */ (function () {
this.creator2 = 'https://www.linkedin.com/in/andrei-popescu/';
//config = require('./configs/config-sdm.json');
this.config = require('./configs/config-smartdatamodel.json');
this.rdf_json_objects = new Map();
// Set the
for (var key in this.config.jsonObjects) {
this.rdf_json_objects.set(key, this.config.jsonObjects[key]);
}
console.log("map of objects", this.rdf_json_objects);
this.jsonSource = source; // Needed when creating a ShaclShape object
this.jsonSchema = require(source);
this.mainObject = mainObj;
this.mainJsonObject = this.getMainJsonObject(this.mainObject);
this.mainJsonObject = this.getJsonObject(this.mainObject);
this.fileName = mainObj;
for (var object in this.config.terms) {
this.map.set(object, this.config.terms[object]);
......@@ -38,9 +44,6 @@ var SMDPattern = /** @class */ (function () {
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal('https://w3id.org/sdm/terms/' + this.mainJsonObject, 'vann:preferredNamespaceUri', 'https://w3id.org/sdm/terms/' + this.mainJsonObject + '#'));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_node('https://w3id.org/sdm/terms/' + this.mainJsonObject, 'dcterms:creator', this.creator1));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_node('https://w3id.org/sdm/terms/' + this.mainJsonObject, 'dcterms:creator', this.creator2));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_node(this.creator1, 'rdf:type', 'foaf:Person'));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal(this.creator1, 'foaf:mbox', 'mailto:pieter.colpaert@imec.be'));
this.writer.addQuad(rdfTools_1.RDFTools.node_node_literal(this.creator1, 'foaf:name', 'Pieter Colpaert'));
// Create a ShaclShape object and insert the first entries
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclRoot();
this.shaclFileText = this.shaclFileText + shaclTools_1.ShaclTools.getShaclTargetClass() + '\n';
......@@ -57,7 +60,7 @@ var SMDPattern = /** @class */ (function () {
// GET the properties of the main object
// If we are looking at depth 1 (second iteration), then we have to slightly change the paths
var jsonobj;
jsonobj = this.getMainJsonObject(this.mainObject);
jsonobj = this.getJsonObject(this.mainObject);
// GBFS
if (depth == 1) { // Then we need the path to the nested object/array
console.log(this.mainObject);
......@@ -272,99 +275,121 @@ var SMDPattern = /** @class */ (function () {
}
return requiredMap;
};
SMDPattern.prototype.getMainJsonObject = function (mainObject) {
switch (mainObject) {
case 'sdm:ElectricalMeasurment': {
return 'allOf';
break;
}
// ---- Nested classes ----
case 'sdm:RefDevice': {
return 'refDevice';
break;
}
case 'sdm:RefTargetDevice': {
return 'refTargetDevice';
break;
}
case 'sdm:ActiveEnergyImport': {
return 'activeEnergyImport';
break;
}
case 'sdm:ReactiveEnergyImport': {
return 'reactiveEnergyImport';
break;
}
case 'sdm:ApparentEnergyImport': {
return 'apparentEnergyImport';
break;
}
case 'sdm:ApparentEnergyImport': {
return 'apparentEnergyImport';
break;
}
case 'sdm:ApparentEnergyImport': {
return 'apparentEnergyImport';
break;
}
case 'sdm:ApparentEnergyImport': {
return 'apparentEnergyImport';
break;
}
case 'sdm:ActiveEnergyExport': {
return 'activeEnergyExport';
break;
}
case 'sdm:ReactiveEnergyExport': {
return 'reactiveEnergyExport';
break;
}
case 'sdm:ApparentEnergyExport': {
return 'apparentEnergyExport';
break;
}
case 'sdm:ActivePower': {
return 'activePower';
break;
}
case 'sdm:ReactivePower': {
return 'reactivePower';
break;
}
case 'sdm:ApparentPower': {
return 'apparentPower';
break;
}
case 'sdm:PowerFactor': {
return 'powerFactor';
break;
}
case 'sdm:DisplacementPowerFactor': {
return 'displacementPowerFactor';
break;
}
case 'sdm:Current': {
return 'current';
break;
}
case 'sdm:PhaseVoltage': {
return 'phaseVoltage';
break;
}
case 'sdm:PhaseToPhaseVoltage': {
return 'phaseToPhaseVoltage';
break;
}
case 'sdm:ThdVoltage': {
return 'thdVoltage';
break;
}
case 'sdm:ThdCurrent': {
return 'thdCurrent';
break;
SMDPattern.prototype.getJsonObject = function (mainObject) {
console.log("ciao", mainObject);