query.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json.decoder
  2. import logging
  3. import ssl
  4. from dataclasses import dataclass, field
  5. from typing import List
  6. from urllib.error import URLError
  7. import owly
  8. import arklog
  9. arklog.set_config_logging()
  10. @dataclass(init=True, repr=True, order=False, frozen=True)
  11. class Prefix:
  12. name: str
  13. namespace: str
  14. def __str__(self) -> str:
  15. """Return the prefix in the SPARQL format."""
  16. return f"PREFIX {self.name}: <{self.namespace}>"
  17. @dataclass(init=True, repr=True, order=False, frozen=True)
  18. class Replacement:
  19. placeholder: str
  20. description: str
  21. suggestion: str
  22. @dataclass(init=True, repr=True, order=False, frozen=True)
  23. class QueryTemplate:
  24. group: str
  25. name: str
  26. prefix: str
  27. query: str
  28. description: str
  29. visual_support: bool = False
  30. replacements: List[Replacement] = field(default_factory=list)
  31. modifies: bool = False
  32. # TODO Rewrite this (use dict from config + merge per query)
  33. reverse_prefix = {
  34. "https://ontology.rys.app/dt/function/": "dtf",
  35. "http://www.w3.org/2002/07/owl#": "owl",
  36. "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
  37. "http://www.w3.org/2001/XMLSchema#": "xsd",
  38. "http://www.w3.org/2000/01/rdf-schema#": "rdfs",
  39. "http://purl.org/dc/elements/1.1/": "dc",
  40. "http://bipm.org/jcgm/vim4#": "vim4",
  41. "http://www.w3.org/2003/11/swrl#": "swrl",
  42. "http://ua.be/drivetrain/description/artifacts/artifacts#": "dart",
  43. "http://ua.be/drivetrain/description/ftg#": "dftg",
  44. "http://ua.be/sdo2l/vocabulary/base/acyclic#": "acyclic",
  45. "http://ua.be/sdo2l/vocabulary/base/base#": "base",
  46. "http://ua.be/sdo2l/vocabulary/formalisms/file#": "file",
  47. "http://ua.be/sdo2l/vocabulary/formalisms/ftg#": "ftg",
  48. "http://ua.be/sdo2l/vocabulary/formalisms/processtraces#": "traces",
  49. "http://ua.be/sdo2l/vocabulary/formalisms/shaclproperty#": "shaclproperty",
  50. "http://ua.be/sdo2l/vocabulary/formalisms/system#": "system",
  51. "http://ua.be/sdo2l/vocabulary/formalisms/violation#": "violation",
  52. "http://ua.be/sdo2l/vocabulary/formalisms/code#": "code",
  53. "http://ua.be/sdo2l/vocabulary/formalisms/component#": "component",
  54. "http://ua.be/sdo2l/vocabulary/formalisms/cs_as#": "csas",
  55. "http://ua.be/sdo2l/vocabulary/formalisms/dict#": "dict",
  56. "http://ua.be/sdo2l/vocabulary/formalisms/drawio#": "drawio",
  57. "http://ua.be/sdo2l/vocabulary/formalisms/object_diagram#": "object_diagram",
  58. "http://ua.be/sdo2l/vocabulary/formalisms/pm#": "pm",
  59. "http://ua.be/sdo2l/vocabulary/formalisms/requirement#": "requirement",
  60. "http://ua.be/sdo2l/vocabulary/formalisms/richtext#": "richtext",
  61. "http://ua.be/sdo2l/vocabulary/formalisms/script#": "script",
  62. "http://ua.be/sdo2l/vocabulary/formalisms/tabular#": "tabular",
  63. "http://ua.be/sdo2l/vocabulary/formalisms/text#": "text",
  64. "http://ua.be/sdo2l/vocabulary/formalisms/traceability_model#": "traceability_model",
  65. "http://ua.be/sdo2l/vocabulary/formalisms/xopp#": "xopp",
  66. "http://ua.be/sdo2l/vocabulary/federation#": "federation",
  67. "http://ua.be/sdo2l/description/artifacts/FTG_ftg#": "artifacts_ftg",
  68. }
  69. def perform_query(endpoint_uri: str, query_text: str, post: bool = False) -> dict:
  70. """"""
  71. endpoint = owly.Endpoint(endpoint_uri)
  72. try:
  73. result = endpoint.perform_query(query_text)
  74. except URLError:
  75. raise
  76. except json.decoder.JSONDecodeError:
  77. raise
  78. except ssl.SSLCertVerificationError:
  79. raise
  80. return result.as_json()