1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.langspec.step;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.JsonObject;
22 import java.util.Map;
23 import org.mal_lang.langspec.Asset;
24 import org.mal_lang.langspec.Variable;
25 import org.mal_lang.langspec.builders.step.StepAttackStepBuilder;
26 import org.mal_lang.langspec.builders.step.StepCollectBuilder;
27 import org.mal_lang.langspec.builders.step.StepDifferenceBuilder;
28 import org.mal_lang.langspec.builders.step.StepExpressionBuilder;
29 import org.mal_lang.langspec.builders.step.StepFieldBuilder;
30 import org.mal_lang.langspec.builders.step.StepIntersectionBuilder;
31 import org.mal_lang.langspec.builders.step.StepSubTypeBuilder;
32 import org.mal_lang.langspec.builders.step.StepTransitiveBuilder;
33 import org.mal_lang.langspec.builders.step.StepUnionBuilder;
34 import org.mal_lang.langspec.builders.step.StepVariableBuilder;
35
36
37
38
39
40
41 public abstract class StepExpression {
42 private final Asset sourceAsset;
43 private final Asset targetAsset;
44
45 StepExpression(Asset sourceAsset, Asset targetAsset) {
46 this.sourceAsset = requireNonNull(sourceAsset);
47 this.targetAsset = requireNonNull(targetAsset);
48 }
49
50
51
52
53
54
55
56 public Asset getSourceAsset() {
57 return this.sourceAsset;
58 }
59
60
61
62
63
64
65
66 public Asset getTargetAsset() {
67 return this.targetAsset;
68 }
69
70
71
72
73
74
75
76 public abstract JsonObject toJson();
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static StepExpression fromBuilder(
92 StepExpressionBuilder builder,
93 Asset sourceAsset,
94 Map<String, Asset> assets,
95 Map<Variable, Asset> variableTargets) {
96 requireNonNull(builder);
97 requireNonNull(sourceAsset);
98 requireNonNull(assets);
99 requireNonNull(variableTargets);
100 if (builder instanceof StepUnionBuilder) {
101 return StepUnion.fromBuilder(
102 (StepUnionBuilder) builder, sourceAsset, assets, variableTargets);
103 }
104 if (builder instanceof StepIntersectionBuilder) {
105 return StepIntersection.fromBuilder(
106 (StepIntersectionBuilder) builder, sourceAsset, assets, variableTargets);
107 }
108 if (builder instanceof StepDifferenceBuilder) {
109 return StepDifference.fromBuilder(
110 (StepDifferenceBuilder) builder, sourceAsset, assets, variableTargets);
111 }
112 if (builder instanceof StepCollectBuilder) {
113 return StepCollect.fromBuilder(
114 (StepCollectBuilder) builder, sourceAsset, assets, variableTargets);
115 }
116 if (builder instanceof StepTransitiveBuilder) {
117 return StepTransitive.fromBuilder(
118 (StepTransitiveBuilder) builder, sourceAsset, assets, variableTargets);
119 }
120 if (builder instanceof StepSubTypeBuilder) {
121 return StepSubType.fromBuilder(
122 (StepSubTypeBuilder) builder, sourceAsset, assets, variableTargets);
123 }
124 if (builder instanceof StepFieldBuilder) {
125 return StepField.fromBuilder(
126 (StepFieldBuilder) builder, sourceAsset, assets, variableTargets);
127 }
128 if (builder instanceof StepAttackStepBuilder) {
129 return StepAttackStep.fromBuilder(
130 (StepAttackStepBuilder) builder, sourceAsset, assets, variableTargets);
131 }
132 if (builder instanceof StepVariableBuilder) {
133 return StepVariable.fromBuilder(
134 (StepVariableBuilder) builder, sourceAsset, assets, variableTargets);
135 }
136 throw new RuntimeException(
137 String.format(
138 "Invalid %s sub-type %s",
139 StepExpressionBuilder.class.getName(), builder.getClass().getName()));
140 }
141 }