我可以更改关系中定义的参数吗?

时间:2018-11-15 12:39:04

标签: hyperledger-fabric hyperledger hyperledger-composer hyperledger-explorer

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

我想要做的是使用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 file.to 访问进行更改。

 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();
     });

 }

1 个答案:

答案 0 :(得分:1)

  1. 您的问题似乎是:您可以在事务功能(在链代码运行时中运行)中创建从飞行(资产)到飞机(资产)的关系,并且可以更新相关飞机的字段吗? (在其单独的注册表中)。答案是肯定的。您没有为private $checkIsTrue = new Subject<any>(); 函数提供模型,因此只能对其模型定义进行假设。至少(根据您的代码)将需要:

    bookflight

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

  3. 本节:

    transaction bookflight { } const bnDef = bnUtil.connection.getBusinessNetwork();

是作曲者-客户端代码-不会在事务功能内起作用(即运行时代码,您需要删除客户端代码-下面的示例显示“操作方法”。)将第2行替换为:

const factory = bnDef.getFactory();

有关交易功能,示例等的更多信息,请访问https://hyperledger.github.io/composer/latest/reference/js_scripts

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

    const factory = getFactory();

我看不到您的代码来更新Aircraft注册表(使用flightResource.route.origin = 'DEL' ; // no need for.setPropertyValue('origin', 'DEL'); etc etc FYI),但是您需要使用该代码来更新相关资产中的字段(目前,您只在更新上面的Flight注册表) )

  1. flightResource.aircraft是不确定的代码-如果您希望与多个同行/组织达成共识。

  2. 您会注意到我之前发送的链接,显示了new Date()的使用,而不是JS承诺(.then等)的使用-易于编码,易于阅读。欢呼。