From d1e075255e80a252e18785aa92df0e9c4b942598 Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Tue, 13 Feb 2024 08:52:43 +0100 Subject: [PATCH 1/2] Fix unreachable runtimes example and add missing common kbs --- examples/common/posting_kb/Dockerfile | 16 +++++ examples/common/posting_kb/posting_kb.py | 71 +++++++++++++++++++ examples/common/posting_kb/requirements.txt | 9 +++ examples/common/reacting_kb/Dockerfile | 16 +++++ examples/common/reacting_kb/reacting_kb.py | 72 ++++++++++++++++++++ examples/common/reacting_kb/requirements.txt | 9 +++ 6 files changed, 193 insertions(+) create mode 100644 examples/common/posting_kb/Dockerfile create mode 100644 examples/common/posting_kb/posting_kb.py create mode 100644 examples/common/posting_kb/requirements.txt create mode 100644 examples/common/reacting_kb/Dockerfile create mode 100644 examples/common/reacting_kb/reacting_kb.py create mode 100644 examples/common/reacting_kb/requirements.txt diff --git a/examples/common/posting_kb/Dockerfile b/examples/common/posting_kb/Dockerfile new file mode 100644 index 00000000..7334336a --- /dev/null +++ b/examples/common/posting_kb/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.10.6-alpine + +# Create and enable venv +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +RUN pip install --upgrade pip + +WORKDIR /app/ + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY ./posting_kb.py . + +ENTRYPOINT [ "python", "posting_kb.py" ] diff --git a/examples/common/posting_kb/posting_kb.py b/examples/common/posting_kb/posting_kb.py new file mode 100644 index 00000000..90963a68 --- /dev/null +++ b/examples/common/posting_kb/posting_kb.py @@ -0,0 +1,71 @@ +import os +import logging +import time +import json + +from knowledge_mapper.utils import match_bindings +from knowledge_mapper.tke_client import TkeClient +from knowledge_mapper.knowledge_base import KnowledgeBaseRegistrationRequest +from knowledge_mapper.knowledge_interaction import ( + PostKnowledgeInteraction, + PostKnowledgeInteractionRegistrationRequest, +) + +KE_URL = os.getenv("KE_URL") +KB_ID = os.getenv("KB_ID") +KB_NAME = KB_ID.split("/")[-1] +KB_DATA = json.loads(os.getenv("KB_DATA")) +if "PREFIXES" in os.environ: + PREFIXES = json.loads(os.getenv("PREFIXES")) +else: + PREFIXES = None +ARGUMENT_GRAPH_PATTERN = os.getenv("ARGUMENT_GRAPH_PATTERN") +RESULT_GRAPH_PATTERN = os.getenv("RESULT_GRAPH_PATTERN") + +log = logging.getLogger(KB_NAME) +log.setLevel(logging.INFO) + + +def kb_1(): + client = TkeClient(KE_URL) + client.connect() + log.info(f"registering KB...") + kb = client.register( + KnowledgeBaseRegistrationRequest( + id=f"{KB_ID}", + name=f"{KB_NAME}", + description=f"{KB_NAME}", + ) + ) + log.info(f"KB registered!") + log.info(f"registering POST KI...") + post: PostKnowledgeInteraction = kb.register_knowledge_interaction( + PostKnowledgeInteractionRegistrationRequest( + argument_pattern=ARGUMENT_GRAPH_PATTERN, result_pattern=RESULT_GRAPH_PATTERN, prefixes=PREFIXES + ) + ) + log.info(f"POST KI registered!") + result = [] + while True: + log.info(f"posting...") + result = post.post(KB_DATA) + resultBindingSet = result["resultBindingSet"] + exchangeInfo = result["exchangeInfo"] + kbs = [ exchange['knowledgeBaseId'] for exchange in exchangeInfo] + + if len(result) == 0: + log.debug(f"posting gave no results; will sleep for 2s...") + message = f"empty bindingset" + else: + message = f"{resultBindingSet}" + log.debug(f"got reaction: {resultBindingSet}") + + log.info(f"Received {message} from following KBs: {kbs}") + time.sleep(2) + + log.info(f"unregistering...") + kb.unregister() + + +if __name__ == "__main__": + kb_1() diff --git a/examples/common/posting_kb/requirements.txt b/examples/common/posting_kb/requirements.txt new file mode 100644 index 00000000..de076513 --- /dev/null +++ b/examples/common/posting_kb/requirements.txt @@ -0,0 +1,9 @@ +knowledge-mapper==0.0.23 +# certifi==2022.9.24 # Installed as dependency for requests +# charset-normalizer==2.1.1 # Installed as dependency for requests +# idna==3.4 # Installed as dependency for requests +# json5==0.9.10 # Installed as dependency for knowledge-mapper +# mysql-connector-python==8.0.30 # Installed as dependency for knowledge-mapper +# protobuf==3.20.1 # Installed as dependency for mysql-connector-python +# requests==2.28.1 # Installed as dependency for knowledge-mapper +# urllib3==1.26.12 # Installed as dependency for requests diff --git a/examples/common/reacting_kb/Dockerfile b/examples/common/reacting_kb/Dockerfile new file mode 100644 index 00000000..fe8bbae6 --- /dev/null +++ b/examples/common/reacting_kb/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.10.6-alpine + +# Create and enable venv +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" + +RUN pip install --upgrade pip + +WORKDIR /app/ + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY ./reacting_kb.py . + +ENTRYPOINT [ "python", "reacting_kb.py" ] diff --git a/examples/common/reacting_kb/reacting_kb.py b/examples/common/reacting_kb/reacting_kb.py new file mode 100644 index 00000000..54f78e68 --- /dev/null +++ b/examples/common/reacting_kb/reacting_kb.py @@ -0,0 +1,72 @@ +import os +import logging +import json + +log = logging.getLogger(__name__) +log.setLevel(logging.INFO) + +from typing import Dict, List +from knowledge_mapper.tke_client import TkeClient +from knowledge_mapper.knowledge_base import KnowledgeBaseRegistrationRequest +from knowledge_mapper.knowledge_interaction import ( + ReactKnowledgeInteractionRegistrationRequest, +) + +KE_URL = os.getenv("KE_URL") +KB_ID = os.getenv("KB_ID") +KB_NAME = KB_ID.split("/")[-1] +if "PREFIXES" in os.environ: + PREFIXES = json.loads(os.getenv("PREFIXES")) +else: + PREFIXES = None +ARGUMENT_GRAPH_PATTERN = os.getenv("ARGUMENT_GRAPH_PATTERN") +RESULT_GRAPH_PATTERN = None + +def react(bindings): + log.info(f"Reacting with empty bindingset to {bindings}...") + result = [] + return result; + + +log = logging.getLogger(KB_NAME) +log.setLevel(logging.INFO) + + +def reacting_kb(): + client = TkeClient(KE_URL) + client.connect() + log.info(f"registering KB...") + kb = client.register( + KnowledgeBaseRegistrationRequest( + id=KB_ID, + name=KB_NAME, + description=KB_ID.split("/")[-1], + ) + ) + log.info(f"KB registered!") + + def handler( + bindings: List[Dict[str, str]], requesting_kb_id: str + ) -> List[Dict[str, str]]: + log.info(f"REACT KI is handling a request...") + return react(bindings) + + log.info(f"registering REACT KI...") + kb.register_knowledge_interaction( + ReactKnowledgeInteractionRegistrationRequest( + argument_pattern=ARGUMENT_GRAPH_PATTERN, + result_pattern=RESULT_GRAPH_PATTERN, + prefixes=PREFIXES, + handler=handler, + ) + ) + log.info(f"REACT KI registered!") + + kb.start_handle_loop() + + log.info(f"unregistering...") + kb.unregister() + + +if __name__ == "__main__": + reacting_kb() diff --git a/examples/common/reacting_kb/requirements.txt b/examples/common/reacting_kb/requirements.txt new file mode 100644 index 00000000..de076513 --- /dev/null +++ b/examples/common/reacting_kb/requirements.txt @@ -0,0 +1,9 @@ +knowledge-mapper==0.0.23 +# certifi==2022.9.24 # Installed as dependency for requests +# charset-normalizer==2.1.1 # Installed as dependency for requests +# idna==3.4 # Installed as dependency for requests +# json5==0.9.10 # Installed as dependency for knowledge-mapper +# mysql-connector-python==8.0.30 # Installed as dependency for knowledge-mapper +# protobuf==3.20.1 # Installed as dependency for mysql-connector-python +# requests==2.28.1 # Installed as dependency for knowledge-mapper +# urllib3==1.26.12 # Installed as dependency for requests From dad692f1fb40bda8e9f10764e34e9ce1fb5565e6 Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Tue, 13 Feb 2024 08:53:56 +0100 Subject: [PATCH 2/2] Fix unreachable runtimes example. --- examples/unreachable-runtimes/Dockerfile | 8 +++++--- .../unreachable-runtimes/docker-compose.yml | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/unreachable-runtimes/Dockerfile b/examples/unreachable-runtimes/Dockerfile index 5114f37f..d1360f48 100644 --- a/examples/unreachable-runtimes/Dockerfile +++ b/examples/unreachable-runtimes/Dockerfile @@ -1,4 +1,6 @@ -FROM ghcr.io/tno/knowledge-engine/smart-connector:1.2.3 +FROM ghcr.io/tno/knowledge-engine/smart-connector:1.2.4 -RUN apt update -y -RUN apt-get install iptables sudo -y +USER root + +RUN apk update +RUN apk add iptables iptables-legacy sudo diff --git a/examples/unreachable-runtimes/docker-compose.yml b/examples/unreachable-runtimes/docker-compose.yml index 95573d9a..077370af 100644 --- a/examples/unreachable-runtimes/docker-compose.yml +++ b/examples/unreachable-runtimes/docker-compose.yml @@ -3,12 +3,17 @@ services: # runtimes. It exposes its service over port 8282. knowledge-directory: image: ghcr.io/tno/knowledge-engine/knowledge-directory:1.2.3 + ports: + - "8282:8282" # These services are seperate Knowledge Engine runtime, which can host # multiple smart connectors. Note that the REST API port is a DIFFERENT port # number than the ones configured below. It is still the default 8280. runtime-1: build: . + ports: + - "8001:8280" + - "8000:8000" #to allow configuring iptables cap_add: - NET_ADMIN @@ -17,7 +22,7 @@ services: KE_RUNTIME_EXPOSED_URL: http://runtime-1:8081 # The URL where the runtime is available for inter-runtime communication from the outside. KD_URL: http://knowledge-directory:8282 ENABLE_REASONER: false - JAVA_TOOL_OPTIONS: "-Djdk.httpclient.keepalive.timeout=1" + JAVA_TOOL_OPTIONS: "-Djdk.httpclient.keepalive.timeout=1 -agentlib:jdwp=transport=dt_socket,address=0.0.0.0:8000,server=y,suspend=n" runtime-2: build: . cap_add: @@ -28,6 +33,8 @@ services: KD_URL: http://knowledge-directory:8282 ENABLE_REASONER: false JAVA_TOOL_OPTIONS: "-Djdk.httpclient.keepalive.timeout=1" + extra_hosts: + - "host.docker.internal:host-gateway" runtime-3: build: . cap_add: @@ -38,20 +45,25 @@ services: KD_URL: http://knowledge-directory:8282 ENABLE_REASONER: false JAVA_TOOL_OPTIONS: "-Djdk.httpclient.keepalive.timeout=1" - + extra_hosts: + - "host.docker.internal:host-gateway" # These Knowledge Bases use the different runtimes, and exchange data with eachother. kb1: build: ../common/asking_kb environment: - KE_URL: http://runtime-1:8280/rest KB_ID: http://example.org/kb1 + KE_URL: http://runtime-1:8280/rest + #KE_URL: http://PC-40442.local:8280/rest + #KE_URL: http://host.docker.internal:8280/rest PREFIXES: | { "ex": "http://example.org/" } GRAPH_PATTERN: | ?a ex:relatedTo ?b . + extra_hosts: + - host.docker.internal:host-gateway kb2: build: ../common/answering_kb environment: