Class: LinkParser::Connector
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
-
#cost ⇒ Object
readonly
The linking cost of the connector.
-
#multiplicity ⇒ Object
readonly
How many connections it takes to satisfy this connector.
-
#name ⇒ Object
readonly
The name of the connector.
-
#side ⇒ Object
readonly
Which side the connector links to (LEFT, RIGHT or BOTH).
-
#subName ⇒ Object
readonly
The sub name of the connector, if any.
Class Method Summary collapse
-
.new(name, subName = '', side = BOTH, multiplicity = ONCE, cost = 0) ⇒ Object
Creates and returns a connector.
Instance Method Summary collapse
-
#both? ⇒ Boolean
Whether or not the connector links to both sides.
-
#dup ⇒ Object
(also: #clone)
Returns self.
-
#eql?(other) ⇒ Boolean
(also: #equal?, #==)
Tests for equality by value.
-
#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.
-
#initialize(name, subName, side, multiplicity, cost) ⇒ Connector
constructor
Initializes a new Connector object.
-
#left? ⇒ Boolean
Whether or not the connector links to the LEFT.
-
#match(other) ⇒ Object
Checks to see if the two connectors can match.
-
#multiple? ⇒ Boolean
Whether or not the connector accepts multiple connections.
-
#optional? ⇒ Boolean
(also: #optional)
Whether or not the connector is optional.
-
#right? ⇒ Boolean
Whether or not the connector links to the RIGHT.
-
#to_s ⇒ Object
(also: #inspect)
Returns a simple stringy representation.
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
#cost ⇒ Object (readonly)
The linking cost of the connector.
73 74 75 |
# File 'lib/linkparser/connector.rb', line 73 def cost @cost end |
#multiplicity ⇒ Object (readonly)
How many connections it takes to satisfy this connector.
76 77 78 |
# File 'lib/linkparser/connector.rb', line 76 def multiplicity @multiplicity end |
#name ⇒ Object (readonly)
The name of the connector.
67 68 69 |
# File 'lib/linkparser/connector.rb', line 67 def name @name end |
#side ⇒ Object (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 |
#subName ⇒ Object (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
103 104 105 |
# File 'lib/linkparser/connector.rb', line 103 def both? @side == BOTH end |
#dup ⇒ Object 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.
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.
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.
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.
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.
98 99 100 |
# File 'lib/linkparser/connector.rb', line 98 def right? @side == RIGHT or @side == BOTH end |
#to_s ⇒ Object 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 |