#
# KEHOME/bin/cyc
# Oct/17/2005 Oct/28/2006

# print OpenCyc SubL answers to CycL questions and statements
#
# Usage:
#	cyc world context
#	verb (predicate subject object)
#	...
#	EOF
#
# input format: one CycL expression per line
#	verb (predicate subject object)
# verb ::= assert | query | do
# CycL output format: one bind list per line
#	(((?x . x1)) ((?x . x2)) ... )
# final output format: one concept per line, sorted
#	xi
#	...
#
# Examples:
#	assert (#$isa #$DickMcCullough #$Person)
#	query (#$isa ?PERSON #$Person)
#	query (#$genls #$Person ?X)
#	query (#$specs #$Microtheory ?MT)
#	query (#$genlMt ?MT #$BaseKB)
#	do (cyc-create "DickMcCullough" nil)
#	do (write-image "world/dick")
#	do (exit)

if [ -n "$1" ]
then world="$1"
else world="local"
fi
if [ -n "$2" ]
then mt="$2"
#else mt='#$BaseKB'
#else mt='#$UniversalVocabularyMt'
else mt='#$CurrentWorldDataCollectorMt-NonHomocentric'
fi

query="(CYC-QUERY '"
assert="(CYC-ASSERT '"
context=" $mt)"

postquery="cycbind2column"	# convert CycL bind list to sorted column
postassert="cyclist2column"	# convert CycL list to sorted column
postdebug="cat"			# for debugging


(
sleep 10			# wait for OpenCyc startup
while read verb predicate subject object; do
  case "$verb" in
  assert)prefix="$assert"; suffix="$context";; # postprocess="$postassert";;
  query) prefix="$query";  suffix="$context";; # postprocess="$postquery";;
  do)    prefix="";        suffix="";;         # postprocess="$postassert";;
  esac
  echo "$prefix $predicate $subject $object $suffix"
done
echo "(exit)"			# cleanup & exit OpenCyc
) | opencyc server $world #| "$postquery"

#