# KEHOME/src/cyc.icn
# Sep/7/2003

# methods
# mk_pipeline()
# do_pipeline()
# do_opencyc()
# cyc_translate()	# MKR proposition to CycL expression
# cyc_filter()		# delete OpenCyc opening REMs
# cyc_line2column()	# CycL expression (line) to column

# do_cyc_find_or_create()
# do_cyc_assert()
# do_cyc_unassert()
# do_cyc_query()

# do_cyc_create()
# do_cyc_rename()

# cyc_question()
# cyc_qsubject()
# cyc_qobject()
# cyc_statement()

# string
procedure mk_pipeline(sentenceType,subject,predicate,object,mt,properties)
#=========================================================================
# create cyc pipeline command
# '(exit)' insures clean OpenCyc SubL termination
local cycobject
local pipeline,mkrline,cycline,cyclist
local cmdline
static processor,preprocessor,postprocessor
static lp,rp,b,p,squote,dquote
static info,warning
initial {
  info := "INFO: mk_pipeline: "
  warning := "WARNING: mk_pipeline: "
  lp := "("
  rp := ")"
  b := " "
  p := " | "
  squote := "\'"
  dquote := "\""
# preprocessor := " mkr2cyc "
# preprocessor := " (mkr2cyc; echo '(exit)') "
# preprocessor := " (mkr2cyc; sleep 10; echo '(exit)') "
  preprocessor := " (mkr2cyc; sleep 20; echo '(exit)') "
# preprocessor := " (sleep 10; mkr2cyc; sleep 10; echo '(exit)') "
# preprocessor := " (sleep 10; mkr2cyc; sleep 10; echo '(exit)') "
# preprocessor := " (sleep 10; mkr2cyc; echo '(exit)') "
# preprocessor := " (sleep 20; mkr2cyc; echo '(exit)') "
# preprocessor := " (sleep 30; mkr2cyc; echo '(exit)') "
  processor := " opencyc server 2>&1 "
  postprocessor := ""
# postprocessor := " cycsort "
}
/mt := "#$UniversalVocabularyMt"
/properties := []

if DEBUG=="SENTENCE" then {
	processor := " cat "
}

case subject of {
default: {
	# subject predicate ?
	}
"?": {
	# ? predicate object
	subject := object
	predicate := mkr_inverse(predicate)
	object := "?"
	}
}

if subject ? { ="#$" } then
	subject := squote||subject||squote
if predicate ? { ="#$" } then
	predicate := squote||predicate||squote
if /object then {
	object := ""
} else if object ? { ="#$" } then {
	object := squote||object||squote
}
cycobject := object

case sentenceType of {
default: {
	writes_type(myerr,sentenceType,warning||"unexpected sentenceType")
	mkrline := subject||b||predicate||b||object
	cyclist := cyc_translate(mkrline)
	cycline := !cyclist
	preprocessor := "("||cycline||"; sleep 10; echo '(exit)'}"
	cmdline := preprocessor||p||processor
	pipeline := cmdline
	}
"statement": {
	mkrline := subject||b||predicate||b||object
	cyclist := cyc_translate(mkrline)
	cycline := !cyclist
	preprocessor := "("||cycline||"; sleep 10; echo '(exit)'}"
	cmdline := preprocessor||p||processor
	pipeline := cmdline
	}
"question": {
	mkrline := subject||b||predicate||b||object
	cyclist := cyc_translate(mkrline)
	cycline := !cyclist
	preprocessor := "(echo "||cycline||"; sleep 10; echo '(exit)'}"
	cmdline := preprocessor||p||processor
	pipeline := cmdline
	}
"command": {
	mkrline := predicate||b||object
	cyclist := cyc_translate(mkrline)
	cycline := unparse(cyclist)
	preprocessor := "("||cycline||"; sleep 10; echo '(exit)'}"
	cmdline := preprocessor||p||processor
	pipeline := cmdline
	}
"assignment": {
	mkrline := predicate||b||object
	cyclist := cyc_translate(mkrline)
	cycline := !cyclist
	preprocessor := "("||cycline||"; sleep 10; echo '(exit)'}"
	cmdline := preprocessor||p||processor
	pipeline := cmdline
	pipeline := cycline||p||cmdline
	}
}


#cmdline := "cycprint "||mkrline
#cmdline := dquote||cmdline||dquote

if DEBUG==("CYC"|"PIPE") then {
  writes_type(myerr,mkrline,info||"mkrline")
  writes_type(myerr,cycline,info||"cycline")
  writes_type(myerr,cmdline,info||"cmdline")
  writes_type(myerr,pipeline,info||"pipeline")
}

#======================================#
# new code: pipeio(mkrCyc) in char.icn

return mkrline
#======================================#
return pipeline
end

