Class: ActiveFacts::Generate::Helpers::OO

Inherits:
OrderedDumper show all
Defined in:
lib/activefacts/generate/helpers/oo.rb

Overview

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

Direct Known Subclasses

DM, RUBY

Instance Method Summary collapse

Methods inherited from OrderedDumper

#build_entity_dependencies, #build_indices, #constraint_sort_key, #data_type_dump, #derivation_precursors_complete, #describe_fact_type, #describe_role_sequence, #describe_roles, #entity_types_dump, #fact_instances_dump, #fact_type_dump, #fact_type_dump_with_dependents, #fact_type_key, #fact_types_dump, #generate, #identified_by, #initialize, #non_subtype_dump, #print, #puts, #released_fact_types_dump, #role_ref_key, #set_option, #subtype_dump, #unit_dump, #units_banner, #units_dump, #units_end, #value_type_chain_dump, #value_type_dump, #value_type_fork, #value_types_dump, #vocabulary_end, #vocabulary_start

Constructor Details

This class inherits a constructor from ActiveFacts::Generate::Helpers::OrderedDumper

Instance Method Details

#append_ring_to_reading(reading, ring) ⇒ Object



174
175
176
# File 'lib/activefacts/generate/helpers/oo.rb', line 174

def append_ring_to_reading(reading, ring)
  # REVISIT: debug "Should override append_ring_to_reading"
end

#constraint_bannerObject



184
185
186
# File 'lib/activefacts/generate/helpers/oo.rb', line 184

def constraint_banner
  # debug "Should override constraint_banner"
end

#constraint_dump(c) ⇒ Object



192
193
194
# File 'lib/activefacts/generate/helpers/oo.rb', line 192

def constraint_dump(c)
  # debug "Should override constraint_dump"
end

#constraint_endObject



188
189
190
# File 'lib/activefacts/generate/helpers/oo.rb', line 188

def constraint_end
  # debug "Should override constraint_end"
end

#constraints_dump(constraints_used) ⇒ Object

:nodoc:



15
16
17
# File 'lib/activefacts/generate/helpers/oo.rb', line 15

def constraints_dump(constraints_used)
  # Stub, not needed.
end

#entity_type_bannerObject



168
169
# File 'lib/activefacts/generate/helpers/oo.rb', line 168

def entity_type_banner
end

#entity_type_dump(o) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activefacts/generate/helpers/oo.rb', line 25

def entity_type_dump(o)
  @object_types_dumped[o] = true
  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) }
    subtype_dump(o, supers, pi)
  else
    non_subtype_dump(o, pi)
  end
  @constraints_used[pi] = true
end

#entity_type_group_endObject



171
172
# File 'lib/activefacts/generate/helpers/oo.rb', line 171

def entity_type_group_end
end

#fact_roles_dump(fact_type) ⇒ Object

An objectified fact type has internal roles that are always “has_one”:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/activefacts/generate/helpers/oo.rb', line 148

def fact_roles_dump(fact_type)
  fact_type.all_role.sort_by{|role|
      preferred_role_name(role, fact_type.entity_type)
    }.each{|role| 
      role_name = preferred_role_name(role, fact_type.entity_type)
      one_to_one = role.all_role_ref.detect{|rr|
        rr.role_sequence.all_role_ref.size == 1 &&
        rr.role_sequence.all_presence_constraint.detect{|pc|
          pc.max_frequency == 1
        }
      }
      as = role_name != role.object_type.name.gsub(/ /,'_').snakecase ? "_as_#{role_name}" : ""
      raise "Fact #{fact_type.describe} type is not objectified" unless fact_type.entity_type
      other_role_method = (one_to_one ? "" : "all_") + 
        fact_type.entity_type.name.gsub(/ /,'_').snakecase +
        as
      binary_dump(role, role_name, role.object_type, true, one_to_one, nil, nil, other_role_method)
    }
end

#fact_type_bannerObject



178
179
# File 'lib/activefacts/generate/helpers/oo.rb', line 178

def fact_type_banner
end

#fact_type_endObject



181
182
# File 'lib/activefacts/generate/helpers/oo.rb', line 181

def fact_type_end
end

#preferred_role_name(role, is_for = nil, &b) ⇒ Object



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
# File 'lib/activefacts/generate/helpers/oo.rb', line 100

