34
35:- module(serql_runtime,
36 [ serql_compare/3, 37 serql_eval/2, 38 serql_member_statement/2 39 ]). 40:- use_module(library(xsdp_types)). 41:- use_module(library(debug)). 42:- use_module(library('semweb/rdf_db')). 43
44
51
52
53 56
61
62serql_compare(Op, L, R) :-
63 serql_eval(L, VL),
64 serql_eval(R, VR),
65 !,
66 do_compare(Op, VL, VR).
67
68do_compare(like, literal(Value), Pattern) :-
69 !,
70 to_string(Value, String),
71 rdf_match_label(like, Pattern, String).
72do_compare(like, Resource, Pattern) :-
73 !,
74 atom(Resource),
75 rdf_match_label(like, Pattern, Resource).
76do_compare(=, X, X) :- !.
77do_compare(=, literal(X), query(X)) :- !.
78do_compare(=, X, query(X)) :- !.
79do_compare(\=, X, Y) :-
80 !,
81 \+ do_compare(=, X, Y).
82do_compare(Op, literal(Data), query(Query)) :-
83 catch(to_number(Query, Right, TypeQ), _, fail),
84 !,
85 ( nonvar(TypeQ), atom(Data)
86 -> catch(xsdp_convert(TypeQ, [Data], Left), _, fail)
87 ; catch(to_number(Data, Left, TypeD), _, fail),
88 serql_subsumes(TypeQ, TypeD)
89 ),
90 cmp_nums(Op, Left, Right).
91do_compare(Op, literal(Data), query(Query)) :-
92 !,
93 ( atom(Query) 94 -> atom(Data), 95 cmp_strings(Op, Data, Query)
96 ).
97do_compare(Op, query(Query), literal(Data)) :-
98 !,
99 inverse_op(Op, Inverse),
100 do_compare(Inverse, literal(Data), query(Query)).
101do_compare(Op, literal(Value), literal(Number)) :-
102 catch(to_number(Value, Left, TypeL), _, fail),
103 catch(to_number(Number, Right, TypeR), _, fail),
104 TypeL == TypeR,
105 cmp_nums(Op, Left, Right).
106
107serql_eval(Var, X) :-
108 var(Var),
109 !,
110 X = '$null$'.
111serql_eval(lang(X), Lang) :-
112 !,
113 lang(X, Lang).
114serql_eval(datatype(X), Type) :-
115 !,
116 datatype(X, Type).
117serql_eval(label(X), Lang) :-
118 !,
119 label(X, Lang).
120serql_eval(X, X).
121
127
128lang(literal(lang(Lang0, _)), Lang) :-
129 nonvar(Lang0),
130 !,
131 Lang = literal(Lang0).
132lang(_, '$null$').
133
134datatype(literal(type(Type0, _)), Type) :-
135 nonvar(Type0),
136 !,
137 Type = Type0.
138datatype(_, '$null$').
139
140label(literal(lang(_, Label0)), Label) :-
141 nonvar(Label0),
142 !,
143 Label = literal(Label0).
144label(literal(Label0), Label) :-
145 nonvar(Label0),
146 !,
147 Label = literal(Label0).
148label(_, '$null$').
149
150
151cmp_nums(=, L, R) :- L =:= R.
152cmp_nums(=<, L, R) :- L =< R.
153cmp_nums(<, L, R) :- L < R.
154cmp_nums(>, L, R) :- L > R.
155cmp_nums(>=, L, R) :- L >= R.
156
157cmp_strings(Op, S1, S2) :-
158 atom(S1), atom(S2),
159 compare(Op, S1, S2).
160
161inverse_op(=, =).
162inverse_op(=<, >).
163inverse_op(<, >=).
164inverse_op(>, =<).
165inverse_op(>=, <).
166
167to_number(type(Type, String), Num, Type) :- 168 !,
169 atom_number(String, Num).
170to_number(lang(_Lang, String), Num, _) :-
171 !,
172 atom_number(String, Num).
173to_number(String, Num, _) :-
174 assertion(atom(String)),
175 atom_number(String, Num).
176
178
179serql_subsumes(Q, Var) :- 180 nonvar(Q),
181 var(Var),
182 !.
183serql_subsumes(_, Var) :- 184 var(Var), !, fail.
185serql_subsumes(Var, _) :- 186 var(Var),
187 !.
188serql_subsumes(Query, Data) :-
189 xsdp_subtype_of(Data, Query).
190
191to_string(lang(_, String), String) :- !.
192to_string(type(_, String), String) :- !.
193to_string(String, String) :-
194 atom(String).
195
202
203serql_member_statement(RDF, [H|_]) :-
204 member_statement2(RDF, H).
205serql_member_statement(RDF, [_|T]) :-
206 serql_member_statement(RDF, T).
207
208member_statement2(RDF, optional(True, Statements)) :-
209 !,
210 True = true,
211 serql_member_statement(RDF, Statements).
212member_statement2(RDF, RDF)