Class Slot


  • public class Slot
    extends Object
    Represents a “slot” (that is, a field) on a specific LinkML object.

    This is not a “slot definition” in a LinkML schema! This class is mostly intended to encapsulate the code needed to (1) obtain information about a slot at runtime, (2) get or set the value of a slot.

    • Constructor Detail

      • Slot

        public Slot​(Field field)
             throws LinkMLRuntimeException
        Creates a new instance.
        Parameters:
        field - The Java field that represents the slot.
        Throws:
        LinkMLRuntimeException - If the class the field belongs to does not declare accessor methods for the slot (which should not happen if the class is a bona fide LinkML object).
    • Method Detail

      • getLinkMLName

        public String getLinkMLName()
        Gets the name of the slot as originally declared in LinkML.

        This may not be the same as the name of the underlying Java field because the LinkML name might have been transformed during the code generation process, for example if the original name was not a suitable Java name.

        Returns:
        The original LinkML name.
      • isGlobalIdentifier

        public boolean isGlobalIdentifier()
        Indicates whether the slot holds a global identifier for the class it belongs to.

        In the defining LinkML schema, such a slot is marked with identifier: true.

        Returns:
        true if the slot is a global identifier slot, otherwise false.
      • isLocalIdentifier

        public boolean isLocalIdentifier()
        Indicates whether the slot holds a local identifier for the class it belongs to.

        In the defining LinkML schema, such a slot is marked with key: true (which is why it is sometimes called a key slot).

        Returns:
        true if the slot is a local identifier (key) slot, otherwise false.
      • isIdentifier

        public boolean isIdentifier()
        Indicates whether the slot holds any kind of identifier (global or local) for the class it belongs to.
        Returns:
        true if the slot is a global or local identifier slot, otherwise false.
      • isTypeDesignator

        public boolean isTypeDesignator()
        Indicates whether the slot acts as the designator for its class.
        Returns:
        true is the slot is the class’ type designator, otherwise false.
      • getLinkedURI

        public String getLinkedURI()
        Gets the URI that identifies the slot.

        The “Link URI” is especially intended to be used for import/export from/to RDF, but it may have other uses as well.

        Since we do not have access to the defining schema, we are entirely dependent on the presence of a LinkURI annotation, which should have been inserted by the code generator.

        Returns:
        The link URI for the slot.
      • isMultivalued

        public boolean isMultivalued()
        Indicates whether the slot is expected to hold multiple values.
        Returns:
        true if the slot is multi-valued, otherwise false.
      • getInnerType

        public Class<?> getInnerType()
        Gets the type of the slot.

        For single-valued slot, this is the same as the type of the underlying Java field. But for multi-valued slots, the underlying field is of type List; this method will return the parameter type of the List object.

        Returns:
        The real type of the slot.
      • getInliningMode

        public InliningMode getInliningMode()
        Indicates how the slot value(s) is(are) inlined.

        Note that this returns the effectively expected inlining mode, that takes into account (1) the presence of the Inlined annotation on the field representing the slot, (2) whether the slot is single- or multi-valued, and (3) the type of the slot.

        For example, if a multi-valued slot has for type a class that has no identifier slot of any kind, then the slot is necessarily expected to be inlined as a list, regardless of whether it has been explicitly marked as such.

        Returns:
        The expected inlining mode for the slot.
      • getRequirementLevel

        public RequirementLevel getRequirementLevel()
        Indicates whether the slot is required, recommended, or optional.
        Returns:
        A RequirementLevel value for the slot.
      • getCustomConverter

        public Class<?> getCustomConverter()
        Gets the custom converter class to use to convert values intended for this slot, if any.

        A slot can indicate that it needs a custom converter by mean of a Converter annotation.

        Returns:
        The custom converter for the slot, or null if the code does not mandate a custom converter.
      • isExtensionStore

        public boolean isExtensionStore()
        Indicates whether this slot is intended to store “extended data” (unknown properties) for its object.
        Returns:
        true if the slot is the holder slot for all unknown properties.
      • getDeclaringClass

        public Class<?> getDeclaringClass()
        Gets the class in which the slot is declared.

        If the slot has been inherited from a parent class, this will return that parent class.

        Returns:
        The highest level class in which the slot is originally declared.
      • setValue

        public void setValue​(Object target,
                             Object value)
                      throws LinkMLRuntimeException
        Assigns a value to the slot for the given object.
        Parameters:
        target - The LinkML object holding the slot.
        value - The value to assign.
        Throws:
        LinkMLRuntimeException - If any error occurs when attempting to set the value. Likely causes would be (1) target is not a proper LinkML object, or not an object for which the present slot is valid; (2) the value itself is invalid.
      • getValue

        public Object getValue​(Object source)
                        throws LinkMLRuntimeException
        Gets the value of the slot for the given object.
        Parameters:
        source - The LinkML object holding the slot.
        Returns:
        The value of the slot in the given object.
        Throws:
        LinkMLRuntimeException - If any error occurs when attempting to get the value. A likely cause would be that source is not a proper LinkML object, or not an object for which the slot is valid.
      • getIdentifierSlot

        public static Slot getIdentifierSlot​(Class<?> type)
        Finds the slot that holds the identifier (local or global) for a given LinkML object.

        The “identifier slot” is the slot that is tagged with identifier: true or key: true in its definition (or in the slot_usage block that overrides the global definition for a given class). In LinkML Java objects, the identifier slot is tagged with a dedicated runtime annotation.

        Parameters:
        type - The Java class representing the LinML class for which to get the identifier slot.
        Returns:
        The Slot object representing the identifier slot, or null if the class does not have any identifier slot.
      • getSlot

        public static Slot getSlot​(Class<?> klass,
                                   String name)
                            throws LinkMLRuntimeException
        Gets the Slot object corresponding to a given field in a given class.

        This is mostly a convenience method for new Slot(klass.getDeclaredField(name)), except that this method takes care of looking up the field in the superclass(es) of the given class if needed.

        Parameters:
        klass - The class for which we want to retrieve a slot.
        name - The name of the field representing the slot (this is not the LinkML name of the slot).
        Returns:
        The requested slot, or null if the class does not contain such a slot.
        Throws:
        LinkMLRuntimeException - If the class has a field with the requested name, but no corresponding accessors (which should not happen if the class is a proper LinkML Java object).
      • getSlots

        public static Collection<Slot> getSlots​(Class<?> klass)
        Gets all the slots on a given class.
        Parameters:
        klass - The class for which to retrieve the slots.
        Returns:
        The list of all slots on the class (including the slots that belong to the superclass(es), if any).