Search in sources :

Example 1 with VerdictField

use of com.ge.research.osate.verdict.dsl.type.VerdictField in project VERDICT by ge-high-assurance.

the class ThreatModelUtil method getVarType.

/**
 * Get the type of a variable/field.
 *
 * First finds the type of the variable by looking it up in the
 * scope. Then finds the type of each variable by looking it up
 * in the fields of the previous type.
 *
 * If at any point something is out of scope or does not type-check,
 * then the returned type will be empty.
 *
 * @param varField the Var AST object to type
 * @param indexProvider the index provider, may be obtained from Guice
 * @return see FieldTypeResult
 */
public static FieldTypeResult getVarType(Var varField, ResourceDescriptionsProvider indexProvider) {
    FieldTypeResult result = new FieldTypeResult();
    // Get correct parent for scoping
    EObject scopeParent = getContainerForClasses(varField, VAR_FIELD_SCOPE_PARENT_CLASSES);
    // ID is always present
    result.varName = varField.getId();
    // Get all variables in scope
    List<VerdictVariable> vars = ThreatModelUtil.getScope(scopeParent, indexProvider);
    // Get the variable corresponding to the ID
    // Empty if that ID is not in scope
    result.var = vars.stream().filter(var -> var.getId().equals(result.varName)).findFirst();
    if (!result.var.isPresent()) {
        return result;
    } else {
        if (!result.var.get().getType().isPresent()) {
            // yet have a type because the user is still editing the var/field
            return result;
        } else {
            // Check type iteratively for all fields and their children
            // Invariant: "type" holds the type of the rightmost var/field that has been processed
            VerdictType type = result.var.get().getType().get();
            if (varField.getIds() != null) {
                for (String fieldName : varField.getIds()) {
                    // Find the field of the current type for the next field name
                    Optional<VerdictField> field = type.getFields().stream().filter(f -> f.getName().equals(fieldName)).findFirst();
                    if (field.isPresent()) {
                        // Well-typed, advance to the next field
                        type = field.get().getType();
                        result.fieldIndex++;
                    } else {
                        // Not well-typed, crash and burn
                        result.lastField = fieldName;
                        return result;
                    }
                }
            }
            // All fields type-check and "type" holds the rightmost type
            result.type = Optional.of(type);
            return result;
        }
    }
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) DefaultAnnexLibrary(org.osate.aadl2.DefaultAnnexLibrary) Element(org.osate.aadl2.Element) VerdictField(com.ge.research.osate.verdict.dsl.type.VerdictField) ThreatEqualContains(com.ge.research.osate.verdict.dsl.verdict.ThreatEqualContains) ThreatStatement(com.ge.research.osate.verdict.dsl.verdict.ThreatStatement) PropertySet(org.osate.aadl2.PropertySet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) MetaclassReference(org.osate.aadl2.MetaclassReference) ThreatDatabase(com.ge.research.osate.verdict.dsl.verdict.ThreatDatabase) Map(java.util.Map) Exists(com.ge.research.osate.verdict.dsl.verdict.Exists) VerdictContractLibrary(com.ge.research.osate.verdict.dsl.verdict.VerdictContractLibrary) Aadl2Package(org.osate.aadl2.Aadl2Package) Var(com.ge.research.osate.verdict.dsl.verdict.Var) TreeIterator(org.eclipse.emf.common.util.TreeIterator) VerdictVariable(com.ge.research.osate.verdict.dsl.type.VerdictVariable) Intro(com.ge.research.osate.verdict.dsl.verdict.Intro) VerdictThreatModels(com.ge.research.osate.verdict.dsl.verdict.VerdictThreatModels) Set(java.util.Set) AadlTypeWrapper(com.ge.research.osate.verdict.dsl.type.AadlTypeWrapper) Forall(com.ge.research.osate.verdict.dsl.verdict.Forall) EObject(org.eclipse.emf.ecore.EObject) AadlPackage(org.osate.aadl2.AadlPackage) VerdictType(com.ge.research.osate.verdict.dsl.type.VerdictType) List(java.util.List) ResourceDescriptionsProvider(org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider) Property(org.osate.aadl2.Property) VerdictVariableImpl(com.ge.research.osate.verdict.dsl.type.VerdictVariableImpl) BuiltInType(com.ge.research.osate.verdict.dsl.type.BuiltInType) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) AnnexLibrary(org.osate.aadl2.AnnexLibrary) ThreatModel(com.ge.research.osate.verdict.dsl.verdict.ThreatModel) VerdictType(com.ge.research.osate.verdict.dsl.type.VerdictType) EObject(org.eclipse.emf.ecore.EObject) VerdictField(com.ge.research.osate.verdict.dsl.type.VerdictField) VerdictVariable(com.ge.research.osate.verdict.dsl.type.VerdictVariable)

Aggregations

AadlTypeWrapper (com.ge.research.osate.verdict.dsl.type.AadlTypeWrapper)1 BuiltInType (com.ge.research.osate.verdict.dsl.type.BuiltInType)1 VerdictField (com.ge.research.osate.verdict.dsl.type.VerdictField)1 VerdictType (com.ge.research.osate.verdict.dsl.type.VerdictType)1 VerdictVariable (com.ge.research.osate.verdict.dsl.type.VerdictVariable)1 VerdictVariableImpl (com.ge.research.osate.verdict.dsl.type.VerdictVariableImpl)1 Exists (com.ge.research.osate.verdict.dsl.verdict.Exists)1 Forall (com.ge.research.osate.verdict.dsl.verdict.Forall)1 Intro (com.ge.research.osate.verdict.dsl.verdict.Intro)1 ThreatDatabase (com.ge.research.osate.verdict.dsl.verdict.ThreatDatabase)1 ThreatEqualContains (com.ge.research.osate.verdict.dsl.verdict.ThreatEqualContains)1 ThreatModel (com.ge.research.osate.verdict.dsl.verdict.ThreatModel)1 ThreatStatement (com.ge.research.osate.verdict.dsl.verdict.ThreatStatement)1 Var (com.ge.research.osate.verdict.dsl.verdict.Var)1 VerdictContractLibrary (com.ge.research.osate.verdict.dsl.verdict.VerdictContractLibrary)1 VerdictThreatModels (com.ge.research.osate.verdict.dsl.verdict.VerdictThreatModels)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1