Class: ActiveFacts::Generate::HTML::GLOSSARY

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(vocabulary, *options) ⇒ GLOSSARY

Base class for generators of object-oriented class libraries for an ActiveFacts vocabulary.



15
16
17
18
19
# File 'lib/activefacts/generate/html/glossary.rb', line 15

def initialize(vocabulary, *options)
  @vocabulary = vocabulary
  @vocabulary = @vocabulary.Vocabulary.values[0] if ActiveFacts::API::Constellation === @vocabulary
  options.each{|option| set_option(option) }
end

Instance Method Details

#div(text, klass = nil) ⇒ Object



151
152
153
# File 'lib/activefacts/generate/html/glossary.rb', line 151

def div(text, klass = nil)
  element(text, klass ? {:class => klass} : {}, 'div')
end

#dl(text, klass = nil) ⇒ Object



159
160
161
# File 'lib/activefacts/generate/html/glossary.rb', line 159

def dl(text, klass = nil)
  element(text, klass ? {:class => klass} : {}, 'dl')
end

#element(text, attrs, tag = 'span') ⇒ Object



143
144
145
# File 'lib/activefacts/generate/html/glossary.rb', line 143

def element(text, attrs, tag = 'span')
  "<#{tag}#{attrs.empty? ? '' : attrs.map{|k,v| " #{k}='#{v}'"}*''}>#{text}</#{tag}>"
end

#entities(o) ⇒ Object



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
# File 'lib/activefacts/generate/html/glossary.rb', line 392

def entities(o)
  return if o.preferred_identifier.role_sequence.all_role_ref.size > 1 # REVISIT: Composite identification
  o.all_instance.each do |i|
    v = i.value
    ii = i    # The identifying instance

    until v
      pi = ii.object_type.preferred_identifier		  # ii is an Entity Type
      break if pi.role_sequence.all_role_ref.size > 1	  # REVISIT: Composite identification

      identifying_fact_type = pi.role_sequence.all_role_ref.single.role.fact_type
      # Find the role played by this instance through which it is identified:
      irv = i.all_role_value.detect{|rv| rv.fact.fact_type == identifying_fact_type }
      # Get the other RoleValue in what must be a binary fact type:
      orv = irv.fact.all_role_value.detect{|rv| rv != irv}
      ii = orv.instance
      v = ii.value    # Does this instance have a value? If so, we're done.
    end

    next unless v
    puts div(
	(i.population.name.empty? ? '' : i.population.name+': ') +
	termref(o.name) + ' ' +
	div(
	# v.is_literal_string ? v.literal.inspect : v.literal,
	  v.literal.inspect,
	  'value'),
	'glossary-example'
      )
  end
end

#entity_type_dump(o) ⇒ Object



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
# File 'lib/activefacts/generate/html/glossary.rb', line 358

def entity_type_dump(o)
  pi = o.preferred_identifier
  supers = o.supertypes
  if (supers.size > 0) # Ignore identification by a supertype:
    pi = nil if pi && pi.role_sequence.all_role_ref.detect{|rr| rr.role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) }
  end

  puts "  <dt>" +
    "#{termdef(o.name)} " +
    [
      (supers.size > 0 ? "#{span('is a kind of', 'keyword')} #{supers.map{|s| termref(s.name)}*', '}" : nil),
      (if pi
		"#{span('is identified by', 'keyword')} " +
		pi.role_sequence.all_role_ref_in_order.map do |rr|
		  termref(
		    rr.role.object_type.name,
		    [ rr.leading_adjective,
rr.role.role_name || rr.role.object_type.name,
rr.trailing_adjective
		    ].compact*'-'
		  )
		end*", "
	      else
		nil
	      end)
    ].compact*', '
    "</dt>"

  puts "  <dd>"
  relevant_facts_and_constraints(o)
	  entities(o)
  puts "  </dd>"
end

#expand_reading(reading, include_rolenames = true) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/activefacts/generate/html/glossary.rb', line 258

