All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.siddhi.sample.benchmark.SimpleStockQuoteEventGenerator Maven / Gradle / Ivy

The newest version!
/**
 * 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.siddhi.sample.benchmark;

import org.siddhi.core.event.Event;
import org.siddhi.core.event.generator.EventGenerator;

import java.util.Random;

public class SimpleStockQuoteEventGenerator {

   private Random randomGenerator; // To generate random numbers
   private String[] quoteNames;
   private final EventGenerator stockQuoteEventGeneratorType;
   private String streamName;

    public SimpleStockQuoteEventGenerator(String streamName) {
        randomGenerator = new Random();
        this.streamName = streamName;
        stockQuoteEventGeneratorType =
                EventGenerator.DefaultFactory.create(streamName,
                        new String[]{"symbol", "price", "volume"},
                        new Class[]{String.class, Double.class, Integer.class}
                );
    }

    private static String getQuoteSybmol(int position) {
        return Symbols.SYMBOLS[position];
    }

    private int getQuoteVolume(int max) {
       return randomGenerator.nextInt(max - 1) + 1;
    }

    private double getQuotePrice(double initVal) { // returns the calculated price value to generate events
       int percentVar = randomGenerator.nextInt(9) + 1;
        int trend = randomGenerator.nextInt(3);
        double result = initVal;
        switch (trend) {
            case 0:
                result *= 1.0D - (double) percentVar * 0.01D;
                break;
            case 2:
                result *= 1.0D + (double) percentVar * 0.01D;
                break;
        }
        return result;
    }

    public Event generateEvent(int symbolPosition,double priceInitVal,int volRandMax) {
        return stockQuoteEventGeneratorType.
               createEvent(getQuoteSybmol(symbolPosition),getQuotePrice(priceInitVal),getQuoteVolume(volRandMax));
    }

    public Event generateDefinedEvent(String symbol,double price,int volume) {
        return stockQuoteEventGeneratorType.createEvent(symbol, price, volume);
    }

    public EventGenerator getEventStream() {
        return stockQuoteEventGeneratorType;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy