Class: ActiveFacts::Metamodel::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/vocabulary/metamodel.rb,
lib/activefacts/vocabulary/extensions.rb

Instance Method Summary collapse

Instance Method Details

#verbalise(context = nil) ⇒ Object



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/activefacts/vocabulary/extensions.rb', line 1022

def verbalise(context = nil)
  return "#{object_type.name} #{value}" if object_type.is_a?(ValueType)

  return "#{object_type.name} where #{fact.verbalise(context)}" if object_type.fact_type

  # It's an entity that's not an objectified fact type
  # REVISIT: If it has a simple identifier, there's no need to fully verbalise the identifying facts
  pi = object_type.preferred_identifier
  identifying_role_refs = pi.role_sequence.all_role_ref_in_order
  "#{object_type.name}" +
    " is identified by " +      # REVISIT: Where the single fact type is TypeInheritance, we can shrink this
    if identifying_role_refs.size == 1 &&
      (role = identifying_role_refs[0].role) &&
      (my_role = (role.fact_type.all_role.to_a-[role])[0]) &&
      (identifying_fact = my_role.all_role_value.detect{|rv| rv.instance == self}.fact) &&
      (identifying_instance = identifying_fact.all_role_value.detect{|rv| rv.role == role}.instance)

        "its #{identifying_instance.verbalise(context)}"
    else
      identifying_role_refs.map do |rr|
        rr = rr.preferred_reference
        [ (l = rr.leading_adjective) ? l+"-" : nil,
          rr.role.role_name || rr.role.object_type.name,
          (t = rr.trailing_adjective) ? l+"-" : nil
        ].compact*""
      end * " and " +
      " where " +
      identifying_role_refs.map do |rr|  # Go through the identifying roles and emit the facts that define them
        instance_role = object_type.all_role.detect{|r| r.fact_type == rr.role.fact_type}
        identifying_fact = all_role_value.detect{|rv| rv.fact.fact_type == rr.role.fact_type}.fact
        #counterpart_role = (rr.role.fact_type.all_role.to_a-[instance_role])[0]
        #identifying_instance = counterpart_role.all_role_value.detect{|rv| rv.fact == identifying_fact}.instance
        identifying_fact.verbalise(context)
      end*", "
    end

end