File: LogicalExpression.rb

package info (click to toggle)
tj3 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,048 kB
  • sloc: ruby: 36,481; javascript: 1,113; sh: 19; makefile: 17
file content (68 lines) | stat: -rw-r--r-- 2,164 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = LogicalExpression.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
#               by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'taskjuggler/LogicalOperation'
require 'taskjuggler/Attributes'
require 'taskjuggler/LogicalFunction'

class TaskJuggler


  # A LogicalExpression is an object that describes tree of LogicalOperation
  # objects and the context that it should be evaluated in.
  class LogicalExpression

    attr_reader :query, :sourceFileInfo

    # Create a new LogicalExpression object. _op_ must be a LogicalOperation.
    # _sourceFileInfo_ is the file position where expression started. It may be
    # nil if not available.
    def initialize(op, sourceFileInfo = nil)
      @operation = op
      @sourceFileInfo = sourceFileInfo

      @query = nil
    end

    # This function triggers the evaluation of the expression. _property_ is the
    # PropertyTreeNode that should be used for the evaluation. _scopeProperty_
    # is the PropertyTreeNode that describes the scope. It may be nil.
    def eval(query)
      @query = query
      res = @operation.eval(self)
      return res if res.is_a?(TrueClass) || res.is_a?(FalseClass) ||
                    res.is_a?(String)
      # In TJP syntax 'non 0' means false.
      return res != 0
    end

    # Dump the LogicalExpression as a String. If _query_ is provided, it will
    # show the actual values, otherwise just the variable names.
    def to_s(query = nil)
      if @sourceFileInfo.nil?
        "#{@operation.to_s(query)}"
      else
        "#{@sourceFileInfo} #{@operation.to_s(query)}"
      end
    end

    # This is an internal function. It's called by the LogicalOperation methods
    # in case something went wrong during an evaluation.
    def error(text) # :nodoc:
      raise TjException.new, "#{to_s}\nLogical expression error: #{text}"
    end

  end

end