%% Note that each query finishes by ;. Everything after semicolon %% will be ignored by the query system. %% Section 3.4.1 (Queries with declarations) %% The following query uses declared set constant BibDB %% and query getBooks in query call getBooks(BibDB). %% It extracts all books from the database. set query let set constant BibDB be http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#BibDB, set query getBooks (set input) be separate { pub-type:pub in input where pub-type='book' } in call getBooks(BibDB) endlet; %% Section 3.4.2 (Library) %% Adding and listing library queries and set constants. %% Queries and constants added by a user will be valid during the current session %% and will be forgotten when the query system is restarted. %% Some useful queries are built-in in the library. %% If some query or constant name are repeated in the library, %% the last declared will be used when called. library add set constant some_book = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#b1; library add set constant some_book = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#b2; library list; library list verbose; %% Section 3.5.1 (Example of non-well-typed query) set query collect { pub-type:pub where pub-type:pub in BibDB and exists 'refers-to':ref in pub . ref=b2 }; %% In the above query the system does not know what is BibDB. %% It should be declared like in the following example. %% Section 3.5.2 (Example of valid and executable query) %% Informally this query asks for all publications which refer to the book b2. set query let set constant BibDB be http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#BibDB, set constant b2 be http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#b2 in collect { pub-type:pub where pub-type:pub in BibDB and exists 'refers-to':ref in pub . ref=b2 } endlet; %% To understand the query result, note that in fact p3 = b2 (as sets). %% Query language Delta deals with sets, NOT with graph nodes! %% Section 3.5.3 (Restructuring query) set query let set constant BibDB = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#BibDB, set constant restructuredBibDB be (U collect{ 'null':if (L='paper' or L='book') then { 'publication':X, 'type':call Pair(call Second(X),{L:{}}), L:call Pair({L:{}}, {}) } else {L:X} fi where L:X in call CanGraph(BibDB) } ) in decorate ( restructuredBibDB, BibDB ) endlet; %% The original WDB graph is restructured as follows: %% !!!! Present here the resulting graph %% Section 3.5.4 (Horizontal transitive closure) %% The following query computes transitive closure of the graph g %% considered as a set of ordered pairs. %% See the precise set theoretic meaning of Pair(x,y) and HorizontalTC(g) in the library. set query let set constant g be { 'null':call Pair("a","b"), 'null':call Pair("b","c") } in call HorizontalTC(g) endlet; %% As the result of the above query contains many repetitions (redundancies), %% they can be removed by the library canonisation query Can: set query let set constant g be { 'null':call Pair("a","b"), 'null':call Pair("b","c") } in call Can(call HorizontalTC(g)) endlet; %% Note that Can(x) = x for any hyperset x; it does not change abstract meaning of the argument, %% but it removes redundancies in its representation by set equations. %% Section 3.5.5 (Dealing with proper hypersets) %% The primitive operator decorate (called also plan performance operator) %% assigns a hyperset to graph g node "a". set query let set constant g = { 'null':call Pair("a","b"), 'null':call Pair("b","a"), 'null':call Pair("a","c"), 'null':call Pair("a","d"), 'null':call Pair("b","d") } in decorate (g, "a") endlet; %% Unfortunately the result contains some redundancies. %% We remove them below in Section 3.5.6 %% In fact, decorate assigns the same hypersets to the nodes "a" and "b" of g. Just run boolean query let set constant g = { 'null':call Pair("a","b"), 'null':call Pair("b","a"), 'null':call Pair("a","c"), 'null':call Pair("a","d"), 'null':call Pair("b","d") } in decorate (g, "a") = decorate (g, "b") endlet; %% Section 3.5.6 (Query optimisation by removing redundancies) %% Using again canonisation library query Can removes redundancies in the result. set query let set constant g = { 'null':call Pair("a","b"), 'null':call Pair("b","a"), 'null':call Pair("a","c"), 'null':call Pair("a","d"), 'null':call Pair("b","d") } in call Can ( decorate (g, "a") ) endlet; %% Section 3.6 (Imitating path expressions) %% Informally, the following query means: %% Find all publications authored by Smith lying on a 'refers-to' path %% from b1 to b2, just one step before b2. set query let set constant BibDB = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#BibDB, set constant b1 = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#b1, set constant b2 = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#b2 in separate { pub-type:x in BibDB where exists m:y in separate {n:xx in call TC_along_label('refers-to',b1) where 'refers-to':b2 in xx } . (x=y and 'author':"Smith" in x) } endlet; %% To understand the query result, recall that p3 = b2. %% Queries in Delta language do not (and cannot!) distinguish these two nodes/sets. %% Delta is set theoretical, NOT graph theoretical language! %% Section 3.7 (Linear ordering query) %% This query (having mainly a theoretical value) calls two library queries %% SuccessorPairs and StrictLinOrder_on_TC where the latter orders %% in a "lexicographical" way all sets/graph nodes from the database %% (in fact, from transitive closure of BibDB). %% Note that particular set names (node names) play no role in the ordering. %% They could be renamed. %% Only set theoretical nested structure of the database is used, %% as well as particular label values (strings of symbols). %% Please, be patient. It takes several minutes! set query let set constant BibDB = http://www.csc.liv.ac.uk/~molyneux/t/BibDB-f1.xml#BibDB in call SuccessorPairs( call StrictLinOrder_on_TC(BibDB) ) endlet; %% The resulting ordering (represented as a set of "successor" ordered pairs) is: %% {}, "Databases", "Jones", "Smith", BibDB, p1, b1, b2/p3, p2. %% It is advised to look at least at the meaning of SuccessorPairs in the library.