File:  [LON-CAPA] / loncom / homework / math_parser / CalcEnv.pm
Revision 1.1: download - view: text, annotated - select for diffs
Mon Jun 29 15:42:13 2015 UTC (8 years, 11 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
added implementation for <lm>

    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 = shift;
   40:     my $self = {
   41:         _unit_mode => shift // 0,
   42:     };
   43:     if ($self->{_unit_mode}) {
   44:         $self->{_units} = Units->new();
   45:     } else {
   46:         $self->{_variables} = { }; # hash variable name -> quantity
   47:     }
   48:     my $constants_txt = read_file("$Apache::lonnet::perlvar{'lonTabDir'}/constants.json");
   49:     $self->{_constants} = JSON::DWIW->new->from_json($constants_txt);
   50:     $self->{_tolerance} = 0;
   51:     bless $self, $class;
   52:     return $self;
   53: }
   54: 
   55: # Attribute helpers
   56: 
   57: ##
   58: # Unit mode ?
   59: # @returns {boolean}
   60: ##
   61: sub unit_mode {
   62:     my $self = shift;
   63:     return $self->{_unit_mode};
   64: }
   65: 
   66: ##
   67: # Units
   68: # @returns {Units}
   69: ##
   70: sub units {
   71:     my $self = shift;
   72:     return $self->{_units};
   73: }
   74: 
   75: ##
   76: # Variables
   77: # @returns {Object.<string, Quantity>} hash variable name -> quantity
   78: ##
   79: sub variables {
   80:     my $self = shift;
   81:     return $self->{_variables};
   82: }
   83: 
   84: ##
   85: # The constants, read from constants.json.
   86: # @returns {hash} A hash name -> hash with the keys value and units
   87: ##
   88: sub constants {
   89:     my $self = shift;
   90:     return $self->{_constants};
   91: }
   92: 
   93: ##
   94: # Tolerance
   95: # @returns {string|float} tolerance
   96: ##
   97: sub tolerance {
   98:     my $self = shift;
   99:     return $self->{_tolerance};
  100: }
  101: 
  102: 
  103: ##
  104: # Changes an existing unit or defines a new one.
  105: # @param {string} symbol - name used in math expressions
  106: # @param {string} convert - SI equivalent or using other units to help converting to SI
  107: ##
  108: sub setUnit {
  109:     my( $self, $symbol, $convert ) = @_;
  110:     $self->units->{_derived}->{$symbol} = $convert;
  111: }
  112: 
  113: ##
  114: # Changes an existing variable value or defines a new one.
  115: # @param {string} symbol - name used in math expressions
  116: # @param {float|Quantity} value - number value or Quantity
  117: ##
  118: sub setVariable {
  119:     my( $self, $symbol, $value ) = @_;
  120:     if ($value->isa(Quantity)) {
  121:         $self->variables->{$symbol} = $value;
  122:     } else {
  123:         $self->variables->{$symbol} = Quantity->new($value);
  124:     }
  125: }
  126: 
  127: ##
  128: # Defines the tolerance to use for = operations.
  129: # @param {string|float} tolerance
  130: ##
  131: sub setTolerance {
  132:     my( $self, $tolerance ) = @_;
  133:     $self->{_tolerance} = $tolerance;
  134: }
  135: 
  136: ##
  137: # Returns a variable quantity or undef.
  138: # @param {string} symbol - name used in math expressions
  139: # @returns {Quantity}
  140: ##
  141: sub getVariable {
  142:     my( $self, $symbol ) = @_;
  143:     return $self->variables->{$symbol};
  144: }
  145: 
  146: ##
  147: # Returns a constant quantity or undef.
  148: # @param {string} symbol - name used in math expressions
  149: # @returns {Quantity}
  150: ##
  151: sub getConstant {
  152:     my( $self, $symbol ) = @_;
  153:     my $cst = $self->constants->{$symbol};
  154:     if (!defined $cst) {
  155:         return undef;
  156:     }
  157:     return Quantity->new($cst->{"value"}, $cst->{"units"});
  158: }
  159: 
  160: ##
  161: # Converts a unit name into a Quantity. Throws an exception if the unit is not known.
  162: # @param {string} name - the unit name
  163: # @returns {Quantity}
  164: ##
  165: sub convertToSI {
  166:     my ( $self, $name ) = @_;
  167:     return $self->units->convertToSI($self, $name);
  168: }
  169: 
  170: 
  171: 1;
  172: __END__

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