File:  [LON-CAPA] / loncom / homework / math_parser / CalcEnv.pm
Revision 1.2: download - view: text, annotated - select for diffs
Tue Jun 30 17:42:14 2015 UTC (8 years, 11 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
math parser: changed syntax to avoid compatibility problems with Perl 5.8.8

    1: # The LearningOnline Network with CAPA - LON-CAPA
    2: # CalcEnv
    3: #
    4: # Copyright (C) 2014 Michigan State University Board of Trustees
    5: #
    6: # This program is free software: you can redistribute it and/or modify
    7: # it under the terms of the GNU General Public License as published by
    8: # the Free Software Foundation, either version 3 of the License, or
    9: # (at your option) any later version.
   10: #
   11: # This program is distributed in the hope that it will be useful,
   12: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   14: # GNU General Public License for more details.
   15: #
   16: # You should have received a copy of the GNU General Public License
   17: # along with this program. If not, see <http://www.gnu.org/licenses/>.
   18: #
   19: 
   20: ##
   21: # Calculation environment, using either units or variables.
   22: ##
   23: package Apache::math_parser::CalcEnv;
   24: 
   25: use strict;
   26: use warnings;
   27: use utf8;
   28: 
   29: use File::Slurp;
   30: 
   31: use aliased 'Apache::math_parser::Quantity';
   32: use aliased 'Apache::math_parser::Units';
   33: 
   34: ##
   35: # Constructor
   36: # @param {boolean} unit_mode
   37: ##
   38: sub new {
   39:     my ($class, $unit_mode) = @_;
   40:     if (!defined $unit_mode) {
   41:         $unit_mode = 0;
   42:     }
   43:     my $self = {
   44:         _unit_mode => $unit_mode,
   45:     };
   46:     if ($self->{_unit_mode}) {
   47:         $self->{_units} = Units->new();
   48:     } else {
   49:         $self->{_variables} = { }; # hash variable name -> quantity
   50:     }
   51:     my $constants_txt = read_file("$Apache::lonnet::perlvar{'lonTabDir'}/constants.json");
   52:     $self->{_constants} = JSON::DWIW->new->from_json($constants_txt);
   53:     $self->{_tolerance} = 0;
   54:     bless $self, $class;
   55:     return $self;
   56: }
   57: 
   58: # Attribute helpers
   59: 
   60: ##
   61: # Unit mode ?
   62: # @returns {boolean}
   63: ##
   64: sub unit_mode {
   65:     my $self = shift;
   66:     return $self->{_unit_mode};
   67: }
   68: 
   69: ##
   70: # Units
   71: # @returns {Units}
   72: ##
   73: sub units {
   74:     my $self = shift;
   75:     return $self->{_units};
   76: }
   77: 
   78: ##
   79: # Variables
   80: # @returns {Object.<string, Quantity>} hash variable name -> quantity
   81: ##
   82: sub variables {
   83:     my $self = shift;
   84:     return $self->{_variables};
   85: }
   86: 
   87: ##
   88: # The constants, read from constants.json.
   89: # @returns {hash} A hash name -> hash with the keys value and units
   90: ##
   91: sub constants {
   92:     my $self = shift;
   93:     return $self->{_constants};
   94: }
   95: 
   96: ##
   97: # Tolerance
   98: # @returns {string|float} tolerance
   99: ##
  100: sub tolerance {
  101:     my $self = shift;
  102:     return $self->{_tolerance};
  103: }
  104: 
  105: 
  106: ##
  107: # Changes an existing unit or defines a new one.
  108: # @param {string} symbol - name used in math expressions
  109: # @param {string} convert - SI equivalent or using other units to help converting to SI
  110: ##
  111: sub setUnit {
  112:     my( $self, $symbol, $convert ) = @_;
  113:     $self->units->{_derived}->{$symbol} = $convert;
  114: }
  115: 
  116: ##
  117: # Changes an existing variable value or defines a new one.
  118: # @param {string} symbol - name used in math expressions
  119: # @param {float|Quantity} value - number value or Quantity
  120: ##
  121: sub setVariable {
  122:     my( $self, $symbol, $value ) = @_;
  123:     if ($value->isa(Quantity)) {
  124:         $self->variables->{$symbol} = $value;
  125:     } else {
  126:         $self->variables->{$symbol} = Quantity->new($value);
  127:     }
  128: }
  129: 
  130: ##
  131: # Defines the tolerance to use for = operations.
  132: # @param {string|float} tolerance
  133: ##
  134: sub setTolerance {
  135:     my( $self, $tolerance ) = @_;
  136:     $self->{_tolerance} = $tolerance;
  137: }
  138: 
  139: ##
  140: # Returns a variable quantity or undef.
  141: # @param {string} symbol - name used in math expressions
  142: # @returns {Quantity}
  143: ##
  144: sub getVariable {
  145:     my( $self, $symbol ) = @_;
  146:     return $self->variables->{$symbol};
  147: }
  148: 
  149: ##
  150: # Returns a constant quantity or undef.
  151: # @param {string} symbol - name used in math expressions
  152: # @returns {Quantity}
  153: ##
  154: sub getConstant {
  155:     my( $self, $symbol ) = @_;
  156:     my $cst = $self->constants->{$symbol};
  157:     if (!defined $cst) {
  158:         return undef;
  159:     }
  160:     return Quantity->new($cst->{"value"}, $cst->{"units"});
  161: }
  162: 
  163: ##
  164: # Converts a unit name into a Quantity. Throws an exception if the unit is not known.
  165: # @param {string} name - the unit name
  166: # @returns {Quantity}
  167: ##
  168: sub convertToSI {
  169:     my ( $self, $name ) = @_;
  170:     return $self->units->convertToSI($self, $name);
  171: }
  172: 
  173: 
  174: 1;
  175: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>