* Finished checker
authorJames Cheney <jcheney@inf.ed.ac.uk>
Thu, 31 Jan 2013 17:03:10 +0000
changeset 5468 c09a6121c6ab
parent 5467 631ce28288c9
child 5469 cd0dac47a48c
* Finished checker
* Added Stephen Cresswell's xml parsing code
semantics/checker.pl
semantics/prov_xml.pl
semantics/prov_xml_test.pl
--- a/semantics/checker.pl	Thu Jan 31 14:34:12 2013 +0000
+++ b/semantics/checker.pl	Thu Jan 31 17:03:10 2013 +0000
@@ -424,7 +424,7 @@
 normalize(I,K) :- infer(I,J), !, normalize(J,K).
 
 
-constrain(P,O1,O2) :- \+(member(precedes(Start,End),O1)),
+constrain(P,O1,O2) :- \+(member(P,O1)),
 	O2 = [P|O1].
 
 
@@ -433,7 +433,7 @@
 ordering_step(I,O1,O2) :-
 	member(wasStartedBy(Start,A,_E1,_A1,_T1,_Attrs1),I),
 	member(wasEndedBy(End,A,_E2,_A2,_T2,_Attrs2),I),
-	constrain(precedes(Start1,Start2),O1,O2).
+	constrain(precedes(Start,End),O1,O2).
 
 %Constraint 31
 ordering_step(I,O1,O2) :-
@@ -494,7 +494,127 @@
 ordering_step(I,O1,O2) :-
 	member(used(Use,_A1,E,_T1,_Attrs1),I),
 	member(wasInvalidatedBy(Inv,E,_A2,_T2,_Attrs2),I),
-	constrain(precedes(Gen,Inv),O1,O2).
+	constrain(precedes(Use,Inv),O1,O2).
+
+% Constraint 39
+ordering_step(I,O1,O2) :-
+	member(wasGeneratedBy(Gen1, E,_A1,_T1,_Attrs1),I),  
+	member(wasGeneratedBy(Gen2, E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(Gen1,Gen2),O1,O2).
+
+%Constraint 40
+ordering_step(I,O1,O2) :-
+	member(wasInvalidatedBy(Inv1, E,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv2, E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(Inv1,Inv2),O1,O2).
+
+
+% Constraint 41
+ordering_step(I,O1,O2) :-
+	member(wasDerivedFrom(_ID, _E2,_E1,A,Gen,Use,_Attrs),I),
+	A \== null, Gen \== null, Use \== null,
+	constrain(precedes(Use,Gen),O1,O2).
+
+% Constraint 42
+ordering_step(I,O1,O2) :-
+	member(wasDerivedFrom(_ID, E2,E1,_A,_Gen,_Use,_Attrs),I),
+	member(wasGeneratedBy(Gen1, E1,_A1,_T1,_Attrs1),I),  
+	member(wasGeneratedBy(Gen2, E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Gen1,Gen2),O1,O2).
+
+%Constraint 43.1
+ordering_step(I,O1,O2) :-
+	member(wasGeneratedBy(Gen, E,_A1,_T1,_Attrs1),I),  
+	member(wasStartedBy(Start, _A,E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(Gen,Start),O1,O2).
+
+%Constraint 43.2
+ordering_step(I,O1,O2) :-
+	member(wasStartedBy(Start, _A,E,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv, E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(Start,Inv),O1,O2).
+
+%Constraint 44.1
+ordering_step(I,O1,O2) :-
+	member(wasGeneratedBy(Gen, E,_A1,_T1,_Attrs1),I),  
+	member(wasEndedBy(End, _A,E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(Gen,End),O1,O2).
+
+%Constraint 44.2
+ordering_step(I,O1,O2) :-
+	member(wasEndedBy(End, _A,E,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv, E,_A2,_T2,_Attrs2),I),
+	constrain(precedes(End,Inv),O1,O2).
+
+%Constraint 45
+ordering_step(I,O1,O2) :-
+	member(specializationOf(E2,E1),I),  
+	member(wasGeneratedBy(Gen1, E1,_A1,_T1,_Attrs1),I),  
+	member(wasGeneratedBy(Gen2, E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Gen1,Gen2),O1,O2).
+
+%Constraint 46
+ordering_step(I,O1,O2) :-
+	member(specializationOf(E1,E2),I),  
+	member(wasInvalidatedBy(Inv1, E1,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv2, E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Inv1,Inv2),O1,O2).
+
+% Constraint 47.1
+ordering_step(I,O1,O2) :-
+	member(wasAssociatedWith(_Assoc,A,Ag,_Pl,_Attrs),I),  
+	member(wasStartedBy(Start1, A,_E1,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv2, Ag,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Start1,Inv2),O1,O2).
+
+% Constraint 47.2
+ordering_step(I,O1,O2) :-
+	member(wasAssociatedWith(_Assoc,_A,Ag,_Pl,_Attrs),I),  
+	member(wasGeneratedBy(Gen1, Ag,_A1,_T1,_Attrs1),I),  
+	member(wasEndedBy(End2, Ag,_E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Gen1,End2),O1,O2).
+
+% Constraint 47.3
+ordering_step(I,O1,O2) :-
+	member(wasAssociatedWith(_Assoc,A,Ag,_Pl,_Attrs),I),  
+	member(wasStartedBy(Start1, A,_E1,_A1,_T1,_Attrs1),I),  
+	member(wasEndedBy(End2, Ag,_E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Start1,End2),O1,O2).
+
+% Constraint 47.4
+ordering_step(I,O1,O2) :-
+	member(wasAssociatedWith(_Assoc,A,Ag,_Pl,_Attrs),I),  
+	member(wasStartedBy(Start1, Ag,_E1,_A1,_T1,_Attrs1),I),  
+	member(wasEndedBy(End2, A,_E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Start1,End2),O1,O2).
+
+% Constraint 48.1
+ordering_step(I,O1,O2) :-
+	member(wasAttributedTo(_Att,E,Ag,_Attrs),I),  
+	member(wasGeneratedBy(Gen1, Ag,_A1,_T1,_Attrs1),I),  
+	member(wasGeneratedBy(Gen2, E,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Gen1,Gen2),O1,O2).
+
+% Constraint 48.2
+ordering_step(I,O1,O2) :-
+	member(wasAttributedTo(_Att,E,Ag,_Attrs),I),  
+	member(wasStartedBy(Start1, Ag,_E1,_A1,_T1,_Attrs1),I),  
+	member(wasGeneratedBy(Gen2, E,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Start1,Gen2),O1,O2).
+
+% Constraint 49.1
+ordering_step(I,O1,O2) :-
+	member(actedOnBehalfOf(_Del,Ag2,Ag1,_A,_Attrs),I),  
+	member(wasGeneratedBy(Gen1, Ag1,_A1,_T1,_Attrs1),I),  
+	member(wasInvalidatedBy(Inv2, Ag2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Gen1,Inv2),O1,O2).
+
+% Constraint 49.2
+ordering_step(I,O1,O2) :-
+	member(actedOnBehalfOf(_Del,Ag2,Ag1,_A,_Attrs),I),  
+	member(wasStartedBy(Start1, Ag1,_E1,_A1,_T1,_Attrs1),I),  
+	member(wasEndedBy(End2, Ag2,_E2,_A2,_T2,_Attrs2),I),  
+	constrain(precedes(Start1,End2),O1,O2).
 
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/semantics/prov_xml.pl	Thu Jan 31 17:03:10 2013 +0000
@@ -0,0 +1,111 @@
+
+% Rudimentary import of PROV-XML
+% stephen.cresswell@tso.co.uk
+%
+% Uses SWI-Prolog XML parser
+%
+% Issues:
+%
+% - Doesn't handle bundle
+%
+% - Using dialect(xmlns) option causes namespace expansion to
+%    the form URI:LocalName. Namespace prefixes within attribute 
+%    values and content are preserved are prefixes - but the
+%    prefix mappings are not preserved in the output.
+%
+
+ns(prov,'http://www.w3.org/ns/prov#').
+
+% Read XML from Filename and
+% bind J to a list of Prolog terms like
+%  [activity('ex:a1',_,_,[]), ... ]
+provx_load(Filename,J):-
+	load_structure(Filename,X,[dialect(xmlns),space(remove)]),
+	provx(X,J).
+
+provx([],_).
+provx([element(Prov:document,_,Contents)|Elements],J):-
+	ns(prov,Prov),
+	provx_expressions(Contents,J),
+	provx(Elements,J).
+
+provx_expressions([],[]):-!.
+provx_expressions([element(Prov:Name,Attrs,Contents)|Elements],[Expression|J]):-
+	ns(prov,Prov),
+	template(Name,TArgs),
+	attribute_value(Prov:id,Attrs,ID),
+	provx_args(TArgs,Contents,Args),
+	Expression=..[Name,ID|Args],
+	!,
+	provx_expressions(Elements,J).
+provx_expressions([E|_],_):-
+	write('Failing on '),write(E),nl,
+	fail.
+
+provx_args([],Elements,[Attrs]):-
+	% Additional attributes
+	!,
+	provx_attributes(Elements,Attrs).
+provx_args([Name|TArgs],[element(Prov:Name,Attrs,Contents)|Elements],[Value|Values]):-
+	% obligatory argument
+	ns(prov,Prov),
+	!,
+	arg_value(Attrs,Contents,Value),
+	provx_args(TArgs,Elements,Values).
+provx_args([+Name|TArgs],[element(Prov:Name,Attrs,Contents)|Elements],[Value|Values]):-
+	% optional argument present
+	ns(prov,Prov),
+	!,
+	arg_value(Attrs,Contents,Value),
+	provx_args(TArgs,Elements,Values).
+provx_args([+_|TArgs],Elements,[_|Args]):-
+	% optional argument absent
+	!,
+	provx_args(TArgs,Elements,Args).
+provx_args(TArgs,Elements,_):-
+	write('Failing on args'),write(Elements),nl,
+	write('Expected '),write(TArgs),nl,
+	fail.
+
+provx_attributes([],[]).
+provx_attributes([element(Key,Attrs,Contents)|Elements],[Key=Value]):-
+	Contents=[Value|_], % Expecting Contents single element list containing atom
+	provx_attributes(Elements,Attrs).
+
+% If no Name=Val pair in Attrs, succeed anyway with Val unbound
+attribute_value(Name,Attrs,Val):-
+	memberchk(Name=Val,Attrs),
+	!.
+attribute_value(_,_,_).
+	
+arg_value(Attrs,_,ID):-
+	% Find an ID
+	ns(prov,Prov),
+	memberchk(Prov:ref=ID,Attrs),
+	!.
+arg_value(_,[Content|_],Content).
+	% Find content
+
+
+% Arguments for each type of expression
+% '+' prefix indicates optional argument
+%   - we need a placeholder in Prolog term, but value might not be provided
+template(entity,[]).
+template(activity,[+startTime,+endTime]).
+template(agent,[]).
+
+template(wasGeneratedBy,[entity,+activity,+time]).
+template(used,[activity,entity,+time]).
+
+template(wasInformedBy,[informed,informant]).
+template(wasStartedBy,[activity,+trigger,+starter,+time]).
+template(wasEndedBy,[activity,+trigger,+ender,+time]).
+template(wasInvalidatedBy,[entity,+activity,+time]).
+
+template(wasDerivedFrom,[generatedEntity,usedEntity,+activity,+generation,+usage]).
+template(wasAttributedTo,[entity,agent]).
+template(wasAssociatedWith,[activity,+agent]).
+template(actedOnBehalfOf,[delegate,responsible,+activity]).
+template(wasInfluencedBy,[influencee,influencer]).
+template(specializationOf,[specificEntity,generalEntity]).
+template(alternate,[alternate1,alternate2]).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/semantics/prov_xml_test.pl	Thu Jan 31 17:03:10 2013 +0000
@@ -0,0 +1,27 @@
+
+test1(J):-
+	provx_load('constraint_test_provx/unification-usage-s2-PASS-c23.provx',J).
+
+% Test and report on directory of .provx files
+% - only tests whether the mapping completes, 
+%  not whether result is correct,
+test2(File,Files,J):-
+	Dir='constraint_test_provx',
+	directory_files(Dir,Files),
+	sort(Files,Files1),
+	member(File,Files1),
+	\+memberchk(File,['.','..']),
+	absolute_file_name(File,AbsFile,[relative_to(Dir)]),
+	exists_file(AbsFile),
+	succeeds(provx_load(AbsFile,J),PassOrFail),
+	write(PassOrFail),write(' '),write(File),nl,nl.
+
+test_all:-
+	test2(_,_,_),
+	fail.
+test_all.
+	
+succeeds(Goal,pass):-
+	call(Goal),
+	!.
+succeeds(_Goal,fail).