Andrew Cooke | Contents | Latest | RSS | Previous | Next

C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

Personal Projects

Choochoo Training Diary

Last 100 entries

[Programming] React Leaflet; AliExpress Independent Sellers; Applebaum - Twilight of Democracy; [Politics] Back + US Elections; [Programming,Exercise] Simple Timer Script; [News] 2019: The year revolt went global; [Politics] The world's most-surveilled cities; [Bike] Hope Freehub; [Restaurant] Mama Chau's (Chinese, Providencia); [Politics] Brexit Podcast; [Diary] Pneumonia; [Politics] Britain's Reichstag Fire moment; install cairo; [Programming] GCC Sanitizer Flags; [GPU, Programming] Per-Thread Program Counters; My Bike Accident - Looking Back One Year; [Python] Geographic heights are incredibly easy!; [Cooking] Cookie Recipe; Efficient, Simple, Directed Maximisation of Noisy Function; And for argparse; Bash Completion in Python; [Computing] Configuring Github Jekyll Locally; [Maths, Link] The Napkin Project; You can Masquerade in Firewalld; [Bike] Servicing Budget (Spring) Forks; [Crypto] CIA Internet Comms Failure; [Python] Cute Rate Limiting API; [Causality] Judea Pearl Lecture; [Security, Computing] Chinese Hardware Hack Of Supermicro Boards; SQLAlchemy Joined Table Inheritance and Delete Cascade; [Translation] The Club; [Computing] Super Potato Bruh; [Computing] Extending Jupyter; Further HRM Details; [Computing, Bike] Activities in ch2; [Books, Link] Modern Japanese Lit; What ended up there; [Link, Book] Logic Book; Update - Garmin Express / Connect; Garmin Forerunner 35 v 230; [Link, Politics, Internet] Government Trolls; [Link, Politics] Why identity politics benefits the right more than the left; SSH Forwarding; A Specification For Repeating Events; A Fight for the Soul of Science; [Science, Book, Link] Lost In Math; OpenSuse Leap 15 Network Fixes; Update; [Book] Galileo's Middle Finger; [Bike] Chinese Carbon Rims; [Bike] Servicing Shimano XT Front Hub HB-M8010; [Bike] Aliexpress Cycling Tops; [Computing] Change to ssh handling of multiple identities?; [Bike] Endura Hummvee Lite II; [Computing] Marble Based Logic; [Link, Politics] Sanity Check For Nuclear Launch; [Link, Science] Entropy and Life; [Link, Bike] Cheap Cycling Jerseys; [Link, Music] Music To Steal 2017; [Link, Future] Simulated Brain Drives Robot; [Link, Computing] Learned Index Structures; Solo Air Equalization; Update: Higher Pressures; Psychology; [Bike] Exercise And Fuel; Continental Race King 2.2; Removing Lowers; Mnesiacs; [Maths, Link] Dividing By Zero; [Book, Review] Ray Monk - Ludwig Wittgenstein: The Duty Of Genius; [Link, Bike, Computing] Evolving Lacing Patterns; [Jam] Strawberry and Orange Jam; [Chile, Privacy] Biometric Check During Mail Delivery; [Link, Chile, Spanish] Article on the Chilean Drought; [Bike] Extended Gear Ratios, Shimano XT M8000 (24/36 Chainring); [Link, Politics, USA] The Future Of American Democracy; Mass Hysteria; [Review, Books, Links] Kazuo Ishiguro - Never Let Me Go; [Link, Books] David Mitchell's Favourite Japanese Fiction; [Link, Bike] Rear Suspension Geometry; [Link, Cycling, Art] Strava Artwork; [Link, Computing] Useful gcc flags; [Link] Voynich Manuscript Decoded; [Bike] Notes on Servicing Suspension Forks; [Links, Computing] Snap, Flatpack, Appimage; [Link, Computing] Oracle is leaving Java (to die); [Link, Politics] Cubans + Ultrasonics; [Book, Link] Laurent Binet; VirtualBox; [Book, Link] No One's Ways; [Link] The Biggest Problem For Cyclists Is Bad Driving; [Computing] Doxygen, Sphinx, Breathe; [Admin] Brokw Recent Permalinks; [Bike, Chile] Buying Bearings in Santiago; [Computing, Opensuse] Upgrading to 42.3; [Link, Physics] First Support for a Physics Theory of Life; [Link, Bike] Peruvian Frame Maker; [Link] Awesome Game Theory Tit-For-Tat Thing; [Food, Review] La Fabbrica - Good Italian Food In Santiago; [Link, Programming] MySQL UTF8 Broken; [Link, Books] Latin American Authors

