Search in sources :

Example 1 with ToscaKey

use of org.opentosca.toscana.model.util.ToscaKey in project TOSCAna by StuPro-TOSCAna.

the class IntrinsicFunctionResolver method getPossibleTargetLocations.

private static List<Entity> getPossibleTargetLocations(ToscaKey location, Entity targetNode) {
    List<Entity> possibleLocations = new ArrayList<>();
    ToscaKey capabilityLocation = new ToscaKey<>(RootNode.CAPABILITIES, location.name);
    ToscaKey requirementLocation = new ToscaKey<>(RootNode.REQUIREMENTS, location.name);
    for (ToscaKey key : new ToscaKey[] { location, capabilityLocation, requirementLocation }) {
        targetNode.getChild(key).ifPresent(possibleLocations::add);
    }
    return possibleLocations;
}
Also used : ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Entity(org.opentosca.toscana.core.parse.model.Entity) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) ArrayList(java.util.ArrayList) ToscaKey(org.opentosca.toscana.model.util.ToscaKey)

Example 2 with ToscaKey

use of org.opentosca.toscana.model.util.ToscaKey in project TOSCAna by StuPro-TOSCAna.

the class KeyReflector method detectRealSubtype.

/**
 *     Returns the real class belonging to given entity.
 *
 *     @param type the (possibly abstract) parent class
 *     @return the real class, is a subclass of given type
 */
public static Class detectRealSubtype(MappingEntity entity, Class type) {
    MappingEntity nodeEntity = NavigationUtil.getEnclosingNode(entity);
    String nodeTypeIdentifier = nodeEntity.getValue(BaseToscaElement.TYPE);
    Class nodeType = TypeResolver.resolve(nodeTypeIdentifier);
    String filterName;
    Class toscaKeyType;
    ToscaKey result;
    if (Capability.class.isAssignableFrom(type)) {
        filterName = entity.getName();
        toscaKeyType = type;
        result = detectRealKey(entity, nodeType, toscaKeyType, filterName);
        return result.getType();
    } else if (RootRelationship.class.isAssignableFrom(type)) {
        filterName = entity.getParent().get().getName();
        toscaKeyType = Requirement.class;
        result = detectRealKey(entity, nodeType, toscaKeyType, filterName);
        return (Class) result.getDirectives().get(RequirementKey.RELATIONSHIP);
    } else {
        throw new IllegalStateException(String.format("Conversion of given abstract type %s not yet supported", type));
    }
}
Also used : Requirement(org.opentosca.toscana.model.requirement.Requirement) ToscaKey(org.opentosca.toscana.model.util.ToscaKey) MappingEntity(org.opentosca.toscana.core.parse.model.MappingEntity) RootRelationship(org.opentosca.toscana.model.relation.RootRelationship)

Example 3 with ToscaKey

use of org.opentosca.toscana.model.util.ToscaKey in project TOSCAna by StuPro-TOSCAna.

the class KeyReflector method reflectKeys.

