Beispiel #1
0
        private static object Filter(ExpressMetaProperty property, object parentObject)
        {
            //leave out geometry and placement of products
            if (parentObject is Ifc4.Kernel.IfcProduct &&
                (property.PropertyInfo.Name == "Representation" ||
                 property.PropertyInfo.Name == "ObjectPlacement"))
            {
                return(null);
            }

            //leave out representation maps
            if (parentObject is IfcTypeProduct &&
                property.PropertyInfo.Name == "RepresentationMaps")
            {
                return(null);
            }

            //leave out eventual connection geometry
            if (parentObject is IfcRelSpaceBoundary &&
                property.PropertyInfo.Name == "ConnectionGeometry")
            {
                return(null);
            }

            //return the value for anything else
            return(property.PropertyInfo.GetValue(parentObject, null));
        }
Beispiel #2
0
        private int getPropertyValue(IPersistEntity entity, HashSet <ExpressType> type) // labvit: sheetcode
        {
            //int strLength = "ObjectType".Length;
            int retVal = 0;
            ExpressMetaProperty obType = null;

            if (type == null)
            {
                return(0);
            }
            if (type != null && !type.Contains(entity.ExpressType))
            {
                return(0);
            }
            obType = entity.ExpressType.Properties.ToList()[4].Value; // hard coding, it may be realised function below
                                                                      //foreach (var prop in entity.ExpressType.Properties)
                                                                      //    if (string.Compare(prop.Value.Name, "ObjectType", false) == 0)
                                                                      //    {
                                                                      //        obType = prop.Value;
                                                                      //        obType = prop.Value ;
            var ob = obType.PropertyInfo.GetValue(entity);

            if (ob != null)
            {
                retVal = ob.GetHashCode();// GetType().GetProperty("Value").GetValue(ob).GetHashCode();
            }
            //        break;
            //  }

            return(retVal);
        }
Beispiel #3
0
 /// <summary>
 /// Delegate handling the instance's property transformation. By default all properties (relations and data) are
 /// forwarded as they are into the transformation queue.
 /// </summary>
 /// <param name="property">The meta property descriptor</param>
 /// <param name="hostObject">The hosting object</param>
 /// <param name="package">The task's work package</param>
 /// <param name="cp">Cancelable progress monitor</param>
 /// <returns></returns>
 protected virtual object PropertyTransform(ExpressMetaProperty property, object hostObject, T package, CancelableProgressing cp)
 {
     if (cp?.State.IsAboutCancelling ?? false)
     {
         return(null);
     }
     else
     {
         return(property?.PropertyInfo.GetValue(hostObject));
     }
 }
Beispiel #4
0
        private object PropertyTransform(ExpressMetaProperty property, object parentObject)
        {
            //only bring over IsDefinedBy and IsTypedBy inverse relationships which will take over all properties and types
            if (property.EntityAttribute.Order < 0 && CopyPropertySets && !(
                    property.PropertyInfo.Name == nameof(IIfcProduct.IsDefinedBy) ||
                    property.PropertyInfo.Name == nameof(IIfcProduct.IsTypedBy)
                    ))
            {
                return(null);
            }

            return(property.PropertyInfo.GetValue(parentObject, null));
        }
Beispiel #5
0
        private object InsertCopyComponentFilter(ExpressMetaProperty property, object parentObject)
        {
            if (!property.IsInverse)
            {
                return(property.PropertyInfo.GetValue(parentObject, null));
            }

            if (property.Name == "InSystems")
            {
                return(property.PropertyInfo.GetValue(parentObject, null));
            }

            return(null);
        }
        private bool IsIfcProperty(string elementName, out int index, out ExpressMetaProperty prop)
        {
            ExpressType expressType;
            var         xmlEntity = _currentNode as XmlEntity;

            if (xmlEntity != null && !_metadata.TryGetExpressType(elementName.ToUpper(), out expressType))
            {
                var t = _metadata.ExpressType(xmlEntity.Entity);

                foreach (var p in t.Properties.Where(p => p.Value.PropertyInfo.Name == elementName))
                {
                    prop  = p.Value;
                    index = p.Key;
                    return(true);
                }
            }
            prop  = null;
            index = -1;
            return(false);
        }
