aggregator-core/src/main/java/org/arrowhead/wp5/agg/impl/billing/AggregatorBillFactory.java
public static AggregatorBill generateBill(String customerId, AggregatorContract contract, Collection<FlexOffer> flexOffers) {
		AggregatorBill bill = new AggregatorBill();

		bill.setCustomerId(customerId);
		
		// Calculate statistics 
		for(FlexOffer f : flexOffers) {
			
			if(!isExecutedCorrectly(f)) {	
				continue;		/* Flexoffer was not executed correctly - no reward is provided */
			}
			
			/* Increment flexoffers*/
			bill.setNumFlexOffers(bill.getNumFlexOffers() + 1);
				
			/* Add time flexibility part */
			bill.setTotalTimeFlex(bill.getTotalTimeFlex() + (f.getStartBeforeInterval() - f.getStartAfterInterval()));
			
			/* Add energy flexibility part */
			for(FlexOfferSlice s : f.getSlices()) {
				bill.setTotalEnergyFlex(bill.getTotalEnergyFlex() + (s.getEnergyUpper() - s.getEnergyLower()));
			}
			
			/* Add scheduling part */
			if ((f.getDefaultSchedule() != null) && (f.getFlexOfferSchedule() != null) &&
				(f.getFlexOfferSchedule().getEnergyAmounts().length == f.getSlices().length) &&					
				(f.getFlexOfferSchedule().getEnergyAmounts().length == f.getDefaultSchedule().getEnergyAmounts().length) &&
				(!f.getFlexOfferSchedule().equals(f.getDefaultSchedule()))) {
				
				// Check if there're baseline deviations
				boolean deviations = f.getDefaultSchedule().getStartInterval() != f.getFlexOfferSchedule().getStartInterval();
				for(int sid = 0; sid < f.getSlices().length; sid++) {
					if (Math.abs(f.getDefaultSchedule().getEnergyAmount(sid) - 
								 f.getFlexOfferSchedule().getEnergyAmount(sid)) > 1e-3) {
						deviations = true;
						break;
					}
				}		
				
				if (deviations) {					
				
					/* Incremenent custom schedule activation count */
					bill.setNumCustomScheduleActivations(bill.getNumCustomScheduleActivations() + 1);
				
					/* Give a reward depending on deviations from default ... */				
					/* ... for start-time deviation */
					bill.setTotalStartTimeDeviations(bill.getTotalStartTimeDeviations() + 
							Math.abs(f.getFlexOfferSchedule().getStartInterval() - f.getDefaultSchedule().getStartInterval()));
									
					/* ... for energy deviations */
					for(int i=0; i<f.getFlexOfferSchedule().getEnergyAmounts().length; i++) {
						bill.setTotalEnergyDeviations(bill.getTotalEnergyDeviations() + Math.abs(f.getFlexOfferSchedule().getEnergyAmount(i) - 
																	    f.getDefaultSchedule().getEnergyAmount(i)));
					}	
				}	
			}
		}
		
		/* Compute actual reward amounts */		
		bill.setRewardFixed(flexOffers.isEmpty() ? 0 : contract.getFixedReward());
		bill.setRewardTotalTimeFlex(bill.getTotalTimeFlex() * contract.getTimeFlexReward());
		bill.setRewardTotalEnergyFlex(bill.getTotalEnergyFlex() * contract.getEnergyFlexReward());		
		bill.setRewardTotalSchedFixed(bill.getNumCustomScheduleActivations() * contract.getSchedulingFixedReward());
		bill.setRewardTotalSchedEST(bill.getTotalStartTimeDeviations() * contract.getSchedulingStartTimeReward());
		bill.setRewardTotalSchedEnergy(bill.getTotalEnergyDeviations() * contract.getSchedulingEnergyReward());
		bill.setRewardTotal(bill.getRewardFixed() + 
						    bill.getRewardTotalEnergyFlex() + 
						    bill.getRewardTotalSchedEnergy() +
						    bill.getRewardTotalSchedEST() + 
						    bill.getRewardTotalSchedFixed() + 
						    bill.getRewardTotalTimeFlex());
		
		return bill;
	}