© 2006-2017 Andrew Cooke (site) / post authors (content).

Datalog, DLV, SQL

From: "andrew cooke" <andrew@...>

Date: Mon, 25 Dec 2006 13:35:17 -0300 (CLST)

I've been experimenting with Datalog to understand the messages in the NSA
(the SOA system at work).

Datalog - http://en.wikipedia.org/wiki/Datalog
DLV - http://www.dbai.tuwien.ac.at/proj/dlv/

At first I had some problems, because I couldn't define the constraints I
wanted (I think it's because, to avoid the inconsistencies you get with
recursive sets, negation is stratified, but I'm not clear on exactly what
was wrong with my particular formulation).  See
http://groups.google.com/group/comp.lang.prolog/browse_thread/thread/7114a8cb8a095aa1
http://groups.google.com/group/comp.databases.theory/browse_thread/thread/7114a8cb8a095aa1

I got round that be putting everything in an SQL database and using
foreign key constraints (and it was worthwhile - I did have a bunch of
errors in my input).

Then I configured ODBC for Postgres.  This turned out to be very simple:

[msg]
Driver = /usr/lib/psqlodbc.so
Description = NS Message Description
Servername = localhost
Port = 5432
Database = msg

(test this with "isql msg")

Once that was done, I could read the information into DLV as clauses:

#import(msg, "", "", "select name, description from applications",
application, type: CONST, Q_CONST).
#import(msg, "", "", "select service, name, description from methods",
method, type: CONST, CONST, Q_CONST).
#import(msg, "", "", "select name, description from processes", process,
type: CONST, Q_CONST).
#import(msg, "", "", "select process, name, source, destination,
description from calls", call,
type: CONST, CONST, CONST, CONST, Q_CONST).
#import(msg, "", "", "select process, call from returns", return, type:
CONST, CONST).
#import(msg, "", "", "select process, caller, target from nesteds",
nested, type: CONST, CONST, CONST).
#import(msg, "", "", "select process, before, after from follows", follow,
type: CONST, CONST, CONST).

and, finally, define extra rules to analyse the data.  OK, so nothing very
exciting yet (need more data on deployment etc), but this gives the idea:

synch(F, T) :- call(P, C, F, T, _), return(P, C).
asynch(F, T) :- call(P, C, F, T, _), not return(P, C).
mixed(F, T) :- synch(F, T), asynch(F, T).

connected(F1, T1, F2, T2) :- call(P, _, F1, T1, _), call(P, _, F2, T2, _).
ordered(F1, T1, F2, T2) :- call(P, B, F1, T1, _), call(P, A, F2, T2, _),
follow(P, B, A).
ordered(F1, T1, F2, T2) :- call(P, B, F1, T1, _), call(P, A, F2, T2, _),
nested(P, B, A).
ordered(F1, T1, F2, T2) :- ordered(F1, T1, F, T), ordered(F, T, F2, T2).

In particular, note the "ordered" definition, which is horrible to do in
plain old SQL (pre-SQL3) because it's recursive.  A typical fragment of
output is:
  ordered(unp,usr_unreg,usr,mds_persist),...
which means that the UNP calls the User Service before the User service
calls the metadata service.

So I have an "expert system" that "knows" about the messages we send.

Sweet! :o)
Andrew


SQL Input:

-- these are the facts we know about the messages and system we have

-- this used dlv (datalog), but it's essentially the same as sql -
-- think of the declarations below as sql stored procedures that enter
-- the data in suitable tables.


-- applications (services etc)

select application('aud', 'Audit Service');
select application('ath', 'Authorization Service');
select application('cfg', 'Configuration Service');
select application('des', 'Data Entry Service');
select application('dls', 'Data Load Service');
select application('ds',  'Data Service');
select application('fis', 'File Information Service');
select application('mds', 'MetaData Service');
select application('nmg', 'Naming Service');
select application('ptl', 'Portal');
select application('reg', 'Registration Service');
select application('unp', 'User Notification Program');
select application('usr', 'User Service');


-- methods (actions provided by service interfaces)

select method('aud', 'aud_event',    'registerEvent');
select method('aud', 'aud_visit',    'registerVisit');
select method('cfg', 'cfg_getcnf',   'getConfiguration');
select method('dls', 'dls_load',     'load');
select method('ds',  'ds_store',     'store');
select method('fis', 'fis_regfile',  'registerFileInformation');
select method('mds', 'mds_getmo',    'getMetadataObject');
select method('mds', 'mds_getmos',   'getMetadataObjects');
select method('mds', 'mds_persist',  'persistMetadataObjects');
select method('nmg', 'nmg_newname',  'getNewName');
select method('reg', 'reg_invite',   'inviteNewUser');
select method('reg', 'reg_tokinfo',  'getUserInfo');
select method('reg', 'reg_validate', 'validateUser');
select method('usr', 'usr_getinfo',  'getUserInfo');
select method('usr', 'usr_setinfo',  'setUserInfo');
select method('usr', 'usr_unreg',    'getUnregisteredPis');


-- processes (use cases that capture a description of the working system)

