def onePatternIncludesOther(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("AAPL", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), GreaterThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b")), SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), timedelta(minutes=3)) eval_mechanism_params = TreeBasedEvaluationMechanismParameters( TreePlanBuilderParameters( TreePlanBuilderTypes.TRIVIAL_LEFT_DEEP_TREE, TreeCostModels.INTERMEDIATE_RESULTS_TREE_COST_MODEL), TreeStorageParameters(sort_storage=False, clean_up_interval=10, prioritize_sorting_by_timestamp=True), MultiPatternEvaluationParameters( MultiPatternEvaluationApproaches.SUBTREES_UNION)) runMultiTest("onePatternIncludesOther", [pattern1, pattern2], createTestFile, eval_mechanism_params)
def KC_Condition_Failure_03(createTestFile=False): """ KC(And([a, b, c])) """ try: pattern = Pattern( KleeneClosureOperator( AndOperator( PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("GOOG", "c") ), min_size=1, max_size=3 ), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])), KCIndexCondition(names={'a', 'b', 'c'}, getattr_func=lambda x: x["Peak Price"], relation_op=lambda x, y: x < 1 + y, offset=-1, first_index=2) ), timedelta(minutes=3) ) except Exception as e: print("Test KC_Condition_Failure_03 Succeeded") return print("Test KC_Condition_Failure_03 Failed")
def samePatternDifferentTimeStampsFullSharing(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=2)) runMultiTest("DifferentTimeStampFullSharing", [pattern1, pattern2], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name="DifferentTimeStamp")
def zStreamPatternSearchTest(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("MSFT", "a"), PrimitiveEventStructure("DRIV", "b"), PrimitiveEventStructure("ORLY", "c"), PrimitiveEventStructure("CBRL", "d")), AndCondition( AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), AndCondition( SmallerThanCondition( Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])))), SmallerThanCondition(Variable("c", lambda x: x["Peak Price"]), Variable("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.set_statistics( StatisticsTypes.SELECTIVITY_MATRIX_AND_ARRIVAL_RATES, (selectivityMatrix, arrivalRates)) eval_params = TreeBasedEvaluationMechanismParameters( TreePlanBuilderParameters(TreePlanBuilderTypes.ZSTREAM_BUSHY_TREE), DEFAULT_TESTING_EVALUATION_MECHANISM_SETTINGS.storage_params) runTest('zstream1', [pattern], createTestFile, eval_mechanism_params=eval_params, events=nasdaqEventStream)
def frequencyPatternSearch2Test(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("LOCM", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("AAPL", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) pattern.set_statistics( {StatisticsTypes.ARRIVAL_RATES: [0.0076, 0.0153, 0.0159]}) eval_params = TreeBasedEvaluationMechanismParameters( optimizer_params=StatisticsDeviationAwareOptimizerParameters( tree_plan_params=TreePlanBuilderParameters( TreePlanBuilderTypes.SORT_BY_FREQUENCY_LEFT_DEEP_TREE), statistics_collector_params=StatisticsCollectorParameters( statistics_types=[StatisticsTypes.ARRIVAL_RATES])), storage_params=DEFAULT_TESTING_EVALUATION_MECHANISM_SETTINGS. storage_params) runTest("frequency2", [pattern], createTestFile, eval_mechanism_params=eval_params)
def distinctPatterns(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AMZN", "x1"), PrimitiveEventStructure("AMZN", "x2"), PrimitiveEventStructure("AMZN", "x3")), AndCondition( SmallerThanEqCondition(Variable("x1", lambda x: x["Lowest Price"]), 75), GreaterThanEqCondition(Variable("x2", lambda x: x["Peak Price"]), 78), SmallerThanEqCondition(Variable("x3", lambda x: x["Lowest Price"]), Variable("x1", lambda x: x["Lowest Price"]))), timedelta(days=1)) runMultiTest("BigMultiPattern", [pattern1, pattern2], createTestFile, leaf_sharing_eval_mechanism_params)
def nonFrequencyPatternSearch2Test(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("LOCM", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("AAPL", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runTest("nonFrequency2", [pattern], createTestFile)
def severalPatternShareSubtreeFullSharing(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c"), NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP2", "y")), NegationOperator(PrimitiveEventStructure("TYP3", "z"))), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("TYP1", "x")), GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), timedelta(minutes=5)) pattern3 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b")), GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), timedelta(minutes=5)) runMultiTest("threeSharingSubtreesFullSharing", [pattern, pattern2, pattern3], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name="threeSharingSubtrees")
def multipleNotInTheMiddleTest(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), NegationOperator(PrimitiveEventStructure("LI", "d")), PrimitiveEventStructure("AMZN", "b"), NegationOperator(PrimitiveEventStructure("FB", "e")), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"])), GreaterThanCondition(Variable("e", lambda x: x["Opening Price"]), Variable("a", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("d", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=4)) runTest("MultipleNotMiddle", [pattern], createTestFile)
def iiGreedy2PatternSearchTest(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("MSFT", "a"), PrimitiveEventStructure("DRIV", "b"), PrimitiveEventStructure("ORLY", "c"), PrimitiveEventStructure("CBRL", "d")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("c", lambda x: x["Peak Price"]), Variable("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.set_statistics({ StatisticsTypes.SELECTIVITY_MATRIX: selectivityMatrix, StatisticsTypes.ARRIVAL_RATES: arrivalRates }) eval_params = TreeBasedEvaluationMechanismParameters( optimizer_params=StatisticsDeviationAwareOptimizerParameters( tree_plan_params=IterativeImprovementTreePlanBuilderParameters( DEFAULT_TREE_COST_MODEL, 20, IterativeImprovementType.CIRCLE_BASED, IterativeImprovementInitType.GREEDY), statistics_collector_params=StatisticsCollectorParameters( statistics_types=[ StatisticsTypes.ARRIVAL_RATES, StatisticsTypes.SELECTIVITY_MATRIX ])), storage_params=DEFAULT_TESTING_EVALUATION_MECHANISM_SETTINGS. storage_params) runTest('iiGreedy2', [pattern], createTestFile, eval_mechanism_params=eval_params, events=nasdaqEventStream)
def KC_AND(createTestFile=False): """ KC(And([a, b, c])) """ pattern = Pattern( KleeneClosureOperator( AndOperator( PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("GOOG", "c") ), min_size=1, max_size=3 ), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])), ), timedelta(minutes=3) ) runTest("KC_AND_", [pattern], createTestFile, events=nasdaqEventStreamKC)
def onePatternIncludesOther(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("AAPL", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), GreaterThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b")), SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), timedelta(minutes=3)) runMultiTest("onePatternIncludesOther", [pattern1, pattern2], createTestFile, leaf_sharing_eval_mechanism_params)
def KC_AND_NegOffSet_01(createTestFile=False): """ KC(And([a, b, c])) """ pattern = Pattern( KleeneClosureOperator( AndOperator( PrimitiveEventStructure("GOOG", "a"), PrimitiveEventStructure("GOOG", "b"), PrimitiveEventStructure("GOOG", "c") ), min_size=1, max_size=3 ), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])), KCIndexCondition(names={'a', 'b', 'c'}, getattr_func=lambda x: x["Peak Price"], relation_op=lambda x, y: x < 1 + y, offset=-1) ), timedelta(minutes=3) ) runTest("KC_AND_NegOffSet_01_", [pattern], createTestFile, events=nasdaqEventStreamKC)
def oneNotAtTheBeginningTest(createTestFile=False): pattern = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runTest("OneNotBegin", [pattern], createTestFile)
def samePatternDifferentTimeStamps(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=2)) runMultiTest("DifferentTimeStamp", [pattern1, pattern2], createTestFile)
def testWithMultipleNotAtBeginningMiddleEnd(createTestFile=False): pattern = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("AAPL", "a")), PrimitiveEventStructure("AMAZON", "b"), NegationOperator(PrimitiveEventStructure("GOOG", "c")), PrimitiveEventStructure("FB", "d"), NegationOperator(PrimitiveEventStructure("TYP1", "x"))), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runTest("NotEverywhere", [pattern], createTestFile)
def frequencyPatternSearch2Test(createTestFile=False): pattern = Pattern( SeqOperator(PrimitiveEventStructure("LOCM", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("AAPL", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) pattern.set_statistics(StatisticsTypes.FREQUENCY_DICT, { "AAPL": 2, "AMZN": 3, "LOCM": 1 }) eval_params = TreeBasedEvaluationMechanismParameters( TreePlanBuilderParameters( TreePlanBuilderTypes.SORT_BY_FREQUENCY_LEFT_DEEP_TREE), DEFAULT_TESTING_EVALUATION_MECHANISM_SETTINGS.storage_params) runTest("frequency2", [pattern], createTestFile, eval_mechanism_params=eval_params)
def notInTheBeginningShare(createTestFile=False): getattr_func = lambda x: x["Opening Price"] pattern1 = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP2", "y")), NegationOperator(PrimitiveEventStructure("TYP3", "z")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), SmallerThanCondition(Variable("b", getattr_func), Variable("c", getattr_func))), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP2", "y")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b")), GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), timedelta(minutes=5)) pattern3 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), GreaterThanCondition(Variable("c", getattr_func), Variable("b", getattr_func))), timedelta(minutes=5)) eval_mechanism_params = TreeBasedEvaluationMechanismParameters( TreePlanBuilderParameters( TreePlanBuilderTypes.TRIVIAL_LEFT_DEEP_TREE, TreeCostModels.INTERMEDIATE_RESULTS_TREE_COST_MODEL), TreeStorageParameters(sort_storage=False, clean_up_interval=10, prioritize_sorting_by_timestamp=True), MultiPatternEvaluationParameters( MultiPatternEvaluationApproaches.SUBTREES_UNION)) runMultiTest("MultipleNotBeginningShare", [pattern1, pattern2, pattern3], createTestFile, eval_mechanism_params)
def leafIsRoot(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a")), GreaterThanCondition(Variable("a", lambda x: x["Peak Price"]), 135), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), NegationOperator(PrimitiveEventStructure("AMZN", "b")), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runMultiTest("FirstMultiPattern", [pattern1, pattern2], createTestFile)
def multipleNotBeginAndEndTest(createTestFile=False): pattern = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP4", "t")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c"), NegationOperator(PrimitiveEventStructure("TYP2", "y")), NegationOperator(PrimitiveEventStructure("TYP3", "z"))), AndCondition( GreaterThanCondition(Variable("x", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("y", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"])), GreaterThanCondition(Variable("t", lambda x: x["Opening Price"]), Variable("a", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runTest("MultipleNotBeginAndEnd", [pattern], createTestFile)
def rootAndInner(createTestFile=False): #similar to leafIsRoot, but the time windows are different pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a")), GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanEqCondition(Variable("a", lambda x: x["Peak Price"]), 135), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=3)) runMultiTest("RootAndInner", [pattern1, pattern2], createTestFile)
def notInTheBeginningShareFullSharing(createTestFile=False): getattr_func = lambda x: x["Opening Price"] pattern1 = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP2", "y")), NegationOperator(PrimitiveEventStructure("TYP3", "z")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), SmallerThanCondition(Variable("b", getattr_func), Variable("c", getattr_func))), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("TYP1", "x")), NegationOperator(PrimitiveEventStructure("TYP2", "y")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b")), GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), timedelta(minutes=5)) pattern3 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", getattr_func), Variable("b", getattr_func)), GreaterThanCondition(Variable("c", getattr_func), Variable("b", getattr_func))), timedelta(minutes=5)) runMultiTest("MultipleNotBeginningShareFullSharing", [pattern1, pattern2, pattern3], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name="MultipleNotBeginningShare")
def leafIsRootFullSharing(createTestFile=False): pattern1 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a")), GreaterThanCondition(Variable("a", lambda x: x["Peak Price"]), 135), timedelta(minutes=5)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), NegationOperator(PrimitiveEventStructure("AMZN", "b")), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"])), SmallerThanCondition(Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runMultiTest("FirstMultiPatternFullSharing", [pattern1, pattern2], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name="FirstMultiPattern")
def samePatternSharingRoot(createTestFile=False): hierarchyPattern = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=1)) hierarchyPattern2 = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=0.5)) hierarchyPattern3 = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=0.1)) eval_mechanism_params = TreeBasedEvaluationMechanismParameters( TreePlanBuilderParameters( TreePlanBuilderTypes.TRIVIAL_LEFT_DEEP_TREE, TreeCostModels.INTERMEDIATE_RESULTS_TREE_COST_MODEL), TreeStorageParameters(sort_storage=False, clean_up_interval=10, prioritize_sorting_by_timestamp=True), MultiPatternEvaluationParameters( MultiPatternEvaluationApproaches.SUBTREES_UNION)) runMultiTest('hierarchyMultiPattern', [hierarchyPattern, hierarchyPattern2, hierarchyPattern3], createTestFile, eval_mechanism_params)
def simpleNotTest(createTestFile=False): pattern = Pattern( SeqOperator( PrimitiveEventStructure("AAPL", "a"), NegationOperator(PrimitiveEventStructure("AMZN", "b")), PrimitiveEventStructure("GOOG", "c"), ), AndCondition( GreaterThanCondition( Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"]), ), SmallerThanCondition( Variable("b", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]), ), ), timedelta(minutes=5), confidence=0.95, ) runTest("simpleNotProb", [pattern], createTestFile, eventStream=nasdaqEventStreamP)
def threePatternsTestFullSharing(createTestFile=False): pattern1 = Pattern( AndOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=1)) pattern2 = Pattern( SeqOperator(PrimitiveEventStructure("MSFT", "a"), PrimitiveEventStructure("DRIV", "b"), PrimitiveEventStructure("MSFT", "c"), PrimitiveEventStructure("DRIV", "d"), PrimitiveEventStructure("MSFT", "e")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("c", lambda x: x["Peak Price"]), Variable("d", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("d", lambda x: x["Peak Price"]), Variable("e", lambda x: x["Peak Price"]))), timedelta(minutes=10)) pattern3 = Pattern( SeqOperator(PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"])), GreaterThanCondition(Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"]))), timedelta(minutes=5)) runMultiTest("ThreePatternTestFullSharing", [pattern1, pattern2, pattern3], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name="ThreePatternTest")
def samePatternSharingRootFullSharing(createTestFile=False): hierarchyPattern = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=1)) hierarchyPattern2 = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=0.5)) hierarchyPattern3 = Pattern( AndOperator(PrimitiveEventStructure("AMZN", "a"), PrimitiveEventStructure("AAPL", "b"), PrimitiveEventStructure("GOOG", "c")), AndCondition( SmallerThanCondition(Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"])), SmallerThanCondition(Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]))), timedelta(minutes=0.1)) runMultiTest('hierarchyMultiPatternFullSharing', [hierarchyPattern, hierarchyPattern2, hierarchyPattern3], createTestFile, subtree_sharing_eval_mechanism_params, expected_file_name='hierarchyMultiPattern')
def threePatternsTest(createTestFile=False): pattern1 = Pattern( AndOperator( PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), ), AndCondition( SmallerThanCondition( Variable("a", lambda x: x["Peak Price"] - x["Opening Price"]), Variable("b", lambda x: x["Peak Price"] - x["Opening Price"]), ), ), timedelta(minutes=1), confidence=0.96) pattern2 = Pattern( SeqOperator( PrimitiveEventStructure("MSFT", "a"), PrimitiveEventStructure("DRIV", "b"), PrimitiveEventStructure("MSFT", "c"), PrimitiveEventStructure("DRIV", "d"), PrimitiveEventStructure("MSFT", "e"), ), AndCondition( SmallerThanCondition( Variable("a", lambda x: x["Peak Price"]), Variable("b", lambda x: x["Peak Price"]), ), SmallerThanCondition( Variable("b", lambda x: x["Peak Price"]), Variable("c", lambda x: x["Peak Price"]), ), SmallerThanCondition( Variable("c", lambda x: x["Peak Price"]), Variable("d", lambda x: x["Peak Price"]), ), SmallerThanCondition( Variable("d", lambda x: x["Peak Price"]), Variable("e", lambda x: x["Peak Price"]), ), ), timedelta(minutes=10), confidence=0.6, ) pattern3 = Pattern( SeqOperator( PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AMZN", "b"), PrimitiveEventStructure("GOOG", "c"), ), AndCondition( GreaterThanCondition( Variable("a", lambda x: x["Opening Price"]), Variable("c", lambda x: x["Opening Price"]), ), GreaterThanCondition( Variable("a", lambda x: x["Opening Price"]), Variable("b", lambda x: x["Opening Price"]), ), ), timedelta(minutes=5), confidence=0.6, ) runMultiTest("ThreePatternTestProb", [pattern1, pattern2, pattern3], createTestFile, eventStream=nasdaqEventStreamP)