Re: newbie prolog question, passing functions
- From: Duncan Patton <campbell@xxxxxxxxxx>
- Date: Tue, 26 Jul 2005 16:57:58 GMT
On 26 Jul 2005 07:06:10 -0700
"mans" <mshapshak@xxxxxxxxx> wrote:
> Dear All,
>
> Very interesting material here and I would just like to sum up my
> understanding:
>
> - Everybody seems to be using a event que/dispatch model for the event
> processing to minimize the need for global variables .ie in the case of
> >>read(Event)<< from the dispatch part of the code we are accessing a global event que associated with the input buffer. Nobody seemed to confortable on calling more comlicated glocal variable structures from an direct event driven interface.
>
> - From my naive viewpoint it looks like everybody is using procedural
> conceptual model to do the coding of event driven models. Logical or
> natural language is not really used.
>
This is true of the hacks here like myself who do executeable specs and RAD prototypes.
The comp-sci academic guys tend to have more time and resources to devote to more elegant models.
> - One needs to be carefull of memory overflow and maybe event some
> garbage collection issues when doing the infinite recursive
> repeat_forever dispatch loop.
>
> Did I get some of this right?
>
Ya, I'd say it's a fairly good short-take. My own experience with Prolog
is that while it may do many neat "AI"-oriented things, it also makes a
good shovel: most of my prolog programming is _very_ plain stuff you
could do easily in C or assembly.
The fact that you can mix in bits of real declarative programming into
a procedural model makes it ideal for writing muli-language tools and
systems. As an example, here is a bit of code that provides a
general MySQL/MSSQL interface to Gprolog.
This is actually a pretty ugly bit of code, breaking a number of
programming precepts including the use of tightly-coupled co-routines,
which is exactly what this is and what makes it useful...
Dhu
/*
sqndhu.pl -- access to MSSQL and MYSQL for gprolog via php(CLI)/TDS
should work for any SQL access provided by PHP ;-)
only tested to date on a GPG/PHP/Apache/BSD box talking to MySQL on same and MSSQL on a Windos 2k box.
Duncan Patton a Campbell can be contacted at campbell@xxxxxxxxxx, or campbell@xxxxxxxx
Copyright (C) <2004> <Duncan Patton a Campbell aka Duncan Duibh>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
%MSSQL/192.168.0.22:1433/esdev2/!esdev2!/dbES_qa/
%skin_dhu(["MSSQL","192.168.0.22:1433","username","userpass","dbainstance"],["select count(*) from account_leads","select count(*) from contact_leads"],XLL).
% --> XLL = [[[[53,49,48]]],[[[53,55,49]]]]
%skin_dhu(["MYSQL","localhost:3306","username","userpass","dbainstance"],["select count(*) from urldex","select count(*) from emldex","select count(*) from rhosts"],XLL).
skin_dhu([DBTypS,HnpS,DbUnmS,DbUpsS,DbNmS],SQLstrL,XLL):-
penditaLL([DBTypS,HnpS,DbUnmS,DbUpsS,DbNmS],"/",AccS),atom_codes(AccA,AccS),
exec('/usr/local/bin/php -f /usr/local/gprolog-1.2.16/Dlib/sqndhu.php',Sin,Sout,_),
nl,write(Sin,AccA),nl(Sin),
writnread(Sin,Sout,SQLstrL,XLL),
close(Sin),close(Sout).
writnread(_,_,[],[]):-!.
writnread(Sin,Sout,[SqS|SQLstrL],[XL|XLL]):-!,
atom_codes(SqA,SqS),
nl,write(Sin,SqA),nl(Sin),
set_stream_type(Sout,binary),
readB2(Sout,[10],XYZ), %eat the first nl
readB2(Sout,[10,13],NrwS0),
str_X_lst(NrwS0,[10],[NrwS|_]),
%atom_codes(NA,NrwS),nl,write(['skin_dhu NrwS',NA]),
null_number_codes(NrwN,NrwS),
readrowS(Sout,NrwN,XL),
writnread(Sin,Sout,SQLstrL,XLL).
readrowS(_,0,[]):-!.
readrowS(Sout,NrwN,[CL|RLL]):-!,
repeat,
readB2(Sout,[10],NfldS0), %eat nl until a number comes up
str_X_lst(NfldS0,[10],[NfldS|_]),!,
null_number_codes(NfldN,NfldS),
readcolS(Sout,NfldN,CL),
NrwN_1 is NrwN - 1,
readrowS(Sout,NrwN_1,RLL).
readcolS(_,0,[]):-!.
readcolS(Sout,NfldN,[CS|CL]):-!,
readB2(Sout,[32],FlnS0),str_X_lst(FlnS0,[10,32],[FlnS|_]),
%atom_codes(FA,FlnS),nl,write(['skin_dhu FlnS',FA]),
null_number_codes(FlnN,FlnS),
readNbytes(Sout,FlnN,CS),
NfldN_1 is NfldN - 1,
readcolS(Sout,NfldN_1,CL).
readNbytes(_,0,[]):-!.
readNbytes(Sout,FlnN,[ChB|CS]):-!,
get_byte(Sout,ChB),
FlnN_1 is FlnN - 1,
readNbytes(Sout,FlnN_1,CS).
penditaLL([X],_,X):-!.
penditaLL([],_,[]):-!.
penditaLL([A|AtmLst],Sp,Catom):-!,
penditaLL(AtmLst,Sp,Catom0),!,
append(A,Sp,A1),append(A1,Catom0,Catom).
null_number_codes(0,[]):-!.
null_number_codes(N,C):- subset(C,".0123456789"),!,number_codes(N,C).
null_number_codes(_,C):-!,nl,write(['NC',C]),!,fail.
/*********************
Duncan Patton a Campbell can be contacted at campbell@xxxxxxxxxx, or campbell@xxxxxxxx
Tight coupling with sqNdhu.php -- general purpose access to SQL databases ... here implemented for commandline operations
php -f sqndhu.php
line one on stdin == MSSQL/MSHOST:1433/Uname/Upass/Dbname/
line one on stdin == MYSQL/MYHOST:3306/Uname/Upass/Dbname/
line two thru n-1 == select/insert/replace...
line n == \n
uses http://www.freetds.org/ libraries
*//*
<?php
$stdinline = trim(fgets(STDIN));
list($stype,$hnp,$dbu,$dbp,$dbn) = split("/", $stdinline) ;
switch ($stype)
{
case "MSSQL":
{
$db = mssql_connect($hnp,$dbu,$dbp);
if(!$db)
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not connect"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not connect");
}
if(!mssql_select_db($dbn,$db))
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not select database"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not select database");
}
while($squery= trim(fgets(STDIN)))
{
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "MySqery:".$squery); fwrite($handle, "\n\n"); fclose($handle);
$result = mssql_query($squery) or die("Query failed : " . $squery);
if(is_resource($result))
{
$num_results = mssql_num_rows($result);
echo "\n" . $num_results;
if($num_results == 0) echo "\n";
for ($i=0; $i <$num_results; $i++)
{
while($myrow = mssql_fetch_row($result))
{
$colcnt = count($myrow);
echo "\n" . $colcnt;
for($x=0; $x < $colcnt; $x++)
{
echo sprintf("\n%d ",strlen($myrow[$x])) . $myrow[$x];
}
}
}
mssql_free_result($result);
}
else
{
echo "\n1\n1\n1 !";
}
}
mssql_close($db);
}
break;
case "MYSQL":
{
$db = mysql_connect($hnp,$dbu,$dbp);
if(!$db)
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not connect"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not connect");
}
if(!mysql_select_db($dbn,$db))
{
$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "Could not select database"); fwrite($handle, "\n\n"); fclose($handle);
exit("Could not select database");
}
while($squery= trim(fgets(STDIN)))
{
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "MySqery:".$squery); fwrite($handle, "\n\n"); fclose($handle);
$result = mysql_query($squery) or die("Query failed : " . $squery);
if(is_resource($result))
{
$num_results = mysql_num_rows($result);
echo "\n" . $num_results;
if($num_results == 0) echo "\n";
for ($i=0; $i <$num_results; $i++)
{
while($myrow = mysql_fetch_row($result))
{
$colcnt = count($myrow);
echo "\n" . $colcnt;
for($x=0; $x < $colcnt; $x++)
{
echo sprintf("\n%d ",strlen($myrow[$x])) . $myrow[$x];
//$handle=fopen('/tmp/perror.err', 'a');fwrite($handle, "."); fwrite($handle, "-"); fclose($handle);
}
}
}
mysql_free_result($result);
}
else
{
echo "\n1\n1\n1 !";
}
}
mysql_close($db);
}
break;
} //end switch
fflush(STDOUT);
exit();
?>
*********************/
> Thanks Again!
> Mans
>
--
???????????????????????????????????????
Can't get good help?
Contact Fubar the Hack: fubar AT neotext.ca
Area code seven eight zero, Exchange four six six, Local zero one zero nine
Highland terms, Canadian workmanship.
All persons named herein are purely fictional victims
of the Canidian Bagle Breeder's Association.
Save the Bagle!
Sun Ðhu
???????????????????????????????????????
.
- Follow-Ups:
- Re: newbie prolog question, passing functions
- From: Duncan Patton
- Re: newbie prolog question, passing functions
- From: Duncan Patton
- Re: newbie prolog question, passing functions
- References:
- newbie prolog question, passing functions
- From: mans
- Re: newbie prolog question, passing functions
- From: mans
- newbie prolog question, passing functions
- Prev by Date: Re: newbie prolog question, passing functions
- Next by Date: Re: 用TP20, VP51生花, 一切出於六合彩開始
- Previous by thread: Re: newbie prolog question, passing functions
- Next by thread: Re: newbie prolog question, passing functions
- Index(es):
Relevant Pages
|