fuzzy_dl_owl2.fuzzyowl2.parser.owl2_parser
A specialized parser that interprets Fuzzy OWL 2 XML annotations and transforms them into a knowledge base and query objects using the pyparsing library.
Description
The implementation relies on the pyparsing library to define a comprehensive grammar capable of recognizing complex fuzzy logic constructs embedded within XML-like tags. It handles a wide variety of fuzzy elements, including modified concepts, weighted aggregations, integral-based concepts like OWA and Sugeno, and specific fuzzy datatypes such as triangular or trapezoidal functions. As the input is processed, a series of internal callback functions transform raw token sequences into strongly typed domain objects, effectively bridging the gap between text representation and the internal object model. Parsing orchestration is managed through a central class that exposes the grammar definition and provides a static method to initiate the translation of annotation strings into a knowledge base and associated queries.
Classes
This class provides a specialized parser for the Fuzzy OWL 2 ontology language, designed to interpret XML-based annotation strings and transform them into structured data objects, specifically a KnowledgeBase and a list of Query instances. It leverages the pyparsing library to construct a detailed grammar capable of handling complex fuzzy logic constructs, such as modified concepts, weighted aggregations (including OWA and Choquet integrals), fuzzy datatypes (e.g., triangular or trapezoidal), and axioms with degree definitions. The primary interface is the static main method, which orchestrates configuration loading and parsing while managing error handling, though direct access to the underlying grammar and parsing logic is available via get_grammatics and parse_string. |
Functions
Parses a list of tokens resulting from a grammar parsing operation to construct a specific fuzzy datatype object. The function inspects the first token to determine the type of fuzzy function to instantiate, supporting left shoulder, right shoulder, linear, triangular, and trapezoidal functions. It extracts the necessary parameters from the subsequent tokens to initialize the corresponding function objects. If the first token does not match a known fuzzy keyword, the original tokens are returned unchanged. Additionally, the function logs the input tokens for debugging purposes. |
|
|
This internal parsing function constructs a FuzzyNominalConcept instance from a pyparsing.ParseResults object. It converts the input tokens into a standard list and extracts the first two elements to serve as arguments for the concept constructor. The function triggers a debug log operation with the input tokens before returning the new concept definition. Note that the function assumes the input list contains at least two elements; otherwise, an IndexError will occur. |
This internal helper function processes a list of parsed tokens to construct a specific fuzzy integral concept definition based on the identified keyword. It inspects the first element of the token list to determine the integral type—checking against OWA, Sugeno, Quasi-Sugeno, or Choquet keywords—and instantiates the corresponding ConceptDefinition subclass, passing the second and third elements of the list as arguments to the constructor. The function logs the input tokens for debugging purposes; however, it assumes the token list contains at least three elements and matches one of the expected keywords, potentially raising an IndexError or returning None implicitly if these conditions are not met. |
|
This internal helper function serves as a parsing callback to construct a domain object from raw pyparsing tokens. It converts the input ParseResults into a list and instantiates a ModifiedConcept using the first two elements found in the token sequence. The function performs a side effect of logging the input tokens for debugging purposes. It assumes that the input tokens contain at least two elements; failure to meet this condition will result in an IndexError. |
|
This internal helper function processes a list of parsing tokens to construct a specific fuzzy modifier object based on the keyword found at the beginning of the list. If the first token corresponds to a linear modifier, it instantiates a LinearModifier using the subsequent token as an argument. If the first token indicates a triangular modifier, it instantiates a TriangularModifier using the following three tokens as parameters. In cases where the keyword is unrecognized, the function returns the original input tokens unchanged. Additionally, the function logs the incoming tokens for debugging purposes. |
|
|
This internal helper function processes parsing results to construct a ModifiedProperty instance. It expects the input tokens to contain a ModifiedConcept object as the first element; otherwise, it raises an AssertionError. The function extracts the fuzzy modifier and fuzzy concept from this ModifiedConcept and uses them to instantiate the new ModifiedProperty. Additionally, it logs the incoming tokens for debugging purposes. |
|
This internal function processes the results of a pyparsing match to construct a QowaConcept instance. It converts the input ParseResults object into a standard list and extracts the first two elements to pass as arguments to the QowaConcept constructor. The function logs the incoming tokens for debugging purposes before returning the constructed concept definition. It assumes the input tokens contain at least two elements; otherwise, an IndexError will occur. |
Constructs a specific weighted complex concept definition from a list of parsed tokens, typically used as a callback or action within a parsing grammar. The function interprets the first element of the token list as a fuzzy OWL2 keyword determining the aggregation logic—such as weighted maximum, minimum, sum, or sum-zero—and treats all subsequent elements as operands. It asserts that these operands are instances of WeightedConcept before instantiating and returning the appropriate concrete class (e.g., WeightedMaxConcept or WeightedSumConcept) containing the list of weighted concepts. This process includes a debug logging step and will raise an AssertionError if the operand types are invalid. |
|
This function serves as a parse action to transform raw parsing tokens into a structured WeightedConcept object. It accepts a pyparsing.ParseResults object, converts it into a list, and extracts the first two elements to initialize the WeightedConcept. The function logs the input tokens for debugging purposes before performing the conversion. Note that if the input token list contains fewer than two elements, this function will raise an IndexError. |
|
|
Converts the first element of a pyparsing ParseResults object into a numeric value, returning either an integer or a float. The function extracts the initial token, converts it to a float, and checks if the value is mathematically an integer; if so, it returns an int, otherwise it returns the float. This is typically employed as a parse action to transform string matches into their appropriate Python numeric types, though it will raise errors if the token list is empty or the string is not a valid number representation. |
Module Contents
UML Class Diagram for FuzzyOwl2Parser
- class FuzzyOwl2Parser[source]
Bases:
objectThis class provides a specialized parser for the Fuzzy OWL 2 ontology language, designed to interpret XML-based annotation strings and transform them into structured data objects, specifically a KnowledgeBase and a list of Query instances. It leverages the pyparsing library to construct a detailed grammar capable of handling complex fuzzy logic constructs, such as modified concepts, weighted aggregations (including OWA and Choquet integrals), fuzzy datatypes (e.g., triangular or trapezoidal), and axioms with degree definitions. The primary interface is the static main method, which orchestrates configuration loading and parsing while managing error handling, though direct access to the underlying grammar and parsing logic is available via get_grammatics and parse_string.
- static get_grammatics() pyparsing.ParserElement[source]
This function generate the grammatics to parse the predicate wih formula “formula”.
- Parameters:
formula (
= The predicate formula used for the parsing.)- Return type:
The parsed result given by pyparsing.
- static main(
- annotation: str,
- *args,
- _parse_fuzzy_datatype(tokens: pyparsing.ParseResults) fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_datatype.FuzzyDatatype[source]
Parses a list of tokens resulting from a grammar parsing operation to construct a specific fuzzy datatype object. The function inspects the first token to determine the type of fuzzy function to instantiate, supporting left shoulder, right shoulder, linear, triangular, and trapezoidal functions. It extracts the necessary parameters from the subsequent tokens to initialize the corresponding function objects. If the first token does not match a known fuzzy keyword, the original tokens are returned unchanged. Additionally, the function logs the input tokens for debugging purposes.
- Parameters:
tokens (pp.ParseResults) – Parsed tokens representing a fuzzy datatype definition, containing the function type keyword and its associated numeric parameters.
- Returns:
A specific FuzzyDatatype instance (such as LeftShoulderFunction or TriangularFunction) constructed from the parsed tokens based on the identified keyword. Returns the original tokens if the input does not match a known fuzzy datatype pattern.
- Return type:
- _parse_fuzzy_nominal(tokens: pyparsing.ParseResults) fuzzy_dl_owl2.fuzzyowl2.owl_types.concept_definition.ConceptDefinition[source]
This internal parsing function constructs a FuzzyNominalConcept instance from a pyparsing.ParseResults object. It converts the input tokens into a standard list and extracts the first two elements to serve as arguments for the concept constructor. The function triggers a debug log operation with the input tokens before returning the new concept definition. Note that the function assumes the input list contains at least two elements; otherwise, an IndexError will occur.
- Parameters:
tokens (pp.ParseResults) – Parsed results containing the elements required to construct a FuzzyNominalConcept.
- Returns:
A FuzzyNominalConcept instance constructed from the parsed tokens, representing a fuzzy nominal concept definition.
- Return type:
- _parse_integral_concept(
- tokens: pyparsing.ParseResults,
This internal helper function processes a list of parsed tokens to construct a specific fuzzy integral concept definition based on the identified keyword. It inspects the first element of the token list to determine the integral type—checking against OWA, Sugeno, Quasi-Sugeno, or Choquet keywords—and instantiates the corresponding ConceptDefinition subclass, passing the second and third elements of the list as arguments to the constructor. The function logs the input tokens for debugging purposes; however, it assumes the token list contains at least three elements and matches one of the expected keywords, potentially raising an IndexError or returning None implicitly if these conditions are not met.
- Parameters:
tokens (pp.ParseResults) – The parsed results containing the integral type keyword followed by the necessary arguments to construct the concept definition.
- Returns:
A specific ConceptDefinition instance (OwaConcept, SugenoConcept, QsugenoConcept, or ChoquetConcept) corresponding to the integral type identified in the tokens.
- Return type:
- _parse_modified_concept(
- tokens: pyparsing.ParseResults,
This internal helper function serves as a parsing callback to construct a domain object from raw pyparsing tokens. It converts the input ParseResults into a list and instantiates a ModifiedConcept using the first two elements found in the token sequence. The function performs a side effect of logging the input tokens for debugging purposes. It assumes that the input tokens contain at least two elements; failure to meet this condition will result in an IndexError.
- Parameters:
tokens (pp.ParseResults) – The parsed results containing the matched tokens that constitute the modified concept.
- Returns:
A ModifiedConcept instance representing the parsed modified concept, constructed from the first two elements of the input tokens.
- Return type:
- _parse_modifier_function(tokens: pyparsing.ParseResults) fuzzy_dl_owl2.fuzzyowl2.owl_types.fuzzy_modifier.FuzzyModifier[source]
This internal helper function processes a list of parsing tokens to construct a specific fuzzy modifier object based on the keyword found at the beginning of the list. If the first token corresponds to a linear modifier, it instantiates a LinearModifier using the subsequent token as an argument. If the first token indicates a triangular modifier, it instantiates a TriangularModifier using the following three tokens as parameters. In cases where the keyword is unrecognized, the function returns the original input tokens unchanged. Additionally, the function logs the incoming tokens for debugging purposes.
- Parameters:
tokens (pp.ParseResults) – Parsed elements containing the modifier keyword and the associated arguments used to instantiate a specific FuzzyModifier object.
- Returns:
A FuzzyModifier instance (LinearModifier or TriangularModifier) initialized with the parsed parameters, or the raw tokens if the modifier type is unrecognized.
- Return type:
- _parse_property(tokens: pyparsing.ParseResults) fuzzy_dl_owl2.fuzzyowl2.owl_types.modified_property.ModifiedProperty[source]
This internal helper function processes parsing results to construct a ModifiedProperty instance. It expects the input tokens to contain a ModifiedConcept object as the first element; otherwise, it raises an AssertionError. The function extracts the fuzzy modifier and fuzzy concept from this ModifiedConcept and uses them to instantiate the new ModifiedProperty. Additionally, it logs the incoming tokens for debugging purposes.
- Parameters:
tokens (pp.ParseResults) – The parsed results expected to contain a ModifiedConcept as the primary element.
- Returns:
A ModifiedProperty instance constructed from the fuzzy modifier and concept extracted from the parsed tokens.
- Return type:
- _parse_q_owa_concept(tokens: pyparsing.ParseResults) fuzzy_dl_owl2.fuzzyowl2.owl_types.concept_definition.ConceptDefinition[source]
This internal function processes the results of a pyparsing match to construct a QowaConcept instance. It converts the input ParseResults object into a standard list and extracts the first two elements to pass as arguments to the QowaConcept constructor. The function logs the incoming tokens for debugging purposes before returning the constructed concept definition. It assumes the input tokens contain at least two elements; otherwise, an IndexError will occur.
- Parameters:
tokens (pp.ParseResults) – The parsed result object containing the sequence of elements extracted from the input, specifically the first two elements which are used to construct the QowaConcept.
- Returns:
A ConceptDefinition object representing the parsed Q-OWA concept, initialized with the first two elements of the parsed tokens.
- Return type:
- _parse_weighted_complex_concept(
- tokens: pyparsing.ParseResults,
Constructs a specific weighted complex concept definition from a list of parsed tokens, typically used as a callback or action within a parsing grammar. The function interprets the first element of the token list as a fuzzy OWL2 keyword determining the aggregation logic—such as weighted maximum, minimum, sum, or sum-zero—and treats all subsequent elements as operands. It asserts that these operands are instances of WeightedConcept before instantiating and returning the appropriate concrete class (e.g., WeightedMaxConcept or WeightedSumConcept) containing the list of weighted concepts. This process includes a debug logging step and will raise an AssertionError if the operand types are invalid.
- Parameters:
tokens (pp.ParseResults) – Parsed elements where the first item is the aggregation operator keyword and the remaining items are WeightedConcept instances.
- Returns:
Returns a ConceptDefinition object representing a weighted aggregation (maximum, minimum, sum, or sum-zero) of the parsed weighted concepts.
- Return type:
- _parse_weighted_concept(
- tokens: pyparsing.ParseResults,
This function serves as a parse action to transform raw parsing tokens into a structured WeightedConcept object. It accepts a pyparsing.ParseResults object, converts it into a list, and extracts the first two elements to initialize the WeightedConcept. The function logs the input tokens for debugging purposes before performing the conversion. Note that if the input token list contains fewer than two elements, this function will raise an IndexError.
- Parameters:
tokens (pp.ParseResults) – Parsed results containing the concept and weight components required to construct a WeightedConcept.
- Returns:
A WeightedConcept instance representing the parsed weighted concept, constructed from the first two elements of the provided tokens.
- Return type:
- _to_number(tokens: pyparsing.ParseResults) float | int[source]
Converts the first element of a pyparsing ParseResults object into a numeric value, returning either an integer or a float. The function extracts the initial token, converts it to a float, and checks if the value is mathematically an integer; if so, it returns an int, otherwise it returns the float. This is typically employed as a parse action to transform string matches into their appropriate Python numeric types, though it will raise errors if the token list is empty or the string is not a valid number representation.
- Parameters:
tokens (pp.ParseResults) – Parsed results containing the string representation of the numeric value to be converted.
- Returns:
The numeric value of the first parsed token, returned as an int if the value is an integer, otherwise as a float.
- Return type:
float | int