stepmod-utils 0.3.17 → 0.3.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8be21fe4d04046487ecb36fdebeb74f8dbbb7bda473cbb1b3451b62b15d62a0
4
- data.tar.gz: f3005c150d30d5c8aac9b77f9221cfcfc575e0d541208152087f6ff88623375d
3
+ metadata.gz: 2a0c01510a286c5aa7e3b0a32a910b7b3637726e01cb7433f8552ef6758f3fa5
4
+ data.tar.gz: 0a3fd286cb1f42a4d5cb52989616cb36cc39cffd49c0cd6a7219685b9b83dfba
5
5
  SHA512:
6
- metadata.gz: b004697a0c9eeecc570ba7e184fd7165fa991be9b0046f13305106a6cbdff123aca57299c340ce72fb95214ad878a0d0aff140af5e07308070d1c8a336c09921
7
- data.tar.gz: 9aaa80fff000066c954b660592626a9ee0132cfca173c0d93aeb572211adf1929d79dc1f1336858725b031b287d1a9d07f9e4d79e45b8d4a4727819fbe303b9f
6
+ metadata.gz: 4cca0b6de2ee2109e0f11f144406a14f58a54cbd2030ae15180d897a923bf8c98de36b9f8c1d59445a2cde22a1cc2c9ac70474f6b1262fb6a7faa4bec460a631
7
+ data.tar.gz: c4c78c48c121619e7e47fda0a86caeaec622535cd5c57468e1316998fa4ab7a2c73549fe227c0a7df2bde4ea69f32852b52018304cfabe6c129241f8a2b54484
@@ -153,10 +153,13 @@ end
153
153
 
154
154
  log "INFO: written summary file to: 04x-stepmod-entities-resources.adoc"
155
155
 
156
- part_modules.each do |(bibdata, part_modules_arm, part_modules_mim)|
156
+ part_modules.sort_by do |(bibdata, part_modules_arm, part_modules_mim)|
157
+ bibdata.part.to_i
158
+ end.each do |(bibdata, part_modules_arm, part_modules_mim)|
157
159
  fn = "05x-stepmod-entities-modules-#{bibdata.part}.adoc"
158
160
  File.open(fn, "w") do |file|
159
161
  file.puts("")
162
+
160
163
  unless part_modules_arm.empty?
161
164
  schema_name = part_modules_arm.first.first
162
165
  concepts = part_modules_arm.first.last.to_a.map do |n|
@@ -181,6 +184,7 @@ part_modules.each do |(bibdata, part_modules_arm, part_modules_mim)|
181
184
  file.puts(replace_images(concepts.map(&:to_mn_adoc).join("\n")))
182
185
  end
183
186
  end
187
+
184
188
  log "INFO: written to: #{fn}"
185
189
  end
186
190
 
@@ -10,6 +10,8 @@ module Stepmod
10
10
  file_path
11
11
  )
12
12
 
13
+ # TODO: converted_definition is not supposed to be an attribute, it is
14
+ # supposed to be a method!
13
15
  class << self
14
16
  def parse(definition_xml, reference_anchor:, reference_clause:, file_path:, language_code: "en")
15
17
  converted_definition = Stepmod::Utils::StepmodDefinitionConverter.convert(
@@ -35,6 +37,8 @@ module Stepmod
35
37
  ).strip
36
38
  end
37
39
 
