Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java互操作和clojure宏_Clojure_Macros_Clojure Java Interop - Fatal编程技术网

java互操作和clojure宏

java互操作和clojure宏,clojure,macros,clojure-java-interop,Clojure,Macros,Clojure Java Interop,我面临以下问题。我试图在hapi fhir api的基础上构造一个函数(宏) function (macro) on topo of hapi-fhir api (defmacro search-patient-resource "This macro searches for a specified resource based on the Patient Id" [res id json?] (let [tmp (symbol res)] (if json?

我面临以下问题。我试图在hapi fhir api的基础上构造一个函数(宏)

function (macro) on topo of hapi-fhir api
  (defmacro search-patient-resource
  "This macro searches for a specified resource based on the
  Patient Id"
  [res id json?]
  (let [tmp (symbol res)]
    (if json?
         `(. (. (. (. (. (. @restful-client search) (forResource ~(symbol res))) encodedJson) (where (. (. ~(resolve tmp)  PATIENT)
                    (hasId (str ~id))))) (returnBundle Bundle)) execute)
    )))
此宏在执行以下操作时起作用

(let [id 10465]
 (search-patient-resource "Observation" id true))
=>#object[ca.uhn.fhir.model.dstu2.resource.Bundle 0x520a3cc9 "Bundle[id=Bundle/9ca62ae1-82af-488f-a166-5b014f45886e]"]
但当我这样做的时候就不行了

 (let [id 10465 res "Observation"]
 (search-patient-resource "Observation" id true))
=> CompilerException java.lang.NullPointerException, compiling:(apycare_emrspp/hapi_fhir_helper.clj:122:1)
当然,我不能写(symbol~res),因为这样读者会进行评估 (符号“观察”)在编译时

 CompilerException java.lang.IllegalArgumentException: No matching method found: forResource for class ca.uhn.fhir.rest.client.GenericCl
 ient$SearchInternal, compiling:(apycare_emrspp/hapi_fhir_helper.clj:122:1)
也不是

   (resolve (symbol ~res) 
也不是

工作

原始java代码如下所示

 FhirContext ctx = FhirContext.forDstu2();
 String serverBase = "fhirtest.uhn.ca/baseDstu2";
 IGenericClient client = ctx.newRestfulGenericClient(serverBase); 
 Bundle results = client .search() .forResource(Observation.class) 
.where(Observation.PATIENT.hasId("1234")) 
.returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) 
.execute(); 
我所做的就是尝试用

 client
 .search() 
 .forResource(another-resource.class) 
 .where(another-resource.PATIENT.hasId(another-id)) 
 .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) 
 .execute();

嗯。我所面临的问题也是由于我在从不同的名称空间调用代码时忽略了导入适当的符号。 当上述代码中的res=“Resource”时 然后

如果我没有首先添加

(import '(ca.uhn.fhir.model.dstu2.resource.Resource))  
在命名空间中调用了宏。 这使得代码在大多数情况下都能正常工作。使其充分发挥功能 我得换衣服

 ~(symbol "Resource")
到 (标识(符号“资源”))

这是java语言的正确翻译

Resource.class
到clojure代码

最后,宏采用以下形式:

 (defmacro search-patient-resource
 "This macro searches for a specified resource based on the
  Patient Id"
 [res id json?]
 (let [tmp (symbol res)]
   (if json?
     `(. (. (. (. (. (. @restful-client search)
                     (forResource (identity ~(symbol res))))
                  encodedJson)
               (where
                 (.
                  (. ~(resolve tmp)  PATIENT)
                  (hasId (~str ~id)))))
            (returnBundle Bundle))
         execute)
     `(. (. (. (. (. @restful-client search)
                  (forResource (identity ~(symbol res))))
               (where (. (. ~(symbol res)
                            PATIENT)
                         (hasId (str ~id)))))
            (returnBundle Bundle))
         execute))))

我不明白你为什么把它写成宏。看起来你可以把它写成一个函数来避免这个问题。怎么会呢?我试图将其作为函数编写,但是如何在clojure编译器不抱怨的情况下传递arbirtery方法名呢?例如,让我们假设我想通过[Observation]调用(.resource-Observation)。如果我将其作为字符串传递,则(.resource(符号“Observation)),除非通过宏从列表中展开,否则将不起作用。请澄清它的工作方式。将字符串和Id作为参数传递,必须将该字符串转换为具有相同名称的java方法并在处调用。这起作用,但在我预定义字符串时不起作用。也就是说,当我传递“观察”时,我有(forResource~(symbol res))被转换为(forResource Observation),但是如果我这样做了(def st“Observation”),那么我得到了(forResource st)。我试图创建一个函数来从字符串创建符号,但它不起作用
 ~(symbol "Resource")
 (defmacro search-patient-resource
 "This macro searches for a specified resource based on the
  Patient Id"
 [res id json?]
 (let [tmp (symbol res)]
   (if json?
     `(. (. (. (. (. (. @restful-client search)
                     (forResource (identity ~(symbol res))))
                  encodedJson)
               (where
                 (.
                  (. ~(resolve tmp)  PATIENT)
                  (hasId (~str ~id)))))
            (returnBundle Bundle))
         execute)
     `(. (. (. (. (. @restful-client search)
                  (forResource (identity ~(symbol res))))
               (where (. (. ~(symbol res)
                            PATIENT)
                         (hasId (str ~id)))))
            (returnBundle Bundle))
         execute))))