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 notOrPatternTransformationTest(): pattern = Pattern( SeqOperator( PrimitiveEventStructure("AAPL", "a"), NegationOperator( OrOperator(PrimitiveEventStructure("AMZN", "z"), PrimitiveEventStructure("GOOG", "g"))), PrimitiveEventStructure("MSFT", "m")), TrueCondition(), timedelta(minutes=5)) expected_pattern = Pattern( SeqOperator( PrimitiveEventStructure("AAPL", "a"), AndOperator(NegationOperator(PrimitiveEventStructure("AMZN", "z")), NegationOperator(PrimitiveEventStructure("GOOG", "g"))), PrimitiveEventStructure("MSFT", "m")), TrueCondition(), timedelta(minutes=5)) rules_directive = [PatternTransformationRules.NOT_OR_PATTERN] params = PatternPreprocessingParameters() params.transformation_rules = rules_directive pattern_transformation = PatternPreprocessor(params) transformed_patterns = pattern_transformation.transform_patterns([pattern]) assert len( transformed_patterns) == 1, "Test notOrPatternTransformation Failed" assert transformed_patterns[0].full_structure == expected_pattern.full_structure, \ "Test notOrPatternTransformation Failed"
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 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 multipleNotAtTheBeginningTest(createTestFile=False): pattern = 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", 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("MultipleNotBegin", [pattern], createTestFile)
def _actually_transform_structure(self, pattern_structure): if pattern_structure.get_top_operator() != NegationOperator or \ pattern_structure.arg.get_top_operator() != OrOperator: return pattern_structure new_args = [ NegationOperator(arg) for arg in pattern_structure.arg.args ] return AndOperator(*new_args)
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 notNotPatternTransformationTest(): pattern = Pattern( SeqOperator( PrimitiveEventStructure("AMZN", "z"), NegationOperator( NegationOperator(PrimitiveEventStructure("GOOG", "g")))), TrueCondition(), timedelta(minutes=5)) expected_pattern = Pattern( SeqOperator(PrimitiveEventStructure("AMZN", "z"), PrimitiveEventStructure("GOOG", "g")), TrueCondition(), timedelta(minutes=5)) pattern_transformation = PatternPreprocessor( PatternPreprocessingParameters(TESTING_PREPROCESSING_RULES_ORDER)) transformed_patterns = pattern_transformation.transform_patterns([pattern]) assert len( transformed_patterns) == 1, "Test notNotPatternTransformation Failed" assert transformed_patterns[0].full_structure == expected_pattern.full_structure, \ "Test notNotPatternTransformation Failed"
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 innerOrPatternTransformationTest(): pattern = Pattern( SeqOperator( NegationOperator(PrimitiveEventStructure("AMZN", "z")), OrOperator(PrimitiveEventStructure("GOOG", "g"), PrimitiveEventStructure("AAPL", "a")), OrOperator(PrimitiveEventStructure("GOOG", "gg"), PrimitiveEventStructure("AAPL", "aa"))), TrueCondition(), timedelta(minutes=5)) pattern_list = [ Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("AMZN", "z")), PrimitiveEventStructure("GOOG", "g"), PrimitiveEventStructure("GOOG", "gg")), TrueCondition(), timedelta(minutes=5)), Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("AMZN", "z")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AAPL", "gg")), TrueCondition(), timedelta(minutes=5)), Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("AMZN", "z")), PrimitiveEventStructure("GOOG", "g"), PrimitiveEventStructure("AAPL", "aa")), TrueCondition(), timedelta(minutes=5)), Pattern( SeqOperator(NegationOperator(PrimitiveEventStructure("AMZN", "z")), PrimitiveEventStructure("AAPL", "a"), PrimitiveEventStructure("AAPL", "aa")), TrueCondition(), timedelta(minutes=5)) ] pattern_transformation = PatternPreprocessor( PatternPreprocessingParameters(TESTING_PREPROCESSING_RULES_ORDER)) transformed_patterns = pattern_transformation.transform_patterns([pattern]) expected_patterns_structures = [] for tmp_pattern in pattern_list: expected_patterns_structures.append(tmp_pattern.full_structure) assert len(transformed_patterns) == len( pattern_list), "Test innerOrPatternTransformation Failed" for tmp_pattern in transformed_patterns: assert tmp_pattern.full_structure in expected_patterns_structures, "Test innerOrPatternTransformation Failed"
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)) runTest("simpleNot", [pattern], createTestFile)
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 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")