Java tutorial
/******************************************************************************* * Copyright 2014 Spyros Papageorgiou * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package org.powertac.producer.hydro; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.junit.Test; import org.junit.runner.RunWith; import org.powertac.common.Competition; import org.powertac.producer.utils.Curve; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.thoughtworks.xstream.XStream; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:test-config.xml" }) @DirtiesContext public class RunOfRiverTest { @Test public void dataGenerateXml() throws IOException { Curve inputFlow = new Curve(); double[] y = { 150.93, 139.35, 178.59, 174.37, 136.75, 133.64, 128.89, 127.25, 128.73, 144.62, 156.51, 181.89, 141.64, 159.33, 149.30, 121.12, 179.54, 232.12, 258.08, 237.18, 220.32, 230.75, 256.72, 257.83, 251.68, 229.52, 252.37, 251.43, 222.80, 187.57, 232.88, 226.58, 207.18, 197.13, 199.27, 163.70, 150.40, 155.70, 211.47, 215.75, 217.23, 219.50, 213.97, 177.71, 203.06, 221.15, 215.45, 220.16, 215.02, 211.03, 200.98, 194.78, 183.42, 214.86, 190.08, 192.33, 183.75, 194.89, 210.47, 222.11, 214.89, 203.90, 217.38, 186.53, 184.18, 213.71, 191.74, 224.60, 201.29, 204.02, 227.96, 185.46, 220.18, 209.19, 182.50, 224.28, 229.55, 219.83, 198.23, 174.27, 198.77, 237.54, 227.49, 225.27, 213.58, 242.83, 208.50, 225.17, 228.20, 244.45, 293.15, 319.49, 354.15, 351.33, 374.75, 348.11, 303.55, 278.62, 314.15, 319.30, 289.06, 299.55, 278.69, 261.02, 275.69, 202.89, 242.91, 267.86, 251.20, 270.84, 270.20, 282.11, 255.64, 243.84, 253.75, 234.95, 261.82, 257.20, 226.31, 219.35, 257.10, 241.93, 246.79, 250.24, 230.08, 254.66, 231.22, 238.59, 267.91, 289.35, 248.45, 270.34, 335.20, 338.03, 366.69, 423.45, 442.26, 444.96, 449.96, 451.19, 461.15, 457.13, 471.72, 477.84, 476.64, 462.09, 486.46, 491.88, 497.34, 494.46, 491.72, 486.49, 480.88, 481.12, 498.41, 493.33, 491.76, 482.40, 492.29, 494.48, 494.78, 491.09, 478.23, 482.07, 489.95, 489.84, 492.75, 488.38, 462.97, 440.38, 426.90, 428.48, 440.70, 432.90, 480.63, 451.82, 423.08, 396.45, 394.52, 381.41, 388.90, 418.33, 428.18, 405.74, 409.38, 359.30, 370.30, 385.97, 376.35, 376.27, 365.44, 343.22, 306.27, 294.97, 320.97, 331.74, 289.69, 269.50, 289.15, 279.23, 283.67, 262.26, 259.69, 255.34, 244.33, 244.51, 236.17, 226.47, 238.11, 209.22, 203.91, 225.66, 221.99, 191.54, 198.33, 205.45, 194.11, 171.04, 181.43, 197.49, 185.96, 194.35, 196.43, 200.32, 160.97, 149.27, 188.71, 189.23, 194.86, 189.84, 195.41, 175.54, 157.37, 161.75, 173.32, 186.87, 195.82, 167.11, 169.41, 148.44, 146.42, 140.38, 165.04, 143.84, 123.20, 101.61, 97.750, 116.29, 117.95, 124.26, 123.54, 139.54, 120.89, 110.25, 128.37, 108.38, 104.58, 109.73, 116.92, 109.42, 104.07, 102.33, 122.03, 123.26, 104.29, 103.62, 91.750, 96.140, 116.95, 109.68, 110.71, 101.40, 105.67, 78.380, 80.710, 90.290, 100.03, 108.30, 117.16, 96.900, 93.890, 94.100, 129.53, 114.42, 100.36, 113.98, 133.06, 136.70, 127.35, 122.11, 119.30, 103.62, 114.25, 118.22, 115.95, 88.670, 107.94, 118.67, 104.45, 131.19, 126.84, 121.48, 94.980, 96.540, 131.90, 131.34, 124.03, 116.47, 117.32, 118.13, 117.99, 132.40, 116.32, 115.21, 126.75, 109.51, 112, 129.35, 116.82, 143.01, 132.10, 127.81, 121.95, 135.30, 131.32, 127.96, 116.67, 114.47, 113.17, 117.26, 109.72, 123.01, 121.85, 127.22, 125.33, 122.18, 122.19, 125.14, 136.47, 134.70, 139.05, 131.83, 136.58, 138.34, 122.26, 140.76, 130.10, 141.18, 132.31, 117.35, 133.08, 112.71, 120.88, 134.15, 121.01, 132.05, 124.80, 125.95, 118.71, 131.57, 136.90, 127.24, 112.67, 126.07, 123.33 }; for (int i = 0; i < 365; i++) { inputFlow.add(i, y[i]); } inputFlow.value(300); double[] x = { 0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.30, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.40, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.50, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.60, 0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.70, 0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.80, 0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.90, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1 }; double[] y2 = { 0.54, 0.567510059257987, 0.593427468223978, 0.617806965819306, 0.640703290965304, 0.662171182583305, 0.682265379594641, 0.701040620920646, 0.718551645482652, 0.734853192201992, 0.75, 0.764046807798008, 0.777048354517348, 0.789059379079354, 0.800134620405359, 0.810328817416695, 0.819696709034696, 0.828293034180694, 0.836172531776022, 0.843389940742013, 0.85, 0.856052709549983, 0.861579113706630, 0.866605517863277, 0.871158227413260, 0.875263547749914, 0.878947784266575, 0.882237242356579, 0.885158227413260, 0.887737044829955, 0.89, 0.891972354002061, 0.893675190656132, 0.895128549467537, 0.896352469941601, 0.897366991583648, 0.898192153899004, 0.898847996392992, 0.899354558570938, 0.899731879938166, 0.90, 0.900177874441773, 0.900280123668842, 0.900320284266575, 0.900311892820337, 0.900268485915493, 0.900203600137410, 0.900130772071453, 0.900063538302989, 0.900015435417382, 0.90, 0.900026148230849, 0.900084314668499, 0.900160313466163, 0.900239958777053, 0.900309064754380, 0.900353445551357, 0.900358915321196, 0.900311288217108, 0.900196378392305, 0.90, 0.899712532634833, 0.899342617657162, 0.898903461868774, 0.898408272071453, 0.897870255066987, 0.897302617657163, 0.896718566643765, 0.896131308828581, 0.895554051013397, 0.895000000000000, 0.894478721229818, 0.893985214702851, 0.893510839058743, 0.893046952937135, 0.892584914977671, 0.892116083819993, 0.891631818103744, 0.891123476468568, 0.890582417554105, 0.89, 0.889367582445895, 0.888676523531433, 0.887918181896256, 0.887083916180007, 0.886165085022329, 0.885153047062865, 0.884039160941257, 0.882814785297149, 0.881471278770182, 0.88 }; Curve efficiency = new Curve(); for (int i = 0; i < x.length; i++) { efficiency.add(x[i], y2[i]); } efficiency.value(0.5); Competition.newInstance("run of river"); RunOfRiver river = new RunOfRiver(inputFlow, 20, 500, efficiency, 1000000, 80, 0.95, -300000); new File("data/").mkdir(); FileWriter fw = new FileWriter("data/runoftheriver.xml"); XStream xstream = new XStream(); xstream.autodetectAnnotations(true); xstream.toXML(river, fw); fw.close(); } }