fuzzy_dl_owl2.fuzzyowl2.parser.owl2_xml_parser
A specialized parser converts FuzzyOWL2 XML annotations into corresponding Python data structures representing fuzzy logic elements.
Description
The software interprets XML strings containing FuzzyOWL2 annotations to instantiate a wide variety of Python objects representing fuzzy logic constructs, such as concept definitions, fuzzy datatypes, and property definitions. By leveraging the standard library’s XML parsing capabilities, the logic inspects specific attributes within the XML structure to determine the appropriate class to construct, ranging from simple triangular functions to complex weighted or aggregated concepts like Choquet or Sugeno integrals. A central dispatch mechanism routes the parsing flow based on the annotation type, ensuring that nested elements, such as lists of weights or concept names, are correctly extracted and mapped to the corresponding object attributes. Additionally, the implementation integrates with an external configuration system to load necessary parameters before parsing and includes robust error handling to manage file access issues or malformed XML gracefully.
Classes
This class serves as a specialized parser for converting FuzzyOWL2 XML annotations into corresponding Python data structures. It interprets XML strings to instantiate a variety of objects representing fuzzy logic elements, including concept definitions (such as weighted or modified concepts), fuzzy datatypes (like triangular or trapezoidal functions), and property definitions. The parsing logic relies on inspecting specific XML attributes to determine the correct object type to construct. Additionally, the class provides functionality to load configuration parameters from an external file and includes error handling mechanisms to manage parsing or file access issues gracefully. |
Module Contents
UML Class Diagram for FuzzyOwl2XMLParser
- class FuzzyOwl2XMLParser[source]
Bases:
objectThis class serves as a specialized parser for converting FuzzyOWL2 XML annotations into corresponding Python data structures. It interprets XML strings to instantiate a variety of objects representing fuzzy logic elements, including concept definitions (such as weighted or modified concepts), fuzzy datatypes (like triangular or trapezoidal functions), and property definitions. The parsing logic relies on inspecting specific XML attributes to determine the correct object type to construct. Additionally, the class provides functionality to load configuration parameters from an external file and includes error handling mechanisms to manage parsing or file access issues gracefully.
- Raises:
ValueError – Raised when the parsed XML string contains an unsupported, unrecognized, or missing annotation type that does not correspond to any of the defined FuzzyOWL2 elements (Concept, Datatype, Modifier, Axiom, Ontology, or Role).
- static get_caseless_attrib(attrib: dict[str, str], key: str) str | None[source]
This static method retrieves a value from a dictionary of attributes by performing a case-insensitive lookup on the provided key. It scans the dictionary for keys that match the target key when both are converted to lowercase, returning the value associated with the first such match found. If no matching key exists, the method returns None. Note that if the input dictionary contains multiple keys that differ only by case, the value returned corresponds to the first match encountered during iteration, which depends on the dictionary’s insertion order.
- Parameters:
attrib (dict[str, str]) – A dictionary containing the attributes to search.
key (str) – The name of the attribute to retrieve, matched case-insensitively.
- Returns:
The value of the attribute if a case-insensitive match is found, otherwise None.
- Return type:
Optional[str]
- static load_config(*args) None[source]
This static method loads configuration parameters by reading a “CONFIG.ini” file located in the current working directory. It acts as a wrapper that forwards any provided arguments to the underlying ConfigReader.load_parameters method to facilitate the parsing and application of settings. The operation modifies the global or class-level configuration state but does not return a value. Note that this method relies on the specific execution context, as it will fail to locate the configuration file if the current working directory does not contain “CONFIG.ini”.
- Parameters:
args (Any) – Additional arguments passed directly to the underlying configuration loader.
- static main(
- annotation: str,
- *args,
This static method serves as the primary entry point for parsing FuzzyOWL2 XML strings into structured Python objects. It initializes the parser’s configuration using the provided arguments before processing the input annotation, ensuring that necessary settings are loaded prior to parsing. The method returns a parsed entity, which may be a ConceptDefinition, FuzzyDatatype, PropertyDefinition, FuzzyModifier, or a primitive value, depending on the content of the XML. In the event of a missing configuration file or a general parsing exception, the method logs the error and traceback details to the standard error output and returns None implicitly, allowing the program to handle failures gracefully without crashing.
- Parameters:
annotation (str) – The FuzzyOWL2 XML string to be parsed.
args (Any) – Variable length argument list passed to the configuration loader, typically starting with the path to the configuration file.
- Returns:
The parsed representation of the FuzzyOWL2 XML annotation, which may be a ConceptDefinition, FuzzyDatatype, PropertyDefinition, FuzzyModifier, float, or str depending on the input content. Returns None if parsing or configuration fails.
- Return type:
Union[ConceptDefinition, FuzzyDatatype, PropertyDefinition, FuzzyModifier, float, str]
- static parse_string(
- instring: str,
Parses a string containing FuzzyOWL2 XML and constructs the corresponding Python representation based on the structure and attributes of the XML. The method inspects the root element’s type annotation to dispatch the parsing logic to specific handlers for concepts, datatypes, modifiers, axioms, ontology settings, or roles. Depending on the content, it returns specialized objects such as ModifiedConcept, TriangularFunction, or LinearModifier, or primitive values like floats for axiom degrees and strings for logic types. During execution, the method logs the XML structure for debugging purposes. It raises an AssertionError if the root element does not match the expected FuzzyOWL2 tag and a ValueError if the fuzzy type is unsupported.
- Parameters:
instring (str) – A string containing the FuzzyOWL2 XML data to be parsed.
- Raises:
ValueError – Raised if the input XML string does not specify a valid or supported FuzzyOWL2 annotation type.
- Returns:
Returns a Python object representing the parsed FuzzyOWL2 element. The specific type is determined by the XML’s fuzzyType attribute and may be a ConceptDefinition, FuzzyDatatype, PropertyDefinition, FuzzyModifier, a float (for axiom degrees), or a str (for ontology logic).
- Return type:
Union[ConceptDefinition, FuzzyDatatype, PropertyDefinition, FuzzyModifier, float, str]