Java Code Examples for org.eclipse.emf.ecore.EReference#getEOpposite()

The following examples show how to use org.eclipse.emf.ecore.EReference#getEOpposite() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkOppositeReferenceUsed(Assignment assignment) {
	Severity severity = getIssueSeverities(getContext(), getCurrentObject()).getSeverity(BIDIRECTIONAL_REFERENCE);
	if (severity == null || severity == Severity.IGNORE) {
		// Don't perform any check if the result is ignored
		return;
	}
	EClassifier classifier = GrammarUtil.findCurrentType(assignment);
	if (classifier instanceof EClass) {
		EStructuralFeature feature = ((EClass) classifier).getEStructuralFeature(assignment.getFeature());
		if (feature instanceof EReference) {
			EReference reference = (EReference) feature;
			if (reference.getEOpposite() != null && !(reference.isContainment() || reference.isContainer())) {
				addIssue("The feature '" + assignment.getFeature() + "' is a bidirectional reference."
						+ " This may cause problems in the linking process.",
						assignment, XtextPackage.eINSTANCE.getAssignment_Feature(), BIDIRECTIONAL_REFERENCE);
			}
		}
	}
}
 
Example 2
Source File: PackageMetaData.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private void initOppositeInfo() {
	for (EClassifier eClassifier : ePackage.getEClassifiers()) {
		if (eClassifier instanceof EClass) {
			EClass eClass = (EClass)eClassifier;
			boolean hasOpposites = false;
			boolean hasManyOpposites = false;
			for (EReference eReference : eClass.getEAllReferences()) {
				if (eReference.getEOpposite() != null) {
					hasOpposites = true;
					if (eReference.isMany()) {
						hasManyOpposites = true;
					}
				}
			}
			oppositeInfos.put(eClass, new OppositeInfo(hasOpposites, hasManyOpposites));
		}
	}
}
 
Example 3
Source File: MigrationValidator.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate whether opposite references are consistent with each other
 *
 * @param referenceSlot
 * @return true if they are consistent, false otherwise
 */
private boolean validate_validOpposite(ReferenceSlot referenceSlot) {

	final Instance from = referenceSlot.getInstance();
	final EReference reference = referenceSlot.getEReference();
	final EReference opposite = reference.getEOpposite();
	if (opposite != null) {
		for (final Instance to : referenceSlot.getValues()) {
			final Object value = to.get(opposite);
			if (opposite.isMany()) {
				if (!((Collection) value).contains(from)) {
					return false;
				}
			}
			else {
				if (value != from) {
					return false;
				}
			}
		}
	}

	return true;
}
 
Example 4
Source File: MetamodelImpl.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 *
 * @generated NOT
 */
@Override
public void setEOpposite(EReference reference, EReference opposite) {
	if (reference.getEOpposite() != null) {
		reference.getEOpposite().setEOpposite(null);
	}
	if (opposite != null) {
		final Model model = getRepository().getModel();
		if (model != null) {
			for (final Instance instance : model.getAllInstances(opposite
				.getEContainingClass())) {
				final EList<Instance> inverse = instance.getInverse(reference);
				if (!inverse.isEmpty()) {
					final ReferenceSlot referenceSlot = ((InstanceImpl) instance)
						.getCreateReferenceSlot(opposite);
					referenceSlot.getValues().clear();
					referenceSlot.getValues().addAll(inverse);
				}
			}
		}
		opposite.setEOpposite(reference);
	}
	reference.setEOpposite(opposite);
}
 
Example 5
Source File: InstanceImpl.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc --> <!-- end-user-doc -->
 *
 * @generated NOT
 */
@Override
public void remove(EStructuralFeature feature, int index) {
    if (feature instanceof EAttribute) {
        final EAttribute attribute = (EAttribute) feature;
        removeDeleteAttribute(attribute, index);
    } else {
        final EReference reference = (EReference) feature;
        final EReference opposite = reference.getEOpposite();
        if (opposite != null && reference.eContainer() != null) {
            final ReferenceSlot referenceSlot = (ReferenceSlot) getSlot(reference);
            final Instance target = referenceSlot.getValues().get(index);
            final int oppositeIndex = ((ReferenceSlot) target.getSlot(opposite))
                    .getValues().indexOf(this);
            ((InstanceImpl) target).removeDeleteReference(opposite,
                    oppositeIndex);
        }
        removeDeleteReference(reference, index);
    }
}
 
