Skip to content

Commit

Permalink
Modify KB, solver, and help parser to handle greek letters.
Browse files Browse the repository at this point in the history
  • Loading branch information
bvds committed Jun 10, 2010
1 parent 68a7b00 commit 6a4bdf4
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 179 deletions.
22 changes: 21 additions & 1 deletion Algebra/src/getall.cpp
@@ -1,6 +1,6 @@
// getall.cpp
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -122,6 +122,10 @@ bool getavar(const string bufst)
for (kend = bufst.size() - 2; kend >= kstrt
&& (bufst[kend] == ' ' || bufst[kend] == '|'); kend--);
string newvar = bufst.substr(kstrt, kend-kstrt+1);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG (
cout << "variable |" << newvar << "|" << endl; );
for (k = 0; k < canonvars->size(); k++)
Expand All @@ -147,6 +151,10 @@ bool makenn(const string bufst)
for (kend = bufst.size() - 2; kend >= kstrt
&& (bufst[kend] == ' ' || bufst[kend] == '|'); kend--);
string newvar = bufst.substr(kstrt, kend-kstrt+1);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG ( cout << "variable |" << newvar << "| set nn" << endl; );
for (k = 0; k < canonvars->size(); k++)
if (newvar == (*canonvars)[k]->clipsname)
Expand All @@ -172,6 +180,10 @@ bool makepos(const string bufst)
for (kend = bufst.size() - 2; kend >= kstrt
&& (bufst[kend] == ' ' || bufst[kend] == '|'); kend--);
string newvar = bufst.substr(kstrt,kend-kstrt+1);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG (cout << "variable |" << newvar << "| set pos?" << endl);
for (k = 0; k < canonvars->size(); k++)
if (newvar == (*canonvars)[k]->clipsname)
Expand Down Expand Up @@ -201,6 +213,10 @@ bool makenz(const string bufst)
for (kend = bufst.size() - 2; kend >= kstrt
&& (bufst[kend] == ' ' || bufst[kend] == '|'); kend--);
string newvar = bufst.substr(kstrt,kend-kstrt+1);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG ( cout << "variable |" << newvar << "| set nonzero?" << endl; );
for (k = 0; k < canonvars->size(); k++)
if (newvar == (*canonvars)[k]->clipsname)
Expand Down Expand Up @@ -281,6 +297,10 @@ bool makepar(const string bufst, bool keep_algebraic)
if ((kend == kstrt) || kend == bufst.size())
throw(string("parameter in bad format ") + bufst);
string newvar = bufst.substr(kstrt,kend-kstrt);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG ( cout << "variable |" << newvar << "| set param?" << endl);
// skip white space and check for optional numerical value argument
for (kstrt = kend+1; kstrt < bufst.size() && bufst[kstrt] == ' '; kstrt++);
Expand Down
6 changes: 5 additions & 1 deletion Algebra/src/getavar.cpp
@@ -1,6 +1,6 @@
// getavar.cpp
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -87,6 +87,10 @@ int getavarwu(const string bufst, const bool varNew, const bool valToo,
if ((kend == kstrt) || kend == bufst.size())
throw(string("svar in bad format ") + bufst);
string newvar = bufst.substr(kstrt,kend-kstrt);
// Unescape any backslashes
for (k = 0; (k = newvar.find("\\\\", k)) != std::string::npos;){
newvar.replace(k,2,"\\");
}
DBG(cout << "variable |" << newvar <<"|" << endl);
if (valToo) {
kend = parseanum(bufst,kstrt);
Expand Down
11 changes: 6 additions & 5 deletions Algebra/src/parse.cpp
@@ -1,6 +1,6 @@
// parse.cc
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -126,20 +126,21 @@ bool isanum(string token) // returns true if token is an number
}

/************************************************************************
* finds the longest string which starts with a letter : or $ *
* and continues with '-', '_', '$', '&', or alphanumeric chars. *
* finds the longest string which starts with a letter : or \ *
* and continues with '-', '_', '$', '&', '\' or alphanumeric chars. *
* returns the index of first character not in string *
************************************************************************/
int getclipsvar(string token, int start)
{
int j;
if (!isalpha(token[start]) && (token[start] != '$')
if (!isalpha(token[start]) && (token[start] != '\\')
&& (token[start] != ':')) return (start);
for (j = start+1; j<token.size();j++)
if ((!isalnum(token[j]))
&&(token[j] != '-')
&&(token[j] != '-')
&&(token[j] != '_')
&&(token[j] != '&')
&&(token[j] != '\\')
&&(token[j] != '$'))
return(j);
throw(string("getclipsvar ran off end of token string"));
Expand Down
16 changes: 10 additions & 6 deletions Algebra/src/parseeqwunits.cpp
@@ -1,6 +1,6 @@
// parseeqwunits.cpp
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -32,10 +32,9 @@ using namespace std;
* parseEqWUnits(const string & lispeq) *
* returns a stack of string tokens, each of which is one of *
* ( = + - * / ^ ) *
* or a string starting with [A-Z] | [a-z] and continuing *
* with [A-Z] | [a-z] | [0-9] | _ | $ | : *
* or a number *
* or string "U)", which is used for ending units *
* or a variable string, as determined by getclipsvar(...) *
* or a number *
* or string "U)", which is used for ending units *
* if top element returned is not ), something is wrong *
************************************************************************/

Expand Down Expand Up @@ -121,10 +120,15 @@ stack<string> *parseEqWUnits(const string & lispeq)
index = j;
continue;
}
if (isalpha(thiscar) || (thiscar == '$') || (thiscar == ':'))
// should match test at beginning of getclipsvar(...).
if (isalpha(thiscar) || (thiscar == '\\')|| (thiscar == ':'))
{
j = getclipsvar(lispeq,index);
string *p = new string(lispeq.substr(index,j-index));
// Unescape any backslashes
for (int k = 0; (k = p->find("\\\\", k)) != std::string::npos;){
p->replace(k,2,"\\");
}
index = j;
if ((p->compare("dnum")==0) || (p->compare("DNUM")==0) )
{
Expand Down
13 changes: 7 additions & 6 deletions Algebra/src/pconsts.h
@@ -1,6 +1,6 @@
// list of constants, mathematical and physical
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand All @@ -25,12 +25,13 @@
//
// name value m kg s C K
//
physc("$P", 3.14159265358979323, 0, 0, 0, 0, 0), // pi
physc("Pi", 3.14159265358979323, 0, 0, 0, 0, 0), // pi
physc("$e0", 8.854187817E-12,-3,-1, 2, 2, 0), // pemittivity of vacuum
physc("eps0", 8.854187817E-12,-3,-1, 2, 2, 0), // pemittivity of vacuum
physc("\\pi", 3.14159265358979323, 0, 0, 0, 0, 0), // pi
physc("\\epsilon0", 8.854187817E-12,-3,-1, 2, 2, 0), // permittivity of vacuum
physc("\\epsilon_0", 8.854187817E-12,-3,-1, 2, 2, 0), // permittivity of vacuum
physc("eps0", 8.854187817E-12,-3,-1, 2, 2, 0), // permittivity of vacuum
physc("kelec", 8.98755178799791E+9, 3, 1,-2,-2, 0), // 1/4*pi*eps0
physc("$m0", 1.2566370614359173E-6, 1, 1, 0,-2, 0), // permeability of vacuum
physc("\\mu0", 1.2566370614359173E-6, 1, 1, 0,-2, 0), // permeability of vacuum
physc("\\mu_0", 1.2566370614359173E-6, 1, 1, 0,-2, 0), // permeability of vacuum
physc("mu0", 1.2566370614359173E-6, 1, 1, 0,-2, 0), // permeability of vacuum
physc("kmag", 1.0E-7, 1, 1, 0,-2, 0), // mu0/(4*pi)
physc("c", 299792458, 1, 0,-1, 0, 0), // speed of light
Expand Down
5 changes: 3 additions & 2 deletions Algebra/src/prefixes.h
@@ -1,6 +1,6 @@
// Prefixes
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand All @@ -23,8 +23,9 @@ pfx( "a", 1.0E-18 ), // atto
pfx( "f", 1.0E-15 ), // femto
pfx( "p", 1.0E-12 ), // pico
pfx( "n", 1.0E-9 ), // nano
// "mu" must be tested before "m"
pfx( "mu", 1.0E-6 ), // micro
pfx( "$m", 1.0E-6 ), // micro
pfx( "\\mu", 1.0E-6 ), // micro
pfx( "m", 1.0E-3 ), // milli
pfx( "c", 1.0E-2 ), // centi
pfx( "d", 1.0E-1 ), // deci
Expand Down
23 changes: 10 additions & 13 deletions Algebra/src/unitabr.cpp
@@ -1,6 +1,6 @@
// unitabr.cpp get unit abbreviations and handle lookup
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -102,21 +102,18 @@ numvalexp * unitabrs::unitget(const string & unitname)
numvalexp * retval;
string pureunit = unitname;
double pfxvalue = 1.;
// Test for a unit without prefix.
for (q = 0; q < abbrev.size(); q++)
if (pureunit == abbrev[q]) break;
if (q == abbrev.size()) {
if ((unitname.substr(0,2) == "$m") ||(unitname.substr(0,2) == "mu")) {
pureunit = unitname.substr(2,unitname.size()-2);
pfxvalue = 1.0E-6;
}
else {
pureunit = unitname.substr(1,unitname.size()-1);
for (k = 0; k < pfxs.size(); k++)
if (unitname.substr(0,1) == pfxs[k]) {
pfxvalue = pfxvals[k];
break; }
if (k == pfxs.size()) return((numvalexp *) NULL);
}
// Try to match up the unit prefix
for (k = 0; k < pfxs.size(); k++)
if (unitname.compare(0,pfxs[k].length(),pfxs[k])==0) break;
// If there is no match, return error.
if (k == pfxs.size()) return((numvalexp *) NULL);
pureunit.erase(0,pfxs[k].length()); // remove prefix
pfxvalue = pfxvals[k];
// now try to match up the unit, again.
for (q = 0; q < abbrev.size(); q++)
if ((pureunit == abbrev[q]) && pfxable[q]) break;
if (q == abbrev.size()) return((numvalexp *) NULL);
Expand Down
4 changes: 2 additions & 2 deletions Algebra/src/units.h
@@ -1,6 +1,6 @@
// list of units in terms of fundamental dimensions.
// Copyright (C) 2001 by Joel A. Shapiro -- All Rights Reserved
// Modifications by Brett van de Sande, 2005-2008
// Modifications by Brett van de Sande, 2005-2010
//
// This file is part of the Andes Solver.
//
Expand Down Expand Up @@ -36,7 +36,7 @@ punit( "A", 1., 0, 0, -1, 1, 0, 1), // amps, Amperes
punit( "T", 1., 0, 1, -1, -1, 0, 1), // tesla
punit( "G", 1.0e-4, 0, 1, -1, -1, 0, 1), // gauss
punit( "Wb", 1., 2, 1, -1, -1, 0, 1), // webers
punit( "$W", 1., 2, 1, -1, -2, 0, 1), // ohm (as capital omega)
punit( "\\Omega", 1., 2, 1, -1, -2, 0, 1), // ohm (as capital omega)
punit( "ohm", 1., 2, 1, -1, -2, 0, 1), // ohm
punit( "Hz", 1., 0, 0, -1, 0, 0, 1), // Hertz
punit( "Pa", 1., -1, 1, -2, 0, 0, 1), // Pascal
Expand Down
47 changes: 0 additions & 47 deletions Help/Commands.cl
Expand Up @@ -302,50 +302,3 @@
:Assoc '((handle-text . comment))))) )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Entry Status Return Values are 3+ field strings of the form:
;; StatusCode;ErrorList!Command where
;; StatusCode -> T | NIL | <nada>
;; ErrorList -> SlotName | SlotName;MoreErrors | <nada>
;; MoreErrors -> SlotName | SlotName;MoreErrors
;; Command -> WBCommand | <nada>
;; SlotName -> name -- student's label
;; body -- principal body
;; time -- time on which defined
;; type -- qualified when required:
;; force - force type
;; vector - average or instantaneous
;; energy - total, kinetic, or grav-potential
;; spring
;; agent -- second body where required
;; force -- force agent
;; variable - second body in some defs, e.g. distance between
;; dir -- vector - direction
;; zdir -- vector - z-axis direction (only in rotational problems)
;; ang -- vector - angular or linear (only in rotational problems)
;; side1 - first argument of angle dialog (Angle Dialog)
;; side2 - second side argument (Angle Dialog)
;; bodies - list of bodies include in system (System Dialog)
;; WBCommand -> show-hint <HintSpec> -- show a hint in the tutor mesg pane
;; show-lesson lesson-file-name -- show an ANDES min-lesson
;; open-browser URL -- open a lesson viewer on any URL
;; close-browser -- close lesson viewer if open
;; msg <message text> -- show text in a dialog box
;; training-card card-id -- pop up a training card
;; show-demo demo-file-name -- play a log file as a demo script
;; HintSpec -> MessageBody[Flags]
;; Flags -> Flag | Flag Flags
;; Flag -> w -- why -- continuation (why)
;; e -- explain further -- continuation (explain)
;; h -- how do I do that -- continuation (hint-next-substep)
;; MessageBody ->
;; free form text with crude hypertext facilities
;; (\d text) -- RTF-like tags text as definition
;; (\v text) -- hidden text
;; (\h text) -- callback to help-system
;; (\h "What's wrong?") (\v what-is-wrong) calls help
;; function what-is-wrong if the link "What's wrong?" is
;; selected.
;; Support for Greek alphabet names (ie $a short for greek alpha char)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 changes: 15 additions & 0 deletions Help/parse-andes.cl
Expand Up @@ -404,12 +404,14 @@
If the equation is incorrect, set the ErrInterp slot of the student entry."
(let* ((parse (StudentEntry-ParsedEqn se))
(answer (subst-canonical-vars
;; stringify student variables
(parse-pack-lhs 'unknown (parse-tree parse))))
(strings-in-answer (contains-strings answer)))
(cond
(strings-in-answer (handle-undefined-variables-equation se strings-in-answer))
(t
(setf answer (parse-pack-to-string-lhs 'unknown answer))
(setf answer (parse-pack-cs-lhs 'symbol-number answer))
(setf answer (parse-remove-lhs 'wspace answer))
(setf answer (parse-pack-lhs 'r-paren answer))
(setf answer (parse-pack-lhs 'l-paren answer))
Expand Down Expand Up @@ -1274,6 +1276,19 @@
rem))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun subst-canonical-vars (Exp)
"Translate expression subsituting canonical vars for student vars, or standard vars throughout."
;; NB: student variables must be *strings* in Exp, not symbols
;; Strings with no matching expression pass through translation unchanged.
(cond ((stringp exp)
(or (symbols-sysvar exp) ;find canonical variable
exp))
((atom Exp) Exp)
(t (cons (subst-canonical-vars (car Exp))
(subst-canonical-vars (cdr Exp))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; end of parse-andes.cl
;; Copyright (C) 2001 by <Linwood H. Taylor's Employer> -- All Rights Reserved.
Expand Down
19 changes: 7 additions & 12 deletions Help/parse.cl
Expand Up @@ -159,24 +159,19 @@
;;
(defun parse-pack (parse)
(list (first parse)
(remove #\Space (map 'string #'(lambda (x)
(if (characterp x)
x
#\Space))
(flatten (rest parse))))))
(coerce (remove-if-not #'characterp (flatten (cdr parse)))
'string)))
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
(defun parse-pack-cs (parse)
(list (first parse)
(concatenate 'string "|"
(remove #\Space (map 'string #'(lambda (x)
(if (characterp x)
x
#\Space))
(flatten (rest parse)))) "|")))
(list (car parse)
(format nil "|~{~A~}|"
(substitute
"\\\\" #\\
(remove-if-not #'characterp (flatten (cdr parse)))))))
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Expand Down

0 comments on commit 6a4bdf4

Please sign in to comment.