select process('p_dsinit',  'Data Service startup uses the configuration
service');
select process('p_des',     'Data enters the system');
select process('p_unp',     'UNP used to trigger the invitation of new
users');
select process('p_reg',     'User registers via portal');
select process('p_downld',  'User/portal downloads (possibly proprietary)
data');
select process('p_skynode', 'User/portal runs ADQL/SkyNode query');


-- here we describe how applications call each other.
-- for each process, we list the calls and describe the dependencies
-- (nested or follow) and whether the calls return.
-- note that within a process each message name should be unique.


-- messages for p_dsinit

select call('p_dsinit', 'ds_cfg_getcnf', 'ds', 'cfg_getcnf',
'configuration data for ds');
select return('p_dsinit', 'ds_cfg_getcnf');

select call('p_dsinit', 'cfg_ms_getmo', 'cfg', 'mds_getmo', 'configuration
data for ds');
select return('p_dsinit', 'cfg_ms_getmo');
select nested('p_dsinit', 'ds_cfg_getcnf', 'cfg_ms_getmo');


-- messages for p_des

-- probably missing calls' at start (what triggers des?)
select call('p_des', 'des_nmg_newname', 'des', 'nmg_newname', 'new name
for new data');
select return('p_des', 'des_nmg_newname');

select call('p_des', 'des_fis_regfile', 'des', 'fis_regfile', 'register
new data');
select return('p_des', 'des_fis_regfile');
select follow('p_des', 'des_nmg_newname', 'des_fis_regfile');

-- nested 'call 'to MDS here?

select call('p_des', 'des_ds_store', 'des', 'ds_store', 'store new data');
select return('p_des', 'des_ds_store');
select follow('p_des', 'des_fis_regfile', 'des_ds_store');

select call('p_des', 'ds_aud_visit', 'ds', 'aud_visit', 'audit visit to
new data');
select return('p_des', 'ds_aud_visit');
select nested('p_des', 'des_ds_store', 'ds_aud_visit');

select call('p_des', 'des_dls_load', 'des', 'dls_load', 'load new data
into database');
select return('p_des', 'des_dls_load');
select follow('p_des', 'des_ds_store', 'des_dls_load');

select call('p_des', 'des_aud_event', 'des', 'aud_event', 'audit
successful data entry');
select return('p_des', 'des_aud_event');
select follow('p_des', 'des_dls_load', 'des_aud_event');


-- messages for p_unp

select call('p_unp', 'unp_usr_unreg', 'unp', 'usr_unreg', 'list
unregistered pis');
select return('p_unp', 'unp_usr_unreg');

select call('p_unp', 'usr_mds_getmos', 'usr', 'mds_getmos', 'get all pis');
select return('p_unp', 'usr_mds_getmos');
select nested('p_unp', 'unp_usr_unreg', 'usr_mds_getmos');

select call('p_unp', 'unp_reg_invite', 'unp', 'reg_invite', 'invite new pi');
select return('p_unp', 'unp_reg_invite');
select follow('p_unp', 'usr_mds_getmos', 'unp_reg_invite');

select call('p_unp', 'reg_usr_getinfo', 'reg', 'usr_getinfo', 'check pi
uninvited');
select return('p_unp', 'reg_usr_getinfo');
select nested('p_unp', 'unp_reg_invite', 'reg_usr_getinfo');

select call('p_unp', 'reg_usr_setinfo_1', 'reg', 'usr_setinfo', 'set pi
being emailed');
select return('p_unp', 'reg_usr_setinfo_1');
select follow('p_unp', 'reg_usr_getinfo', 'reg_usr_setinfo_1');

select call('p_unp', 'usr_mds_persist_1', 'usr', 'mds_persist', 'set pi
being emailed');
select return('p_unp', 'usr_mds_persist_1');
select nested('p_unp', 'reg_usr_setinfo_1', 'usr_mds_persist_1');

-- mail sent here

select call('p_unp', 'reg_usr_setinfo_2', 'reg', 'usr_setinfo', 'set pi
invited');
select return('p_unp', 'reg_usr_setinfo_2');
select follow('p_unp', 'reg_usr_getinfo', 'reg_usr_setinfo_2');

select call('p_unp', 'usr_mds_persist_2', 'usr', 'mds_persist', 'set pi
invited');
select return('p_unp', 'usr_mds_persist_2');
select nested('p_unp', 'reg_usr_setinfo_2', 'usr_mds_persist_2');


-- messages for p_reg

select call('p_reg', 'ptl_reg_tokinfo', 'ptl', 'reg_tokinfo', 'request
info for token');
select return('p_reg', 'ptl_reg_tokinfo');

select call('p_reg', 'reg_usr_getinfo_1', 'reg', 'usr_getinfo', 'request
info for token');
select return('p_reg', 'reg_usr_getinfo_1');
select nested('p_reg', 'ptl_reg_tokinfo', 'reg_usr_getinfo_1');

select call('p_reg', 'usr_mds_getmos', 'usr', 'mds_getmos', 'get all user
info');
select return('p_reg', 'usr_mds_getmos');
select nested('p_reg', 'reg_usr_getinfo_1', 'usr_mds_getmos');

select call('p_reg', 'ptl_reg_validate', 'ptl', 'reg_validate', 'validate
user, provide dn');
select return('p_reg', 'ptl_reg_validate');
select follow('p_reg', 'ptl_reg_tokinfo', 'ptl_reg_validate');

select call('p_reg', 'reg_usr_getinfo_2', 'reg', 'usr_getinfo', 'request
info for token');
select return('p_reg', 'reg_usr_getinfo_2');
select nested('p_reg', 'ptl_reg_validate', 'reg_usr_getinfo_2');

select call('p_reg', 'reg_usr_setinfo', 'reg', 'usr_setinfo', 'set pi dn,
registered');
select return('p_reg', 'reg_usr_setinfo');
select follow('p_reg', 'reg_usr_getinfo_2', 'reg_usr_setinfo');


-- messages for p_downld

-- missing calls at start
select call('p_downld', 'ath_mds_getmos', 'ath', 'mds_getmos', 'get all
file info');
select return('p_downld', 'ath_mds_getmos');


SQL Schema:


-- applications

create table applications (
  name text unique not null,
  description text not null
);

create function application(nm text, ds text) returns void as $$
begin
  insert into applications (name, description) values (nm, ds);
end;
$$ language plpgsql;


-- methods

create table methods (
  service text not null references applications(name),
  name text unique not null,
  description text not null
);

create function method(ap text, nm text, ds text) returns void as $$
begin
  insert into methods (service, name, description) values (ap, nm, ds);
end;
$$ language plpgsql;


-- processes

create table processes (
  name text unique not null,
  description text not null
);

create function process(nm text, ds text) returns void as $$
begin
  insert into processes (name, description) values (nm, ds);
end;
$$ language plpgsql;



-- calls

create table calls (
  process text not null references processes(name),
  name text not null,
  source text not null references applications(name),
  destination text not null references methods(name),
  description text not null,
  unique (process, name)
);

create function call(pr text, nm text, sr text, de text, ds text) returns
void as $$
begin
  insert into calls (process, name, source, destination, description)
values (pr, nm, sr, de, ds);
end;
$$ language plpgsql;


create table returns (
  process text not null references processes(name),
  call text not null,
  foreign key (process, call) references calls(process, name)
);

create function return(pr text, cl text) returns void as $$
begin
  insert into returns (process, call) values (pr, cl);
end;
$$ language plpgsql;

create table nesteds (
  process text not null references processes(name),
  caller text not null,
  target text not null,
  foreign key (process, caller) references calls(process, name),
  foreign key (process, target) references calls(process, name)
);

create function nested(pr text, cr text, tg text) returns void as $$
begin
  insert into nesteds (process, caller, target) values (pr, cr, tg);
end;
$$ language plpgsql;

create table follows (
  process text not null references processes(name),
  before text not null,
  after text not null,
  foreign key (process, before) references calls(process, name),
  foreign key (process, after) references calls(process, name)
);

create function follow(pr text, bf text, af text) returns void as $$
begin
  insert into follows (process, before, after) values (pr, bf, af);
end;
$$ language plpgsql;

Prolog / XSB book, possible explanation of "unsafe" problem

From: "andrew cooke" <andrew@...>

Date: Mon, 25 Dec 2006 21:34:09 -0300 (CLST)

I just found this draft of a book by Warren -
http://www.cs.sunysb.edu/~warren/xsbbook/book.html - and the introduction
to Prolog chapter is particularly good (I need to look again at Oz,
because I somehoe missed how closely Oz was connected to Prolog; but I
think I also need to look at Prolog, or perhaps XSB, in more detail too).

Anyway, there's a comment here -
http://www.cs.sunysb.edu/~warren/xsbbook/node58.html - that explains the
problem with negating an expression with free terms.  I think that may be
the problem I rant into with DLV.

XSB, incidentally, is at http://xsb.sourceforge.net

Andrew

More on Logic Programming

From: "andrew cooke" <andrew@...>

Date: Tue, 26 Dec 2006 10:51:08 -0300 (CLST)

A nice email from one of the people who developed DLV explained the
problem I had with my constraint
  :- process(P, _), not call(P, _, _, _, _).
which is clearer if written as
  :- process(P, _), not call(P, A, B, C, D).
and none of A-D are grounded (so it's not clear what you are negating).

A better approach is to be explicit:
  process_id(P) :- process(P, _).
  :- call(P, _, _, _, _), not process_id(P).

Also, I did some reading around on Mercury and Oz

Mercury tutorial -
http://www.cs.mu.oz.au/research/mercury/tutorial/book/book.pdf

Logic programming in OZ -
http://www.ps.uni-sb.de/Papers/abstracts/lpinoz99.ps

Andrew

Datalog Jobs

From: "andrew cooke" <andrew@...>

Date: Mon, 25 May 2009 21:10:11 -0400 (CLT)

Wonder if this is going to make a comeback?

http://www.haskell.org/pipermail/haskell/2009-May/021346.html

Andrew

Comment on this post