public static GenerationDependencies fromXml(Element root) { String version = GenerationRootDependencies.getValue(root, ATTR_VERSION); if (version == null || !version.equals(Integer.toString(DEPENDENCIES_VERSION))) { /* regenerate all */ return null; } Map<String, String> externalHashes = new HashMap<String, String>(); for (Element e : ((List<Element>) root.getChildren(NODE_MODEL))) { String modelReference = GenerationRootDependencies.getValue(e, ATTR_MODEL_ID); String rootHash = GenerationRootDependencies.getValue(e, ATTR_HASH); if (modelReference != null) { externalHashes.put(modelReference, rootHash); } } List<GenerationRootDependencies> data = new ArrayList<GenerationRootDependencies>(); for (Element e : ((List<Element>) root.getChildren(NODE_COMMON))) { data.add(GenerationRootDependencies.fromXml(e, true)); } for (Element e : ((List<Element>) root.getChildren(NODE_ROOT))) { data.add(GenerationRootDependencies.fromXml(e, false)); } String modelHash = GenerationRootDependencies.getValue(root, ATTR_MODEL_HASH); String paramsHash = GenerationRootDependencies.getValue(root, ATTR_PARAMS_HASH); if (externalHashes.isEmpty() && data.isEmpty()) { return new GenerationDependencies(modelHash, paramsHash); } return new GenerationDependencies( data, modelHash, paramsHash, externalHashes, Collections.<GenerationRootDependencies>emptyList(), 0, 0); }
public static GenerationDependencies fromIncremental( Map<SNode, SNode> currentToOriginalMap, RootDependenciesBuilder[] roots, String modelHash, String parametersHash, IOperationContext operationContext, IncrementalGenerationStrategy incrementalStrategy, int skippedCount, int fromCacheCount) { Map<String, List<String>> generatedFiles = getGeneratedFiles(currentToOriginalMap); List<GenerationRootDependencies> unchanged = null; List<GenerationRootDependencies> rootDependencies = new ArrayList<GenerationRootDependencies>(roots.length); Map<String, String> externalHashes = new HashMap<String, String>(); for (RootDependenciesBuilder l : roots) { SNode originalRoot = l.getOriginalRoot(); List<String> files = generatedFiles.get(originalRoot != null ? originalRoot.getId() : ""); if (files == null) { files = Collections.emptyList(); } GenerationRootDependencies dep; if (l.isUnchanged()) { dep = l.getSavedDependencies(); if (unchanged == null) { unchanged = new ArrayList<GenerationRootDependencies>(); } unchanged.add(dep); } else { dep = GenerationRootDependencies.fromData(l, files); } rootDependencies.add(dep); for (String modelReference : dep.getExternal()) { if (!externalHashes.containsKey(modelReference)) { SModelDescriptor sm = SModelRepository.getInstance() .getModelDescriptor(SModelReference.fromString(modelReference)); Map<String, String> hashes = incrementalStrategy.getModelHashes(sm, operationContext); String value = hashes != null ? hashes.get(ModelDigestHelper.FILE) : null; externalHashes.put(modelReference, value); } } } return new GenerationDependencies( rootDependencies, modelHash, parametersHash, externalHashes, unchanged == null ? Collections.<GenerationRootDependencies>emptyList() : unchanged, skippedCount, fromCacheCount); }
@Override public List<String> getWarnings() { List<String> warnings = new ArrayList<String>(super.getWarnings()); Set<String> usedLanguages = new HashSet<String>(); for (SModel model : myModule.getOwnTemplateModels()) { if (SModelStereotype.isGeneratorModel(model)) { usedLanguages.addAll(ModelContentUtil.getUsedLanguageNamespacesInTemplateModel(model)); } } Set<String> extendedLanguages = new HashSet<String>(); Language sourceLanguage = myModule.getSourceLanguage(); usedLanguages.remove(sourceLanguage.getModuleName()); Set<Language> ext = new LinkedHashSet<Language>(); sourceLanguage.getDependenciesManager().collectAllExtendedLanguages(ext); for (Language language : ext) { extendedLanguages.add(language.getModuleName()); } for (String lang : usedLanguages) { Language language = ModuleRepositoryFacade.getInstance().getModule(lang, Language.class); if (language == null) continue; if (!extendedLanguages.contains(lang) && !language.getRuntimeModulesReferences().isEmpty()) { warnings.add(sourceLanguage + " should extend " + lang); } } return warnings; }
@Override public List<String> getErrors() { List<String> errors = new ArrayList<String>(super.getErrors()); for (SModuleReference gen : myModule.getModuleDescriptor().getDepGenerators()) { if (ModuleRepositoryFacade.getInstance().getModule(gen) == null) { errors.add("Can't find generator dependency: " + gen.getModuleName()); } } return errors; }
private static <C> void assertListsEqual( List<C> expectedList, List<C> actualList, Comparator<C> comparator, String name) { List<C> notFoundExpected = new ArrayList<C>(); List<C> notFoundActual = new ArrayList<C>(); for (C expected : expectedList) { boolean found = false; for (C actual : actualList) { if (comparator.compare(actual, expected) == 0) { found = true; break; } } if (!found) { notFoundExpected.add(expected); } } for (C actual : actualList) { boolean found = false; for (C expected : expectedList) { if (comparator.compare(actual, expected) == 0) { found = true; break; } } if (!found) { notFoundActual.add(actual); } } if (!notFoundExpected.isEmpty()) { fail("Not found expected " + name + " " + Arrays.toString(notFoundExpected.toArray())); } if (!notFoundActual.isEmpty()) { fail("Not expected " + name + " " + Arrays.toString(notFoundActual.toArray())); } }
@Override public Iterable<ModelRoot> getModelRoots(Module module) { List<ModelRoot> singleton = new ArrayList<ModelRoot>(1); // singleton.add( new PsiJavaStubModelRoot(module) ); for (VirtualFile sourceRoot : ModuleRootManager.getInstance(module).getSourceRoots(false)) { String path = sourceRoot.getPath(); EclipseJavaStubModelRoot modelRoot = new EclipseJavaStubModelRoot(); modelRoot.setPath(path); // not singleton singleton.add(modelRoot); } return singleton; }
private static Map<String, List<String>> getGeneratedFiles( Map<SNode, SNode> currentToOriginalMap) { Map<String, List<String>> generatedFiles = new HashMap<String, List<String>>(); for (Map.Entry<SNode, SNode> entry : currentToOriginalMap.entrySet()) { String inputRootId = entry.getValue() != null ? entry.getValue().getId() : ""; SNode outputRoot = entry.getKey(); List<String> filesList = generatedFiles.get(inputRootId); if (filesList == null) { filesList = new ArrayList<String>(); generatedFiles.put(inputRootId, filesList); } String fileName = getFileName(outputRoot); if (fileName != null) { filesList.add(fileName); } } return generatedFiles; }
private static List<INodeSubstituteAction> createActions_internal( SNode parentNode, SNode currentChild, SNode childConcept, IChildNodeSetter childSetter, IOperationContext context) { List<INodeSubstituteAction> resultActions = new ArrayList<INodeSubstituteAction>(); if (childConcept == null) { return resultActions; } // special case if (childConcept == SModelUtil.getBaseConcept()) { if ((currentChild == null || currentChild.getConcept().getId().equals(SNodeUtil.concept_BaseConcept))) { resultActions = new ArrayList<INodeSubstituteAction>(); ISearchScope conceptsSearchScope = SModelSearchUtil.createConceptsFromModelLanguagesScope( parentNode.getModel(), true, context.getScope()); List<SNode> allVisibleConcepts = conceptsSearchScope.getNodes(); for (final SNode visibleConcept : allVisibleConcepts) { resultActions.add( new DefaultChildNodeSubstituteAction( visibleConcept, parentNode, currentChild, childSetter, context.getScope()) { public String getMatchingText(String pattern) { return getMatchingText(pattern, true, true); } public String getVisibleMatchingText(String pattern) { return getMatchingText(pattern); } public String getDescriptionText(String pattern) { String fqName = NameUtil.nodeFQName(visibleConcept); return "lang: " + NameUtil.compactNamespace(NameUtil.namespaceFromConceptFQName(fqName)); } public Icon getIconFor(String pattern) { return getIconFor(pattern, true); } }); } return resultActions; } // pretend we are going to substitute more concrete concept childConcept = ChildSubstituteActionsUtil.getRefinedChildConcept(currentChild); } Language primaryLanguage = SModelUtil.getDeclaringLanguage(childConcept); if (primaryLanguage == null) { LOG.error( "Couldn't build actions : couldn't get declaring language for concept " + org.jetbrains.mps.openapi.model.SNodeUtil.getDebugText(childConcept)); return resultActions; } List<SNode> allBuilders = ChildSubstituteActionsUtil.getActionsBuilders( parentNode, currentChild, childConcept, childSetter, context); if (!ChildSubstituteActionsUtil.containsRemoveDefaults(allBuilders)) { resultActions.addAll( createPrimaryChildSubstituteActions( parentNode, currentChild, childConcept, childSetter, context)); } for (SNode builder : allBuilders) { List<INodeSubstituteAction> addActions = ChildSubstituteActionsUtil.invokeActionFactory( builder, parentNode, currentChild, childConcept, childSetter, context); resultActions.addAll(addActions); } for (SNode builder : allBuilders) { resultActions = ChildSubstituteActionsUtil.applyActionFilter( builder, resultActions, parentNode, currentChild, childConcept, context); } if (childSetter instanceof DefaultChildNodeSetter || childSetter instanceof AbstractCellMenuPart_ReplaceNode_CustomNodeConcept && currentChild != null) { SNode linkDeclaration; if (childSetter instanceof DefaultChildNodeSetter) { linkDeclaration = ((DefaultChildNodeSetter) childSetter).myLinkDeclaration; } else { linkDeclaration = currentChild.getRoleLink(); } Iterator<INodeSubstituteAction> it = resultActions.iterator(); while (it.hasNext()) { INodeSubstituteAction action = it.next(); SNode conceptNode = action.getOutputConcept(); if (conceptNode == null) { continue; } if (!ModelConstraintsManager.canBeParent(parentNode, conceptNode, linkDeclaration, context) || !ModelConstraintsManager.canBeAncestor(parentNode, conceptNode, context)) { it.remove(); } } } return resultActions; }
private static List<INodeSubstituteAction> createSmartReferenceActions( final SNode smartConcept, final SNode smartReference, final SNode parentNode, final SNode currentChild, final IChildNodeSetter childSetter, final IOperationContext context) { if (parentNode == null) { return null; } // try to create referent-search-scope SNode linkDeclaration = null; int index = 0; if (currentChild != null) { linkDeclaration = currentChild.getRoleLink(); index = parentNode.getChildren(currentChild.getRoleInParent()).indexOf(currentChild); } // TODO generate wrapping setter to have access to original link // if(childSetter instanceof WrappingSetter) { // childSetter = ((WrappingSetter)childSetter).unwrap(); // } if (linkDeclaration == null && childSetter instanceof DefaultChildNodeSetter) { linkDeclaration = ((DefaultChildNodeSetter) childSetter).getLinkDeclaration(); } // TODO restore (when wrapping setter is created) // if (linkDeclaration == null) { // return null; // } ReferenceDescriptor refDescriptor = ModelConstraintsUtil.getSmartReferenceDescriptor( parentNode, linkDeclaration == null ? null : SModelUtil.getLinkDeclarationRole(linkDeclaration), index, smartConcept); if (refDescriptor == null) return null; Scope searchScope = refDescriptor.getScope(); if (searchScope == null) return null; // create smart actions final String targetConcept = NameUtil.nodeFQName(SModelUtil.getLinkDeclarationTarget(smartReference)); List<INodeSubstituteAction> actions = new ArrayList<INodeSubstituteAction>(); IReferencePresentation presentation = refDescriptor.getReferencePresentation(); Iterable<SNode> referentNodes = searchScope.getAvailableElements(null); for (SNode referentNode : referentNodes) { if (referentNode == null || !referentNode .getConcept() .isSubConceptOf(SConceptRepository.getInstance().getConcept(targetConcept))) continue; actions.add( new SmartRefChildNodeSubstituteAction( referentNode, parentNode, currentChild, childSetter, context.getScope(), smartConcept, smartReference, presentation)); } return actions; }