Skip to content

Commit

Permalink
Merge pull request #479 from TNO/fix-examples
Browse files Browse the repository at this point in the history
Fix examples
  • Loading branch information
bnouwt authored Feb 13, 2024
2 parents 3e55d8e + dad692f commit da6fb91
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 6 deletions.
16 changes: 16 additions & 0 deletions examples/common/posting_kb/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
71 changes: 71 additions & 0 deletions examples/common/posting_kb/posting_kb.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions examples/common/posting_kb/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions examples/common/reacting_kb/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
72 changes: 72 additions & 0 deletions examples/common/reacting_kb/reacting_kb.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions examples/common/reacting_kb/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions examples/unreachable-runtimes/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
18 changes: 15 additions & 3 deletions examples/unreachable-runtimes/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit da6fb91

Please sign in to comment.