단계적 쓰기는 개발 beta 단계에 있습니다. 개발 중 기능이 변경될 수 있습니다.
라이브 프리뷰 및 게시된 Function 프리뷰는 Code Repositories에서만 지원되며, 로컬 개발 또는 VS Code 워크스페이스 환경에서는 지원되지 않습니다.
단계적 쓰기는 온톨로지에서 오브젝트를 편집하는 Python Function에 대한 추가 실행 모델을 제공합니다. 일반 Python 온톨로지 편집 Function과 달리, 단계적 쓰기 Function은 다음과 같은 특징을 가집니다:
이 페이지에서는 Python에서 단계적 쓰기 Function을 작성하는 방법과 그 고유한 속성을 설명합니다. TypeScript v2에 대한 내용은 TypeScript v2 Function에서 단계적 쓰기를 확인해주세요. 편집 Function 작동 방식에 대한 자세한 내용은 온톨로지 편집 개요를 참고하세요.
단계적 쓰기 Function은 몇 가지 중요한 점에서 일반 편집 Function과 다릅니다.
단계적 쓰기 Function 내에서 읽는 모든 온톨로지 데이터는 Function 내에서 이전에 수행된 모든 편집과 동일한 액션 실행 내에서 호출된 단계적 쓰기 Function이 수행한 모든 편집을 반영합니다. 이러한 편집은 단계적으로 처리되며, 실행 컨텍스트 외부의 다른 사용자나 Function에는 보이지 않습니다. Function 내에서 검색 요청 및 집계를 사용하여 온톨로지를 쿼리할 수 있으며, 결과는 이전에 단계적으로 처리된 모든 편집을 반영합니다.
일반 Python 온톨로지 편집 Function은 편집이 적용되기 위해 온톨로지 편집 배치를 반환해야 합니다. 단계적 쓰기 Function은 자동으로 편집을 단계적으로 처리하고 액션이 완료되면 온톨로지에 적용합니다. Function은 편집을 반환하는 대신 지원되는 Function 유형을 반환할 수 있습니다.
단계적 쓰기 Function 내의 모든 작업(쿼리, Function 호출, AIP Logic 실행 포함)은 편집을 함께 단계적으로 처리합니다. Function이 성공적으로 완료된 후 편집이 온톨로지에 커밋됩니다. Function에서 오류가 발생하면 온톨로지는 수정되지 않으며, 모든 단계적 편집은 액션이 Function을 다시 시도하기 전에 버려집니다.
WriteableClientPython 단계적 쓰기 Function은 표준 FoundryClient 대신 생성된 WriteableClient를 사용합니다. WriteableClient를 사용하여 단계적 쓰기 컨테이너를 생성합니다. 컨테이너는 일반 Python 편집 컨테이너와 동일한 오브젝트 및 링크 편집 API를 제공하지만, Function에서 편집을 반환하지 않습니다.
Python 단계적 쓰기 Function을 정의하려면:
@function 데코레이터에서 beta=True로 설정합니다.edits 파라미터를 사용하여 Function이 편집할 수 있는 모든 오브젝트 및 링크 유형을 선언합니다. 중첩 Function 호출을 통해 편집할 수 있는 경우도 포함됩니다. 링크 편집을 선언하려면 OntologyLinkType(SourceObjectType, "linkApiName")을 사용합니다.WriteableClient 유형으로 주석 처리합니다. Function 런타임은 액션 실행을 위한 트랜잭션 컨텍스트를 가진 클라이언트를 주입하므로, Function 내에서 클라이언트를 생성하지 마세요.다음 Function은 Employee 오브젝트 유형에 대한 편집을 선언하고 적용합니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14from functions.api import Integer, function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Employee @function(beta=True, edits=[Employee]) def update_employee_last_name( client: WriteableClient, employee_id: Integer, new_last_name: str, ) -> Integer: staged_writes = client.ontology.staged_writes() editable_employee = staged_writes.objects.Employee.edit(employee_id) editable_employee.last_name = new_last_name return employee_id
단계적 쓰기 Function을 Function 기반 액션으로 구성하여 편집을 적용합니다.
단계적 쓰기 컨테이너에서 오브젝트 유형의 create 메서드를 사용합니다. 오브젝트의 기본 키를 제공해야 하며, 동일한 호출에서 다른 속성을 초기화할 수 있습니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18from functions.api import function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Employee @function(beta=True, edits=[Employee]) def create_employee( client: WriteableClient, employee_id: str, first_name: str, last_name: str, ) -> str: staged_writes = client.ontology.staged_writes() staged_writes.objects.Employee.create( employee_id=employee_id, first_name=first_name, last_name=last_name, ) return employee_id
기본 키를 생성하고 오브젝트를 생성한 후 동일한 실행에서 키를 반환하거나 사용할 수 있습니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16from uuid import uuid4 from functions.api import function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Ticket @function(beta=True, edits=[Ticket]) def create_ticket(client: WriteableClient, title: str) -> str: ticket_id = str(uuid4()) staged_writes = client.ontology.staged_writes() staged_writes.objects.Ticket.create( ticket_id=ticket_id, title=title, status="open", ) return ticket_id
단계적 쓰기 컨테이너에서 오브젝트 유형의 edit 메서드를 사용합니다. 오브젝트 또는 기본 키를 전달한 후 편집 가능한 오브젝트에 새로운 속성 값을 할당할 수 있습니다:
Copied!1 2 3 4 5 6staged_writes = client.ontology.staged_writes() editable_employee = staged_writes.objects.Employee.edit(employee) editable_employee.last_name = new_name editable_employee = staged_writes.objects.Employee.edit(23) editable_employee.last_name = new_name
인터페이스 편집은 단계적 쓰기 Function에서 지원되지 않습니다.
오브젝트 유형의 delete 메서드를 사용하여 오브젝트 또는 기본 키를 전달합니다:
Copied!1 2 3staged_writes = client.ontology.staged_writes() staged_writes.objects.Ticket.delete(ticket) staged_writes.objects.Ticket.delete(12)
단계적 쓰기 컨테이너를 통해 오브젝트를 편집한 후, 링크 속성의 add 또는 remove 메서드를 사용하여 다대다 링크를 편집합니다:
Copied!1 2 3 4staged_writes = client.ontology.staged_writes() editable_employee = staged_writes.objects.Employee.edit(employee) editable_employee.assigned_tickets.add(ticket) editable_employee.assigned_tickets.remove(ticket)
일대다 링크를 편집하려면 오브젝트 생성 또는 업데이트 편집을 사용하여 외래 키 속성을 편집하세요.
WriteableClient를 통한 읽기는 동일한 실행에서 이전에 작성된 데이터를 반영합니다. 온톨로지 읽기 전에 클라이언트는 대기 중인 편집을 단계적 트랜잭션에 전송하여 쿼리 또는 집계에 이를 포함시킵니다.
다음 Function은 티켓을 생성한 후 열린 티켓 수를 계산합니다. 결과에는 새로 생성된 티켓이 포함됩니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23from functions.api import Integer, function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Ticket @function(beta=True, edits=[Ticket]) def create_ticket_and_count_open( client: WriteableClient, ticket_id: str, title: str, ) -> Integer: staged_writes = client.ontology.staged_writes() staged_writes.objects.Ticket.create( ticket_id=ticket_id, title=title, status="open", ) return ( client.ontology.objects.Ticket.where( Ticket.object_type.status == "open" ) .count() .compute() )
WriteableClient를 통해 다른 Function이나 쿼리를 호출하면 해당 작업이 동일한 단계적 편집에 참여합니다. 호출된 Function에서의 읽기는 실행 중 이전에 단계적으로 처리된 편집을 반영하며, 호출된 Function에서 수행된 편집은 동일한 단계적 편집에 추가됩니다. 이는 다음에 적용됩니다:
최상위 Function이 성공적으로 완료되면, 중첩 호출 전반에 걸쳐 단계적으로 처리된 모든 편집이 함께 커밋됩니다. 호출 중 하나에서 오류가 발생하면 전체 편집 세트가 버려집니다.
다음 예제에서 assign_ticket은 동일한 저장소에서 게시된 별도의 단계적 쓰기 Function입니다. bulk_assign_tickets는 OSDK 생성 쿼리 API를 통해 이를 호출하며, 각 호출은 동일한 단계적 편집에 편집을 추가합니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19from functions.api import Integer, OntologyLinkType, function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Employee, Ticket @function( beta=True, edits=[Ticket, OntologyLinkType(Employee, "assignedTickets")], ) def bulk_assign_tickets( client: WriteableClient, employee_id: Integer, ticket_ids: list[str], ) -> Integer: for ticket_id in ticket_ids: client.ontology.queries.assign_ticket( employee_id=employee_id, ticket_id=ticket_id, ) return len(ticket_ids)
단계적 쓰기 Function은 다음 실행 라이프사이클을 사용합니다:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16from functions.api import Integer, function from ontology_sdk import WriteableClient from ontology_sdk.ontology.objects import Employee @function(beta=True, edits=[Employee]) def update_employee_with_validation( client: WriteableClient, employee_id: Integer, new_salary: float, ) -> Integer: if new_salary < 0: raise ValueError("Salary cannot be negative") staged_writes = client.ontology.staged_writes() editable_employee = staged_writes.objects.Employee.edit(employee_id) editable_employee.salary = new_salary return employee_id