bashlog icon indicating copy to clipboard operation
bashlog copied to clipboard

Encountered Unexpected Bash Script when Using Bashlog API

Open Meng6 opened this issue 2 years ago • 1 comments

Thanks so much for developing this amazing tool! I got an issue while running the bash script to get the lowest common ancestors (LCAs).

I wrote the following Datalog script to get common ancestors who are not LCAs first:

    facts_p(S, P, O) :~ cat ./data/person.tsv
    facts_d(S, P, O) :~ cat ./data/dissertation.tsv
    facts_a(S, P1, O1, P2, O2) :~ cat ./data/advised.tsv

    % advise(advisor, student).
    advise(A, S) :-
        facts_a(DID, "<advised_by>", A, _, _),
        facts_d(DID, "<author>", S).

    % ancestors of 100
    ancestor_of_pid(X, 100) :- advise(X, 100).
    ancestor_of_pid(X, 100) :- advise(X, Y), ancestor_of_pid(Y, 100).

    % ancestors of 200
    ancestor_of_pid(X, 200) :- advise(X, 200).
    ancestor_of_pid(X, 200) :- advise(X, Y), ancestor_of_pid(Y, 200).

    common_ancestors(100, 200, X) :- 
        ancestor_of_pid(X, 100),
        ancestor_of_pid(X, 200).

    main(X) :- 
        common_ancestors(100, 200, X),
        common_ancestors(100, 200, Y),
        advise(X, Y).

The following Bash script was returned by Bashlog API:

#!/bin/bash
###############################################################
# This script was generated by bashlog
# For more information, visit thomasrebele.org/projects/bashlog
###############################################################

export LC_ALL=C
mkdir -p tmp
rm -f tmp/*
if type mawk > /dev/null; then awk="mawk"; else awk="awk"; fi
sort="sort "
check() { grep -- $1 <(sort --help) > /dev/null; }
check "--buffer-size" && sort="$sort --buffer-size=100% "
check "--parallel"    && sort="$sort --parallel=2 "

read_ntriples() { $awk -F" " '{ sub(" ", "\t"); sub(" ", "\t"); sub(/ \.$/, ""); print $0 }' "$@"; }
conv_ntriples() { $awk -F$'\t' '{ print $1 " " $2 " " $3 " ." }'; }


$sort -t $'\t' -k 1 -u \
<(echo -n)

 rm -f tmp/*

It seems this Bash script was wrong. Do you know how to solve this issue? Thanks!

Meng6 avatar Aug 13 '23 15:08 Meng6

There seems to be a problem with the generation of the script. I'll need to analyze this in more detail. As a workaround I would recommend replacing the constants with variables. A script has been generated for the following Datalog program:

facts_p(S, P, O) :~ cat ./data/person.tsv
facts_d(S, P, O) :~ cat ./data/dissertation.tsv
facts_a(S, P1, O1, P2, O2) :~ cat ./data/advised.tsv

% advise(advisor, student).
advise(A, S) :-
    facts_a(DID, "<advised_by>", A, _, _),
    facts_d(DID, "<author>", S).

ancestor_of_pid(X, P) :- advise(X, P).
ancestor_of_pid(X, P) :- advise(X, Y), ancestor_of_pid(Y, P).

common_ancestors(P1, P2, X) :- 
    ancestor_of_pid(X, P1),
    ancestor_of_pid(X, P2).

main(X) :- 
   common_ancestors(100, 200, X),
   common_ancestors(100, 200, Y),
   advise(X, Y).

thomasrebele avatar Aug 27 '23 20:08 thomasrebele