/* * Chrysalix * See the COPYRIGHT.txt file distributed with this work for information * regarding copyright ownership. Some portions may be licensed * to Red Hat, Inc. under one or more contributor license agreements. * See the AUTHORS.txt file in the distribution for a full listing of * individual contributors. * * Chrysalix is free software. Unless otherwise indicated, all code in Chrysalix * is licensed to you under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Chrysalix is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.chrysalix.operation; import org.chrysalix.ChrysalixException; import org.chrysalix.ChrysalixI18n; import org.chrysalix.transformation.Operation; import org.chrysalix.transformation.OperationDescriptor; import org.chrysalix.transformation.Transformation; import org.chrysalix.transformation.TransformationFactory; import org.chrysalix.transformation.ValidationProblem; import org.chrysalix.transformation.ValidationProblems; import org.chrysalix.transformation.ValueDescriptor; import org.modelspace.ModelObject; import org.modelspace.ModelspaceException; /** * Provides a random number with a positive sign, greater than or equal to {@code 0.0} and less than {@code 1.0}. There is an * optional term for a seed value. */ public final class Random extends AbstractOperation< Double > { static final String DESCRIPTION = "Provides a random number with a positive sign, greater than or equal to 0.0 and less than 1.0."; private static final String INVALID_NUMBER_OF_TERMS = "Random operation in transformation '%s' cannot have any terms"; static final String NAME = "Random"; /** * The output descriptor. */ public static final OperationDescriptor< Double > DESCRIPTOR = new AbstractOperationDescriptor< Double >( TransformationFactory.createId( Random.class ), ChrysalixI18n.localize( DESCRIPTION ), ChrysalixI18n.localize( NAME ), Double.class, ValueDescriptor.NO_DESCRIPTORS ) { /** * {@inheritDoc} * * @see org.chrysalix.transformation.OperationDescriptor#newInstance(org.modelspace.ModelObject, * org.chrysalix.transformation.Transformation) */ @Override public Operation< Double > newInstance( final ModelObject operation, final Transformation transformation ) throws ModelspaceException, ChrysalixException { return new Random( operation, transformation ); } }; /** * @param operation * the operation model object (cannot be <code>null</code>) * @param transformation * the transformation containing this operation (cannot be <code>null</code>) * @throws ModelspaceException * if an error with the model object occurs * @throws ChrysalixException * if a non-model object error occurs * @throws IllegalArgumentException * if the input is <code>null</code> */ Random( final ModelObject operation, final Transformation transformation ) throws ModelspaceException, ChrysalixException { super( operation, transformation ); } /** * {@inheritDoc} * * @see org.chrysalix.operation.AbstractOperation#calculate() */ @Override protected Double calculate() throws ChrysalixException { assert !problems().isError(); return Math.random(); } /** * {@inheritDoc} * * @see org.chrysalix.operation.AbstractOperation#problems() */ @Override public ValidationProblems problems() throws ChrysalixException { this.problems.clear(); // make sure there is at most one term if ( inputs().length != 0 ) { final ValidationProblem problem = TransformationFactory.createError( transformationId(), ChrysalixI18n.localize( INVALID_NUMBER_OF_TERMS, transformationId() ) ); problems().add( problem ); } return super.problems(); } }