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