Skip to content

Commit

Permalink
Adds support for "concise" version of context to satisfy req 5 of issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kfairbanks committed Aug 10, 2022
1 parent 9ce2a4f commit cce5505
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 38 deletions.
70 changes: 64 additions & 6 deletions src/uco_jsonld_context_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,35 @@ def add_minimal_datatype_props_to_cntxt(self) -> None:

self.context_str += dtp_str_sect

def add_concise_datatype_props_to_cntxt(self) -> None:
"""Adds Datatype Properties to context string"""
dtp_str_sect = ""
dtp_list = list(self.datatype_properties_dict.keys())
dtp_list.sort()
for key in dtp_list:
if len(self.datatype_properties_dict[key]) > 1:
for dtp_obj in self.datatype_properties_dict[key]:
# print(dtp_obj.ns_prefix, key)
con_str = f"\"{dtp_obj.ns_prefix}:{dtp_obj.root_property_name}\":{{\n"
con_str += "\t\"@type\":\"@id\""
if dtp_obj.shacl_count_lte_1 is not True:
con_str += ",\n\t\"@container\":\"@set\"\n"
else:
con_str += "\n"
con_str += "},\n"
dtp_str_sect += con_str
else:
for dtp_obj in self.datatype_properties_dict[key]:
con_str = f"\"{dtp_obj.root_property_name}\":{{\n"
con_str += "\t\"@type\":\"@id\""
if dtp_obj.shacl_count_lte_1 is not True:
con_str += ",\n\t\"@container\":\"@set\"\n"
else:
con_str += "\n"
con_str += "},\n"
dtp_str_sect += con_str
self.context_str += dtp_str_sect

def add_minimal_object_props_to_cntxt(self) -> None:
"""Adds Object Properties to context string"""
op_str_sect = ""
Expand All @@ -353,6 +382,35 @@ def add_minimal_object_props_to_cntxt(self) -> None:

op_str_sect += con_str
self.context_str += op_str_sect

def add_concise_object_props_to_cntxt(self) -> None:
"""Adds Object Properties to context string"""
op_str_sect = ""
op_list = list(self.object_properties_dict.keys())
op_list.sort()
for key in op_list:
if len(self.object_properties_dict[key]) > 1:
for op_obj in self.object_properties_dict[key]:
# print(op_obj.ns_prefix, op_obj.root_class_name)
con_str = f"\"{op_obj.ns_prefix}:{op_obj.root_class_name}\":{{\n"
con_str += "\t\"@type\":\"@id\""
if op_obj.shacl_count_lte_1 is not True:
con_str += ",\n\t\"@container\":\"@set\"\n"
else:
con_str += "\n"
con_str += "},\n"
op_str_sect += con_str
else:
for op_obj in self.object_properties_dict[key]:
con_str = f"\"{op_obj.root_class_name}\":{{\n"
con_str += "\t\"@type\":\"@id\""
if op_obj.shacl_count_lte_1 is not True:
con_str += ",\n\t\"@container\":\"@set\"\n"
else:
con_str += "\n"
con_str += "},\n"
op_str_sect += con_str
self.context_str += op_str_sect

def add_key_strings_to_cntxt(self) -> None:
"""Adds id, type, and graph key strings to context string"""
Expand All @@ -375,11 +433,7 @@ def main():
Will print to stdout by default.")
args = argument_parser.parse_args()

print(args)
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
if (args.concise):
logging.error("\tConsice context has not been implemented yet.")
sys.exit()

_logger.debug("\t***Debug Mode enabled***")

Expand All @@ -396,8 +450,12 @@ def main():
cb.process_ObjectProperties()
cb.init_context_str()
cb.add_prefixes_to_cntxt()
cb.add_minimal_object_props_to_cntxt()
cb.add_minimal_datatype_props_to_cntxt()
if args.concise:
cb.add_concise_object_props_to_cntxt()
cb.add_concise_datatype_props_to_cntxt()
else:
cb.add_minimal_object_props_to_cntxt()
cb.add_minimal_datatype_props_to_cntxt()
cb.add_key_strings_to_cntxt()
cb.close_context_str()

Expand Down
87 changes: 55 additions & 32 deletions tests/context_builder/context_tester.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,68 @@
#!python
#
# NOTICE
# This software was produced for the U.S. Government under contract FA8702-22-C-0001,
# and is subject to the Rights in Data-General Clause 52.227-14, Alt. IV (DEC 2007)
# This software was produced for the U.S. Government under contract
# FA8702-22-C-0001, and is subject to the Rights in Data-General
# Clause 52.227-14, Alt. IV (DEC 2007)
#
# ©2022 The MITRE Corporation. All Rights Reserved.
# Released under PRS 18-4297.
#


import argparse
import json
import rdflib
import sys
import subprocess
import os

# Test graph file in JSON format
test_file = "action_result_NO_CONTEXT.json"
# File to which context will be written
output_file = "_temp_cntxt.json"
# Serialization of graph without using context
no_cntxt_out = "_test_out_no_cntxt.json-ld"
# Serialization of graph using context
cntxt_out = "_test_out_cntxt.json-ld"

# Execute Context builder
cmd = "python ../../src/uco_jsonld_context_builder.py --output " + output_file
print(cmd)
subprocess.run(cmd.split())

with open(output_file, 'r') as file:
tmp_c = json.load(file)

graph = rdflib.Graph()
graph.parse(test_file, format="json-ld")
graph.serialize(no_cntxt_out, format="json-ld")
graph2 = rdflib.Graph()
graph2.parse(test_file, format="json-ld", context_data=tmp_c)
graph.serialize(cntxt_out, context_data=tmp_c, format="json-ld", auto_compact=True)

# Clean up
# os.remove(output_file)
# os.remove(no_cntxt_out)
# os.remove(cntxt_out)
sys.exit()

def main():

arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--skip-clean", action="store_true",
help="Keeps intermediate test files instead of \
automatic deletion")
arg_parser.add_argument('--concise', action="store_true",
help="Perform testing on \"concise\" context instead of \"minimal\"")
args = arg_parser.parse_args()
print(args)

# Test graph file in JSON format
test_file = "action_result_NO_CONTEXT.json"
# File to which context will be written
output_file = "_temp_cntxt.json"
# Serialization of graph without using context
no_cntxt_out = "_test_out_no_cntxt.json-ld"
# Serialization of graph using context
cntxt_out = "_test_out_cntxt.json-ld"
# Execute Context builder
if args.concise:
cmd = "python ../../src/uco_jsonld_context_builder.py\
--concise --output " + output_file
else:
cmd = "python ../../src/uco_jsonld_context_builder.py\
--output " + output_file

print(cmd)
subprocess.run(cmd.split())
with open(output_file, 'r') as file:
tmp_c = json.load(file)
graph = rdflib.Graph()
graph.parse(test_file, format="json-ld")
graph.serialize(no_cntxt_out, format="json-ld")
graph2 = rdflib.Graph()
graph2.parse(test_file, format="json-ld", context_data=tmp_c)
graph.serialize(cntxt_out, context_data=tmp_c,
format="json-ld", auto_compact=True)

# Clean up
if not args.skip_clean:
os.remove(output_file)
os.remove(no_cntxt_out)
os.remove(cntxt_out)
return


if __name__ == '__main__':
main()

0 comments on commit cce5505

Please sign in to comment.