4
4
import json
5
5
from dataclasses import dataclass , field
6
6
from enum import Enum
7
- from typing import Any , Dict , List , Optional , Union
7
+ from typing import Any , Dict , List , Optional , Union , Tuple
8
8
9
9
from lbox .exceptions import InconsistentOntologyException
10
10
@@ -70,6 +70,15 @@ class Tool:
70
70
instructions = "Classification Example")
71
71
tool.add_classification(classification)
72
72
73
+ relationship_tool = Tool(
74
+ tool = Tool.Type.RELATIONSHIP,
75
+ name = "Relationship Tool Example",
76
+ constraints = [
77
+ ("source_tool_feature_schema_id_1", "target_tool_feature_schema_id_1"),
78
+ ("source_tool_feature_schema_id_2", "target_tool_feature_schema_id_2")
79
+ ]
80
+ )
81
+
73
82
Attributes:
74
83
tool: (Tool.Type)
75
84
name: (str)
@@ -79,6 +88,7 @@ class Tool:
79
88
schema_id: (str)
80
89
feature_schema_id: (str)
81
90
attributes: (list)
91
+ constraints: (list of [str, str]) (only available for RELATIONSHIP tool type)
82
92
"""
83
93
84
94
class Type (Enum ):
@@ -102,21 +112,28 @@ class Type(Enum):
102
112
schema_id : Optional [str ] = None
103
113
feature_schema_id : Optional [str ] = None
104
114
attributes : Optional [FeatureSchemaAttributes ] = None
115
+ constraints : Optional [Tuple [str , str ]] = None
105
116
106
117
def __post_init__ (self ):
118
+ if self .constraints is not None and self .tool != Tool .Type .RELATIONSHIP :
119
+ warnings .warn (
120
+ "The constraints attribute is only available for Relationship tool. The provided constraints will be ignored."
121
+ )
122
+ self .constraints = None
107
123
if self .attributes is not None :
108
124
warnings .warn (
109
125
"The attributes for Tools are in beta. The attribute name and signature may change in the future."
110
126
)
111
127
112
128
@classmethod
113
129
def from_dict (cls , dictionary : Dict [str , Any ]) -> Dict [str , Any ]:
130
+ tool = Tool .Type (dictionary ["tool" ])
114
131
return cls (
115
132
name = dictionary ["name" ],
116
133
schema_id = dictionary .get ("schemaNodeId" , None ),
117
134
feature_schema_id = dictionary .get ("featureSchemaId" , None ),
118
135
required = dictionary .get ("required" , False ),
119
- tool = Tool . Type ( dictionary [ " tool" ]) ,
136
+ tool = tool ,
120
137
classifications = [
121
138
Classification .from_dict (c )
122
139
for c in dictionary ["classifications" ]
@@ -128,6 +145,9 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> Dict[str, Any]:
128
145
]
129
146
if dictionary .get ("attributes" )
130
147
else None ,
148
+ constraints = dictionary .get ("constraints" , None )
149
+ if tool == Tool .Type .RELATIONSHIP
150
+ else None ,
131
151
)
132
152
133
153
def asdict (self ) -> Dict [str , Any ]:
@@ -144,6 +164,9 @@ def asdict(self) -> Dict[str, Any]:
144
164
"attributes" : [a .asdict () for a in self .attributes ]
145
165
if self .attributes is not None
146
166
else None ,
167
+ "constraints" : self .constraints
168
+ if self .constraints is not None
169
+ else None ,
147
170
}
148
171
149
172
def add_classification (self , classification : Classification ) -> None :
0 commit comments