I have an RDF file and I need to extract some information from it in a single line.
Now, I'm using AllegroGraph with Prolog query engin :
(select (?result)
(q ?a !rdfs:label ?alabel)
(q ?b !rdfs:label ?blabel)
(lisp ?result (string+ ?alabel " AND " ?blabel)))
to get the results in a single line:
"{a1} AND {b1}"
"{a1} AND {b2}"
"{a2} AND {b1}"
"{a2} AND {b2}"
Now, I need to group all the rows of ?result in a single line with the string "OR". so i get:
"{a1} AND {b1} OR {a1} AND {b2} OR {a2} AND {b1} OR {a2} AND {b2}"
Is there any function in prolog to do this?
The fact that you've only got
a*
on the left andb*
on the right means that you've got some other selection condition than just having a label. Given data like this:you can select
?a
and?b
by their classes (:ClassA
and:ClassB
), and then extract their labels as well, with a pattern like:Then you can get the
{alabel} AND {blabel}
with abind
and aconcat
:Using these, a query like
will get you the kind of results that you can already get:
The trick now is to use
group_concat
and an implicit group to combine all these into a string, with a separator of" OR "
:to get a result:
If you like, you can even get rid of the
bind
, and just put theconcat
expression right into thegroup_concat
. You might find that easier to read (less jumping around) or harder to read (big one-liner), but at least it's good to have options:There are some other examples of
group_concat
floating around on StackOverflow that might be useful to you as well: