33import requests
44from fastapi import APIRouter
55
6- import model
7- import service_model
6+ from semantic_matcher import model , service_model
87
98
109class SemanticMatchingService :
@@ -38,6 +37,12 @@ def __init__(
3837 equivalences that this :class:`~.SemanticMatchingService` contains.
3938 """
4039 self .router = APIRouter ()
40+
41+ self .router .add_api_route (
42+ "/all_matches" ,
43+ self .get_all_matches ,
44+ methods = ["GET" ]
45+ )
4146 self .router .add_api_route (
4247 "/get_matches" ,
4348 self .get_matches ,
@@ -48,9 +53,24 @@ def __init__(
4853 self .post_matches ,
4954 methods = ["POST" ]
5055 )
56+ self .router .add_api_route (
57+ "/clear" ,
58+ self .remove_all_matches ,
59+ methods = ["POST" ]
60+ )
5161 self .endpoint : str = endpoint
5262 self .equivalence_table : model .EquivalenceTable = equivalences
5363
64+ def get_all_matches (self ):
65+ """
66+ Returns all matches stored in the equivalence table-
67+ """
68+ matches = self .equivalence_table .get_all_matches ()
69+ return matches
70+
71+ def remove_all_matches (self ):
72+ self .equivalence_table .remove_all_semantic_matches ()
73+
5474 def get_matches (
5575 self ,
5676 request_body : service_model .MatchRequest
@@ -71,7 +91,12 @@ def get_matches(
7191 # Now look for remote matches:
7292 additional_remote_matches : List [model .SemanticMatch ] = []
7393 for match in matches :
94+ if match .base_semantic_id .split ("/" )[0 ] == match .match_semantic_id .split ("/" )[0 ]:
95+ #match_id is local
96+ continue
7497 remote_matching_service = self ._get_matcher_from_semantic_id (match .match_semantic_id )
98+ if remote_matching_service is None :
99+ continue
75100 remote_matching_request = service_model .MatchRequest (
76101 semantic_id = match .match_semantic_id ,
77102 # This is a simple "Ungleichung"
@@ -86,14 +111,14 @@ def get_matches(
86111 name = request_body .name ,
87112 definition = request_body .definition
88113 )
89- new_matches_response = requests .get (remote_matching_service , data = remote_matching_request )
90- match_response : service_model .MatchesList = service_model .MatchesList .model_validate_json (
91- new_matches_response .json ()
92- )
114+ url = f"{ remote_matching_service } /get_matches"
115+ new_matches_response = requests .get (url , json = remote_matching_request .dict ())
116+ match_response = service_model .MatchesList .model_validate_json (new_matches_response .text )
93117 additional_remote_matches .extend (match_response .matches )
94118 # Finally, put all matches together and return
95119 matches .extend (additional_remote_matches )
96- return service_model .MatchesList (matches = matches )
120+ res = service_model .MatchesList (matches = matches )
121+ return res
97122
98123 def post_matches (
99124 self ,
@@ -109,7 +134,20 @@ def _get_matcher_from_semantic_id(self, semantic_id: str) -> str:
109134
110135 :returns: The endpoint with which the `SemanticMatchingService` can be accessed
111136 """
112- return self .endpoint # todo
137+ request_body = {"semantic_id" : semantic_id }
138+ endpoint = config ['RESOLVER' ]['endpoint' ]
139+ port = config ['RESOLVER' ].getint ('port' )
140+ url = f"{ endpoint } :{ port } /get_semantic_matching_service"
141+ response = requests .get (url , json = request_body .dict ())
142+
143+ # Check if the response is successful (status code 200)
144+ if response .status_code == 200 :
145+ # Parse the JSON response and construct SMSResponse object
146+ response_json = response .json ()
147+ response_endpoint = response_json ['semantic_matching_service_endpoint' ]
148+ return response_endpoint
149+
150+ return None
113151
114152
115153if __name__ == '__main__' :
@@ -142,4 +180,4 @@ def _get_matcher_from_semantic_id(self, semantic_id: str) -> str:
142180 APP .include_router (
143181 SEMANTIC_MATCHING_SERVICE .router
144182 )
145- uvicorn .run (APP , host = "127 .0.0.1 " , port = int (config ["SERVICE" ]["PORT" ]))
183+ uvicorn .run (APP , host = "0 .0.0.0 " , port = int (config ["SERVICE" ]["PORT" ]))
0 commit comments