Search in sources :

Example 1 with BPackage

use of org.obeonetwork.dsl.ecorebinding.BPackage in project InformationSystem by ObeoNetwork.

the class EcorebindingModelWizardSpec method createInitialModel.

/**
 *  (non-Javadoc)
 * @see org.obeonetwork.dsl.ecorebinding.presentation.EcorebindingModelWizard#createInitialModel()
 */
@Override
protected EObject createInitialModel() {
    IPath ecoreFilePath = this.ecoreSelectionWizardPage.getEcoreFilePath();
    ResourceSet ecorers = new ResourceSetImpl();
    Resource ecoreResource = ecorers.getResource(URI.createFileURI(ecoreFilePath.toFile().getAbsolutePath()), true);
    try {
        ecoreResource.load(new HashMap<String, String>());
    } catch (IOException e) {
        e.printStackTrace();
    }
    ResourceSet rs = new ResourceSetImpl();
    IPath ecoreBindingModelPath = ecoreFilePath;
    ecoreBindingModelPath = ecoreFilePath.removeFileExtension().addFileExtension("ecorebinding");
    Resource resource = rs.createResource(URI.createFileURI(ecoreBindingModelPath.toFile().getAbsolutePath()));
    BModel ecoreBindingModel = EcorebindingFactory.eINSTANCE.createBModel();
    List<EObject> contents = ecoreResource.getContents();
    for (EObject eObject : contents) {
        // We only support EPackage as root element.
        if (eObject instanceof EPackage) {
            EPackage rootPackage = (EPackage) eObject;
            BPackage ecoreBindingPackage = EcorebindingFactory.eINSTANCE.createBPackage();
            ecoreBindingPackage.setEcorePackage(rootPackage);
            ecoreBindingModel.getBPackages().add(ecoreBindingPackage);
            explore(rootPackage, ecoreBindingPackage);
        } else {
            throw new IllegalArgumentException("The Ecore Binding only supports EPackage as root element");
        }
    }
    resource.getContents().add(ecoreBindingModel);
    try {
        resource.save(new HashMap<String, String>());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ecoreBindingModel;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) IPath(org.eclipse.core.runtime.IPath) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) EPackage(org.eclipse.emf.ecore.EPackage) BPackage(org.obeonetwork.dsl.ecorebinding.BPackage) BModel(org.obeonetwork.dsl.ecorebinding.BModel) EObject(org.eclipse.emf.ecore.EObject)

Example 2 with BPackage

use of org.obeonetwork.dsl.ecorebinding.BPackage in project InformationSystem by ObeoNetwork.

the class EcorebindingModelWizardSpec method explore.

/**
 * Expore the EPackage and init the BPackage.
 * @param ePackage The ecore Package input to read
 * @param bPackage The Binding Package to build
 */
private static void explore(EPackage ePackage, BPackage bPackage) {
    EList<EClassifier> eClassifiers = ePackage.getEClassifiers();
    for (EClassifier eClassifier : eClassifiers) {
        if (eClassifier instanceof EClass) {
            EClass eClass = (EClass) eClassifier;
            // Create BClass
            BClass bClass = EcorebindingFactory.eINSTANCE.createBClass();
            bClass.setEcoreClass(eClass);
            bPackage.getBClasses().add(bClass);
            bPackage.getBClassifiers().add(bClass);
            explore(eClass, bClass);
        } else if (eClassifier instanceof EEnum) {
            EEnum eEnum = (EEnum) eClassifier;
            // Create BEnum
            BEnum bEnum = EcorebindingFactory.eINSTANCE.createBEnum();
            bEnum.setEcoreEnum(eEnum);
            bEnum.setEcoreDataType(eEnum);
            bPackage.getBEnums().add(bEnum);
            bPackage.getBClassifiers().add(bEnum);
            List<EEnumLiteral> eLiterals = eEnum.getELiterals();
            for (EEnumLiteral eLiteral : eLiterals) {
                // Create BEnumLiteral
                BEnumLiteral bEnumLiteral = EcorebindingFactory.eINSTANCE.createBEnumLiteral();
                bEnumLiteral.setBEnum(bEnum);
                bEnumLiteral.setEcoreEnumLiteral(eLiteral);
            }
        } else if (eClassifier instanceof EDataType) {
            EDataType eDataType = (EDataType) eClassifier;
            // Create BDataType
            BDataType bDataType = EcorebindingFactory.eINSTANCE.createBDataType();
            bDataType.setEcoreDataType(eDataType);
            bPackage.getBDataTypes().add(bDataType);
            bPackage.getBClassifiers().add(bDataType);
        }
    }
    List<EPackage> eSubpackages = ePackage.getESubpackages();
    for (EPackage subEPackage : eSubpackages) {
        // Create BPackage
        BPackage subBPackage = EcorebindingFactory.eINSTANCE.createBPackage();
        subBPackage.setEcorePackage(subEPackage);
        bPackage.getNestedBPackages().add(subBPackage);
        explore(subEPackage, subBPackage);
    }
}
Also used : BEnum(org.obeonetwork.dsl.ecorebinding.BEnum) BDataType(org.obeonetwork.dsl.ecorebinding.BDataType) EDataType(org.eclipse.emf.ecore.EDataType) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) BPackage(org.obeonetwork.dsl.ecorebinding.BPackage) EClass(org.eclipse.emf.ecore.EClass) BEnumLiteral(org.obeonetwork.dsl.ecorebinding.BEnumLiteral) BClass(org.obeonetwork.dsl.ecorebinding.BClass) EList(org.eclipse.emf.common.util.EList) List(java.util.List) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral)

Aggregations

EPackage (org.eclipse.emf.ecore.EPackage)2 BPackage (org.obeonetwork.dsl.ecorebinding.BPackage)2 IOException (java.io.IOException)1 List (java.util.List)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 EList (org.eclipse.emf.common.util.EList)1 EClass (org.eclipse.emf.ecore.EClass)1 EClassifier (org.eclipse.emf.ecore.EClassifier)1 EDataType (org.eclipse.emf.ecore.EDataType)1 EEnum (org.eclipse.emf.ecore.EEnum)1 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 BClass (org.obeonetwork.dsl.ecorebinding.BClass)1 BDataType (org.obeonetwork.dsl.ecorebinding.BDataType)1 BEnum (org.obeonetwork.dsl.ecorebinding.BEnum)1 BEnumLiteral (org.obeonetwork.dsl.ecorebinding.BEnumLiteral)1