query.py 3.2 KB

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