/* Generate bill for the customer, after flexoffer execution */
Generate bill for the customer, after flexoffer execution
Generate bill for the customer after flexoffer execution
public static AggregatorBill generateBill(String customerId, AggregatorContract contract, Collection<FlexOffer> flexOffers) {
		AggregatorBill bill = new AggregatorBill();
		bill.setCustomerId(customerId);
		for(FlexOffer f : flexOffers) {
			if(!isExecutedCorrectly(f)) {	
				continue;		/* Flexoffer was not executed correctly - no reward is provided */
			}
			/* Increment flexoffers*/
			bill.setNumFlexOffers(bill.getNumFlexOffers() + 1);
			/* Add time flexibility part */
			bill.setTotalTimeFlex(bill.getTotalTimeFlex() + (f.getStartBeforeInterval() - f.getStartAfterInterval()));
			/* Add energy flexibility part */
			for(FlexOfferSlice s : f.getSlices()) {
				bill.setTotalEnergyFlex(bill.getTotalEnergyFlex() + (s.getEnergyUpper() - s.getEnergyLower()));
			}
			/* Add scheduling part */
			if ((f.getDefaultSchedule() != null) && (f.getFlexOfferSchedule() != null) &&
				(f.getFlexOfferSchedule().getEnergyAmounts().length == f.getSlices().length) &&					
				(f.getFlexOfferSchedule().getEnergyAmounts().length == f.getDefaultSchedule().getEnergyAmounts().length) &&
				(!f.getFlexOfferSchedule().equals(f.getDefaultSchedule()))) {
				boolean deviations = f.getDefaultSchedule().getStartInterval() != f.getFlexOfferSchedule().getStartInterval();
				for(int sid = 0; sid < f.getSlices().length; sid++) {
					if (Math.abs(f.getDefaultSchedule().getEnergyAmount(sid) - 
								 f.getFlexOfferSchedule().getEnergyAmount(sid)) > 1e-3) {
						deviations = true;
						break;
					}
				}		
				if (deviations) {					
					/* Incremenent custom schedule activation count */
					bill.setNumCustomScheduleActivations(bill.getNumCustomScheduleActivations() + 1);
					/* Give a reward depending on deviations from default ... */				
					/* ... for start-time deviation */
					bill.setTotalStartTimeDeviations(bill.getTotalStartTimeDeviations() + 
							Math.abs(f.getFlexOfferSchedule().getStartInterval() - f.getDefaultSchedule().getStartInterval()));
					/* ... for energy deviations */
					for(int i=0; i<f.getFlexOfferSchedule().getEnergyAmounts().length; i++) {
						bill.setTotalEnergyDeviations(bill.getTotalEnergyDeviations() + Math.abs(f.getFlexOfferSchedule().getEnergyAmount(i) - 
																	    f.getDefaultSchedule().getEnergyAmount(i)));
					}	
				}	
			}
		}
		/* Compute actual reward amounts */		
		bill.setRewardFixed(flexOffers.isEmpty() ? 0 : contract.getFixedReward());
		bill.setRewardTotalTimeFlex(bill.getTotalTimeFlex() * contract.getTimeFlexReward());
		bill.setRewardTotalEnergyFlex(bill.getTotalEnergyFlex() * contract.getEnergyFlexReward());		
		bill.setRewardTotalSchedFixed(bill.getNumCustomScheduleActivations() * contract.getSchedulingFixedReward());
		bill.setRewardTotalSchedEST(bill.getTotalStartTimeDeviations() * contract.getSchedulingStartTimeReward());
		bill.setRewardTotalSchedEnergy(bill.getTotalEnergyDeviations() * contract.getSchedulingEnergyReward());
		bill.setRewardTotal(bill.getRewardFixed() + 
						    bill.getRewardTotalEnergyFlex() + 
						    bill.getRewardTotalSchedEnergy() +
						    bill.getRewardTotalSchedEST() + 
						    bill.getRewardTotalSchedFixed() + 
						    bill.getRewardTotalTimeFlex());
		return bill;
	}
