#<html><pre>
# KEHOME/bin/cyc
# Sep/15/2003

# print OpenCyc SubL answers to CycL questions and statements
#
# Usage:
#	cyc context world
#	(predicate subject object)
#	...
#	EOF
#
# input format: one CycL expression per line
#	(predicate subject object)
# CycL output format: one bind list per line
#	(((?x . x1)) ((?x . x2)) ... )
# final output format: one concept per line, sorted
#	xi
#	...
#
# Examples:
#	(#$isa #$DickMcCullough #$Person)
#	(#$isa ?PERSON #$Person)
#	(#$genls #$Person ?X)
#	(#$specs #$Microtheory ?MT)
#	(#$genlMt ?MT #$BaseKB)

if [ -n "$1" ]
then context="$1"
else context='#$UniversalVocabularyMt'
fi
if [ -n "$2" ]
then world="$2"
else world="latest.load"
fi

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

  postprocess="cycbind2column"	# convert CycL bind list to sorted column
# postprocess="cyclist2column"	# convert CycL list to sorted column
# postprocess="cat"		# for debugging


(
sleep 10			# wait for OpenCyc startup
while read line; do
  case "$line" in
  *\?*) prefix="$query";;
  *)    prefix="$assert";;
  esac
  echo "$prefix $line $suffix"	# CycL query/assert
done
echo "(exit)"			# cleanup & exit OpenCyc
) | opencyc server $world #| "$postprocess"

#</pre></html>
