package jetbrains.mps.lang.smodel.generator.smodelAdapter;
/*Generated by MPS */
import java.util.List;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import java.util.Collections;
import java.util.ArrayList;
import org.jetbrains.mps.util.Condition;
import org.jetbrains.mps.util.InstanceOfCondition;
import jetbrains.mps.util.ConditionalIterable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SNodeUtil;
import jetbrains.mps.util.IterableUtil;
import org.jetbrains.mps.openapi.model.SNodeId;
import jetbrains.mps.smodel.SModelUtil_new;
import jetbrains.mps.smodel.behaviour.BHReflection;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.project.structure.ProjectStructureModule;
import jetbrains.mps.smodel.Generator;
import jetbrains.mps.smodel.Language;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
public final class SModelOperations {
public SModelOperations() {
}
public static List<SNode> roots(SModel model, final SAbstractConcept concept) {
if (model == null) {
return Collections.emptyList();
}
List<SNode> list = new ArrayList<SNode>();
Iterable<SNode> iterable = model.getRootNodes();
if (concept != null) {
Condition<SNode> cond = new InstanceOfCondition(concept).tolerateNulls();
iterable = new ConditionalIterable<SNode>(iterable, cond);
}
for (SNode node : iterable) {
list.add(node);
}
return list;
}
@NotNull
public static List<SNode> rootsIncludingImported(@Nullable SModel model, @Nullable SAbstractConcept concept) {
if (model == null) {
return Collections.emptyList();
}
return allNodesIncludingImported(model, true, concept);
}
@NotNull
public static List<SNode> nodesIncludingImported(@Nullable SModel model, @Nullable SAbstractConcept concept) {
if (model == null) {
return Collections.emptyList();
}
return allNodesIncludingImported(model, false, concept);
}
private static List<SNode> allNodesIncludingImported(SModel sModel, boolean roots, @Nullable SAbstractConcept concept) {
List<SModel> modelsList = new ArrayList<SModel>();
modelsList.add(sModel);
List<SModel> modelDescriptors = jetbrains.mps.smodel.SModelOperations.allImportedModels(sModel);
for (SModel descriptor : modelDescriptors) {
modelsList.add(descriptor);
}
List<SNode> resultNodes = new ArrayList<SNode>();
final Condition<SNode> instanceCondition = (concept == null ? null : new InstanceOfCondition(concept));
for (SModel aModel : modelsList) {
if (concept == null || roots) {
ConditionalIterable<SNode> nodes = new ConditionalIterable<SNode>((roots ? aModel.getRootNodes() : SNodeUtil.getDescendants(aModel)), instanceCondition);
resultNodes.addAll(IterableUtil.asList(nodes));
} else {
resultNodes.addAll(jetbrains.mps.smodel.SModelOperations.getNodes(aModel, concept));
}
}
return resultNodes;
}
public static List<SNode> nodes(SModel model, final SAbstractConcept concept) {
if (model == null) {
return new ArrayList<SNode>();
}
if (concept != null) {
return jetbrains.mps.smodel.SModelOperations.getNodes(model, concept);
}
List<SNode> result = new ArrayList<SNode>();
for (SNode node : SNodeUtil.getDescendants(model)) {
result.add(node);
}
return result;
}
public static SNode createNewNode(SModel model, SNodeId id, SAbstractConcept concept) {
if (concept == null) {
return null;
}
SNode result = SModelUtil_new.instantiateConceptDeclaration(concept, model, id, false);
if (result == null) {
return null;
}
BHReflection.initNode(result);
return result;
}
public static SNode createNewRootNode(SModel model, SConcept concept) {
SNode newNode = createNewNode(model, null, concept);
model.addRootNode(newNode);
return newNode;
}
public static SNode addRootNode(SModel model, SNode node) {
if (model != null && node != null) {
model.addRootNode(node);
}
return node;
}
public static String getModelName(SModel model) {
if (model == null) {
return null;
}
return model.getName().getLongName();
}
public static SNode getModuleStub(SModel model) {
final SModule module = model.getModule();
SRepository repo = model.getRepository();
if (repo == null || ProjectStructureModule.getInstance(repo) == null) {
return null;
}
if (module instanceof Generator) {
Language lang = ((Generator) module).getSourceLanguage();
SModel m = ProjectStructureModule.getInstance(repo).getModelByModule(lang);
if (m == null) {
return null;
}
SNode l = ListSequence.fromList(SModelOperations.roots(m, MetaAdapterFactory.getConcept(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe1fL, "jetbrains.mps.lang.project.structure.Language"))).first();
return (l == null ? null : ListSequence.fromList(SLinkOperations.getChildren(l, MetaAdapterFactory.getContainmentLink(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe1fL, 0x5869770da61dfe37L, "generator"))).findFirst(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return eq_kkj9n5_a0a0a0a0a0a4a3a01(SPropertyOperations.getString(it, MetaAdapterFactory.getProperty(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe1eL, 0x5869770da61dfe22L, "uuid")), module.getModuleReference().getModuleId().toString());
}
}));
} else {
SModel m = ProjectStructureModule.getInstance(repo).getModelByModule(module);
return (m == null ? null : ListSequence.fromList(SModelOperations.roots(m, MetaAdapterFactory.getConcept(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe1eL, "jetbrains.mps.lang.project.structure.Module"))).first());
}
}
private static boolean eq_kkj9n5_a0a0a0a0a0a4a3a01(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}