2023-10-26 10:34:08 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-11-21 17:33:33 +08:00
|
|
|
# Copyright 2023 Ant Group CO., Ltd.
|
2023-10-26 10:34:08 +08:00
|
|
|
#
|
2023-11-21 17:33:33 +08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
|
|
# in compliance with the License. You may obtain a copy of the License at
|
2023-10-26 10:34:08 +08:00
|
|
|
#
|
2023-11-21 17:33:33 +08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2023-10-26 10:34:08 +08:00
|
|
|
#
|
2023-11-21 17:33:33 +08:00
|
|
|
# Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
# or implied.
|
2023-10-26 10:34:08 +08:00
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
from knext import rest
|
2023-12-11 23:13:19 +08:00
|
|
|
from knext.client.base import Client
|
2023-10-26 10:34:08 +08:00
|
|
|
|
|
|
|
_DEFAULT_JOB_NAME = "job"
|
|
|
|
|
|
|
|
|
|
|
|
class SchedulerTypeEnum(str, Enum):
|
|
|
|
Sync = "SYNC"
|
|
|
|
Once = "ONCE"
|
|
|
|
Cron = "CRON"
|
|
|
|
|
|
|
|
|
|
|
|
class LocalClusterModeEnum(str, Enum):
|
|
|
|
Local = "LOCAL"
|
|
|
|
Remote = "REMOTE"
|
|
|
|
|
|
|
|
|
2023-12-06 17:26:39 +08:00
|
|
|
class ReasonerClient(Client):
|
2023-11-21 15:17:02 +08:00
|
|
|
"""SPG Reasoner Client."""
|
2023-10-26 10:34:08 +08:00
|
|
|
|
2023-12-11 23:13:19 +08:00
|
|
|
_rest_client = rest.ReasonerApi()
|
|
|
|
|
|
|
|
def __init__(self, host_addr: str = None, project_id: int = None):
|
|
|
|
super().__init__(host_addr, project_id)
|
2023-10-26 10:34:08 +08:00
|
|
|
|
|
|
|
def submit(self, dsl_content: str):
|
|
|
|
"""Submit an asynchronous reasoner job to the server by name."""
|
|
|
|
content = rest.KgdslReasonerContent(kgdsl=dsl_content)
|
|
|
|
request = rest.ReasonerJobSubmitRequest(
|
|
|
|
job_name=_DEFAULT_JOB_NAME, project_id=self._project_id, content=content
|
|
|
|
)
|
2023-12-11 23:13:19 +08:00
|
|
|
return self._rest_client.reasoner_submit_job_info_post(
|
2023-10-26 10:34:08 +08:00
|
|
|
reasoner_job_submit_request=request
|
|
|
|
)
|
|
|
|
|
|
|
|
def run_dsl(self, dsl_content: str):
|
|
|
|
"""Submit a synchronization reasoner job by providing DSL content."""
|
|
|
|
content = rest.KgdslReasonerContent(kgdsl=dsl_content)
|
|
|
|
request = rest.ReasonerDslRunRequest(
|
|
|
|
content=content, project_id=self._project_id
|
|
|
|
)
|
2023-12-11 23:13:19 +08:00
|
|
|
return self._rest_client.reasoner_run_dsl_post(reasoner_dsl_run_request=request)
|
2023-10-26 10:34:08 +08:00
|
|
|
|
|
|
|
def query(self, job_inst_id: int):
|
|
|
|
"""Query status of a submitted reasoner job by job inst id."""
|
2023-12-11 23:13:19 +08:00
|
|
|
return self._rest_client.reasoner_query_job_inst_get(job_inst_id=job_inst_id)
|