public static AggregatorBill generateBill ( String customerId , AggregatorContract contract , Collection < FlexOffer > flexOffers ) { AggregatorBill bill = new AggregatorBill ( ) ; bill . setCustomerId ( customerId ) ; for ( FlexOffer f : flexOffers ) { if ( ! isExecutedCorrectly ( f ) ) { continue ; /* Flexoffer was not executed correctly - no reward is provided */ } /* Increment flexoffers*/ bill . setNumFlexOffers ( bill . getNumFlexOffers ( ) + 1 ) ; /* Add time flexibility part */ bill . setTotalTimeFlex ( bill . getTotalTimeFlex ( ) + ( f . getStartBeforeInterval ( ) - f . getStartAfterInterval ( ) ) ) ; /* Add energy flexibility part */ for ( FlexOfferSlice s : f . getSlices ( ) ) { bill . setTotalEnergyFlex ( bill . getTotalEnergyFlex ( ) + ( s . getEnergyUpper ( ) - s . getEnergyLower ( ) ) ) ; } /* Add scheduling part */ if ( ( f . getDefaultSchedule ( ) != null ) && ( f . getFlexOfferSchedule ( ) != null ) && ( f . getFlexOfferSchedule ( ) . getEnergyAmounts ( ) . length == f . getSlices ( ) . length ) && ( f . getFlexOfferSchedule ( ) . getEnergyAmounts ( ) . length == f . getDefaultSchedule ( ) . getEnergyAmounts ( ) . length ) && ( ! f . getFlexOfferSchedule ( ) . equals ( f . getDefaultSchedule ( ) ) ) ) { boolean deviations = f . getDefaultSchedule ( ) . getStartInterval ( ) != f . getFlexOfferSchedule ( ) . getStartInterval ( ) ; for ( int sid = 0 ; sid < f . getSlices ( ) . length ; sid ++ ) { if ( Math . abs ( f . getDefaultSchedule ( ) . getEnergyAmount ( sid ) - f . getFlexOfferSchedule ( ) . getEnergyAmount ( sid ) ) > 1e-3 ) { deviations = true ; break ; } } if ( deviations ) { /* Incremenent custom schedule activation count */ bill . setNumCustomScheduleActivations ( bill . getNumCustomScheduleActivations ( ) + 1 ) ; /* Give a reward depending on deviations from default ... */ /* ... for start-time deviation */ bill . setTotalStartTimeDeviations ( bill . getTotalStartTimeDeviations ( ) + Math . abs ( f . getFlexOfferSchedule ( ) . getStartInterval ( ) - f . getDefaultSchedule ( ) . getStartInterval ( ) ) ) ; /* ... for energy deviations */ for ( int i = 0 ; i < f . getFlexOfferSchedule ( ) . getEnergyAmounts ( ) . length ; i ++ ) { bill . setTotalEnergyDeviations ( bill . getTotalEnergyDeviations ( ) + Math . abs ( f . getFlexOfferSchedule ( ) . getEnergyAmount ( i ) - f . getDefaultSchedule ( ) . getEnergyAmount ( i ) ) ) ; } } } } /* Compute actual reward amounts */ bill . setRewardFixed ( flexOffers . isEmpty ( ) ? 0 : contract . getFixedReward ( ) ) ; bill . setRewardTotalTimeFlex ( bill . getTotalTimeFlex ( ) * contract . getTimeFlexReward ( ) ) ; bill . setRewardTotalEnergyFlex ( bill . getTotalEnergyFlex ( ) * contract . getEnergyFlexReward ( ) ) ; bill . setRewardTotalSchedFixed ( bill . getNumCustomScheduleActivations ( ) * contract . getSchedulingFixedReward ( ) ) ; bill . setRewardTotalSchedEST ( bill . getTotalStartTimeDeviations ( ) * contract . getSchedulingStartTimeReward ( ) ) ; bill . setRewardTotalSchedEnergy ( bill . getTotalEnergyDeviations ( ) * contract . getSchedulingEnergyReward ( ) ) ; bill . setRewardTotal ( bill . getRewardFixed ( ) + bill . getRewardTotalEnergyFlex ( ) + bill . getRewardTotalSchedEnergy ( ) + bill . getRewardTotalSchedEST ( ) + bill . getRewardTotalSchedFixed ( ) + bill . getRewardTotalTimeFlex ( ) ) ; return bill ; }
Generate bill for the customer, after flexoffer execution
Generate bill for the customer after flexoffer execution
// Calculate statistics  // Check if there're baseline deviations