Search in sources :

Example 1 with GenModel

use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-xtend by eclipse.

the class URIsInEcoreFilesXtendTest method doTestResource.

protected void doTestResource(String platformPath, String... packageNames) {
    Resource resource = resourceSet.getResource(URI.createPlatformPluginURI(platformPath, false), true);
    assertNotNull(resource);
    assertEquals(1, resource.getContents().size());
    EObject obj = resource.getContents().get(0);
    if (obj instanceof EPackage) {
        assertEquals(packageNames[0], ((EPackage) obj).getName());
    } else if (obj instanceof GenModel) {
        GenModel model = (GenModel) obj;
        List<GenPackage> packages = Lists.newArrayList(model.getGenPackages());
        assertEquals(packageNames.length, packages.size());
        ListExtensions.sortInplaceBy(packages, new Functions.Function1<GenPackage, String>() {

            @Override
            public String apply(GenPackage p) {
                return p.getEcorePackage().getName();
            }
        });
        List<String> packageNamesList = Arrays.asList(packageNames);
        Collections.sort(packageNamesList);
        for (int i = 0; i < packageNamesList.size(); i++) {
            assertEquals(packageNamesList.get(i), packages.get(i).getEcorePackage().getName());
        }
        IStatus status = model.validate();
        assertTrue(printLeafs(status), status.isOK());
        EObject orig = EcoreUtil.copy(obj);
        ((GenModel) obj).reconcile();
        if (!EcoreUtil.equals(orig, obj)) {
            Predicate<EStructuralFeature> ignoreContainer = new Predicate<EStructuralFeature>() {

                @Override
                public boolean apply(EStructuralFeature f) {
                    if (f instanceof EReference) {
                        EReference casted = (EReference) f;
                        return !casted.isContainment();
                    }
                    return false;
                }
            };
            String origAsString = EmfFormatter.objToStr(orig, ignoreContainer);
            String newAsString = EmfFormatter.objToStr(obj, ignoreContainer);
            throw new ComparisonFailure("Reconcile changed model", origAsString, newAsString);
        }
    } else {
        fail("Unexpected root element type: " + obj.eClass().getName());
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) EPackage(org.eclipse.emf.ecore.EPackage) Predicate(com.google.common.base.Predicate) ComparisonFailure(org.junit.ComparisonFailure) EObject(org.eclipse.emf.ecore.EObject) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 2 with GenModel

use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.

the class GenModelUtil2 method getGenModelResource.

public static Resource getGenModelResource(final String locationInfo, final String nsURI, final ResourceSet resourceSet) {
    final URI genModelURI = EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).get(nsURI);
    if ((genModelURI == null)) {
        boolean _equals = Objects.equal(EcorePackage.eNS_URI, nsURI);
        if (_equals) {
            return null;
        }
        EList<Resource> _resources = resourceSet.getResources();
        for (final Resource res : _resources) {
            EList<EObject> _contents = res.getContents();
            for (final EObject obj : _contents) {
                if ((obj instanceof GenModel)) {
                    EList<GenPackage> _genPackages = ((GenModel) obj).getGenPackages();
                    for (final GenPackage genPackage : _genPackages) {
                        String _nSURI = genPackage.getNSURI();
                        boolean _equals_1 = Objects.equal(_nSURI, nsURI);
                        if (_equals_1) {
                            return genPackage.eResource();
                        }
                    }
                }
            }
        }
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("Could not find a GenModel for EPackage \'");
        _builder.append(nsURI);
        _builder.append("\'");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(locationInfo);
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append(" from ");
                _builder.append(locationInfo);
            }
        }
        _builder.append(".");
        _builder.newLineIfNotEmpty();
        _builder.append("If the missing GenModel has been generated via ");
        String _simpleName = EMFGeneratorFragment2.class.getSimpleName();
        _builder.append(_simpleName);
        _builder.append(", make sure to run it first in the workflow.");
        _builder.newLineIfNotEmpty();
        _builder.append("If you have a *.genmodel-file, make sure to register it via StandaloneSetup.registerGenModelFile(String).");
        _builder.newLine();
        throw new RuntimeException(_builder.toString());
    }
    if ((resourceSet == null)) {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("There is no ResourceSet for EPackage \'");
        _builder_1.append(nsURI);
        _builder_1.append("\'. Please make sure the EPackage has been loaded from a .ecore file and not from the generated Java file.");
        throw new RuntimeException(_builder_1.toString());
    }
    final Resource genModelResource = resourceSet.getResource(genModelURI, true);
    if ((genModelResource == null)) {
        StringConcatenation _builder_2 = new StringConcatenation();
        _builder_2.append("Error loading GenModel ");
        _builder_2.append(genModelURI);
        throw new RuntimeException(_builder_2.toString());
    }
    EList<EObject> _contents_1 = genModelResource.getContents();
    for (final EObject content : _contents_1) {
        if ((content instanceof GenModel)) {
            ((GenModel) content).reconcile();
        }
    }
    return genModelResource;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) Resource(org.eclipse.emf.ecore.resource.Resource) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) URI(org.eclipse.emf.common.util.URI)

Example 3 with GenModel

use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.

the class EMFGeneratorFragment2 method getSaveAndReconcileGenModel.

protected GenModel getSaveAndReconcileGenModel(final Grammar grammar, final List<EPackage> packs, final ResourceSet rs) {
    final GenModel genModel = this.getGenModel(rs, grammar);
    genModel.initialize(packs);
    EList<GenPackage> _genPackages = genModel.getGenPackages();
    for (final GenPackage genPackage : _genPackages) {
        {
            genPackage.setBasePackage(this.getBasePackage(grammar));
            if (this.suppressLoadInitialization) {
                genPackage.setLoadInitialization(false);
            }
            if (((!this.getLanguage().getFileExtensions().isEmpty()) && packs.contains(genPackage.getEcorePackage()))) {
                genPackage.setFileExtensions(IterableExtensions.join(this.getLanguage().getFileExtensions(), ","));
            }
        }
    }
    final Set<EPackage> referencedEPackages = this.getReferencedEPackages(packs);
    final List<GenPackage> usedGenPackages = this.getGenPackagesForPackages(genModel, referencedEPackages);
    this.reconcileMissingGenPackagesInUsedModels(usedGenPackages);
    genModel.getUsedGenPackages().addAll(usedGenPackages);
    this.saveResource(genModel.eResource());
    new GenModelHelper().registerGenModel(genModel);
    return genModel;
}
Also used : GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) GenModelHelper(org.eclipse.emf.mwe.utils.GenModelHelper) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) EPackage(org.eclipse.emf.ecore.EPackage)

Example 4 with GenModel

use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.

the class EMFGeneratorFragment2 method getGenModel.

