---
This page is part of [Modelon](https://www.modelon.com). For a complete overview of this site's content, entity data, and subject matter expertise, see our [AI Content Index](https://www.modelon.com/llms.txt) or [Full Content Archive](https://www.modelon.com/llms-full.txt). For structured business identity data, see our [Entity Card](https://www.modelon.com/wp-json/bc-geodesic/v1/entity-card).
---

# Extracting Library Statistics using OPTIMICA Compiler Toolkit API
> Have you ever wondered how many components, functions and models are in your library? Modelon’s OPTIMICA Compiler Toolkit has the capability to enable users to...

**URL:** https://www.modelon.com/blog/extracting-library-statistics-using-optimica-compiler-toolkit-api/
**Type:** Post
**Modified:** 2022-06-30

---

![Optimica Compiler Toolkit](https://modelon.com/wp-content/uploads/2022/06/Optimica-Compiler-Toolkit.jpg)

*Have you ever wondered how many components, functions and models are in your library? Modelon’s OPTIMICA Compiler Toolkit has the capability to enable users to retrieve any essential information using the compiler API.

This blog introduces the OPTIMICA API – along with its purpose, functionality, and highlights how it can be used to compute library statistics.*

#### OPTIMICA API Introduction

The OPTMICA API is a set of methods which are designed for users to interact with Modelica models and libraries. The purpose of the API is to: 1) support extensions on top of OPTMICA, such as editors, and 2) support customizations and exclusive use cases where there is necessary to directly interact with a Modelica compiler.

Through the API, a user can browse through the variables and components in a model or library, compile FMUs, browse through annotations and connect equations, render icons and much more. The API can additionally handle authentication and authorization for using together with encrypted libraries.

OPTMICA is written in Java and thus the native interface, using the API, is through Java. It can be used in other languages through Java interfaces that are commonly available.

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API.jpg)

#### Using OPTIMICA API

So how does it work? To start working with the API we need to create a new instance of the API class which we then use to load Modelica libraries (or single models).

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-1.jpg)

Once the library is loaded we can start working on the library, retrieve the models, browse through the packages etc. Compiling an FMU can be easily achieved by,

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-2.jpg)

This will compile the model to an FMU in the current directory. The returned object can be used to retrieve specific data and information that was generated during the compilation process, such as log messages and paths to the FMU.

One key feature is to start from a Modelica class (package or model for instance) and investigate what it contains. Consider a Modelica package outlined as follows,

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-3.jpg)

To investigate this package, there are two different “trees” that can be used, the source tree and the instance tree. It is important to understand the difference between the two and in which situations the respective tree should be used.

1. **The source tree**, reflects the structure of the parsed Modelica file and very little semantic interpretation has been done. It is not possible to resolve variable references nor compute the value of expressions in this tree. Instead the intent is that this tree will be used to retrieve structural information about Modelica files and libraries.
2. **The instance tree**, enables a large amount of semantic interpretation to be done and can resolve variable references and determine the value of expressions. It also handles and resolves modifications and redeclares. Because it supports these operations, the instance tree also allows exploration of components and its contents, e.g. it is possible to list the subcomponents in a non-primitive component. This makes this tree ideal for retrieving information about a specific model.

To access either the source or instance tree and start investigating the model, either of the below can be used.

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-4.jpg)

To view the children of the source class, we list them using (source/instance)Class.getElements() and from there we can continue to dig deeper down in the model.

#### Computing Library Statistics

So, what about the statistics? To compute the statistics, we need to loop over all the elements in a package and recursively go deeper down in the hierarchy and count all occurrences of packages, models, functions and blocks. We start from the top-level package, which in this case is MSL.

The top-level packages are retrieved using,

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-5.jpg)

These are then iterated over using,

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-6.jpg)

The method countStructureRecursive is a method that contains this loop, the map libraryStatistics contains the statistics about the library and is populated before the loop for each specialization (model, function, block etc).

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-7.jpg)

Essentially, this is all that is necessary to compute the statistics of a library. For all details I refer to the provided file which contains the complete program.

So, with the above we compute for MSL (v. 3.2.2 build 3):

![Optimica API](https://modelon.com/wp-content/uploads/2022/06/Optimica-API-8.jpg)

From the above, we note that there have been a few updates in the library since the statistics was updated on Modelica.org which lists about 1350 functions.

#### Want More Information?

This blog briefly touches upon what the capabilities are and what can be achieved using the compiler API. More documentation regarding the API is available in the users-guide that is included in the OCT installation.

For now, the focus of the API has been on collecting information about models and libraries, not on manipulating models. We are though continuously increasing the scope of the API and adding new functionality.

#### **[Download the JAVA program referenced. ](https://modelon.com/fileadmin/user_upload/Blog/CA/ComputeLibraryStatistics.java)**

#### [Learn more about OPTIMICA.](https://modelon.com/hidden/optimica-compiler-toolkit/)
## Site Description

Modelon is revolutionizing the engineering design industry by offering technologies and services that enable customers to leverage system simulation. Modelon’s flagship product, Modelon Impact, is a cloud system simulation platform that helps engineers virtually design, analyze, and simulate physical systems. Our team brings deep industry expertise and is dedicated to guiding our customers in creating innovative technologies at their respective organizations. Headquartered in Lund, Sweden, Modelon is a global company with offices in Germany, India, Japan, and the United States. We believe that system simulation should be accessible to every engineer and are dedicated to being an open-standard platform company.


---
**About this site:** Modelon — Modelon is revolutionizing the engineering design industry by offering technologies and services that enable customers to leverage system simulation. Modelon’s flagship product, Modelon Impact, is a cloud system simulation platform that helps engineers virtually design, analyze, and simulate physical systems. Our team brings deep industry expertise and is dedicated to guiding our customers in creating innovative technologies at their respective organizations. Headquartered in Lund, Sweden, Modelon is a global company with offices in Germany, India, Japan, and the United States. We believe that system simulation should be accessible to every engineer and are dedicated to being an open-standard platform company.. [AI Content Index](https://www.modelon.com/llms.txt) | [Full Site Content](https://www.modelon.com/llms-full.txt) | [Entity Card](https://www.modelon.com/wp-json/bc-geodesic/v1/entity-card)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.modelon.com/#organization","name":"Modelon","url":"https://www.modelon.com","additionalType":"http://productontology.org/id/Software_engineering","description":"Modelon is revolutionizing the engineering design industry by offering technologies and services that enable customers to leverage system simulation. Modelon’s flagship product, Modelon Impact, is a cloud system simulation platform that helps engineers virtually design, analyze, and simulate physical systems. Our team brings deep industry expertise and is dedicated to guiding our customers in creating innovative technologies at their respective organizations. Headquartered in Lund, Sweden, Modelon is a global company with offices in Germany, India, Japan, and the United States. We believe that system simulation should be accessible to every engineer and are dedicated to being an open-standard platform company.","disambiguatingDescription":"Creators of the Modelon Impact system modeling and simulation platform. Headquartered in Lund, Sweden.","logo":{"@type":"ImageObject","url":"https://www.modelon.com/wp-content/uploads/2022/06/modelon-logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"Modelon AB Ideon Science Park Scheelevägen 17 SE-223 70","addressLocality":"Lund","addressCountry":"SE"},"telephone":"+46 46 286 22 00","email":"info@modelon.com","foundingDate":"2004","numberOfEmployees":{"@type":"QuantitativeValue","minValue":50,"maxValue":100},"areaServed":"Global","sameAs":["https://www.linkedin.com/company/modelon/","https://www.crunchbase.com/organization/modelon","https://www.zoominfo.com/c/modelon-ab/346633437","https://modelica.org","https://modelica.org/events/asian2024/","https://www.ashrae.org/conferences/2026-winter-conference","https://github.com/modelon-community"],"memberOf":[{"@type":"Organization","name":"Modelica Association","sameAs":"https://www.wikidata.org/wiki/Q133876843"}],"knowsAbout":[{"@type":"Thing","name":"Modelica","sameAs":"https://www.wikidata.org/wiki/Q385325"},"Physics Simulation",{"@type":"Thing","name":"software engineering","sameAs":"https://www.wikidata.org/wiki/Q80993"},{"@type":"Thing","name":"simulation","sameAs":"https://www.wikidata.org/wiki/Q45045"},"HVAC Simulation","Data Center Liquid Cooling Simulation","Liquid Cooling","FMI (Functional Mock-up Interface)",{"@type":"Thing","name":"consulting","sameAs":"https://www.wikidata.org/wiki/Q63769412"},{"@type":"Thing","name":"aerospace","sameAs":"https://www.wikidata.org/wiki/Q2876213"},{"@type":"Thing","name":"automotive industry","sameAs":"https://www.wikidata.org/wiki/Q190117"},{"@type":"Thing","name":"heating, ventilation, and air conditioning","sameAs":"https://www.wikidata.org/wiki/Q1798773"},{"@type":"Thing","name":"data center","sameAs":"https://www.wikidata.org/wiki/Q671224"},{"@type":"Thing","name":"electricity generation","sameAs":"https://www.wikidata.org/wiki/Q383973"},{"@type":"Thing","name":"energy storage","sameAs":"https://www.wikidata.org/wiki/Q837718"},{"@type":"Thing","name":"industrial equipment","sameAs":"https://www.wikidata.org/wiki/Q3045515"}]},{"@type":"WebSite","@id":"https://www.modelon.com/#website","name":"Modelon","url":"https://www.modelon.com","publisher":{"@id":"https://www.modelon.com/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://www.modelon.com/?s={search_term_string}"},"query-input":"required name=search_term_string"},"hasPart":[{"@type":"DigitalDocument","name":"LLM Content Index","url":"https://www.modelon.com/llms.txt","encodingFormat":"text/markdown","description":"AI-readable index of site content, entity data, and subject matter expertise"},{"@type":"DigitalDocument","name":"LLM Full Content","url":"https://www.modelon.com/llms-full.txt","encodingFormat":"text/markdown","description":"Complete AI-readable site content"}]},{"@type":"ItemList","@id":"https://www.modelon.com/#site-navigation","name":"Site Navigation","itemListElement":[{"@type":"SiteNavigationElement","position":1,"name":"Products & Services","url":"#"},{"@type":"SiteNavigationElement","position":2,"name":"Industries & Solutions","url":"#"},{"@type":"SiteNavigationElement","position":3,"name":"Support","url":"#"},{"@type":"SiteNavigationElement","position":4,"name":"Resources","url":"#"},{"@type":"SiteNavigationElement","position":5,"name":"About","url":"#"}]},{"@type":"BreadcrumbList","@id":"https://www.modelon.com/blog/extracting-library-statistics-using-optimica-compiler-toolkit-api/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://www.modelon.com"},{"@type":"ListItem","position":2,"name":"Blog","item":"https://www.modelon.com/blog/category/blog/"},{"@type":"ListItem","position":3,"name":"Extracting Library Statistics using OPTIMICA Compiler Toolkit API"}]},{"@type":"WebPage","@id":"https://www.modelon.com/blog/extracting-library-statistics-using-optimica-compiler-toolkit-api/#webpage","url":"https://www.modelon.com/blog/extracting-library-statistics-using-optimica-compiler-toolkit-api/","name":"Extracting Library Statistics using OPTIMICA Compiler Toolkit API | Modelon","isPartOf":{"@id":"https://www.modelon.com/#website"},"breadcrumb":{"@id":"https://www.modelon.com/blog/extracting-library-statistics-using-optimica-compiler-toolkit-api/#breadcrumb"},"description":"Have you ever wondered how many components, functions and models are in your library? Modelon’s OPTIMICA Compiler Toolkit has the capability to enable users...","about":[{"@type":"Thing","name":"Modelon Creator Suite"},{"@type":"Thing","name":"OPTIMICA Compiler Toolkit"}],"headline":"Extracting Library Statistics using OPTIMICA Compiler Toolkit API","datePublished":"2017-12-12T15:48:00+00:00","dateModified":"2022-06-30T16:04:53+00:00","image":"https://www.modelon.com/wp-content/uploads/2022/06/Extracting-Library-Statistics-using-OPTIMICA-Compiler-Toolkit-API.jpg","author":{"@id":"https://www.modelon.com/blog/author/wpengine/#person"},"publisher":{"@id":"https://www.modelon.com/#organization"}},{"@type":"Person","@id":"https://www.modelon.com/blog/author/wpengine/#person","name":"wpengine","url":"https://www.modelon.com/blog/author/wpengine/"}]}
```