Beispiel #7
0
        private bool getPropertyValue(IPersistEntity entity, HashSet <ExpressType> type, int comparable) // labvit: sheetcode
        {
            ExpressMetaProperty obType = null;

            if (type == null)
            {
                return(false);
            }
            if (type != null && !type.Contains(entity.ExpressType))
            {
                return(false);
            }
            obType = entity.ExpressType.Properties.ToList()[4].Value;
            var ob = obType.PropertyInfo.GetValue(entity);

            if (ob != null)
            {
                return(comparable == ob.GetHashCode());// GetType().GetProperty("Value").GetValue(ob).GetHashCode();
            }

            return(false);
        }
        private void ReportProp(IPersistEntity entity, ExpressMetaProperty prop, bool verbose)
        {
            var propVal = prop.PropertyInfo.GetValue(entity, null);

            if (propVal == null)
            {
                if (!verbose)
                {
                    return;
                }
                propVal = "<null>";
            }

            if (prop.EntityAttribute.IsEnumerable)
            {
                var propCollection = propVal as System.Collections.IEnumerable;

                if (propCollection != null)
                {
                    var propVals = propCollection.Cast <object>().ToArray();
                    switch (propVals.Length)
                    {
                    case 0:
                        if (!verbose)
                        {
                            return;
                        }
                        _objectProperties.Add(new PropertyItem {
                            Name = prop.PropertyInfo.Name, Value = "<empty>", PropertySetName = "General"
                        });
                        break;

                    case 1:
                        var tmpSingle = GetPropItem(propVals[0]);
                        tmpSingle.Name            = prop.PropertyInfo.Name + "[0]";
                        tmpSingle.PropertySetName = "General";
                        _objectProperties.Add(tmpSingle);
                        break;

                    default:
                        int i = 0;
                        foreach (var item in propVals)
                        {
                            var tmpLoop = GetPropItem(item);
                            tmpLoop.Name            = $"{prop.PropertyInfo.Name}[{i++}]";
                            tmpLoop.PropertySetName = prop.PropertyInfo.Name;
                            _objectProperties.Add(tmpLoop);
                        }
                        break;
                    }
                }
                else
                {
                    if (!verbose)
                    {
                        return;
                    }
                    _objectProperties.Add(new PropertyItem {
                        Name = prop.PropertyInfo.Name, Value = "<not an enumerable>"
                    });
                }
            }
            else
            {
                var tmp = GetPropItem(propVal);
                tmp.Name            = prop.PropertyInfo.Name;
                tmp.PropertySetName = "General";
                _objectProperties.Add(tmp);
            }
        }
Beispiel #9
0
 public XmlMetaProperty(ExpressMetaProperty metaProperty)
 {
     MetaProperty = metaProperty;
 }
 private bool IsCollection(ExpressMetaProperty prop)
 {
     return(typeof(IExpressEnumerable).IsAssignableFrom(prop.PropertyInfo.PropertyType));
 }
