lib/activefacts/generators/cql.rb in activefacts-generators-1.8.3 vs lib/activefacts/generators/cql.rb in activefacts-generators-1.9.0

- old
+ new

@@ -16,22 +16,22 @@ # afgen --cql <file>.cql class CQL < Helpers::OrderedDumper private def vocabulary_start puts "vocabulary #{@vocabulary.name};\n\n" - build_indices + build_indices end def vocabulary_end end def units_banner puts "/*\n * Units\n */" end def unit_dump unit - puts unit.as_cql + puts unit.as_cql end def units_end puts "\n" end @@ -43,35 +43,35 @@ def value_type_end puts "\n" end def data_type_dump(o) - value_type_dump(o, o.name, {}) if o.all_role.size > 0 + value_type_dump(o, o.name, {}) if o.all_role.size > 0 end def value_type_dump(o, super_type_name, facets) # No need to dump it if the only thing it does is be a supertype; it'll be created automatically # return if o.all_value_type_as_supertype.size == 0 # REVISIT: A ValueType that is only used as a reference mode need not be emitted here. - puts o.as_cql + puts o.as_cql end def entity_type_dump(o) - o.ordered_dumped! - pi = o.preferred_identifier + o.ordered_dumped! + 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 - pi.ordered_dumped! if pi + 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 + pi.ordered_dumped! if pi end def append_ring_to_reading(reading, ring) reading << " [#{(ring.ring_type.scan(/StronglyIntransitive|[A-Z][a-z]*/)*", ").downcase}]" end @@ -191,13 +191,13 @@ if nonstandard_readings.size == 0 and c = value_role.role_value_constraint constraint_text = " "+c.as_cql end (entity_type.is_independent ? ' independent' : '') + " identified by its #{value_residual}#{constraint_text}#{mapping_pragma(entity_type, true)}" + - entity_type.concept.all_context_note_as_relevant_concept.map do |cn| - cn.verbalise - end.join("\n") + + entity_type.concept.all_context_note_as_relevant_concept.map do |cn| + cn.verbalise + end.join("\n") + (fact_readings.size > 0 ? " where\n\t" : "") + fact_readings*",\n\t" end def identified_by_roles_and_facts(entity_type, identifying_role_refs, identifying_facts) @@ -228,13 +228,13 @@ }.flatten*",\n\t" (entity_type.is_independent ? ' independent' : '') + " identified by #{ irn*" and " }" + mapping_pragma(entity_type, true) + - entity_type.concept.all_context_note_as_relevant_concept.map do |cn| - cn.verbalise - end.join("\n") + + entity_type.concept.all_context_note_as_relevant_concept.map do |cn| + cn.verbalise + end.join("\n") + " where\n\t"+identifying_fact_text end def entity_type_banner puts "/*\n * Entity Types\n */" @@ -292,12 +292,12 @@ end # Alternate identification of objectified fact type? primary_supertype = supertypes[0] if fact_type.all_role.size > 1 and - pi = fact_type.entity_type.preferred_identifier and - primary_supertype && primary_supertype.preferred_identifier != pi + pi = fact_type.entity_type.preferred_identifier and + primary_supertype && primary_supertype.preferred_identifier != pi puts identified_by(o, pi) + ';' return end print " where\n\t" end @@ -367,11 +367,11 @@ # For all other presence constraints any subtyping step is over the counterpart player role_proximity = c.min_frequency == 1 && [nil, 1].include?(c.max_frequency) ? :proximate : :counterpart if role_proximity == :proximate verbaliser.role_refs_have_subtype_steps(c.role_sequence) else - roles = c.role_sequence.all_role_ref.map{|rr|rr.role} + roles = c.role_sequence.all_role_ref.map{|rr|rr.role} join_over, joined_roles = ActiveFacts::Metamodel.plays_over(roles, role_proximity) verbaliser.roles_have_same_player(joined_roles) if join_over end verbaliser.prepare_role_sequence(c.role_sequence, join_over) @@ -512,22 +512,22 @@ # At present, no ring constraint can be missed to be handled in this pass puts "// #{c.ring_type} ring over #{c.role.fact_type.default_reading}" end def constraint_dump(c) - case c - when ActiveFacts::Metamodel::PresenceConstraint - dump_presence_constraint(c) - when ActiveFacts::Metamodel::RingConstraint - dump_ring_constraint(c) - when ActiveFacts::Metamodel::SetComparisonConstraint # includes SetExclusionConstraint, SetEqualityConstraint - dump_set_comparison_constraint(c) - when ActiveFacts::Metamodel::SubsetConstraint - dump_subset_constraint(c) - else - "#{c.class.basename} #{c.name}: unhandled constraint type" - end + case c + when ActiveFacts::Metamodel::PresenceConstraint + dump_presence_constraint(c) + when ActiveFacts::Metamodel::RingConstraint + dump_ring_constraint(c) + when ActiveFacts::Metamodel::SetComparisonConstraint # includes SetExclusionConstraint, SetEqualityConstraint + dump_set_comparison_constraint(c) + when ActiveFacts::Metamodel::SubsetConstraint + dump_subset_constraint(c) + else + "#{c.class.basename} #{c.name}: unhandled constraint type" + end end # Find the common supertype of these object_types. def common_supertype(object_types) common = object_types[0].supertypes_transitive @@ -584,11 +584,11 @@ nil end end expanded = verbaliser.expand_reading(reading, frequency_constraints, define_role_names, value_constraints) - expanded = "it is not the case that "+expanded if (reading.is_negative) + expanded = "it is not the case that "+expanded if (reading.is_negative) if (ft_rings = @ring_constraints_by_fact[reading.fact_type]) && (ring = ft_rings.detect{|rc| !rc.ordered_dumped}) ring.ordered_dumped! append_ring_to_reading(expanded, ring) @@ -606,33 +606,33 @@ player = players[i] if i [ nil, player.name ] end frequency_constraints = [] unless frequency_constraints.detect{|fc| fc[0] != "some" } - expanded = verbaliser.expand_reading(reading, frequency_constraints) - expanded = "it is not the case that "+expanded if (reading.is_negative) - expanded + expanded = verbaliser.expand_reading(reading, frequency_constraints) + expanded = "it is not the case that "+expanded if (reading.is_negative) + expanded end def build_indices - @presence_constraints_by_fact = Hash.new{ |h, k| h[k] = [] } - @ring_constraints_by_fact = Hash.new{ |h, k| h[k] = [] } + @presence_constraints_by_fact = Hash.new{ |h, k| h[k] = [] } + @ring_constraints_by_fact = Hash.new{ |h, k| h[k] = [] } - @vocabulary.all_constraint.each { |c| - case c - when ActiveFacts::Metamodel::PresenceConstraint - fact_types = c.role_sequence.all_role_ref.map{|rr| rr.role.fact_type}.uniq # All fact types spanned by this constraint - if fact_types.size == 1 # There's only one, save it: - # trace "Single-fact constraint on #{fact_types[0].concept.guid}: #{c.name}" - (@presence_constraints_by_fact[fact_types[0]] ||= []) << c - end - when ActiveFacts::Metamodel::RingConstraint - (@ring_constraints_by_fact[c.role.fact_type] ||= []) << c - else - # trace "Found unhandled constraint #{c.class} #{c.name}" - end - } + @vocabulary.all_constraint.each { |c| + case c + when ActiveFacts::Metamodel::PresenceConstraint + fact_types = c.role_sequence.all_role_ref.map{|rr| rr.role.fact_type}.uniq # All fact types spanned by this constraint + if fact_types.size == 1 # There's only one, save it: + # trace "Single-fact constraint on #{fact_types[0].concept.guid}: #{c.name}" + (@presence_constraints_by_fact[fact_types[0]] ||= []) << c + end + when ActiveFacts::Metamodel::RingConstraint + (@ring_constraints_by_fact[c.role.fact_type] ||= []) << c + else + # trace "Found unhandled constraint #{c.class} #{c.name}" + end + } end end end @@ -653,16 +653,16 @@ }#{ parameters }#{ unit && " "+unit.name }#{ - transaction_phase && " auto-assigned at "+transaction_phase + transaction_phase && " auto-assigned at "+transaction_phase }#{ - concept.all_context_note_as_relevant_concept.map do |cn| - cn.verbalise - end.join("\n") - }#{ + concept.all_context_note_as_relevant_concept.map do |cn| + cn.verbalise + end.join("\n") + }#{ value_constraint && " "+value_constraint.describe };" end end @@ -673,41 +673,41 @@ # REVISIT: Use a smarter algorithm to switch to exponential form when there'd be lots of zeroes. coefficient.numerator.to_s('F') + if d = coefficient.denominator and d != 1 "/#{d}" - else - '' + else + '' end + ' ' else '1 ' end - else - '' + else + '' end + - all_derivation_as_derived_unit. - sort_by{|d| d.base_unit.name}. + all_derivation_as_derived_unit. + sort_by{|d| d.base_unit.name}. # REVISIT: Sort base units # REVISIT: convert negative powers to division? map do |der| base = der.base_unit "#{base.name}#{der.exponent and der.exponent != 1 ? "^#{der.exponent}" : ''} " end*'' + if o = offset and o != 0 "+ #{o.to_s('F')} " - else - '' + else + '' end + "converts to #{name}#{plural_name ? '/'+plural_name : ''}" + - (coefficient && !coefficient.is_precise ? ' approximately' : '') + + (coefficient && !coefficient.is_precise ? ' approximately' : '') + - (ephemera_url ? " ephemera #{ephemera_url}" : '') + + (ephemera_url ? " ephemera #{ephemera_url}" : '') + ';' end end end