40
+ # TODO: `designations:` should include the `alt:[...]` terms here,
41
+ # they are now only included in definition_xml_converted_definition.
38
42
  new(
39
43
  designations: [designation],
40
44
  definition: definition,
@@ -26,7 +26,8 @@ module Stepmod
26
26
  :part_concepts,
27
27
  :part_resources,
28
28
  :part_modules,
29
- :stdout
29
+ :stdout,
30
+ :git_rev
30
31
 
31
32
  def self.call(stepmod_dir, index_path, stdout = $stdout)
32
33
  new(stepmod_dir, index_path, stdout).call
@@ -65,6 +66,12 @@ module Stepmod
65
66
  log "INFO: STEPmod directory set to #{stepmod_dir}."
66
67
  log "INFO: Detecting paths..."
67
68
 
69
+ # Run `cvs status` to find out version
70
+ log "INFO: Detecting Git SHA..."
71
+ Dir.chdir(stepmod_path) do
72
+ @git_rev = `git rev-parse HEAD` || nil
73
+ end
74
+
68
75
  repo_index = Nokogiri::XML(File.read(@index_path)).root
69
76
 
70
77
  files = []
@@ -134,8 +141,8 @@ module Stepmod
134
141
  end
135
142
 
136
143
  unless ACCEPTED_STAGES.include? bibdata.doctype
137
- log "INFO: skipped #{bibdata.docid} as it is not \
138
- one of (#{ACCEPTED_STAGES.join(', ')})."
144
+ log "INFO: skipped #{bibdata.docid} as it is not " \
145
+ "one of (#{ACCEPTED_STAGES.join(', ')})."
139
146
  next
140
147
  end
141
148
 
@@ -145,23 +152,9 @@ module Stepmod
145
152
  next
146
153
  end
147
154
 
148
- revision_string = ""
149
-
150
- # Run `cvs status` to find out version
151
- log "INFO: Detecting Git SHA..."
152
- Dir.chdir(stepmod_path) do
153
- git_sha = `git rev-parse HEAD`
154
-
155
- unless git_sha.empty?
156
- revision_string = "\n// Git: SHA #{git_sha}"
157
- end
158
- end
159
-
160
155
  # read definitions
161
156
  current_part_concepts = Glossarist::Collection.new
162
- definition_index = 0
163
- current_document.xpath("//definition").each do |definition|
164
- definition_index += 1
157
+ current_document.xpath("//definition").each_with_index do |definition, definition_index|
165
158
  term_id = definition["id"]
166
159
  unless term_id.nil?
167
160
  if encountered_terms[term_id]
@@ -178,7 +171,7 @@ module Stepmod
178
171
  definition,
179
172
  reference_anchor: bibdata.anchor,
180
173
  reference_clause: ref_clause,
181
- file_path: fpath + revision_string,
174
+ file_path: fpath,
182
175
  )
183
176
  next unless concept
184
177
 
@@ -197,10 +190,10 @@ module Stepmod
197
190
  current_part_modules_arm = {}
198
191
  current_part_modules_mim = {}
199
192
 
200
- log "INFO: FILE PATH IS #{file_path}"
193
+ # log "INFO: FILE PATH IS #{file_path}"
201
194
  case file_path.to_s
202
195
  when /resource.xml$/
203
- log "INFO: Processing resource.xml for #{file_path}"
196
+ log "INFO: Processing resource.xml for #{fpath}"
204
197
 
205
198
  current_document.xpath("//schema").each do |schema_node|
206
199
  schema_name = schema_node["name"]
@@ -238,7 +231,7 @@ module Stepmod
238
231
  id: "#{reference_anchor}.#{reference_clause}",
239
232
  reference_anchor: reference_anchor,
240
233
  reference_clause: reference_clause,
241
- file_path: Pathname.new(file_path)
234
+ file_path: Pathname.new(exp_annotated_path)
242
235
  .relative_path_from(stepmod_path),
243
236
  language_code: "en",
244
237
  )
@@ -259,7 +252,7 @@ module Stepmod
259
252
  end
260
253
 
261
254
  when /module.xml$/
262
- log "INFO: Processing module.xml for #{file_path}"
255
+ log "INFO: Processing module.xml for #{fpath}"
263
256
  # Assumption: every schema is only linked by a single module document.
264
257
  # puts current_document.xpath('//module').length
265
258
  schema_name = current_document.xpath("//module").first["name"]
@@ -272,101 +265,36 @@ module Stepmod
272
265
  parsed_schema_names[schema_name] = file_path
273
266
  end
274
267
 
275
- exp_annotated_path =
276
- "#{stepmod_path}/modules/#{schema_name}/arm_annotated.exp"
277
-
278
- log "INFO: Processing modules schema #{exp_annotated_path}"
279
-
280
- if File.exists?(exp_annotated_path)
281
- repo = Expressir::Express::Parser.from_file(exp_annotated_path)
282
-
283
- repo.schemas.each do |schema|
284
- schema.entities.each do |entity|
285
- old_definition = entity.remarks.first
286
-
287
- domain = "application module: #{schema.id}"
288
- entity_definition = generate_entity_definition(entity, domain, old_definition)
289
-
290
- reference_anchor = bibdata.anchor
291
- reference_clause = nil
292
-
293
- concept = Stepmod::Utils::Concept.new(
294
- designations: [entity.id],
295
- definition: old_definition,
296
- converted_definition: entity_definition,
297
- id: "#{reference_anchor}.#{reference_clause}",
298
- reference_anchor: reference_anchor,
299
- reference_clause: reference_clause,
300
- file_path: Pathname.new(file_path)
301
- .relative_path_from(stepmod_path),
302
- language_code: "en",
303
- )
304
-
305
- next unless concept
268
+ arm_schema, arm_concepts = parse_annotated_module(
269
+ type: :arm,
270
+ stepmod_path: stepmod_path,
271
+ path: "modules/#{schema_name}/arm_annotated.exp",
272
+ bibdata: bibdata
273
+ )
306
274
 
