{ "cells": [ { "cell_type": "markdown", "id": "d9c2ca8f", "metadata": {}, "source": [ "# Perform a BoM query" ] }, { "cell_type": "markdown", "id": "0491f42b", "metadata": {}, "source": [ "Both impacted substances queries and compliance queries can be performed on an XML BoM instead of a list\n", "of Granta MI records. The BoM must be in the Granta 17/11 format or Granta 23/01 format. This example shows how to use\n", "the ``lxml`` package with the XSD XML schema file to validate the XML format." ] }, { "cell_type": "markdown", "id": "d5601e42", "metadata": {}, "source": [ "For help on constructing an XML BoM, consult the\n", "[Creating a Bill of Materials from an external data source](3-5_Creating_a_Bill_of_Materials.py) example." ] }, { "cell_type": "markdown", "id": "b2435f4e", "metadata": {}, "source": [ "If the XML file is generated by a Granta MI product and has not been modified, it is possible to skip this step before\n", "submitting the query. However, it is strongly advised to validate the XML BoM in all situations to avoid unexpected\n", "server-side failures. If an invalid XML file is used in a query, an exception is raised by the ``requests`` HTTP\n", "package, but it does not contain information about why the XML is non-compliant. A more detailed log is\n", "reported on the server in the MI Service Layer log." ] }, { "cell_type": "markdown", "id": "2517dff2", "metadata": {}, "source": [ "The following supporting files are required for this example:\n", "\n", "* [bom-complex.xml](supporting-files/bom-complex.xml)\n", "* [invalid-bom.xml](supporting-files/invalid-bom.xml)" ] }, { "cell_type": "markdown", "id": "e076d936", "metadata": {}, "source": [ "The XSD XML schema is included in the library in the ``schemas`` sub-package." ] }, { "cell_type": "markdown", "id": "bef1996b", "metadata": {}, "source": [ "## Validating an XML file with an XSD schema" ] }, { "cell_type": "markdown", "id": "06a39e1d", "metadata": {}, "source": [ "The ``lxml`` package provides a similar API to the standard library xml module, but it includes some additional\n", "functionality, including schema validation. First import the ``lxml`` package and then build a simple validator\n", "function that takes both the XML file and schema file and returns the result." ] }, { "cell_type": "code", "execution_count": null, "id": "8b2b7a74", "metadata": {}, "outputs": [], "source": [ "from lxml import etree\n", "\n", "\n", "def xml_validator(xml: str, schema_file: str) -> bool:\n", " schema = etree.XMLSchema(file=schema_file)\n", " doc = etree.fromstring(xml)\n", " return schema.validate(doc)\n", "\n", "\n", "# You can now use this function to test the validity of a BoM generated by BoM Analyzer." ] }, { "cell_type": "code", "execution_count": null, "id": "a1a14dbb", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics.schemas import bom_schema_1711\n", "\n", "valid_xml_file = \"supporting-files/bom-complex.xml\"\n", "with open(valid_xml_file) as f:\n", " valid_xml = f.read()\n", "result = xml_validator(valid_xml, bom_schema_1711)\n", "result" ] }, { "cell_type": "markdown", "id": "cc8c795c", "metadata": {}, "source": [ "You can also test a BoM that is valid XML but is not compliant with the schema." ] }, { "cell_type": "code", "execution_count": null, "id": "84af3cec", "metadata": {}, "outputs": [], "source": [ "invalid_xml_file = \"supporting-files/invalid-bom.xml\"\n", "with open(invalid_xml_file) as f:\n", " invalid_xml = f.read()\n", "result = xml_validator(invalid_xml, bom_schema_1711)\n", "result" ] }, { "cell_type": "markdown", "id": "2238ae18", "metadata": {}, "source": [ "## Run an impacted substances XML-based query" ] }, { "cell_type": "markdown", "id": "13b04435", "metadata": {}, "source": [ "Now that you have validated the XML, you can build your XML BoM-based query.\n", "First, connect to Granta MI." ] }, { "cell_type": "code", "execution_count": null, "id": "4ccb2b3f", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()" ] }, { "cell_type": "markdown", "id": "bb82a531", "metadata": {}, "source": [ "The impacted substances BoM query behaves similar to other impacted substances queries. However,\n", "a BoM query can only accept a single BoM at a time, and so you only ever receive a single list\n", "of substances impacted by the specified legislations." ] }, { "cell_type": "code", "execution_count": null, "id": "0cd11f6e", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import queries\n", "\n", "SIN_LIST = \"SINList\"\n", "impacted_substances_query = (\n", " queries.BomImpactedSubstancesQuery()\n", " .with_bom(valid_xml)\n", " .with_legislation_ids([SIN_LIST])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9414e951", "metadata": {}, "outputs": [], "source": [ "impacted_substances_result = cxn.run(impacted_substances_query)\n", "impacted_substances_result" ] }, { "cell_type": "markdown", "id": "f971f1b9", "metadata": {}, "source": [ "The ``BomImpactedSubstancesQueryResult`` object returned after running the query for impacted substances now behaves\n", "similarly to the result object for any other query for impacted substances. For example, you can print all substances\n", "impacted by the legislation using an approach from the previous example." ] }, { "cell_type": "code", "execution_count": null, "id": "b2117fdb", "metadata": {}, "outputs": [], "source": [ "from tabulate import tabulate\n", "\n", "rows = [\n", " [substance.cas_number, substance.max_percentage_amount_in_material]\n", " for substance in impacted_substances_result.impacted_substances\n", "]\n", "print(f'Substances impacted by \"{SIN_LIST}\" in a BoM (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "39d20506", "metadata": {}, "source": [ "## Running a compliance XML-based query" ] }, { "cell_type": "markdown", "id": "6af0f099", "metadata": {}, "source": [ "Running a BoM compliance query produces the same result as if you had stored the BoM structure as linked **Products\n", "and Parts** records in Granta MI." ] }, { "cell_type": "code", "execution_count": null, "id": "845362d5", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import indicators\n", "\n", "svhc = indicators.WatchListIndicator(\n", " name=\"SVHC\",\n", " legislation_ids=[\"Candidate_AnnexXV\"],\n", " default_threshold_percentage=0.1,\n", ")\n", "compliance_query = (\n", " queries.BomComplianceQuery()\n", " .with_bom(valid_xml)\n", " .with_indicators([svhc])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "9b08b55b", "metadata": {}, "outputs": [], "source": [ "compliance_result = cxn.run(compliance_query)\n", "compliance_result" ] }, { "cell_type": "markdown", "id": "e5629ba6", "metadata": {}, "source": [ "The ``BomComplianceQueryResult`` object returned after running the compliance query contains a list of\n", "``PartWithComplianceResult`` objects, the behavior of which has been covered in a previous example.\n", "The following cell prints the compliance status of the BoM." ] }, { "cell_type": "code", "execution_count": null, "id": "019183cc", "metadata": {}, "outputs": [], "source": [ "root_part = compliance_result.compliance_by_part_and_indicator[0]\n", "print(f\"BoM Compliance Status: {root_part.indicators['SVHC'].flag.name}\")" ] }, { "cell_type": "markdown", "id": "2b9f4b4a", "metadata": {}, "source": [ "## Invalid BoM exception" ] }, { "cell_type": "markdown", "id": "ba918f54", "metadata": {}, "source": [ "If you were to try the same query with an invalid BoM, you would see a stack trace informing you that the MI Service\n", "Layer responded with a 500 HTTP response code. To raise the exception, change the constant ``RUN_QUERY``below to\n", "``True``." ] }, { "cell_type": "code", "execution_count": null, "id": "d8863e3f", "metadata": {}, "outputs": [], "source": [ "broken_query = (\n", " queries.BomImpactedSubstancesQuery()\n", " .with_bom(invalid_xml)\n", " .with_legislation_ids([SIN_LIST])\n", ")\n", "\n", "RUN_QUERY = False\n", "\n", "if RUN_QUERY:\n", " cxn.run(broken_query)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }