Class: GenericType

Inherits:
ActiveRecord::Base show all
Includes:
Generic_Table
Defined in:
app/models/generic_type.rb

Overview

1a) a regexp should match all examples from itself down the specialization tree. 1b) an example should match its regexp and all generalization regexps above if 2) an example should not match at least one of its specialization regexps 3) example strings should not equal specialization examples 4) specialization regexps have fewer choices (including case) or more restricted repetition

Constant Summary collapse

Text =

Define some constants, after find_by_name redefinition

find_by_name('Text_Column')
Ascii =
find_by_name('ascii')

Constants included from ColumnGroup::ClassMethods

ColumnGroup::ClassMethods::History_columns

Constants included from GenericGrep::ClassMethods

GenericGrep::ClassMethods::ASSOCIATION_MACRO_LETTERS, GenericGrep::ClassMethods::ASSOCIATION_MACRO_PATTERN

Constants included from GenericTableAssociation::Examples

GenericTableAssociation::Examples::First_stream_method

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Generic_Table

activeRecordTableNotCreatedYet?, classDefiniton, classReference, class_of_name, eval_constant, is_ActiveRecord_table?, is_generic_table?, is_generic_table_name?, is_table?, rails_MVC_class?, rubyClassName, table_exists?

Methods included from Generic_Table::ClassMethods

#is_active_record_method?, #model_file_name, #nesting, #one_pass_statistics, #sample, #sample_burst

Methods included from ColumnGroup::ClassMethods

#attribute_ddl, #attribute_rails_type, #attribute_ruby_type, #candidate_logical_keys_from_indexes, #categorical?, #column_symbols, #default_logical_primary_key, #defaulted_primary_logical_key?, #history_type?, #is_logical_primary_key?, #logical_attributes, #logical_primary_key, #logical_primary_key_recursive, #numerical?, #probably_categorical?, #probably_numerical?, #sequential_id?

Methods included from GenericGrep::ClassMethods

#all_associations, #association_grep, #association_grep_pattern, #association_macro_type, #association_type, #belongs_to_association?, #grep_all_associations_command, #grep_command, #has_many_association?, #has_one_association?, #model_grep, #model_grep_command

Methods included from GenericTableHtml::ClassMethods

#column_order, #header_html, #table_html

Methods included from GenericTableAssociation

#associated_foreign_key_records, #associated_to_s, #association_class, #association_has_data, #association_state, #foreign_key_points_to_me?, #foreign_key_to_association, #foreign_key_value, #logical_primary_key_recursive_value, #logical_primary_key_value, #name_to_association

Methods included from GenericTableAssociation::Assertions::ClassMethods

#assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableAssociation::Assertions

#assert_associated_foreign_key, #assert_associated_foreign_key_name, #assert_association, #assert_association_methods, #assert_association_to_many, #assert_association_to_one, #assert_foreign_key_association_names, #assert_foreign_key_name, #assert_foreign_key_not_nil, #assert_foreign_keys_not_nil, #assert_invariant, #assert_post_conditions, #assert_pre_conditions

Methods included from GenericTableHtml

#column_html, #rails_route, #row_html

Methods included from GenericTableAssociation::ClassMethods

#associated_foreign_key_name, #association_arity, #association_class, #association_default_class_name?, #association_method_plurality, #association_method_symbol, #association_methods, #association_names, #association_names_to_many, #association_names_to_one, #association_patterns, #foreign_key_association_names, #foreign_key_names, #foreign_key_to_association_name, #is_association?, #is_association_patterns?, #is_association_to_many?, #is_association_to_one?, #is_foreign_key_name?, #is_matching_association?, #is_polymorphic_association?, #match_association_patterns?, #name_symbol

Class Method Details

.find_by_name(name) ⇒ Object

require 'test/assertions/ruby_assertions.rb' def initialize(generic_type) end #initialize


24
25
26
# File 'app/models/generic_type.rb', line 24

def GenericType.find_by_name(name)
	return GenericType.find_by_import_class(name)
end

.logical_primary_keyObject


30
31
32
# File 'app/models/generic_type.rb', line 30

def self.logical_primary_key
	return [:import_class]
end

Instance Method Details

#common_matches?(string_to_match) ⇒ Boolean

Recursively search where in the tree a string matches Returns an array of GenericType instances. The last element of the array matched. The receiving object is a GenericType instance used as an starting place in the tree. If the receiving object matched it will be the first element in returned array. If the string doesn't match the receiving object, generalize is returned. If start matches, the returned array will be the ordered array of matching specializations. Calls match_exact? and specializations_that_match? above

Returns:

  • (Boolean)

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/generic_type.rb', line 168

def common_matches?(string_to_match)
	if match_exact?(string_to_match) then
		if unspecialized? then
			return NestedArray.new([self])
		else
			specializations=specializations_that_match?(string_to_match)
			if specializations.empty? then
				NestedArray.new([self])
			else
				NestedArray.new([self] << specializations)
			end #if
		end #if
	else
		generalize.common_matches?(string_to_match)
	end #if
end

#expandObject

expansion_termination


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/generic_type.rb', line 74

def expand
	parse=RegexpTree.new(self[:data_regexp])
	if expansion_termination? then
		return parse[0] # terminates recursion
	else # possible expansions
		parse.map_branches do |branch|
			macro_name=parse.macro_call?(branch)
			if macro_name then
				macro_generic_type=GenericType.find_by_name(macro_name)
				macro_call=macro_generic_type[:data_regexp]
				macro_generic_type.expand
			else
				branch
			end #if
		end #map_branches
	end #if possible expansions
end

