如何更新事务处理器函数中的关系值?

How to update the relationship values inside transaction processor functions?

我有一个模型文件 org.acme.interm.container.cto,看起来像这样:

namespace org.acme.interm.container

asset Container identified by containerId {

  o String containerId 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 containerNumber
  --> Truck truck  optional
}

asset Truck identified by truckId {
  o String truckId          
  o Ownership ownershipType default="LEASED"
  o Integer normalWeight range = [,100]
  o Integer fragileWeight range = [,50]          
}

enum Ownership {
  o LEASED
  o OWNED
}

transaction AssignTruck {
  o String containerId
  o String truckId
}

event TruckAssigned {
  o String containerId
  o String truckId
}

transaction LoadContainer {
  o String containerId
  --> Truck    truck
  o Integer fragileWeight
  o Integer normalWeight   
}

我在以下交易中将 Truck 关系分配给集装箱资产:

function AssignTruck(containerTruckData) {  
   console.log("ASSIGN TRUCK CALLED!");
   var containerRegistry={}
   return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
            containerRegistry = registry
            return containerRegistry.get(containerTruckData.containerId);
        }).then(function(container){
            console.log("Got Container",container);
            if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
            var factory = getFactory();
            var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
            container.truck = relationship;

            return containerRegistry.update(container);
        }).then(function(){
            // Successful update
            var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
            event.containerId = containerTruckData.containerId;
            event.truckId = containerTruckData.truckId;
            emit(event);
        }).catch(function(error){
            throw new Error(error);
        });

}

现在,我如何编写 loadContainer 事务,其中容器的 normalWeight 和 fragileWeight 属性的现有值(对于给定的 containerId)应该添加到新传递的 fragileWeight 和 normalWeight 价值观?

首先,除非每个容器都有一个重量变量,否则不可能跟踪单个重量。即使你不断更新卡车的重量,你也会知道集装箱的总重量是不够的。特别是如果你想卸载容器。我会建议这种方法

asset Container identified by containerId {

  o String containerId 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 containerNumber
  o Integer normalWeight range = [,100]
  o Integer fragileWeight range = [,50]    
  --> Truck truck  optional
}

asset Truck identified by truckId {
  o String truckId          
  o Ownership ownershipType default="LEASED"
  o Integer totalNormalWeight range default=0
  o Integer totalFragileWeight range default=0
  --> Container containersLoaded[] optional
}

这样您就可以跟踪每个集装箱的重量,然后将它们相加以获得卡车的总重量。 containersLoaded 变量可让您跟踪装载的容器。这样做的一个好处是,如果一个容器被卸载或拆分(我假设这将是用户流程的一部分),您可以准确地减轻重量

function AssignTruck(containerTruckData) {
   var myContainer; // store data from the promise
   var myTruck;
   console.log("ASSIGN TRUCK CALLED!");
   var containerRegistry={}
   var truckRegistry;
   return getAssetRegistry('org.acme.interm.container.Container').then(function(registry){
            containerRegistry = registry
            return containerRegistry.get(containerTruckData.containerId);
        }).then(function(container){
            console.log("Got Container",container);
            myContainer = container;
            if(!container) throw new Error("Container : "+containerTruckData.containerId," Not Found!!!");
            var factory = getFactory();
            var relationship = factory.newRelationship('org.acme.interm.truck','Truck',containerTruckData.truckId);
            container.truck = relationship;
            return containerRegistry.update(container);
        }).then(function() {
          return getAssetRegistry('org.acme.interm.truck.Truck');
        }).then(function (truckRegistry) {
          truckRegistry = truckRegistry;
          return truckRegistry.get(containerTruckData.truckId);
        }).then(function (truck) {
          truck.totalNormalWeight +=  myContainer.normalWeight;
          truck.totalFragileWeight += myContainer.fragileWeight;
          if (!truck.containersLoaded) {
            truck.containersLoaded = [];
          } else {
            truck.containersLoaded.push(factory.newRelationship('org.acme.interm.container', 'Container', myContainer.containerId))
          };
          return truckRegistry.update(truck)
        }).then(function(){
            // Successful update
            var event = getFactory().newEvent('org.acme.interm.container', 'TruckAssigned');
            event.containerId = containerTruckData.containerId;
            event.truckId = containerTruckData.truckId;
            emit(event);
        }).catch(function(error){
            throw new Error(error);
        });
}

这应该适合您。现在您可以通过检查 totalNormalWeighttotalFragileWeight 变量来查询卡车并获得所有集装箱的总重量。您还可以检查 containersLoaded 数组以获取容器列表。要获取容器的单个重量,您可以查询容器并获取其重量,也可以直接从 containersLoaded 变量中获取。 当需要从卡车上移除/卸载集装箱时,您可以获取它们的单个重量并从总重量中减去,并从 containersLoaded.

中弹出关系