def zStreamPatternSearchTest(createTestFile=False): pattern = Pattern( SeqOperator([ QItem("MSFT", "a"), QItem("DRIV", "b"), QItem("ORLY", "c"), QItem("CBRL", "d") ]), AndFormula( AndFormula( SmallerThanFormula( IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), SmallerThanFormula( IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"]))), SmallerThanFormula(IdentifierTerm("c", lambda x: x["Peak Price"]), IdentifierTerm("d", lambda x: x["Peak Price"]))), timedelta(minutes=3)) selectivityMatrix = [[1.0, 0.9457796098355941, 1.0, 1.0], [0.9457796098355941, 1.0, 0.15989723367389616, 1.0], [1.0, 0.15989723367389616, 1.0, 0.9992557393942864], [1.0, 1.0, 0.9992557393942864, 1.0]] arrivalRates = [ 0.016597077244258872, 0.01454418928322895, 0.013917884481558803, 0.012421711899791231 ] pattern.setAdditionalStatistics( StatisticsTypes.SELECTIVITY_MATRIX_AND_ARRIVAL_RATES, (selectivityMatrix, arrivalRates)) runTest('zstream1', [pattern], createTestFile, ZStreamAlgorithm(), nasdaqEventStream)
def multiplePatternSearchTest(createTestFile=False): amazonInstablePattern = Pattern( SeqOperator( [QItem("AMZN", "x1"), QItem("AMZN", "x2"), QItem("AMZN", "x3")]), AndFormula( SmallerThanEqFormula( IdentifierTerm("x1", lambda x: x["Lowest Price"]), AtomicTerm(75)), AndFormula( GreaterThanEqFormula( IdentifierTerm("x2", lambda x: x["Peak Price"]), AtomicTerm(78)), SmallerThanEqFormula( IdentifierTerm("x3", lambda x: x["Lowest Price"]), IdentifierTerm("x1", lambda x: x["Lowest Price"])))), timedelta(days=1)) googleAscendPattern = Pattern( SeqOperator( [QItem("GOOG", "a"), QItem("GOOG", "b"), QItem("GOOG", "c")]), AndFormula( SmallerThanFormula(IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), SmallerThanFormula(IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) runTest('multiplePatterns', [amazonInstablePattern, googleAscendPattern], createTestFile)
def msftDrivRacePatternSearchTest(createTestFile=False): """ This pattern is looking for a race between driv and microsoft in ten minutes PATTERN SEQ(MicrosoftStockPriceUpdate a, DrivStockPriceUpdate b, MicrosoftStockPriceUpdate c, DrivStockPriceUpdate d, MicrosoftStockPriceUpdate e) WHERE a.PeakPrice < b.PeakPrice AND b.PeakPrice < c.PeakPrice AND c.PeakPrice < d.PeakPrice AND d.PeakPrice < e.PeakPrice WITHIN 10 minutes """ msftDrivRacePattern = Pattern( SeqOperator([ QItem("MSFT", "a"), QItem("DRIV", "b"), QItem("MSFT", "c"), QItem("DRIV", "d"), QItem("MSFT", "e") ]), AndFormula( AndFormula( SmallerThanFormula( IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), SmallerThanFormula( IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"]))), AndFormula( SmallerThanFormula( IdentifierTerm("c", lambda x: x["Peak Price"]), IdentifierTerm("d", lambda x: x["Peak Price"])), SmallerThanFormula( IdentifierTerm("d", lambda x: x["Peak Price"]), IdentifierTerm("e", lambda x: x["Peak Price"])))), timedelta(minutes=10)) runTest('msftDrivRace', [msftDrivRacePattern], createTestFile)
def nonFrequencyPatternSearch2Test(createTestFile=False): pattern = Pattern( SeqOperator( [QItem("LOCM", "a"), QItem("AMZN", "b"), QItem("AAPL", "c")]), AndFormula( SmallerThanFormula( IdentifierTerm("a", lambda x: x["Opening Price"]), IdentifierTerm("b", lambda x: x["Opening Price"])), SmallerThanFormula( IdentifierTerm("b", lambda x: x["Opening Price"]), IdentifierTerm("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runTest("nonFrequency2", [pattern], createTestFile)
def hierarchyPatternSearchTest(createTestFile=False): """ The following pattern is looking for Amazon < Apple < Google cases in one minute windows. PATTERN AND(AmazonStockPriceUpdate a, AppleStockPriceUpdate b, GoogleStockPriceUpdate c) WHERE a.PeakPrice < b.PeakPrice AND b.PeakPrice < c.PeakPrice WITHIN 1 minute """ hierarchyPattern = Pattern( AndOperator( [QItem("AMZN", "a"), QItem("AAPL", "b"), QItem("GOOG", "c")]), AndFormula( SmallerThanFormula(IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), SmallerThanFormula(IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"]))), timedelta(minutes=1)) runTest('hierarchy', [hierarchyPattern], createTestFile)
def googleAscendPatternSearchTest(createTestFile=False): """ This pattern is looking for a short ascend in the Google peak prices. PATTERN SEQ(GoogleStockPriceUpdate a, GoogleStockPriceUpdate b, GoogleStockPriceUpdate c) WHERE a.PeakPrice < b.PeakPrice AND b.PeakPrice < c.PeakPrice WITHIN 3 minutes """ googleAscendPattern = Pattern( SeqOperator( [QItem("GOOG", "a"), QItem("GOOG", "b"), QItem("GOOG", "c")]), AndFormula( SmallerThanFormula(IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), SmallerThanFormula(IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) runTest('googleAscend', [googleAscendPattern], createTestFile)
def frequencyPatternSearch2Test(createTestFile=False): pattern = Pattern( SeqOperator( [QItem("LOCM", "a"), QItem("AMZN", "b"), QItem("AAPL", "c")]), AndFormula( SmallerThanFormula( IdentifierTerm("a", lambda x: x["Opening Price"]), IdentifierTerm("b", lambda x: x["Opening Price"])), SmallerThanFormula( IdentifierTerm("b", lambda x: x["Opening Price"]), IdentifierTerm("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) pattern.setAdditionalStatistics(StatisticsTypes.FREQUENCY_DICT, { "AAPL": 2, "AMZN": 3, "LOCM": 1 }) runTest("frequency2", [pattern], createTestFile, AscendingFrequencyAlgorithm())
def nonsensePatternSearchTest(createTestFile=False): """ This pattern is looking for something that does not make sense. PATTERN AND(AmazonStockPriceUpdate a, AvidStockPriceUpdate b, AppleStockPriceUpdate c) WHERE a.PeakPrice < b.PeakPrice AND b.PeakPrice < c.PeakPrice AND c.PeakPrice < a.PeakPrice """ nonsensePattern = Pattern( AndOperator( [QItem("AMZN", "a"), QItem("AVID", "b"), QItem("AAPL", "c")]), AndFormula( SmallerThanFormula(IdentifierTerm("a", lambda x: x["Peak Price"]), IdentifierTerm("b", lambda x: x["Peak Price"])), AndFormula( SmallerThanFormula( IdentifierTerm("b", lambda x: x["Peak Price"]), IdentifierTerm("c", lambda x: x["Peak Price"])), SmallerThanFormula( IdentifierTerm("c", lambda x: x["Peak Price"]), IdentifierTerm("a", lambda x: x["Peak Price"])))), timedelta(minutes=1)) runTest('nonsense', [nonsensePattern], createTestFile)