genestorian / ShareYourCloning_LinkML

A LinkML data model for ShareYourCloning
MIT License
0 stars 0 forks source link

Slack Thread #1

Open manulera opened 2 months ago

manulera commented 2 months ago

Fixed values in LinkML?

Hello, I have a question regarding fixed values for fields in a class. I am coming at this because I am trying to move from using pydantic models in an API to use LinkML. Let’s imagine I had a class Employee, which has a field employee_type. This field could contain any value from a given enum. Let’s imagine then that we have some subclasses of that Employee that should have constrained values of employee_type.

from pydantic import BaseModel
from enum import Enum

class EmployeeType(str, Enum):
    hr = 'hr'
    research = 'research'

class Employee(BaseModel):
    name: str
    employee_type: EmployeeType

class HREmployee(Employee):
    employee_type: EmployeeType = EmployeeType.hr

class ResearchEmployee(Employee):
    employee_type: EmployeeType = EmployeeType.research

Can you do this in LinkML?

Should I be doing this? In the real use-case, I don’t need this field for python code, but I use an equivalent type field in the javascript frontend to identify the type of object.

Slack Message

manulera commented 2 months ago

Hi @Manuel Lera Ramírez . For your specific scenario you might want to consider a type designator. It will generate pydantic exactly as you have it without the need for any boilerplate. But in the general case you can use either equals_string or just define a new single member Enum

manulera commented 2 months ago

I see - check this thread: https://obo-communitygroup.slack.com/archives/C04EU7JL1NF/p1710174896621499?thread_ts=1710165955.698459&cid=C04EU7JL1NF - it has an example of what I think you are asking for using "rules" syntax in LinkML. Note the example specifies rules for one slot value affecting the other slot's value but its the same idea

Sierra Moxon (she/her) thanks! looking at the snippet in the ticket above, could you use a LinkML any_of construct with the necessary strings to constrain with? e.g.:

  resident_of:
    # remember to provide a range here, even though
    # it could be inferred from the any_of, good to be explicit
    range: uriorcurie
    # any_of is the same as exactly_one_of unless multivalued=true
    any_of:
      - equals_string: geonames:1
      - equals_string: geonames:2