Search in sources :

Example 36 with EDataType

use of org.eclipse.emf.ecore.EDataType in project xtext-core by eclipse.

the class XtextValidator method checkCrossReferenceTerminal.

public boolean checkCrossReferenceTerminal(RuleCall call) {
    if (call.getRule() != null && call.getRule().getType() != null) {
        EClassifier type = call.getRule().getType().getClassifier();
        EDataType dataType = GrammarUtil.findEString(GrammarUtil.getGrammar(call));
        if (dataType == null)
            dataType = EcorePackage.Literals.ESTRING;
        if (type != null && dataType != type) {
            error("The rule '" + call.getRule().getName() + "' is not valid for a cross reference since it does not return " + "an EString. You'll have to wrap it in a data type rule.", call, null, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
            return true;
        }
    }
    return false;
}
Also used : EDataType(org.eclipse.emf.ecore.EDataType) EClassifier(org.eclipse.emf.ecore.EClassifier)

Example 37 with EDataType

use of org.eclipse.emf.ecore.EDataType in project xtext-core by eclipse.

the class Xtext2EcoreTransformer method deriveTypeHierarchyFromOverridden.

private boolean deriveTypeHierarchyFromOverridden(ParserRule rule, Grammar grammar) throws TransformationException {
    AbstractRule parentRule = GrammarUtil.findRuleForName(grammar, rule.getName());
    if (parentRule != null) {
        if (parentRule != rule && parentRule instanceof ParserRule) {
            ParserRule casted = (ParserRule) parentRule;
            if (casted.isFragment() != rule.isFragment()) {
                if (rule.isFragment()) {
                    throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride, "A fragment rule cannot override a production rule.", rule);
                } else {
                    throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride, "Only fragment rule can override other fragment rules.", rule);
                }
            }
            if (casted.isWildcard() != rule.isWildcard()) {
                if (rule.isWildcard()) {
                    throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride, "A wildcard fragment rule cannot override a typed fragment rule.", rule);
                } else {
                    throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride, "Only wildcard fragment rules can override other wildcard fragments.", rule);
                }
            }
            if (rule.isFragment() && !rule.isWildcard() && parentRule.getType() != null) {
                if (rule.getType().getClassifier() != parentRule.getType().getClassifier()) {
                    throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride, "Overriding fragment rules cannot redeclare their type.", rule.getType());
                }
            }
            checkParameterLists(rule, casted);
        }
        if (parentRule.getType() != null && parentRule != rule) {
            if (parentRule.getType().getClassifier() instanceof EDataType)
                throw new TransformationException(TransformationErrorCode.InvalidSupertype, "Cannot inherit from datatype rule and return another type.", rule.getType());
            EClassifierInfo parentTypeInfo = eClassifierInfos.getInfoOrNull(parentRule.getType());
            if (parentTypeInfo == null)
                throw new TransformationException(TransformationErrorCode.InvalidSupertype, "Cannot determine return type of overridden rule.", rule.getType());
            addSuperType(rule, rule.getType(), parentTypeInfo);
            return true;
        }
    }
    return false;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EDataType(org.eclipse.emf.ecore.EDataType) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 38 with EDataType

use of org.eclipse.emf.ecore.EDataType in project BIMserver by opensourceBIM.

the class DatabaseSession method convertObjectToByteArray.

private ByteBuffer convertObjectToByteArray(IdEObject object, ByteBuffer buffer, PackageMetaData packageMetaData) throws BimserverDatabaseException {
    if (object.getUuid() == null) {
        throw new BimserverDatabaseException("UUID is required " + object.eClass().getName());
    }
    int bufferSize = getExactSize(object, packageMetaData, true);
    if (bufferSize > buffer.capacity()) {
        LOGGER.debug("Buffer too small (" + bufferSize + ")");
        buffer = ByteBuffer.allocate(bufferSize);
    }
    int unsettedLength = packageMetaData.getUnsettedLength(object.eClass());
    byte[] unsetted = new byte[unsettedLength];
    int fieldCounter = 0;
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (useUnsetBit(feature, object)) {
                unsetted[fieldCounter / 8] |= (1 << (fieldCounter % 8));
            }
            fieldCounter++;
        }
    }
    buffer.put(unsetted);
    EClass eClass = getEClassForOid(object.getOid());
    if (!eClass.isSuperTypeOf(object.eClass())) {
        throw new BimserverDatabaseException("Object with oid " + object.getOid() + " is a " + object.eClass().getName() + " but it's cid-part says it's a " + eClass.getName());
    }
    buffer.putLong(object.getUuid().getMostSignificantBits());
    buffer.putLong(object.getUuid().getLeastSignificantBits());
    for (EStructuralFeature feature : object.eClass().getEAllStructuralFeatures()) {
        if (packageMetaData.useForDatabaseStorage(object.eClass(), feature)) {
            if (!useUnsetBit(feature, object)) {
                if (feature.isMany()) {
                    writeList(object, buffer, packageMetaData, feature);
                } else {
                    Object value = object.eGet(feature);
                    if (feature.getEType() instanceof EEnum) {
                        if (value == null) {
                            buffer.putInt(-1);
                        } else {
                            EEnum eEnum = (EEnum) feature.getEType();
                            EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
                            if (eEnumLiteral != null) {
                                buffer.putInt(eEnumLiteral.getValue());
                            } else {
                                LOGGER.error(((Enum<?>) value).toString() + " not found");
                                buffer.putInt(-1);
                            }
                        }
                    } else if (feature.getEType() instanceof EClass) {
                        if (value == null) {
                            buffer.order(ByteOrder.LITTLE_ENDIAN);
                            buffer.putShort((short) -1);
                            buffer.order(ByteOrder.BIG_ENDIAN);
                        } else {
                            IdEObject referencedObject = (IdEObject) value;
                            EClass referencedClass = referencedObject.eClass();
                            if (feature.getEAnnotation("dbembed") != null) {
                                writeEmbeddedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else if (referencedClass.getEAnnotation("wrapped") != null) {
                                writeWrappedValue(object.getPid(), object.getRid(), value, buffer, packageMetaData);
                            } else {
                                writeReference(object, value, buffer, feature);
                            }
                        }
                    } else if (feature.getEType() instanceof EDataType) {
                        writePrimitiveValue(feature, value, buffer);
                    }
                }
            }
        }
    }
    if (buffer.position() != bufferSize) {
        throw new BimserverDatabaseException("Value buffer sizes do not match for " + object.eClass().getName() + " " + buffer.position() + "/" + bufferSize);
    }
    return buffer;
}
Also used : EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) EClass(org.eclipse.emf.ecore.EClass) IdEObject(org.bimserver.emf.IdEObject) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) EEnum(org.eclipse.emf.ecore.EEnum)

Example 39 with EDataType

use of org.eclipse.emf.ecore.EDataType in project BIMserver by opensourceBIM.

the class DatabaseSession method convertByteArrayToObject.

