Esempio n. 1
0
def generate_dependency_graph(contract_specification, call_dependencies_repo, call_dependents_repo, call_leafs_repo,
                              call_requirement_repo):

    assert isinstance(contract_specification, ContractSpecification)
    dsl_module = dsl_parse(dsl_source=contract_specification.specification)
    assert isinstance(dsl_module, Module)
    dsl_globals = DslNamespace()
    function_defs, expressions = extract_defs_and_exprs(dsl_module, dsl_globals)
    dsl_expr = expressions[0]
    assert isinstance(dsl_expr, DslExpression)
    dsl_locals = DslNamespace()

    leaf_ids = []
    all_dependents = defaultdict(list)

    # Generate stubbed call from the parsed DSL module object.
    for stub in generate_stubbed_calls(contract_specification.id, dsl_module, dsl_expr, dsl_globals, dsl_locals):
        # assert isinstance(stub, StubbedCall)

        # Register the call requirements.
        call_id = stub.call_id
        dsl_source = str(stub.dsl_expr)
        effective_present_time = stub.effective_present_time
        call_requirement = register_call_requirement(call_id, dsl_source, effective_present_time)

        # Hold onto the dsl_expr, helps in "single process" modes....
        call_requirement._dsl_expr = stub.dsl_expr
        # - put the entity directly in the cache, otherwise the entity will be regenerated when it is next accessed
        #   and the _dsl_expr will be lost.
        call_requirement_repo.add_cache(call_id, call_requirement)

        # Register the call requirements.
        dependencies = stub.requirements
        register_call_dependencies(call_id, dependencies)

        # Keep track of the leaves and the dependents.
        if len(dependencies) == 0:
            leaf_ids.append(call_id)
        else:
            for dependency_call_id in dependencies:
                all_dependents[dependency_call_id].append(call_id)

    # Register the call dependents.
    for call_id, dependents in all_dependents.items():
        register_call_dependents(call_id, dependents)
    register_call_dependents(contract_specification.id, [])

    # Generate and register the call order.
    link_id = contract_specification.id
    for call_id in generate_execution_order(leaf_ids, call_dependents_repo, call_dependencies_repo):
        register_call_link(link_id, call_id)
        link_id = call_id

    # Register the leaf ids.
    register_call_leafs(contract_specification.id, leaf_ids)
Esempio n. 2
0
def generate_dependency_graph(contract_specification, call_dependencies_repo,
                              call_dependents_repo):

    assert isinstance(contract_specification, ContractSpecification)
    dsl_module = dsl_parse(dsl_source=contract_specification.specification)
    assert isinstance(dsl_module, Module)
    dsl_globals = DslNamespace()
    function_defs, expressions = extract_defs_and_exprs(
        dsl_module, dsl_globals)
    dsl_expr = expressions[0]
    assert isinstance(dsl_expr, DslExpression)
    dsl_locals = DslNamespace()

    leaf_call_ids = []
    all_dependents = defaultdict(list)
    # Generate stubbed call from the parsed DSL module object.
    for stub in generate_stubbed_calls(contract_specification.id, dsl_module,
                                       dsl_expr, dsl_globals, dsl_locals):
        assert isinstance(stub, StubbedCall)
        call_id = stub.call_id
        dsl_source = stub.dsl_source
        effective_present_time = stub.effective_present_time
        dependencies = stub.dependencies

        # Register the call requirements.
        register_call_requirement(call_id, dsl_source, effective_present_time)

        # Register the call dependencies.
        register_call_dependencies(call_id, dependencies)

        # Keep track of the leaves and the dependents.
        if len(dependencies) == 0:
            leaf_call_ids.append(call_id)
        else:
            for dependency_call_id in dependencies:
                all_dependents[dependency_call_id].append(call_id)

    # Register the call dependents.
    for call_id, dependents in all_dependents.items():
        register_call_dependents(call_id, dependents)
    register_call_dependents(contract_specification.id, [])
    # Generate and register the call order.
    link_id = contract_specification.id
    for call_id in generate_execution_order(leaf_call_ids,
                                            call_dependents_repo,
                                            call_dependencies_repo):
        register_call_link(link_id, call_id)
        link_id = call_id
    def test_delete_result(self):
        # In this test, there are two "calls": call1 and call2.
        # It is supposed that call1 happens first, and call2 uses the result of call1.
        # Therefore call2 depends upon call1, call1 is a dependency of call2, and call2 is a dependent of call1.
        call1_id = 'call1'
        call2_id = 'call2'
        contract_valuation_id = 'val1'
        contract_specification_id = 'spec1'
        # call1_id = uuid4().hex
        # call2_id = uuid4().hex
        # contract_valuation_id = uuid4().hex
        # contract_specification_id = uuid4().hex

        register_call_dependencies(call2_id, [call1_id])

        # Check the policy has the dependencies for call2.
        self.assertEqual(self.policy.dependencies[call2_id], [call1_id])

        # Register dependents of call1, as call2.
        register_call_dependents(call1_id, [call2_id])

        # Check the policy has the dependencies for call2.
        self.assertEqual(self.policy.dependents[call1_id], [call2_id])

        # Register call result for call1.
        # - this should trigger deletion of call2 result
        call1_result = register_call_result(call1_id, 1.0, {}, contract_valuation_id, contract_specification_id, [])

        # Check the policy has the result for call1.
        self.assertTrue(call1_result.id in self.policy.result)

        # Check the result for call1 exists.
        self.assertTrue(call1_result.id in self.call_result_repo)

        # Register call result for call2.
        call2_result = register_call_result(call2_id, 1.0, {}, contract_valuation_id, contract_specification_id, [])

        # Check the policy has the result for call2.
        self.assertTrue(call2_result.id in self.policy.result)

        # Check the result for call2 exists.
        self.assertTrue(call2_result.id in self.call_result_repo)

        # Check the policy does not have the result for call1.
        self.assertFalse(call1_result.id in self.policy.result)

        # Check the result for call1 doesn't exist (because it's dependents have results).
        self.assertFalse(call1_result.id in self.call_result_repo)
Esempio n. 4
0
def generate_dependency_graph(contract_specification, call_dependencies_repo, call_dependents_repo):

    assert isinstance(contract_specification, ContractSpecification)
    dsl_module = dsl_parse(dsl_source=contract_specification.specification)
    assert isinstance(dsl_module, Module)
    dsl_globals = DslNamespace()
    function_defs, expressions = extract_defs_and_exprs(dsl_module, dsl_globals)
    dsl_expr = expressions[0]
    assert isinstance(dsl_expr, DslExpression)
    dsl_locals = DslNamespace()

    leaf_call_ids = []
    all_dependents = defaultdict(list)
    # Generate stubbed call from the parsed DSL module object.
    for stub in generate_stubbed_calls(contract_specification.id, dsl_module, dsl_expr, dsl_globals, dsl_locals):
        assert isinstance(stub, StubbedCall)
        call_id = stub.call_id
        dsl_source = stub.dsl_source
        effective_present_time = stub.effective_present_time
        dependencies = stub.dependencies

        # Register the call requirements.
        register_call_requirement(call_id, dsl_source, effective_present_time)

        # Register the call dependencies.
        register_call_dependencies(call_id, dependencies)

        # Keep track of the leaves and the dependents.
        if len(dependencies) == 0:
            leaf_call_ids.append(call_id)
        else:
            for dependency_call_id in dependencies:
                all_dependents[dependency_call_id].append(call_id)

    # Register the call dependents.
    for call_id, dependents in all_dependents.items():
        register_call_dependents(call_id, dependents)
    register_call_dependents(contract_specification.id, [])
    # Generate and register the call order.
    link_id = contract_specification.id
    for call_id in generate_execution_order(leaf_call_ids, call_dependents_repo, call_dependencies_repo):
        register_call_link(link_id, call_id)
        link_id = call_id
Esempio n. 5
0
 def register_call_dependencies(self, call_id, dependencies):
     return register_call_dependencies(call_id=call_id,
                                       dependencies=dependencies)
Esempio n. 6
0
def generate_dependency_graph(contract_specification, call_dependencies_repo, call_dependents_repo,
                              call_requirement_repo, dsl_classes=None):
    assert isinstance(contract_specification, ContractSpecification)
    dsl_module = dsl_parse(
        dsl_source=contract_specification.source_code,
        dsl_classes=dsl_classes,
    )
    assert isinstance(dsl_module, Module)
    dsl_globals = dsl_module.namespace.copy()
    function_defs, expressions = extract_defs_and_exprs(dsl_module, dsl_globals)
    dsl_expr = expressions[0]
    assert isinstance(dsl_expr, DslExpression)
    dsl_locals = DslNamespace()

    leaf_ids = []
    all_dependents = defaultdict(list)

    # Generate stubbed call from the parsed DSL module object.
    for stubed_call in generate_stubbed_calls(contract_specification.id, dsl_expr, dsl_globals, dsl_locals,
                                              contract_specification.observation_date):
        # assert isinstance(stub, StubbedCall)

        # Estimate the cost of evaluating this expression.
        estimated_cost = stubed_call.dsl_expr.cost_expression()

        # Register the call requirements.
        call_id = stubed_call.call_id
        dsl_source = str(stubed_call.dsl_expr)
        present_time = stubed_call.present_time
        call_requirement = register_call_requirement(
            call_id=call_id,
            dsl_source=dsl_source,
            present_time=present_time,
            contract_specification_id=contract_specification.id,
            cost=estimated_cost,
        )

        # Hold onto the dsl_expr, helps in "single process" modes.
        # - put the entity directly in the cache, otherwise the entity will be
        # regenerated when it is next accessed and the _dsl_expr will be "lost"
        call_requirement._dsl_expr = stubed_call.dsl_expr
        call_requirement_repo.add_cache(call_id, call_requirement)

        # Register the call dependencies (things needed by this call).
        dependencies = stubed_call.requirements
        register_call_dependencies(call_id, dependencies)

        # Keep track of the leaves and the dependents.
        if len(dependencies) == 0:
            leaf_ids.append(call_id)
        else:
            for dependency_call_id in dependencies:
                all_dependents[dependency_call_id].append(call_id)

    # Register the call dependents.
    for call_id, dependents in all_dependents.items():
        register_call_dependents(call_id, dependents)
    register_call_dependents(contract_specification.id, [])

    # Generate and register the call order.
    link_id = contract_specification.id
    for call_id in generate_execution_order(leaf_ids, call_dependents_repo, call_dependencies_repo):
        register_call_link(link_id, call_id)
        link_id = call_id

    # Register the leaf ids.
    register_call_leafs(contract_specification.id, leaf_ids)
Esempio n. 7
0
 def register_call_dependencies(self, call_id, dependencies):
     return register_call_dependencies(call_id=call_id, dependencies=dependencies)
Esempio n. 8
0
def generate_dependency_graph(contract_specification,
                              call_dependencies_repo,
                              call_dependents_repo,
                              call_requirement_repo,
                              dsl_classes=None):
    assert isinstance(contract_specification, ContractSpecification)
    dsl_module = dsl_parse(
        dsl_source=contract_specification.source_code,
        dsl_classes=dsl_classes,
    )
    assert isinstance(dsl_module, Module)
    dsl_globals = dsl_module.namespace.copy()
    function_defs, expressions = extract_defs_and_exprs(
        dsl_module, dsl_globals)
    dsl_expr = expressions[0]
    assert isinstance(dsl_expr, DslExpression)
    dsl_locals = DslNamespace()

    leaf_ids = []
    all_dependents = defaultdict(list)

    # Generate stubbed call from the parsed DSL module object.
    for stubed_call in generate_stubbed_calls(
            contract_specification.id, dsl_expr, dsl_globals, dsl_locals,
            contract_specification.observation_date):
        # assert isinstance(stub, StubbedCall)

        # Estimate the cost of evaluating this expression.
        estimated_cost = stubed_call.dsl_expr.cost_expression()

        # Register the call requirements.
        call_id = stubed_call.call_id
        dsl_source = str(stubed_call.dsl_expr)
        present_time = stubed_call.present_time
        call_requirement = register_call_requirement(
            call_id=call_id,
            dsl_source=dsl_source,
            present_time=present_time,
            contract_specification_id=contract_specification.id,
            cost=estimated_cost,
        )

        # Hold onto the dsl_expr, helps in "single process" modes.
        # - put the entity directly in the cache, otherwise the entity will be
        # regenerated when it is next accessed and the _dsl_expr will be "lost"
        call_requirement._dsl_expr = stubed_call.dsl_expr
        call_requirement_repo.add_cache(call_id, call_requirement)

        # Register the call dependencies (things needed by this call).
        dependencies = stubed_call.requirements
        register_call_dependencies(call_id, dependencies)

        # Keep track of the leaves and the dependents.
        if len(dependencies) == 0:
            leaf_ids.append(call_id)
        else:
            for dependency_call_id in dependencies:
                all_dependents[dependency_call_id].append(call_id)

    # Register the call dependents.
    for call_id, dependents in all_dependents.items():
        register_call_dependents(call_id, dependents)
    register_call_dependents(contract_specification.id, [])

    # Generate and register the call order.
    link_id = contract_specification.id
    for call_id in generate_execution_order(leaf_ids, call_dependents_repo,
                                            call_dependencies_repo):
        register_call_link(link_id, call_id)
        link_id = call_id

    # Register the leaf ids.
    register_call_leafs(contract_specification.id, leaf_ids)