# list
procedure do_pipeline(pipeline,option,sharp)
#===========================================
# execute cyc pipeline
local mkrline
local dval, line, nline
local flag, ans, append
local answer,answer1,answer2
local truthvalue,truthvalue1,truthvalue2
static squote,dquote,b,prefix
static info,warning,ierror
initial {
  info := "INFO: do_pipeline: "
  warning := "WARNING: do_pipeline: "
  ierror := "Internal ERROR: do_pipeline: "
  squote := "\'"
  dquote := "\""
  b := " "
  prefix := repl(b,2)
}
/option := "pipe"
/sharp := '#'

dval := []

#======================================#
# new code: pipeio(mkrCyc) in char.icn

mkrline := pipeline
write(OpenCycInput,mkrline)
nline := 0
while line := read(OpecCycOutput) do {
	nline +:= 1
	put(dval,line)
}
#======================================#

#####case option of {
#####default: {
#####	writes_type(myerr,option,warning||"unexpected option")
#####	}
######"pipe":   { dval := pipe2list(pipeline,"quiet",sharp) }	# shell.icn
#####"pipe":   { dval := pipe2list(pipeline,"write",sharp) }	# shell.icn
#####"system": { dval := system("sh -c "||dquote||pipeline||dquote) }
#####"shell":  { dval := shell(pipeline) }			# shell.icn
#####}
if DEBUG==("CYC"|"DELETE") then {
  write(myerr,TypeComment,info||"dval")
  every write(myerr,!dval)
}

# dval := cyc_filter(dval)
answer := cyc_line2column(dval)
return answer
end

# list
procedure cyc_translate(mkrline)
#===============================
# from MKR propositions to CycL expressions
local pipeline,cycline,cyclist
static p
static info,warning
initial {
  info := "INFO: cyc_translate: "
  warning := "WARNING: cyc_translate: "
  p := " | "
}
#if DEBUG=="MKR2CYC" then {
  writes_type(myerr,mkrline,info||"mkrline")
#}
pipeline := "echo "||mkrline||p||"mkr2cyc"
cyclist := pipe2list(pipeline)

#if DEBUG=="MKR2CYC" then {
  writes_type(myerr,cyclist,info||"cyclist")
#}
#####cycline := cyclist[1]
return cyclist
end

# list
procedure cyc_line2column(dval)
#==============================
local line,ans,answer
static b,p,info
initial {
  info := "INFO: cyc_line2column: "
  b := " "
  p := repl(b,2)
}
answer := []
every line := !dval do {
	writes_type(myout,line,info||"line")
	line := trimws(line)
	ans := p||replace(line,b,"\n"||p)
	put(answer,ans)
	writes_type(myout,ans,info||"ans")
	write(myout,ans)
}
return answer
end

# list
procedure cyc_filter(dval)
#=========================
# from CycL expression (line) to column format
# delete OpenCyc opening remarks
# print list with one item per line
# do NOT sort -- these are lists, NOT sets
local nresult  # 1 (iss,isu,isg,isp) or 2 (isa,isc)
local append,line,flag,ans
local answer, truthvalue
local answer1,truthvalue1
local answer2,truthvalue2

nresult := 0
answer := ""
append := "no"
answer := []
answer1 := []
answer2 := []
truthvalue := ""
truthvalue1 := ""
truthvalue2 := ""
every line := !dval do {
	flag := "DELETE"
	ans := ""
	if line ? {
	  (="CYC(" || (flag <- m_number()) || ="):" || (ans <-  tab(0)))  |
	  (="CYC(" || (flag <- m_number()) || ="):" & pos(0))  |
	  ((flag <- ="T")   & pos(0))  |
	  ((flag <- ="NIL") & pos(0))  |
	  (ans <- tab(0))
	} then {
		case flag of {
		default: { }
		"DELETE": {
			if append=="yes" then {
				put(answer,trimws(ans))
			} else {
				if DEBUG==("DELETE"|"IGNORE"|"FILTER") then {
				  write(myout,"DELETE: ",line)
				}
			} # end if
			} # end "DELETE"
		"1":   { append := "yes"; put(answer,trimws(ans)) }
		"T":   { append := "no";  truthvalue := flag }
		"NIL": { append := "no";  truthvalue := flag }
		"2":   {
			nresult := 1
			answer1 := answer
			truthvalue1 := truthvalue
			answer := []
			truthvalue := ""
			put(answer,trimws(ans))
			}
		"3":   {
			nresult := 2
			append := "no"
			answer2 := answer
			truthvalue2 := truthvalue
			answer := []
			truthvalue := ""
			}
		} # end case flag
	} else {
		if DEBUG==("DELETE"|"IGNORE"|"FILTER") then {
		  writes_type(mylog,line,ierror||"no match")
		}
	}
} # end every line

