Class: LinkParser::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/linkparser/connector.rb

Constant Summary collapse

LEFT =

left side

'l'
RIGHT =

right side

'r'
BOTH =

both sides

'*'
ONCE =

matches once

1..1
OPTIONAL =

matches zero or one times

0..1
PLUS =

matches one or more times

1..n
STAR =

matches zero or more times

0..n
@@connectors =

The names ‘star’ and ‘plus’ are taken from regexen.

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, subName, side, multiplicity, cost) ⇒ Connector

Initializes a new Connector object.



58
59
60
61
62
63
64
# File 'lib/linkparser/connector.rb', line 58

def initialize( name, subName, side, multiplicity, cost )
	@name = name
	@subName = subName
	@side = side
	@multiplicity = multiplicity
	@cost = cost
end

Instance Attribute Details

#costObject (readonly)

The linking cost of the connector.



73
74
75
# File 'lib/linkparser/connector.rb', line 73

def cost
  @cost
end

#multiplicityObject (readonly)

How many connections it takes to satisfy this connector.



76
77
78
# File 'lib/linkparser/connector.rb', line 76

def multiplicity
  @multiplicity
end

#nameObject (readonly)

The name of the connector.



67
68
69
# File 'lib/linkparser/connector.rb', line 67

def name
  @name
end

#sideObject (readonly)

Which side the connector links to (LEFT, RIGHT or BOTH).



90
91
92
# File 'lib/linkparser/connector.rb', line 90

def side
  @side
end

#subNameObject (readonly)

The sub name of the connector, if any.



70
71
72
# File 'lib/linkparser/connector.rb', line 70

def subName
  @subName
end

Class Method Details

.new(name, subName = '', side = BOTH, multiplicity = ONCE, cost = 0) ⇒ Object

Creates and returns a connector. Creation only happens once for each unique set of arguments. name - major name of the connector (‘A’,‘AA’,‘P*’,&c.) subName - minor name of the connector (‘s’ or ‘p*x’ from ‘MVs’ or ‘MVp*x’, respectively) side - which side the connector accepts from multiplicity - the number of connections required to satisfy this connector.



46
47
48
49
50
51
52
53
54
55
# File 'lib/linkparser/connector.rb', line 46

def Connector.new( name, subName='', side=BOTH, multiplicity=ONCE, cost=0 )
	subName = '' unless subName
connector = super(name,subName,side,multiplicity,cost)
if(already = (@@connectors.find {|c| c.eql? connector}))
	return already
else
	@@connectors << connector
	return connector
end
end

Instance Method Details

#both?Boolean

Whether or not the connector links to both sides

Returns:

  • (Boolean)


103
104
105
# File 'lib/linkparser/connector.rb', line 103

def both? 
	@side == BOTH
end

#dupObject Also known as: clone

Returns self



108
109
110
# File 'lib/linkparser/connector.rb', line 108

def dup 
	self
end

#eql?(other) ⇒ Boolean Also known as: equal?, ==

Tests for equality by value.

Returns:

  • (Boolean)


190
191
192
193
194
195
196
# File 'lib/linkparser/connector.rb', line 190

def eql?(other)
	@name == other.name and
		@subName == other.subName and
		@cost == other.cost and
		@multiplicity == other.multiplicity and
		@side == other.side
end

#generalize(other) ⇒ Object

Generalizes this connector with the given other connector in a position-based comparison according to the following rules:

Names are compared separately from subNames.

Matching characters: left the same. ‘*’ and any character: becomes a ‘*’. ‘^’ matches only the ‘*’ character Non-matching characters: turned into a ‘*’.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/linkparser/connector.rb', line 163

def generalize(other)
	name = @name
	subName = @subName
	
	i = 0
	while( i < name.length && i < other.name.length )
		name[i] = '*' if name[i] != other.name[i] or
			name[i,1] == '*' or other.name[i,1] == '*'
		i += 1
	end
	
	if other.subName.empty?
		subName = '*' * subName.length
	elsif subName.empty?
		subName = '*' * other.subName.length
	else
		i = 0
		while( i < subName.length && i < other.subName.length )
			subName[i] = '*' unless subName[i] == other.subName[i]
			i += 1
		end
	end
	
	return [name,subName]
end

#left?Boolean

Whether or not the connector links to the LEFT.

Returns:

  • (Boolean)


93
94
95
# File 'lib/linkparser/connector.rb', line 93

def left? 
	@side == LEFT or @side == BOTH
end

#match(other) ⇒ Object

Checks to see if the two connectors can match. rules for matching: 1: a ‘*’ matches any character 2: a ‘^’ matches only the ‘*’ character 3: a ‘a’ matches either a ‘a’ or a ‘*’ (for any character ‘a-Z’) 4: a Connector is considered to be followed by an infinite number of ‘*’s



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/linkparser/connector.rb', line 130

def match(other)
	return false unless (@side != other.side ||
						 @side == BOTH)		
	i = 0
	while( i <= @name.length && i <= other.name.length )
		return( false ) unless ( @name[i] == '*'[0] || 
								other.name[i] == '*'[0] ||
								( (@name[i] == other.name[i]) &&
								  (@name[i] != '^'[0]) ) ||
								(@name[i].nil? || other.name[i].nil?) )
		i += 1
	end
	
	i = 0
	while( i <= @subName.length && i <= other.subName.length )
		return( false ) unless ( @subName[i] == '*'[0] || 
								other.subName[i] == '*'[0] ||
								( (@subName[i] == other.subName[i]) &&
								  (@subName[i] != '^'[0]) ) ||
								(@subName[i].nil? || other.subName[i].nil? ) )
		i += 1
	end
	
	return true
end

#multiple?Boolean

Whether or not the connector accepts multiple connections.

Returns:

  • (Boolean)


85
86
87
# File 'lib/linkparser/connector.rb', line 85

def multiple? 
	@multiplicity === 2
end

#optional?Boolean Also known as: optional

Whether or not the connector is optional.

Returns:

  • (Boolean)


79
80
81
# File 'lib/linkparser/connector.rb', line 79

def optional? 
	@multiplicity === 0
end

#right?Boolean

Whether or not the connector links to the RIGHT.

Returns:

  • (Boolean)


98
99
100
# File 'lib/linkparser/connector.rb', line 98

def right? 
	@side == RIGHT or @side == BOTH
end

#to_sObject Also known as: inspect

Returns a simple stringy representation



114
115
116
117
118
119
120
121
# File 'lib/linkparser/connector.rb', line 114

def to_s 
	result = @name + @subName
	result = "@" + result if self.multiple?
	result += (@side == LEFT ? "-" : "+") unless @side == BOTH
	result += "(#{@cost})" if self.cost.nonzero?
	result = "{" + result + "}" if self.optional?
	return result
end