@SuppressWarnings({ "unused" })
private IdEObject convertByteArrayToObject(IdEObject idEObject, EClass originalQueryClass, EClass eClass, long oid, ByteBuffer buffer, IfcModelInterface model, int rid, QueryInterface query, TodoList todoList) throws BimserverDatabaseException {
    try {
        if (idEObject == null) {
            idEObject = createInternal(eClass, query);
            ((IdEObjectImpl) idEObject).setOid(oid);
            ((IdEObjectImpl) idEObject).setPid(query.getPid());
            if (rid == Integer.MAX_VALUE) {
                throw new BimserverDatabaseException("Database corrupt, rid cannot be " + Integer.MAX_VALUE);
            }
        }
        if (idEObject.eClass().getEAnnotation("wrapped") == null) {
            try {
                model.addAllowMultiModel(oid, idEObject);
            } catch (IfcModelInterfaceException e) {
                throw new BimserverDatabaseException(e);
            }
        }
        ((IdEObjectImpl) idEObject).setRid(rid);
        ((IdEObjectImpl) idEObject).useInverses(false);
        if (DEVELOPER_DEBUG && StorePackage.eINSTANCE == idEObject.eClass().getEPackage()) {
            LOGGER.info("Read: " + idEObject.eClass().getName() + " pid=" + query.getPid() + " oid=" + oid + " rid=" + rid);
        }
        ((IdEObjectImpl) idEObject).setLoadingState(State.LOADING);
        objectCache.put(oid, idEObject);
        int unsettedLength = model.getPackageMetaData().getUnsettedLength(eClass);
        byte[] unsetted = new byte[unsettedLength];
        buffer.get(unsetted);
        int fieldCounter = 0;
        ((IdEObjectImpl) idEObject).setUuid(new UUID(buffer.getLong(), buffer.getLong()));
        for (EStructuralFeature feature : eClass.getEAllStructuralFeatures()) {
            try {
                if (model.getPackageMetaData().useForDatabaseStorage(eClass, feature)) {
                    boolean isUnsetted = (unsetted[fieldCounter / 8] & (1 << (fieldCounter % 8))) != 0;
                    if (isUnsetted) {
                        if (feature.isUnsettable()) {
                            idEObject.eUnset(feature);
                        } else if (feature.isMany()) {
                        // do nothing
                        } else if (feature.getDefaultValue() != null) {
                            idEObject.eSet(feature, feature.getDefaultValue());
                        }
                    } else {
                        if (false) {
                            // we have to do some reading to maintain a correct
                            // index
                            fakeRead(buffer, feature);
                        } else {
                            Object newValue = null;
                            if (feature.isMany()) {
                                newValue = readList(idEObject, originalQueryClass, buffer, model, query, todoList, feature);
                            } else {
                                if (feature.getEType() instanceof EEnum) {
                                    int enumOrdinal = buffer.getInt();
                                    if (enumOrdinal == -1) {
                                        newValue = null;
                                    } else {
                                        EClassifier eType = feature.getEType();
                                        EEnumLiteral enumLiteral = ((EEnumImpl) eType).getEEnumLiteral(enumOrdinal);
                                        if (enumLiteral != null) {
                                            newValue = enumLiteral.getInstance();
                                        }
                                    }
                                } else if (feature.getEType() instanceof EClass) {
                                    // EReference eReference = (EReference) feature;
                                    buffer.order(ByteOrder.LITTLE_ENDIAN);
                                    short cid = buffer.getShort();
                                    buffer.order(ByteOrder.BIG_ENDIAN);
                                    if (cid == -1) {
                                    // null, do nothing
                                    } else if (cid < 0) {
                                        // non minus one and negative cid means value is embedded in record
                                        EClass referenceClass = database.getEClassForCid((short) (-cid));
                                        if (feature.getEAnnotation("dbembed") != null) {
                                            newValue = readEmbeddedValue(feature, buffer, referenceClass, query);
                                        } else {
                                            newValue = readWrappedValue(feature, buffer, referenceClass, query);
                                        }
                                    } else if (cid > 0) {
                                        // positive cid means value is reference to other record
                                        EClass referenceClass = database.getEClassForCid(cid);
                                        if (referenceClass == null) {
                                            throw new BimserverDatabaseException("No eClass found for cid " + cid);
                                        }
                                        // readReference is going to read a long, which includes the 2 bytes for the cid
                                        buffer.position(buffer.position() - 2);
                                        newValue = readReference(originalQueryClass, buffer, model, idEObject, feature, referenceClass, query, todoList);
                                    // if (eReference.getEOpposite() != null &&
                                    // ((IdEObjectImpl)
                                    // newValue).isLoadedOrLoading()) {
                                    // newValue = null;
                                    // }
                                    }
                                } else if (feature.getEType() instanceof EDataType) {
                                    newValue = readPrimitiveValue(feature.getEType(), buffer, query);
                                }
                            }
                            if (newValue != null) {
                                idEObject.eSet(feature, newValue);
                            }
                        }
                    }
                    fieldCounter++;
                }
            } catch (StringIndexOutOfBoundsException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferUnderflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            } catch (BufferOverflowException e) {
                throw new BimserverDatabaseException("Reading " + eClass.getName() + "(" + oid + ")." + feature.getName(), e);
            }
        }
        ((IdEObjectImpl) idEObject).setLoaded();
        ((IdEObjectImpl) idEObject).useInverses(true);
        if (DEVELOPER_DEBUG && idEObject.getRid() > 100000 || idEObject.getRid() < -100000) {
            LOGGER.debug("Improbable rid " + idEObject.getRid() + " - " + idEObject);
        }
        return idEObject;
    } catch (BufferUnderflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    } catch (BufferOverflowException e) {
        throw new BimserverDatabaseException("Reading " + eClass.getName(), e);
    }
}
Also used : IdEObjectImpl(org.bimserver.emf.IdEObjectImpl) EDataType(org.eclipse.emf.ecore.EDataType) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) EEnum(org.eclipse.emf.ecore.EEnum) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) EClass(org.eclipse.emf.ecore.EClass) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) BufferOverflowException(java.nio.BufferOverflowException) UUID(java.util.UUID) EEnumImpl(org.eclipse.emf.ecore.impl.EEnumImpl) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 40 with EDataType