Example 6
Source File: N4JSValidationTestHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Asserts that root and the entire object tree below root does not contain any dangling references, i.e.
 * cross-references to target {@link EObject}s that are not contained in a {@link Resource}.
 */
public void assertNoDanglingReferences(EObject root) {
	final List<String> errMsgs = new ArrayList<>();
	final TreeIterator<EObject> iter = root.eAllContents();
	while (iter.hasNext()) {
		final EObject currObj = iter.next();
		if (currObj != null && !currObj.eIsProxy()) {
			for (EReference currRef : currObj.eClass().getEAllReferences()) {
				if (!currRef.isContainment() && !currRef.isContainer() && currRef.getEOpposite() == null) {
					if (currRef.isMany()) {
						@SuppressWarnings("unchecked")
						final EList<? extends EObject> targets = (EList<? extends EObject>) currObj.eGet(currRef,
								false);
						for (EObject currTarget : targets) {
							if (isDangling(currTarget)) {
								errMsgs.add(getErrorInfoForDanglingEObject(currObj, currRef));
								break;
							}
						}
					} else {
						final EObject target = (EObject) currObj.eGet(currRef, false);
						if (isDangling(target))
							errMsgs.add(getErrorInfoForDanglingEObject(currObj, currRef));
					}
				}
			}
		}
	}
	if (!errMsgs.isEmpty())
		fail("Expected no dangling references, but found the following: " + Joiner.on("; ").join(errMsgs) + ".");
}
 
Example 7
Source File: InstanceImpl.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc --> <!-- end-user-doc -->
 *
 * @generated NOT
 */
@Override
public void add(EStructuralFeature feature, int index, Object value) {
    if (feature instanceof EAttribute) {
        final EAttribute attribute = (EAttribute) feature;
        final AttributeSlot attributeSlot = getCreateAttributeSlot(attribute);
        if (!attribute.isUnique()
                || !attributeSlot.getValues().contains(value)) {
            attributeSlot.getValues().add(index, value);
        }
    } else {
        final EReference reference = (EReference) feature;
        final Instance target = (Instance) value;
        if (reference.isUnique() && contains(reference, target)) {
            return;
        }
        final EReference opposite = reference.getEOpposite();
        if (opposite != null && reference.eContainer() != null) {
            // if opposite is single-valued, unset it before
            if (!opposite.isMany()) {
                target.unset(opposite);
            }
            final ReferenceSlot oppositeSlot = ((InstanceImpl) target)
                    .getCreateReferenceSlot(opposite);
            oppositeSlot.getValues().add(this);
        }
        final ReferenceSlot referenceSlot = getCreateReferenceSlot(reference);
        referenceSlot.getValues().add(index, target);
    }
}
 
Example 8
Source File: ForwardConverter.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether a certain feature should be ignored during conversion.
 */
protected boolean ignore(EStructuralFeature feature) {
	if (feature.isTransient()) {
		if (feature instanceof EReference) {
			final EReference reference = (EReference) feature;
			if (reference.getEOpposite() != null
				&& !reference.getEOpposite().isTransient()) {
				return false;
			}
		}
		return true;
	}
	return false;
}
 
Example 9
Source File: PackageMetaData.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
private void initHasInverse(EReference eReference) {
		/*
		 * This has been implemented manually, but with the assistance of the output of Express2Emf (which dumps all the mismatched inverses in the schema).
		 * Code has been updated for IFC4_ADD2
		 * 
		 */
		boolean hasInverse = false;
		if (eReference.getEOpposite() != null) {
			hasInverse = isInverse(eReference.getEOpposite());
		}
		if (!hasInverse) {
			if (eReference.getEContainingClass().getEPackage() == Ifc2x3tc1Package.eINSTANCE) {
				if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcRelContainedInSpatialStructure_RelatedElements()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcPresentationLayerAssignment_AssignedItems()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcRelAssociates_RelatedObjects()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcTerminatorSymbol_AnnotatedCurve()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcRelReferencedInSpatialStructure_RelatedElements()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcProduct_Representation()) {
					hasInverse = true;
				} else if (eReference == Ifc2x3tc1Package.eINSTANCE.getIfcRelConnectsStructuralActivity_RelatingElement()) {
					hasInverse = true;
				}
			} else if (eReference.getEContainingClass().getEPackage() == Ifc4Package.eINSTANCE) {
				if (eReference == Ifc4Package.eINSTANCE.getIfcExternalReferenceRelationship_RelatedResourceObjects()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelContainedInSpatialStructure_RelatedElements()) {
					hasInverse = true;
					// Removed in IFC4 _after_ IFC4-final
//				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelCoversBldgElements_RelatingBuildingElement()) {
//					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssociatesClassification_RelatingClassification()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcClassificationReference_ReferencedSource()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelDefinesByProperties_RelatedObjects()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssociatesDocument_RelatingDocument()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelReferencedInSpatialStructure_RelatedElements()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelSpaceBoundary_RelatingSpace()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssociatesLibrary_RelatingLibrary()) {
					hasInverse = true;
				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssociatesMaterial_RelatingMaterial()) {
					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelDeclares_RelatedDefinitions()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssociates_RelatedObjects()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssignsToProcess_RelatingProcess()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssignsToProduct_RelatingProduct()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcProduct_Representation()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcShapeAspect_PartOfProductDefinitionShape()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelDefinesByProperties_RelatingPropertyDefinition()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcPresentationLayerAssignment_AssignedItems()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelAssignsToResource_RelatingResource()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcRelConnectsStructuralActivity_RelatingElement()) {
 					hasInverse = true;

 				// New in IFC4 _after_ IFC4-final
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcCoordinateOperation_SourceCRS()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcResourceConstraintRelationship_RelatedResourceObjects()) {
 					hasInverse = true;
 				} else if (eReference == Ifc4Package.eINSTANCE.getIfcResourceApprovalRelationship_RelatedResourceObjects()) {
 					hasInverse = true;
 				}			
			} 
		}
		hasInverseCache.put(eReference, hasInverse);
	}
 
Example 10
Source File: CopyEObjectFeaturesCommand.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param targetElement
 * @param sourceElement
 * 
 */
public static void copyFeatures(EObject sourceElement, EObject targetElement) {
    List<EStructuralFeature> targetFeatures = targetElement.eClass().getEAllStructuralFeatures();
    for (EStructuralFeature feature : sourceElement.eClass().getEAllStructuralFeatures()) {
        if (targetFeatures.contains(feature)) {
            if (feature instanceof EReference) {
                EReference reference = (EReference)feature;
                Object value = sourceElement.eGet(reference);
                if (value instanceof EList<?>) {
                    EList<EObject> sourceList = (EList<EObject>)value;
                    EList<EObject> destList = (EList<EObject>)targetElement.eGet(feature);
                    while (!sourceList.isEmpty()) {
                        EObject referencedItem = sourceList.get(0);
                        sourceList.remove(referencedItem);
                        if (reference.getEOpposite() != null) {
                            referencedItem.eSet(reference.getEOpposite(), targetElement);
                        } else {
                            destList.add(referencedItem);
                        }
                    }
                } else {
                    targetElement.eSet(feature, sourceElement.eGet(feature));
                }
            } else if (feature instanceof EAttribute) {
                targetElement.eSet(feature, sourceElement.eGet(feature));
            }
        }else{//unset other features
            if(feature.isMany()){
                sourceElement.eSet(feature, Collections.emptyList());
            }else{
                sourceElement.eSet(feature, feature.getDefaultValue());
            }
        }
    }


    EObject container = sourceElement.eContainer();
    if (container != null) {
        Object parentFeatureValue = container.eGet(sourceElement.eContainingFeature());
        if (parentFeatureValue instanceof EList<?>) {
            //			int index = ((EList) parentFeatureValue).indexOf(sourceElement);
            ((EList<?>) parentFeatureValue).remove(sourceElement);
            // Add the new element at the same place than the older one
            // ((EList) parentFeatureValue).set(((EList)
            // parentFeatureValue).indexOf(sourceElement), targetElement);
            ((EList) parentFeatureValue).add(/* index, */targetElement);
        } else {
            container.eSet(sourceElement.eContainingFeature(), targetElement);
        }
    }
}