Class: Stepmod::Utils::StepmodFileAnnotator

Inherits:
Object
  • Object
show all
Defined in:
lib/stepmod/utils/stepmod_file_annotator.rb

Constant Summary collapse

SCHEMA_VERSION_MATCH_REGEX =
/^SCHEMA [0-9a-zA-Z_]+;\s*$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(express_file:, stepmod_dir: nil) ⇒ StepmodFileAnnotator

Returns a new instance of StepmodFileAnnotator.

Parameters:

  • express_file (String)

    path to the exp file needed to annotate

  • resource_docs_cache (String)

    output of ./stepmod-build-resource-docs-cache



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 19

def initialize(express_file:, stepmod_dir: nil)
  @express_file = express_file
  @resource_docs_cache = resource_docs_schemas(stepmod_dir)
  @stepmod_dir = stepmod_dir || Dir.pwd
  @added_bibdata = {}
  @images_references = {}

  @schema_name = Expressir::Express::Parser.from_file(express_file)
                                           .schemas
                                           .first
                                           .id
  @schema_name = normalize_schema_name(@schema_name)
  self
end

Instance Attribute Details

#express_fileObject (readonly)

Returns the value of attribute express_file.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def express_file
  @express_file
end

#resource_docs_cacheObject (readonly)

Returns the value of attribute resource_docs_cache.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def resource_docs_cache
  @resource_docs_cache
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def schema_name
  @schema_name
end

#stepmod_dirObject (readonly)

Returns the value of attribute stepmod_dir.



15
16
17
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 15

def stepmod_dir
  @stepmod_dir
end

Instance Method Details

#build_schema_string_with_versionObject



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
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 58

def build_schema_string_with_version
  # Geometric_tolerance_arm => geometric-tolarance-arm
  name_in_asn1 = @schema_name.downcase.gsub("_", "-")
  schema_type, type_number = case @schema_name.downcase
  when /_arm\Z/i
    [:module, 1]
  when /_mim\Z/i
    [:module, 2]
  when /_arm_lf\Z/i
    [:module, 3]
  when /_mim_lf\Z/i
    [:module, @module_has_arm_lf ? 4 : 3]
  else # any resource schema without version strings
    puts "[annotator-WARNING] this resource schema is missing a version string: #{@schema_name}"
    [:resource, 1]
  end

  # TODO there are schemas with only arm, arm_lf:
  # schemas/modules/reference_schema_for_sysml_mapping/arm_lf.exp
  # TODO there are schemas with only arm, mim, mim_lf:
  # schemas/modules/limited_length_or_area_indicator_assignment/mim_lf.exp
  part = @identifier.part
  edition = @identifier.edition
  schema_or_object = (schema_type == :module) ? "schema" : "object"

  "SCHEMA #{@schema_name} '{ " \
    "iso standard 10303 part(#{part}) " \
    "version(#{edition}) " \
    "#{schema_or_object}(1) " \
    "#{name_in_asn1}(#{type_number}) " \
  "}';\n"
end

#callObject



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
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 109