Beispiel #11
0
        private void SetPropertyFromString(ExpressMetaProperty property, IPersistEntity entity, string value, int[] pos, Type valueType = null)
        {
            var pIndex = property.EntityAttribute.Order - 1;
            var type   = valueType ?? property.PropertyInfo.PropertyType;

            type = GetNonNullableType(type);
            var propVal = new PropertyValue();

            if (type.IsValueType || type == typeof(string))
            {
                if (typeof(IExpressComplexType).IsAssignableFrom(type))
                {
                    var meta      = _metadata.ExpressType(type);
                    var values    = value.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
                    var underType = meta.UnderlyingComplexType;
                    foreach (var v in values)
                    {
                        IPropertyValue pv;
                        if (InitPropertyValue(underType, v, out pv))
                        {
                            entity.Parse(pIndex, pv, pos);
                        }
                    }
                    return;
                }
                if (type.IsEnum)
                {
                    propVal.Init(value, StepParserType.Enum);
                    entity.Parse(pIndex, propVal, pos);
                    return;
                }

                //handle other value types
                if (typeof(IExpressValueType).IsAssignableFrom(type))
                {
                    var meta = _metadata.ExpressType(type);
                    type = meta.UnderlyingType;
                }
                IPropertyValue pVal;
                if (InitPropertyValue(type, value, out pVal))
                {
                    entity.Parse(pIndex, pVal, pos);
                }
                return;
            }

            //lists of value types will be serialized as lists. If this is not an IEnumerable this is not the case
            if (!typeof(IEnumerable).IsAssignableFrom(type) || !type.IsGenericType)
            {
                throw new XbimParserException("Unexpected enumerable type " + type.Name);
            }

            var genType = type.GetGenericArguments()[0];

            if (genType.IsValueType || genType == typeof(string))
            {
                //handle enumerable of value type and string
                var values = value.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
                foreach (var v in values)
                {
                    SetPropertyFromString(property, entity, v, pos, genType);
                }
                return;
            }

            //rectangular nested lists can also be serialized as attribute if defined in configuration
            if (typeof(IEnumerable).IsAssignableFrom(genType))
            {
                //handle rectangular nested lists (like IfcPointList3D)
                var cardinality = property.EntityAttribute.MaxCardinality;
                if (cardinality != property.EntityAttribute.MinCardinality)
                {
                    throw new XbimParserException(property.Name + " is not rectangular so it can't be serialized as a simple text string");
                }

                var values  = value.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
                var valType = GetNonGenericType(genType);
                if (typeof(IExpressValueType).IsAssignableFrom(valType))
                {
                    var expValType = _metadata.ExpressType(valType);
                    if (expValType == null)
                    {
                        throw new XbimParserException("Unexpected data type " + valType.Name);
                    }
                    valType = expValType.UnderlyingType;
                }

                for (var i = 0; i < values.Length; i++)
                {
                    IPropertyValue pValue;
                    InitPropertyValue(valType, values[i], out pValue);
                    var idx = i / cardinality;
                    entity.Parse(pIndex, pValue, new [] { idx });
                }
            }
        }