case nresult of {
default: { ans := [] }
0: { ans := [] }
1: { ans := [answer1,truthvalue1] }
2: { ans := [answer1,truthvalue1,answer2,truthvalue2] }
}

#####if nresult < 1 then
#####	return []
##########writes_type(myout,nresult,info||"nresult")
##########writes_type(myout,answer1,info||"answer1")
#####case truthvalue1 of {
#####default: {
#####	writes_type(myerr,truthvalue1,warning||"unexptected truthvalue1")
#####	every write(myerr,!answer1)
#####	}
#####"T": {
#####	ans := ""
#####	if *answer1 > 0 then {
#####		every ans ||:= !answer1
#####		ans := prefix||replace(ans,b,"\n"||prefix)
#####		write(myout,ans)
#####	}
#####	#####return answer1
#####	}
#####"NIL": {
#####	writes_type(myerr,truthvalue1,warning||"truthvalue1")
#####	every write(myerr,!answer1)
#####	#####return []
#####	}
#####} # end case truthvalue1
#####
#####if nresult < 2 then
#####	return answer1
##########writes_type(myout,answer2,info||"answer2")
#####case truthvalue2 of {
#####default: {
#####	writes_type(myerr,truthvalue2,warning||"unexptected truthvalue2")
#####	every write(myerr,!answer2)
#####	}
#####"T": {
#####	ans := ""
#####	if *answer2 > 0 then {
#####		every ans ||:= !answer2
#####		ans := prefix||replace(ans,b,"\n"||prefix)
#####		write(myout,ans)
#####	}
#####	#####return answer2
#####	}
#####"NIL": {
#####	writes_type(myerr,truthvalue2,warning||"truthvalue2")
#####	every write(myerr,!answer)
#####	#####return []
#####	}
#####} # end case truthvalue2
#####return answer2

return ans
end

# list
procedure do_opencyc(program,world)
#==================================
# do opencyc od program from world done;
# called from command() in command.icn
local dval,pipeline,mkrline,cycline
local cmdline,cyccmd
local sharp,SaveCommentCharacter
local processor,preprocessor,postprocessor
static squote,dquote,b
static info,warning
initial {
  info := "INFO: do_opencyc: "
  warning := "WARNING: do_opencyc: "
  squote := "\'"
  dquote := "\""
  b := " "
}
/program := "server"
/world := "[]"
dval := []

processor := "opencyc "
case program of {
default: { 
	writes_type(myerr,program,warning||"unxpected program")
	write(myerr,"# usage: do opencyc od server done;")
	write(myerr,"# usage: do opencyc od server from world done;")
	write(myerr,"# usage: do opencyc od browser done;")
	return dval
	}
"":        { program := "server" }
"server":  { }
"browser": { }
}
processor ||:= program
case world of {
"[]":    { }
default: { processor ||:= " "||world }
}
processor ||:= " 2>&1 "  # include stderr
pipeline := "mkr2cyc | "||processor

dval := do_pipeline(pipeline)

if DEBUG=="CYC" then {
  writes_type_all(cmdline,info||"cmdline")
  writes_type_all(dval,info||"dval")
}
return dval
end


#procedure do_cyc_query(sentence,mt,properties)
procedure do_cyc_query(subject,predicate,object,mt,properties)
#=============================================================
# (CYC-QUERY '(predicate subject object) mt)
# assume: ke|deletecomment|opencyc server
local dval,pipeline,mkrline,cycline
local cmdline
static lp,rp,b,squote,dquote
static info,warning
initial {
  info := "INFO: do_cyc_query: "
  warning := "WARNING: do_cyc_query: "
  lp := "("
  rp := ")"
  b := " "
  squote := "\'"
  dquote := "\""
}
dval := []

case subject of {
default: {
	# subject predicate ?
	}
"?": {
	# ? predicate object
	subject := object
	predicate := mkr_inverse(predicate)
	object := "?"
	}
}

pipeline := mk_pipeline("question",subject,predicate,object,mt,properties)
if DEBUG=="CYC" then {
  writes_type(myout,pipeline,info||"pipeline")
}

dval := do_pipeline(pipeline)

return dval
end

#procedure do_cyc_assert(sentence,mt,properties)
procedure do_cyc_assert(subject,predicate,object,mt,properties)
#==============================================================
# (CYC-ASSERT '(predicate subject object) mt)
# assume: ke|deletecomment|opencyc server
local dval,pipeline
static info,warning
initial {
  info := "INFO: do_cyc_assert: "
  warning := "WARNING: do_cyc_assert: "
}
dval := []

