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



183
184
185
# File 'lib/activefacts/generate/html/glossary.rb', line 183

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

#dl(text, klass = nil) ⇒ Object



191
192
193
# File 'lib/activefacts/generate/html/glossary.rb', line 191

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

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



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

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

#entities(o) ⇒ Object



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

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



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

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



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/activefacts/generate/html/glossary.rb', line 290

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



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

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



304
305
306
# File 'lib/activefacts/generate/html/glossary.rb', line 304

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



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/activefacts/generate/html/glossary.rb', line 354

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/activefacts/generate/html/glossary.rb', line 334

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/activefacts/generate/html/glossary.rb', line 42

def generate(out = $>)
  @out = out
  
 @all_object_type =
   @vocabulary.
     all_object_type.
     sort_by{|o| o.name.gsub(/ /,'').downcase}
  
  vocabulary_start

  if @gen_bootstrap
    object_types_dump_toc()
    object_types_dump_def()
  else
    object_types_dump_def()
    object_types_dump_toc()
  end
  vocabulary_end
end

#h1(text, klass = nil) ⇒ Object



187
188
189
# File 'lib/activefacts/generate/html/glossary.rb', line 187

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

#object_types_dump_defObject



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

def object_types_dump_def
  if @gen_bootstrap
     puts '<div class="col-md-5 glossary-doc hide-alternates hide-constraints" id="glossary-doc">'
   else
     puts '<div class="glossary-doc hide-alternates hide-constraints" id="glossary-doc">'
   end
  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

#object_types_dump_tocObject



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

def object_types_dump_toc
 if @gen_bootstrap
    puts '<div class="col-md-3 glossary-sidebar">'
  else
	  puts '<div class="glossary-sidebar">'
  end
 puts '<h1 style="visibility: hidden">X</h1>'
  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>'
end

#objectified_fact_type_dump(o) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/activefacts/generate/html/glossary.rb', line 372

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


38
39
40
# File 'lib/activefacts/generate/html/glossary.rb', line 38

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

#puts(*a) ⇒ Object



34
35
36
# File 'lib/activefacts/generate/html/glossary.rb', line 34

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

#relevant_facts_and_constraints(o) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/activefacts/generate/html/glossary.rb', line 262

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



280
281
282
283
284
285
286
287
288
# File 'lib/activefacts/generate/html/glossary.rb', line 280

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
23
24
25
26
27
28
29
30
31
32
# File 'lib/activefacts/generate/html/glossary.rb', line 21

def set_option(option)
  @gen_bootstrap = false
  case option
  when 'help', '?'
    $stderr.puts "Usage:\t\tafgen --html/glossary[=option] input_file.cql\n"+
        "\t\tbootstrap\tGenerate bootstrap styled glossary html"
    exit 0
  when /bootstrap/
    @gen_bootstrap = true
  else super
  end
end

#span(text, klass = nil) ⇒ Object



179
180
181
# File 'lib/activefacts/generate/html/glossary.rb', line 179

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)



207
208
209
# File 'lib/activefacts/generate/html/glossary.rb', line 207

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

#termdef(name) ⇒ Object

A definition of a term



196
197
198
# File 'lib/activefacts/generate/html/glossary.rb', line 196

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)



201
202
203
204
# File 'lib/activefacts/generate/html/glossary.rb', line 201

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

#value_sub_types(o) ⇒ Object



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

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



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 211

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/activefacts/generate/html/glossary.rb', line 242

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



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

def vocabulary_end
  if !@gen_bootstrap
	  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
end

#vocabulary_startObject



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

def vocabulary_start
  if !@gen_bootstrap
    # puts "<link rel='stylesheet' href='css/orm2.css' media='screen' type='text/css'/>"
    css_file = "/../../../../css/orm2.css"
  
	  File.open(File.dirname(__FILE__)+css_file) 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
end