openspg/python/knext/chain/builder_chain.py

50 lines
1.4 KiB
Python
Raw Normal View History

2023-12-21 17:38:20 +08:00
# -*- coding: utf-8 -*-
# Copyright 2023 Ant Group CO., Ltd.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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-12-15 17:33:54 +08:00
from knext import rest
2023-12-06 17:26:39 +08:00
from knext.chain.base import Chain
2023-12-11 23:13:19 +08:00
class BuilderChain(Chain):
2023-12-06 17:26:39 +08:00
@property
def input_types(self):
return None
@property
def output_types(self):
return None
2023-12-08 11:25:26 +08:00
@classmethod
def from_config(cls):
return cls()
2023-12-11 23:13:19 +08:00
def invoke(self, **kwargs):
2023-12-15 17:33:54 +08:00
from knext.client.builder import BuilderClient
2023-12-18 14:30:59 +08:00
client = BuilderClient()
2023-12-11 23:13:19 +08:00
client.execute(self, **kwargs)
2023-12-15 17:33:54 +08:00
@classmethod
def from_chain(cls, chain):
return cls(dag=chain.dag)
def to_rest(self) -> rest.Pipeline:
nodes, edges = [], []
for node in self.dag.nodes:
nodes.append(node.to_rest())
for edge in self.dag.edges:
edges.append(rest.Edge(_from=edge[0].id, to=edge[1].id))
dag_config = rest.Pipeline(
nodes=nodes,
edges=edges
)
return dag_config