private static Set<ToscaKey<?>> reflectKeys(Class keyContainer, Class<?> filter) {
    Field[] fields = keyContainer.getFields();
    Map<ToscaKey, Field> tmpKeyMap = new HashMap<>();
    for (Field field : fields) {
        if (field.getType().equals(ToscaKey.class) && Modifier.isStatic(field.getModifiers())) {
            try {
                ToscaKey toscaKey = (ToscaKey) field.get(null);
                if (filter.isAssignableFrom(toscaKey.getType())) {
                    tmpKeyMap.put(toscaKey, field);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    Set<ToscaKey<?>> keys = new HashSet<>();
    for (Map.Entry<ToscaKey, Field> entry : tmpKeyMap.entrySet()) {
        for (Map.Entry<ToscaKey, Field> otherEntry : tmpKeyMap.entrySet()) {
            Field field = entry.getValue();
            Field otherField = otherEntry.getValue();
            ToscaKey<?> key = entry.getKey();
            ToscaKey<?> otherKey = otherEntry.getKey();
            if (field != otherField && key.hasSameShape(otherKey)) {
                // find out which field should not be considered (due to "tosca shadowing")
                // shadowing works roughly the same in tosca as in java, but for static fields it doesn't work,
                // hence this workaround
                Class<?> clazz = field.getDeclaringClass();
                Class<?> otherClazz = otherField.getDeclaringClass();
                if (clazz.isAssignableFrom(otherClazz)) {
                    continue;
                }
            }
            keys.add(key);
        }
    }
    return keys;
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ToscaKey(org.opentosca.toscana.model.util.ToscaKey) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with ToscaKey

use of org.opentosca.toscana.model.util.ToscaKey in project TOSCAna by StuPro-TOSCAna.

the class ScalarTypeConverter method convertScalarEntity.

/**
 *     Converts the value attached to given {@code scalarEntity } to an instance of a type which is specified by given {@code key}.
 */
static <T> T convertScalarEntity(ScalarEntity scalarEntity, ToscaKey<T> key, Entity parent) {
    String value = scalarEntity.getValue();
    Class targetType = key.getType();
    if (String.class.isAssignableFrom(targetType)) {
        return (T) value;
    } else if (Integer.class.isAssignableFrom(targetType)) {
        Integer number;
        if (UNBOUNDED.equals(value)) {
            number = Integer.MAX_VALUE;
        } else {
            number = Integer.valueOf(value);
        }
        return (T) number;
    } else if (Boolean.class.isAssignableFrom(targetType)) {
        return (T) Boolean.valueOf(value);
    } else if (targetType.isEnum()) {
        Map<String, T> enumMap = EnumUtils.getEnumMap(targetType);
        Optional<T> result = enumMap.entrySet().stream().filter(entry -> value.equalsIgnoreCase(entry.getKey())).map(Map.Entry::getValue).findAny();
        return result.orElseThrow(() -> new NoSuchElementException(String.format("No value with name '%s' in enum '%s'", value, targetType.getSimpleName())));
    } else if (OperationVariable.class.isAssignableFrom(targetType)) {
        Connection c = scalarEntity.getGraph().getEdge(parent, scalarEntity);
        String name = null;
        OperationVariable var;
        if (c != null) {
            name = c.getKey();
            var = new OperationVariable(scalarEntity, name);
        } else {
            var = new OperationVariable(scalarEntity);
        }
        return (T) var;
    } else if (SizeUnit.class.isAssignableFrom(targetType)) {
        SizeUnit.Unit fromDefaultUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.FROM);
        SizeUnit.Unit toUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.TO);
        if (fromDefaultUnit == null || toUnit == null) {
            throw new IllegalStateException("ToscaKey defining a SizeUnit is illegal: No directive set for source and target units");
        }
        return (T) SizeUnit.convert(value, fromDefaultUnit, toUnit);
    } else if (Port.class.isAssignableFrom(targetType)) {
        return (T) new Port(Integer.valueOf(value));
    } else {
        throw new UnsupportedOperationException(String.format("Cannot convert value of type %s: currently unsupported", targetType.getSimpleName()));
    }
}
Also used : Port(org.opentosca.toscana.model.datatype.Port) ToscaKey(org.opentosca.toscana.model.util.ToscaKey) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) EnumUtils(org.apache.commons.lang3.EnumUtils) Map(java.util.Map) Connection(org.opentosca.toscana.core.parse.model.Connection) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Optional(java.util.Optional) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) NoSuchElementException(java.util.NoSuchElementException) Entity(org.opentosca.toscana.core.parse.model.Entity) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) Optional(java.util.Optional) Port(org.opentosca.toscana.model.datatype.Port) Connection(org.opentosca.toscana.core.parse.model.Connection) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ToscaKey (org.opentosca.toscana.model.util.ToscaKey)4 Map (java.util.Map)2 Entity (org.opentosca.toscana.core.parse.model.Entity)2 MappingEntity (org.opentosca.toscana.core.parse.model.MappingEntity)2 ScalarEntity (org.opentosca.toscana.core.parse.model.ScalarEntity)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 EnumUtils (org.apache.commons.lang3.EnumUtils)1 Connection (org.opentosca.toscana.core.parse.model.Connection)1 Port (org.opentosca.toscana.model.datatype.Port)1 SizeUnit (org.opentosca.toscana.model.datatype.SizeUnit)1 OperationVariable (org.opentosca.toscana.model.operation.OperationVariable)1 RootRelationship (org.opentosca.toscana.model.relation.RootRelationship)1 Requirement (org.opentosca.toscana.model.requirement.Requirement)1