307
- current_part_modules_arm[schema.id] ||=
308
- Glossarist::Collection.new
309
- find_or_initialize_concept(
310
- current_part_modules_arm[schema.id], concept
311
- )
275
+ mim_schema, mim_concepts = parse_annotated_module(
276
+ type: :mim,
277
+ stepmod_path: stepmod_path,
278
+ path: "modules/#{schema_name}/mim_annotated.exp",
279
+ bibdata: bibdata
280
+ )
312
281
 
313
- # puts part_modules_arm.inspect
314
- parsed_bibliography << bibdata
315
- end
316
- end
282
+ if arm_concepts.to_a.size > 0
283
+ current_part_modules_arm[arm_schema] = arm_concepts
317
284
  end
318
285
 
319
- mim_exp_annotated_path = "#{stepmod_path}/modules/#{schema_name}/mim_annotated.exp"
320
-
321
- log "INFO: Processing modules schema #{mim_exp_annotated_path}"
322
-
323
- if File.exists?(mim_exp_annotated_path)
324
- repo = Expressir::Express::Parser.from_file(mim_exp_annotated_path)
325
-
326
- repo.schemas.each do |schema|
327
- schema.entities.each do |entity|
328
- old_definition = entity.remarks.first
329
-
330
- domain = "application module: #{schema.id}"
331
- definition = generate_entity_definition(entity, domain, old_definition)
332
-
333
- reference_anchor = bibdata.anchor
334
- reference_clause = nil
335
-
336
- concept = Stepmod::Utils::Concept.new(
337
- designations: [entity.id],
338
- definition: old_definition,
339
- converted_definition: definition,
340
- id: "#{reference_anchor}.#{reference_clause}",
341
- reference_anchor: reference_anchor,
342
- reference_clause: reference_clause,
343
- file_path: Pathname.new(file_path)
344
- .relative_path_from(stepmod_path),
345
- language_code: "en",
346
- )
347
-
348
- next unless concept
349
-
350
- current_part_modules_mim[schema.id] ||=
351
- Glossarist::Collection.new
352
- find_or_initialize_concept(
353
- current_part_modules_mim[schema.id], concept
354
- )
355
-
356
- parsed_bibliography << bibdata
357
- end
358
- end
286
+ if mim_concepts.to_a.size > 0
287
+ current_part_modules_mim[mim_schema] = mim_concepts
359
288
  end
360
-
361
289
  end
362
290
 
363
291
  log "INFO: Completed processing XML file #{fpath}"
364
292
  if current_part_concepts.to_a.empty?
365
- log "INFO: Skipping #{fpath} (#{bibdata.docid}) \
366
- because it contains no concepts."
293
+ log "INFO: Skipping #{fpath} (#{bibdata.docid}) " \
294
+ "because it contains no concepts."
367
295
  elsif current_part_concepts.to_a.length < 3
368
- log "INFO: Skipping #{fpath} (#{bibdata.docid}) \
369
- because it only has #{current_part_concepts.to_a.length} terms."
296
+ log "INFO: Skipping #{fpath} (#{bibdata.docid}) " \
297
+ "because it only has #{current_part_concepts.to_a.length} terms."
370
298
 
371
299
  current_part_concepts.to_a.each do |x|
372
300
  general_concepts.store(x)
@@ -377,38 +305,201 @@ module Stepmod
377
305
  current_part_concepts]
378
306
  end
379
307
  end
308
+
380
309
  unless current_part_resources.to_a.empty?
381
310
  part_resources << [bibdata,
382
311
  current_part_resources]
383
312
  end
313
+
384
314
  if (current_part_modules_arm.to_a.size +
385
315
  current_part_modules_mim.to_a.size).positive?
316
+
386
317
  part_modules << [bibdata, current_part_modules_arm,
387
318
  current_part_modules_mim]
319
+ parsed_bibliography << bibdata
388
320
  end
321
+
389
322
  end
390
323
  end
391
324
 