def call
  match = File.basename(express_file).match('^(arm|mim|bom|arm_lf|mim_lf|DomainModel)\.exp$')
  descriptions_base = match ? "#{match.captures[0]}_descriptions.xml" : "descriptions.xml"

  descriptions_file = File.join(File.dirname(express_file),
                                descriptions_base)

  output_express = File.read(express_file)
  converted_description = ""
  base_linked = ""
  processed_images_cache = {}

  if File.exist?(descriptions_file)
    descriptions = Nokogiri::XML(File.read(descriptions_file)).root
    added_resource_descriptions = {}

    descriptions.xpath("ext_description").each do |description|
      # Add base resource from linked path if exists, eg "language_schema.language.wr:WR1" -> "language_schema"
      base_linked = description["linkend"].to_s.split(".").first

      if added_resource_descriptions[base_linked].nil?
        base_reource_doc_dir = resource_docs_cache[description["linkend"].to_s.split(".").first]
        if base_reource_doc_dir
          converted_description << convert_from_resource_file(
            base_reource_doc_dir, stepmod_dir, base_linked, descriptions_file
          )
        end
        added_resource_descriptions[base_linked] = true
      end
      resource_docs_dir = resource_docs_cache[description["linkend"]]
      # Convert content description
      # when a schema description is available from resource.xml and also descriptions.xml, the description from resource.xml is only used.
      # https://github.com/metanorma/annotated-express/issues/32#issuecomment-792609078
      if description.text.strip.length.positive? && resource_docs_dir.nil?
        converted_description << convert_from_description_text(
          descriptions_file, description
        )
      end

      schema_base_dir = resource_docs_cache[base_linked]
      add_images_references(converted_description, schema_base_dir,
                            processed_images_cache)

      # Add converted description from exact linked path
      if resource_docs_dir && added_resource_descriptions[description["linkend"]].nil?
        output_express << convert_from_resource_file(resource_docs_dir,
                                                     stepmod_dir, description["linkend"], descriptions_file)
        added_resource_descriptions[description["linkend"]] = true
      end
    end
  end

  bib_file_name = extract_bib_file_name(match,
                                        resource_docs_cache[@schema_name || ""])
  bib_file = if match
               File.join(File.dirname(express_file), bib_file_name)
             else
               resource_docs_file_path(stepmod_dir, bib_file_name)
             end

  unless bib_file && File.exist?(bib_file)
    raise StandardError.new(
      "bib_file for #{schema_name} does not exist: #{bib_file}"
    )
  end

  output_express << prepend_bibdata(
                        converted_description || "",
                        # bib_file will not be present for resouces
                        # that are not in resource_docs cache.
                        # e.g hierarchy_schema
                        bib_file,
                        @schema_name,
                        match,
                      )

  if is_missing_version(output_express)
    puts "[annotator-WARNING] schema (#{@schema_name}) missing version string. "\
      "Adding: `#{build_schema_string_with_version}` to schema."

    output_express.gsub!(
      SCHEMA_VERSION_MATCH_REGEX,
      build_schema_string_with_version
    )
  end

  {
    annotated_text: sanitize(output_express),
    images_references: @images_references,
  }
rescue StandardError => e
  puts "[ERROR]!!! #{e.message}"
  puts e.backtrace
end

#is_missing_version(schema_content) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 47

def is_missing_version(schema_content)
  m = schema_content.match(SCHEMA_VERSION_MATCH_REGEX)
  if m.nil?
    false
  elsif m[0] # match
    true
  else
    false
  end
end

#normalize_schema_name(name) ⇒ Object

Needed to fix scheme casing issues, e.g. xxx_LF => xxx_lf



35
36
37
38
39
40
41
42
43
44
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 35

def normalize_schema_name(name)
  case name.downcase
  # module schemas have first letter capitalized, rest in lowercase
  when /_arm_lf\Z/i, /_mim_lf\Z/i, /_arm\Z/i, /_mim\Z/i
    name.downcase.capitalize
  # resource schemas are all in lowercase
  else
    name.downcase
  end
end

#resource_docs_schemas(stepmod_dir) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stepmod/utils/stepmod_file_annotator.rb', line 91

def resource_docs_schemas(stepmod_dir)
  filepath = File.join(stepmod_dir, "data", "resource_docs", "*",
                       "resource.xml")

  schemas = {}
  Dir.glob(filepath).each do |resource_docs_file|
    match = resource_docs_file.match("data[/\]resource_docs[/\]([^/\]+)[/\]resource.xml")
    resource_docs_dir = match.captures[0]

    resource_docs = Nokogiri::XML(File.read(resource_docs_file)).root
    resource_docs.xpath("schema").each do |schema|
      schemas[schema["name"]] = resource_docs_dir
    end
  end

  schemas
end