public @NonNull Model importObjects( @NonNull Collection<EObject> ecoreContents, @NonNull URI pivotURI) { EPackage libraryEPackage = isLibrary(ecoreContents); if (libraryEPackage != null) { newCreateMap = new HashMap<@NonNull EObject, @NonNull Element>(); org.eclipse.ocl.pivot.Package asLibrary = standardLibrary.getPackage(); newCreateMap.put(libraryEPackage, asLibrary); List<org.eclipse.ocl.pivot.Class> ownedType = asLibrary.getOwnedClasses(); // int prefix = LibraryConstants.ECORE_STDLIB_PREFIX.length(); for (@SuppressWarnings("null") @NonNull EClassifier eClassifier : libraryEPackage.getEClassifiers()) { String name = environmentFactory.getTechnology().getOriginalName(eClassifier); // .substring(prefix); Type asType = NameUtil.getNameable(ownedType, name); if (asType != null) { newCreateMap.put(eClassifier, asType); } } Model containingRoot = PivotUtil.getContainingModel(asLibrary); return ClassUtil.nonNullModel(containingRoot); } @NonNull ASResource asResource = metamodelManager.getResource(pivotURI, ASResource.ECORE_CONTENT_TYPE); // try { if ((metamodelManager.getLibraryResource() == null) && isPivot(ecoreContents)) { String nsURI = ((EPackage) ecoreContents.iterator().next()).getNsURI(); if (nsURI != null) { String stdlibASUri = LibraryConstants.STDLIB_URI + PivotConstants.DOT_OCL_AS_FILE_EXTENSION; OCLstdlib library = OCLstdlib.create(stdlibASUri); metamodelManager.installResource(library); // metamodelManager.installAs(nsURI, OCLstdlibTables.PACKAGE); } } URI uri = ecoreURI != null ? ecoreURI : ecoreResource.getURI(); Model pivotModel2 = null; if (asResource.getContents().size() > 0) { EObject eObject = asResource.getContents().get(0); if (eObject instanceof Model) { pivotModel2 = (Model) eObject; } } if (pivotModel2 == null) { pivotModel2 = pivotModel = PivotUtil.createModel(uri.toString()); } pivotModel = pivotModel2; // installImports(); update(asResource, ecoreContents); // } // catch (Exception e) { // if (errors == null) { // errors = new ArrayList<Resource.Diagnostic>(); // } // errors.add(new XMIException("Failed to load '" + pivotURI + "'", e)); // } List<Diagnostic> errors2 = errors; if (errors2 != null) { asResource.getErrors().addAll(ClassUtil.nullFree(errors2)); } return pivotModel2; }
private @NonNull URI resolveImportURI( @NonNull URI uri, @NonNull EPackage ePackage, @Nullable URI baseURI) { if (baseURI == null) { return uri; } ProjectManager projectManager = environmentFactory.getProjectManager(); if (!(projectManager instanceof StandaloneProjectMap)) { return uri; } StandaloneProjectMap projectMap = (StandaloneProjectMap) projectManager; /* if (baseURI == null) { IPackageDescriptor packageDescriptor = projectManager.getPackageDescriptor(URI.createURI(ePackage.getNsURI())); if (packageDescriptor == null) { return uri; } IResourceDescriptor resourceDescriptor = packageDescriptor.getResourceDescriptor(); baseURI = resourceDescriptor.getPlatformPluginURI(); } */ uri = uri.resolve(baseURI); if (uri.isPlatformPlugin() && ClassUtil.safeEquals(ePackage.getNsURI(), String.valueOf(ePackage.eResource().getURI())) && (uri.segmentCount() >= 1)) { @NonNull String projectName = uri.segment(1); IProjectDescriptor projectDescriptor = projectMap.getProjectDescriptor(projectName); if (projectDescriptor instanceof IProjectDescriptorExtension) { Collection<IResourceDescriptor> resourceDescriptors = projectDescriptor.getResourceDescriptors(); if (resourceDescriptors != null) { for (IResourceDescriptor resourceDescriptor : resourceDescriptors) { if (ClassUtil.safeEquals( uri.trimFragment(), resourceDescriptor.getPlatformPluginURI())) { Iterable<@NonNull IPackageDescriptor> packageDescriptors = ((IProjectDescriptorExtension) projectDescriptor).getPackageDescriptors(); if (packageDescriptors != null) { for (IPackageDescriptor packageDescriptor : packageDescriptors) { uri = packageDescriptor.getNsURI(); break; } } break; } } } } } return uri; }
@Override public @Nullable Object evaluate( @NonNull Executor executor, @NonNull TypeId returnTypeId, @Nullable Object sourceValue) { IdResolver idResolver = executor.getIdResolver(); Property oppositeProperty = idResolver.getProperty(oppositePropertyId); ModelManager.ModelManagerExtension modelManager = (ModelManager.ModelManagerExtension) executor.getModelManager(); Type thatType = ClassUtil.nonNullModel(oppositeProperty.getType()); if (thatType instanceof CollectionType) { thatType = ((CollectionType) thatType).getElementType(); } List<Object> results = new ArrayList<Object>(); if (thatType instanceof org.eclipse.ocl.pivot.Class) { for (@NonNull Object eObject : modelManager.get((org.eclipse.ocl.pivot.Class) thatType)) { // FIXME Use a cache EClass eClass = modelManager.eClass(eObject); EStructuralFeature eFeature = eClass.getEStructuralFeature(oppositeProperty.getName()); assert eFeature != null; Object eGet = modelManager.eGet(eObject, eFeature); if (eGet == sourceValue) { results.add(eObject); } } } return results; }
@Override public @NonNull String getInitialContentsAsString( @NonNull IFile newFile, @NonNull AbstractFileDialog dialog) { URI ecoreURI = URI.createPlatformResourceURI(newFile.getFullPath().toString(), true); URI oclInEcoreURI = ecoreURI.trimFileExtension().appendFileExtension("oclinecore"); String initialContentsAsString = super.getInitialContentsAsString(newFile, dialog); @SuppressWarnings("null") OCL ocl = OCL.newInstance(EPackage.Registry.INSTANCE); ResourceSet resourceSet2 = ocl.getResourceSet(); BaseCSResource csResource = ClassUtil.nonNullState((BaseCSResource) resourceSet2.createResource(oclInEcoreURI)); try { ByteArrayInputStream inputStream = new ByteArrayInputStream(initialContentsAsString.getBytes()); csResource.load(inputStream, null); ASResource asResource = ocl.cs2as(csResource); Resource eResource = ocl.as2ecore(asResource, ecoreURI); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); eResource.save(outputStream, null); @SuppressWarnings("null") @NonNull String string = outputStream.toString(); return string; } catch (IOException e) { logger.error("Failed to create " + ecoreURI, e); } return initialContentsAsString; }
protected boolean isPivot(@NonNull Collection<EObject> ecoreContents) { if (ecoreContents.size() != 1) { return false; } EObject ecoreRoot = ecoreContents.iterator().next(); if (!(ecoreRoot instanceof EPackage)) { return false; } EPackage ecorePackage = (EPackage) ecoreRoot; if (ClassUtil.basicGetMetamodelAnnotation(ecorePackage) != null) { return true; } // FIXME Following code should be redundant if (ecorePackage.getEClassifier(PivotPackage.Literals.ENUMERATION_LITERAL.getName()) == null) { return false; } if (ecorePackage.getEClassifier(PivotPackage.Literals.EXPRESSION_IN_OCL.getName()) == null) { return false; } if (ecorePackage.getEClassifier(PivotPackage.Literals.OPERATION_CALL_EXP.getName()) == null) { return false; } if (ecorePackage.getEClassifier(PivotPackage.Literals.TEMPLATE_PARAMETER_SUBSTITUTION.getName()) == null) { return false; } return true; }
@Override public @NonNull Model getASModel() { Model pivotModel2 = pivotModel; if (pivotModel2 == null) { loadImports(ecoreResource); pivotModel2 = pivotModel = importObjects(ClassUtil.nonNullEMF(ecoreResource.getContents()), createPivotURI()); @SuppressWarnings("null") @NonNull Resource asResource = pivotModel2.eResource(); AliasAdapter ecoreAdapter = AliasAdapter.findAdapter(ecoreResource); if (ecoreAdapter != null) { Map<EObject, String> ecoreAliasMap = ecoreAdapter.getAliasMap(); AliasAdapter pivotAdapter = AliasAdapter.getAdapter(asResource); Map<EObject, String> pivotAliasMap = pivotAdapter.getAliasMap(); for (EObject eObject : ecoreAliasMap.keySet()) { String alias = ecoreAliasMap.get(eObject); Element element = newCreateMap.get(eObject); pivotAliasMap.put(element, alias); } } metamodelManager.installResource(asResource); installImports(); } return pivotModel2; }
public static Ecore2AS loadFromEcore(@NonNull ASResource ecoreASResource, @NonNull URI ecoreURI) { EnvironmentFactoryInternal environmentFactory = PivotUtilInternal.getEnvironmentFactory(ecoreASResource); ResourceSet resourceSet = environmentFactory.getResourceSet(); Resource ecoreResource = resourceSet.getResource(ecoreURI, true); if (ecoreResource == null) { return null; } Ecore2AS conversion = getAdapter(ecoreResource, environmentFactory); conversion.loadImports(ecoreResource); // if (asMetamodels != null) { // // } PivotMetamodelManager metamodelManager = environmentFactory.getMetamodelManager(); conversion.pivotModel = PivotUtil.createModel(ecoreASResource.getURI().toString()); // conversion.installImports(); conversion.update(ecoreASResource, ClassUtil.nonNullEMF(ecoreResource.getContents())); AliasAdapter ecoreAdapter = AliasAdapter.findAdapter(ecoreResource); if (ecoreAdapter != null) { Map<EObject, String> ecoreAliasMap = ecoreAdapter.getAliasMap(); AliasAdapter pivotAdapter = AliasAdapter.getAdapter(ecoreASResource); Map<EObject, String> pivotAliasMap = pivotAdapter.getAliasMap(); for (EObject eObject : ecoreAliasMap.keySet()) { String alias = ecoreAliasMap.get(eObject); Element element = conversion.newCreateMap.get(eObject); pivotAliasMap.put(element, alias); } } metamodelManager.installResource(ecoreASResource); conversion.installImports(); return conversion; }
public static @NonNull AbstractPlace createLocalPlace( @NonNull Map<CGElement, AbstractPlace> element2place, @NonNull CGValuedElement cgElement) { boolean isGlobal = cgElement.isGlobal(); if (isGlobal) { return ClassUtil.nonNullState(element2place.get(null)); } else { return ControlPlace.createControlPlace(element2place, cgElement); } }
/** * Convert an (annotated) Ecore object to a pivot element. * * @param eObject the annotated Ecore object * @return the pivot element */ public static Element importFromEcore( @NonNull EnvironmentFactoryInternal environmentFactory, String alias, @NonNull EObject eObject) { Resource ecoreResource = ClassUtil.nonNullEMF(eObject.eResource()); Ecore2AS conversion = getAdapter(ecoreResource, environmentFactory); @SuppressWarnings("unused") Model pivotModel = conversion.getASModel(); return conversion.newCreateMap.get(eObject); }
@Override public RootPackageId getMetamodelId( @NonNull EnvironmentFactoryInternal environmentFactory, @NonNull EPackage ePackage) { assert !"http://www.eclipse.org/uml2/5.0.0/UML".equals(ePackage.getNsURI()); assert !"http://www.eclipse.org/uml2/5.0.0/Types".equals(ePackage.getNsURI()); RootPackageId metamodel = null; if (ClassUtil.basicGetMetamodelAnnotation(ePackage) != null) { metamodel = IdManager.METAMODEL; } else { String nsURI = ePackage.getNsURI(); String sharedNsURI = environmentFactory.getCompleteModel().getCompleteURI(nsURI); if ((sharedNsURI != null) && !sharedNsURI.equals(nsURI)) { metamodel = IdManager.getRootPackageId(sharedNsURI); } } return metamodel; }
protected void loadImports(@NonNull EPackage ePackage, @Nullable URI baseURI) { if (ClassUtil.basicGetMetamodelAnnotation(ePackage) != null) { if (asMetamodels == null) { asMetamodels = new HashSet<EPackage>(); } asMetamodels.add(ePackage); } EAnnotation importAnnotation = ePackage.getEAnnotation(PivotConstants.IMPORT_ANNOTATION_SOURCE); if (importAnnotation != null) { EMap<String, String> details = importAnnotation.getDetails(); for (String key : details.keySet()) { URI uri = URI.createURI(details.get(key)); uri = resolveImportURI(uri, ePackage, baseURI); assert uri != null; ResourceSet resourceSet = environmentFactory.getResourceSet(); EObject importedEObject = null; String fragment = uri.fragment(); if (fragment == null) { importedEObject = resourceSet.getPackageRegistry().getEPackage(uri.toString()); } else { importedEObject = resourceSet.getEObject(uri, true); } if (importedEObject != null) { if (importedEObjects == null) { importedEObjects = new HashSet<EObject>(); } if (importedEObjects.add(importedEObject) && (importedEObject instanceof EPackage)) { Resource importedResource = importedEObject.eResource(); if (importedResource != null) { URI baseURI2 = getBaseURI(importedResource); loadImports((EPackage) importedEObject, baseURI2); } } } } } for (EPackage eSubPackage : ePackage.getESubpackages()) { if (eSubPackage != null) { loadImports(eSubPackage, baseURI); } } }
/** Return the baseURI of ecoreResource against which its imports should be resolved. */ protected @Nullable URI getBaseURI(@NonNull Resource ecoreResource) { URI ecoreURI = ecoreResource.getURI(); if (ecoreURI == null) { return null; } if (ClassUtil.isRegistered(ecoreResource)) { ProjectManager projectManager = environmentFactory.getProjectManager(); StandaloneProjectMap.IPackageDescriptor packageDescriptor = projectManager.getPackageDescriptor(ecoreURI); if (packageDescriptor == null) { return null; } return packageDescriptor.getResourceDescriptor().getPlatformPluginURI(); } else { if (!ecoreURI.isHierarchical() || ecoreURI.isRelative()) { return null; } return ecoreURI; } }
/** * Executes the query for the specified <tt>target</tt> object. The result is the OCL evaluation * result which may be a Number, String, Collection or other object for normal returns or a * NullLiteralExp for null, or an InvalidLiteralExp for invalid. * * @param target the object on which to execute the query; this must be an instance of the context * with which the delegate was created * @param arguments a map of variable names to values; these must correspond to the variables with * which the delegate was created * @return the query's result * @throws InvocationTargetException in case of failure to prepare or execute the query, usually * because of an exception */ @Override public Object execute(@Nullable Object target, Map<String, ?> arguments) throws InvocationTargetException { @SuppressWarnings("null") @NonNull Map<String, ?> nonNullArguments = (arguments != null ? arguments : (Map<String, ?>) Collections.<String, Object>emptyMap()); try { if (specification == null) { prepare(); } @SuppressWarnings("null") @NonNull ExpressionInOCL nonNullSpecification = specification; OCL ocl = delegateDomain.getOCL(); IdResolver idResolver = ocl.getIdResolver(); Object targetValue = idResolver.boxedValueOf(target); org.eclipse.ocl.pivot.Class targetType = idResolver.getStaticTypeOf(targetValue); Type requiredType = nonNullSpecification.getOwnedContext().getType(); if ((requiredType == null) || !targetType.conformsTo(ocl.getStandardLibrary(), requiredType)) { String message = StringUtil.bind( PivotMessagesInternal.WrongContextClassifier_ERROR_, targetType, requiredType); throw new OCLDelegateException(new SemanticException(message)); } List<Variable> parameterVariables = nonNullSpecification.getOwnedParameters(); int argCount = arguments != null ? arguments.size() : 0; if (parameterVariables.size() != argCount) { String message = StringUtil.bind( PivotMessagesInternal.MismatchedArgumentCount_ERROR_, argCount, parameterVariables.size()); throw new OCLDelegateException(new SemanticException(message)); } Query query = ocl.createQuery(nonNullSpecification); EvaluationEnvironment env = query.getEvaluationEnvironment(target); for (Variable parameterVariable : parameterVariables) { // bind arguments to parameter names String name = parameterVariable.getName(); Object object = nonNullArguments.get(name); if ((object == null) && !nonNullArguments.containsKey(name)) { String message = StringUtil.bind( PivotMessagesInternal.EvaluationResultIsInvalid_ERROR_, nonNullSpecification.getBody()); throw new OCLDelegateException(new SemanticException(message)); } Object value = idResolver.boxedValueOf(object); targetType = idResolver.getStaticTypeOf(value); requiredType = ClassUtil.nonNullModel(parameterVariable.getType()); if (!targetType.conformsTo(ocl.getStandardLibrary(), requiredType)) { String message = StringUtil.bind( PivotMessagesInternal.MismatchedArgumentType_ERROR_, name, targetType, requiredType); throw new OCLDelegateException(new SemanticException(message)); } env.add(parameterVariable, value); } Object result = evaluate(query, target); // if (result.isInvalid()) { // String message = ClassUtil.bind(OCLMessages.EvaluationResultIsInvalid_ERROR_, // getOperationName()); // throw new OCLDelegateException(message); // } // if ((result == null) / * || ocl.isInvalid(result) * /) { // String message = ClassUtil.bind(OCLMessages.EvaluationResultIsNull_ERROR_, // getOperationName()); // throw new OCLDelegateException(message); // } // return converter.convert(ocl, result); // if (result == null) { // String message = NLS.bind(OCLMessages.EvaluationResultIsInvalid_ERROR_, // PivotUtil.getBody(specification)); // throw new InvocationTargetException(new OCLDelegateException(message)); // } return idResolver.ecoreValueOf(null, result); } catch (InvocationTargetException e) { throw e; } catch (EvaluationException e) { String message = StringUtil.bind( PivotMessagesInternal.EvaluationResultIsInvalid_ERROR_, specification.getBody()); throw new InvocationTargetException(new EvaluationException(message)); } catch (WrappedException e) { throw new InvocationTargetException(e.getCause()); } catch (Exception e) { throw new InvocationTargetException(e); } }
public void update(@NonNull Resource asResource, @NonNull Collection<EObject> ecoreContents) { newCreateMap = new HashMap<@NonNull EObject, @NonNull Element>(); referencers = new HashSet<@NonNull EObject>(); genericTypes = new ArrayList<@NonNull EGenericType>(); PivotUtilInternal.refreshList( asResource.getContents(), Collections.singletonList(ClassUtil.nonNull(pivotModel))); List<org.eclipse.ocl.pivot.Package> newPackages = new ArrayList<org.eclipse.ocl.pivot.Package>(); for (EObject eObject : ecoreContents) { EClass eClass = eObject.eClass(); if (eClass.getEPackage() != EcorePackage.eINSTANCE) { error("Non Ecore " + eClass.getName() + " for Ecore2AS.update"); } else { Object pivotElement = declarationPass.doInPackageSwitch(eObject); if (pivotElement instanceof org.eclipse.ocl.pivot.Package) { newPackages.add((org.eclipse.ocl.pivot.Package) pivotElement); } else { error("Bad ecore content"); } } } PivotUtilInternal.refreshList(pivotModel.getOwnedPackages(), newPackages); Map<@NonNull String, @NonNull Type> resolvedSpecializations = new HashMap<@NonNull String, @NonNull Type>(); for (@NonNull EGenericType eGenericType : genericTypes) { Type pivotType = resolveType(resolvedSpecializations, eGenericType); if (pivotType != null) { newCreateMap.put(eGenericType, pivotType); } } for (EObject eObject : referencers) { referencePass.doInPackageSwitch(eObject); } for (EObject eObject : referencers) { if (eObject instanceof EReference) { Property pivotElement = getCreated(Property.class, eObject); if (pivotElement != null) { Property oppositeProperty = pivotElement.getOpposite(); if ((oppositeProperty == null) && (eObject.eContainer() instanceof EClass)) { // Skip annotation references metamodelManager.installPropertyDeclaration(pivotElement); } } } } referencers = null; genericTypes = null; oldIdMap = new HashMap<@NonNull String, @NonNull Element>(); for (EObject ecoreContent : ecoreContents) { Resource resource = ecoreContent.eResource(); if (resource instanceof XMLResource) { XMLResource xmlResource = (XMLResource) resource; String id = xmlResource.getID(ecoreContent); if (id != null) { Element element = newCreateMap.get(ecoreContent); if (element != null) { oldIdMap.put(id, element); } } for (TreeIterator<EObject> tit = ecoreContent.eAllContents(); tit.hasNext(); ) { EObject eObject = tit.next(); id = xmlResource.getID(eObject); if (id != null) { Element element = newCreateMap.get(eObject); if (element != null) { oldIdMap.put(id, element); } } } } } }
@Override public @NonNull URI getURI() { return ClassUtil.nonNullEMF(ecoreResource.getURI()); }
public @NonNull Type getType(@NonNull EClassifier eClassifier) { Ecore2AS ecore2as = Ecore2AS.getAdapter(ClassUtil.nonNullEMF(eClassifier.eResource()), environmentFactory); Type pivotType = ecore2as.getCreated(Type.class, eClassifier); return ClassUtil.nonNullState(pivotType); }