392
- def find_or_initialize_concept(collection, localized_concept)
393
- concept_id = SecureRandom.uuid
325
+ def parse_annotated_module(type:, stepmod_path:, path:, bibdata:)
326
+ log "INFO: parse_annotated_module: Processing modules schema #{path}"
327
+
328
+ fpath = File.join(stepmod_path, path)
329
+
330
+ unless File.exists?(fpath)
331
+ log "ERROR: parse_annotated_module: No module schema exists at #{fpath}."
332
+ return
333
+ end
334
+
335
+ repo = Expressir::Express::Parser.from_file(fpath)
336
+
337
+ unless repo
338
+ log "ERROR: parse_annotated_module: failed to parse EXPRESS file at #{path}."
339
+ return
340
+ end
341
+
342
+ # See: metanorma/iso-10303-2#90
343
+ domain_prefix = case type
344
+ when :mim
345
+ "application module"
346
+ when :arm
347
+ "application object"
348
+ end
394
349
 
350
+ if repo.schemas.length > 1
351
+ raise StandardError.new(
352
+ "ERROR: FATAL: #{fpath} contains more than one schema:" +
353
+ "#{repo.schemas.map(&:id).join(", ")} (not supposed to happen!!)"
354
+ )
355
+ end
356
+
357
+ schema = repo.schemas.first
358
+ collection = Glossarist::Collection.new
359
+ domain = "#{domain_prefix}: #{schema.id}"
360
+
361
+ schema.entities.each do |entity|
362
+ old_definition = entity.remarks.first
363
+ new_definition = generate_entity_definition(entity, domain, old_definition)
364
+
365
+ concept = Stepmod::Utils::Concept.new(
366
+ designations: [entity.id],
367
+ definition: old_definition,
368
+ converted_definition: new_definition,
369
+ # TODO: Find a proper ID for this
370
+ id: "#{bibdata.anchor}.",
371
+ reference_anchor: bibdata.anchor,
372
+ reference_clause: nil,
373
+ file_path: path,
374
+ language_code: "en",
375
+ )
376
+
377
+ next unless concept
378
+ find_or_initialize_concept(collection, concept)
379
+ end
380
+
381
+ [schema.id, collection]
382
+ end
383
+
384
+ def find_or_initialize_concept(collection, localized_concept)
395
385
  concept = collection
396
- .store(Glossarist::Concept.new(id: concept_id))
386
+ .store(Glossarist::Concept.new(id: SecureRandom.uuid))
397
387
  concept.add_l10n(localized_concept)
398
388
  end
399
389
 
390
+ def combine_paragraphs(full_paragraph, next_paragraph)
391
+ # If full_paragraph already contains a period, extract that.
392
+ if m = full_paragraph.match(/\A(?<inner_first>[^\n]*?\.)\s/)
393
+ # puts "CONDITION 1"
394
+ if m[:inner_first]
395
+ return m[:inner_first]
396
+ else
397
+ return full_paragraph
398
+ end
399
+ end
400
+
401
+ # If full_paragraph ends with a period, this is the last.
402
+ if full_paragraph =~ /\.\s*\Z/
403
+ # puts "CONDITION 2"
404
+ return full_paragraph
405
+ end
406
+
407
+ # If next_paragraph is a list
408
+ if next_paragraph.match(/\A\*/)
409
+ # puts "CONDITION 3"
410
+ return full_paragraph + "\n\n" + next_paragraph
411
+ end
412
+
413
+ # If next_paragraph is a continuation of a list
414
+ if next_paragraph.match(/\Awhich/) || next_paragraph.match(/\Athat/)
415
+ # puts "CONDITION 4"
416
+ return full_paragraph + "\n\n" + next_paragraph
417
+ end
418
+
419
+ # puts "CONDITION 5"
420
+ full_paragraph
421
+ end
422
+
423
+ def trim_definition(definition)
424
+ # Unless the first paragraph ends with "between" and is followed by a
425
+ # list, don't split
426
+ paragraphs = definition.split("\n\n")
427
+
428
+ # puts paragraphs.inspect
429
+
430
+ first_paragraph = paragraphs.first
431
+
432
+ if paragraphs.length > 1
433
+ combined = paragraphs[1..-1].inject(first_paragraph) do |acc, p|
434
+ combine_paragraphs(acc, p)
435
+ end
436
+ else
437
+ combined = combine_paragraphs(first_paragraph, "")
438
+ end
439
+
440
+ # puts "combined--------- #{combined}"
441
+
442
+ # Remove comments until end of line
443
+ combined = combined + "\n"
444
+ combined.gsub!(/\n\/\/.*?\n/, "\n")
445
+ combined.strip!
446
+
447
+ combined
448
+ # # TODO: If the definition contains a list immediately after the first paragraph, don't split
449
+ # return definition if definition =~ /\n\* /
450
+
451
+ # unless (
452
+ # first_paragraph =~ /between:?\s*\Z/ ||
453
+ # first_paragraph =~ /include:?\s*\Z/ ||
454
+ # first_paragraph =~ /of:?\s*\Z/ ||
455
+ # first_paragraph =~ /[:;]\s*\Z/
456
+ # ) &&
457
+ # definition =~ /\n\n\*/
458
+
459
+ # # Only taking the first paragraph of the definition
460
+ # first_paragraph
461
+ # end
462
+ end
463
+
464
+ def entity_name_to_text(entity_id)
465
+ entity_id.downcase.gsub(/_/, " ")
466
+ end
467
+
400
468
  # rubocop:disable Layout/LineLength
