Skip to content
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

[MPS-28688] add findOutputNodeByComparableInputNodeAndMappingName #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SNode> comparable, @Nullable String mappingName){
return myMappings.findOutputNodeByComparableInputNodeAndMappingName(comparable, mappingName);
}

@Override
public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, String mappingName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public void registerMappingLabel(SNode inputNode, String mappingName, SNode outp
throw new UnsupportedOperationException();
}

@Nullable
@Override
public SNode findOutputNodeByComparableInputNodeAndMappingName(@NotNull Comparable<SNode> comparable, @Nullable String mappingName) {
return null;
}

@Nullable
@Override
public SNode findOutputNodeByInputNodeAndMappingName(SNode inputNode, @Nullable String mappingName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SNode> comparable, @Nullable String mappingName){
Map<SNode, Object> currentMapping = myMappingNameAndInputNodeToOutputNodeMap.get(mappingName);
if (currentMapping == null) {
return null;
}
Optional<SNode> foundInput = currentMapping.keySet().parallelStream().filter(labeledOne -> comparable.compareTo(labeledOne)==0).findFirst();
if(foundInput.isPresent()){
return (SNode) currentMapping.get(foundInput.get());
}
return null;
}

public List<SNode> findAllOutputNodesByInputNodeAndMappingName(SNode inputNode, String mappingName) {
if (mappingName == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SNode> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public SNode getOutputNodeByMappingLabel(String label, @Nullable SModel inputMod
return myGenerator.findOutputNode(inputModel, label);
}

@Nullable
public SNode getOutputNodeByComparableInputNodeAndMappingLabel(@NotNull Comparable<SNode> inputNode, @NotNull String label) {
return myGenerator.findOutputNodeByComparableInputNodeAndMappingName(inputNode, label);
}
public SNode getOutputNodeByInputNodeAndMappingLabel(SNode inputNode, String label) {
if (inputNode == null) return null;
if (!myGenerator.areMappingsAvailable()) {
Expand Down