class TaskJuggler::LogicalOperation

A LogicalOperation is the basic building block for a LogicalExpression. A logical operation has one or two operands and an operator. The operands can be LogicalOperation objects, fixed values or references to project data. The LogicalOperation can be evaluated in a certain context. This contexts determines the actual values of the project data references. The evaluation is done by calling LogicalOperation#eval. The result must be of a type that responds to all the operators that are used in the eval method.

Attributes

operand1[R]
operand2[RW]
operator[RW]

Public Class Methods

new(opnd1, operator = nil, opnd2 = nil) click to toggle source

Create a new LogicalOperation object. opnd1 is the mandatory operand. The @operand2 and the @operator can be set later.

# File lib/taskjuggler/LogicalOperation.rb, line 35
def initialize(opnd1, operator = nil, opnd2 = nil)
  @operand1 = opnd1
  @operand2 = opnd2
  @operator = operator
end

Public Instance Methods

eval(expr) click to toggle source

Evaluate the expression in a given context represented by expr of type LogicalExpression. The result must be of a type that responds to all the operators of this function.

# File lib/taskjuggler/LogicalOperation.rb, line 44
def eval(expr)
  case @operator
  when nil
    if @operand1.respond_to?(:eval)
      # An operand can be a fixed value or another term. This could be a
      # LogicalOperation, LogicalFunction or anything else that provides
      # an appropriate eval() method.
      return @operand1.eval(expr)
    else
      return @operand1
    end
  when '~'
    return !coerceBoolean(@operand1.eval(expr), expr)
  when '>', '>=', '=', '<', '<=', '!='
    # Evaluate the operation for all 2 operand operations that can be
    # either interpreted as date, numbers or Strings.
    opnd1 = @operand1.eval(expr)
    opnd2 = @operand2.eval(expr)
    if opnd1.is_a?(TjTime)
      res= evalBinaryOperation(opnd1, operator, opnd2) do |o|
        coerceTime(o, expr)
      end
      return res
    elsif opnd1.is_a?(Integer) || opnd1.is_a?(Float)
      return evalBinaryOperation(opnd1, operator, opnd2) do |o|
        coerceNumber(o, expr)
      end
    elsif opnd1.is_a?(RichTextIntermediate)
      return evalBinaryOperation(opnd1.to_s, operator, opnd2) do |o|
        coerceString(o, expr)
      end
    elsif opnd1.is_a?(String)
      return evalBinaryOperation(opnd1, operator, opnd2) do |o|
        coerceString(o, expr)
      end
    else
      expr.error("First operand of a binary operation must be a date, " +
                 "a number or a string: #{opnd1.class}")
    end
  when '&'
    return coerceBoolean(@operand1.eval(expr), expr) &&
           coerceBoolean(@operand2.eval(expr), expr)
  when '|'
    return coerceBoolean(@operand1.eval(expr), expr) ||
           coerceBoolean(@operand2.eval(expr), expr)
  else
    expr.error("Unknown operator #{@operator} in logical expression")
  end
end
to_s(query) click to toggle source

Convert the operation into a textual representation.

# File lib/taskjuggler/LogicalOperation.rb, line 95
def to_s(query)
  if @operator.nil?
    operand_to_s(@operand1, query)
  elsif @operand2.nil?
    @operator + operand_to_s(@operand1, query)
  else
    "(#{operand_to_s(@operand1, query)} #{@operator} " +
    "#{operand_to_s(@operand2, query)})"
  end
end