Skip to content

Commit

Permalink
Merge pull request #733 from hlg/master
Browse files Browse the repository at this point in the history
Inverse fixing in non-streaming STEP deserialzer
  • Loading branch information
rubendel authored Mar 30, 2018
2 parents a5ad963 + 37c155b commit e0b4304
Showing 1 changed file with 138 additions and 56 deletions.
194 changes: 138 additions & 56 deletions Shared/src/org/bimserver/ifc/IfcModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*****************************************************************************/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -32,29 +31,19 @@
import java.util.Set;
import java.util.TreeMap;
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.plugins.ObjectAlreadyExistsException;
import org.bimserver.plugins.objectidms.ObjectIDM;
import org.bimserver.shared.exceptions.PublicInterfaceNotFoundException;
import org.bimserver.shared.exceptions.ServerException;
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;
Expand Down Expand Up @@ -974,43 +963,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<? extends IdEObject>) relation.eGet(attribute)) {
if (isInstanceOfAnyOf(related, fix.subclasses)) {
((EList<IdEObject>) related.eGet(related.eClass().getEStructuralFeature(fix.inverse))).add(relation);
nrFixes++;
}
} else {
if (isInstanceOfAnyOf((IdEObject) relation.eGet(attribute), fix.subclasses)){
((EList<IdEObject>) 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<? extends IdEObject>) 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<String>() {
@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<IdEObject>) referenced.eGet(inverse)).add(entityToBeFixed);
else referenced.eSet(inverse, entityToBeFixed);
nrFixes++;
}
return nrFixes;
}

@Override
Expand Down Expand Up @@ -1049,19 +1136,14 @@ public Set<EClass> 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 extends IdEObject> T getFirst(Class<T> class1) {
return getAll(class1).iterator().next();
Expand Down

0 comments on commit e0b4304

Please sign in to comment.