Search in sources :

Example 11 with AtlasEnumElementDef

use of org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef in project incubator-atlas by apache.

the class TestUtilsV2 method defineDeptEmployeeTypes.

/**
     * Class Hierarchy is:
     * Department(name : String, employees : Array[Person])
     * Person(name : String, department : Department, manager : Manager)
     * Manager(subordinates : Array[Person]) extends Person
     * <p/>
     * Persons can have SecurityClearance(level : Int) clearance.
     */
public static AtlasTypesDef defineDeptEmployeeTypes() {
    String _description = "_description";
    AtlasEnumDef orgLevelEnum = new AtlasEnumDef("OrgLevel", "OrgLevel" + _description, "1.0", Arrays.asList(new AtlasEnumElementDef("L1", "Element" + _description, 1), new AtlasEnumElementDef("L2", "Element" + _description, 2)));
    AtlasStructDef addressDetails = createStructTypeDef("Address", "Address" + _description, AtlasTypeUtil.createRequiredAttrDef("street", "string"), AtlasTypeUtil.createRequiredAttrDef("city", "string"));
    AtlasEntityDef deptTypeDef = AtlasTypeUtil.createClassTypeDef(DEPARTMENT_TYPE, "Department" + _description, ImmutableSet.<String>of(), AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"), new AtlasAttributeDef("employees", String.format("array<%s>", "Employee"), true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {

        {
            add(new AtlasStructDef.AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
        }
    }));
    AtlasEntityDef personTypeDef = AtlasTypeUtil.createClassTypeDef("Person", "Person" + _description, ImmutableSet.<String>of(), AtlasTypeUtil.createUniqueRequiredAttrDef("name", "string"), AtlasTypeUtil.createOptionalAttrDef("address", "Address"), AtlasTypeUtil.createOptionalAttrDef("birthday", "date"), AtlasTypeUtil.createOptionalAttrDef("hasPets", "boolean"), AtlasTypeUtil.createOptionalAttrDef("numberOfCars", "byte"), AtlasTypeUtil.createOptionalAttrDef("houseNumber", "short"), AtlasTypeUtil.createOptionalAttrDef("carMileage", "int"), AtlasTypeUtil.createOptionalAttrDef("age", "float"), AtlasTypeUtil.createOptionalAttrDef("numberOfStarsEstimate", "biginteger"), AtlasTypeUtil.createOptionalAttrDef("approximationOfPi", "bigdecimal"));
    AtlasEntityDef employeeTypeDef = AtlasTypeUtil.createClassTypeDef("Employee", "Employee" + _description, ImmutableSet.of("Person"), AtlasTypeUtil.createOptionalAttrDef("orgLevel", "OrgLevel"), new AtlasAttributeDef("department", "Department", false, AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, new ArrayList<AtlasConstraintDef>()), new AtlasAttributeDef("manager", "Manager", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, new ArrayList<AtlasConstraintDef>() {

        {
            add(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

                {
                    put(AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "subordinates");
                }
            }));
        }
    }), new AtlasAttributeDef("mentor", EMPLOYEE_TYPE, true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasConstraintDef>emptyList()), AtlasTypeUtil.createOptionalAttrDef("shares", "long"), AtlasTypeUtil.createOptionalAttrDef("salary", "double"));
    employeeTypeDef.getAttribute("department").addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

        {
            put(AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "employees");
        }
    }));
    AtlasEntityDef managerTypeDef = AtlasTypeUtil.createClassTypeDef("Manager", "Manager" + _description, ImmutableSet.of("Employee"), new AtlasAttributeDef("subordinates", String.format("array<%s>", "Employee"), false, AtlasAttributeDef.Cardinality.SET, 1, 10, false, false, Collections.<AtlasConstraintDef>emptyList()));
    AtlasClassificationDef securityClearanceTypeDef = AtlasTypeUtil.createTraitTypeDef("SecurityClearance", "SecurityClearance" + _description, ImmutableSet.<String>of(), AtlasTypeUtil.createRequiredAttrDef("level", "int"));
    return new AtlasTypesDef(ImmutableList.of(orgLevelEnum), ImmutableList.of(addressDetails), ImmutableList.of(securityClearanceTypeDef), ImmutableList.of(deptTypeDef, personTypeDef, employeeTypeDef, managerTypeDef));
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEnumElementDef(org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasConstraintDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef) ArrayList(java.util.ArrayList) AtlasEnumDef(org.apache.atlas.model.typedef.AtlasEnumDef) AtlasConstraintDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef)

