class Eventsims::Randomsim

module Simevent

Public Class Methods

new(*args) click to toggle source
# File lib/eventsims/simevent.rb, line 6
def initialize(*args)
        ''' Initialisation '''

        stop, amount, @_intarrival,  @_service = 10, (2..20).step(1).to_a.sample, [], []

        if args.size == 0
                amount.times{@_intarrival << (1..stop).step(1).to_a.sample}
                amount.times{@_service << (1..stop).step(1).to_a.sample}     

        elsif args.size == 1
                args[0].times{@_intarrival << (1..stop).step(1).to_a.sample}
                args[0].times{@_service << (1..stop).step(1).to_a.sample}    

        elsif args.size == 2
                args[1].times{@_intarrival << (1..args[0]).step(1).to_a.sample}
                args[1].times{@_service << (1..args[0]).step(1).to_a.sample} 

        elsif args.size == 3
                args[2].times{@_intarrival << (1..args[0]).step(1).to_a.sample}
                args[2].times{@_service << (1..args[1]).step(1).to_a.sample}

        else
                raise "Arguments must be between 0 to 3"
        end

        #Sets its first value to zero
        @_intarrival[0] = 0

        # Required variables
        @_arrival, @_preservstart, @_servstart = [], [0], []
        @_queue, @_servend, @_custspend, @_idle = [], [], [], [0]

        def getarrive_()
                '''Returns arrival time'''
                increase = 0
                @_intarrival.each {|i| increase +=i
                        @_arrival << increase }
                return @_arrival
        end
        
        def servbegins_()
                '''Returns time when service begin'''
                increase = 0
                i = 0; while i < @_service.size
                        increase+= @_service[i]
                        @_preservstart << increase
                        i+=1
                end
                @_preservstart.pop
                return @_preservstart
        end

        #populate @_servend with values. just to get same size
        @_intarrival.each{|i| @_servend << i}
                
        #Please maintain order if you are editing the code!
        # Calling functions
        getarrive_()  #Returns arrival time
        servbegins_() #Returns time when servce begin

        def get_servend_()
                '''Retuns time when service ends'''
                x = 0; while x < ((@_preservstart).size)
                        @_servend[x] = @_preservstart[x] + @_service[x]
                        s = 1; while s < (@_preservstart).size
                                if @_preservstart[s] < (maximum = @_arrival[s] > @_servend[x]? @_arrival[s] : @_servend[x])
                                        @_preservstart[s] = (maximum)
                                end
                                s+=1 
                        end
                        x+=1
                end

                return @_servend
        end

        # Method used to calculate the rest of the data like
        #time when service begins
        #wait time in queue, (_queue)
        #time customer spent in the system (_custspend)
        def otherresults(list1, list2, list3)
                ''' Stores and return the value of (list2 - list3) in list1 '''
                x = 0; while x < list2.size
                        list1 << (list2[x] - list3[x]) 
                        x+=1
                end
                list1[0] = 0 if list1[0] < 0 
        end

        def idletime_()
                '''Returns the idle time of server'''
                x,y = 0,1
                while y < @_servend.size
                        (@_idle) << (@_servstart[y] - @_servend[x])
                        x+=1; y+=1
                end
                return @_idle
        end

        # Calling other methods
        get_servend_()
        otherresults(@_servstart, @_servend, @_service) 
        otherresults(@_queue, @_servstart, @_arrival) 
        otherresults(@_custspend, @_servend, @_arrival)
        idletime_()
end

Public Instance Methods

arrival() click to toggle source
# File lib/eventsims/simevent.rb, line 121
def arrival()
        '''Returns the arrival time'''
        return @_arrival
end
custspend() click to toggle source
# File lib/eventsims/simevent.rb, line 146
def custspend()
        '''Returns the time customer spends in system'''
        return @_custspend
end
get_servend_() click to toggle source
# File lib/eventsims/simevent.rb, line 66
def get_servend_()
        '''Retuns time when service ends'''
        x = 0; while x < ((@_preservstart).size)
                @_servend[x] = @_preservstart[x] + @_service[x]
                s = 1; while s < (@_preservstart).size
                        if @_preservstart[s] < (maximum = @_arrival[s] > @_servend[x]? @_arrival[s] : @_servend[x])
                                @_preservstart[s] = (maximum)
                        end
                        s+=1 
                end
                x+=1
        end

        return @_servend
end
getarrive_() click to toggle source
# File lib/eventsims/simevent.rb, line 38
def getarrive_()
        '''Returns arrival time'''
        increase = 0
        @_intarrival.each {|i| increase +=i
                @_arrival << increase }
        return @_arrival
end
idle() click to toggle source
# File lib/eventsims/simevent.rb, line 151
def idle()
        '''Returns the idle time of server'''
        return @_idle
end
idletime_() click to toggle source
# File lib/eventsims/simevent.rb, line 95
def idletime_()
        '''Returns the idle time of server'''
        x,y = 0,1
        while y < @_servend.size
                (@_idle) << (@_servstart[y] - @_servend[x])
                x+=1; y+=1
        end
        return @_idle
end
intarrival() click to toggle source

Methods to be used outside of initialize returning necessary values Main methods

# File lib/eventsims/simevent.rb, line 116
def intarrival()
        '''Returns the interarrival time'''
        return @_intarrival
end
otherresults(list1, list2, list3) click to toggle source
Method used to calculate the rest of the data like

time when service begins wait time in queue, (_queue) time customer spent in the system (_custspend)

# File lib/eventsims/simevent.rb, line 86
def otherresults(list1, list2, list3)
        ''' Stores and return the value of (list2 - list3) in list1 '''
        x = 0; while x < list2.size
                list1 << (list2[x] - list3[x]) 
                x+=1
        end
        list1[0] = 0 if list1[0] < 0 
end
queuewait() click to toggle source
# File lib/eventsims/simevent.rb, line 136
def queuewait()
        """Returns the customer's waiting time in the queue"""
        return @_queue
end
servbegin() click to toggle source
# File lib/eventsims/simevent.rb, line 131
def servbegin()
        '''Returns the time when service began'''
        return @_servstart
end
servbegins_() click to toggle source
# File lib/eventsims/simevent.rb, line 46
def servbegins_()
        '''Returns time when service begin'''
        increase = 0
        i = 0; while i < @_service.size
                increase+= @_service[i]
                @_preservstart << increase
                i+=1
        end
        @_preservstart.pop
        return @_preservstart
end
servend() click to toggle source
# File lib/eventsims/simevent.rb, line 141
def servend()
        '''Returns the time service ended'''
        return @_servend
end
service() click to toggle source
# File lib/eventsims/simevent.rb, line 126
def service()
        '''Returns the service time'''
        return @_service
end