查询Concept中特定时段可用Doctor数组返回NULL,求正确写法
问题分析与解决方案
先拆解你的问题,核心是查询语句的逻辑和语法有误,我们一步步来修正:
你的模型定义
participant Doctor identified by doctorID { o String doctorID o String contact o String doctorName o String description --> Schedule schedule } asset Schedule identified by scheduleID{ o String scheduleID o Day availableDays } concept Time{ --> Doctor[] AM00 --> Doctor[] AM01 --> Doctor[] AM02 --> Doctor[] AM03 --> Doctor[] AM04 --> Doctor[] AM05 --> Doctor[] AM06 --> Doctor[] AM07 --> Doctor[] AM08 --> Doctor[] AM09 --> Doctor[] AM10 --> Doctor[] AM11 --> Doctor[] PM00 --> Doctor[] PM01 --> Doctor[] PM02 --> Doctor[] PM03 --> Doctor[] PM04 --> Doctor[] PM05 --> Doctor[] PM06 --> Doctor[] PM07 --> Doctor[] PM08 --> Doctor[] PM09 --> Doctor[] PM10 --> Doctor[] PM11 } concept Day{ o Time monday o Time tuesday o Time wednesday o Time thursday o Time friday o Time saturday o Time sunday }
你尝试的查询语句
query getAvailableDoctors { description: "Returns a list of doctors available at a specific hour" statement: SELECT org.acme.Doctor FROM org.acme.Schedule WHERE (schedule.availableDays.monday.AM00 CONTAINS('resource:org.acme.Doctor') ) }
问题根源
你的查询有两个关键错误:
- 实体范围不匹配:你要查询的是
Doctor,但FROM子句指定了Schedule,这会导致系统尝试从Schedule实体中提取Doctor,逻辑完全错位。 - CONTAINS用法错误:
CONTAINS需要匹配具体的资源引用,而不是类型字符串;而且你的需求是取出该时段的所有医生,不是判断是否包含某类医生。
正确的查询写法
写法一:直接查询符合条件的Doctor
利用Doctor与Schedule的关联关系,反向查找被包含在目标时段的医生:
query getAvailableDoctors { description: "Returns a list of doctors available at a specific hour" statement: SELECT org.acme.Doctor WHERE (schedule.availableDays.monday.AM00 CONTAINS(this)) }
这里的this指代当前遍历的Doctor实例,查询会自动筛选出所有在周一凌晨0点时段的Schedule中被标记为可用的医生。
写法二:从Schedule中提取目标时段的医生数组
如果想直接从Schedule实体中取出对应时段的医生列表,可以调整查询返回内容:
query getAvailableDoctors { description: "Returns a list of doctors available at a specific hour" statement: SELECT schedule.availableDays.monday.AM00 AS availableDoctors FROM org.acme.Schedule }
如果需要指定某个具体的Schedule,可以在WHERE子句中添加scheduleID == '你的日程ID'来过滤。
更灵活的优化方案
如果想支持动态指定星期和时段(比如不固定是周一AM00),可以使用参数化查询:
query getAvailableDoctors($day: String, $hour: String) { description: "Returns a list of doctors available at a specific day and hour" statement: SELECT org.acme.Doctor WHERE (schedule.availableDays[$day][$hour] CONTAINS(this)) }
调用时传入参数即可,比如{"day": "monday", "hour": "AM00"},这样就能动态查询任意时段的可用医生了。
内容的提问来源于stack exchange,提问作者tahaf10
相关产品推荐
相关产品推荐