def expand_reading(reading, include_rolenames = true)
  element(
    reading.expand([], include_rolenames) do |rr, freq_con, l_adj, name, t_adj, role_name_def, literal|
	      if role_name_def
		role_name_def = role_name_def.gsub(/\(as ([^)]+)\)/) {
		  span("(as #{ termref(rr.role.object_type.name, $1) })", 'keyword')
		}
	      end
      role_ref rr, freq_con, l_adj, name, t_adj, role_name_def, literal
    end,
    {:class => 'copula'}
  )
end

#fact_type(ft, include_alternates = true, wrt = nil, include_rolenames = true) ⇒ Object



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
# File 'lib/activefacts/generate/html/glossary.rb', line 276

def fact_type(ft, include_alternates = true, wrt = nil, include_rolenames = true)
  role = ft.all_role.detect{|r| r.object_type == wrt}
  preferred_reading = ft.reading_preferably_starting_with_role(role)
  alternate_readings = ft.all_reading.reject{|r| r == preferred_reading}

	  div(
	    expand_reading(preferred_reading, include_rolenames),
	    'glossary-reading'
	  )+
	  (if include_alternates and alternate_readings.size > 0
	    div(
	      "(alternatively: " +
	      alternate_readings.map do |reading|
		div(
		  expand_reading(reading, include_rolenames),
		  'glossary-reading'
		)
	      end*",\n"+')',
	      'glossary-alternates'
	    )
	  else
	    ''
	  end
	  )
end

#fact_type_block(ft, include_alternates = true, wrt = nil, include_rolenames = true) ⇒ Object



272
273
274
# File 'lib/activefacts/generate/html/glossary.rb', line 272

def fact_type_block(ft, include_alternates = true, wrt = nil, include_rolenames = true)
  div(fact_type(ft, include_alternates, wrt, include_rolenames), 'glossary-facttype')
end

#fact_type_constraints(ft) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/activefacts/generate/html/glossary.rb', line 322

def fact_type_constraints(ft)
  ft.internal_presence_constraints.map do |pc|
    residual_role = ft.all_role.detect{|r| !pc.role_sequence.all_role_ref.detect{|rr| rr.role == r}}
    next nil unless residual_role
    reading = ft.all_reading.detect{|reading|
        reading.role_sequence.all_role_ref_in_order[reading.role_numbers[-1]].role == residual_role
      }
    next nil unless reading
	    div(
	      element(
		reading.expand_with_final_presence_constraint { |*a| role_ref(*a) },
		{:class => 'copula'}
	      ),
	      'glossary-constraint'
	    )+"\n"
  end.compact*''
end

#fact_type_with_constraints(ft, wrt = nil) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/activefacts/generate/html/glossary.rb', line 302

def fact_type_with_constraints(ft, wrt = nil)
	  if ft.entity_type
	    div(
	      termref(ft.entity_type.name) +
		div(' is where ', 'keyword') +
		fact_type(ft, true, wrt),
	      'glossary-objectification'
	    )
	  else
	    fact_type_block(ft, true, wrt)
	  end +
    %Q{\n<ul class="glossary-constraints">\n}+
	    (unless ft.is_a?(ActiveFacts::Metamodel::TypeInheritance)
	      fact_type_constraints(ft)
	    else
	      ''
	    end) +
    "</ul>"
end

#generate(out = $>) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/activefacts/generate/html/glossary.rb', line 32

def generate(out = $>)
  @out = out
  vocabulary_start

  object_types_dump()

  vocabulary_end
end

#h1(text, klass = nil) ⇒ Object



155
156
157
# File 'lib/activefacts/generate/html/glossary.rb', line 155

def h1(text, klass = nil)
  element(text, klass ? {:class => klass} : {}, 'h1')
end

#object_types_dumpObject



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
# File 'lib/activefacts/generate/html/glossary.rb', line 95

