Corticon.js Service Callout APIs let you use payloads to add, set, and remove associations between entities. The parameters are:
  • entityParent - The Entity for the association
  • roleName - The association roleName
  • entityChildren - The Entity's association
  • payload - The payload as a JSONObject or JSONArray

add Entities and Associations from a payload

addEntitiesAndAssociations(entityType, payload)

To enrich the data and the working memory you can add entities and associations using this API. The entities will be added as top level entities. This api takes two parameters, the <entityType> and the <payload>. The entityType is the type of entity (defined in the vocabulary) at the root of the payload. The payload itself can either be a JSON Object or a JSONArray. If passing in a JSONObject, the entityType should match the entityType at the root of the JSONObject.

Use Cargo as the Sample
Let's say you want to add another Cargo to the payload for your rules to execute. You can create 
a service callout and append that cargo to the working memory.
For example:.
{
        "volume": 10,
        "container": null,
        "flightPlan": {
            "aircraft": {
                "aircraftType": "Airbus",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "30.000000"
            },
            "flightNumber": 380
        },
        "weight": 1000
}
The above JSON payload has a Cargo at root and its associations nested in the JSON. 
You can use the API to add this Cargo to the working memory.
//Sample Code for using the API as below 
//Metadata for the Service Callout
const asyncAddCargoEntity = {
    func: 'asyncAddCargoEntityFct',
    type: 'ServiceCallout',
    description: {'en_US': 'This function is used to add some data to the working memory.'},
    extensionType: 'SERVICE_CALLOUT',
    name: {'en_US': 'asyncAddCargoEntity'}
};
//Service Callout Function
async function asyncAddCargoEntityFct(corticonDataManager) {
	const logger = corticonDataManager.getLogger();
	//Define the payload i.e your JSON Object
	const payload = {
        "volume": 10,
        "container": null,
        "flightPlan": {
            "aircraft": {
                "aircraftType": "Airbus",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "30.000000"
            },
            "flightNumber": 380
        },
        "weight": 1000
    };
try {
	//Api to add the Cargo to the root. Takes the entity Type and the payload.    
	const returnCode = corticonDataManager.addEntitiesAndAssociations('Cargo', payload);
		if(returnCode === success)
			logger.logDebug(`*** Cargo has been Added`);
	}
	catch ( e ) {
		logger.logError(`*** Error adding entity: ${e}`);
	}    
}
exports.asyncAddCargoEntityFct = asyncAddCargoEntityFct;
In the above example the API will add a Cargo at root and 
also add the nested association FlightPlan and the association Aircraft to the working memory.

You can also add multiple Cargo’s to the working memory. In that case you can pass in a JSON Array of Cargo’s instead of a JSONObject:

eg. To pass JSONArray
//Metadata for the Service Callout
const asyncAddCargoEntity = {
    func: 'asyncAddCargoEntityFct',
    type: 'ServiceCallout',
    description: {'en_US': 'This function is used to add some data to the working memory.'},
    extensionType: 'SERVICE_CALLOUT',
    name: {'en_US': 'asyncAddCargoEntity'}
};
//Service Callout Function
async function asyncAddCargoEntityFct(corticonDataManager) {
	const logger = corticonDataManager.getLogger();
	const payload = [
    {
        "volume": 10,
        "container": null,
        "flightPlan": {
            "aircraft": {
                "aircraftType": "Airbus",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "30.000000"
            },
            "flightNumber": 380
        },
        "weight": 1000
    },
    {
        "volume": 40,
        "container": null,
        "weight": 1000,
		"flightPlan": {
            "aircraft": {
                "aircraftType": "Boeing",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "50.000000"
            },
            "flightNumber": 737
        }
    },
    {
        "volume": 20,
        "container": null,
        "weight": 30000
    },
    {
        "volume": 10,
        "container": null,
        "needsRefrigeration": true,
        "weight": 1000
    }
];
try {
	//API to add Cargo's to the working memory
	//In the above example, 4 Cargo entities will be added to working memory    
	const returnCode = corticonDataManager.addEntitiesAndAssociations('Cargo', payload);
		if(returnCode === success)
			logger.logDebug(`*** Cargos have been Added`);
	}
	catch ( e ) {
		logger.logError(`*** Error adding entity: ${e}`);
	}    
}
//Exports
exports.asyncAddCargoEntityFct = asyncAddCargoEntityFct;
Note: Make sure the payload has the JSON Overrides value . Refer to the JSON Overrides sections here.

addAssociationsToEntity

