-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👌 IMPROVE: Add solution for validation exercise
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
docs/sections/writing_workflows/include/code/validation/validated_multiple_add.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from aiida.orm import Int, Float | ||
from aiida.engine import WorkChain, calcfunction | ||
|
||
|
||
@calcfunction | ||
def addition(x, y): | ||
return x + y | ||
|
||
|
||
@calcfunction | ||
def multiplication(x, y): | ||
return x * y | ||
|
||
|
||
def validate_z(node, _): | ||
"""Validate the `z` input.""" | ||
if node.value == 0: | ||
return "The value of `z` can not be zero." | ||
|
||
|
||
def validate_inputs(inputs, _): | ||
"""Validate the top-level inputs.""" | ||
if inputs["x"] + inputs["y"] == 0: | ||
return "The sum of `x` and `y` can not be zero." | ||
|
||
|
||
class MultiplyAddWorkChain(WorkChain): | ||
"""WorkChain to multiply two integers and add a third.""" | ||
|
||
@classmethod | ||
def define(cls, spec): | ||
"""Specify inputs, outputs, and the workchain outline.""" | ||
super().define(spec) | ||
|
||
spec.input("x", valid_type=(Int, Float)) | ||
spec.input("y", valid_type=(Int, Float)) | ||
spec.input("z", valid_type=Int, validator=validate_z) | ||
spec.inputs.validator = validate_inputs | ||
|
||
spec.outline(cls.multiply, cls.add, cls.result) | ||
spec.output("workchain_result", valid_type=(Int, Float)) | ||
spec.output("product", valid_type=(Int, Float)) | ||
|
||
def multiply(self): | ||
"""Multiply two integers.""" | ||
|
||
multiplication_result = multiplication(self.inputs.x, self.inputs.y) | ||
|
||
# Passing to context to be used by other functions | ||
self.ctx.product = multiplication_result | ||
|
||
def add(self): | ||
"""Add two numbers.""" | ||
|
||
# Call `addition` using a variable from the context and one of the inputs | ||
addition_result = addition(self.ctx.product, self.inputs.z) | ||
|
||
# Passing to context to be used by other functions | ||
self.ctx.summation = addition_result | ||
|
||
def result(self): | ||
"""Parse the result.""" | ||
|
||
# Declaring the output | ||
self.out("workchain_result", self.ctx.summation) | ||
self.out("product", self.ctx.product) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters