Directories in Modelica

Here’s the translation of the Go code to Modelica, formatted in Markdown suitable for Hugo:

Our first example demonstrates how to work with directories in Modelica. Here’s the full source code:

model DirectoryOperations
  import Modelica.Utilities.Files;
  import Modelica.Utilities.System;

  function createEmptyFile
    input String fileName;
  algorithm
    Files.remove(fileName);
    Files.createFile(fileName);
  end createEmptyFile;

equation
  when initial() then
    // Create a new sub-directory in the current working directory
    Files.createDirectory("subdir");

    // Create empty files
    createEmptyFile("subdir/file1");

    // Create a hierarchy of directories
    Files.createDirectory("subdir/parent");
    Files.createDirectory("subdir/parent/child");

    createEmptyFile("subdir/parent/file2");
    createEmptyFile("subdir/parent/file3");
    createEmptyFile("subdir/parent/child/file4");

    // List directory contents
    Modelica.Utilities.Streams.print("Listing subdir/parent");
    for fileName in Files.listFiles("subdir/parent") loop
      Modelica.Utilities.Streams.print(" " + fileName + " " + String(Files.isDirectory("subdir/parent/" + fileName)));
    end for;

    // Change current working directory
    System.setWorkingDirectory("subdir/parent/child");

    Modelica.Utilities.Streams.print("Listing subdir/parent/child");
    for fileName in Files.listFiles(".") loop
      Modelica.Utilities.Streams.print(" " + fileName + " " + String(Files.isDirectory(fileName)));
    end for;

    // Change back to the original directory
    System.setWorkingDirectory("../../..");

    // Recursively visit directory
    Modelica.Utilities.Streams.print("Visiting subdir");
    visitDirectory("subdir");

    // Clean up
    Files.removeDirectory("subdir", true);
  end when;

  function visitDirectory
    input String dirPath;
  algorithm
    for fileName in Files.listFiles(dirPath) loop
      Modelica.Utilities.Streams.print(" " + dirPath + "/" + fileName + " " + String(Files.isDirectory(dirPath + "/" + fileName)));
      if Files.isDirectory(dirPath + "/" + fileName) then
        visitDirectory(dirPath + "/" + fileName);
      end if;
    end for;
  end visitDirectory;
end DirectoryOperations;

This Modelica code demonstrates various directory operations:

  1. Creating directories using Files.createDirectory.
  2. Creating empty files with a custom function.
  3. Listing directory contents using Files.listFiles.
  4. Changing the current working directory with System.setWorkingDirectory.
  5. Recursively visiting a directory structure with a custom function.

Note that Modelica doesn’t have built-in functions for all the operations we saw in the original example. For instance, there’s no direct equivalent to os.MkdirAll or filepath.WalkDir. We’ve implemented similar functionality using custom functions and recursive calls.

To run this model, you would typically use a Modelica simulation environment. The output would be similar to the following:

Listing subdir/parent
 child true
 file2 false
 file3 false
Listing subdir/parent/child
 file4 false
Visiting subdir
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This example demonstrates how to perform basic directory operations in Modelica, including creating directories and files, listing contents, and traversing directory structures.