11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
# File 'lib/solis/shape/reader/sheet.rb', line 11
def self.read(key, spreadsheet_id, options = {})
class << self
def progress(i, data)
if data.key?(:job_id) && data.key?(:store)
job_id = data[:job_id]
progress = data[:store]
progress[job_id] = i
end
end
def validate(sheets, prefixes = nil, metadata = nil)
prefixes = sheets.key?('_PREFIXES') && prefixes.nil? ? sheets['_PREFIXES'] : prefixes
metadata = sheets.key?('_METADATA') && metadata.nil? ? sheets['_METADATA'] : metadata
raise "_PREFIXES tab must have ['base', 'prefix', 'uri'] as a header at row 1" unless (%w[base prefix uri] - prefixes.).length == 0
raise '_PREFIXES.base can only have one base URI' if prefixes.map { |m| m['base'] }.grep(/\*/).count != 1
raise "_METADATA tab must have ['key', 'value'] as a header at row 1" unless (%w[key value] - metadata.).length == 0
if sheets.key?('_ENTITIES')
entities = sheets['_ENTITIES']
raise "_ENTITIES tab must have ['name', 'nameplural', 'description', 'subclassof', 'sameas'] as a header at row 1" unless (%w[name nameplural description subclassof sameas] - entities.).length == 0
entities.each do |entity|
raise "Plural not found for #{entity['name']}" if entity['nameplural'].nil? || entity['nameplural'].empty?
end
end
if sheets.key?('_REFERENCES')
references = sheets['_REFERENCES']
raise "_REFERENCES tab must have ['sheeturl', 'description', 'entityrange'] as a header at row 1" unless (%w[sheeturl description entityrange] - references.).length == 0
end
sheets.each do |sheet_name, sheet|
if sheet_name !~ /^_/
entities = sheets[sheet_name]
raise "#{sheet_name} tab must have ['Name', 'Description', 'MIN', 'MAX', 'sameAs', 'datatype'] as a header at row 1" unless (%w[name description min max sameas datatype] - entities.).length == 0
end
end
end
def read_sheets(key, spreadsheet_id, options)
data = nil
prefixes = options[:prefixes] || nil
metadata = options[:metadata] || nil
cache_dir = ConfigFile.include?(:cache) ? ConfigFile[:cache] : '/tmp'
if ::File.exist?("#{cache_dir}/#{spreadsheet_id}.json") && (options.include?(:from_cache) && options[:from_cache])
Solis::LOGGER.info("from cache #{cache_dir}/#{spreadsheet_id}.json")
data = JSON.parse(::File.read("#{cache_dir}/#{spreadsheet_id}.json"), { symbolize_names: true })
return data
else
Solis::LOGGER.info("from source #{spreadsheet_id}")
session = SimpleSheets.new(key, spreadsheet_id)
session.key = key
sheets = {}
session.worksheets.each do |worksheet|
sheet = ::Sheet.new(worksheet)
sheets[sheet.title] = sheet
end
validate(sheets, prefixes, metadata)
end
sheets
end
def process_sheet(key, sheet_id, sheets, options = { follow: true })
entities = {}
prefixes = {}
ontology_metadata = {}
sheets['_PREFIXES'].each do |e|
prefixes.store(e['prefix'].to_sym, { uri: e['uri'], base: e['base'].eql?('*') })
end
sheets['_METADATA'].each { |e| ontology_metadata.store(e['key'].to_sym, e['value']) }
base_uri = prefixes.select { |_k, v| v[:base] }.select { |s| !s.empty? }
graph_prefix = base_uri.keys.first
graph_name = base_uri.values.first[:uri]
sheets['_ENTITIES'].each do |e|
top_class = e['name'].to_s
top_sheet = sheets[top_class] || nil
entity_data = parse_entity_data(e['name'].to_s, graph_prefix, graph_name, top_sheet, { key: key, prefixes: prefixes, follow: options[:follow] })
if entity_data.empty?
entity_data[:id] = {
datatype: 'xsd:string',
path: "#{graph_prefix}:id",
cardinality: { min: '1', max: '1' },
same_as: '',
description: 'systeem UUID'
}
end
entities.store(e['name'].to_sym, { description: e['description'],
order: e['order'],
plural: e['nameplural'],
label: e['name'].to_s.strip,
sub_class_of: e['subclassof'].nil? || e['subclassof'].empty? ? [] : [e['subclassof']],
same_as: e['sameas'],
properties: entity_data })
end
data = {
entities: entities,
ontologies: {
all: prefixes,
base: {
prefix: graph_prefix,
uri: graph_name
}
},
metadata: ontology_metadata
}
cache_dir = ConfigFile.include?(:cache) ? ConfigFile[:cache] : '/tmp'
::File.open("#{::File.absolute_path(cache_dir)}/#{sheet_id}.json", 'wb') do |f|
f.puts data.to_json
end
data
rescue StandardError => e
raise Solis::Error::GeneralError, e.message
end
def parse_entity_data(entity_name, graph_prefix, _graph_name, e, options = {})
properties = {}
entity_data = e
if entity_data && !entity_data.nil? && (entity_data.count > 0)
entity_data.each do |p|
property_name = I18n.transliterate(p['name'].strip)
next if property_name.empty?
min_max = {}
%w[min max].each do |n|
min_max[n] = if p.key?(n) && p[n] =~ /\d+/
p[n].to_i.to_s
else
''
end
end
puts "#{entity_name}.#{property_name}"
unless p.key?('name')
puts "No 'name' property found"
pp p
end
if properties.key?(property_name)
puts "Found #{entity_name}.#{property_name}"
else
datatype_prefix, datatype_name = p['datatype'].split(':')
properties[property_name] = {
datatype: p['datatype'],
path: "#{graph_prefix}:#{property_name.to_s.classify}",
cardinality: { min: min_max['min'], max: min_max['max'] },
same_as: p['sameas'],
order: p['order'],
description: p['description']
}
end
end
end
properties
end
def build_plantuml(datas)
out = %(@startuml
skinparam classFontSize 14
!define LIGHTORANGE
skinparam groupInheritance 1
skinparam componentStyle uml2
skinparam wrapMessageWidth 100
skinparam ArrowColor #Maroon
title #{datas.first[:metadata][:title]} - #{datas.first[:metadata][:version]} - #{Time.now}
)
out += "\npackage #{datas.first[:ontologies][:base][:prefix]} {\n"
datas.each do |data|
data[:entities].each do |entity_name, metadata|
out += "\nclass #{entity_name}"
properties = metadata[:properties]
relations = []
unless properties.nil? || properties.empty?
out += "{\n"
properties.each do |property, property_metadata|
out += "\t{field} #{property_metadata[:datatype]} : #{property} \n"
if property_metadata[:datatype].split(':').first.eql?(data[:ontologies][:base][:prefix].to_s)
relations << "#{property_metadata[:datatype].split(':').last} - #{property_metadata[:cardinality].key?(:max) && !property_metadata[:cardinality][:max].empty? ? "\"#{property_metadata[:cardinality][:max]}\"" : ''} #{entity_name} : #{property} >"
end
end
out += "}\n"
out += relations.join("\n")
end
out += "\n"
sub_classes = metadata[:sub_class_of]
sub_classes = [sub_classes] unless sub_classes.is_a?(Array)
sub_classes.each do |sub_class|
out += "#{entity_name} --|> #{sub_class.split(':').last}\n" unless sub_class.empty?
end
end
end
out += %(
hide circle
hide methods
hide empty members
@enduml
)
out
rescue StandardError => e
puts e.message
end
def datatype_lookup(datatype, as = :sql)
datatypes = {
'xsd:string' => { sql: 'text' },
'xsd:date' => { sql: 'date' },
'xsd:boolean' => { sql: 'bool' },
'xsd:integer' => { sql: 'integer' }
}
datatypes.default = { sql: 'text' }
datatypes[datatype][as]
end
def build_shacl(datas)
shacl_prefix = datas.first[:ontologies][:all].select { |_, v| v[:uri] =~ /shacl/ }.keys.first
shacl_prefix = 'sh' if shacl_prefix.nil?
out = (datas.first)
datas.each do |data|
data[:entities].each do |entity_name, metadata|
graph_prefix = data[:ontologies][:base][:prefix]
graph_name = data[:ontologies][:base][:uri]
description = metadata[:description]
label = metadata[:label]
target_class = "#{graph_prefix}:#{entity_name}"
node = metadata[:sub_class_of]
if node && !node.empty?
node = node.first if node.is_a?(Array)
node = node.strip
node += 'Shape' if node != /Shape$/
else
node = target_class
end
out += %(
#{graph_prefix}:#{entity_name}Shape
a #{shacl_prefix}:NodeShape ;
#{shacl_prefix}:description "#{description&.gsub('"', "'")&.gsub(/\n|\r/, '')}" ;
#{shacl_prefix}:targetClass #{target_class} ;#{
unless node.nil? || node.empty?
"\n #{shacl_prefix}:node #{node} ;"
end}
#{shacl_prefix}:name "#{label}" ;
)
metadata[:properties].each do |property, property_metadata|
attribute = property.to_s.strip
next if attribute.empty?
description = property_metadata[:description]&.gsub('"', "'")&.gsub(/\n|\r/, '').strip
path = "#{graph_prefix}:#{attribute}"
datatype = property_metadata[:datatype].strip
min_count = property_metadata[:cardinality][:min].strip
max_count = property_metadata[:cardinality][:max].strip
order = property_metadata.key?(:order) && property_metadata[:order] ? property_metadata[:order]&.strip : nil
if datatype =~ /^#{graph_prefix}:/ || datatype =~ /^<#{graph_name}/
out += %( #{shacl_prefix}:property [#{shacl_prefix}:path #{path} ;
#{shacl_prefix}:name "#{attribute}" ;
#{shacl_prefix}:description "#{description}" ;#{order.nil? ? '' : "\n #{shacl_prefix}:order #{order} ;"}
#{shacl_prefix}:nodeKind #{shacl_prefix}:IRI ;
#{shacl_prefix}:class #{datatype} ;#{min_count =~ /\d+/ ? "\n #{shacl_prefix}:minCount #{min_count} ;" : ''}#{max_count =~ /\d+/ ? "\n #{shacl_prefix}:maxCount #{max_count} ;" : ''}
] ;
)
else
if datatype.eql?('rdf:langString') && max_count.eql?('1')
out += %( #{shacl_prefix}:property [#{shacl_prefix}:path #{path} ;
#{shacl_prefix}:name "#{attribute}";
#{shacl_prefix}:description "#{description}" ;#{order.nil? ? '' : "\n #{shacl_prefix}:order #{order} ;"}
#{shacl_prefix}:uniqueLang true ;
#{shacl_prefix}:datatype #{datatype} ;#{min_count =~ /\d+/ ? "\n #{shacl_prefix}:minCount #{min_count} ;" : ''}
] ;
)
elsif datatype.eql?('rdf:langString')
out += %( #{shacl_prefix}:property [#{shacl_prefix}:path #{path} ;
#{shacl_prefix}:name "#{attribute}";
#{shacl_prefix}:description "#{description}" ;#{order.nil? ? '' : "\n #{shacl_prefix}:order #{order} ;"}
#{shacl_prefix}:datatype #{datatype} ;#{min_count =~ /\d+/ ? "\n #{shacl_prefix}:minCount #{min_count} ;" : ''}#{max_count =~ /\d+/ ? "\n #{shacl_prefix}:maxCount #{max_count} ;" : ''}
] ;
)
else
out += %( #{shacl_prefix}:property [#{shacl_prefix}:path #{path} ;
#{shacl_prefix}:name "#{attribute}";
#{shacl_prefix}:description "#{description}" ;#{order.nil? ? '' : "\n #{shacl_prefix}:order #{order} ;"}
#{shacl_prefix}:datatype #{datatype} ;#{min_count =~ /\d+/ ? "\n #{shacl_prefix}:minCount #{min_count} ;" : ''}#{max_count =~ /\d+/ ? "\n #{shacl_prefix}:maxCount #{max_count} ;" : ''}
] ;
)
end
end
end
out += ".\n"
end
end
out
rescue StandardError => e
puts e.message
end
def build_schema(datas)
classes = {}
datatype_properties = {}
object_properties = {}
format = :ttl
graph_prefix = datas.first[:ontologies][:base][:prefix]
graph_name = datas.first[:ontologies][:base][:uri]
all_prefixes = {}
datas.first[:ontologies][:all].each { |k, v| all_prefixes[k] = v[:uri] }
datas.each do |data|
data[:entities].each do |entity_name, metadata|
classes[entity_name] = {
comment: metadata[:description],
label: entity_name.to_s,
type: 'owl:Class',
subClassOf: metadata[:sub_class_of]
}
metadata[:properties].each do |property, property_metadata|
attribute = property.to_s.strip
description = property_metadata[:description]
path = "#{graph_name}#{attribute}"
datatype = property_metadata[:datatype]
schema_data = datatype_properties[attribute] || {}
domain = schema_data[:domain] || []
domain << "#{graph_name}#{entity_name}"
datatype_properties[attribute] = {
domain: domain,
comment: description,
label: attribute.to_s,
range: datatype,
type: 'rdf:Property'
}
unless property_metadata[:same_as].nil? || property_metadata[:same_as].empty?
datatype_properties[attribute]['owl:sameAs'] =
property_metadata[:same_as]
end
subclass_data = data[:entities][entity_name][:sub_class_of] || []
unless property_metadata[:cardinality][:min].empty?
subclass_data << RDF::Vocabulary.term(type: 'owl:Restriction',
onProperty: path,
minCardinality: property_metadata[:cardinality][:min])
end
unless property_metadata[:cardinality][:max].empty?
subclass_data << RDF::Vocabulary.term(type: 'owl:Restriction',
onProperty: path,
maxCardinality: property_metadata[:cardinality][:max])
end
data[:entities][entity_name][:sub_class_of] = subclass_data
end
end
end
lp = RDF::StrictVocabulary(graph_name)
o = ::Class.new(lp) do
ontology(graph_name.to_sym, {
"dc11:title": datas.first[:metadata][:title].freeze,
"dc11:description": datas.first[:metadata][:description].freeze,
"dc11:date": Time.now.to_s.freeze,
"dc11:creator": datas.first[:metadata][:author].freeze,
"owl:versionInfo": datas.first[:metadata][:version].freeze,
type: 'owl:Ontology'.freeze
})
classes.each do |k, v|
term k.to_sym, v
end
object_properties.each do |k, v|
property k.is_a?(RDF::URI) ? k.value.to_sym : k.to_sym, v
end
datatype_properties.each do |k, v|
property k.is_a?(RDF::URI) ? k.value.to_sym : k.to_sym, v
end
end
RDF::Vocabulary.register(graph_prefix.to_sym, o, uri: graph_name)
graph = RDF::Graph.new
graph.graph_name = RDF::URI(graph_name)
datas.each do |data|
data[:entities].select { |_k, v| !v[:same_as].empty? }.each do |k, v|
same_as_value = v[:same_as].strip
if same_as_value.start_with?('<') && same_as_value.end_with?('>')
uri_string = same_as_value[1...-1]
rdf_verb = RDF::URI(uri_string)
else
prefix, verb = same_as_value.split(':')
rdf_vocabulary = RDF::Vocabulary.from_sym(prefix.upcase)
rdf_verb = rdf_vocabulary[verb.to_sym]
end
graph << RDF::Statement.new(rdf_verb, RDF::RDFV.type, RDF::OWL.Class)
graph << RDF::Statement.new(rdf_verb, RDF::Vocab::OWL.sameAs, o[k.to_sym])
rescue StandardError => e
puts e.message
end
end
graph << o.to_enum
graph.dump(format, prefixes: all_prefixes)
rescue StandardError => e
puts e.message
end
def build_inflections(datas)
inflections = {}
datas.each do |data|
data[:entities].each do |entity, metadata|
inflections[entity] = metadata[:plural]
inflections[entity.to_s.underscore.to_sym] = metadata[:plural].underscore
end
end
inflections.to_json
rescue StandardError => e
puts e.message
end
def build_json_schema(shacl_file)
def map_rdf_datatype_to_json_schema(datatype)
case datatype
when /string$/
{ "type" => "string" }
when /integer$/
{ "type" => "integer" }
when /decimal$/, /double$/, /float$/
{ "type" => "number" }
when /boolean$/
{ "type" => "boolean" }
when /date$/, /dateTime$/
{ "type" => "string", "format" => "date-time" }
when /anyURI$/
{ "type" => "string", "format" => "uri" }
when /datatypes\/edtf/, /edtf$/i
{ "type" => "string", "format" => "edtf" }
else
{ "type" => "string" }
end
end
def self.default_value_for_type(type)
case type
when "string"
""
when "integer", "number"
0
when "boolean"
false
when "array"
[]
when "object"
{}
else
""
end
end
graph = RDF::Graph.new
graph.from_ttl(shacl_file)
json_schema = {
"$schema" => "http://json-schema.org/draft-07/schema#",
"type" => "object",
"properties" => {},
"required" => []
}
graph.query([nil, RDF.type, RDF::Vocab::SHACL.NodeShape]) do |shape|
shape_subject = shape.subject
graph.query([shape_subject, RDF::Vocab::SHACL.property, nil]) do |prop_stmt|
prop_subject = prop_stmt.object
prop_name = graph.query([prop_subject, RDF::Vocab::SHACL.path, nil]).first&.object.to_s
datatype = map_rdf_datatype_to_json_schema(graph.query([prop_subject, RDF::Vocab::SHACL.datatype, nil]).first&.object)
min_count = graph.query([prop_subject, RDF::Vocab::SHACL.minCount, nil]).first&.object&.to_i
max_count = graph.query([prop_subject, RDF::Vocab::SHACL.maxCount, nil]).first&.object&.to_i
pattern = graph.query([prop_subject, RDF::Vocab::SHACL.pattern, nil]).first&.object&.to_s
json_schema["properties"][prop_name] = {}
json_schema["properties"][prop_name]["type"] = datatype["type"] if datatype
json_schema["properties"][prop_name]["pattern"] = pattern if pattern
json_schema["required"] << prop_name if min_count && min_count > 0
json_schema["properties"][prop_name]["maxItems"] = max_count if max_count
end
end
JSON.pretty_generate(json_schema)
end
def every_entity(data)
all_references = []
graph_prefix = data[:ontologies][:base][:prefix]
data[:entities].map do |entity_name, metadata|
table_name = metadata[:plural].to_s.underscore
= metadata[:description]&.gsub('\'', '')&.gsub(/\n|\r/, ' ')
properties = metadata[:properties].map do |name, property_metadata|
raise Solis::Error::NotFoundError, "A property in the #{entity_name} tab is empty #{metadata[:properties].to_json}" if name.empty?
is_fk = property_metadata[:datatype].split(':').first.eql?(data[:ontologies][:base][:prefix].to_s) ? true : false
if property_metadata.key?(:datatype) && property_metadata[:datatype].empty?
raise Solis::Error::NotFoundError,
"#{entity_name}.#{name} Has no datatype"
end
if data[:entities][property_metadata[:datatype].split(':').last.to_sym].nil? && is_fk
raise Solis::Error::NotFoundError,
"#{entity_name}.#{name} Not found in _ENTITIES tab"
end
if is_fk
references = data[:entities][property_metadata[:datatype].split(':').last.to_sym][:plural].to_s.underscore
end
mandatory = property_metadata[:cardinality][:min].to_i > 0
datatype = datatype_lookup(property_metadata[:datatype], :sql)
column_name = "#{name}#{is_fk ? '_id' : ''}"
{
schema: graph_prefix,
name: name,
column_name: column_name,
column: lambda { |type = :uml|
out = ''
case type
when :sql
if name.to_s.eql?('id')
out += "\t#{column_name} SERIAL#{mandatory ? ' NOT NULL' : ''} PRIMARY KEY"
else
out += "\t#{column_name} #{is_fk ? 'int' : datatype}#{mandatory ? ' NOT NULL' : ''}#{is_fk ? " REFERENCES #{graph_prefix}.#{references}(id)" : ''}"
end
else
cardinality_min = { '0' => '|o', '' => '}o', '1' => '||' }
cardinality_max = { '0' => 'o|', '' => 'o{', '1' => '||' }
if name.to_s.eql?('id')
out += "\t *#{name} : #{datatype} <<generated>>\n"
out += "--\n"
else
out += "\t #{mandatory ? '*' : ''}#{column_name} : #{datatype} #{is_fk ? '<<FK>>' : ''}"
end
relations = []
if property_metadata[:datatype].split(':').first.eql?(graph_prefix.to_s)
cmin = cardinality_min[(property_metadata[:cardinality][:min]).to_s]
cmax = cardinality_max[(property_metadata[:cardinality][:max]).to_s]
ref_table_name = [property_metadata[:datatype].split(':').last.to_sym,
property_metadata[:path].split(':').last.classify.to_sym].map do |m|
data[:entities][m].nil? ? nil : data[:entities][m][:plural].underscore
end.compact.first
relations << "#{table_name} #{cmin}--#{cmax} #{ref_table_name} "
end
out = { out: out, relations: relations, references: references }
end
out
},
type: datatype,
foreign_key: is_fk,
mandatory: mandatory,
references: references,
cardinality: property_metadata[:cardinality],
comment: property_metadata[:description]&.gsub('\'', '')&.gsub(/\n|\r/, ' ')
}
end
{
table: lambda { |type = :uml|
out = ''
case type
when :sql
out = "CREATE TABLE #{graph_prefix}.#{table_name}(\n"
properties.each_with_index do |property, i|
out += ", \n" if i > 0
out += property[:column].call(type)
all_references << property[:references]
end
out += "\n);\n"
unless .nil? || .empty?
out += "COMMENT ON TABLE #{graph_prefix}.#{table_name} '#{table_comment}';\n"
end
properties.each_with_index do |property, i|
if property.key?(:comment) && !property[:comment].empty?
out += "COMMENT ON COLUMN #{graph_prefix}.#{table_name}.#{property[:column_name]} IS '#{property[:comment]}';\n"
end
end
out = { out: out, references: all_references.compact.sort.each_with_object(Hash.new(0)) { |o, h| h[o] += 1 } }
else
out += "entity \"#{table_name}\" as #{table_name}"
unless properties.nil? || properties.empty?
out += "{\n"
relations = []
properties.each_with_index do |property, i|
d = property[:column].call(:uml)
out += "\n" if i > 1
out += d[:out]
relations += d[:relations]
all_references << property[:references]
end
out += "\n}\n"
out = { out: out, relations: relations }
end
end
out
},
schema: graph_prefix,
entity_name: entity_name,
name: table_name,
comment: ,
properties: properties
}
end
end
def (data)
out = data[:ontologies][:all].map do |k, v|
"@prefix #{k}: <#{v[:uri]}> ."
end.join("\n")
"#{out}\n"
end
def spreadsheet_id_from_url(sheet_url)
tmp = URI(sheet_url).path.split('/')
spreadsheet_id = tmp[tmp.index('d') + 1]
end
end
sheet_data = read_sheets(key, spreadsheet_id, options)
prefixes = sheet_data['_PREFIXES']
metadata = sheet_data['_METADATA']
raise "_PREFIXES tab must have ['base', 'prefix', 'uri'] as a header at row 1" unless (%w[base prefix uri] - prefixes.).length == 0
raise '_PREFIXES.base can only have one base URI' if prefixes.map { |m| m['base'] }.grep(/\*/).count != 1
raise "_METADATA tab must have ['key', 'value'] as a header at row 1" unless (%w[key value] - metadata.).length == 0
options[:prefixes] = prefixes
options[:metadata] = metadata
if sheet_data.is_a?(Hash)
raise "No _REFERENCES sheet found" unless sheet_data.key?("_REFERENCES")
Solis::LOGGER.info('Reading referenced ontologies')
references = sheet_data['_REFERENCES'].map do |reference|
{ sheet_url: reference['sheeturl'], description: reference['description'] }
end
cache_dir = ConfigFile.include?(:cache) ? ConfigFile[:cache] : '/tmp'
::File.open("#{::File.absolute_path(cache_dir)}/#{spreadsheet_id}.json", 'wb') do |f|
f.puts references.to_json
end
else
references = sheet_data
end
datas = []
references.each_with_index do |v, i|
progress((100/(references.length+1))*(i+1), options[:progress] || {})
sheet_id = spreadsheet_id_from_url(v[:sheet_url])
sheet_data = read_sheets(key, sheet_id, options)
unless sheet_data.key?('_PREFIXES')
sheet_data['_PREFIXES'] = prefixes
end
unless sheet_data.key?('_METADATA')
sheet_data['_METADATA'] = metadata
end
if sheet_data.key?("_PREFIXES")
datas << process_sheet(key, sheet_id, sheet_data)
sleep 30
else
datas << sheet_data
end
end
Solis::LOGGER.info('Generating SHACL')
shacl = build_shacl(datas)
Solis::LOGGER.info('Generating PLANTUML')
plantuml = build_plantuml(datas)
Solis::LOGGER.info('Generating SCHEMA')
schema = build_schema(datas)
Solis::LOGGER.info('Generating INFLECTIONS')
inflections = build_inflections(datas)
Solis::LOGGER.info('Generating JSON SCHEMA')
json_schema = build_json_schema(shacl)
{ inflections: inflections, shacl: shacl, schema: schema, plantuml: plantuml, json_schema: json_schema }
end
|