1

1.我想知道我是否可以更改关系中定义的参数。

我想做的是用 bookflight 的名称创建一个函数,然后在预订航班时更改座位数。这些是我的 cto 文件

namespace org.acme.airline.aircraft

/** Aircraft is an ACME Asset*/

asset Aircraft identified by aircraftId {
  o String      aircraftId 

  o Ownership   ownershipType default="LEASED"

  // Number of seats per class 
  o Integer     firstClassSeats      range = [4,]
  o Integer     businessClassSeats   range = [6, 20]
  o Integer     economyClassSeats    range = [30, ]

  o String      nickName  optional 
}

enum Ownership {
  o   LEASED
  o   OWNED
}

对于航班,代码是

namespace org.acme.airline.flight

import org.acme.airline.aircraft.Aircraft

asset Flight identified by flightId {
  // Solution to the exercise - try out the Regular expression at http://regex101.com
  // Share your optimized regular expression with others :) 
  o   String            flightId regex=/[A-Z][A-Z][0-9][0-9][0-9]-[0-9][0-9]-[0-3][0-9]-[0-9][0-9]/
  o   String            flightNumber
  o   Route             route
  o   String[]          aliasFlightNumber  optional
  --> Aircraft          aircraft  optional
}

concept Route {
  o   String    origin       regex=/[A-Z][A-Z][A-Z]/
  o   String    destination  regex=/[A-Z][A-Z][A-Z]/
  o   DateTime  schedule  
}

// Logistics department of ACME creates the flights
transaction CreateFlight {
  o   String      flightNumber
  o   String      origin
  o   String      destination
  o   DateTime    schedule
}

event FlightCreated {
  o   String      flightId
}

// Assigns an aircraft to the flight
// The logistics / validation on availability of aircraft
// Kept outside of the Blockchain
transaction AssignAircraft {
  o   String    flightId
  o   String    aircraftId
}

// Event indicating that aircraft was assigned
event AircraftAssigned {
  o   String    flightId
  o   String    aircraftId
}

现在我想改变飞行中的关系来改变它我应该怎么做。我已经制作了一个 javascript 文件。要访问它,请对其进行更改。

 function booktickets(registry){
     //array to recored the hold the instances of aircraft resourse 
     const  bnDef = bnUtil.connection.getBusinessNetwork();
     const  factory = bnDef.getFactory();
     let    flightResource = factory.newResource(aircraftNamespace,aircraftType,'AE201-05-05-2020');
     flightResource.setPropertyValue('flightNumber','AE101');
     flightResource.route = factory.newConcept(aircraftNamespace,'Route');
     flightResource.route.setPropertyValue('origin', 'DEL');
     flightResource.route.setPropertyValue('destination' , 'APH');
     flightResource.route.setPropertyValue('schedule' , new Date('2018-10-15T21:44Z'));
     flightResource.aircraft = factory.newRelationship('org.acme.airline.aircraft', 'Aircraft', 'CRAFT01');
    //.setPropertyValue()
    flightResource.aircraft.setPropertyValue('ownershipType','LEASED');
    flightResource.aircraft.setPropertyValue('firstClassSeats',10);
    flightResource.aircraft.setPropertyValue('businessClassSeats',10);
    flightResource.aircraft.setPropertyValue('economyClassSeats',100);

     return registry.update(flightResource).then(()=>{
         console.log('Successfully created the flight!!!');
         bnUtil.disconnect();
     }).catch((error)=>{
         console.log(error);
        bnUtil.disconnect();
     });

 }
4

1 回答 1

1
  1. 您的问题似乎是:您能否在交易函数(在链代码运行时中运行)中创建从航班(资产)到飞机(资产)的关系,并且您能否更新相关飞机的字段(在其单独的注册表中) . 答案是“是”,你可以,两者都可以。您不提供bookflight函数的模型,因此只能对其模型定义进行假设。至少(根据您的代码)它将需要:

    transaction bookflight { }

  2. 您尝试处理关系的代码示例 - 显示在这里 -> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/perishable-network/lib/logic.js# L130

  3. 本节:

    const bnDef = bnUtil.connection.getBusinessNetwork(); const factory = bnDef.getFactory();

是 composer-client 代码 - 并且不会在事务函数中工作(即运行时代码,您需要删除客户端代码 - 下面的示例显示了“如何”执行此操作。)将第 2 行替换为:

const factory = getFactory();

在https://hyperledger.github.io/composer/latest/reference/js_scripts查看更多关于交易功能、示例等的信息

  1. 注意:您可以分配如下值:

    flightResource.route.origin = 'DEL' ; // no need for.setPropertyValue('origin', 'DEL'); etc etc

我没有看到您更新飞机注册表的代码(使用flightResource.aircraftFYI)-但您需要更新该相关资产中的字段(目前,您只更新上面的航班注册表)

  1. new Date()是非确定性代码 - 如果您希望获得多个同行/组织的共识。

  2. 你会注意到我之前发送的链接,显示了使用async/await而不是 JS 承诺(.then 等) - 更容易编码,更容易阅读。干杯。

于 2018-11-16T10:37:34.387 回答