diff --git a/core/generator/source/jetbrains/mps/generator/impl/AbstractTemplateGenerator.java b/core/generator/source/jetbrains/mps/generator/impl/AbstractTemplateGenerator.java index cb125e32af3f..9a744efc909b 100644 --- a/core/generator/source/jetbrains/mps/generator/impl/AbstractTemplateGenerator.java +++ b/core/generator/source/jetbrains/mps/generator/impl/AbstractTemplateGenerator.java @@ -24,6 +24,7 @@ import jetbrains.mps.generator.runtime.TemplateContext; import jetbrains.mps.generator.template.ITemplateGenerator; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.mps.openapi.language.SContainmentLink; import org.jetbrains.mps.openapi.language.SReferenceLink; import org.jetbrains.mps.openapi.model.SModel; @@ -103,6 +104,20 @@ public void registerMappingLabel(SNode inputNode, String mappingName, SNode outp public SNode findOutputNodeByTemplateNodeUnique(String templateNode) { return myMappings.findOutputNodeByTemplateNodeUnique(templateNode); } + /** + * For the mapping label the comparable decides if the key is in the keySet. + * If so, the key will be used to return the value. + * This is specially useful if the comparable might be equivalent to a search key, + * which is produced on demand + * @param comparable + * @param mappingName + * @return output if the key could be found with the help of the comparator + */ + @Nullable + @Override + public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable comparable, @Nullable String mappingName){ + return myMappings.findOutputNodeByComparableInputNodeAndMappingName(comparable, mappingName); + } @Override public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, String mappingName) { diff --git a/core/generator/source/jetbrains/mps/generator/impl/BogusTemplateGenerator.java b/core/generator/source/jetbrains/mps/generator/impl/BogusTemplateGenerator.java index 7b6b07f20d3f..3d6111e7cb7c 100644 --- a/core/generator/source/jetbrains/mps/generator/impl/BogusTemplateGenerator.java +++ b/core/generator/source/jetbrains/mps/generator/impl/BogusTemplateGenerator.java @@ -73,6 +73,12 @@ public void registerMappingLabel(SNode inputNode, String mappingName, SNode outp throw new UnsupportedOperationException(); } + @Nullable + @Override + public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable comparable, @Nullable String mappingName) { + return null; + } + @Nullable @Override public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, @Nullable String mappingName) { diff --git a/core/generator/source/jetbrains/mps/generator/impl/GeneratorMappings.java b/core/generator/source/jetbrains/mps/generator/impl/GeneratorMappings.java index d4d8cb151363..202707504767 100644 --- a/core/generator/source/jetbrains/mps/generator/impl/GeneratorMappings.java +++ b/core/generator/source/jetbrains/mps/generator/impl/GeneratorMappings.java @@ -35,6 +35,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -192,6 +193,27 @@ public SNode findOutputNodeByInputNodeAndMappingName(@Nullable SNode inputNode, return (SNode) o; } + /** + * For the mapping label the comparable decides if the key is in the keySet. + * If so, the key will be used to return the value. + * This is specially useful if the comparable might be equivalent to a search key, + * which is produced on demand + * @param comparable + * @param mappingName + * @return output if the key could be found with the help of the comparator + */ + public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable comparable, @Nullable String mappingName){ + Map currentMapping = myMappingNameAndInputNodeToOutputNodeMap.get(mappingName); + if (currentMapping == null) { + return null; + } + Optional foundInput = currentMapping.keySet().parallelStream().filter(labeledOne -> comparable.compareTo(labeledOne)==0).findFirst(); + if(foundInput.isPresent()){ + return (SNode) currentMapping.get(foundInput.get()); + } + return null; + } + public List findAllOutputNodesByInputNodeAndMappingName(SNode inputNode, String mappingName) { if (mappingName == null) { return null; diff --git a/core/generator/source/jetbrains/mps/generator/template/ITemplateGenerator.java b/core/generator/source/jetbrains/mps/generator/template/ITemplateGenerator.java index 921942078dd7..8e0ad56224d3 100644 --- a/core/generator/source/jetbrains/mps/generator/template/ITemplateGenerator.java +++ b/core/generator/source/jetbrains/mps/generator/template/ITemplateGenerator.java @@ -19,6 +19,7 @@ import jetbrains.mps.generator.IGeneratorLogger; import jetbrains.mps.generator.impl.query.GeneratorQueryProvider; import jetbrains.mps.util.annotation.ToRemove; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.mps.openapi.model.SModel; import org.jetbrains.mps.openapi.model.SNode; @@ -42,6 +43,18 @@ public interface ITemplateGenerator extends GeneratorQueryProvider.Source { void registerMappingLabel(SNode inputNode, String mappingName, SNode outputNode); + /** + * For the mapping label the comparable decides if the key is in the keySet. + * If so, the key will be used to return the value. + * This is specially useful if the comparable might be equivalent to a search key, + * which is produced on demand + * @param comparable + * @param mappingName + * @return output if the key could be found with the help of the comparator + */ + @Nullable + SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable comparable, @Nullable String mappingName); + /** * @param inputNode node from almost any model that may have served as an input for a generator. We tolerate null value now, indicating * we are looking for conditional root (takes no input to create one) diff --git a/core/generator/source/jetbrains/mps/generator/template/TemplateQueryContext.java b/core/generator/source/jetbrains/mps/generator/template/TemplateQueryContext.java index 644066361b8e..e0fdd8b0d12b 100644 --- a/core/generator/source/jetbrains/mps/generator/template/TemplateQueryContext.java +++ b/core/generator/source/jetbrains/mps/generator/template/TemplateQueryContext.java @@ -126,6 +126,10 @@ public SNode getOutputNodeByMappingLabel(String label, @Nullable SModel inputMod return myGenerator.findOutputNode(inputModel, label); } + @Nullable + public SNode getOutputNodeByComparableInputNodeAndMappingLabel(@NotNull Comparable inputNode, @NotNull String label) { + return myGenerator.findOutputNodeByComparableInputNodeAndMappingName(inputNode, label); + } public SNode getOutputNodeByInputNodeAndMappingLabel(SNode inputNode, String label) { if (inputNode == null) return null; if (!myGenerator.areMappingsAvailable()) {