Beispiel #12
0
        private void SetPropertyFromElement(ExpressMetaProperty property, IPersistEntity entity, XmlReader input, int[] pos, ExpressType valueType = null)
        {
            var name    = input.LocalName;
            var type    = valueType != null ? valueType.Type : GetNonNullableType(property.PropertyInfo.PropertyType);
            var pIndex  = property.EntityAttribute.Order - 1;
            var expType = valueType ?? GetExpresType(input);

            //select type
            if (typeof(IExpressSelectType).IsAssignableFrom(type) && type.IsInterface)
            {
                //move to inner element which represents the data
                var sDepth = input.Depth;
                while (input.Read())
                {
                    if (input.NodeType == XmlNodeType.EndElement && input.Depth == sDepth)
                    {
                        return;
                    }
                    if (input.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    expType = GetExpresType(input);
                    if (expType == null)
                    {
                        throw new XbimParserException("Unexpected select data type " + name);
                    }

                    if (typeof(IExpressValueType).IsAssignableFrom(expType.Type))
                    {
                        SetPropertyFromElement(property, entity, input, pos, expType);
                        return;
                    }

                    if (typeof(IPersistEntity).IsAssignableFrom(expType.Type))
                    {
                        SetPropertyFromElement(property, entity, input, pos, expType);
                        return;
                    }

                    //this should either be a defined type or entity
                    throw new XbimParserException("Unexpected select data type " + expType.Name);
                }
                return;
            }

            //defined type
            if (typeof(IExpressValueType).IsAssignableFrom(type))
            {
                var cType = expType.UnderlyingComplexType;

                //if it is just a value we can use 'SetPropertyFromAttribute'
                if (type == property.PropertyInfo.PropertyType && !(cType != null && typeof(IPersistEntity).IsAssignableFrom(cType)))
                {
                    var strValue = input.ReadElementContentAsString();
                    SetPropertyFromString(property, entity, strValue, pos);
                    return;
                }

                if (cType != null)
                {
                    var cInnerValueType = typeof(List <>);
                    cInnerValueType = cInnerValueType.MakeGenericType(cType);
                    var cInnerList = Activator.CreateInstance(cInnerValueType) as IList;
                    if (cInnerList == null)
                    {
                        throw new XbimParserException("Initialization of " + cInnerValueType.Name + " failed.");
                    }

                    if (typeof(IPersistEntity).IsAssignableFrom(cType))
                    {
                        if (!input.IsEmptyElement)
                        {
                            var cDepth = input.Depth;
                            while (input.Read())
                            {
                                if (input.NodeType == XmlNodeType.EndElement && input.Depth == cDepth)
                                {
                                    break;
                                }
                                if (input.NodeType != XmlNodeType.Element)
                                {
                                    continue;
                                }
                                var innerEntity = ReadEntity(input);
                                cInnerList.Add(innerEntity);
                                if (input.NodeType == XmlNodeType.EndElement && input.Depth == cDepth)
                                {
                                    break;
                                }
                            }

                            var cValueVal = Activator.CreateInstance(expType.Type, cInnerList);
                            var cpValue   = new PropertyValue();
                            cpValue.Init(cValueVal);
                            entity.Parse(pIndex, cpValue, null);
                        }
                    }
                    else
                    {
                        var cValuesString = input.ReadElementContentAsString();
                        SetPropertyFromString(property, entity, cValuesString, pos);
                    }
                    return;
                }

                //normal defined type has string based constructor which will set the right value
                var sValue = input.ReadElementContentAsString();
                if (property.EnumerableType == expType.Type)
                {
                    IPropertyValue pValue;
                    InitPropertyValue(expType.UnderlyingType, sValue, out pValue);
                    entity.Parse(pIndex, pValue, pos);
                    return;
                }
                else
                {
                    var pValue    = new PropertyValue();
                    var pValueVal = Activator.CreateInstance(expType.Type, sValue);
                    pValue.Init(pValueVal);
                    entity.Parse(pIndex, pValue, pos);
                    return;
                }
            }

            if (typeof(IPersistEntity).IsAssignableFrom(type) ||
                (typeof(IEnumerable).IsAssignableFrom(type) && property.EntityAttribute.MaxCardinality == 1))
            {
                var value = ReadEntity(input);
                var pVal  = new PropertyValue();

                if (property.IsInverse)
                {
                    pVal.Init(entity);
                    var remotePropName = property.InverseAttributeProperty.RemoteProperty;
                    var remoteProperty = value.ExpressType.Properties.FirstOrDefault(p => p.Value.Name == remotePropName).Value;
                    if (remoteProperty == null)
                    {
                        throw new XbimParserException("Non existing counterpart to " + property.Name);
                    }
                    value.Parse(remoteProperty.EntityAttribute.Order - 1, pVal, null);
                }
                else
                {
                    pVal.Init(value);
                    entity.Parse(pIndex, pVal, null);
                }
                return;
            }

            //enumeration or inverse enumeration
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                //do nothing with empty list. If it is mandatory it is initialized anyway
                if (input.IsEmptyElement)
                {
                    return;
                }

                var enumDepth = input.Depth;
                while (input.Read())
                {
                    if (input.NodeType == XmlNodeType.EndElement && input.Depth == enumDepth)
                    {
                        break;
                    }
                    if (input.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    //position is optional
                    var posAttr = input.GetAttribute("pos");
                    pos = null;
                    if (!string.IsNullOrWhiteSpace(posAttr))
                    {
                        var idx = posAttr.Split(_separator, StringSplitOptions.RemoveEmptyEntries)
                                  .Select(i => Convert.ToInt32(i)).ToList();
                        //remove the last one as it is not used in Parse function
                        if (idx.Count > 0)
                        {
                            idx.RemoveAt(idx.Count - 1);
                        }
                        //only set if it has any values in
                        if (idx.Count > 0)
                        {
                            pos = idx.ToArray();
                        }
                    }

                    //it might be a primitive
                    name = input.LocalName;
                    if (Primitives.ContainsKey(input.LocalName))
                    {
                        var iVal = input.ReadElementContentAsString();
                        var pVal = new PropertyValue();
                        pVal.Init(iVal, Primitives[name]);
                        entity.Parse(pIndex, pVal, pos);
                        if (input.NodeType == XmlNodeType.EndElement && input.Depth == enumDepth)
                        {
                            break;
                        }
                        continue;
                    }

                    var eType = GetExpresType(input);
                    if (eType == null)
                    {
                        throw new XbimParserException("Unexpected type " + name);
                    }
                    SetPropertyFromElement(property, entity, input, pos, eType);
                    if (input.NodeType == XmlNodeType.EndElement && input.Depth == enumDepth)
                    {
                        break;
                    }
                }
                return;
            }
            throw new XbimParserException("Unexpected type: " + type.Name);
        }
Beispiel #13
0
        protected static IEnumerable <ValidationResult> GetSchemaErrors(IPersist instance, ExpressMetaProperty prop, ValidationFlags validateLevel, bool hierarchical)
        {
            var attr     = prop.EntityAttribute;
            var propVal  = prop.PropertyInfo.GetValue(instance, null);
            var propName = prop.PropertyInfo.Name;

            if (propVal is IExpressValueType)
            {
                var val            = ((IExpressValueType)propVal).Value;
                var underlyingType = ((IExpressValueType)propVal).UnderlyingSystemType;
                if (attr.State == EntityAttributeState.Mandatory && val == null && underlyingType != typeof(bool?))
                {
                    yield return(new ValidationResult()
                    {
                        Item = instance,
                        IssueType = ValidationFlags.Properties,
                        IssueSource = propName,
                        Message = string.Format("{0}.{1} is not optional.", instance.GetType().Name, propName)
                    });
                }
                if (validateLevel.HasFlag(ValidationFlags.TypeWhereClauses) && propVal is IExpressValidatable)
                {
                    var hierResult = new ValidationResult()
                    {
                        Item = instance, IssueType = ValidationFlags.None
                    };
                    foreach (var issue in ((IExpressValidatable)propVal).Validate())
                    {
                        if (hierarchical)
                        {
                            hierResult.AddDetail(issue);
                        }
                        else
                        {
                            yield return(issue);
                        }
                    }
                    if (hierarchical && hierResult.IssueType != ValidationFlags.None)
                    {
                        // the IssueType is populated if any children have been added.
                        hierResult.Message = string.Format("Property {0} has validation failures.", prop.Name);
                        yield return(hierResult);
                    }
                }
                yield break;
            }

            if (attr.State == EntityAttributeState.Mandatory && propVal == null)
            {
                yield return new ValidationResult()
                       {
                           Item        = instance,
                           IssueType   = ValidationFlags.Properties,
                           IssueSource = propName,
                           Message     = string.Format("{0}.{1} is not optional.", instance.GetType().Name, propName)
                       }
            }
            ;
            if (attr.State == EntityAttributeState.Optional && propVal == null)
            {
                //if it is null and optional then it is ok
                yield break;
            }
            if (attr.State == EntityAttributeState.Optional && propVal is IOptionalItemSet && !((IOptionalItemSet)propVal).Initialized)
            {
                //if it is non-initialized list and optional then it is ok
                yield break;
            }
            if (attr.IsEnumerable)
            {
                if (
                    (attr.MinCardinality == null || attr.MinCardinality.Length == 0 || attr.MinCardinality.All(c => c < 0)) &&
                    (attr.MaxCardinality == null || attr.MaxCardinality.Length == 0 || attr.MaxCardinality.All(c => c < 1))) //we don't care how many so don't check
                {
                    yield break;
                }

                var depth = attr.MinCardinality.Length;
                if (depth != attr.MaxCardinality.Length)
                {
                    throw new System.Exception("Inconsistent metadata: minimal and maximal cardinality has to have the same length.");
                }

                var sb    = new StringBuilder();
                var items = (IEnumerable)propVal;
                CheckCardinality(attr.MinCardinality, attr.MaxCardinality, items, 0, sb);
                var msg = sb.ToString();
                if (string.IsNullOrWhiteSpace(msg))
                {
                    yield break;
                }

                yield return(new ValidationResult()
                {
                    Item = instance,
                    IssueType = ValidationFlags.Properties,
                    IssueSource = propName,
                    Message = string.Format("{0}.{1}: {2}", instance.GetType().Name, prop.Name, msg)
                });
            }
        }
Beispiel #14
0
        private static object Filter(CopyContext context, ExpressMetaProperty property, object parentObject)
        {
            //ignore inverses except for style
            if (property.IsInverse)
            {
                return(property.Name == "StyledByItem" ? property.PropertyInfo.GetValue(parentObject, null) : null);
            }

            if (context.PrimaryElements != null && context.PrimaryElements.Any())
            {
                if (typeof(IIfcProduct).IsAssignableFrom(property.PropertyInfo.PropertyType))
                {
                    var element = property.PropertyInfo.GetValue(parentObject, null) as IIfcProduct;
                    if (element != null && context.PrimaryElements.Contains(element))
                    {
                        return(element);
                    }
                    return(null);
                }
                if (property.EnumerableType != null && !property.EnumerableType.IsValueType && property.EnumerableType != typeof(string))
                {
                    //this can either be a list of IPersistEntity or select type. The very base type is IPersist
                    var entities = property.PropertyInfo.GetValue(parentObject, null) as IEnumerable <IPersist>;
                    if (entities != null)
                    {
                        var persistEntities  = entities as IList <IPersist> ?? entities.ToList();
                        var elementsToRemove = persistEntities.OfType <IIfcProduct>().Where(e => !context.PrimaryElements.Contains(e)).ToList();
                        //if there are no IfcElements return what is in there with no care
                        if (elementsToRemove.Any())
                        {
                            //return original values excluding elements not included in the primary set
                            return(persistEntities.Except(elementsToRemove).ToList());
                        }
                    }
                }
            }

            //if geometry is to be included don't filter it out
            if (context.IncludeGeometry)
            {
                return(property.PropertyInfo.GetValue(parentObject, null));
            }

            //leave out geometry and placement of products
            if (parentObject is IIfcProduct &&
                (property.PropertyInfo.Name == "Representation" || property.PropertyInfo.Name == "ObjectPlacement")
                )
            {
                return(null);
            }

            //leave out representation maps
            if (parentObject is IIfcTypeProduct && property.PropertyInfo.Name == "RepresentationMaps")
            {
                return(null);
            }

            //leave out eventual connection geometry
            if (parentObject is IIfcRelSpaceBoundary && property.PropertyInfo.Name == "ConnectionGeometry")
            {
                return(null);
            }

            //return the value for anything else
            return(property.PropertyInfo.GetValue(parentObject, null));
        }
Beispiel #15
0
        private static object GetFieldValue(IPersistEntity entity, string attributeName, out ExpressMetaProperty prop)
        {
            var entType = entity.ExpressType;

            prop = entType.Properties.FirstOrDefault(x => x.Value.PropertyInfo.Name == attributeName).Value  // test direct
                   ?? entType.Inverses.FirstOrDefault(x => x.PropertyInfo.Name == attributeName);            // otherwise inverses
            var propVal = prop?.PropertyInfo.GetValue(entity, null);

            return(propVal);
        }