query.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 (use dict from config + merge per query)
  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. "http://ua.be/sdo2l/description/artifacts/FTG_ftg#": "artifacts_ftg",
  66. }
  67. def perform_query(endpoint_uri: str, query_text: str, post: bool = False) -> dict:
  68. """"""
  69. endpoint = owly.Endpoint(endpoint_uri)
  70. try:
  71. result = endpoint.perform_query(query_text)
  72. except URLError:
  73. raise
  74. except json.decoder.JSONDecodeError:
  75. raise
  76. return result.as_json()