query.py 3.4 KB

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