#if DEBUG=="ASSERT" then {
  writes_type(myerr,subject,info||"subject")
  writes_type(myerr,predicate,info||"predicate")
  writes_type(myerr,subject,info||"subject")
#}
pipeline := mk_pipeline("statement",subject,predicate,object,mt,properties)
if DEBUG=="CYC" then {
  writes_type(myerr,pipeline,info||"pipeline")
}

dval := do_pipeline(pipeline)

return dval
end

#-------------------------------------------------------#
#-------------------------------------------------------#

# list
procedure cyc_question(qtype,line,symbol,option)
#===============================================
# answer question using Stanford OpenCyc KB
# execute mkrGetData command
# option = "quiet" => no output
# called from xx() in question.icn
# called from xx() in hwalk.icn
local cycline
local svopad,subject,verb,object,pplist
local nvobject,nvobj
local prefix,suffix
local iverb,iprop,iline,isymbol,iflag
local question,cmdline,qline
local fd,rc
local dval,count
local prop,value,val
local subprop,subvalue,subquestion
static GDcmd,GRcmd
static blank,squote,dquote
static info,warning,ierror
static rdfWordChar
initial {
  info := "INFO: cyc_question: "
  warning := "WARNING: cyc_question: "
  ierror := "Internal ERROR: cyc_question: "
  GDcmd := "GetData"
  GRcmd := "GetResource"
  blank := " "
  squote := "\'"
  dquote := "\""
  rdfWordChar := &cset -- WhiteSpace
}
/option := "write"
if DEBUG=="CYC" then {
  writes_type(myerr,qtype,info||"qtype")
  writes_type(myerr,line,info||"line")
  writes_type(myerr,symbol,info||"symbol")
}
dval := []

question := trim(line,";")
question := trimws(question)

svopad := symbol.svalue
subject  := svopad[1]
verb     := svopad[2]
nvobj    := svopad[3]
pplist   := svopad[4]

subject  := unparse(subject)
verb     := unparse(verb)

iflag := ""
every nvobject := !nvobj do {
#================================================#

object   := unparse(nvobject)

if DEBUG=="CYC" then {
  writes_type(myerr,subject,info||"subject")
  writes_type(myerr,verb,info||"verb")
  writes_type(myerr,nvobject,info||"nvobject")
  writes_type(myerr,object,info||"object")
  writes_type(myerr,pplist,info||"pplist")
}

#####dval := do_cyc_query(subject,verb,nobject)
#####return dval

case qtype of {
default: {
	writes_type(myerr,qtype,warning||"unexpected question type")
	return dval
	}
"qsubject": {
	dval := cyc_qsubject(subject,verb,nvobject,pplist,option)
	return dval
	} # end "qsubject"
"qverb": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"qobject": { iflag := "no" }
"qprop":   { iflag := "no" }
"hwalk":   { iflag := "no" }
} # end case qtype

dval := cyc_qobject(subject,verb,nvobject,pplist,option)

} # end every nvobject

return dval
end

# list
procedure cyc_qobject(subject,verb,nvobject,pplist,option)
#=========================================================
local dval,count,ansfd
local line,cycline
local prefix,suffix
local iverb,iprop,iline,isymbol,iflag
local question,cmdline,qline
local fd,rc
local prop,value,val
local subprop,subvalue,subquestion
local v,hexp,DOLLAR,dollar_question
local uname,cname
local saveHOSEPARATOR,save_hmaxlevel
local symbol,object
static GDcmd,GRcmd
static blank,squote,dquote
static info,warning,ierror
static rdfWordChar
initial {
  info := "INFO: cyc_qobject: "
  warning := "WARNING: cyc_qobject: "
  ierror := "Internal ERROR: cyc_qobject: "
  GDcmd := "GetData"
  GRcmd := "GetResource"
  blank := " "
  squote := "\'"
  dquote := "\""
  rdfWordChar := &cset -- WhiteSpace
}
/option := "write"
ansfd := myout

value := ""
iflag := ""
dval := []

object := unparse(nvobject)

case verb of {
default: { } # other verb: continue below
(
"isa"|"iss"|"isu"|"isc"|"isg"|"isp"|
"haspart"|"isapart"|"ismem"|"isall"|"isalt"|"isany"|
"is"|"has"|"do"|"hdo"|"vdo"|"rel"|"isin"|"set"|"vset"|
"means"|"isref"|"causes"|"causedBy"|

"isa*"|"iss*"|"isu*"|"isc*"|"isg*"|"isp*"|
"haspart*"|"isapart*"|"ismem*"|"isall*"|"isalt*"|"isany*"|
"is*"|"has*"|"do*"|"hdo*"|"vdo*"|"rel*"|"isin*"|"set*"|"vset*"|
"means*"|"isref*"|"causes*"|"causedBy*"|

"genlMt"|"#$genlMt"
): { 
	dval := do_cyc_query(subject,verb,object)
	return dval
	}
}
#================================================#

