hyperledger-fabrichyperledgerhyperledger-composerhyperledger-explorer

Can i make changes to the parameters defined in relationships


1.i want to know whether i can make changes to the parmeter defined in the relationship.

what i want to do is to make a function with name of bookflight and then make changes to the number of seats if the flight is booked . these are my cto files

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
}

and for flight the code is

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
}

now what i want to make changes to reltionship in the flight to make changes to it what should i do . i have made a javascript file.to access make changes in it.

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

 }

Solution

    1. your question appears to be: can you create a relationship from Flight (asset) to Aircraft (asset) in a transaction function (that operates in the chaincode runtime), and can you update the fields of the related Aircraft (in its separate registry). The answer is 'yes', you can, for both. You don't provide the model for the bookflight function, so can only make assumptions about its model definition. as a minimum (based on your code) it will need:

      transaction bookflight { }

    2. A code example of what you are trying to do with relationships - is shown here -> https://github.com/hyperledger/composer-sample-networks/blob/master/packages/perishable-network/lib/logic.js#L130

    3. This section:

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

    is composer-client code - and will not work inside a transaction function (that is runtime code, you need to remove client code - the example below shows 'how' to do it.) Replace line 2 with :

    const factory = getFactory();

    See more on transaction functions, examples etc at https://hyperledger.github.io/composer/latest/reference/js_scripts

    1. note: you can just assign values like:

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

    I don't see your code to update the Aircraft registry (with flightResource.aircraft FYI) - but you would need that to update the fields in that related asset (presently, you're only updating the Flight registry above )

    1. new Date() is non deterministic code - if you're hoping to achieve consensus from multiple peers/orgs.

    2. You'll notice the link I sent earlier, shows the use of async/await rather than JS promises (.then etc etc) - easier to code, easier to read. cheers.