| 1 | package PIANOS.datastructures; |
| 2 | |
| 3 | import PIANOS.exceptions.*; |
| 4 | |
| 5 | import java.util.*; |
| 6 | |
| 7 | public class UserDefinedDistribution extends Distribution{ |
| 8 | |
| 9 | private DistributionSkeleton skeleton; |
| 10 | |
| 11 | public UserDefinedDistribution (DistributionSkeleton skeleton){ |
| 12 | //Call the constructor of Distribution class |
| 13 | super (skeleton.getNumberOfParameters()); |
| 14 | this.skeleton = skeleton; |
| 15 | |
| 16 | boolean[] paramType = skeleton.getTypeOfParameters(); |
| 17 | |
| 18 | //Process the parameter types |
| 19 | for (int i = 0; i < numberOfParameters; i++){ |
| 20 | if (paramType[i] == true) |
| 21 | parameterType[i] = 0; |
| 22 | else |
| 23 | parameterType[i] = 1; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public ArrayList<String> getIntroduction() throws IllegalParametersException{ |
| 28 | |
| 29 | return new ArrayList<String>(); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | public ArrayList<String> getGenCode(String[] parameters) throws IllegalParametersException, MissingFunctionException{ |
| 34 | |
| 35 | //Check that generating function exists |
| 36 | if (!skeleton.hasGenFunction()) |
| 37 | throw new MissingFunctionException ("User defined distribution " + skeleton.getName() + " does not have generator function."); |
| 38 | |
| 39 | //Check the number of parameters |
| 40 | if (parameters.length != numberOfParameters + 1) |
| 41 | throw new IllegalParametersException ("User defined distribution " + skeleton.getName() + " has wrong number of parameters in the proposal distribution file." ); |
| 42 | |
| 43 | //process the parameters |
| 44 | String temp = parameters[0]; |
| 45 | for (int i = 1; i < numberOfParameters + 1; i++) |
| 46 | temp += ", " + parameters[i]; |
| 47 | |
| 48 | |
| 49 | //Create a result String |
| 50 | ArrayList<String> result = new ArrayList<String>(); |
| 51 | result.add("CALL " + skeleton.getName() + "_gen(" + temp + ")"); |
| 52 | return result; |
| 53 | |
| 54 | /* |
| 55 | String[] tester = new String[3]; |
| 56 | tester[0] = "This is a test String."; |
| 57 | tester[1] = "User defined distribution: " + skeleton.getName(); |
| 58 | tester[2] = "Generator function, number of parameters: " + numberOfParameters; |
| 59 | return tester; |
| 60 | */ |
| 61 | } |
| 62 | |
| 63 | public ArrayList<String> getFreqCode(String[] parameters) throws IllegalParametersException, MissingFunctionException{ |
| 64 | |
| 65 | //Check that frequency function exists |
| 66 | if (!skeleton.hasFreqFunction()) |
| 67 | throw new MissingFunctionException ("User defined distribution " + skeleton.getName() + " does not have frequency function."); |
| 68 | |
| 69 | //Check the number of parameters |
| 70 | if (parameters.length != numberOfParameters + 2) |
| 71 | throw new IllegalParametersException ("User defined distribution " + skeleton.getName() + " has wrong number of parameters in the model file"); |
| 72 | |
| 73 | //process the parameters |
| 74 | String temp = parameters[0]; |
| 75 | for (int i = 1; i < numberOfParameters + 2; i++) |
| 76 | temp += ", " + parameters[i]; |
| 77 | |
| 78 | //Create a result String |
| 79 | ArrayList<String> result = new ArrayList<String>(); |
| 80 | result.add("CALL " + skeleton.getName() + "_freq(" + temp + ")"); |
| 81 | |
| 82 | return result; |
| 83 | |
| 84 | /* |
| 85 | String[] tester = new String[3]; |
| 86 | tester[0] = "This is a test String."; |
| 87 | tester[1] = "User defined distribution: " + skeleton.getName(); |
| 88 | tester[2] = "Frequency function, number of parameters: " + numberOfParameters; |
| 89 | return tester; |
| 90 | */ |
| 91 | } |
| 92 | |
| 93 | public String toString(){ |
| 94 | String toReturn = skeleton.getName() +"("; |
| 95 | for (int i = 0; i < numberOfParameters; ++i){ |
| 96 | toReturn += parameterString[i]; |
| 97 | if (i + 1 != numberOfParameters) |
| 98 | toReturn += ", "; |
| 99 | } |
| 100 | toReturn += ")"; |
| 101 | |
| 102 | return toReturn; |
| 103 | } |
| 104 | |
| 105 | } |