use of org.eclipse.emf.ecore.EDataType in project BIMserver by opensourceBIM.

the class DatabaseSession method getWrappedValueSize.

private int getWrappedValueSize(Object val, EReference eReference, PackageMetaData packageMetaData) {
    if (val == null) {
        return 2;
    }
    if (val instanceof EObject) {
        IdEObject eObject = (IdEObject) val;
        if (eReference.getEAnnotation("twodimensionalarray") != null) {
            int refSize = 4;
            EStructuralFeature eStructuralFeature = eObject.eClass().getEStructuralFeature("List");
            List<?> l = (List<?>) eObject.eGet(eStructuralFeature);
            for (Object o : l) {
                if (o instanceof EObject) {
                    refSize += getWrappedValueSize(o, (EReference) eStructuralFeature, packageMetaData);
                } else {
                    refSize += getPrimitiveSize((EDataType) eStructuralFeature.getEType(), o);
                }
            }
            return refSize;
        } else if (eReference.getEAnnotation("dbembed") != null) {
            int refSize = 2;
            refSize += getExactSize(eObject, packageMetaData, false);
            return refSize;
        } else if (eObject.eClass().getEAnnotation("wrapped") != null) {
            IdEObject wrappedValue = (IdEObject) val;
            EStructuralFeature wrappedValueFeature = wrappedValue.eClass().getEStructuralFeature("wrappedValue");
            Object wrappedVal = eObject.eGet(wrappedValueFeature);
            int refSize = 2 + getPrimitiveSize((EDataType) wrappedValueFeature.getEType(), wrappedVal);
            if (wrappedValueFeature.getEType() == EcorePackage.eINSTANCE.getEDouble() || wrappedValueFeature.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) {
                EStructuralFeature wrappedStringFeature = wrappedValue.eClass().getEStructuralFeature("wrappedValueAsString");
                String str = (String) eObject.eGet(wrappedStringFeature);
                refSize += getPrimitiveSize(EcorePackage.eINSTANCE.getEString(), str);
            }
            return refSize;
        } else {
            return 8;
        }
    } else {
        throw new RuntimeException("Programming error, cannot happen");
    }
}
Also used : IdEObject(org.bimserver.emf.IdEObject) EDataType(org.eclipse.emf.ecore.EDataType) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) List(java.util.List) BasicEList(org.eclipse.emf.common.util.BasicEList) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) VirtualObject(org.bimserver.shared.VirtualObject) HashMapVirtualObject(org.bimserver.shared.HashMapVirtualObject) EObject(org.eclipse.emf.ecore.EObject) IdEObject(org.bimserver.emf.IdEObject) EReference(org.eclipse.emf.ecore.EReference)

Aggregations

EDataType (org.eclipse.emf.ecore.EDataType)48 EClass (org.eclipse.emf.ecore.EClass)21 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)15 EEnum (org.eclipse.emf.ecore.EEnum)14 EClassifier (org.eclipse.emf.ecore.EClassifier)13 EObject (org.eclipse.emf.ecore.EObject)13 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)10 Test (org.junit.Test)10 List (java.util.List)7 ArrayList (java.util.ArrayList)6 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)6 IdEObject (org.bimserver.emf.IdEObject)6 VirtualObject (org.bimserver.shared.VirtualObject)6 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)6 EPackage (org.eclipse.emf.ecore.EPackage)6 HashMapWrappedVirtualObject (org.bimserver.shared.HashMapWrappedVirtualObject)4 EAttribute (org.eclipse.emf.ecore.EAttribute)4 EReference (org.eclipse.emf.ecore.EReference)4 AbstractRule (org.eclipse.xtext.AbstractRule)4 ByteBuffer (java.nio.ByteBuffer)3