case verb of {
"is": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"has": {
	case type(nvobject) of {
	"NVPHRASE": {
		prop  := unparse(nv_name(nvobject))
		value := unparse(nv_value(nvobject))
		}
	default: {
		value := ""
		case object of {
		"?":     { prop := "?"; value := "?"  }
		default: { prop := object }
		} # end case object
		}
	} # end case type
	if *iflag = 0 then iflag := "no"
	}
"do": {
	case type(nvobject) of {
	"NVPHRASE": {
		prop  := unparse(nv_name(nvobject))
		value := unparse(nv_value(nvobject))
		}
	default: {
		value := ""
		case object of {
		"?":     { prop := "?"; value := "?" }
		default: { prop := object }
		} # end case object
		}
	} # end case type
	if *iflag = 0 then iflag := "no"
	}
"rel": {
	case type(nvobject) of {
	"NVPHRASE": {
		prop  := unparse(nv_name(nvobject))
		value := unparse(nv_value(nvobject))
		}
	default: {
		value := ""
		case object of {
		"?":     { prop := "?"; value := "?" }
		default: { prop := object }
		} # end case object
		}
	} # end case type
	if *iflag = 0 then iflag := "no"
	}
"isin": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"means": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"isref": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"causes": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"causedBy": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
#-----------------------------------------------#
"isa": {
	dval := cyc_qobject(subject,"iss",nvobject,pplist)
	dval := cyc_qobject(subject,"isu",nvobject,pplist)
	return dval
	}
"iss": {
	prop := "genls"
	iflag := "no"
	}
"isu": {
	prop := "iss"
	iflag := "no"
	}
"isc": {
	dval := cyc_qobject(subject,"isg",nvobject,pplist)
	dval := cyc_qobject(subject,"isp",nvobject,pplist)
	return dval
	}
"isg": {
	prop := "specs"
	iflag := "no"
	}
"isp": {
	prop := "instances"
	iflag := "no"
	}
"ismem": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"isall": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"isalt": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"isany": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"haspart": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}
"isapart": {
	writes_type(myerr,verb,warning||"verb not implemented")
	return dval
	}

"isa*": {
	saveHOSEPARATOR := HOSEPARATOR
	HOSEPARATOR := "\\"
	dval := hwalk(subject,"print","ISA")
	HOSEPARATOR := saveHOSEPARATOR 
	return dval
	}
"iss*": {
	saveHOSEPARATOR := HOSEPARATOR
	HOSEPARATOR := "\\"
	dval := hwalk(subject,"print","genus")
	HOSEPARATOR := saveHOSEPARATOR 
	return dval
	}
"isu*": {
	saveHOSEPARATOR := HOSEPARATOR
	HOSEPARATOR := "\\"
	dval := hwalk(subject,"unithierarchy","ISA")
	HOSEPARATOR := saveHOSEPARATOR 
	return dval
	}
"isc*": {
	dval := hwalk(subject,"print","ISC")
	return dval
	}
"isg*": {
	dval := hwalk(subject,"print","species")
	return dval
	}
"isp*": {
	dval := hwalk(subject,"unithierarchy","ISC")
	return dval
	}

