pyrdfrules.application
1from typing import List 2from pydantic import BaseModel 3from pyrdfrules.common.http.url import Url 4 5from pyrdfrules.common.logging.logger import configure_logging, log 6from pyrdfrules.config import Config 7from pyrdfrules.engine.http_engine import HttpEngine 8from pyrdfrules.engine.local_http_engine import LocalHttpEngine 9from pyrdfrules.engine.remote_http_engine import RemoteHttpEngine 10from pyrdfrules.rdfrules.rdfrules import RDFRules 11 12class Application(BaseModel): 13 """ 14 The Application class provides methods to start and stop local or remote instances of RDFRules. 15 """ 16 17 __rdfrules: List[RDFRules] = [] 18 19 def start_local(self, config: Config|None = None, **kwargs) -> RDFRules: 20 """Starts a local instance of RDFRules. 21 22 Args: 23 config: The configuration to use. 24 install_jvm: True if JVM should be installed. 25 install_rdfrules: True if RDFRules should be installed. 26 rdfrules_path: Path where RDFRules is installed, or where it should be downloaded to. 27 jvm_path: Path where JVM is installed, or where it should be downloaded to. 28 port: Port to use, if not specified, will default to 8851 + the number of instances already started. 29 **kwargs: Additional arguments. 30 31 Returns: 32 The RDFRules instance. 33 34 """ 35 36 if "port" not in kwargs: 37 kwargs["port"] = 8851 + len(self.__rdfrules) 38 39 log().info("Starting local RDFRules") 40 41 config = Config() if config is None else config 42 43 rdfrules = RDFRules( 44 engine=LocalHttpEngine( 45 config=config, 46 **kwargs 47 ), 48 config=config 49 ) 50 51 rdfrules.engine.start() 52 53 self.__rdfrules.append(rdfrules) 54 configure_logging(rdfrules.config) 55 56 log().info("Local instance of RDFRules started") 57 58 return rdfrules 59 60 def start_remote(self, url: Url|str, config: Config|None = None) -> RDFRules: 61 """Starts a remote instance of RDFRules. 62 """ 63 64 log().info("Connecting to remote instance of RDFRules at %s", url) 65 66 config = Config() if config is None else config 67 68 rdfrules = RDFRules( 69 engine=RemoteHttpEngine( 70 config=config, 71 url=url 72 ), 73 config=config 74 ) 75 76 self.__rdfrules.append(rdfrules) 77 78 configure_logging(rdfrules.config) 79 80 rdfrules.engine.start() 81 82 log().info("Connected to remote instance of RDFRules at %s", url) 83 84 return rdfrules 85 86 def stop(self) -> None: 87 """Stops the application. 88 """ 89 90 log().info(f"Stopping {len(self.__rdfrules)} instance(s) of RDFRules") 91 92 count = len(self.__rdfrules) 93 it = 0 94 95 for rdfrules in self.__rdfrules: 96 it += 1 97 log().info(f"Stopping RDFRules instance {it} of {count}") 98 rdfrules.engine.stop() 99 log().info(f"Stopped RDFRules instance {it} of {count}")
13class Application(BaseModel): 14 """ 15 The Application class provides methods to start and stop local or remote instances of RDFRules. 16 """ 17 18 __rdfrules: List[RDFRules] = [] 19 20 def start_local(self, config: Config|None = None, **kwargs) -> RDFRules: 21 """Starts a local instance of RDFRules. 22 23 Args: 24 config: The configuration to use. 25 install_jvm: True if JVM should be installed. 26 install_rdfrules: True if RDFRules should be installed. 27 rdfrules_path: Path where RDFRules is installed, or where it should be downloaded to. 28 jvm_path: Path where JVM is installed, or where it should be downloaded to. 29 port: Port to use, if not specified, will default to 8851 + the number of instances already started. 30 **kwargs: Additional arguments. 31 32 Returns: 33 The RDFRules instance. 34 35 """ 36 37 if "port" not in kwargs: 38 kwargs["port"] = 8851 + len(self.__rdfrules) 39 40 log().info("Starting local RDFRules") 41 42 config = Config() if config is None else config 43 44 rdfrules = RDFRules( 45 engine=LocalHttpEngine( 46 config=config, 47 **kwargs 48 ), 49 config=config 50 ) 51 52 rdfrules.engine.start() 53 54 self.__rdfrules.append(rdfrules) 55 configure_logging(rdfrules.config) 56 57 log().info("Local instance of RDFRules started") 58 59 return rdfrules 60 61 def start_remote(self, url: Url|str, config: Config|None = None) -> RDFRules: 62 """Starts a remote instance of RDFRules. 63 """ 64 65 log().info("Connecting to remote instance of RDFRules at %s", url) 66 67 config = Config() if config is None else config 68 69 rdfrules = RDFRules( 70 engine=RemoteHttpEngine( 71 config=config, 72 url=url 73 ), 74 config=config 75 ) 76 77 self.__rdfrules.append(rdfrules) 78 79 configure_logging(rdfrules.config) 80 81 rdfrules.engine.start() 82 83 log().info("Connected to remote instance of RDFRules at %s", url) 84 85 return rdfrules 86 87 def stop(self) -> None: 88 """Stops the application. 89 """ 90 91 log().info(f"Stopping {len(self.__rdfrules)} instance(s) of RDFRules") 92 93 count = len(self.__rdfrules) 94 it = 0 95 96 for rdfrules in self.__rdfrules: 97 it += 1 98 log().info(f"Stopping RDFRules instance {it} of {count}") 99 rdfrules.engine.stop() 100 log().info(f"Stopped RDFRules instance {it} of {count}")
The Application class provides methods to start and stop local or remote instances of RDFRules.
20 def start_local(self, config: Config|None = None, **kwargs) -> RDFRules: 21 """Starts a local instance of RDFRules. 22 23 Args: 24 config: The configuration to use. 25 install_jvm: True if JVM should be installed. 26 install_rdfrules: True if RDFRules should be installed. 27 rdfrules_path: Path where RDFRules is installed, or where it should be downloaded to. 28 jvm_path: Path where JVM is installed, or where it should be downloaded to. 29 port: Port to use, if not specified, will default to 8851 + the number of instances already started. 30 **kwargs: Additional arguments. 31 32 Returns: 33 The RDFRules instance. 34 35 """ 36 37 if "port" not in kwargs: 38 kwargs["port"] = 8851 + len(self.__rdfrules) 39 40 log().info("Starting local RDFRules") 41 42 config = Config() if config is None else config 43 44 rdfrules = RDFRules( 45 engine=LocalHttpEngine( 46 config=config, 47 **kwargs 48 ), 49 config=config 50 ) 51 52 rdfrules.engine.start() 53 54 self.__rdfrules.append(rdfrules) 55 configure_logging(rdfrules.config) 56 57 log().info("Local instance of RDFRules started") 58 59 return rdfrules
Starts a local instance of RDFRules.
Args: config: The configuration to use. install_jvm: True if JVM should be installed. install_rdfrules: True if RDFRules should be installed. rdfrules_path: Path where RDFRules is installed, or where it should be downloaded to. jvm_path: Path where JVM is installed, or where it should be downloaded to. port: Port to use, if not specified, will default to 8851 + the number of instances already started. **kwargs: Additional arguments.
Returns: The RDFRules instance.
61 def start_remote(self, url: Url|str, config: Config|None = None) -> RDFRules: 62 """Starts a remote instance of RDFRules. 63 """ 64 65 log().info("Connecting to remote instance of RDFRules at %s", url) 66 67 config = Config() if config is None else config 68 69 rdfrules = RDFRules( 70 engine=RemoteHttpEngine( 71 config=config, 72 url=url 73 ), 74 config=config 75 ) 76 77 self.__rdfrules.append(rdfrules) 78 79 configure_logging(rdfrules.config) 80 81 rdfrules.engine.start() 82 83 log().info("Connected to remote instance of RDFRules at %s", url) 84 85 return rdfrules
Starts a remote instance of RDFRules.
87 def stop(self) -> None: 88 """Stops the application. 89 """ 90 91 log().info(f"Stopping {len(self.__rdfrules)} instance(s) of RDFRules") 92 93 count = len(self.__rdfrules) 94 it = 0 95 96 for rdfrules in self.__rdfrules: 97 it += 1 98 log().info(f"Stopping RDFRules instance {it} of {count}") 99 rdfrules.engine.stop() 100 log().info(f"Stopped RDFRules instance {it} of {count}")
Stops the application.
265def init_private_attributes(self: BaseModel, __context: Any) -> None: 266 """This function is meant to behave like a BaseModel method to initialise private attributes. 267 268 It takes context as an argument since that's what pydantic-core passes when calling it. 269 270 Args: 271 self: The BaseModel instance. 272 __context: The context. 273 """ 274 if getattr(self, '__pydantic_private__', None) is None: 275 pydantic_private = {} 276 for name, private_attr in self.__private_attributes__.items(): 277 default = private_attr.get_default() 278 if default is not PydanticUndefined: 279 pydantic_private[name] = default 280 object_setattr(self, '__pydantic_private__', pydantic_private)
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that's what pydantic-core passes when calling it.
Args: self: The BaseModel instance. __context: The context.