1 /* 2 * Copyright 2020-2022 Foreseeti AB <https://foreseeti.com> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.mal_lang.langspec.builders.step; 18 19 import static java.util.Objects.requireNonNull; 20 21 import jakarta.json.JsonObject; 22 import java.util.List; 23 import java.util.Map; 24 import org.mal_lang.langspec.Asset; 25 import org.mal_lang.langspec.builders.AssetBuilder; 26 27 /** 28 * A builder for creating {@link org.mal_lang.langspec.step.StepExpression} objects. 29 * 30 * @since 1.0.0 31 */ 32 public abstract class StepExpressionBuilder { 33 /** 34 * Constructs a new {@code StepExpressionBuilder} object. 35 * 36 * @since 1.0.0 37 */ 38 protected StepExpressionBuilder() {} 39 40 /** 41 * Returns the target asset of this {@code StepExpressionBuilder}. 42 * 43 * @param sourceAsset the source asset 44 * @param assets all assets of the language 45 * @param assetBuilders all asset builders of the language 46 * @return the target asset of this {@code StepExpressionBuilder} 47 * @since 1.0.0 48 */ 49 public abstract Asset getTarget( 50 Asset sourceAsset, Map<String, Asset> assets, List<AssetBuilder> assetBuilders); 51 52 /** 53 * Creates a new {@code StepExpressionBuilder} from a {@link jakarta.json.JsonObject}. 54 * 55 * @param jsonStepExpression the {@link jakarta.json.JsonObject} 56 * @return a new {@code StepExpressionBuilder} 57 * @throws java.lang.NullPointerException if {@code jsonStepExpression} is {@code null} 58 * @since 1.0.0 59 */ 60 public static StepExpressionBuilder fromJson(JsonObject jsonStepExpression) { 61 requireNonNull(jsonStepExpression); 62 switch (jsonStepExpression.getString("type")) { 63 case "union": 64 case "intersection": 65 case "difference": 66 case "collect": 67 return StepBinaryOperationBuilder.fromJson(jsonStepExpression); 68 case "transitive": 69 return StepTransitiveBuilder.fromJson(jsonStepExpression); 70 case "subType": 71 return StepSubTypeBuilder.fromJson(jsonStepExpression); 72 case "field": 73 case "attackStep": 74 case "variable": 75 return StepReferenceBuilder.fromJson(jsonStepExpression); 76 default: 77 throw new RuntimeException( 78 String.format( 79 "Invalid step expression type \"%s\"", jsonStepExpression.getString("type"))); 80 } 81 } 82 }