Class: OCL::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/dbc/ocl.rb

Overview

Needs to be able to deal with old values!

Constant Summary collapse

DEFAULT_ERROR_FUNCTION =
'dbc_error'.freeze
@@mappings =
{
	'pre' => 'precondition',
	'post' => 'postcondition',
	'inv' => 'invariant'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, label) ⇒ Condition

Returns a new instance of Condition.


362
363
364
365
366
367
368
# File 'lib/dbc/ocl.rb', line 362

def initialize(type, label)
	@old_values = nil
	@type = type
	raise "invalid condition type #{type}" unless @type
	@label = label
	@error_function = DEFAULT_ERROR_FUNCTION
end

Instance Attribute Details

#error_functionObject

Returns the value of attribute error_function


375
376
377
# File 'lib/dbc/ocl.rb', line 375

def error_function
  @error_function
end

#labelObject (readonly)

Returns the value of attribute label


376
377
378
# File 'lib/dbc/ocl.rb', line 376

def label
  @label
end

#old_valuesObject (readonly)

Returns the value of attribute old_values


376
377
378
# File 'lib/dbc/ocl.rb', line 376

def old_values
  @old_values
end

#typeObject (readonly)

Returns the value of attribute type


376
377
378
# File 'lib/dbc/ocl.rb', line 376

def type
  @type
end

Instance Method Details

#abort_condition(error_info, result, indent_str = '') ⇒ Object


400
401
402
403
404
405
# File 'lib/dbc/ocl.rb', line 400

def abort_condition(error_info, result, indent_str='')
	str = ''
	str << indent_str << "if (!(#{result})) {\n"
	str << indent_str << "\t#{@error_function}(#{error_info});\n"
	str << indent_str << "}\n"
end

#condition=(expression) ⇒ Object


370
371
372
373
# File 'lib/dbc/ocl.rb', line 370

def condition=(expression)
	raise "condition already set" if @condition
	@condition = expression
end

#long_typeObject


378
379
380
# File 'lib/dbc/ocl.rb', line 378

def long_type
	@@mappings[@type]
end

#to_cexp(indent = '', context = nil) ⇒ Object


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/dbc/ocl.rb', line 382

def to_cexp(indent='', context=nil)
	raise "condition not set" unless @condition

	error_tag = "#{@label.to_s(context)} \"#{long_type} failed\""

	str = ''
	str << indent << "{\n"
	if @condition.cexp?
		result = @condition.to_cexp
	else
		result = "__ocl_result_"
		str << indent << "\tint #{result};\n"
		str << @condition.to_exp(result, "\t" << indent)
	end
	str << abort_condition(error_tag, result, "\t" << indent)
	str << indent << "}\n"
end

#to_sObject


407
408
409
# File 'lib/dbc/ocl.rb', line 407

def to_s
	"#{@type}:"
end