2017-08-26 92 views
1

的會話業務有其Java代碼寫成Clojure的:Java的互操作IBM沃森會話業務

import com.ibm.watson.developer_cloud.conversation.v1.ConversationService; 
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest; 
import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse; 

/* 
some code written here 
*/ 
MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build(); 

我的主要問題是如何寫這個

**MessageRequest newMessage = new MessageRequest.Builder().inputText(input).context(context).build();** 
Clojure中

這是我做的,到目前爲止是

(ns clj.core 
(:import 
    (com.ibm.watson.developer_cloud.conversation.v1 ConversationService) 
    (com.ibm.watson.developer_cloud.conversation.v1.model MessageRequest) 
    (com.ibm.watson.developer_cloud.conversation.v1.model MessageResponse))) 

(let [username "foo" 
     password "bar" 
     input "hello" 
     context {} 
     workspaceId "ibm-watson-id" 
     service (ConversationService. "2017-08-26") 
     userPass (.setUsernameAndPassword service username password) 

     ;obviously this is wrong 
     ;dont know how to get this right 
     newMessage (.build (.context context (.inputText input (MessageRequest.Builder.)))) 



     response (.message service workspaceId newMessage)] 
     response) 

請幫助。由於

回答

3

啊哈,終於搞明白了,看來MessageRequest類有一個名爲生成器另一個類

我所做的就是引用這個類和調整要緊的

(ns clj.core 
(:import 
    (com.ibm.watson.developer_cloud.conversation.v1 ConversationService) 
    (com.ibm.watson.developer_cloud.conversation.v1.model MessageRequest) 
    (com.ibm.watson.developer_cloud.conversation.v1.model MessageResponse))) 

(let [username "foo" 
    password "bar" 
    input "hello" 
    context {} 
    workspaceId "ibm-watson-id" 
    service (ConversationService. "2017-08-26") 
    userPass (.setUsernameAndPassword service username password) 

    ;just wanna make it work 
    msgReq (MessageRequest$Builder.) 
    inputText (.inputText msgReq input) 
    content (.context inputText context) 
    newMessage (.build content) 
    response (.execute (.message service workspaceId newMessage))] 
    (println "Watson Response: " response)) 
+1

'doto'是好的( - >(MessageRequest $ Builder。)(.inputText輸入)(.context上下文)(.build)),這些情況下需要對對象進行狀態更改,' - >'對於這些構建器模式來說很好。 )';這樣你就不必考慮命名所有那些在建設者之間的狀態。 – cfrick

+0

太棒了,謝謝 –