All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdm.event.common.functions.Qualify_Shaping Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.event.common.functions;

import cdm.base.staticdata.identifier.IdentifiedList;
import cdm.base.staticdata.party.Counterparty;
import cdm.base.staticdata.party.Party;
import cdm.base.staticdata.party.metafields.ReferenceWithMetaParty;
import cdm.event.common.BusinessEvent;
import cdm.event.common.ExecutionDetails;
import cdm.event.common.Instruction;
import cdm.event.common.PrimitiveInstruction;
import cdm.event.common.SplitInstruction;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.event.common.metafields.ReferenceWithMetaTradeState;
import cdm.product.template.TradableProduct;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.functions.IQualifyFunctionExtension;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.Mapper;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import java.util.Arrays;
import javax.inject.Inject;

import static com.rosetta.model.lib.expression.ExpressionOperators.*;

@ImplementedBy(Qualify_Shaping.Qualify_ShapingDefault.class)
public abstract class Qualify_Shaping implements RosettaFunction,IQualifyFunctionExtension {
	
	// RosettaFunction dependencies
	//
	@Inject protected FilterClosedTradeStates filterClosedTradeStates;
	@Inject protected FilterOpenTradeStates filterOpenTradeStates;
	@Inject protected TradeNoExecutionDetails tradeNoExecutionDetails;

	/**
	* @param businessEvent 
	* @return is_event 
	*/
	@Override
	public Boolean evaluate(BusinessEvent businessEvent) {
		Boolean is_event = doEvaluate(businessEvent);
		
		return is_event;
	}

	protected abstract Boolean doEvaluate(BusinessEvent businessEvent);

	protected abstract Mapper beforeTradeState(BusinessEvent businessEvent);

	protected abstract Mapper closedTradeState(BusinessEvent businessEvent);

	protected abstract Mapper openTradeStates(BusinessEvent businessEvent);

	protected abstract Mapper packageRef(BusinessEvent businessEvent);

	protected abstract Mapper openTradeNoExecutionDetails(BusinessEvent businessEvent);

	public static class Qualify_ShapingDefault extends Qualify_Shaping {
		@Override
		protected Boolean doEvaluate(BusinessEvent businessEvent) {
			Boolean is_event = null;
			return assignOutput(is_event, businessEvent);
		}
		
		protected Boolean assignOutput(Boolean is_event, BusinessEvent businessEvent) {
			is_event = exists(MapperS.of(beforeTradeState(businessEvent).get())).and(exists(MapperS.of(closedTradeState(businessEvent).get()))).and(greaterThan(MapperS.of(MapperC.of(openTradeStates(businessEvent).getMulti()).resultCount()), MapperS.of(Integer.valueOf(1)), CardinalityOperator.All)).and(onlyExists(Arrays.asList(MapperS.of(businessEvent).mapC("getInstruction", eventInstruction -> eventInstruction.getInstruction()).map("getPrimitiveInstruction", instruction -> instruction.getPrimitiveInstruction()).map("getSplit", primitiveInstruction -> primitiveInstruction.getSplit())))).and(areEqual(MapperC.of(openTradeStates(businessEvent).getMulti())
				.mapItem(item -> (MapperS)areEqual(item.map("getTrade", tradeState -> tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).mapC("getCounterparty", tradableProduct -> tradableProduct.getCounterparty()).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()), MapperS.of(beforeTradeState(businessEvent).get()).map("getTrade", tradeState -> tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).mapC("getCounterparty", tradableProduct -> tradableProduct.getCounterparty()).map("getPartyReference", counterparty -> counterparty.getPartyReference()).map("getValue", _f->_f.getValue()), CardinalityOperator.All).asMapper()), MapperS.of(Boolean.valueOf(true)), CardinalityOperator.All)).and(areEqual(MapperS.of(MapperC.of(packageRef(businessEvent).getMulti()).resultCount()), MapperS.of(MapperC.of(openTradeNoExecutionDetails(businessEvent).getMulti()).resultCount()), CardinalityOperator.All)).and(areEqual(MapperS.of(distinct(MapperC.of(packageRef(businessEvent).getMulti())).resultCount()), MapperS.of(Integer.valueOf(1)), CardinalityOperator.All)).get();
			
			return is_event;
		}
		
		@Override
		protected Mapper beforeTradeState(BusinessEvent businessEvent) {
			return MapperS.of(MapperS.of(businessEvent).mapC("getInstruction", eventInstruction -> eventInstruction.getInstruction()).map("getBefore", instruction -> instruction.getBefore()).map("getValue", _f->_f.getValue()).get());
		}
		
		@Override
		protected Mapper closedTradeState(BusinessEvent businessEvent) {
			return MapperS.of(MapperC.of(filterClosedTradeStates.evaluate(MapperS.of(businessEvent).mapC("getAfter", _businessEvent -> _businessEvent.getAfter()).getMulti())).get());
		}
		
		@Override
		protected Mapper openTradeStates(BusinessEvent businessEvent) {
			return MapperC.of(filterOpenTradeStates.evaluate(MapperS.of(businessEvent).mapC("getAfter", _businessEvent -> _businessEvent.getAfter()).getMulti()));
		}
		
		@Override
		protected Mapper packageRef(BusinessEvent businessEvent) {
			return MapperC.of(openTradeStates(businessEvent).getMulti()).map("getTrade", tradeState -> tradeState.getTrade()).map("getExecutionDetails", trade -> trade.getExecutionDetails()).map("getPackageReference", executionDetails -> executionDetails.getPackageReference());
		}
		
		@Override
		protected Mapper openTradeNoExecutionDetails(BusinessEvent businessEvent) {
			return MapperC.of(openTradeStates(businessEvent).getMulti())
				.mapItem(item -> (MapperS)MapperS.of(tradeNoExecutionDetails.evaluate(item.map("getTrade", tradeState -> tradeState.getTrade()).get())));
		}
	}
		
		@Override
		public String getNamePrefix() {
			return "Qualify";
		}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy