/******************************************************************************* | |
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* Gabor Bergmann, Istvan Rath - initial API and implementation | |
*******************************************************************************/ | |
package org.eclipse.viatra2.natives; | |
import java.util.Random; | |
import org.eclipse.viatra2.core.IModelSpace; | |
import org.eclipse.viatra2.errors.VPMRuntimeException; | |
import org.eclipse.viatra2.natives.NativeFunctionParameter.ParameterType; | |
/** | |
* Native function to generate a fresh random number. | |
* | |
* @author Gabor Bergmann | |
* | |
*/ | |
@VIATRANativeFunction( | |
name = "randomInteger", | |
remark = "Returns a fresh random non-negative integer less than the argument, or a random integer without range restriction if no argument is provided.", | |
params = { | |
@NativeFunctionParameter( | |
type = ParameterType.INTEGER, | |
name="range", | |
description="the upper limit of the random integers, omit if full range is required")}, | |
returns = { ParameterType.INTEGER }) | |
public class RandomIntegerFunction implements ASMNativeFunction { | |
Random random = new Random(); | |
public Object evaluate(IModelSpace msp, Object[] params) throws VPMRuntimeException { | |
if (params.length > 0 && params[0] !=null && params[0] instanceof Integer) | |
return random.nextInt((Integer)params[0]); | |
else return random.nextInt(); | |
} | |
public String getDescription() { | |
return "Returns a fresh random non-negative integer less than the range argument, or a random integer without range restriction if no argument is provided."; | |
} | |
public String getID() { | |
return this.getClass().getCanonicalName(); | |
} | |
public String getName() { | |
return "randomInteger"; // ez megy a VTCLbe | |
} | |
} |