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