Class: Petri::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/petri/net.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Net

Returns a new instance of Net.



2
3
4
5
6
7
8
# File 'lib/petri/net.rb', line 2

def initialize(options={}, &block)
  @places = []
  @transitions = []
  @start = nil
  @end = nil
  block.call(self)
end

Instance Method Details

#add_place(place, options = {}) ⇒ Object

Raises:

  • (TypeError)


10
11
12
13
14
15
16
17
# File 'lib/petri/net.rb', line 10

def add_place(place, options={})
  entity = place if place.is_a? Petri::Place
  entity = Petri::Place.new(place, options) if place.is_a? Symbol
  raise TypeError unless entity.is_a? Petri::Place

  @places << entity unless @places.include?(entity)
  entity
end

#compileObject



35
36
37
38
39
40
41
42
# File 'lib/petri/net.rb', line 35

def compile
  {
    places: @places.map { |place| place.compile },
    transitions: @transitions.map { |transition| transition.compile },
    start_place: @start,
    end_place: @end,
  }
end

#end_place(label, options = {}) ⇒ Object



24
25
26
27
# File 'lib/petri/net.rb', line 24

def end_place(label, options={})
  place = add_place(label, options)
  @end = place.label
end

#start_place(label, options = {}) ⇒ Object



19
20
21
22
# File 'lib/petri/net.rb', line 19

def start_place(label, options={})
  place = add_place(label, options)
  @start = place.label
end

#transition(label, options = {}, &block) ⇒ Object

Raises:

  • (TypeError)


29
30
31
32
33
# File 'lib/petri/net.rb', line 29

def transition(label, options={}, &block)
  raise TypeError unless label.is_a? Symbol
  raise ArgumentError if @transitions.include? label
  @transitions << Petri::Transition.new(self, label, options, &block)
end