通过概念查询参与者数组

时间:2018-05-29 16:16:38

标签: hyperledger hyperledger-composer

这是我的模型文件的样子。

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
}

我试图编写一个查询,显示特定时刻所有可用的医生。例如,如果我在周一凌晨00点寻找医生,那么应该返回AM00的医生阵列。

我正在尝试这样的事情

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') )
}

但是当存在AM00数组中的实际医生时,所有这一切都会返回NULL。我做错了吗?或者有更好的方法来查询这个吗?

1 个答案:

答案 0 :(得分:1)

我建议如下所示[模型,查询定义等] - 它基于你去了“概念”的事实。 route(你也可以使用普通数组并查询一个Days / Hours数组,稍微不同的模型,但那是另一天)。您的查询(上图)不起作用,因为您无法执行此操作。通过查询定义中的对象 - 它不像SQL数据库(只是为了清除它)。

以下模型提供概念Availability以捕获医生的可用性矩阵,在资产Schedule中(因此与医生的关系)。此外,它是一系列概念,允许每天在每天内安排该计划ID。最后,医生(或一系列医生) - 可以与该时间表相关联 - 如果是一对一的话,那么显然你只需要提供一名医生'条目。请参阅下面的schedule示例JSON数据。同样,医生(参与者)可以与一个或多个时间表有关系(或者不是,视情况而定 - 无论如何都有选项 - 正如您在Doctor参与者中看到的那样,我有一个可选的schedule(一组时间表) - 在这种情况下,它会将概念数据(在资产内)存储在实际的参与者记录上(然后你会查询它) - 但你可能不想管理时间表参与者内部的数据显而易见的原因)。注意:根据您发布的内容,这只是一个建议 - 有很多方法可以剥橙: - )

participant Doctor identified by doctorID {
o String doctorID
o String contact 
o String doctorName
o String description
o Schedule[] schedule optional // see comment below
}

asset Schedule identified by scheduleID {
 o String scheduleID
 o Availability[] availability
 --> Doctor[] doctor
}

concept Availability {
o WeekDay Day 
o Hours Hour 
}

enum WeekDay {
o MON
o TUE
o WED
o THU
o FRI
o SAT
o SUN
}

enum Hours {
o H00
o H01
o H02
o H03
o H04
o H05
o H06
o H07
o H08
o H09
o H10
o H11
o H12
o H13
o H14
o H15
o H16
o H17
o H18
o H19
o H20
o H21
o H22
o H23

}

使用该模型,您现在可以定义概念查询。

示例1:

query doctordoctor1 { 
  description: "Select all doctors available at 1st hour on MON only" 
  statement: SELECT org.acme.mynetwork.Schedule 
  WHERE ( availability CONTAINS (Day == "MON" AND Hour == "H01" )   ) 
}

仅返回附表#4,因此医生#1和#2(而不是医生3或4)

示例2:

query doctordoctor2 { 
  description: "Select all doctors available at 1st hour - any day" 
  statement: SELECT org.acme.mynetwork.Schedule 
  WHERE ( availability CONTAINS (Hour == "H01" )   ) 
}

将返回Schedule#4和#5,这意味着医生#1和#2 - 加上 - 医生#3和#4都将匹配。

请注意,它不会返回该查询中Doctor's配置文件的详细信息请注意 - 您可以在事务中调用查询,然后为每个找到的医生获取相关的医生信息(如果需要) ,事实上,返回的查询对象(日程表)存在关系。

=================== DATA =========(留在这里,但可以放入pastebin)

{
  "$class": "org.acme.mynetwork.Schedule",
  "scheduleID": "4",
  "availability": [
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "MON",
      "Hour": "H01"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "MON",
      "Hour": "H02"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "MON",
      "Hour": "H03"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "TUE",
      "Hour": "H04"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "TUE",
      "Hour": "H05"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "TUE",
      "Hour": "H06"
    }
  ],
  "doctor": [
    "resource:org.acme.mynetwork.Doctor#1",
    "resource:org.acme.mynetwork.Doctor#2"
  ]
}

下一个项目

{
  "$class": "org.acme.mynetwork.Schedule",
  "scheduleID": "5",
  "availability": [
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "WED",
      "Hour": "H01"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "WED",
      "Hour": "H02"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "THU",
      "Hour": "H03"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "FRI",
      "Hour": "H04"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "SAT",
      "Hour": "H05"
    },
    {
      "$class": "org.acme.mynetwork.Availability",
      "Day": "SAT",
      "Hour": "H06"
    }
  ],
  "doctor": [
    "resource:org.acme.mynetwork.Doctor#3",
    "resource:org.acme.mynetwork.Doctor#4"
  ]
}

==== CODE从事务处理器调用查询 - 或者只是调用它

来自REST API的

/**
 * Track the schedule
 * @param {org.acme.mynetwork.doctor} doctor - the query object as a trxn
 * @transaction
 */

function doctor(doctor) {

  console.log('got inside doctor function');
  return query('doctordoctor') // 
        .then(function (results) {

           var schedule_asset = [];
           for (var n = 0; n < results.length; n++) {
                   var schedule_asset = results[n];
                   console.log('The transaction object ' + (n+1) + ', object is ' +  schedule_asset);
                   console.log('Schedule identifier is: ' + schedule_asset.getIdentifier()); 
                   console.log(' Doctor id is ' + schedule_asset.doctor[0].getIdentifier()); 


           }

      });  

}