addAssociationsToEntity(parentEntity, rolename, payload) - To enrich the data and the working memory you can add nested associations to existing entities using this API. The entity to which you want to add associations should be in the working memory . The entity can be at any level in the input tree. This api takes three parameters, the <parentEntity> the <rolename> and the <payload>. The parentEntity is the entity in the working memory to which you want to add the association. The rolename is the association you want to add (defined in the vocabulary). The payload itself can either be a JSON Object or a JSONArray. :
Let say we want to add a flightPlan association to Cargo. 
The JSON payload looks like this -
{
            "aircraft": {
                "aircraftType": "Airbus",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "30.000000"
            },
            "flightNumber": 380
}
The above JSON has FlightPlan and nested Aircraft.
You can add this asscoaition to the existing Cargo using the sample below -
//Metadata for the Service Callout
const asyncAddFlightPlanAssociationEntity = {
    func: 'asyncAddFlightPlanEntityFct',
    type: 'ServiceCallout',
    description: {'en_US': 'This function is used to add some flight plan to the cargo entity.'},
    extensionType: 'SERVICE_CALLOUT',
    name: {'en_US': 'asyncAddFlightPlanAssociationEntity'}
};
//Service Callout Function
async function asyncAddFlightPlanEntityFct(corticonDataManager) {
	const logger = corticonDataManager.getLogger();
	const payload = {
            "aircraft": {
                "aircraftType": "Airbus",
                "maxCargoWeight": null,
                "tailNumber": null,
                "maxCargoVolume": "30.000000"
            },
            "flightNumber": 380
        };
try {
		//This API gets the Cargo entities in working memory
		const entities = corticonDataManager.getEntitiesByType('Cargo');
		/* Logic to determine which Cargo you want to associate this
		   Flight Plan with.
		   In this example we are using volume and if the volume of the Cargo
		   equals 10 then we are going to attach the flight plan to the Cargo
		   The logic determines the parent entity and that parent entity in passed
		   to the API below
		*/
		let entityParent;
		for (let entity of entities) {
			if(entity['volume'] === 10) {
				entityParent = entity; //Determines the parent Entity to be used.
				break;
			}
		}
		/* This call then passes in the parent entity, the role name < FlightPlan> as defined
		in the vocabulary and the above payload. If there are no errors, success is returned from the API
		and the association will be attached to the Cargo */
		const success = corticonDataManager.addAssociationsToEntity(entityParent, "FlightPlan", payload);
		logger.logDebug(`*** Data Added`);
	}
	catch ( e ) {
		logger.logError(`*** Error adding entity: ${e}`);
	}  
}	
//Exports
exports.asyncAddFlightPlanEntityFct = asyncAddFlightPlanEntityFct;
You can also add multiple nested associations to an entity. If you want to add multiple associations to an entity, it must be supported in the vocabulary. To add multiple associations one must pass in a JSONArray:
const asyncAddFlightPlanAssociationEntity = {
    func: 'asyncAddFlightPlanEntityFct',
    type: 'ServiceCallout',
    description: {'en_US': 'This function is used to add some flight plan to the cargo entity.'},
    extensionType: 'SERVICE_CALLOUT',
    name: {'en_US': 'asyncAddFlightPlanAssociationEntity'}
};
async function asyncAddFlightPlanEntityFct(corticonDataManager) {
	const logger = corticonDataManager.getLogger();
	const payload = [{
		"aircraft": {
			"aircraftType": "Airbus",
			"maxCargoWeight": null,
			"tailNumber": null,
			"maxCargoVolume": "30.000000"
		},
		"flightNumber": 380
	},
	{
		"aircraft": {
			"aircraftType": "Boeing",
			"maxCargoWeight": null,
			"tailNumber": null,
			"maxCargoVolume": "30.000000"
		},
		"flightNumber": 737
	},
	{
		"aircraft": {
			"aircraftType": "Airbus",
			"maxCargoWeight": null,
			"tailNumber": null,
			"maxCargoVolume": "30.000000"
		},
		"flightNumber": 350
	},
	{
		"aircraft": {
			"aircraftType": "Boeing",
			"maxCargoWeight": null,
			"tailNumber": null,
			"maxCargoVolume": "30.000000"
		},
		"flightNumber": 747
	}
	];
try {
		//This API gets the Cargo entities in working memory
		const entities = corticonDataManager.getEntitiesByType('Cargo');
		/* Logic to determine which Cargo you want to associate this
		   Flight Plan with.
		   In this example we are using volume and if the volume of the Cargo
		   equals 10 then we are going to attach the flight plan to the Cargo
		   The logic determines the parent entity and that parent entity in passed
		   to the API below
		*/
		let entityParent;
		for (let entity of entities) {
			if(entity['volume'] === 10) {
				entityParent = entity; //Determines the parent Entity to be used.
				break;
			}
		}
		/* This call then passes in the parent entity, the role name < FlightPlan> as defined
		in the vocabulary and the above payload. If there are no errors, success is returned from the API
		and the association will be attached to the Cargo. It will add 4 Flight Plan to the Cargo  */
		const success = corticonDataManager.addAssociationsToEntity(entityParent, "FlightPlan", payload);
		logger.logDebug(`*** Data Added`);
	}
	catch ( e ) {
		logger.logError(`*** Error adding entity: ${e}`);
	}  
}	
exports.asyncAddFlightPlanEntityFct = asyncAddFlightPlanEntityFct;