/** * Return the ConstrainingNode node for EObject creating any parent ConstrainingNodes that are * required to ensure that the returned ConstrainingNode is installed in the root. */ public @NonNull ConstrainingNode getConstrainingNode(@NonNull EObject eObject) { URI uri = getURI(eObject); ConstrainingNode constrainingNode = allConstrainingNodes.get(uri); if (constrainingNode == null) { EObject eContainer = eObject.eContainer(); if (eContainer == null) { RootConstrainingNode rootConstrainingNode = createRootConstrainingNode(); rootNode.getConstrainingNodes().add(rootConstrainingNode); constrainingNode = rootConstrainingNode; } // else if (eObject instanceof EClass) { // constrainingNode = ValidationFactory.eINSTANCE.createLeafConstrainingNode(); // ConstrainingNode parentConstrainingNode = getConstrainingNode(eContainer); // parentConstrainingNode.getChildren().add(constrainingNode); // } else { constrainingNode = createConstrainingNode(); ConstrainingNode parentConstrainingNode = getConstrainingNode(eContainer); parentConstrainingNode.getChildren().add(constrainingNode); } constrainingNode.setConstrainingObject(eObject); constrainingNode.setLabel(validityManager.getLabel(eObject)); constrainingNode.setEnabled(true); allConstrainingNodes.put(uri, constrainingNode); } return constrainingNode; }
private @NonNull URI getURI(@NonNull EObject eObject) { ConstraintLocator constraintLocator = ValidityManager.getConstraintLocator(eObject); if (constraintLocator != null) { URI uri = constraintLocator.getURI(eObject); if (uri != null) { return uri; } } return EcoreUtil.getURI(eObject); }
/** Return all types that may provode constraints to an instance if aType. */ protected @NonNull Set<URI> getTypeClosure(@NonNull EModelElement aType) { Set<URI> typeClosure = typeClosures.get(aType); if (typeClosure == null) { typeClosure = new HashSet<URI>(); List<ConstraintLocator> constraintLocators = ValidityManager.getConstraintLocators(aType.eClass().getEPackage().getNsURI()); if (constraintLocators != null) { for (ConstraintLocator constraintLocator : constraintLocators) { typeClosure.addAll(constraintLocator.getAllTypes(aType)); } } typeClosures.put(aType, typeClosure); } return typeClosure; }
// // Find all constraints for each EClass // protected Map<EModelElement, Set<LeafConstrainingNode>> locateConstraints( @NonNull Map<EPackage, Set<Resource>> ePackage2resources) { Map<EModelElement, Set<LeafConstrainingNode>> allConstraints = new HashMap<EModelElement, Set<LeafConstrainingNode>>(); for (@SuppressWarnings("null") @NonNull EPackage ePackage : ePackage2resources.keySet()) { List<ConstraintLocator> list = ValidityManager.getConstraintLocators(ePackage.getNsURI()); if (list != null) { @SuppressWarnings("null") @NonNull Set<Resource> ePackageResources = ePackage2resources.get(ePackage); for (ConstraintLocator constraintLocator : list) { try { Map<EModelElement, List<LeafConstrainingNode>> availableConstraints = constraintLocator.getConstraints(this, ePackage, ePackageResources); if (availableConstraints != null) { assert !availableConstraints.containsKey(null); for (EModelElement constrainedType : availableConstraints.keySet()) { Set<LeafConstrainingNode> typeConstraints = allConstraints.get(constrainedType); if (typeConstraints == null) { typeConstraints = new HashSet<LeafConstrainingNode>(); allConstraints.put(constrainedType, typeConstraints); } typeConstraints.addAll(availableConstraints.get(constrainedType)); } } } catch (Exception e) { Set<ConstraintLocator> badConstraintLocators2 = badConstraintLocators; if (badConstraintLocators2 == null) { synchronized (this) { badConstraintLocators = badConstraintLocators2 = new HashSet<ConstraintLocator>(); } } if (!badConstraintLocators2.contains(constraintLocator)) { synchronized (badConstraintLocators2) { if (badConstraintLocators2.add(constraintLocator)) { logger.error("ConstraintLocator " + constraintLocator + " failed", e); } } } } } } } return allConstraints; }
/** * Return the ValidatableNode node for EObject creating any ValidatableNodes that are required to * ensure that the returned ValidatableNode is installed in the root. */ protected @NonNull ValidatableNode getValidatableNode(@NonNull EObject eObject) { ValidatableNode validatable = allValidatableNodes.get(eObject); if (validatable == null) { EObject eContainer = eObject.eContainer(); if (eContainer != null) { // ValidatableNode parentValidatableNode = getValidatableNode(eContainer); // parentValidatableNode.getChildren().add(validatable); } else if (eObject instanceof DynamicEObjectImpl) { EClass eClass = eObject.eClass(); for (EStructuralFeature eStructuralFeature : eClass.getEAllStructuralFeatures()) { String featureName = eStructuralFeature.getName(); if ((featureName != null) && featureName.startsWith( "base_") // org.eclipse.uml2.uml.Extension.METACLASS_ROLE_PREFIX) && (eStructuralFeature instanceof EReference) && eObject.eIsSet( eStructuralFeature)) { // Unset for an applicable stereotype that has not been // applied eContainer = (EObject) eObject.eGet(eStructuralFeature); break; } } } if (eContainer != null) { validatable = createValidatableNode(); ValidatableNode parentValidatableNode = getValidatableNode(eContainer); parentValidatableNode.getChildren().add(validatable); } else { RootValidatableNode rootValidatableNode = createRootValidatableNode(); rootNode.getValidatableNodes().add(rootValidatableNode); validatable = rootValidatableNode; } validatable.setEnabled(true); validatable.setLabel(validityManager.getLabel(eObject)); validatable.setConstrainedObject(eObject); allValidatableNodes.put(eObject, validatable); } return validatable; }
// // Find all EPackages in the source Resources // protected @NonNull Map<EPackage, Set<Resource>> analyzeResources( @NonNull Collection<Resource> resources) { List<Resource> allResources = new ArrayList<Resource>(resources); Map<EPackage, Set<Resource>> ePackage2resources = new HashMap<EPackage, Set<Resource>>(); for (int i = 0; i < allResources.size(); i++) { Resource resource = allResources.get(i); System.out.println(i + "/" + allResources.size() + " analyzeResources " + resource.getURI()); // System.out.println(resource); Set<EClass> eClasses = new HashSet<EClass>(); for (TreeIterator<EObject> tit = resource.getAllContents(); tit.hasNext(); ) { @SuppressWarnings("null") @NonNull EObject eObject = tit.next(); @SuppressWarnings("null") @NonNull EClass eClass = eObject.eClass(); eClasses.add(eClass); } Set<EPackage> ePackages = new HashSet<EPackage>(); for (@SuppressWarnings("null") @NonNull EClass eClass : eClasses) { ePackages.add(eClass.getEPackage()); for (@SuppressWarnings("null") @NonNull EClass eSuperClass : eClass.getEAllSuperTypes()) { ePackages.add(eSuperClass.getEPackage()); } } for (@SuppressWarnings("null") @NonNull EPackage ePackage : ePackages) { Set<Resource> ePackageResources = ePackage2resources.get(ePackage); if (ePackageResources == null) { ePackageResources = new HashSet<Resource>(); ePackage2resources.put(ePackage, ePackageResources); } ePackageResources.add(resource); List<ConstraintLocator> list = ValidityManager.getConstraintLocators(ePackage.getNsURI()); if (list != null) { for (ConstraintLocator constraintLocator : list) { try { Collection<Resource> moreResources = constraintLocator.getImports(ePackage, resource); if (moreResources != null) { for (Resource anotherResource : moreResources) { if (!allResources.contains(anotherResource)) { allResources.add(anotherResource); } } } } catch (Exception e) { Set<ConstraintLocator> badConstraintLocators2 = badConstraintLocators; if (badConstraintLocators2 == null) { synchronized (this) { badConstraintLocators = badConstraintLocators2 = new HashSet<ConstraintLocator>(); } } if (!badConstraintLocators2.contains(constraintLocator)) { synchronized (badConstraintLocators2) { if (badConstraintLocators2.add(constraintLocator)) { logger.error("ConstraintLocator " + constraintLocator + " failed", e); } } } } } } } } return ePackage2resources; }
public @NonNull String getLabel(@NonNull EObject eObject) { return validityManager.getLabel(eObject); }