Class: ActiveFacts::Generate::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/generate/json.rb

Overview

Generate json output from a vocabulary, for loading into APRIMO. Invoke as

afgen --json <file>.cql=diagrams

Instance Method Summary collapse

Instance Method Details

#generate(out = $>) ⇒ Object



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
# File 'lib/activefacts/generate/json.rb', line 27

def generate(out = $>)
  @out = out
  uuids = {}

  puts "{ model: '#{@vocabulary.name}',\n" +
  "diagrams: [\n#{
    @vocabulary.all_diagram.sort_by{|o| o.name.gsub(/ /,'')}.map do |d|
      j = {:uuid => (uuids[d] ||= uuid_from_id(d)), :name => d.name}
      "    #{j.to_json}"
    end*",\n"
  }\n  ],"

  object_types = @vocabulary.all_object_type.sort_by{|o| o.name.gsub(/ /,'')}
  puts "  object_types: [\n#{
    object_types.sort_by{|o|o.identifying_role_values.inspect}.map do |o|
      uuids[o] ||= uuid_from_id(o)
      ref_mode = nil
      if o.is_a?(ActiveFacts::Metamodel::EntityType) and
        p = o.preferred_identifier and
        (rrs = p.role_sequence.all_role_ref).size == 1 and
        (r = rrs.single.role).fact_type != o.fact_type and
        r.object_type.is_a?(ActiveFacts::Metamodel::ValueType) and
        !r.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
        ref_mode = "#{r.object_type.name}"
        ref_mode.sub!(%r{#{o.name} *}, '.')
      end
      j = {
        :uuid => uuids[o],
        :name => o.name,
        :shapes => o.all_object_type_shape.sort_by{|s| [s.location.x, s.location.y]}.map do |shape|
          x = { :diagram => uuids[shape.orm_diagram],
            :is_expanded => shape.is_expanded,
            :uuid => uuid_from_id(shape),
            :x => shape.location.x,
            :y => shape.location.y
          }
          x[:is_expanded] = true if ref_mode && shape.is_expanded  # Don't show the reference mode
          x
        end
      }
      j[:ref_mode] = ref_mode if ref_mode
      j[:independent] = true if o.is_independent

      if o.is_a?(ActiveFacts::Metamodel::EntityType)
        # Entity Type may be objectified, and may have supertypes:
        if o.fact_type
          uuid = (uuids[o.fact_type] ||= uuid_from_id(o.fact_type))
          j[:objectifies] = uuid
          j[:implicit] = true if o.concept.implication_rule
        end
        if o.all_type_inheritance_as_subtype.size > 0
          j[:supertypes] = o.
            all_type_inheritance_as_subtype.
            sort_by{|ti| ti.provides_identification ? 0 : 1}.
            map{|ti|
              [ uuids[ti.supertype] ||= uuid_from_id(ti.supertype),
                uuids[ti.supertype_role] = uuid_from_id(ti.supertype_role)
              ]
            }
        end
      else
        # ValueType usually has a supertype:
        if (o.supertype)
          j[:supertype] = (uuids[o.supertype] ||= uuid_from_id(o.supertype))
        end
      end
      # REVISIT: Place a ValueConstraint and shape
      "    #{j.to_json}"
    end*",\n"
  }\n  ],"

  fact_types = @vocabulary.constellation.
    FactType.values.
    reject{|ft|
      ActiveFacts::Metamodel::LinkFactType === ft || ActiveFacts::Metamodel::TypeInheritance === ft
    }
  puts "  fact_types: [\n#{
    fact_types.sort_by{|f| f.identifying_role_values.inspect}.map do |f|
      uuids[f] ||= uuid_from_id(f)
      j = {:uuid => uuids[f]}

      if f.entity_type
        j[:objectified_as] = uuids[f.entity_type]
      end

      # Emit roles
      roles = f.all_role.sort_by{|r| r.ordinal }
      j[:roles] = roles.map do |role|
        uuid = (uuids[role] ||= uuid_from_id(role))
        # REVISIT: Internal Mandatory Constraints
        # REVISIT: Place a ValueConstraint and shape
        # REVISIT: Place a RoleName shape
        {:uuid => uuid, :player => uuids[role.object_type]}
        # N.B. The object_type shape to which this role is attached is not in the meta-model
        # Attach to the closest instance on this diagram (if any)
      end

      # Emit readings. Each is a [role_order, text] pair
      j[:readings] = f.all_reading.map do |r|
        role_refs = r.role_sequence.all_role_ref_in_order
        [
          role_order(uuids, role_refs.map{|rr| rr.role}, roles),
          r.text.gsub(/\{([0-9])\}/) do |insert|
            role_ref = role_refs[$1.to_i]
            la = role_ref.leading_adjective
            la = nil if la == ''
            ta = role_ref.trailing_adjective
            ta = nil if ta == ''
            (la ? la+'-' : '') +
              (la && la.index(' ') ? ' ' : '') +
              insert +
              (ta && ta.index(' ') ? ' ' : '') +
              (ta ? '-'+ta : '')
          end
        ]
      end.sort_by{|(ro,text)| ro }.map do |(ro,text)|
        [ ro, text ]
      end

      # Emit shapes
      j[:shapes] = f.all_fact_type_shape.sort_by{|s| [s.location.x, s.location.y]}.map do |shape|
        sj = {
          :diagram => uuids[shape.orm_diagram],
          :uuid => uuid_from_id(shape),
          :x => shape.location.x,
          :y => shape.location.y
        }

        # Add the role_order, if specified
        if shape.all_role_display.size > 0
          if shape.all_role_display.size != roles.size
            raise "Invalid RoleDisplay for #{f.default_reading} in #{shape.orm_diagram.name} diagram"
          end
          ro = role_order(
            uuids,
            shape.all_role_display.sort_by{|rd| rd.ordinal }.map{|rd| rd.role },
            roles
          )
          sj[:role_order] = ro if ro
        end

        # REVISIT: Place the ReadingShape

        # Emit the location of the name, if objectified
        if n = shape.objectified_fact_type_name_shape
          sj[:name_shape] = {:x => n.location.x, :y => n.location.y}
        end
        sj
      end

      # Emit Internal Presence Constraints
      f.internal_presence_constraints.to_a.sort_by{|ipc, z|
		  [ipc.is_preferred_identifier ? 0 : 1, ipc.is_mandatory ? 0 : 1, ipc.min_frequency || 0, ipc.max_frequency || 1_000]
		}.each do |ipc|
        uuid = (uuids[ipc] ||= uuid_from_id(ipc))

        constraint = {
          :uuid => uuid,
          :min => ipc.min_frequency,
          :max => ipc.max_frequency,
          :is_preferred => ipc.is_preferred_identifier,
          :mandatory => ipc.is_mandatory
        }

        # Get the role (or excluded role, for a UC)
        roles = ipc.role_sequence.all_role_ref_in_order.map{|r| r.role}
        if roles.size > 1 || (!ipc.is_mandatory && ipc.max_frequency == 1)
          # This can be only a uniqueness constraint. Record the missing role, if any
          role = (f.all_role.to_a - roles)[0]
          constraint[:uniqueExcept] = uuids[role]
        else
          # An internal mandatory or frequency constraint applies to only one role.
          # If it's also unique (max == 1), that applies on the counterpart role.
          # You can also have a mandatory frequency constraint, but that applies on this role.
          constraint[:role] = uuids[roles[0]]
        end
        (j[:constraints] ||= []) << constraint
      end

      # Add ring constraints
      f.all_role_in_order.
        map{|r| r.all_ring_constraint.to_a+r.all_ring_constraint_as_other_role.to_a }.
        flatten.uniq.each do |ring|
          (j[:constraints] ||= []) << {
              :uuid => (uuids[ring] ||= uuid_from_id(ring)),
              :shapes => ring.all_constraint_shape.sort_by{|s| [s.location.x, s.location.y]}.map do |shape|
                { :diagram => uuids[shape.orm_diagram],
                  :uuid => uuid_from_id(shape),
                  :x => shape.location.x,
                  :y => shape.location.y
                }
              end,
              :ringKind => ring.ring_type,
              :roles => [uuids[ring.role], uuids[ring.other_role]]
              # REVISIT: Deontic, enforcement
            }
        end

      # REVISIT: RotationSetting

      "    #{j.to_json}"
    end*",\n"
  }\n  ],"

  constraints = @vocabulary.constellation.
    Constraint.values
  puts "  constraints: [\n#{
    constraints.sort_by{|c|c.identifying_role_values.inspect}.select{|c| !uuids[c]}.map do |c|
      uuid = uuids[c] ||= uuid_from_id(c)
      j = {
        :uuid => uuid,
        :type => c.class.basename,
        :shapes => c.all_constraint_shape.sort_by{|s| [s.location.x, s.location.y]}.map do |shape|
          { :diagram => uuids[shape.orm_diagram],
            :uuid => uuid_from_id(shape),
            :x => shape.location.x,
            :y => shape.location.y
          }
        end
      }

      if (c.enforcement)
        # REVISIT: Deontic constraint
      end
      if (c.concept.all_context_note_as_relevant_concept.size > 0)
        # REVISIT: Context Notes
      end

      case c
      when ActiveFacts::Metamodel::PresenceConstraint
        j[:min_frequency] = c.min_frequency
        j[:max_frequency] = c.max_frequency
        j[:is_mandatory] = c.is_mandatory
        j[:is_preferred_identifier] = c.is_preferred_identifier
        rss = [c.role_sequence.all_role_ref_in_order.map(&:role)]

        # Ignore internal presence constraints on TypeInheritance fact types
        next nil if !c.role_sequence.all_role_ref.
          detect{|rr|
            !rr.role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
          }

      when ActiveFacts::Metamodel::RingConstraint
        next nil  # These are emitted with the corresponding fact type

      when ActiveFacts::Metamodel::SetComparisonConstraint
        rss = c.
          all_set_comparison_roles.sort_by{|scr| scr.ordinal}.
          map{|scr| scr.role_sequence.all_role_ref_in_order.map(&:role) }
        if (ActiveFacts::Metamodel::SetExclusionConstraint === c)
          j[:is_mandatory] = c.is_mandatory
        end

      when ActiveFacts::Metamodel::SubsetConstraint
        rss = [c.subset_role_sequence, c.superset_role_sequence].
          map{|rs| rs.all_role_ref_in_order.map(&:role) }

      when ActiveFacts::Metamodel::ValueConstraint
        next nil  # REVISIT: Should have been handled elsewhere
        if (c.role)
          # Should have been handled as role.role_value_constraint
        elsif (c.value_type)
          # Should have been handled as object_type.value_constraint
        end
        j[:allowed_ranges] = c.all_allowed_range.map{|ar|
          [ ar.value_range.minimum_bound, ar.value_range.maximum_bound ].
            map{|b| [b.value.literal, b.value.unit.name, b.is_inclusive] }
        }

      else
        raise "REVISIT: Constraint type not yet dumped to JSON"
      end

      # rss contains the constrained role sequences; map to uuids
      j[:role_sequences] = rss.map{|rs|
        rs.map do |role|
          uuids[role]
        end
      }

      "    #{j.to_json}"
    end.compact*",\n"
  }\n  ]"

  puts "}"
end

#role_order(uuids, roles, order) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/activefacts/generate/json.rb', line 314

def role_order(uuids, roles, order)
  if (roles.size > 9)
    roles.map{|r| uuids[r] }
  else
    roles.map{|r| order.index(r).to_s }*''
  end
end

#uuid_from_id(o) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/activefacts/generate/json.rb', line 322

def uuid_from_id o
  irvs = o.identifying_role_values.inspect
  d = Digest::SHA1.digest irvs
  # $stderr.puts "#{o.class.basename}: #{irvs}"
  d[0,4].unpack("H8")[0]+'-'+
    d[4,2].unpack("H4")[0]+'-'+
    d[6,2].unpack("H4")[0]+'-'+
    d[8,2].unpack("H4")[0]+'-'+
    d[10,6].unpack("H6")[0]
end