Example 12 with AtlasEnumElementDef

use of org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef in project incubator-atlas by apache.

the class AtlasEnumFormatConverter method fromV2ToV1.

@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    EnumValue ret = null;
    if (v2Obj == null || !(type instanceof AtlasEnumType)) {
        return ret;
    }
    AtlasEnumType enumType = (AtlasEnumType) type;
    AtlasEnumElementDef elementDef = enumType.getEnumElementDef(v2Obj.toString());
    if (elementDef != null) {
        ret = new EnumValue(elementDef.getValue(), elementDef.getOrdinal());
    }
    return ret;
}
Also used : AtlasEnumElementDef(org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef) EnumValue(org.apache.atlas.typesystem.types.EnumValue) AtlasEnumType(org.apache.atlas.type.AtlasEnumType)

Example 13 with AtlasEnumElementDef

use of org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef in project incubator-atlas by apache.

the class AtlasEnumFormatConverter method fromV1ToV2.

@Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    String ret = null;
    if (v1Obj == null || !(type instanceof AtlasEnumType)) {
        return ret;
    }
    Object v1Value = null;
    if (v1Obj instanceof EnumValue) {
        EnumValue enumValue = (EnumValue) v1Obj;
        v1Value = enumValue.value;
        if (v1Value == null) {
            v1Value = enumValue.ordinal;
        }
    } else if (v1Obj instanceof Map) {
        Map mapValue = (Map) v1Obj;
        v1Value = mapValue.get("value");
        if (v1Value == null) {
            v1Value = mapValue.get("ordinal");
        }
    }
    if (v1Value == null) {
        // could be 'value' or 'ordinal'
        v1Value = v1Obj;
    }
    AtlasEnumElementDef elementDef;
    if (v1Value instanceof Number) {
        elementDef = ((AtlasEnumType) type).getEnumElementDef((Number) v1Value);
    } else {
        elementDef = ((AtlasEnumType) type).getEnumElementDef(v1Value.toString());
    }
    if (elementDef != null) {
        ret = elementDef.getValue();
    }
    return ret;
}
Also used : AtlasEnumElementDef(org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef) EnumValue(org.apache.atlas.typesystem.types.EnumValue) AtlasEnumType(org.apache.atlas.type.AtlasEnumType) Map(java.util.Map)

Example 14 with AtlasEnumElementDef

use of org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef in project incubator-atlas by apache.

the class AtlasEnumDefStoreV1 method toVertex.

private void toVertex(AtlasEnumDef enumDef, AtlasVertex vertex) throws AtlasBaseException {
    if (CollectionUtils.isEmpty(enumDef.getElementDefs())) {
        throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, enumDef.getName(), "values");
    }
    List<String> values = new ArrayList<>(enumDef.getElementDefs().size());
    for (AtlasEnumElementDef element : enumDef.getElementDefs()) {
        // Validate the enum element
        if (StringUtils.isEmpty(element.getValue()) || null == element.getOrdinal()) {
            throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, enumDef.getName(), "elementValue");
        }
        String elemKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(enumDef, element.getValue());
        AtlasGraphUtilsV1.setProperty(vertex, elemKey, element.getOrdinal());
        if (StringUtils.isNotBlank(element.getDescription())) {
            String descKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(elemKey, "description");
            AtlasGraphUtilsV1.setProperty(vertex, descKey, element.getDescription());
        }
        values.add(element.getValue());
    }
    AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getTypeDefPropertyKey(enumDef), values);
}
Also used : AtlasEnumElementDef(org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ArrayList(java.util.ArrayList)

Aggregations

AtlasEnumElementDef (org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef)14 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)8 ArrayList (java.util.ArrayList)7 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)6 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)6 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)5 HashMap (java.util.HashMap)4 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)4 AtlasConstraintDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef)4 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)3 Test (org.testng.annotations.Test)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasEnumType (org.apache.atlas.type.AtlasEnumType)2 EnumValue (org.apache.atlas.typesystem.types.EnumValue)2 Map (java.util.Map)1 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)1