def preferred_role_name(role, is_for = nil, &b)
	  b ||= proc {|names| names.map(&:downcase)*'_' }   # Make snake_case by default
  return b.call([]) if role.fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)

  if is_for && role.fact_type.entity_type == is_for && role.fact_type.all_role.size == 1
    return b.call(role.object_type.name.gsub(/[- ]/,'_').split(/_/))
  end

  # debug "Looking for preferred_role_name of #{describe_fact_type(role.fact_type, role)}"
  reading = role.fact_type.preferred_reading
  preferred_role_ref = reading.role_sequence.all_role_ref.detect{|reading_rr|
      reading_rr.role == role
    }

  # Unaries are a hack, with only one role for what is effectively a binary:
  if (role.fact_type.all_role.size == 1)
    return b.call(
	      ( (role.role_name && role.role_name.snakecase) ||
		reading.text.gsub(/ *\{0\} */,'').gsub(/[- ]/,'_')
	      ).split(/_/)
	    )
  end

  # debug "\tleading_adjective=#{(p=preferred_role_ref).leading_adjective}, role_name=#{role.role_name}, role player=#{role.object_type.name}, trailing_adjective=#{p.trailing_adjective}"
  role_words = []
  role_name = role.role_name
  role_name = nil if role_name == ""

  # REVISIT: Consider whether NOT to use the adjective if it's a prefix of the role_name
  la = preferred_role_ref.leading_adjective
  role_words << la.gsub(/[- ]/,'_') if la && la != "" and !role.role_name

  role_words << (role_name || role.object_type.name.gsub(/[- ]/,'_'))
  # REVISIT: Same when trailing_adjective is a suffix of the role_name
  ta = preferred_role_ref.trailing_adjective
  role_words << ta.gsub(/[- ]/,'_') if ta && ta != "" and !role_name
  n = role_words.map{|w| w.gsub(/([a-z])([A-Z]+)/,'\1_\2').downcase}*"_"
  # debug "\tresult=#{n}"
  return b.call(n.gsub(' ','_').split(/_/))
end

#role_dump(role) ⇒ Object



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
# File 'lib/activefacts/generate/helpers/oo.rb', line 54

def role_dump(role)
  return if role.fact_type.entity_type

  fact_type = role.fact_type
  if fact_type.all_role.size == 1
    unary_dump(role, preferred_role_name(role))
    return
  elsif fact_type.all_role.size != 2
    # Shouldn't come here, except perhaps for an invalid model
    return  # ternaries and higher are always objectified
  end

  # REVISIT: TypeInheritance
  if fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
    # debug "Ignoring role #{role} in #{fact_type}, subtype fact type"
    # REVISIT: What about secondary subtypes?
    # REVISIT: What about dumping the relational mapping when using separate tables?
    return
  end

  return unless role.is_functional

  other_role = fact_type.all_role.select{|r| r != role}[0]
  other_role_name = preferred_role_name(other_role)
  other_player = other_role.object_type

  # It's a one_to_one if there's a uniqueness constraint on the other role:
  one_to_one = other_role.is_functional
  return if one_to_one &&
      !@object_types_dumped[other_role.object_type]

  # Find role name:
  role_method = preferred_role_name(role)
  other_role_method = one_to_one ? role_method : "all_"+role_method
  # puts "---"+role.role_name if role.role_name
  if other_role_name != other_player.name.gsub(/ /,'_').snakecase and
    role_method == role.object_type.name.gsub(/ /,'_').snakecase
    other_role_method += "_as_#{other_role_name}"
  end

  role_name = role_method
  role_name = nil if role_name == role.object_type.name.gsub(/ /,'_').snakecase

  binary_dump(role, other_role_name, other_player, role.is_mandatory, one_to_one, nil, role_name, other_role_method)
end

#roles_dump(o) ⇒ Object

Dump the roles for an object type (excluding the roles of a fact type which is objectified)



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/activefacts/generate/helpers/oo.rb', line 41

def roles_dump(o)
  o.all_role.
    select{|role|
      role.fact_type.all_role.size <= 2 &&
        !role.fact_type.is_a?(ActiveFacts::Metamodel::LinkFactType)
    }.
    sort_by{|role|
      preferred_role_name(role.fact_type.all_role.select{|r2| r2 != role}[0] || role, o)
    }.each{|role| 
      role_dump(role)
    }
end

#skip_fact_type(f) ⇒ Object



141
142
143
144
145
# File 'lib/activefacts/generate/helpers/oo.rb', line 141

def skip_fact_type(f)
  # REVISIT: There might be constraints we have to merge into the nested entity or subtype.  These will come up as un-handled constraints.
  !f.entity_type ||
    f.is_a?(ActiveFacts::Metamodel::TypeInheritance)
end

#value_type_bannerObject



19
20
# File 'lib/activefacts/generate/helpers/oo.rb', line 19

def value_type_banner
end

#value_type_endObject



22
23
# File 'lib/activefacts/generate/helpers/oo.rb', line 22

def value_type_end
end