default: {
	subject := [subject]
	# copy code from qobject() in question.icn
        v := verb
        save_hmaxlevel := HMAXLEVEL
        if find("isc**",v) then {
                #==================#
                # cname isc**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
                DOLLAR := []
                every cname := ! sort(subject) do {
                        hwalk(cname,"print","ISC",option,ansfd)
                        dollar_question := isc_exp(cname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
                dval := set(DOLLAR)
        } else if find("isg**",v) then {
                #==================#
                # cname isg**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
                DOLLAR := []
                every cname := ! sort(subject) do {
                        hwalk(cname,"print","species",option,ansfd)
                        dollar_question := isc_exp(cname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
                dval := set(DOLLAR)
        } else if find("isp**",v) then {
                #==================#
                # cname isp**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
                DOLLAR := []
                every cname := ! sort(subject) do {
                        hwalk(cname,"unithierarchy","ISC",option,ansfd)
                        dollar_question := isc_exp(cname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
                dval := set(DOLLAR)
        } else if find("isa**",v) then {
                #==================#
                # uname isa**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
		saveHOSEPARATOR := HOSEPARATOR
		HOSEPARATOR := "\\"
                DOLLAR := []
                every uname := ! sort(subject) do {
                        hwalk(uname,"print","ISA",option,ansfd)
                        dollar_question := isa_exp(uname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
		HOSEPARATOR := saveHOSEPARATOR 
                dval := set(DOLLAR)
        } else if find("iss**",v) then {
                #==================#
                # uname isa**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
		saveHOSEPARATOR := HOSEPARATOR
		HOSEPARATOR := "\\"
                DOLLAR := []
                every uname := ! sort(subject) do {
                        hwalk(uname,"print","genus",option,ansfd)
                        dollar_question := isa_exp(uname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
		HOSEPARATOR := saveHOSEPARATOR 
                dval := set(DOLLAR)
        } else if find("isu**",v) then {
                #==================#
                # uname isu**max ? #
                #==================#
                hexp := hop2hexp(v)
                #HMAXLEVEL := hexp[2]
                put_parameter("hmaxlevel",hexp[2])  # log change
		saveHOSEPARATOR := HOSEPARATOR
		HOSEPARATOR := "\\"
                DOLLAR := []
                every uname := ! sort(subject) do {
                        hwalk(uname,"unithierarchy","ISA",option,ansfd)
                        dollar_question := isa_exp(uname,HMAXLEVEL)
                        DOLLAR |||:= dollar_question
                }
		HOSEPARATOR := saveHOSEPARATOR 
                dval := set(DOLLAR)
        } else {
		writes_type_all(verb,warning||"unexpected verb")
		return dval
                unknown_question(line,symbol)
        }
        HMAXLEVEL := save_hmaxlevel
	return dval
        } # end case default
} # end case verb

if DEBUG=="CYC" then {
  writes_type(myerr,prop,info||"prop")
  writes_type(myerr,value,info||"value")
}
case prop of {
"?": {
	prop := "?"
	value := "?"
	prefix := squote||subject||blank||verb||squote
	}
default: {
	case type(nvobject) of {
	"NVPHRASE": {
		prefix := squote||subject||blank||verb||blank||
			prop||" = "||squote
		}
	default: {
		prefix := squote||subject||blank||verb||squote
		}
	} # end case type
	}
} # end case prop

suffix := squote||squote

#=========================================================#
#=========================================================#
#=========================================================#
case iflag of {
default: { value := "?" }
"no":    { value := "?" }
"yes":   { val := subject; subject := "?"; value := val }
}
dval := do_cyc_query(subject,prop,value)

#=========================================================#
#=========================================================#
#=========================================================#
# use get_property_values() in mkrGetData.java

###### loop on "arcs" properties
#####case prop of {
#####"arcs": {
#####    # subject has ?;
#####    if DEBUG=="CYC" then {
#####      writes_type(myerr,subject,info||"property loop for subject ")
#####    }
#####    every subprop := !dval do {
#####	if DEBUG=="CYC" then {
#####	  writes_type(myerr,subprop,info||"subprop")
#####	}
#####	case subprop of {
#####	"arcs": { }
#####	default: {
#####		prefix := squote||subject||blank||verb||blank||subprop||" = "||squote
#####		suffix := squote||squote
#####		subdval := do_get_data(subject,subprop,"?",prefix,suffix,option)
#####		}
#####	} # end case subprop
#####    } # end every subprop
#####    } # end "arcs"
#####default: {
#####    if DEBUG=="CYC" then {
#####      writes_type(mylog,question,info||"no property loop for subject")
#####    }
#####    } # end default
#####} # end case prop
#=========================================================#


#================================================#

if DEBUG=="CYC" then {
  writes_type(myerr,dval,info||"answer: dval")
}
return dval
end

# list
procedure cyc_qsubject(subject,verb,nvobject,pplist,option)
#==========================================================
# ? verb nvobject;
# ? verb genus with nvobject;
local dval,count
local line
local prefix,suffix
local iverb,iprop,iline,isymbol,iflag
local genus,vp,object
local question,cmdline,qline
local fd,rc
local prop,value,val
local subprop,subvalue,subquestion
local v,hexp,DOLLAR
static GDcmd,GRcmd
static blank,squote,dquote
static info,warning,ierror
static rdfWordChar
initial {
  info := "INFO: cyc_qsubject: "
  warning := "WARNING: cyc_qsubject: "
  ierror := "Internal ERROR: cyc_qsubject: "
  GDcmd := "GetData"
  GRcmd := "GetResource"
  blank := " "
  squote := "\'"
  dquote := "\""
  rdfWordChar := &cset -- WhiteSpace
}
/option := "write"

object := unparse(nvobject)

dval := []
value := ""
iflag := ""
dval := []

if is_ppnull(pplist) then {
	# ? verb nvobject;
	genus := "Resource"
	#-------------------------------------------------#
	case type(nvobject) of {
	"NVPHRASE": {
		prop  := unparse(nv_name(nvobject))
		value := unparse(nv_value(nvobject))
		}
	"list": {
		prop := unparse(nvobject[1])
		value := unparse(nvobject[3])
		}
	default: {
		value := ""
		case object of {
		"?":     { prop := "?" }
		default: { prop := object }
		} # end case object
		}
	} # end case type
	#-------------------------------------------------#
	case verb of {
	("has"|"do"|"rel"): {
	    case prop of {
	    "name": {
		prefix := squote||squote
		suffix := squote||verb||blank||prop||" = "||value||squote
		dval := do_cyc_get_resource("Resource",value,prefix,suffix,option)
		}
	    default: {
		prefix := squote||squote
		suffix := squote||verb||blank||prop||" = "||value||squote
		dval := do_cyc_query(subject,prop,value,prefix,suffix,option)
		}
	    } # end case prop
	    return dval
	    } # end "has"|...
	(
	"isa"|"iss"|"isu"|
	"isc"|"isg"|"isp"|
	"isa*"|"iss*"|"isu*"|
	"isc*"|"isg*"|"isp*"
	): {
	    #--------------------------------------------------------------#
	    # check for verb inverse
	    if iverb := mkr_inverse(verb) then {
		# ? verb object;
		# ==>> object iverb ?;
		dval := cyc_qobject(object,iverb,subject,pplist)
	    } else {
		vp := verb||" "||prop
		writes_type(mylog,vp,warning||"no MKR inverse for property")
		prefix := squote||squote
		suffix := squote||verb||blank||prop||" = "||value||squote
		dval := do_cyc_query(subject,prop,value,prefix,suffix,option)
	    } # end if mkr_inverse
	    #--------------------------------------------------------------#
	    return dval
	    } # end "isa"|...
	default: { }
	} # end case verb
} else {
	# ? verb genus with nvobject;
	genus := object
	nvobject := get_ppobject(pplist).ppwith
	nvobject := !nvobject
	#-------------------------------------------------#
	case type(nvobject) of {
	"NVPHRASE": {
		prop  := unparse(nv_name(nvobject))
		value := unparse(nv_value(nvobject))
		}
	"list": {
		prop := unparse(nvobject[1])
		value := unparse(nvobject[3])
		}
	default: {
		value := ""
		case object of {
		"?":     { prop := "?" }
		default: { prop := object }
		} # end case object
		}
	} # end case type
	#-------------------------------------------------#
	if DEBUG=="CYC" then {
	  writes_type(myerr,nvobject,info||"with nvobject")
	}
	#####object := unparse(nvobject)

	if DEBUG=="CYC" then {
	  writes_type(myerr,genus,info||"genus")
	}
	if DEBUG=="CYC" then {
		writes_type(myerr,genus,info||"genus")
		writes_type(myerr,prop,info||"prop")
		writes_type(myerr,value,info||"value")
	}
	case prop of {
	"name": {
		# GetResource patterns
		# ? verb name = value;
		# ? verb genus with name = value;
		prefix := squote||squote
		case genus of {
		"Resource": {
		  suffix := squote||"has name = "||value||squote
		  }
		default: {
		  suffix := squote||"isa "||genus||
			" with name = "||value||squote
		  }
		} # end case genus
		dval := do_cyc_get_resource(genus,value,prefix,suffix,option)
		}
	default: {
		prefix := squote||squote
		case genus of {
		"Resource": {
		  suffix := squote||verb||blank||prop||" = "||value||squote
		  }
		default: {
		  suffix := squote||"isa "||genus||
			" with "||prop||" = "||value||squote
		  }
		} # end case genus
		dval := do_cyc_query(subject,prop,value,prefix,suffix,option)
		}
	} # end case prop
	return dval

#####	# ? verb nvobject;
#####	# ? verb genus with nvobject;
#####	case genus of {
#####	"Resource": {
#####		# ? verb nvobject;
#####		}
#####	default: {
#####		# ? verb genus with nvobject;
#####		writes_type(myerr,line,warning||"not implemented")
#####		return dval
#####		}
#####	} # end case genus

} # end if is_ppnull
end

###### list
#####procedure do_ask(subject,predicate,object,prefix,suffix,option)
######==============================================================
#####local line,cycline
#####local question,cmdline
#####local dval,count
#####static info,warning
#####static GDcmd,blank
#####initial {
#####  info := "INFO: do_ask: "
#####  warning := "WARNING: do_ask: "
#####  GDcmd := "Ask"
#####  blank := " "
#####}
#####/option := "write"
#####dval := []
#####
#####line := unparse([subject,predicate,object,";"],blank)
#####writes_type(myerr,line,warning||"not implemented")
#####cycline := unparse(["(",predicate,subject,object,")"],blank)
#####writes_type(myerr,cycline,info||"cycline")
#####return dval
#####
#####
###### fix ~ file name problem
#####subject := replace(subject,"?","'?'")  # replace.icn
#####predicate := replace(predicate,"?","'?'")  # replace.icn
#####object := replace(object,"?","'?'")  # replace.icn
#####
#####question := subject||blank||predicate||blank||object
#####cmdline := GDcmd||blank||question||blank||prefix||blank||suffix
#####
#####if DEBUG=="CYC" then {
#####  writes_type(myerr,question,info||"question")
#####  writes_type(myerr,cmdline,info||"cmdline")
#####}
#####dval := pipe2list(cmdline,option)  # shell.icn
#####count := *dval
#####if DEBUG=="CYC" then {
#####  writes_type(myerr,count,info||"count")
#####  writes_type(myerr,dval,info||"dval")
#####}
#####return dval
#####end

# list
procedure do_cyc_get_resource(genus,value,prefix,suffix,option)
#==============================================================
local line,cycline
local dval,count
static lp,rp,b
static info,warning
initial {
  info := "INFO: do_cyc_get_resource: "
  warning := "INFO: do_cyc_get_resource: "
  lp := "("
  rp := ")"
  b := " "
}
/option := "write"
dval := []

line := unparse(["?","isa",genus,"with","name","=",value,";"],b)
cycline := unparse([lp,"and",lp,
		lp,"isa","?",genus,rp,
		lp,"name","?",value,rp,
		rp,rp],b)
writes_type(myerr,cycline,info||"cycline")

return dval
end

#-----------------------------------------------------#
#-----------------------------------------------------#

# integer
procedure cyc_statement(verb,subject,object,pplist)
#==================================================
# assert statement using local OpenCyc KB
# execute mkrPutData command
# called from xx() in statement.icn
local dval
local cmdline,line,cycline
local statement,usubject,uverb,uobject,upplist
local obj
static cmd,blank
static info,warning
initial {
  info := "INFO: cyc_statement: "
  warning := "WARNING: cyc_statement: "
  cmd := "mkrPutData"
  blank := " "
}
dval := []
if DEBUG=="CYC" then {
  writes_type(myerr,verb,info||"verb")
  writes_type(myerr,subject,info||"subject")
  writes_type(myerr,object,info||"object")
  writes_type(myerr,pplist,info||"pplist")
}
if *subject > 1 then {
  writes_type(myerr,subject,warning||"subject list not implemented")
}
if is_ppnull(pplist) then { } else {
  writes_type(myerr,pplist,warning||"pplist not implemented")
}

uverb := unparse(verb)
usubject := unparse(subject)
upplist := unparse(pplist)
every obj := !object do {
	uobject := unparse(obj)
	statement := usubject||blank||uverb||blank||uobject

	if DEBUG=="CYC" then {
	  writes_type(myerr,statement,info||"statement")
	}
	dval := do_cyc_assert(usubject,uverb,uobject)
} # end every obj

if DEBUG=="CYC" then {
  writes_type(myerr,dval,info||"return code: dval")
}
return dval
end

#=========================================================#
#=========================================================#

# list
procedure do_cyc_command(cmd,directobject,option,product,infile,outfile)
#=======================================================================
# do cmd od directobject with option
#	out product from infile to outfile done;
# called by command() in command.icn
local subject,predicate,object
local dval,pipeline,dvallist
static squote,dquote,b
static info,warning
initial {
  info := "INFO: do_cyc_command: "
  warning := "WARNING: do_cyc_command: "
  squote := "\'"
  dquote := "\""
  b := " "
}

subject := "cyc"
predicate := cmd
object := &null
case cmd of {
default: {
	writes_type(myerr,cmd,warning||"unexpected command")
	object := unparse(directobject)
	dval := do_cyc_assert(subject,predicate,object)
	}
"ke-create-now": {
	# (ke-create-now "name")
	# do ke-create-now od name,... done;
	dvallist := []
	every object := !directobject do {
		object := unparse(object)
		#object := squote||dquote||
		#	dequote(object) ||
		#	dquote||squote
		#####put(dval, !do_cyc_command(cmd,object))
pipeline := mk_pipeline("command",subject,predicate,object)
dval := do_pipeline(pipeline)
put(dvallist,dval)
		#####writes_type(myerr,subject,info||"subject")
	}
	#return dvallist
	return dval
	}
"write-image": {
	# (write-image "world/name")
	# do write-image to "world/myworld" done;
	object := unparse(outfile)
	}
"generate-phrase": {
	# (generate-phrase expression)
	# do generate-phrase from expression done;
	object := unparse(infile)
	}
} # end case cmd

pipeline := mk_pipeline("command",subject,predicate,object)
dval := do_pipeline(pipeline)

if DEBUG=="COMMAND" then {
  writes_type(myerr,dval,info||"dval")
}
return dval
end