#expansion_termination?Boolean

specializations

Returns:

  • (Boolean)

68
69
70
71
72
73
# File 'app/models/generic_type.rb', line 68

def expansion_termination?
	regexp=self[:data_regexp]
	parse=RegexpTree.new(regexp)
	macro_name=parse.macro_call?
	self.name==macro_name
end

#generalizationsObject

name


36
37
38
39
40
41
42
# File 'app/models/generic_type.rb', line 36

def generalizations
	if generalize==self then
		return []
	else
		return generalize.generalizations << generalize
	end #if
end

#match_any?(string_to_match) ⇒ Boolean

Matches expanded regexp anywhere in string Sets EXTENDED format (whitespace and comments) and MULTILINE (newlines are just another character) Calls expand above.

Returns:

  • (Boolean)

115
116
117
118
# File 'app/models/generic_type.rb', line 115

def match_any?(string_to_match)
	regexp=Regexp.new(expand.join,Regexp::EXTENDED | Regexp::MULTILINE)
	return RegexpMatch.match_data?(regexp, string_to_match)
end

#match_end?(string_to_match) ⇒ Boolean

Matches expanded regexp from start of string Sets EXTENDED format (whitespace and comments) and MULTILINE (newlines are just another character) Calls expand above.

Returns:

  • (Boolean)

108
109
110
111
# File 'app/models/generic_type.rb', line 108

def match_end?(string_to_match)
	regexp=Regexp.new(expand.join+'$',Regexp::EXTENDED | Regexp::MULTILINE)
	return RegexpMatch.match_data?(regexp, string_to_match)
end

#match_exact?(string_to_match) ⇒ Boolean

Matches expanded regexp against full string Sets EXTENDED format (whitespace and comments) and MULTILINE (newlines are just another character) Calls expand above.

Returns:

  • (Boolean)

94
95
96
97
# File 'app/models/generic_type.rb', line 94

def match_exact?(string_to_match)
	regexp=Regexp.new('^'+expand.join+'$',Regexp::EXTENDED | Regexp::MULTILINE)
	return RegexpMatch.match_data?(regexp, string_to_match)
end

#match_start?(string_to_match) ⇒ Boolean

Matches string from beginning against expanded Regexp Sets EXTENDED format (whitespace and comments) and MULTILINE (newlines are just another character) Calls expand above.

Returns:

  • (Boolean)

101
102
103
104
# File 'app/models/generic_type.rb', line 101

def match_start?(string_to_match)
	regexp=Regexp.new('^'+expand.join,Regexp::EXTENDED | Regexp::MULTILINE)
	return RegexpMatch.match_data?(regexp, string_to_match)
end

#most_general?Boolean

generalizations

Returns:

  • (Boolean)

43
44
45
# File 'app/models/generic_type.rb', line 43

def most_general?
	return generalize==self || generalize.nil?
end

#most_specialized?(string_to_match, common_matches = common_matches?(string_to_match)) ⇒ Boolean

possibilities

Returns:

  • (Boolean)

153
154
155
156
157
158
159
# File 'app/models/generic_type.rb', line 153

def most_specialized?(string_to_match, common_matches=common_matches?(string_to_match))
	if common_matches.include?(self) then
		possibilities?(common_matches)
	else
		possibilities?([self, common_matches])
	end#if
end

#nameObject

logical_primary_key


33
34
35
# File 'app/models/generic_type.rb', line 33

def name
	return self[:import_class]
end

#one_level_specializationsObject

find Array of more specific types (tree children)


50
51
52
53
54
55
56
57
58
# File 'app/models/generic_type.rb', line 50

def one_level_specializations
	if most_general? then
		return specialize-[self]
	elsif unspecialized? then
		return []
	else
		specialize
	end #if
end

#possibilities?(common_matches) ⇒ Boolean

specializations_that_match

Returns:

  • (Boolean)

142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/generic_type.rb', line 142

def possibilities?(common_matches)
	if common_matches.instance_of?(GenericType) then
		common_matches
	elsif common_matches.kind_of?(Array) then
		if common_matches[1].kind_of?(Array) then
			Array.new(possibilities?(common_matches[1])+possibilities?(common_matches[2..-1]))
		else
			Array.new(common_matches)
		end #if
	end #if
end

#specializationsObject

one_level_specializations


59
60
61
62
63
64
65
66
67
# File 'app/models/generic_type.rb', line 59

def specializations
	if most_general? then
		return (specialize-[self]).map{|s| s.specializations}.flatten + one_level_specializations
	elsif unspecialized? then
		return []
	else
		return specialize.map{|s| s.specializations}.flatten + one_level_specializations
	end #if
end

#specializations_that_match?(string_to_match) ⇒ Boolean

Find specializations that match recursively Returns an (nested?) Array of GenericType Least specialized comes first Multiple specializations that match at the same level are probably not handled correcly yet.

Returns:

  • (Boolean)

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/generic_type.rb', line 123

def specializations_that_match?(string_to_match)
	ret=[]
	one_level_specializations.map do |specialization|
		if specialization.match_exact?(string_to_match) then
			if specialization.unspecialized? then
				ret.push(specialization)
			else
				ret.push(specialization)
				specializations=specialization.specializations_that_match?(string_to_match)
				if !specializations.empty? then
					ret.push(specializations)
				end #if
			end #if
		else
			nil
		end #if
	end .compact.uniq #map
	return NestedArray.new(ret)
end

#unspecialized?Boolean

most_general

Returns:

  • (Boolean)

46
47
48
# File 'app/models/generic_type.rb', line 46

def unspecialized?
	return specialize.empty?
end