protected GenModel getGenModel(final ResourceSet rs, final Grammar grammar) {
    try {
        final URI genModelUri = this.getGenModelUri(grammar);
        final Resource resource = rs.getResource(genModelUri, false);
        if ((resource != null)) {
            resource.unload();
            rs.getResources().remove(resource);
        }
        final Resource genModelFile = rs.createResource(genModelUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
        GenModel genModel = null;
        boolean _exists = rs.getURIConverter().exists(genModelUri, null);
        if (_exists) {
            genModelFile.load(null);
            boolean _hasFragment = genModelUri.hasFragment();
            if (_hasFragment) {
                EObject _eObject = genModelFile.getEObject(genModelUri.fragment());
                genModel = ((GenModel) _eObject);
            } else {
                EObject _head = IterableExtensions.<EObject>head(genModelFile.getContents());
                genModel = ((GenModel) _head);
            }
        } else {
            genModel = new GenModelImpl() {

                @Override
                public GenPackage createGenPackage() {
                    return new GenPackageImpl() {

                        @Override
                        public String getSerializedPackageFilename() {
                            String _name = this.getName();
                            return (_name + ".loadinitialization_ecore");
                        }
                    };
                }
            };
            genModel.setModelName(this.getModelName(grammar));
            genModel.setModelPluginID(this.getModelPluginID());
            genModel.setModelDirectory(this.getJavaModelDirectory());
            if (this.generateEdit) {
                genModel.setEditPluginID(this.getEditPluginID());
                genModel.setEditDirectory(this.getEditDirectory());
            }
            if (this.generateEditor) {
                genModel.setEditorPluginID(this.getEditorPluginID());
                genModel.setEditorDirectory(this.getEditorDirectory());
            }
            genModel.setValidateModel(false);
            genModel.setForceOverwrite(true);
            genModel.setFacadeHelperClass(null);
            genModel.setBundleManifest(true);
            genModel.setUpdateClasspath(false);
            genModel.setComplianceLevel(this.jdkLevel);
            genModel.setRuntimeVersion(this.emfRuntimeVersion);
            genModel.setRootExtendsClass("org.eclipse.emf.ecore.impl.MinimalEObjectImpl$Container");
            genModel.setLineDelimiter(this.codeConfig.getLineDelimiter());
            String _fileHeader = this.codeConfig.getFileHeader();
            boolean _tripleNotEquals = (_fileHeader != null);
            if (_tripleNotEquals) {
                genModel.setCopyrightText(EMFGeneratorFragment2.trimMultiLineComment(this.codeConfig.getFileHeader()));
            }
        }
        genModelFile.getContents().add(genModel);
        return genModel;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) GenModelImpl(org.eclipse.emf.codegen.ecore.genmodel.impl.GenModelImpl) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) URI(org.eclipse.emf.common.util.URI) GenPackageImpl(org.eclipse.emf.codegen.ecore.genmodel.impl.GenPackageImpl)

Example 5 with GenModel

use of org.eclipse.emf.codegen.ecore.genmodel.GenModel in project xtext-core by eclipse.

the class EMFGeneratorFragment2 method generate.

@Override
public void generate() {
    boolean _isEmpty = IterableExtensions.isEmpty(Iterables.<GeneratedMetamodel>filter(this.getGrammar().getMetamodelDeclarations(), GeneratedMetamodel.class));
    if (_isEmpty) {
        return;
    }
    try {
        final Grammar clonedGrammar = this.cloneGrammarIntoNewResourceSet(this.getGrammar());
        final ResourceSet workingResourceSet = clonedGrammar.eResource().getResourceSet();
        final Function1<GeneratedMetamodel, EPackage> _function = (GeneratedMetamodel it) -> {
            return it.getEPackage();
        };
        final List<EPackage> generatedPackages = IterableExtensions.<EPackage>toList(IterableExtensions.<GeneratedMetamodel, EPackage>map(Iterables.<GeneratedMetamodel>filter(clonedGrammar.getMetamodelDeclarations(), GeneratedMetamodel.class), _function));
        if ((this.genModel != null)) {
            this.registerUsedGenModel(workingResourceSet.getURIConverter(), clonedGrammar);
        }
        boolean _isEmpty_1 = generatedPackages.isEmpty();
        boolean _not = (!_isEmpty_1);
        if (_not) {
            final Map<String, EPackage> usedEPackages = this.findAllUsedEPackages(generatedPackages);
            final Map<String, EPackage> loadedEPackages = this.findEPackagesInGenPackages(usedEPackages.keySet(), workingResourceSet);
            final Map<EObject, EObject> eNamedElementMapping = this.createENamedElementMapping(usedEPackages, loadedEPackages);
            this.replaceReferencesInGeneratedPackages(generatedPackages, eNamedElementMapping);
            final Resource ePackageResource = this.createResourceForEPackages(clonedGrammar, generatedPackages, workingResourceSet);
            if ((!this.skipGenerate)) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("Generating EMF model");
                {
                    if (this.generateEdit) {
                        {
                            if (this.generateEditor) {
                                _builder.append(", edit");
                            } else {
                                _builder.append(" and edit");
                            }
                        }
                    }
                }
                {
                    if (this.generateEditor) {
                        _builder.append(" and editor");
                    }
                }
                _builder.append(" code");
                EMFGeneratorFragment2.LOG.info(_builder);
                final GenModel genModel = this.getSaveAndReconcileGenModel(clonedGrammar, generatedPackages, workingResourceSet);
                genModel.reconcile();
                this.doGenerate(genModel);
                this.addProjectContributions(clonedGrammar, generatedPackages, workingResourceSet);
                if (this.bindEPackageAndEFactory) {
                    for (final EPackage pkg : generatedPackages) {
                        {
                            final GenPackage genPkg = GenModelUtil2.getGenPackage(pkg, genModel.eResource().getResourceSet());
                            GuiceModuleAccess.BindingFactory _bindingFactory = new GuiceModuleAccess.BindingFactory();
                            TypeReference _typeRef = TypeReference.typeRef(genPkg.getQualifiedPackageInterfaceName());
                            StringConcatenationClient _client = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    String _packageInterfaceName = genPkg.getPackageInterfaceName();
                                    _builder.append(_packageInterfaceName);
                                    _builder.append(".eINSTANCE");
                                }
                            };
                            GuiceModuleAccess.BindingFactory _addTypeToInstance = _bindingFactory.addTypeToInstance(_typeRef, _client);
                            TypeReference _typeRef_1 = TypeReference.typeRef(genPkg.getQualifiedFactoryInterfaceName());
                            StringConcatenationClient _client_1 = new StringConcatenationClient() {

                                @Override
                                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                                    String _factoryInterfaceName = genPkg.getFactoryInterfaceName();
                                    _builder.append(_factoryInterfaceName);
                                    _builder.append(".eINSTANCE");
                                }
                            };
                            _addTypeToInstance.addTypeToInstance(_typeRef_1, _client_1).contributeTo(this.getLanguage().getRuntimeGenModule());
                        }
                    }
                }
            }
            this.saveResource(ePackageResource);
        }
        this.updateBuildProperties();
    } catch (final Throwable _t) {
        if (_t instanceof Exception) {
            final Exception e = (Exception) _t;
            EMFGeneratorFragment2.LOG.error("Failed to execute EMF generator", e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : GuiceModuleAccess(org.eclipse.xtext.xtext.generator.model.GuiceModuleAccess) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Grammar(org.eclipse.xtext.Grammar) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) EPackage(org.eclipse.emf.ecore.EPackage) GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Aggregations

GenModel (org.eclipse.emf.codegen.ecore.genmodel.GenModel)7 GenPackage (org.eclipse.emf.codegen.ecore.genmodel.GenPackage)7 EObject (org.eclipse.emf.ecore.EObject)6 Resource (org.eclipse.emf.ecore.resource.Resource)6 EPackage (org.eclipse.emf.ecore.EPackage)4 XMLResource (org.eclipse.emf.ecore.xmi.XMLResource)4 URI (org.eclipse.emf.common.util.URI)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)3 Predicate (com.google.common.base.Predicate)2 List (java.util.List)2 IStatus (org.eclipse.core.runtime.IStatus)2 EReference (org.eclipse.emf.ecore.EReference)2 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 ComparisonFailure (org.junit.ComparisonFailure)2 GenModelImpl (org.eclipse.emf.codegen.ecore.genmodel.impl.GenModelImpl)1 GenPackageImpl (org.eclipse.emf.codegen.ecore.genmodel.impl.GenPackageImpl)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 GenModelHelper (org.eclipse.emf.mwe.utils.GenModelHelper)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1