Git Product home page Git Product logo

Comments (3)

dhuebner avatar dhuebner commented on August 19, 2024

@jim-h-wang

By looking into #77 (comment)

Imaging having a func with assign-outputs and no calculation annotation.
Where should the code for assign-outputs be generated?

from rune-dsl.

jim-h-wang avatar jim-h-wang commented on August 19, 2024

@dhuebner

After our discussions this afternoon, here is the java code that I think we should generate. A few points to note:

  1. doEvaluate is a protected abstract method, which should be overridden by the implementor
  2. the assign-output logic is moved into a private method that returns a Builder object that is subsequently used to evaluate post-conditions
  3. The default implementation should return an empty builder object instead of an exception. From making use of the Functions, it was annoying to have to create no-op implementations of every function.
  4. post-conditions to remain where they are currently in the evaluate method, to be placed after the assignOutput call and before objectValidator.validateAndFailOnErorr(...)
@ImplementedBy(ResolveEquityContract.ResolveEquityContractDefault.class)
public abstract class ResolveEquityContract implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	@Inject protected CalculationPeriod calculationPeriod;
	@Inject protected EquityPerformance equityPerformance;

	/**
	* @param contractState 
	* @param observation 
	* @param date 
	* @return updatedContract 
	*/
	public Contract evaluate(ContractState contractState, ObservationPrimitive observation, Date date) {
		// pre-conditions
		assert
			exists(MapperS.of(equityPayout(contractState, observation, date).get()), false).get()
			: "";
		
		ContractBuilder updatedContractHolder = doEvaluate(contractState, observation, date);

		// assign-output
		Contract assignedUpdatedContract = assignOutput(updatedContractHolder, contractState, observation, date).build();

		objectValidator.validateAndFailOnErorr(Contract.class, assignedUpdatedContract);
		
		return assignedUpdatedContract;
	}
	
	protected abstract ContractBuilder doEvaluate(ContractState contractState, ObservationPrimitive observation, Date date);

	private ContractBuilder assignOutput(ContractBuilder updatedContractHolder, ContractState contractState, ObservationPrimitive observation, Date date) {
		updatedEquityPayout(updatedContractHolder, contractState, observation, date)
				.getOrCreatePriceReturnTerms().getOrCreateValuationPriceFinal().getOrCreateNetPrice()
				.setAmount(doIf(MapperS.of(calculationPeriod.evaluate(MapperS.of(equityPayout(contractState, observation, date).get()).<CalculationPeriodDates>map("getCalculationPeriodDates", EquityPayout::getCalculationPeriodDates).get(), MapperS.of(periodEndDate(contractState, observation, date).get()).get())).<Boolean>map("getIsLastPeriod", CalculationPeriodData::getIsLastPeriod),MapperS.of(price(contractState, observation, date).get())).get());

		updatedEquityPayout(updatedContractHolder, contractState, observation, date)
				.getOrCreatePriceReturnTerms().getOrCreateValuationPriceInterim().getOrCreateNetPrice()
				.setAmount(doIf(areEqual(MapperS.of(calculationPeriod.evaluate(MapperS.of(equityPayout(contractState, observation, date).get()).<CalculationPeriodDates>map("getCalculationPeriodDates", EquityPayout::getCalculationPeriodDates).get(), MapperS.of(periodEndDate(contractState, observation, date).get()).get())).<Boolean>map("getIsLastPeriod", CalculationPeriodData::getIsLastPeriod), MapperS.of(Boolean.valueOf(false))),MapperS.of(price(contractState, observation, date).get())).get());

		updatedContractHolder
				.getOrCreateContractualProduct().getOrCreateEconomicTerms().getOrCreatePayout().getOrCreateEquityPayout(0)
				.setPerformance(MapperS.of(equityPerformance(contractState, observation, date).get()).get());

		updatedContractHolder
				.getOrCreateContractualProduct().getOrCreateEconomicTerms().getOrCreatePayout().getOrCreateEquityPayout(0).getOrCreatePayoutQuantity().getOrCreateQuantityMultiplier()
				.setMultiplierValue(MapperMaths.<BigDecimal, Integer, BigDecimal>add(MapperS.of(Integer.valueOf(1)), MapperMaths.<BigDecimal, BigDecimal, Integer>divide(MapperS.of(equityPerformance(contractState, observation, date).get()), MapperS.of(Integer.valueOf(100)))).get());

		return updatedContractHolder;
	}

	public static final class ResolveEquityContractDefault extends ResolveEquityContract {
		@Override
		protected  ContractBuilder doEvaluate(ContractState contractState, ObservationPrimitive observation, Date date) {
			return Contract.builder();
		}
	}

    // Alias's
    ...

from rune-dsl.

dhuebner avatar dhuebner commented on August 19, 2024

@jim-h-wang
Disabled two tests because in doEvaluate we now have an empty object created by the builder.

from rune-dsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.