#
# KEHOME/bin/mkr2cyc
# Nov/6/2006 Apr/11/2009

#####exec 2>&1  # send errors to stdout

# translate mKR propositions to CycL expressions
#
# Usage:
#	mkr2cyc [-q|-e]
#	proposition
#	...
#	EOF
#
# options:
#	-q  no prompt
#	-e  no prompt, exit
#
# proposition ::=
#   exit;
#   at view = mt;
#   do action od directobject with modifier from/to indirectobject done;
#   let variable op value;
#   subject predicate object mod1 mod2 mod3 mod4 mod5 mod6;
# predicate ::=
#   mKR verb
#   CycL predicate
#
# expression ::=
#   see below
#
# Notes:
# mKR commands for special CycL commands
#	do cyc-create od "name" done;
#	do write-image to myworld done;
#	do generate-phrase from expression done;
# context for most questions and statements is
#	#$UniversalVocabularyMt
# mKR verb (is,has,do,rel,means,causes,...)
#	see below
# mKR characteristic (part,attribute,relation,action,interaction)
#	begins with letter
# mKR variable (attribute)
#	begins with letter
# mKR question variable
#	begins with "?"
#
# CycL predicate
#	begins with "#$"
# CycL constant
#	begins with "#$"
# CycL variable
#	begins with "?"

#echo "# mkr2cyc $@" >&2  # echo command line

if [ -n "$1" ]
then prompt=''		# mkr2cyc -q or mkr2cyc -e
else prompt='ke$ \c'	# mkr2cyc
fi

#####context=' #$UniversalVocabularyMt)'
suffix=""	# default

if [ -n "$prompt" ]
then echo -e "$prompt" >&2
fi
while read subject predicate object mod1 mod2 mod3 mod4 mod5 mod6; do

    subject="${subject%;}"
    case "$subject" in
    ##### context, command, assignment #####
	\(*)	# ( CycL pass thru )
		echo "$subject $predicate $object $mod1 $mod2 $mod3 $mod4 $mod5 $mod6";
		:;;
	exit)	# exit;
		echo "do (exit)";
		break;;
	at)	mt=${mod1%;};
		if [ -n "$mod3" ]
		then  # at view = mt { proposition } ;
			proposition="${mod3%;}";
			echo "( '#\$ist' $mt $proposition )"
		else  # at view = mt ;
			echo "( WITH-MT $mt )"
		fi;
		:;;
	do|hdo|vdo)
		# do action od direct with modifier from/to indirect done;
		# do cyc-create od "name" done;
		# do write-image to "world/name" done;
		# do generate-phrase from expression done;
		prefix="do "
		verb="$subject";
		subject="cyc";
		action="${predicate%;}";
		direct="${mod1%;}";
		modifier="${mod3%;}";
		fromto="${mod4%;}";
		indirect="${mod5%;}";
		case "$action" in
		cyc-create)	modifier="nil";;
		write-image)	modifier="";;
		generate-phrase)modifier="";;
		esac
		echo "$prefix ( $action $direct $modifier )";
		:;;
	let|unlet|vlet)
		# let variable op value;
		prefix="do "
		verb="$subject";
		subject="cyc";
		variable="${predicate%;}";
		op="${object%;}";
		value="${mod1%;}";
		echo "$prefix ( $verb $variable $op $value )";
		:;;

	*)	# subject predicate object;
		predicate="${predicate%;}";
		object="${object%;}";
		value="${mod2%;}";

    ##### question, statement #####
	inverse="no"
	suffix="$context"
	prefix="assert"	# default
	case "$subject" in
	\?*) prefix="query ";;
	esac
	case "$object" in
	\?*) prefix="query ";;
	esac

	#####echo "# INFO: mkr2cyc: $prefix ( $predicate $subject $object ) $suffix" >&2

	case "$predicate" in
	##### hierarchy verbs #####

	#isa|iss|isu) predicate='#$genls';;
	isa)	echo "$subject iss $object" | mkr2cyc -e;
		echo "$subject isu $object" | mkr2cyc -e;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	iss)	predicate='#$genls';;
	isu)	predicate='#$isa';;

	#isc|isg|isp) predicate='#$specs';;
	isc)	echo "$subject isg $object" | mkr2cyc -e;
		echo "$subject isp $object" | mkr2cyc -e;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	isg)	predicate='#$specs';;
	isp)	predicate='#$instances';;

	isa\*)	echo "$subject iss* $object" | mkr2cyc -e;
		echo "$subject isu* $object" | mkr2cyc -e;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	iss\*)	predicate='#$all-genls';;
	isu\*)	predicate='#$all-isa';;
	isc\*)	echo "$subject isg* $object" | mkr2cyc -e;
		echo "$subject isp* $object" | mkr2cyc -e;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	isg\*)	predicate='#$all-specs';;
	isp\*)	predicate='#$all-instances';;

	##### other mKR verbs #####
	haspart)predicate='#$physicalParts';;
	isapart)predicate='#$physicalParts';;
	ismem)	predicate='#$groupMembers';;
	isall)	predicate='#$groupMembers';;
	isalt)	predicate='#$groupMembers';;
	isany)	predicate='#$groupMembers';;

	is)	if [ -n "$mod1" ]  # "with"
		then  # subject is genus with differentia ;
			genus="$object";
			diff="$mod2 $mod3 $mod4 $mod5 $mod6";
			diff="${diff%;}"
			echo "( '#\$defnIff' $subject ('#\$and'";
			echo "  ( '#\$genls' $subject $genus }";
			echo "  ( $diff )))"
		else  # subject is object;
			case "$object" in
			\?*) echo "( DEFINING-DEFNS '( $subject $mt ) )";;
			*)  echo "( equals  $subject $object )";;
			esac;
		fi;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	has)	case "$object" in
		\?*) echo "( ALL-TERM-ASSERTIONS '( $subject T ))";;
		*)  echo "( $object $subject $value )";;
		esac;
		if [ -n "$prompt" ]; then echo -e "$prompt" >&2; fi
		continue;;
	do)	prefix="do "; predicate="$object"; object="$value";;
	rel)	predicate="$object"; object="$value";;
	isin)	predicate="$object"; object="$value";;

	means)	 predicate="signifies";;
	isref)	 predicate="signifies"; inverse="yes";;
	causes)	 predicate="causedBy"; inverse="yes";;
	because|causedBy)predicate="causedBy";;

	##### other predicates #####
	genlmt)	predicate='#$genlMt';;
	specmt)	predicate='#$genlMt'; inverse="yes";;
	*)	;;
	esac  # end case "$predicate"

	case "$inverse" in
	yes) temp="$subject"; subject="$object"; object="$temp";;
	esac
	echo "$prefix ($predicate $subject $object) $suffix"

    :;;
    esac # end case "$subject"

	if [ -n "$prompt" ]
	then echo -e "$prompt" >&2
	else
		if [ "$1" = "-e" ]
		then
			####echo "# INFO: mkr2cyc: -e option" >&2
			break
		fi
	fi

done
####echo "# INFO: mkr2cyc: done" >&2

#