From 5c75d6053dd0e26a46efb195ea542b4b8948753d Mon Sep 17 00:00:00 2001 From: Alex Nelson Date: Thu, 24 Mar 2022 10:00:51 -0400 Subject: [PATCH 1/2] Centralize namespace constants in namespace.py References: * https://github.com/casework/CASE-Utilities-Python/issues/38 * [UCO OC-217] (CP-107) Revise ontology IRI to be slash-based and drop IRI base * [ONT-64] (CP-17) Define scheme for CASE and UCO importable versioned IRIs Signed-off-by: Alex Nelson --- case_utils/case_file/__init__.py | 12 +------ case_utils/namespace.py | 38 ++++++++++++++++++++ tests/case_utils/case_file/Makefile | 4 +++ tests/case_utils/case_file/test_case_file.py | 16 +++------ 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 case_utils/namespace.py diff --git a/case_utils/case_file/__init__.py b/case_utils/case_file/__init__.py index c776c22..75c6c42 100644 --- a/case_utils/case_file/__init__.py +++ b/case_utils/case_file/__init__.py @@ -26,20 +26,10 @@ import rdflib # type: ignore import case_utils +from case_utils.namespace import * DEFAULT_PREFIX = "http://example.org/kb/" -NS_RDF = rdflib.RDF -NS_UCO_CORE = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/core#") -NS_UCO_OBSERVABLE = rdflib.Namespace( - "https://unifiedcyberontology.org/ontology/uco/observable#" -) -NS_UCO_TYPES = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/types#") -NS_UCO_VOCABULARY = rdflib.Namespace( - "https://unifiedcyberontology.org/ontology/uco/vocabulary#" -) -NS_XSD = rdflib.XSD - # Shortcut syntax for defining an immutable named tuple is noted here: # https://docs.python.org/3/library/typing.html#typing.NamedTuple # via the "See also" box here: https://docs.python.org/3/library/collections.html#collections.namedtuple diff --git a/case_utils/namespace.py b/case_utils/namespace.py new file mode 100644 index 0000000..e6a0dd0 --- /dev/null +++ b/case_utils/namespace.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +# This software was developed at the National Institute of Standards +# and Technology by employees of the Federal Government in the course +# of their official duties. Pursuant to title 17 Section 105 of the +# United States Code this software is not subject to copyright +# protection and is in the public domain. NIST assumes no +# responsibility whatsoever for its use by other parties, and makes +# no guarantees, expressed or implied, about its quality, +# reliability, or any other characteristic. +# +# We would appreciate acknowledgement if the software is used. + +""" +This module provides importable constants for namespaces. + +To use, add "from case_utils.namespace import *". Namespace variables starting with "NS_" are imported. As needs are demonstrated in CASE tooling (both in case_utils and from downstream requests), namespaces will also be imported from rdflib for a consistent "NS_*" spelling. +""" + +import rdflib # type: ignore + +NS_SH = rdflib.SH +NS_RDF = rdflib.RDF +NS_XSD = rdflib.XSD + +NS_CASE_INVESTIGATION = rdflib.Namespace( + "https://ontology.caseontology.org/case/investigation/" +) +NS_UCO_ACTION = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/action#") +NS_UCO_CORE = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/core#") +NS_UCO_LOCATION = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/location#") +NS_UCO_OBSERVABLE = rdflib.Namespace( + "https://unifiedcyberontology.org/ontology/uco/observable#" +) +NS_UCO_TYPES = rdflib.Namespace("https://unifiedcyberontology.org/ontology/uco/types#") +NS_UCO_VOCABULARY = rdflib.Namespace( + "https://unifiedcyberontology.org/ontology/uco/vocabulary#" +) diff --git a/tests/case_utils/case_file/Makefile b/tests/case_utils/case_file/Makefile index a771381..5306db9 100644 --- a/tests/case_utils/case_file/Makefile +++ b/tests/case_utils/case_file/Makefile @@ -127,6 +127,7 @@ sample.txt.json: \ $(tests_srcdir)/src/isomorphic_diff.py \ $(top_srcdir)/case_utils/case_file/__init__.py \ $(top_srcdir)/case_utils/local_uuid.py \ + $(top_srcdir)/case_utils/namespace.py \ sample.txt-nocompact.json rm -f $@ _$@ __$@ export DEMO_UUID_REQUESTING_NONRANDOM=NONRANDOM_REQUESTED \ @@ -158,6 +159,7 @@ sample.txt.ttl: \ $(tests_srcdir)/.venv.done.log \ $(top_srcdir)/case_utils/case_file/__init__.py \ $(top_srcdir)/case_utils/local_uuid.py \ + $(top_srcdir)/case_utils/namespace.py \ sample.txt.done.log rm -f _$@ __$@ export DEMO_UUID_REQUESTING_NONRANDOM=NONRANDOM_REQUESTED \ @@ -180,6 +182,7 @@ sample.txt-disable_hashes.ttl: \ $(tests_srcdir)/.venv.done.log \ $(top_srcdir)/case_utils/case_file/__init__.py \ $(top_srcdir)/case_utils/local_uuid.py \ + $(top_srcdir)/case_utils/namespace.py \ sample.txt.done.log rm -f _$@ __$@ export DEMO_UUID_REQUESTING_NONRANDOM=NONRANDOM_REQUESTED \ @@ -204,6 +207,7 @@ sample.txt-nocompact.json: \ $(tests_srcdir)/src/isomorphic_diff.py \ $(top_srcdir)/case_utils/case_file/__init__.py \ $(top_srcdir)/case_utils/local_uuid.py \ + $(top_srcdir)/case_utils/namespace.py \ sample.txt.done.log rm -f _$@ export DEMO_UUID_REQUESTING_NONRANDOM=NONRANDOM_REQUESTED \ diff --git a/tests/case_utils/case_file/test_case_file.py b/tests/case_utils/case_file/test_case_file.py index 4eda521..78b4a5c 100644 --- a/tests/case_utils/case_file/test_case_file.py +++ b/tests/case_utils/case_file/test_case_file.py @@ -19,22 +19,14 @@ import rdflib.plugins.sparql # type: ignore import case_utils.ontology +from case_utils.namespace import * _logger = logging.getLogger(os.path.basename(__file__)) -IRI_UCO_CORE = "https://unifiedcyberontology.org/ontology/uco/core#" -IRI_UCO_OBSERVABLE = "https://unifiedcyberontology.org/ontology/uco/observable#" -IRI_UCO_TYPES = "https://unifiedcyberontology.org/ontology/uco/types#" - -NS_RDF = rdflib.RDF -NS_UCO_CORE = rdflib.Namespace(IRI_UCO_CORE) -NS_UCO_OBSERVABLE = rdflib.Namespace(IRI_UCO_OBSERVABLE) -NS_UCO_TYPES = rdflib.Namespace(IRI_UCO_TYPES) - NSDICT = { - "uco-core": IRI_UCO_CORE, - "uco-observable": IRI_UCO_OBSERVABLE, - "uco-types": IRI_UCO_TYPES, + "uco-core": NS_UCO_CORE, + "uco-observable": NS_UCO_OBSERVABLE, + "uco-types": NS_UCO_TYPES, } SRCDIR = os.path.dirname(__file__) From 285c61ccfc7ccca6a1f9ac44c6664d51c1772c69 Mon Sep 17 00:00:00 2001 From: Alex Nelson Date: Mon, 18 Apr 2022 19:50:23 -0400 Subject: [PATCH 2/2] Add other CASE and UCO namespaces Signed-off-by: Alex Nelson --- case_utils/namespace.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/case_utils/namespace.py b/case_utils/namespace.py index dfb6d55..f64b7a4 100644 --- a/case_utils/namespace.py +++ b/case_utils/namespace.py @@ -26,13 +26,35 @@ NS_CASE_INVESTIGATION = rdflib.Namespace( "https://ontology.caseontology.org/case/investigation/" ) -NS_UCO_ACTION = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/action/") +NS_CASE_VOCABULARY = rdflib.Namespace( + "https://ontology.caseontology.org/case/vocabulary/" +) +NS_UCO_ACTION = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/action/" +) NS_UCO_CORE = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/core/") -NS_UCO_LOCATION = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/location/") +NS_UCO_IDENTITY = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/identity/" +) +NS_UCO_LOCATION = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/location/" +) +NS_UCO_MARKING = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/marking/" +) NS_UCO_OBSERVABLE = rdflib.Namespace( "https://ontology.unifiedcyberontology.org/uco/observable/" ) +NS_UCO_PATTERN = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/pattern/" +) +NS_UCO_ROLE = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/role/") +NS_UCO_TIME = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/time/") +NS_UCO_TOOL = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/tool/") NS_UCO_TYPES = rdflib.Namespace("https://ontology.unifiedcyberontology.org/uco/types/") +NS_UCO_VICTIM = rdflib.Namespace( + "https://ontology.unifiedcyberontology.org/uco/victim/" +) NS_UCO_VOCABULARY = rdflib.Namespace( "https://ontology.unifiedcyberontology.org/uco/vocabulary/" )