Class ComponentQuery

java.lang.Object
neqsim.mcp.runners.ComponentQuery

public final class ComponentQuery extends Object
Queries the NeqSim component database for MCP and API integration.

Provides fast, stateless lookup of component names for validation, search, and fuzzy matching. All methods return JSON strings for direct use by MCP tool wrappers.

Component names are loaded once from the resource file neqsim_component_names.txt and cached in memory. For richer property queries, the H2 database (COMP table) is used.

Version:
1.0
Author:
Even Solbraa
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static List<String>
    Cached list of all component names (trimmed, original case).
    private static List<String>
    Cached list of all component names in lower case for matching.
    private static final org.apache.logging.log4j.Logger
    Logger object for class.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Finds the closest matching component name for a possibly misspelled input using edit distance.
    private static void
    Loads component names from the resource file if not already cached.
    private static String
    errorJson(String code, String message, String remediation)
    Creates a standard error JSON response.
    static List<String>
    Returns all known component names.
    static String
    Returns detailed information for a specific component as JSON.
    static boolean
    Checks whether a component name exists in the database.
    (package private) static int
    Computes the Levenshtein edit distance between two strings.
    private static void
    loadFromDatabase(List<String> names, List<String> namesLower)
    Loads component names from the H2 database as a fallback.
    private static double
    Safely parses a string to double, returning 0.0 on failure.
    static String
    search(String query)
    Searches for components whose name contains the query string (case-insensitive).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      private static final org.apache.logging.log4j.Logger logger
      Logger object for class.
    • cachedNames

      private static volatile List<String> cachedNames
      Cached list of all component names (trimmed, original case).
    • cachedNamesLower

      private static volatile List<String> cachedNamesLower
      Cached list of all component names in lower case for matching.
  • Constructor Details

    • ComponentQuery

      private ComponentQuery()
      Private constructor to prevent instantiation.
  • Method Details

    • getAllNames

      public static List<String> getAllNames()
      Returns all known component names.
      Returns:
      unmodifiable list of component names
    • isValid

      public static boolean isValid(String name)
      Checks whether a component name exists in the database.
      Parameters:
      name - the component name to check (case-insensitive)
      Returns:
      true if the component is known
    • search

      public static String search(String query)
      Searches for components whose name contains the query string (case-insensitive).
      Parameters:
      query - the search term (substring match)
      Returns:
      JSON string with an array of matching component names
    • closestMatch

      public static String closestMatch(String name)
      Finds the closest matching component name for a possibly misspelled input using edit distance.
      Parameters:
      name - the possibly misspelled name
      Returns:
      the closest valid component name, or null if no reasonable match
    • getInfo

      public static String getInfo(String name)
      Returns detailed information for a specific component as JSON.

      Queries the H2 database COMP table for properties like molar mass, critical temperature, critical pressure, and acentric factor.

      Parameters:
      name - the exact component name
      Returns:
      JSON string with component properties, or error if not found
    • ensureLoaded

      private static void ensureLoaded()
      Loads component names from the resource file if not already cached.
    • loadFromDatabase

      private static void loadFromDatabase(List<String> names, List<String> namesLower)
      Loads component names from the H2 database as a fallback.
      Parameters:
      names - list to populate with component names
      namesLower - list to populate with lower-case names
    • levenshteinDistance

      static int levenshteinDistance(String a, String b)
      Computes the Levenshtein edit distance between two strings.
      Parameters:
      a - first string
      b - second string
      Returns:
      the edit distance
    • safeDouble

      private static double safeDouble(String value)
      Safely parses a string to double, returning 0.0 on failure.
      Parameters:
      value - the string value
      Returns:
      the parsed double, or 0.0
    • errorJson

      private static String errorJson(String code, String message, String remediation)
      Creates a standard error JSON response.
      Parameters:
      code - error code
      message - error description
      remediation - fix suggestion
      Returns:
      JSON string