-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #479 from TNO/fix-examples
Fix examples
- Loading branch information
Showing
8 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters