petter2025 commited on
Commit
465b6f9
·
verified ·
1 Parent(s): d0da04a

Create ontology_reasoner.py

Browse files
Files changed (1) hide show
  1. ontology_reasoner.py +75 -10
ontology_reasoner.py CHANGED
@@ -1,4 +1,7 @@
1
- # ontology_reasoner.py
 
 
 
2
  import logging
3
  logger = logging.getLogger(__name__)
4
 
@@ -11,20 +14,82 @@ except ImportError:
11
  class InfraOntology:
12
  def __init__(self, path="infra.owl"):
13
  self.available = OWLREADY_AVAILABLE
 
14
  if self.available:
15
  try:
16
  self.onto = get_ontology(path).load()
17
- except:
 
 
18
  self.available = False
19
- logger.warning("Ontology load failed, using mock")
20
  else:
21
  logger.info("Owlready2 not installed, ontology disabled")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- def classify(self, component_type):
24
- if not self.available:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  return {"inferred": [], "consistent": True}
26
- # Run reasoner and return results
27
- with self.onto:
28
- sync_reasoner()
29
- # ... extract inferences
30
- return {"inferred": ["NetworkDevice"], "consistent": True}
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Owlready2 wrapper for a small infrastructure ontology.
3
+ Provides classification and consistency checking.
4
+ """
5
  import logging
6
  logger = logging.getLogger(__name__)
7
 
 
14
  class InfraOntology:
15
  def __init__(self, path="infra.owl"):
16
  self.available = OWLREADY_AVAILABLE
17
+ self.onto = None
18
  if self.available:
19
  try:
20
  self.onto = get_ontology(path).load()
21
+ logger.info(f"Ontology loaded from {path}")
22
+ except Exception as e:
23
+ logger.warning(f"Failed to load ontology: {e}")
24
  self.available = False
 
25
  else:
26
  logger.info("Owlready2 not installed, ontology disabled")
27
+
28
+ def classify(self, component_type: str) -> dict:
29
+ """
30
+ Run reasoner and return inferred classes for a given component type.
31
+ Returns dict with 'inferred' list and 'consistent' bool.
32
+ """
33
+ if not self.available or self.onto is None:
34
+ return {"inferred": [], "consistent": True}
35
+
36
+ try:
37
+ with self.onto:
38
+ sync_reasoner()
39
+ # Find all individuals of the given type (simplified)
40
+ inferred = []
41
+ # Example: if component_type == "server", get all Server instances
42
+ if hasattr(self.onto, component_type.capitalize()):
43
+ cls = getattr(self.onto, component_type.capitalize())
44
+ inferred = [str(i) for i in cls.instances()]
45
+ return {"inferred": inferred, "consistent": True}
46
+ except Exception as e:
47
+ logger.error(f"Reasoning error: {e}")
48
+ return {"inferred": [], "consistent": False, "error": str(e)}"""
49
+ Owlready2 wrapper for a small infrastructure ontology.
50
+ Provides classification and consistency checking.
51
+ """
52
+ import logging
53
+ logger = logging.getLogger(__name__)
54
+
55
+ try:
56
+ from owlready2 import *
57
+ OWLREADY_AVAILABLE = True
58
+ except ImportError:
59
+ OWLREADY_AVAILABLE = False
60
 
61
+ class InfraOntology:
62
+ def __init__(self, path="infra.owl"):
63
+ self.available = OWLREADY_AVAILABLE
64
+ self.onto = None
65
+ if self.available:
66
+ try:
67
+ self.onto = get_ontology(path).load()
68
+ logger.info(f"Ontology loaded from {path}")
69
+ except Exception as e:
70
+ logger.warning(f"Failed to load ontology: {e}")
71
+ self.available = False
72
+ else:
73
+ logger.info("Owlready2 not installed, ontology disabled")
74
+
75
+ def classify(self, component_type: str) -> dict:
76
+ """
77
+ Run reasoner and return inferred classes for a given component type.
78
+ Returns dict with 'inferred' list and 'consistent' bool.
79
+ """
80
+ if not self.available or self.onto is None:
81
  return {"inferred": [], "consistent": True}
82
+
83
+ try:
84
+ with self.onto:
85
+ sync_reasoner()
86
+ # Find all individuals of the given type (simplified)
87
+ inferred = []
88
+ # Example: if component_type == "server", get all Server instances
89
+ if hasattr(self.onto, component_type.capitalize()):
90
+ cls = getattr(self.onto, component_type.capitalize())
91
+ inferred = [str(i) for i in cls.instances()]
92
+ return {"inferred": inferred, "consistent": True}
93
+ except Exception as e:
94
+ logger.error(f"Reasoning error: {e}")
95
+ return {"inferred": [], "consistent": False, "error": str(e)}