Ejemplo n.º 1
0
func (c *clg) calculate(ctx spec.Context, informationSequence string) error {
	// Check the calculated output against the provided expectation, if any. In
	// case there is no expectation provided, we simply go with what we
	// calculated. This then means we are probably not in a training situation.
	expectation, ok := ctx.GetExpectation()
	if !ok {
		err := c.sendTextResponse(ctx, informationSequence)
		if err != nil {
			return maskAny(err)
		}

		return nil
	}

	// There is an expectation provided. Thus we are going to check the calculated
	// output against it. In case the provided expectation does match the
	// calculated result, we simply return it.
	calculatedOutput := expectation.GetOutput()
	if informationSequence == calculatedOutput {
		err := c.sendTextResponse(ctx, informationSequence)
		if err != nil {
			return maskAny(err)
		}
	}

	// The calculated output did not match the given expectation. That means we
	// need to calculate some new output to match the given expectation. To do so
	// we create a new network payload and assign the input CLG of the current CLG
	// tree to it by queueing the new network payload in the underlying storage.
	err := c.forwardNetworkPayload(ctx, informationSequence)
	if err != nil {
		return maskAny(err)
	}

	// The calculated output did not match the given expectation. We return an
	// error to let the neural network know about it.
	return maskAnyf(expectationNotMetError, "'%s' != '%s'", informationSequence, calculatedOutput)
}