-
Notifications
You must be signed in to change notification settings - Fork 243
Add new transform compileBdd and compileBddForAws #2953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06ee3c6
Add new build transform CompileBddFromEndpointRuleSet
joewyz 6a84423
Add changelog
joewyz e365f44
Split transform into two and move to different packages
joewyz 8214d76
update changelog
joewyz 3ee8880
make compileBddForS3 aws generic
joewyz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changes/next-release/feature-e8b0c9d42596402804ac70a348da0fd38622a0e4.json
This file contains hidden or 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,7 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Add new transform compileBdd and compileBddForAws", | ||
| "pull_requests": [ | ||
| "[#2953](https://github.com/smithy-lang/smithy/pull/2953)" | ||
| ] | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
...nts/src/main/java/software/amazon/smithy/rulesengine/aws/transforms/CompileBddForAws.java
This file contains hidden or 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,70 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.rulesengine.aws.transforms; | ||
|
|
||
| import static software.amazon.smithy.rulesengine.transforms.CompileBdd.compileBdd; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import software.amazon.smithy.build.ProjectionTransformer; | ||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.model.transform.ModelTransformer; | ||
| import software.amazon.smithy.rulesengine.aws.s3.S3TreeRewriter; | ||
| import software.amazon.smithy.rulesengine.language.EndpointRuleSet; | ||
| import software.amazon.smithy.rulesengine.language.evaluation.TestEvaluator; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointBddTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestCase; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait; | ||
|
|
||
| /** | ||
| * A dedicated transform to compile Binary Decision Diagram (BDD) from AWS services. | ||
| */ | ||
| public final class CompileBddForAws implements ProjectionTransformer { | ||
|
|
||
| private static final ShapeId S3_SERVICE_ID = ShapeId.from("com.amazonaws.s3#AmazonS3"); | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "compileBddForAws"; | ||
| } | ||
|
|
||
| @Override | ||
| public Model transform(TransformContext transformContext) { | ||
| Model model = transformContext.getModel(); | ||
| Set<Shape> shapes = new HashSet<>(); | ||
| for (ServiceShape serviceShape : model.getServiceShapes()) { | ||
| if (serviceShape.hasTrait(EndpointRuleSetTrait.ID) | ||
| && !serviceShape.hasTrait(EndpointBddTrait.ID)) { | ||
| EndpointRuleSet rules = getEndpointRuleSet(serviceShape); | ||
| EndpointBddTrait bdd = compileBdd(rules); | ||
| shapes.add(serviceShape.toBuilder().addTrait(bdd).build()); | ||
| } | ||
| } | ||
| return ModelTransformer.create().replaceShapes(model, shapes); | ||
| } | ||
|
|
||
| private EndpointRuleSet getEndpointRuleSet(ServiceShape serviceShape) { | ||
| EndpointRuleSet rules = serviceShape.expectTrait(EndpointRuleSetTrait.class).getEndpointRuleSet(); | ||
| if (serviceShape.getId().equals(S3_SERVICE_ID)) { | ||
| return applyS3Transform(rules, serviceShape); | ||
| } | ||
| return rules; | ||
| } | ||
|
|
||
| private EndpointRuleSet applyS3Transform(EndpointRuleSet rules, ServiceShape serviceShape) { | ||
| EndpointRuleSet transformedRules = S3TreeRewriter.transform(rules); | ||
| serviceShape.getTrait(EndpointTestsTrait.class).ifPresent(testsTrait -> { | ||
| for (EndpointTestCase testCase : testsTrait.getTestCases()) { | ||
| TestEvaluator.evaluate(transformedRules, testCase); | ||
| } | ||
| }); | ||
| return transformedRules; | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...s/src/main/resources/META-INF/services/software.amazon.smithy.build.ProjectionTransformer
This file contains hidden or 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 @@ | ||
| software.amazon.smithy.rulesengine.aws.transforms.CompileBddForAws |
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
...-rules-engine/src/main/java/software/amazon/smithy/rulesengine/transforms/CompileBdd.java
This file contains hidden or 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,67 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.rulesengine.transforms; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import software.amazon.smithy.build.ProjectionTransformer; | ||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.transform.ModelTransformer; | ||
| import software.amazon.smithy.rulesengine.language.EndpointRuleSet; | ||
| import software.amazon.smithy.rulesengine.logic.bdd.CostOptimization; | ||
| import software.amazon.smithy.rulesengine.logic.bdd.SiftingOptimization; | ||
| import software.amazon.smithy.rulesengine.logic.cfg.Cfg; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointBddTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; | ||
|
|
||
| /** | ||
| * Compiles a Binary Decision Diagram (BDD) from a service's {@code @endpointRuleSet} | ||
| * trait and attaches the compiled {@code @endpointBdd} trait to the service shape. | ||
| */ | ||
| public final class CompileBdd implements ProjectionTransformer { | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "compileBdd"; | ||
| } | ||
|
|
||
| @Override | ||
| public Model transform(TransformContext transformContext) { | ||
| Model model = transformContext.getModel(); | ||
| Set<Shape> shapes = new HashSet<>(); | ||
| for (ServiceShape serviceShape : model.getServiceShapes()) { | ||
| if (serviceShape.hasTrait(EndpointRuleSetTrait.ID) | ||
| && !serviceShape.hasTrait(EndpointBddTrait.ID)) { | ||
| EndpointRuleSetTrait endpointRuleSetTrait = serviceShape.expectTrait(EndpointRuleSetTrait.class); | ||
| EndpointBddTrait bdd = compileBdd(endpointRuleSetTrait.getEndpointRuleSet()); | ||
| shapes.add(serviceShape.toBuilder().addTrait(bdd).build()); | ||
| } | ||
| } | ||
| return ModelTransformer.create().replaceShapes(model, shapes); | ||
| } | ||
|
|
||
| /** | ||
| * Compile endpointBdd trait from endpoint ruleset and return the optimized version. | ||
| * | ||
| * @param rules Endpoint ruleset from service shape. | ||
| */ | ||
| public static EndpointBddTrait compileBdd(EndpointRuleSet rules) { | ||
| // Create the CFG to start BDD compilation process. | ||
| Cfg cfg = Cfg.from(rules); | ||
| EndpointBddTrait unoptimizedTrait = EndpointBddTrait.from(cfg); | ||
|
|
||
| // Sift the BDD to shorten paths and reduce the BDD size. | ||
| EndpointBddTrait siftedTrait = SiftingOptimization.builder().cfg(cfg).build().apply(unoptimizedTrait); | ||
|
|
||
| // "cost optimize" the BDD to ensure cheap conditions come first with up to 10% size impact. | ||
| EndpointBddTrait costOptimizedTrait = CostOptimization.builder().cfg(cfg).build().apply(siftedTrait); | ||
|
|
||
| // Remove unreferenced conditions. This is destructive and further optimizations cannot be applied after this. | ||
| return costOptimizedTrait.removeUnreferencedConditions(); | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...e/src/main/resources/META-INF/services/software.amazon.smithy.build.ProjectionTransformer
This file contains hidden or 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 @@ | ||
| software.amazon.smithy.rulesengine.transforms.CompileBdd |
34 changes: 34 additions & 0 deletions
34
...es-engine/src/test/java/software/amazon/smithy/rulesengine/transforms/CompileBddTest.java
This file contains hidden or 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,34 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.rulesengine.transforms; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointBddTrait; | ||
|
|
||
| public class CompileBddTest { | ||
|
|
||
| @Test | ||
| public void compilesAndAttachesBddTrait() throws Exception { | ||
| ShapeId serviceId = ShapeId.from("smithy.example#ExampleService"); | ||
| Model model = Model.assembler() | ||
| .discoverModels() | ||
| .addImport(Paths.get(getClass().getResource("compile-bdd.smithy").toURI())) | ||
| .assemble() | ||
| .unwrap(); | ||
| TransformContext context = TransformContext.builder() | ||
| .model(model) | ||
| .build(); | ||
| Model result = new CompileBdd().transform(context); | ||
| Shape serviceShape = result.expectShape(serviceId); | ||
| assertTrue(serviceShape.hasTrait(EndpointBddTrait.ID)); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
...ngine/src/test/resources/software/amazon/smithy/rulesengine/transforms/compile-bdd.smithy
This file contains hidden or 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,50 @@ | ||
| $version: "2.0" | ||
|
|
||
| namespace smithy.example | ||
|
|
||
| use smithy.rules#clientContextParams | ||
| use smithy.rules#endpointRuleSet | ||
| use smithy.rules#endpointTests | ||
|
|
||
| @clientContextParams( | ||
| Region: {type: "string", documentation: "docs"} | ||
| ) | ||
| @endpointRuleSet({ | ||
| "version": "1.1" | ||
| "parameters": { | ||
| "Region": { | ||
| "required": true | ||
| "type": "String" | ||
| "documentation": "docs" | ||
| } | ||
| } | ||
| "rules": [ | ||
| { | ||
| "conditions": [] | ||
| "documentation": "base rule" | ||
| "endpoint": { | ||
| "url": "https://{Region}.amazonaws.com" | ||
| "properties": {} | ||
| "headers": {} | ||
| } | ||
| "type": "endpoint" | ||
| } | ||
| ] | ||
| }) | ||
| @endpointTests({ | ||
| "version": "1.0" | ||
| "testCases": [ | ||
| { | ||
| "documentation": "example endpoint test" | ||
| "expect": { | ||
| "endpoint": { | ||
| "url": "https://example-region.amazonaws.com" | ||
| } | ||
| } | ||
| "params": { | ||
| Region: "example-region" | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
| service ExampleService {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.