1:- module(labelutils,
    2          [
    3           label_atom/2,
    4           label_atom/3,
    5           row_labelify/2,
    6           term_labelify/2
    7          ]).

utilities for labeling entities

Convenience predicate for attaching labels to simple or compound entities.

*/

   16:- use_module(library(semweb/rdf11)).   17:- use_module(library(sparqlprog/emulate_builtins)).   18
   19label_atom(S,A) :-
   20        label_atom(S,A,[]).
 label_atom(+Input, ?Output, +Opts:list) is det
   24label_atom(S,A, _Opts) :-
   25        string(S),
   26        !,
   27        atom_string(A,S).
   28label_atom(X,A, Opts) :-
   29        atom(X),
   30        concat_atom([Pre,Local],':',X),
   31        \+ \+ rdf_current_prefix(Pre,_),
   32        rdf_global_id(Pre:Local,Y),
   33        Y\=X,
   34        member(expand_uris(true), Opts),
   35        !,
   36        label_atom(Y, A, Opts).
   37label_atom(X,A, Opts) :-
   38        atom(X),
   39        member(expand_uris(true), Opts),
   40        concat_atom([Pre,Local],':',X),
   41        \+ rdf_current_prefix(Pre,_),
   42        concat_atom(['http://purl.obolibrary.org/obo/',Pre,'_',Local],IRI),
   43        \+ \+ rdf(IRI,_,_),
   44        IRI\=X,
   45        !,
   46        label_atom(IRI, A, Opts).
   47label_atom(X,A, Opts) :-
   48        \+ compound(X),
   49        atom(X),
   50        atom_iri(X,IRI),
   51        get_label_predicate(P,Opts),
   52        rdf(IRI,P,Literal),
   53        !,
   54        ensure_atom(Literal,A).
   55
   56get_label_predicate(P, Opts) :-   option(label_predicate(P),Opts).
   57get_label_predicate(rdfs:label, _).
   58get_label_predicate(skos:prefLabel, _).
   59
   60
   61
   62atom_iri(X,I) :-
   63        concat_atom([Pre,Local],:,X),
   64        \+ \+ rdf_current_prefix(Pre,_),
   65        rdf_global_id(Pre:Local,I),
   66        !.
   67atom_iri(X,X).
 term_labelify(+Term, ?LabeledTerm) is det
If Term is a compound term, LabeledTerm is the same term but with all atomic components A replaced with A-LabelA

where LabelA is the rdfs:label for A

   75term_labelify(V,V) :-
   76        var(V),
   77        !.
   78term_labelify([],[]) :- !.
   79term_labelify([H|T],[H2|T2]) :-
   80        !,
   81        term_labelify(H,H2),
   82        term_labelify(T,T2).
   83term_labelify(T,T2) :-
   84        T =.. [P|Args],
   85        Args=[_|_],
   86        !,
   87        term_labelify(Args,Args2),
   88        T2 =.. [P|Args2].
   89
   90term_labelify(T,T-A) :-
   91        label_atom(T,A),
   92        !.
   93term_labelify(T,T).
   94
   95%! row_labelify(+Row,?LabeledRow) is det
   96%
   97% given a row term Foo(V1,V2,...,Vn)
   98% add an extra argument for the label for each
   99row_labelify(X,X2) :-
  100        X = _^^_,
  101        !,
  102        label_atom(X,X2).
  103row_labelify(X,X2) :-
  104        X = _@_,
  105        !,
  106        label_atom(X,X2).
  107row_labelify(Row,Row2) :-
  108        Row =.. [P|Args],
  109        rowargs_labelify(Args,Args2),
  110        Row2 =.. [P|Args2].
  111
  112rowargs_labelify([],[]).
  113rowargs_labelify([H|T],[H2,Label|T2]) :-
  114        (   label_or_list_to_atom(H,Label)
  115        ->  true
  116        ;   Label=''),
  117        compactify_arg(H,H2),
  118        rowargs_labelify(T,T2).
  119
  120compactify_arg(Var,'_') :-
  121        var(Var),
  122        !.
  123compactify_arg(L,A) :-
  124        is_list(L),
  125        !,
  126        maplist(compactify_arg,L,L2),
  127        concat_atom(L2,'|',A).
  128compactify_arg(H,H2) :-
  129        compound(H),
  130        !,
  131        term_labelify(H,H2).
  132        %format(atom(H2),'~w',[H]).
  133compactify_arg(H,H2) :-
  134        rdf_global_id(Curie,H),
  135        Curie\=H,
  136        Curie = Pre:Local,
  137        !,
  138        format(atom(H2),'~w:~w',[Pre,Local]).
  139compactify_arg(Str^^_,H2) :-
  140        atom_string(H2,Str),
  141        !.
  142compactify_arg(H,H).
  143
  144label_atom_det(L,A) :- label_atom(L,A),!.
  145label_atom_det(_,'').
  146
  147label_or_list_to_atom(X,A) :-
  148        label_atom(X,A),
  149        !.
  150label_or_list_to_atom(L,A) :-
  151        is_list(L),
  152        !,
  153        maplist(label_atom_det,L,L2),
  154        concat_atom(L2,'|',A)