/*

  Starting to build some prolog bits to support semantic web hacking

  This is my first day of writing in prolog since 1984, I think.
                                                         -- sandro

  $Id: lib.P,v 1.1.1.1 2001/04/24 19:25:50 sandro Exp $

  Using XSB 2.3


  triple(subject, predicate, object).
  model(model, subject, predicate, object).

  atom 'default_model' joins the two?

*/

/* :- auto_table. */

%% :- dynamic(triple/3).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Load, Unload, Dump
%%

%%  the same as consult(File, []).  just for kicks (and retract).
assert_file(File):-
  repeat,
  read(File, X), 
  assertz(X),
  X = end_of_file.

retract_file(File):-
  repeat,
  read(File, X), 
  retract(X),
  X = end_of_file.

dump_triples :-
  findall([A,B,C], triple(A,B,C), L),
  show_list(L).

dm :-
  findall([A,B,C,D], model(A,B,C,D), L),
  show_list(L).

show_list([H|T]) :-
  writeq(H), nl, show_list(T).

show_list([]) :- true.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Extract Model info from flattened n3
%%

:- auto_table.

model(Inner, S, P, V) :- 
   model(M, Inner, 'STMTLIST', L),
   list_contains(M, L, ST),
   model(M, ST, 'rdf:subject', S),
   model(M, ST, 'rdf:predicate', P),
   model(M, ST, 'rdf:value', V).
   

list_contains(M, L, N) :-
   model(M, L, 'daml:head', N).

list_contains(M, L, N) :-
   model(M, L, 'daml:tail', SUBLIST),
   list_contains(M, SUBLIST, N).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Extract default Model info from all triples
%%

:- table(triple/3).

model(default_model,A,B,C) :- triple(A,B,C).

supermodel(Inner, Outer) :-
  Inner = Outer.

supermode(Inner, Outer) :-
  model(Outer, Inner, 'STMTLIST', L).

strictlysupermodel(Inner, Outer) :-
  model(Outer, Inner, 'STMTLIST', L).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Look for n3 logic structures
%%

timbl(X,Y) :-
  str_cat('http://www.w3.org/2000/10/swap/log.n3#',X,Y).

timbl(X,Y) :-       % because sometimes the % gets left off!
  str_cat('http://www.w3.org/2000/10/swap/log.n3',X,Y).

%% forSome
%% forAll
%% Truth
%% Falsehood
%% implies

%  X is a universal variable in model M if...
%  (1) in M (or some surrounding context) it's been declared
%      forall that context
univar(X,M) :- 
   timbl('forAll', FA),
   supermodel(M, Outer),
   model(Outer, '', FA, X).

%  (2) in some surrounding context, it's been declared as 
%      forall this model
univar(X,M) :-
   timbl('forAll', FA),
   supermodel(M, Outer),
   model(Outer, M, FA, X).

rule(Outer, If, Then, Vars) :-
   timbl(implies, IMPLIES),
   strictlysupermodel(If, Outer),
   strictlysupermodel(Then, Outer),
   model(Outer, If, IMPLIES, Then).
   % vars is all the terms which are variable in those models....?


   
   
