-
Notifications
You must be signed in to change notification settings - Fork 5
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 #70 from meaningfy-ws/feature/TED-213
Added SPARQL CLI-tool
- Loading branch information
Showing
9 changed files
with
160 additions
and
18 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
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
100 changes: 100 additions & 0 deletions
100
ted_sws/mapping_suite_processor/entrypoints/cmd_sparql_generator.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,100 @@ | ||
#!/usr/bin/python3 | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
from ted_sws.core.adapters.cmd_runner import CmdRunner as BaseCmdRunner | ||
from ted_sws.core.adapters.logger import LOG_INFO_TEXT | ||
from ted_sws.mapping_suite_processor.services.conceptual_mapping_generate_sparql_queries import \ | ||
mapping_suite_processor_generate_sparql_queries as generate_sparql_queries, DEFAULT_RQ_NAME | ||
|
||
DEFAULT_MAPPINGS_PATH = 'mappings' | ||
DEFAULT_CONCEPTUAL_MAPPINGS_FILE = '{mappings_path}/{mapping_suite_id}/transformation/conceptual_mappings.xlsx' | ||
DEFAULT_OUTPUT_SPARQL_QUERIES_FOLDER = '{mappings_path}/{mapping_suite_id}/validation/sparql/cm_assertions' | ||
CMD_NAME = "CMD_SPARQL_GENERATOR" | ||
|
||
""" | ||
USAGE: | ||
# sparql_generator --help | ||
""" | ||
|
||
|
||
class CmdRunner(BaseCmdRunner): | ||
""" | ||
Keeps the logic to be used by SPARQL queries Generator | ||
""" | ||
|
||
def __init__( | ||
self, | ||
conceptual_mappings_file, | ||
output_sparql_queries_folder, | ||
rq_name | ||
): | ||
super().__init__(name=CMD_NAME) | ||
self.conceptual_mappings_file_path = Path(os.path.realpath(conceptual_mappings_file)) | ||
self.output_sparql_queries_folder_path = Path(os.path.realpath(output_sparql_queries_folder)) | ||
self.rq_name = rq_name | ||
|
||
if not self.conceptual_mappings_file_path.is_file(): | ||
error_msg = "No such file :: [" + conceptual_mappings_file + "]" | ||
self.log_failed_msg(error_msg) | ||
raise FileNotFoundError(error_msg) | ||
|
||
def run_cmd(self): | ||
self.generate(self.conceptual_mappings_file_path, self.output_sparql_queries_folder_path, self.rq_name) | ||
|
||
def generate(self, conceptual_mappings_file_path, rml_output_file_path, rq_name): | ||
""" | ||
Generates SPARQL queries from Conceptual Mappings | ||
""" | ||
self.log("Running " + LOG_INFO_TEXT.format("SPARQL queries") + " generation ... ") | ||
|
||
error = None | ||
try: | ||
generate_sparql_queries(conceptual_mappings_file_path, rml_output_file_path, rq_name) | ||
except Exception as e: | ||
error = e | ||
|
||
return self.run_cmd_result(error) | ||
|
||
|
||
@click.command() | ||
@click.argument('mapping-suite-id', nargs=1, required=False) | ||
@click.option('-i', '--opt-conceptual-mappings-file', help="Use to overwrite INPUT generator") | ||
@click.option('-o', '--opt-output-sparql-queries-folder', help="Use to overwrite OUTPUT generator") | ||
@click.option('-rq-name', '--opt-rq-name', default=DEFAULT_RQ_NAME) | ||
@click.option('-m', '--opt-mappings-path', default=DEFAULT_MAPPINGS_PATH) | ||
def main(mapping_suite_id, opt_conceptual_mappings_file, opt_output_sparql_queries_folder, | ||
opt_rq_name, opt_mappings_path): | ||
""" | ||
Generates SPARQL queries from Conceptual Mappings. | ||
""" | ||
|
||
if opt_conceptual_mappings_file: | ||
conceptual_mappings_file = opt_conceptual_mappings_file | ||
else: | ||
conceptual_mappings_file = DEFAULT_CONCEPTUAL_MAPPINGS_FILE.format( | ||
mappings_path=opt_mappings_path, | ||
mapping_suite_id=mapping_suite_id | ||
) | ||
|
||
if opt_output_sparql_queries_folder: | ||
output_sparql_queries_folder = opt_output_sparql_queries_folder | ||
else: | ||
output_sparql_queries_folder = DEFAULT_OUTPUT_SPARQL_QUERIES_FOLDER.format( | ||
mappings_path=opt_mappings_path, | ||
mapping_suite_id=mapping_suite_id | ||
) | ||
|
||
cmd = CmdRunner( | ||
conceptual_mappings_file=conceptual_mappings_file, | ||
output_sparql_queries_folder=output_sparql_queries_folder, | ||
rq_name=opt_rq_name | ||
) | ||
cmd.run() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
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
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
Binary file added
BIN
+4.59 KB
...te_processor_repository/test_package_fake/transformation/invalid_conceptual_mappings.xlsx
Binary file not shown.
37 changes: 37 additions & 0 deletions
37
tests/unit/mapping_suite_processor/test_cmd_sparql_generator.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,37 @@ | ||
import os | ||
|
||
from click.testing import CliRunner | ||
|
||
from ted_sws.mapping_suite_processor.entrypoints.cmd_sparql_generator import main as generate | ||
|
||
cmdRunner = CliRunner() | ||
|
||
|
||
def __process_output_dir(fake_repository_path, fake_mapping_suite_id): | ||
output_dir_path = fake_repository_path / fake_mapping_suite_id / "validation" / "sparql" / "cm_assertions" | ||
assert os.path.isdir(output_dir_path) | ||
for filename in os.listdir(output_dir_path): | ||
f = os.path.join(output_dir_path, filename) | ||
if os.path.isfile(f): | ||
os.remove(f) | ||
|
||
|
||
def test_sparql_generator(fake_mapping_suite_id, file_system_repository_path): | ||
response = cmdRunner.invoke(generate, [fake_mapping_suite_id, "--opt-mappings-path", file_system_repository_path]) | ||
assert response.exit_code == 0 | ||
assert "SUCCESS" in response.output | ||
__process_output_dir(file_system_repository_path, fake_mapping_suite_id) | ||
|
||
|
||
def test_sparql_generator_with_non_existing_input(file_system_repository_path): | ||
response = cmdRunner.invoke(generate, ["-i", "non_existing_dir/non_existing_file", | ||
"-o", "non_existing_dir", | ||
"--opt-mappings-path", file_system_repository_path]) | ||
assert "No such file" in response.output | ||
|
||
|
||
def test_sparql_generator_with_invalid_input(file_system_repository_path, fake_mapping_suite_id): | ||
response = cmdRunner.invoke(generate, ["-i", str(file_system_repository_path / fake_mapping_suite_id / | ||
"transformation" / "invalid_conceptual_mappings.xlsx"), | ||
"--opt-mappings-path", file_system_repository_path]) | ||
assert "FAILED" in response.output |
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