Skip to content

Commit c2d39f3

Browse files
Kemal SoysalKemalSoysal
authored andcommitted
[MPS-28688] add findOutputNodeByComparableInputNodeAndMappingName
1 parent 92ad0de commit c2d39f3

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

core/generator/source/jetbrains/mps/generator/impl/AbstractTemplateGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import jetbrains.mps.generator.runtime.TemplateContext;
2525
import jetbrains.mps.generator.template.ITemplateGenerator;
2626
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
2728
import org.jetbrains.mps.openapi.language.SContainmentLink;
2829
import org.jetbrains.mps.openapi.language.SReferenceLink;
2930
import org.jetbrains.mps.openapi.model.SModel;
@@ -103,6 +104,20 @@ public void registerMappingLabel(SNode inputNode, String mappingName, SNode outp
103104
public SNode findOutputNodeByTemplateNodeUnique(String templateNode) {
104105
return myMappings.findOutputNodeByTemplateNodeUnique(templateNode);
105106
}
107+
/**
108+
* For the mapping label the comparable decides if the key is in the keySet.
109+
* If so, the key will be used to return the value.
110+
* This is specially useful if the comparable might be equivalent to a search key,
111+
* which is produced on demand
112+
* @param comparable
113+
* @param mappingName
114+
* @return output if the key could be found with the help of the comparator
115+
*/
116+
@Nullable
117+
@Override
118+
public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable<SNode> comparable, @Nullable String mappingName){
119+
return myMappings.findOutputNodeByComparableInputNodeAndMappingName(comparable, mappingName);
120+
}
106121

107122
@Override
108123
public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, String mappingName) {

core/generator/source/jetbrains/mps/generator/impl/BogusTemplateGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public void registerMappingLabel(SNode inputNode, String mappingName, SNode outp
7373
throw new UnsupportedOperationException();
7474
}
7575

76+
@Nullable
77+
@Override
78+
public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable<SNode> comparable, @Nullable String mappingName) {
79+
return null;
80+
}
81+
7682
@Nullable
7783
@Override
7884
public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, @Nullable String mappingName) {

core/generator/source/jetbrains/mps/generator/impl/GeneratorMappings.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636
import java.util.Map;
3737
import java.util.Map.Entry;
38+
import java.util.Optional;
3839
import java.util.Set;
3940
import java.util.concurrent.ConcurrentHashMap;
4041
import java.util.concurrent.ConcurrentMap;
@@ -192,6 +193,27 @@ public SNode findOutputNodeByInputNodeAndMappingName(@Nullable SNode inputNode,
192193
return (SNode) o;
193194
}
194195

196+
/**
197+
* For the mapping label the comparable decides if the key is in the keySet.
198+
* If so, the key will be used to return the value.
199+
* This is specially useful if the comparable might be equivalent to a search key,
200+
* which is produced on demand
201+
* @param comparable
202+
* @param mappingName
203+
* @return output if the key could be found with the help of the comparator
204+
*/
205+
public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable<SNode> comparable, @Nullable String mappingName){
206+
Map<SNode, Object> currentMapping = myMappingNameAndInputNodeToOutputNodeMap.get(mappingName);
207+
if (currentMapping == null) {
208+
return null;
209+
}
210+
Optional<SNode> foundInput = currentMapping.keySet().parallelStream().filter(labeledOne -> comparable.compareTo(labeledOne)==0).findFirst();
211+
if(foundInput.isPresent()){
212+
return (SNode) currentMapping.get(foundInput.get());
213+
}
214+
return null;
215+
}
216+
195217
public List<SNode> findAllOutputNodesByInputNodeAndMappingName(SNode inputNode, String mappingName) {
196218
if (mappingName == null) {
197219
return null;

core/generator/source/jetbrains/mps/generator/template/ITemplateGenerator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import jetbrains.mps.generator.IGeneratorLogger;
2020
import jetbrains.mps.generator.impl.query.GeneratorQueryProvider;
2121
import jetbrains.mps.util.annotation.ToRemove;
22+
import org.jetbrains.annotations.NotNull;
2223
import org.jetbrains.annotations.Nullable;
2324
import org.jetbrains.mps.openapi.model.SModel;
2425
import org.jetbrains.mps.openapi.model.SNode;
@@ -42,6 +43,18 @@ public interface ITemplateGenerator extends GeneratorQueryProvider.Source {
4243

4344
void registerMappingLabel(SNode inputNode, String mappingName, SNode outputNode);
4445

46+
/**
47+
* For the mapping label the comparable decides if the key is in the keySet.
48+
* If so, the key will be used to return the value.
49+
* This is specially useful if the comparable might be equivalent to a search key,
50+
* which is produced on demand
51+
* @param comparable
52+
* @param mappingName
53+
* @return output if the key could be found with the help of the comparator
54+
*/
55+
@Nullable
56+
SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable<SNode> comparable, @Nullable String mappingName);
57+
4558
/**
4659
* @param inputNode node from almost any model that may have served as an input for a generator. We tolerate null value now, indicating
4760
* we are looking for conditional root (takes no input to create one)

core/generator/source/jetbrains/mps/generator/template/TemplateQueryContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public SNode getOutputNodeByMappingLabel(String label, @Nullable SModel inputMod
126126
return myGenerator.findOutputNode(inputModel, label);
127127
}
128128

129+
@Nullable
130+
public SNode getOutputNodeByComparableInputNodeAndMappingLabel(@NotNull Comparable<SNode> inputNode, @NotNull String label) {
131+
return myGenerator.findOutputNodeByComparableInputNodeAndMappingName(inputNode, label);
132+
}
129133
public SNode getOutputNodeByInputNodeAndMappingLabel(SNode inputNode, String label) {
130134
if (inputNode == null) return null;
131135
if (!myGenerator.areMappingsAvailable()) {

0 commit comments

Comments
 (0)