### Home Classes Methods

### In Files

- lib/main-structures.rb

### Parent

Object

### Methods

- ::new
- #add_rule
- #axiom=
- #get_rule_index
- #load
- #remove_rule
- #save
- #search_rule_by_id
- #search_shape_by_id

### Files

- LessThan5BlueLabels.txt
- AtLeast2LargeSegments.txt

### Class Index

Quicksearch

- AddLabelTool
- AllRulesAppliedGoal
- Area
- AreaConstraint
- Array
- BalancedBinaryTree
- BalancedBinaryTreeNode
- CloseObserver
- Constants
- Constraint
- CurrentLabelledShape
- DistinctShapeConstraint
- Execution
- Goal
- Grammar
- Label
- LabelledShape
- LineDescriptor
- LinearLinkedList
- LinearLinkedListNode
- NoLabelsGoal
- NoMoreRulesGoal
- NoScalesConstraint
- Numeric
- Object
- OrderedPoint
- PiList
- Point
- Project
- Robot
- Rule
- RuleGroupsObserver
- RuleLabelledShape
- RuleShapeObserver
- Segment
- ShaDeLayersObserver
- Shade
- ShadeUtils
- UndoObserver

No matching classes.

# Grammar

Author | Manuela Ruiz (mruiz@lcc.uma.es)

Abstract representation of a grammar, that is, a set of rules

### Attributes

rules[R]

Array for the rules

axiom[R]

The axiom LabelledShape

saved[RW]

True iff the grammar has been saved

### Public Class Methods

new() click to toggle source

Initializing

         # File lib/main-structures.rb, line 223
    223:         def initialize()

    224:                 @rules = Array.new

    225:                 @saved = true

    226:         end

### Public Instance Methods

add_rule(rule) click to toggle source

rule | a Rule object to add

Adds a rule to the grammar

         # File lib/main-structures.rb, line 231
    231:         def add_rule(rule)

    232:                 @rules.push(rule)

    233:         end

axiom=(shape) click to toggle source

shape | a LabelledShape that will be the axiom of the grammar

Sets the axiom to the specified shape

         # File lib/main-structures.rb, line 248
    248:         def axiom=(shape)

    249:                 @axiom = shape

    250:         end

get_rule_index(rule) click to toggle source

rule | a Rule object, representing one of the grammar rules
returns | the index of the rule (not the ID)

         # File lib/main-structures.rb, line 255
    255:         def get_rule_index(rule)

    256:                 result = nil

    257:                 i = 0

    258:                 while ((i < @rules.size) && !result)

    259:                         if @rules[i].rule_id == rule.rule_id

    260:                                 result = i

    261:                         end

    262:                         i+=1

    263:                 end

    264:                 return result

    265:         end

load(path) click to toggle source

path | the path to load the shape from

Loads the shape from the specified path

         # File lib/main-structures.rb, line 341
    341:         def load(path)

    342:

    343:                 #the path is a .gr2 file

    344:                 #we need the directory

    345:                 directory = ShadeUtils.get_directory_from_path(path)

    346:

    347:                 size = @rules.size

    348:                 i = 0

    349:                 while i < size

    350:                         remove_rule(size-i-1)

    351:                         i += 1

    352:                 end

    353:                 @rules = Array.new

    354:

    355:                 Shade.project.execution.file_axiom = false

    356:

    357:                 filename = path.strip

    358:

    359:                 File.open(filename, 'r') do |f|

    360:                         while line = f.gets

    361:                                 line_a = line.split

    362:                                 if line_a[0] == "AXIOM:"

    363:                                         Shade.project.execution.file_axiom = true

    364:                                         @axiom = LabelledShape.new(Array.new, Array.new)

    365:                                         @axiom.load("#{directory}#{line_a[1]}")

    366:                                 else

    367:                                         alpha_title = line_a[0]

    368:                                         beta_title = line_a[1]

    369:                                         alpha = RuleLabelledShape.new(Array.new, Array.new, nil, nil)

    370:                                         alpha.load("#{directory}#{alpha_title}")

    371:

    372:                                         beta = RuleLabelledShape.new(Array.new, Array.new, nil, nil)

    373:                                         beta.load("#{directory}#{beta_title}")

    374:                                         if @rules.empty?

    375:                                                 last_id = 0

    376:                                         else

    377:                                                 last_id = @rules.last.rule_id

    378:                                         end

    379:                                         rule = ShadeUtils.paint_rule(last_id+1, alpha, beta)

    380:                                         @rules.push rule

    381:                                 end

    382:                         end

    383:                 end

    384:

    385:                 if !Shade.project.execution.file_axiom

    386:                         new_axiom = LabelledShape.new(Array.new, Array.new)

    387:                         @rules[0].left.p.each_key {|layer_name|

    388:                                 new_axiom.p[layer_name] = @rules[0].left.p[layer_name].clone

    389:                         }

    390:                         @rules[0].left.s.each_key {|layer_name|

    391:                                 new_axiom.s[layer_name] = @rules[0].left.s[layer_name].clone

    392:                         }

    393:                         Shade.project.execution.grammar.axiom = new_axiom

    394:                 end

    395:

    396:                 Shade.project.execution.reset

    397:         end

remove_rule(i) click to toggle source

i | index of the rule to remove

Removes the rule in the grammar with the index i

         # File lib/main-structures.rb, line 238
    238:         def remove_rule(i)

    239:                 rule = @rules[i]

    240:                 rule.erase

    241:                 #Delete from the array

    242:                 @rules.delete_at(i)

    243:         end

save(path, text = false) click to toggle source

path | the path to save the shape in

Saves the shape in the specified path

         # File lib/main-structures.rb, line 311
    311:         def save(path, text = false)

    312:                 #the path is a .gr2 file

    313:                 #we need the directory

    314:                 directory = ShadeUtils.get_directory_from_path(path)

    315:                 title = ShadeUtils.get_title_from_path(path)

    316:

    317:                 if text

    318:                         extension = "txt"

    319:                 else

    320:                         extension = "skp"

    321:                 end

    322:

    323:                 File.open(path.strip, 'w') do |f|

    324:                         @rules.each { |rule|

    325:                                 f.write("alpha#{rule.rule_id}#{title}.#{extension} beta#{rule.rule_id}#{title}.#{extension}\n")

    326:                                 rule.alpha.save("#{directory}alpha#{rule.rule_id}#{title}.#{extension}")

    327:                                 rule.beta.save("#{directory}beta#{rule.rule_id}#{title}.#{extension}")

    328:                         }

    329:                         if Shade.project.execution.file_axiom

    330:                                 f.write("AXIOM: axiom#{title}.#{extension}\n")

    331:                                 @axiom.save("#{directory}axiom#{title}.#{extension}")

    332:                         end

    333:                 end

    334:

    335:

    336:         end

search_rule_by_id(rule_id) click to toggle source

rule_id | an id

returns the rule with the internal id rule_id. In case it does not exist, returns nil

         # File lib/main-structures.rb, line 270
    270:         def search_rule_by_id(rule_id)

    271:                 i = 0

    272:                 found = false

    273:                 size = @rules.size

    274:                 rule = nil

    275:

    276:                 while i < size and !found

    277:                         if @rules[i].rule_id == rule_id

    278:                                 rule = @rules[i]

    279:                                 found = true

    280:                         end

    281:                         i +=1

    282:                 end

    283:                 return rule

    284:         end

search_shape_by_id(shape_id) click to toggle source

shape_id | an id

returns the shape with the internal id shape_id. In case it does not exist, returns nil

         # File lib/main-structures.rb, line 289
    289:         def search_shape_by_id(shape_id)

    290:                 i = 0

    291:                 found = false

    292:                 size = @rules.size

    293:                 shape = nil

    294:

    295:                 while i < size and !found

    296:                         if @rules[i].left.shape_id == shape_id

    297:                                 shape = @rules[i].left

    298:                                 found = true

    299:                         elsif @rules[i].right.shape_id == shape_id

    300:                                 shape = @rules[i].right

    301:                                 found = true

    302:                         end

    303:                         i +=1

    304:                 end

    305:                 return shape

    306:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.