401
469
  def generate_entity_definition(entity, domain, old_definition)
402
470
  return "" if entity.nil?
403
471
 
404
- entity_text = if entity.subtype_of.size.zero?
405
- "entity data type that represents " +
406
- entity.id.indefinite_article + " **#{entity.id}** entity"
407
- else
408
- "entity data type that is a type of "+
409
- "**#{entity.subtype_of.map(&:id).join('** and **')}** that represents " +
410
- entity.id.indefinite_article + " **#{entity.id}** entity"
411
- end
472
+ # See: metanorma/iso-10303-2#90
473
+ # TODO: This is not DRY in case we have to further customize
474
+ entity_text = if domain_type = domain.match(/\A(application.*?):/)
475
+
476
+ if entity.subtype_of.size.zero?
477
+ "#{domain_type[1]} that represents the " +
478
+ "{{#{entity.id},#{entity_name_to_text(entity.id)}}} entity"
479
+ else
480
+ entity_subtypes = entity.subtype_of.map do |e|
481
+ "{{#{e.id},#{entity_name_to_text(e.id)}}}"
482
+ end
483
+ "#{domain_type[1]} that is a type of " +
484
+ "#{entity_subtypes.join(' and ')} that represents the " +
485
+ "{{#{entity.id},#{entity_name_to_text(entity.id)}}} entity"
486
+ end
487
+
488
+ else
489
+
490
+ # Not "application object" or "application module"
491
+ if entity.subtype_of.size.zero?
492
+ "entity data type that represents " +
493
+ entity.id.indefinite_article + " {{#{entity.id}}} entity"
494
+ else
495
+ entity_subtypes = entity.subtype_of.map do |e|
496
+ "{{#{e.id}}}"
497
+ end
498
+ "entity data type that is a type of "+
499
+ "#{entity_subtypes.join(' and ')} that represents " +
500
+ entity.id.indefinite_article + " {{#{entity.id}}} entity"
501
+ end
502
+ end
412
503
 
413
504
  definition = <<~DEFINITION
414
505
  === #{entity.id}
@@ -418,11 +509,14 @@ module Stepmod
418
509
 
419
510
  DEFINITION
420
511
 
512
+ # If there is a definition, we add it as the first NOTE
421
513
  unless old_definition.nil? || old_definition.blank?
514
+ old_definition = trim_definition(old_definition)
515
+
422
516
  definition << <<~OLD_DEFINITION
423
517
  [NOTE]
424
518
  --
425
- #{old_definition&.strip}
519
+ #{old_definition.strip}
426
520
  --
427
521
  OLD_DEFINITION
428
522
  end
@@ -1,5 +1,5 @@
1
1
  module Stepmod
2
2
  module Utils
3
- VERSION = "0.3.17".freeze
3
+ VERSION = "0.3.20".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stepmod-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.17
4
+ version: 0.3.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-07 00:00:00.000000000 Z
11
+ date: 2022-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -253,7 +253,7 @@ homepage: https://github.com/metanorma/stepmod-utils
253
253
  licenses:
254
254
  - BSD-2-Clause
255
255
  metadata: {}
256
- post_install_message:
256
+ post_install_message:
257
257
  rdoc_options: []
258
258
  require_paths:
259
259
  - lib
@@ -268,8 +268,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
268
268
  - !ruby/object:Gem::Version
269
269
  version: '0'
270
270
  requirements: []
271
- rubygems_version: 3.3.7
272
- signing_key:
271
+ rubygems_version: 3.1.6
272
+ signing_key:
273
273
  specification_version: 4
274
274
  summary: Stepmod-utils is a toolkit that works on STEPmod data.
275
275
  test_files: []