From 9cc62375c15b47fe8f2f96600df421bdd6370a92 Mon Sep 17 00:00:00 2001 From: hlg Date: Tue, 27 Mar 2018 13:07:08 +0800 Subject: [PATCH 1/2] fix inverses in non-streaming STEP deserializer, resolves #728 * update for IFC4 (taken from PackageMetadata, same as for streaming serializer) * prevent typos by using EPackage methods --- Shared/src/org/bimserver/ifc/IfcModel.java | 193 +++++++++++++++------ 1 file changed, 139 insertions(+), 54 deletions(-) diff --git a/Shared/src/org/bimserver/ifc/IfcModel.java b/Shared/src/org/bimserver/ifc/IfcModel.java index 1e4c3158f5..9e0f3f5bd5 100644 --- a/Shared/src/org/bimserver/ifc/IfcModel.java +++ b/Shared/src/org/bimserver/ifc/IfcModel.java @@ -34,14 +34,11 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.function.Predicate; -import org.bimserver.emf.IdEObject; -import org.bimserver.emf.IdEObjectImpl; +import org.bimserver.emf.*; import org.bimserver.emf.IdEObjectImpl.State; -import org.bimserver.emf.IfcModelInterface; -import org.bimserver.emf.IfcModelInterfaceException; -import org.bimserver.emf.ModelMetaData; -import org.bimserver.emf.OidProvider; -import org.bimserver.emf.PackageMetaData; +import org.bimserver.models.ifc2x3tc1.Ifc2x3tc1Package; +import org.bimserver.models.ifc4.Ifc4Package; +import org.bimserver.models.ifc4.impl.Ifc4PackageImpl; import org.bimserver.plugins.ObjectAlreadyExistsException; import org.bimserver.plugins.objectidms.ObjectIDM; import org.bimserver.shared.exceptions.PublicInterfaceNotFoundException; @@ -49,12 +46,7 @@ import org.bimserver.shared.exceptions.UserException; import org.eclipse.emf.common.util.ECollections; import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.ecore.EAttribute; -import org.eclipse.emf.ecore.EClass; -import org.eclipse.emf.ecore.EClassifier; -import org.eclipse.emf.ecore.EReference; -import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.*; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -974,43 +966,141 @@ public PackageMetaData getPackageMetaData() { @Override public void fixInverseMismatches() { int nrFixes = 0; - Fix[] fixes = new Fix[]{ - new Fix("IfcRelContainedInSpatialStruture", "relatedElements", "containedInStructure", new String[]{"IfcElement", "IfcAnnotation", "IfcGrid"}), - new Fix("IfcPresentationLayerAssignment", "assignedItems", "layerAssignments", new String[]{"IfcRepresentation", "IfcRepresentationItem"}), - new Fix("IfcRelAssociates", "relatedObjects", "hasAssociations", new String[]{"IfcObjectDefinition", "IfcPropertyDefinition"}), - new Fix("IfcTerminatorSymbol", "annotatedCurve", "annotatedBySymbol", new String[]{"IfcDimensionCurve"}), - new Fix("IfcRelReferencedInSpatialStructure", "relatedElements", "referencedInStructure", new String[]{"IfcElement"}), - new Fix("IfcProduct", "representation", "shapeOfProduct", new String[]{"IfcProductDefinitionShape"}), - new Fix("IfcRelConnectsStructuralActivity", "relatingElement", "assignedStructuralActivity", new String[]{"IfcStructuralItem"}) + Ifc4Package ifc4 = Ifc4Package.eINSTANCE; + Ifc2x3tc1Package ifc2x3 = Ifc2x3tc1Package.eINSTANCE; + Mismatch[] misMatches = packageMetaData.getSchema().equals(Schema.IFC2X3TC1) ? new Mismatch[]{ + new Mismatch(ifc2x3.getIfcRelContainedInSpatialStructure_RelatedElements(), new EReference[]{ + ifc2x3.getIfcElement_ContainedInStructure(), + ifc2x3.getIfcGrid_ContainedInStructure(), + ifc2x3.getIfcAnnotation_ContainedInStructure()}), + new Mismatch(ifc2x3.getIfcPresentationLayerAssignment_AssignedItems(), new EReference[]{ + ifc2x3.getIfcRepresentation_LayerAssignments(), + ifc2x3.getIfcRepresentationItem_LayerAssignments() }), + new Mismatch(ifc2x3.getIfcRelAssociates_RelatedObjects(), new EReference[]{ + ifc2x3.getIfcObjectDefinition_HasAssociations(), + ifc2x3.getIfcPropertyDefinition_HasAssociations() }), + new Mismatch(ifc2x3.getIfcTerminatorSymbol_AnnotatedCurve(), new EReference[]{ + ifc2x3.getIfcDimensionCurve_AnnotatedBySymbols() }), + new Mismatch(ifc2x3.getIfcRelReferencedInSpatialStructure_RelatedElements(), new EReference[]{ + ifc2x3.getIfcElement_ReferencedInStructures() }), + new Mismatch(ifc2x3.getIfcProduct_Representation(), new EReference[]{ + ifc2x3.getIfcProductDefinitionShape_ShapeOfProduct() }), + new Mismatch(ifc2x3.getIfcRelConnectsElements_RelatingElement(), new EReference[]{ + ifc2x3.getIfcStructuralItem_AssignedStructuralActivity()}) + } : new Mismatch[]{ + new Mismatch(ifc4.getIfcRelContainedInSpatialStructure_RelatedElements(), new EReference[]{ + ifc4.getIfcElement_ContainedInStructure(), + ifc4.getIfcGrid_ContainedInStructure(), + ifc4.getIfcAnnotation_ContainedInStructure()}), + new Mismatch(ifc4.getIfcPresentationLayerAssignment_AssignedItems(), new EReference[]{ + ifc4.getIfcRepresentation_LayerAssignments(), + ifc4.getIfcRepresentationItem_LayerAssignment() }), + new Mismatch(ifc4.getIfcRelAssociates_RelatedObjects(), new EReference[]{ + ifc4.getIfcObjectDefinition_HasAssociations(), + ifc4.getIfcPropertyDefinition_HasAssociations() }), + new Mismatch(ifc4.getIfcRelReferencedInSpatialStructure_RelatedElements(), new EReference[]{ + ifc4.getIfcElement_ReferencedInStructures() }), + new Mismatch(ifc4.getIfcProduct_Representation(), new EReference[]{ + ifc4.getIfcProductDefinitionShape_ShapeOfProduct() }), + new Mismatch(ifc4.getIfcRelConnectsElements_RelatingElement(), new EReference[]{ + ifc4.getIfcStructuralItem_AssignedStructuralActivity()}), + new Mismatch(ifc4.getIfcExternalReferenceRelationship_RelatedResourceObjects(), new EReference[]{ + ifc4.getIfcActorRole_HasExternalReference(), + ifc4.getIfcAppliedValue_HasExternalReference(), + ifc4.getIfcApproval_HasExternalReferences(), + ifc4.getIfcConstraint_HasExternalReferences(), + ifc4.getIfcContextDependentUnit_HasExternalReference(), + ifc4.getIfcConversionBasedUnit_HasExternalReference(), + ifc4.getIfcMaterialDefinition_HasExternalReferences(), + ifc4.getIfcPhysicalQuantity_HasExternalReferences(), + ifc4.getIfcProfileDef_HasExternalReference(), + ifc4.getIfcPropertyAbstraction_HasExternalReferences(), + ifc4.getIfcTimeSeries_HasExternalReference() + }), + new Mismatch(ifc4.getIfcRelAssociatesClassification_RelatingClassification(), new EReference[]{ + ifc4.getIfcClassification_ClassificationForObjects(), + ifc4.getIfcClassificationReference_ClassificationRefForObjects() + }), + new Mismatch(ifc4.getIfcClassificationReference_ReferencedSource(),new EReference[]{ + ifc4.getIfcClassification_HasReferences(), + ifc4.getIfcClassificationReference_HasReferences() + }), + new Mismatch(ifc4.getIfcRelDefinesByProperties_RelatedObjects(),new EReference[]{ + ifc4.getIfcContext_IsDefinedBy(), + ifc4.getIfcObject_IsDefinedBy() + }), + new Mismatch(ifc4.getIfcRelAssociatesDocument_RelatingDocument(),new EReference[]{ + ifc4.getIfcDocumentInformation_DocumentInfoForObjects(), + ifc4.getIfcDocumentReference_DocumentRefForObjects() + }), + new Mismatch(ifc4.getIfcRelSpaceBoundary_RelatingSpace(),new EReference[]{ + ifc4.getIfcExternalSpatialElement_BoundedBy(), + ifc4.getIfcSpace_BoundedBy() + }), + new Mismatch(ifc4.getIfcRelAssociatesLibrary_RelatingLibrary(),new EReference[]{ + ifc4.getIfcLibraryInformation_LibraryInfoForObjects(), + ifc4.getIfcLibraryReference_LibraryRefForObjects() + }), + new Mismatch(ifc4.getIfcRelAssociatesMaterial_RelatingMaterial(),new EReference[]{ + ifc4.getIfcMaterialDefinition_AssociatedTo(), + ifc4.getIfcMaterialUsageDefinition_AssociatedTo() + }), + new Mismatch(ifc4.getIfcRelDeclares_RelatedDefinitions(),new EReference[]{ + ifc4.getIfcObjectDefinition_HasContext(), + ifc4.getIfcPropertyDefinition_HasContext() + }), + new Mismatch(ifc4.getIfcRelAssignsToProcess_RelatingProcess(),new EReference[]{ + ifc4.getIfcProcess_OperatesOn(), + ifc4.getIfcTypeProcess_OperatesOn() + }), + new Mismatch(ifc4.getIfcShapeAspect_PartOfProductDefinitionShape(),new EReference[]{ + ifc4.getIfcProductDefinitionShape_HasShapeAspects(), + ifc4.getIfcRepresentationMap_HasShapeAspects() + }), + new Mismatch(ifc4.getIfcRelDefinesByProperties_RelatingPropertyDefinition(),new EReference[]{ + ifc4.getIfcPropertySetDefinition_DefinesOccurrence() + }), + new Mismatch(ifc4.getIfcRelAssignsToResource_RelatingResource(),new EReference[]{ + ifc4.getIfcResource_ResourceOf(), + ifc4.getIfcTypeResource_ResourceOf() + }), + new Mismatch(ifc4.getIfcCoordinateOperation_SourceCRS(),new EReference[]{ + ifc4.getIfcCoordinateReferenceSystem_HasCoordinateOperation(), + ifc4.getIfcGeometricRepresentationContext_HasCoordinateOperation() + }), + new Mismatch(ifc4.getIfcRelAssignsToProduct_RelatingProduct(),new EReference[]{ + ifc4.getIfcProduct_ReferencedBy() + }), + new Mismatch(ifc4.getIfcResourceConstraintRelationship_RelatedResourceObjects(),new EReference[]{ + ifc4.getIfcProperty_HasConstraints() + }), + new Mismatch(ifc4.getIfcResourceApprovalRelationship_RelatedResourceObjects(),new EReference[]{ + ifc4.getIfcProperty_HasApprovals() + }) }; - for(Fix fix : fixes) { - EClass relationClass = getPackageMetaData().getEClass(fix.relation); - for (IdEObject relation : getAllWithSubTypes(relationClass)) { - EStructuralFeature attribute = relationClass.getEStructuralFeature(fix.attribute); - if(attribute.isMany()) for (IdEObject related : (EList) relation.eGet(attribute)) { - if (isInstanceOfAnyOf(related, fix.subclasses)) { - ((EList) related.eGet(related.eClass().getEStructuralFeature(fix.inverse))).add(relation); - nrFixes++; - } - } else { - if (isInstanceOfAnyOf((IdEObject) relation.eGet(attribute), fix.subclasses)){ - ((EList) relation.eGet(relation.eClass().getEStructuralFeature(fix.inverse))).add(relation); - nrFixes++; - } - } + + + for(Mismatch mismatch: misMatches){ + for(IdEObject entityToBeFixed : getAllWithSubTypes(mismatch.forward.getEContainingClass())){ + Object referenced = entityToBeFixed.eGet(mismatch.forward); + if(referenced != null) + if (mismatch.forward.isMany()) for (IdEObject referencedInList : (EList) referenced){ + nrFixes += fixMisMatchInstance(mismatch, entityToBeFixed, referencedInList); + } else + nrFixes += fixMisMatchInstance(mismatch, entityToBeFixed, (IdEObject) referenced); } } LOGGER.info("Nr inverse fixes: " + nrFixes); } - private boolean isInstanceOfAnyOf(IdEObject idEObject, String[] classes) { - return Arrays.stream(classes).anyMatch(new Predicate() { - @Override - public boolean test(String s) { - EClass subClass = getPackageMetaData().getEClass(s); - return subClass.isInstance(idEObject); - } - }); + private int fixMisMatchInstance(Mismatch mismatch, IdEObject entityToBeFixed, IdEObject referenced) { + int nrFixes = 0; + for(EReference inverse: mismatch.inverse) if(inverse.getEContainingClass().isInstance(referenced)){ + if(inverse.isMany()) ((EList) referenced.eGet(inverse)).add(entityToBeFixed); + else referenced.eSet(inverse, entityToBeFixed); + nrFixes++; + } + return nrFixes; } @Override @@ -1049,19 +1139,14 @@ public Set getUsedClasses() { @Override public void query(ObjectNode query) { } - class Fix { - String relation; - String attribute; - String inverse; - String[] subclasses; - Fix(String relation, String attribute, String inverse, String[] subclasses){ - this.relation = relation; + class Mismatch { + EReference forward; + EReference[] inverse; + Mismatch(EReference forward, EReference[] inverse){ + this.forward = forward; this.inverse = inverse; - this.subclasses = subclasses; } - } - @Override public T getFirst(Class class1) { return getAll(class1).iterator().next(); From 37c155b94aadb3f8a1fdc4dd692666cbbacb8a1c Mon Sep 17 00:00:00 2001 From: hlg Date: Wed, 28 Mar 2018 11:25:28 +0800 Subject: [PATCH 2/2] remove unused imports --- Shared/src/org/bimserver/ifc/IfcModel.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Shared/src/org/bimserver/ifc/IfcModel.java b/Shared/src/org/bimserver/ifc/IfcModel.java index 9e0f3f5bd5..4e197f993c 100644 --- a/Shared/src/org/bimserver/ifc/IfcModel.java +++ b/Shared/src/org/bimserver/ifc/IfcModel.java @@ -18,7 +18,6 @@ *****************************************************************************/ import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -32,13 +31,11 @@ import java.util.Set; import java.util.TreeMap; import java.util.concurrent.LinkedBlockingQueue; -import java.util.function.Predicate; import org.bimserver.emf.*; import org.bimserver.emf.IdEObjectImpl.State; import org.bimserver.models.ifc2x3tc1.Ifc2x3tc1Package; import org.bimserver.models.ifc4.Ifc4Package; -import org.bimserver.models.ifc4.impl.Ifc4PackageImpl; import org.bimserver.plugins.ObjectAlreadyExistsException; import org.bimserver.plugins.objectidms.ObjectIDM; import org.bimserver.shared.exceptions.PublicInterfaceNotFoundException;