Context Final
------=_NextPart_000_0043_01BFE179.1FCD3DA0
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
After some thinking and studying, I say a major flaw in the algorithm of =
the previous Context, and also some design issues. Which is why I've =
reshaped the whole library. Here is the new improved include file. Its =
also a whole let better managed and cleaner code, its still as fast as I =
could make it. Also it should be pretty memory efficient, considering =
what its doing.
You can use it like you would use most other expert language tools. Use =
it for an RPG game, an advanced database, a help system, or any other =
place where abstract models, context-sensitive and relationship are the =
keywords or your programming problem.
The file is inserted below and because of that, I've chosen for HTML =
rather than a text message. No zip, no attachment.
Greetings,=20
Ralf N.
nieuwen at xs4all.nl
-------------------------------------------------------------------------=
-----------------
--
-- CONTEXT.E June =
2000
-- (c) By Ralf Nieuwenhuijsen USE AT OWN =
RISK
--
-------------------------------------------------------------------------=
-----------------
--
-- Offers context sensitive information management through the =
*formal* routines:
--
-- context c
-- query q, left, right
--
-- c =3D EMPTY
--
-- define ( c, left, right )
-- ask_query ( c, q )
--
-- However, you can also use the quick'n'dirty interface, where =
all strings are
-- parsed into chunks and stored in a seperate database, to save =
precious memory
-- space:
--
-- concept (prefix)
-- the (left) equals (right)
-- the (left) setto (some_value)
-- top_level ()
--
-- ask (query)
--
-- Prefix, query, left and right will be parsed. I.E. you can say =
things like:=20
-- =20
-- ask ("Application ProgramName")=20
--
-------------------------------------------------------------------------=
-----------------
-------------------------------------------------------------------------=
-----------------
-- Variables & Constants
sequence
l_rules, r_rules
constant
TRUE =3D 1, FALSE =3D 0
global constant
EMPTY =3D {{}, {}, FALSE}
l_rules =3D {} r_rules =3D {}
-------------------------------------------------------------------------=
-----------------
-- Basic Types
type code (atom x)
return x >=3D 0 and x <=3D length( names )
end type
global type context (sequence s)
return length(s) =3D 3 and length(s[1]) =3D length(s[2])
end type
global type query (sequence s)
for index =3D 1 to length(s) do
if not code (s[index]) then
return FALSE
end if
end for
return TRUE
end type
-------------------------------------------------------------------------=
-----------------
-- Core Routines
function new (query l, query r)
integer pos
pos =3D find (1, l_rules)
if pos then
r_rules[pos] =3D r
else
l_rules =3D append (l_rules, l)
r_rules =3D append (r_rules, r)
return length(l_rules)
end if
return pos
end function
function tree_straighten (context c, integer i, integer o)
if c[3] !=3D o then
return c
end if
c[3] =3D i
for index =3D 1 to length(c[2]) do
c[2][index] =3D tree_straighten (c[2][index], i, o)
end for
return c
end function
function store_rule (sequence c, integer i, integer index)
sequence item
integer pos
item =3D l_rules[i]
if index > length(item) then
c =3D tree_straighten (c, i, c[3])
else
pos =3D find (item[index], c[1])
if pos then
c[2][pos] =3D store_rule (c[2][pos], i, index+1)
else
c[1] =3D append (c[1], l_rules[i][index])
c[2] =3D append (c[2], store_rule (EMPTY, i, index+1))
end if
end if
return c
end function
function match_rules (context c, query s)
integer pos
for index =3D 1 to length(s) do
pos =3D find (s[index], c[1])
if pos then
c =3D c[2][pos]
else
return c[3]
end if
end for
return c[3]
end function
-------------------------------------------------------------------------=
-----------------
-- Exportable Routines
global function define (context c, query l, query r)
integer i
i =3D new (l, r)
return store_rule (c, i, 1)
end function
global function ask_query (context c, query s)
sequence olds
integer i
i =3D NONE olds =3D {}
while compare (olds, s) do
olds =3D s
i =3D match_rules (c, s)
if i then
s =3D r_rules[i] & s[length(l_rules[i])+1..length(s)]
elsif length(s) > 1 then
s =3D s[2..length(s)]
end if
end while
return s
end function
-------------------------------------------------------------------------=
-----------------
-- Quick'n'dirty Interface
constant WHITESPACE =3D 13 & 10 & 255 & 32
sequence prefix, knowledge, lquery, names
prefix =3D {} knowledge =3D EMPTY names =3D {}
type char (integer i)
return i >=3D 0 and i < 256
end type
type string (sequence s)
for index =3D 1 to length(s) do
if not char( s[index] ) then
return FALSE
end if
end for
return TRUE
end type
function parse (sequence query)
sequence ret
integer start, flag
flag =3D FALSE ret =3D {}
for index =3D 1 to length(query) do
if flag then
if find (query[index], WHITESPACE) then
ret =3D append(ret, query[start..index-1])
flag =3D FALSE
end if
else
if not find (query[index], WHITESPACE) then
flag =3D TRUE
start =3D index
end if
end if
end for
if flag then
return append(ret, query[start..length(query)])
else
return ret
end if
end function
function flatten (sequence s, object p)
sequence result
result =3D ""
for index =3D 1 to length(s) - 1 do
result &=3D s[index] & p
end for
if not length(s) then return "" end if
return result & s[length(s)]
end function
function to_names (sequence x)
for index =3D 1 to length(x) do
x[index] =3D names[x[index]]
end for
return x
end function
function to_codes (sequence x)
integer pos
for index =3D 1 to length(x) do
pos =3D find( x[index], names )
if not pos then
names =3D append( names, x[index] )
pos =3D length( names )
end if
x[index] =3D pos
end for
return x
end function
global procedure concept (sequence s)
prefix =3D to_codes ( parse (s) )
end procedure
global procedure top_level ()
prefix =3D {}
end procedure
global procedure the (sequence s)
lquery =3D to_codes ( parse (s) )
end procedure
global procedure equals (sequence s)
s =3D to_codes ( parse (s) )
knowledge =3D define (knowledge, lquery, s)
end procedure
global procedure setto (object x)
x =3D to_codes ( {x} )
knowledge =3D define (knowledge, lquery, x)
end procedure
global function ask (sequence s)
return flatten(to_names (ask_query(knowledge, to_codes (parse(s))) =
), ' ')
end function
---------------------------------------------------------------------
Here the code ends! .. Greetings, Ralf.
------=_NextPart_000_0043_01BFE179.1FCD3DA0
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2314.1000" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>After some thinking and studying, I say =
a major=20
flaw in the algorithm of the previous Context, and also some design =
issues.=20
Which is why I've reshaped the whole library. Here is the new improved =
include=20
file. Its also a whole let better managed and cleaner code, its still as =
fast as=20
I could make it. Also it should be pretty memory efficient, considering=20
what its doing.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>You can use it like you would use most =
other expert=20
language tools. Use it for an RPG game, an advanced database, a help =
system, or=20
any other place where abstract models, context-sensitive and =
relationship are=20
the keywords or your programming problem.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>The file is inserted below and because =
of=20
that</FONT><FONT face=3DArial size=3D2>, I've chosen for HTML rather =
than a text=20
message. No zip, no attachment.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Greetings, </FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2>Ralf N.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><A=20
<DIV> </DIV>
<DIV><FONT face=3D"Courier New"=20
nbsp;=20
June 2000<BR>-- (c) By Ralf=20
=20
USE AT OWN=20
Offers context sensitive information management through the *formal*=20
p;=20
context c<BR>-- =
query q,=20
left, =
right<BR>--<BR>-- =
c=20
=3D =
EMPTY<BR>--<BR>-- =
define=20
( c, left, right =
ask_query ( c, q )<BR>--<BR>-- =
However, you=20
can also use the quick'n'dirty interface, where all strings=20
are<BR>-- parsed into chunks and stored in a =
seperate=20
database, to save precious memory<BR>-- =20
space:<BR>--<BR>-- =
concept=20
; =20
the (left) equals (right)</FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
bsp; the=20
(left) setto=20
(some_value)<BR>-- =
top_level =
ask (query)</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>--</FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>-- Prefix, query, left =
and right=20
will be parsed. I.E. you can say things like: </FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>-- =
</FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
k=20
("Application ProgramName")=20
------------------------------------------------<BR>--=20
Variables & Constants</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> sequence<BR> =
l_rules,=20
r_rules<BR> constant<BR> TRUE =3D 1, FALSE =3D =
0<BR> global=20
constant<BR> EMPTY =3D {{}, {}, FALSE}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> l_rules =3D {} r_rules =
=3D=20
{}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New"=20
--------------------------<BR>--=20
Basic Types</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> type code (atom=20
x)<BR> return x >=3D 0 and x <=3D length( names =
)<BR> =20
end type</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global type context =
(sequence=20
s)<BR> return length(s) =3D 3 and length(s[1]) =3D=20
length(s[2])<BR> end type</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global type query =
(sequence=20
s)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(s) do<BR> if not code (s[index])=20
then<BR> return=20
FALSE<BR> end if<BR> end =
for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
TRUE<BR> =20
end type</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New"=20
--------------------------<BR>--=20
Core Routines</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function new (query l, =
query=20
r)<BR> integer pos</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> pos =3D find =
(1,=20
l_rules)<BR> if pos =
then<BR> =20
r_rules[pos] =3D r<BR> =
else<BR> =20
l_rules =3D append (l_rules, l)<BR> =
r_rules =3D append=20
(r_rules, r)<BR> return=20
length(l_rules)<BR> end if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
pos<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function tree_straighten =
(context c,=20
integer i, integer o)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> if c[3] !=3D =
o=20
then<BR> return c<BR> =
end=20
if<BR> c[3] =3D i</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(c[2]) do<BR> c[2][index] =3D =
tree_straighten=20
(c[2][index], i, o)<BR> end for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
c<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function store_rule =
(sequence c,=20
integer i, integer index)<BR> sequence item<BR> integer=20
pos</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> item =3D=20
l_rules[i]</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> if index =
>=20
length(item) then<BR> c =3D =
tree_straighten (c, i,=20
c[3])<BR> else<BR> pos =
=3D find=20
(item[index], c[1])<BR> if pos=20
then<BR> c[2][pos] =3D =
store_rule=20
(c[2][pos], i, index+1)<BR> =20
else<BR> c[1] =3D append =
(c[1],=20
l_rules[i][index])<BR> c[2] =
=3D append=20
(c[2], store_rule (EMPTY, i, index+1))<BR> =
end=20
if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> end =
if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
c<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function match_rules =
(context c,=20
query s)<BR> integer pos</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(s) do</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> =
pos =3D find=20
(s[index], c[1])<BR> if pos=20
then<BR> c =3D=20
c[2][pos]<BR> =20
else<BR> return=20
c[3]<BR> end if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> end =
for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
c[3]<BR> =20
end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New"=20
--------------------------<BR>--=20
Exportable Routines</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global function define =
(context c,=20
query l, query r)<BR> integer i</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> i =3D new =
(l,=20
r)<BR> return store_rule (c, i, 1)<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global function =
ask_query (context=20
c, query s)<BR> sequence olds<BR> integer i</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> i =3D NONE =
olds =3D=20
{}<BR> while compare (olds, s)=20
do<BR> olds =3D =
s<BR> =20
i =3D match_rules (c, s)<BR> if i=20
then<BR> s =3D r_rules[i] =
&=20
s[length(l_rules[i])+1..length(s)]<BR> =
elsif=20
length(s) > 1 then<BR> s =
=3D=20
s[2..length(s)]<BR> end =
if<BR> =20
end while</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
s<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New"=20
--------------------------<BR>--=20
Quick'n'dirty Interface</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> constant WHITESPACE =3D =
13 & 10=20
& 255 & 32<BR> sequence prefix, knowledge, lquery, =
names<BR> =20
prefix =3D {} knowledge =3D EMPTY names =3D {}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> type char (integer=20
i)<BR> return i >=3D 0 and i < 256<BR> end =
type</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> type string (sequence=20
s)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(s) do<BR> if not char( s[index] )=20
then<BR> return=20
FALSE<BR> end if<BR> end =
for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
TRUE<BR> =20
end type</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function parse (sequence =
query)<BR> sequence ret<BR> integer start, flag</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> flag =3D FALSE ret =
=3D=20
{}<BR> for index =3D 1 to length(query) =
do<BR> =20
if flag then<BR> if find =
(query[index],=20
WHITESPACE) then<BR> ret =
=3D=20
append(ret,=20
; flag=20
=3D FALSE<BR> end=20
if<BR> =
else<BR> if=20
not find (query[index], WHITESPACE)=20
then<BR> flag =3D=20
TRUE<BR> start =3D=20
index<BR> end =
if<BR> =20
end if<BR> end for<BR> if flag=20
then<BR> return append(ret,=20
query[start..length(query)])<BR> =
else<BR> =20
return ret<BR> end if<BR> end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function flatten =
(sequence s, object=20
p)<BR> sequence result</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> result =3D =
""<BR> =20
for index =3D 1 to length(s) - 1 do<BR> result =
&=3D=20
s[index] & p<BR> end for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> if not length(s) =
then return=20
"" end if</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return result =
&=20
s[length(s)]<BR> end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function to_names =
(sequence=20
x)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(x) do<BR> x[index] =3D=20
names[x[index]]<BR> end for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
x<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> function to_codes =
(sequence=20
x)<BR> integer pos</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> for index =
=3D 1 to=20
length(x) do<BR> pos =3D find( x[index], =
names=20
)<BR> if not pos=20
then<BR> names =3D append( =
names,=20
x[index] )<BR> pos =3D length( =
names=20
)<BR> end =
if<BR> =20
x[index] =3D pos<BR> end for</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> return =
x<BR> end=20
function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global procedure concept =
(sequence=20
s)<BR> prefix =3D to_codes ( parse (s) )<BR> end =
procedure</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global procedure =
top_level=20
()<BR> prefix =3D {}<BR> end =
procedure</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global procedure the =
(sequence=20
s)<BR> lquery =3D to_codes ( parse (s) )<BR> end =
procedure</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global procedure equals =
(sequence=20
s)<BR> s =3D to_codes ( parse (s) =
)<BR> =20
knowledge =3D define (knowledge, lquery, s)<BR> end =
procedure</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global procedure setto =
(object=20
x)<BR> x =3D to_codes ( {x} )<BR> =
knowledge =3D=20
define (knowledge, lquery, x)<BR> end procedure</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2> global function ask =
(sequence=20
s)<BR> return flatten(to_names (ask_query(knowledge, =
to_codes=20
(parse(s))) ), ' ')<BR> end function</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial=20
-----</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=3DArial size=3D2> Here the code ends! .. =
Greetings,=20
Ralf.</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
------=_NextPart_000_0043_01BFE179.1FCD3DA0--
|
Not Categorized, Please Help
|
|