Re: Storing units in the database
Date: 7 Jul 2005 20:12:28 -0700
Message-ID: <1120792347.997173.220590_at_o13g2000cwo.googlegroups.com>
> 20 feet = 20*0.3 meters > Now, when applied to the database, how should we store 20 feet? User > defined types provide us some (quite unsophisticated) solution. But > do we really have to program a class every time we want to do trivial > conversions?
Below script models 20 ft, 20 sec, conversion from ft to meter, etc in
xrdb.
Application code (OO or not) should perform conversion based on data in
db.
// Create type distance, time, qty and unit // and make them items of main directory. // Note: "inst" is short for "instance". // Note: "it" refers to last new thing created by "*" in CREATE stmt.
(CREATE type inst *distance & dir item it)
(CREATE type inst *time & dir item it)
(CREATE type inst *qty & dir item it)
(CREATE type inst *unit & dir item it)
// Create 20 ft.
(CREATE distance inst *
& it qty +20 & it unit +feet)
// Create 40 ft.
// Note: db auto normalized unit "feet"
(CREATE distance inst *
& it qty +40 & it unit +feet)
// Create 20 sec.
// Note: db auto normalized qty "20".
(CREATE time inst *
& it qty +20 & it unit +second)
// Create abbreviation for feet and second.
(CREATE type inst *abbr)
(CREATE feet abbr +ft)
(CREATE second abbr +sec)
// Find all things whose qty is 20.
// Returns 20ft and 20sec.
(SELECT * qty 20)
// Find all things whose qty is abbreviated as "ft".
// Returns 20ft and 40ft.
(SELECT * unit (* abbr ft))
// Create verb height to relate distance as height of things.
(CREATE verb inst *height)
// Create person John's height is 20 ft.
(CREATE type inst *person & dir item it)
(CREATE person inst *john & it height (SELECT * qty 20 & * unit
feet))
// Create tree1's height is 20 ft.
(CREATE type inst *tree & dir item it)
(CREATE tree inst *tree1 & it height (SELECT * qty 20 & * unit feet))
// Find all things that are 20 ft in height.
// Returns john and tree1.
(SELECT * height (SELECT * qty 20 & * unit feet))
// Create conversion rate from feet to meter
// and vise versa.
(CREATE type inst *"conversion rate")
(CREATE unit inst *meter)
(CREATE meter abbr +m)
(CREATE feet to meter "conversion rate" +0.3)
(CREATE meter to feet "conversion rate" +3.3)
// Find conversion rate from feet to meter.
// Returns 0.3
(SELECT feet to meter "conversion rate" *)
Received on Fri Jul 08 2005 - 05:12:28 CEST