/// <summary> /// Filters the parameters, matching only those that are applicable for the target and measure. /// <para> /// The resulting parameters are filtered to the target and measure. /// The implementation of each parameter may be changed by this process. /// If two parameters are filtered to the same <seealso cref="CalculationParameter#queryType() query type"/> /// then an exception will be thrown /// /// </para> /// </summary> /// <param name="target"> the calculation target, such as a trade </param> /// <param name="measure"> the measure to be calculated </param> /// <returns> the filtered calculation parameters </returns> /// <exception cref="IllegalArgumentException"> if two parameters are filtered to the same query type </exception> public CalculationParameters filter(CalculationTarget target, Measure measure) { //JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter: ImmutableList <CalculationParameter> filtered = parameters.values().Select(cp => cp.filter(target, measure)).Where(opt => opt.Present).Select(opt => opt.get()).collect(toImmutableList()); return(of(filtered)); }
private CalculationResults(CalculationTarget target, IList <CalculationResult> cells) { JodaBeanUtils.notNull(target, "target"); JodaBeanUtils.notNull(cells, "cells"); this.target = target; this.cells = ImmutableList.copyOf(cells); }
// creates the tasks for a single target private static IList <CalculationTask> createTargetTasks <T1>(CalculationTarget resolvedTarget, int rowIndex, CalculationFunction <T1> function, IList <Column> columns) { // create the cells and group them ListMultimap <CalculationParameters, CalculationTaskCell> grouped = ArrayListMultimap.create(); for (int colIndex = 0; colIndex < columns.Count; colIndex++) { Column column = columns[colIndex]; Measure measure = column.Measure; ReportingCurrency reportingCurrency = column.ReportingCurrency.orElse(ReportingCurrency.NATURAL); CalculationTaskCell cell = CalculationTaskCell.of(rowIndex, colIndex, measure, reportingCurrency); // group to find cells that can be shared, with same mappings and params (minus reporting currency) CalculationParameters @params = column.Parameters.filter(resolvedTarget, measure); grouped.put(@params, cell); } // build tasks ImmutableList.Builder <CalculationTask> taskBuilder = ImmutableList.builder(); foreach (CalculationParameters @params in grouped.Keys) { taskBuilder.add(CalculationTask.of(resolvedTarget, function, @params, grouped.get(@params))); } return(taskBuilder.build()); }
/// <summary> /// Obtains an instance from a set of targets, columns and rules, resolving the targets. /// <para> /// The targets will typically be trades and positions. /// The columns represent the measures to calculate. /// </para> /// <para> /// The targets will be resolved if they implement <seealso cref="ResolvableCalculationTarget"/>. /// /// </para> /// </summary> /// <param name="rules"> the rules defining how the calculation is performed </param> /// <param name="targets"> the targets for which values of the measures will be calculated </param> /// <param name="columns"> the columns that will be calculated </param> /// <param name="refData"> the reference data to use to resolve the targets </param> /// <returns> the calculation tasks </returns> public static CalculationTasks of <T1>(CalculationRules rules, IList <T1> targets, IList <Column> columns, ReferenceData refData) where T1 : com.opengamma.strata.basics.CalculationTarget { // create columns that are a combination of the column overrides and the defaults // this is done once as it is the same for all targets //JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter: IList <Column> effectiveColumns = columns.Select(column => column.combineWithDefaults(rules.ReportingCurrency, rules.Parameters)).collect(toImmutableList()); // loop around the targets, then the columns, to build the tasks ImmutableList.Builder <CalculationTask> taskBuilder = ImmutableList.builder(); for (int rowIndex = 0; rowIndex < targets.Count; rowIndex++) { CalculationTarget target = resolveTarget(targets[rowIndex], refData); // find the applicable function, resolving the target if necessary //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: CalculationFunction<?> fn = target instanceof UnresolvableTarget ? UnresolvableTargetCalculationFunction.INSTANCE : rules.getFunctions().getFunction(target); CalculationFunction <object> fn = target is UnresolvableTarget ? UnresolvableTargetCalculationFunction.INSTANCE : rules.Functions.getFunction(target); // create the tasks IList <CalculationTask> targetTasks = createTargetTasks(target, rowIndex, fn, effectiveColumns); taskBuilder.addAll(targetTasks); } // calculation tasks holds the original user-specified columns, not the derived ones return(new CalculationTasks(taskBuilder.build(), columns)); }
// determine the available measures internal static IList <string> measureNames(CalculationTarget target, CalculationFunctions calculationFunctions) { ISet <Measure> validMeasures = calculationFunctions.findFunction(target).map(fn => fn.supportedMeasures()).orElse(ImmutableSet.of()); //JAVA TO C# CONVERTER TODO TASK: Method reference arbitrary object instance method syntax is not converted by Java to C# Converter: //JAVA TO C# CONVERTER TODO TASK: Most Java stream collectors are not converted by Java to C# Converter: return(validMeasures.Select(Measure::getName).OrderBy(c => c).collect(toImmutableList())); }
/// <summary> /// Obtains an instance that will calculate the specified cells. /// <para> /// The cells must all be for the same row index and none of the column indices must overlap. /// /// </para> /// </summary> /// <param name="target"> the target for which the value will be calculated </param> /// <param name="function"> the function that performs the calculation </param> /// <param name="parameters"> the additional parameters </param> /// <param name="cells"> the cells to be calculated by this task </param> /// <returns> the task </returns> public static CalculationTask of <T1>(CalculationTarget target, CalculationFunction <T1> function, CalculationParameters parameters, IList <CalculationTaskCell> cells) where T1 : com.opengamma.strata.basics.CalculationTarget { //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @SuppressWarnings("unchecked") CalculationFunction<com.opengamma.strata.basics.CalculationTarget> functionCast = (CalculationFunction<com.opengamma.strata.basics.CalculationTarget>) function; CalculationFunction <CalculationTarget> functionCast = (CalculationFunction <CalculationTarget>)function; return(new CalculationTask(target, functionCast, parameters, cells)); }
private CalculationTask(CalculationTarget target, CalculationFunction <CalculationTarget> function, CalculationParameters parameters, IList <CalculationTaskCell> cells) { JodaBeanUtils.notNull(target, "target"); JodaBeanUtils.notNull(function, "function"); JodaBeanUtils.notNull(parameters, "parameters"); JodaBeanUtils.notEmpty(cells, "cells"); this.target = target; this.function = function; this.parameters = parameters; this.cells = ImmutableList.copyOf(cells); }
public void resultReceived(CalculationTarget target, CalculationResult calculationResult) { //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result = calculationResult.getResult(); Result <object> result = calculationResult.Result; //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> unwrappedResult = unwrapScenarioResult(result); Result <object> unwrappedResult = unwrapScenarioResult(result); CalculationResult unwrappedCalculationResult = calculationResult.withResult(unwrappedResult); @delegate.resultReceived(target, unwrappedCalculationResult); }
public override Optional <CalculationParameter> filter(CalculationTarget target, Measure measure) { if (target is Trade) { Trade trade = (Trade)target; Optional <StandardId> idOpt = trade.Info.Counterparty; if (idOpt.Present) { StandardId id = idOpt.get(); CalculationParameter value = parameters.getOrDefault(id, defaultParameter); return(value.filter(target, measure)); } } return(defaultParameter.filter(target, measure)); }
// resolves the target private static CalculationTarget resolveTarget(CalculationTarget target, ReferenceData refData) { if (target is ResolvableCalculationTarget) { ResolvableCalculationTarget resolvable = (ResolvableCalculationTarget)target; try { return(resolvable.resolveTarget(refData)); } catch (Exception ex) { return(new UnresolvableTarget(resolvable, ex.Message)); } } return(target); }
/// <summary> /// Creates the result from the map of calculated measures. /// <para> /// This extracts the calculated measure and performs currency conversion if necessary. /// /// </para> /// </summary> /// <param name="task"> the calculation task </param> /// <param name="target"> the target of the calculation </param> /// <param name="results"> the map of result by measure </param> /// <param name="fxProvider"> the market data </param> /// <param name="refData"> the reference data </param> /// <returns> the calculation result </returns> internal CalculationResult createResult <T1>(CalculationTask task, CalculationTarget target, IDictionary <T1> results, ScenarioFxRateProvider fxProvider, ReferenceData refData) { // caller expects that this method does not throw an exception //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> calculated = results.get(measure); Result <object> calculated = results[measure]; if (calculated == null) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: calculated = Result.failure(FailureReason.CALCULATION_FAILED, "Measure '{}' was not calculated by the function for target type '{}'", measure, target.GetType().FullName); } //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result = convertCurrencyIfNecessary(task, calculated, fxProvider, refData); Result <object> result = convertCurrencyIfNecessary(task, calculated, fxProvider, refData); return(CalculationResult.of(rowIndex, columnIndex, result)); }
public void resultReceived(CalculationTarget target, CalculationResult result) { if (!string.ReferenceEquals(threadName, null)) { errors.AddLast("Expected threadName to be null but it was " + threadName); } threadName = Thread.CurrentThread.Name; try { // Give other threads a chance to get into this method Thread.Sleep(5); } catch (InterruptedException) { // Won't ever happen } threadName = null; }
//------------------------------------------------------------------------- /// <summary> /// Creates an instance. /// </summary> /// <param name="tasks"> the tasks that perform the calculations </param> /// <param name="columns"> the columns that define the calculations </param> private CalculationTasks(IList <CalculationTask> tasks, IList <Column> columns) { this.columns = ImmutableList.copyOf(columns); this.tasks = ImmutableList.copyOf(tasks); // validate the number of tasks and number of columns tally long cellCount = tasks.stream().flatMap(task => task.Cells.stream()).count(); int columnCount = columns.Count; if (cellCount != 0) { if (columnCount == 0) { throw new System.ArgumentException("There must be at least one column"); } if (cellCount % columnCount != 0) { throw new System.ArgumentException(Messages.format("Number of cells ({}) must be exactly divisible by the number of columns ({})", cellCount, columnCount)); } } // pull out the targets from the tasks int targetCount = (int)cellCount / columnCount; CalculationTarget[] targets = new CalculationTarget[targetCount]; foreach (CalculationTask task in tasks) { int rowIdx = task.RowIndex; if (targets[rowIdx] == null) { targets[rowIdx] = task.Target; } else if (targets[rowIdx] != task.Target) { throw new System.ArgumentException(Messages.format("Tasks define two different targets for row {}: {} and {}", rowIdx, targets[rowIdx], task.Target)); } } this.targets = ImmutableList.copyOf(targets); // missing targets will be caught here by null check }
//------------------------------------------------------------------------- /// <summary> /// Obtains a calculation result from individual calculations. /// </summary> /// <param name="target"> the calculation target, such as a trade </param> /// <param name="results"> the results of the calculation </param> /// <returns> the calculation result </returns> public static CalculationResults of(CalculationTarget target, IList <CalculationResult> results) { return(new CalculationResults(target, results)); }
public void resultReceived(CalculationTarget target, CalculationResult result) { this.result = result; }
public override abstract void resultReceived(CalculationTarget target, CalculationResult result);
//------------------------------------------------------------------------- public override Optional <CalculationParameter> filter(CalculationTarget target, Measure measure) { return(target is CmsTrade ? this : null); }
//------------------------------------------------------------------------- /// <summary> /// Obtains an instance that will calculate the specified cells. /// <para> /// The cells must all be for the same row index and none of the column indices must overlap. /// The result will contain no calculation parameters. /// /// </para> /// </summary> /// <param name="target"> the target for which the value will be calculated </param> /// <param name="function"> the function that performs the calculation </param> /// <param name="cells"> the cells to be calculated by this task </param> /// <returns> the task </returns> public static CalculationTask of <T1>(CalculationTarget target, CalculationFunction <T1> function, params CalculationTaskCell[] cells) where T1 : com.opengamma.strata.basics.CalculationTarget { return(of(target, function, CalculationParameters.empty(), ImmutableList.copyOf(cells))); }
public override Optional <CalculationParameter> filter(CalculationTarget target, Measure measure) { CalculationParameter value = parameters.getOrDefault(target.GetType(), defaultParameter); return(value.filter(target, measure)); }
public override void resultReceived(CalculationTarget target, CalculationResult result) { results.Add(result); }