def object_types_dump
  all_object_type =
    @vocabulary.
      all_object_type.
      sort_by{|o| o.name.gsub(/ /,'').downcase}

  # Put out a table of contents first:
  puts '<div class="glossary-sidebar">'
  puts '<ol class="glossary-toc">'
  all_object_type.
  reject do |o|
    o.name == '_ImplicitBooleanValueType' or
    o.kind_of?(ActiveFacts::Metamodel::ValueType) && o.all_role.size == 0 or
    o.kind_of?(ActiveFacts::Metamodel::TypeInheritance)
  end.
    each do |o|
      puts "<li>#{termref(o.name)}</li>"
    end
  puts '</ol>'
  puts '<div class="glossary-controls">'
  puts '  <input type="button" onclick="toggle_constraints()" value="Constraints" class="glossary-toggle-constraint">'
  puts '  <input type="button" onclick="toggle_alternates()" value="Alternates" class="glossary-toggle-alternates">'
  puts '  <input type="button" onclick="toggle_examples()" value="Examples" class="glossary-toggle-examples">'
  puts '</div>'
  puts '</div>'

  puts '<div class="glossary-doc hide-alternates hide-constraints" id="glossary-doc">'
    puts "<h1>#{@vocabulary.name}</h1>"
    puts '<dl>'
    all_object_type.
      each do |o|
	case o
	when ActiveFacts::Metamodel::TypeInheritance
	  nil
	when ActiveFacts::Metamodel::ValueType
	  value_type_dump(o)
	else
	  if o.fact_type
	    objectified_fact_type_dump(o)
	  else
	    entity_type_dump(o)
	  end
	end
      end
    puts '</dl>'
  puts '</div>'
end

#objectified_fact_type_dump(o) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/activefacts/generate/html/glossary.rb', line 340

def objectified_fact_type_dump(o)
  puts "  <dt>" +
    "#{termdef(o.name)}" +
    # " (#{span('in which', 'keyword')} #{fact_type(o.fact_type, false, nil, nil)})" +
    "</dt>"
  # REVISIT: Handle separate identification

  puts "  <dd>"
	  puts fact_type_with_constraints(o.fact_type)

  o.fact_type.all_role_in_order.each do |r|
    n = r.object_type.name
    puts div("#{termref(o.name)} involves #{span('one', 'keyword')} #{termref(r.role_name || n, n)}", "glossary-facttype")
  end
  relevant_facts_and_constraints(o)
  puts "  </dd>"
end


28
29
30
# File 'lib/activefacts/generate/html/glossary.rb', line 28

def print(*a)
  @out.print *a
end

#puts(*a) ⇒ Object



24
25
26
# File 'lib/activefacts/generate/html/glossary.rb', line 24

def puts(*a)
  @out.puts *a
end

#relevant_facts_and_constraints(o) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/activefacts/generate/html/glossary.rb', line 230

def relevant_facts_and_constraints(o)
  puts(
    o.
      all_role.
      map{|r| r.fact_type}.
      uniq.
      reject do |ft|
		ft.is_a?(ActiveFacts::Metamodel::LinkFactType)
	      end.
      map { |ft| [ft, "    #{fact_type_with_constraints(ft, o)}"] }.
      sort_by{|ft, text|
		[ ft.is_a?(ActiveFacts::Metamodel::TypeInheritance) ? 0 : 1, text]
	      }.
	      map{|ft, text| text}.
	      join "\n"
  )
end

#role_ref(rr, freq_con, l_adj, name, t_adj, role_name_def, literal) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/activefacts/generate/html/glossary.rb', line 248

def role_ref(rr, freq_con, l_adj, name, t_adj, role_name_def, literal)
  term_parts = [l_adj, termref(name), t_adj].compact
  [
    freq_con ? element(freq_con, :class=>:keyword) : nil,
    term_parts.size > 1 ? term([l_adj, termref(name), t_adj].compact*' ') : term_parts[0],
    role_name_def,
    literal
  ]
end

#set_option(option) ⇒ Object



21
22
# File 'lib/activefacts/generate/html/glossary.rb', line 21

def set_option(option)
end

#span(text, klass = nil) ⇒ Object



147
148
149
# File 'lib/activefacts/generate/html/glossary.rb', line 147

def span(text, klass = nil)
  element(text, klass ? {:class => klass} : {})
end

#term(name) ⇒ Object

Text that should appear as part of a term (including role adjectives)



175
176
177
# File 'lib/activefacts/generate/html/glossary.rb', line 175

def term(name)
  element(name, :class=>:object_type)
end

#termdef(name) ⇒ Object

A definition of a term



164
165
166
# File 'lib/activefacts/generate/html/glossary.rb', line 164

def termdef(name)
  element(name, {:name => name, :class => 'object_type'}, 'a')
end

#termref(name, role_name = nil) ⇒ Object

A reference to a defined term (excluding role adjectives)



169
170
171
172
# File 'lib/activefacts/generate/html/glossary.rb', line 169

def termref(name, role_name = nil)
  role_name ||= name
  element(role_name, {:href=>'#'+name, :class=>:object_type}, 'a')
end

#value_sub_types(o) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/activefacts/generate/html/glossary.rb', line 198

def value_sub_types(o)
  o.
    all_value_type_as_supertype.    # All value types for which o is a supertype
    sort_by{|sub| sub.name}.
    each do |sub|
      puts div(
	"#{termref(sub.name)} #{span('is written as', 'keyword')} #{termref(o.name)}",
	'glossary-facttype'
      )
    end
end

#value_type_dump(o) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/activefacts/generate/html/glossary.rb', line 179

def value_type_dump(o)
  return if o.all_role.size == 0 or  # Skip value types that are only used as supertypes
	    o.name == '_ImplicitBooleanValueType'
  puts "  <dt>" +
    "#{termdef(o.name)} " +
	    (if o.supertype
	      span('is written as ', :keyword) + termref(o.supertype.name)
	    else
	      " (a fundamental data type)"
	    end) +
    "</dt>"

  puts "  <dd>"
	  value_sub_types(o)
  relevant_facts_and_constraints(o)
	  values(o)
  puts "  </dd>"
end

#values(o) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/activefacts/generate/html/glossary.rb', line 210

def values(o)
  o.all_instance.
  sort_by{|i|
    [i.population.name, i.value.literal]
  }.
  each do |i|
    v = i.value
    puts div(
	(i.population.name.empty? ? '' : i.population.name+': ') +
	termref(o.name) + ' ' +
	div(
	# v.is_literal_string ? v.literal.inspect : v.literal,
	  v.literal.inspect,
	  'value'
	),
	'glossary-example'
      )
  end
end

#vocabulary_endObject



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
# File 'lib/activefacts/generate/html/glossary.rb', line 67

def vocabulary_end
	  puts %Q{
	  <script type="text/javascript">
	  function toggle_class(e, c) {
	    if (!e) return;
	    var n = e.className;
	    var i = n.indexOf(c);
	    if (i == -1) {
	      e.className = n+' '+c;
	    } else {
	      e.className = n.slice(0, i)+n.slice(i+c.length);
	    }
	    if (document.location.toString().indexOf('#') >= 0)
	      document.location = document.location; // Re-scroll to the current fragment
	  }
	  function toggle_constraints() {
	    toggle_class(document.getElementById('glossary-doc'), 'hide-constraints');
	  }
	  function toggle_alternates() {
	    toggle_class(document.getElementById('glossary-doc'), 'hide-alternates');
	  }
	  function toggle_examples() {
	    toggle_class(document.getElementById('glossary-doc'), 'hide-examples');
	  }
	  </script>
	  }.gsub(/^\s+/, '')
end

#vocabulary_startObject



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
# File 'lib/activefacts/generate/html/glossary.rb', line 41

def vocabulary_start
  # puts "<link rel='stylesheet' href='css/orm2.css' media='screen' type='text/css'/>"
	  File.open(File.dirname(__FILE__)+"/../../../../css/orm2.css") do |f|
	    puts "<style media='screen' type='text/css'>"
	    puts f.read
	    puts %Q{
	      .glossary-facttype, .glossary-constraints { display: block; }
	      .glossary-doc.hide-alternates .glossary-alternates { display: none; }
	      .glossary-doc.hide-constraints .glossary-constraints { display: none; }
	      .glossary-doc.hide-examples .glossary-example { display: none; }
	    }.gsub(/^\s+/, '')
	    puts "</style>"
	  end

	  puts %Q{
	    <style media='print' type='text/css'>
	    .keyword { color: #0000CC; font-style: italic; display: inline; }
	    .vocabulary, .object_type { color: #8A0092; font-weight: bold; }
	    .copula { color: #0E5400; }
	    .value { color: #FF990E; display: inline; }
	    .glossary-toc { display: none; }
	    .glossary-facttype, .glossary-reading { display: inline; }
	    </style>
	  }.gsub(/^\s+/, '')
end