Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpiutil] DataLog: Use codegen for entry types #7479

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/pregen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def main():
[sys.executable, f"{REPO_ROOT}/thirdparty/imgui_suite/generate_gl3w.py"],
check=True,
)
subprocess.run(
[sys.executable, f"{REPO_ROOT}/wpiutil/generate_datalog_entries.py"],
check=True,
)
subprocess.run(f"{REPO_ROOT}/thirdparty/imgui_suite/generate_fonts.sh", check=True)


Expand Down
5 changes: 4 additions & 1 deletion wpiutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ cc_library(

java_library(
name = "wpiutil-java",
srcs = glob(["src/main/java/**/*.java"]),
srcs = glob([
"src/main/java/**/*.java",
"src/generated/java/**/*.java",
]),
visibility = ["//visibility:public"],
deps = [
"@maven//:com_fasterxml_jackson_core_jackson_annotations",
Expand Down
2 changes: 1 addition & 1 deletion wpiutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if(WITH_JAVA)

set(CMAKE_JNI_TARGET true)

file(GLOB_RECURSE JAVA_SOURCES src/main/java/*.java)
file(GLOB_RECURSE JAVA_SOURCES src/main/*.java)

add_jar(
wpiutil_jar
Expand Down
2 changes: 2 additions & 0 deletions wpiutil/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ model {
}
}

sourceSets.main.java.srcDir "${projectDir}/src/main/generated/java"

sourceSets {
printlog
}
Expand Down
66 changes: 66 additions & 0 deletions wpiutil/generate_datalog_entries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3

# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
import argparse
import json
import sys
from pathlib import Path
from typing import Any, Dict

from jinja2 import Environment, FileSystemLoader
from jinja2.environment import Template


def render_template(
template: Template, output_dir: Path, filename: str, ty: Dict[str, Any]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use type_ (or _type) instead of ty to be clearer about the meaning while still avoiding hiding the builtin type function?

):
output_dir.mkdir(parents=True, exist_ok=True)

output_file = output_dir / filename
output_file.write_text(template.render(ty), encoding="utf-8")


def generate_log_entries(output_root, template_root):
with (template_root / "types.json").open(encoding="utf-8") as f:
types = json.load(f)["types"]

env = Environment(
loader=FileSystemLoader(str(template_root)),
autoescape=False,
keep_trailing_newline=True,
)

java_root_path = Path(output_root) / "java/edu/wpi/first/util/datalog"
java_template = env.get_template("LogEntry.java.jinja")

for ty in types:
entry_name = f"{ty['name']}LogEntry.java"
render_template(java_template, java_root_path, entry_name, ty)


def main(argv):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per #7311, the argv argument should be removed and the no-args overload of parser.parse_args() should be used.

script_path = Path(__file__).resolve()
dirname = script_path.parent

parser = argparse.ArgumentParser()
parser.add_argument(
"--output_directory",
help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script",
default=dirname / "src/main/generated",
type=Path,
)
parser.add_argument(
"--template_root",
help="Optional. If set, will use this directory as the root for the jinja templates",
default=dirname / "src/main/generate",
type=Path,
)
args = parser.parse_args(argv)

generate_log_entries(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
217 changes: 217 additions & 0 deletions wpiutil/src/main/generate/LogEntry.java.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

// THIS FILE WAS GENERATED BY generate_datalog_entries.py DO NOT EDIT IT MANUALLY

package edu.wpi.first.util.datalog;

{% if IsArray %}
import java.util.Arrays;
{% endif %}

/** Log {{ DataType }} values. */
public class {{ name }}LogEntry extends DataLogEntry {
/** The data type for {{ DataType }} values. */
public static final String kDataType = "{{ DataType }}";

/**
* Constructs a {{ DataType }} log entry.
*
* @param log datalog
* @param name name of the entry
* @param metadata metadata
* @param timestamp entry creation timestamp (0=now)
*/
public {{ name }}LogEntry(DataLog log, String name, String metadata, long timestamp) {
super(log, name, kDataType, metadata, timestamp);
}

/**
* Constructs a {{ DataType }} log entry.
*
* @param log datalog
* @param name name of the entry
* @param metadata metadata
*/
public {{ name }}LogEntry(DataLog log, String name, String metadata) {
this(log, name, metadata, 0);
}

/**
* Constructs a {{ DataType }} log entry.
*
* @param log datalog
* @param name name of the entry
* @param timestamp entry creation timestamp (0=now)
*/
public {{ name }}LogEntry(DataLog log, String name, long timestamp) {
this(log, name, "", timestamp);
}

/**
* Constructs a {{ DataType }} log entry.
*
* @param log datalog
* @param name name of the entry
*/
public {{ name }}LogEntry(DataLog log, String name) {
this(log, name, 0);
}

/**
* Appends a record to the log.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public void append({{ AppendType }} value, long timestamp) {
m_log.append{{ name }}(m_entry, value, timestamp);
}

/**
* Appends a record to the log.
*
* @param value Value to record
*/
public void append({{ AppendType }} value) {
m_log.append{{ name }}(m_entry, value, 0);
}

{% if IsArray %}
/**
* Updates the last value and appends a record to the log if it has changed.
*
* <p>Note: the last value is local to this class instance; using update() with two instances
* pointing to the same underlying log entry name will likely result in unexpected results.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update({{ AppendType }} value, long timestamp) {
if (!equalsLast(value)) {
copyToLast(value);
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* <p>Note: the last value is local to this class instance; using update() with two instances
* pointing to the same underlying log entry name will likely result in unexpected results.
*
* @param value Value to record
*/
public void update({{ AppendType }} value) {
update(value, 0);
}

/**
* Gets whether there is a last value.
*
* <p>Note: the last value is local to this class instance and updated only with update(), not
* append().
*
* @return True if last value exists, false otherwise.
*/
public synchronized boolean hasLastValue() {
return m_lastValue != null;
}

/**
* Gets the last value.
*
* <p>Note: the last value is local to this class instance and updated only with update(), not
* append().
*
* @return Last value, or false if none.
*/
@SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
public synchronized {{ AppendType }} getLastValue() {
if (m_lastValue == null) {
return null;
}
return Arrays.copyOf(m_lastValue, m_lastValueLen);
}

private boolean equalsLast({{ AppendType }} value) {
if (m_lastValue == null || m_lastValueLen != value.length) {
return false;
}
return Arrays.equals(m_lastValue, 0, value.length, value, 0, value.length);
}

private void copyToLast({{ AppendType }} value) {
if (m_lastValue == null || m_lastValue.length < value.length) {
m_lastValue = Arrays.copyOf(value, value.length);
} else {
System.arraycopy(value, 0, m_lastValue, 0, value.length);
}
m_lastValueLen = value.length;
}

private int m_lastValueLen;
{% else %}
/**
* Updates the last value and appends a record to the log if it has changed.
*
* <p>Note: the last value is local to this class instance; using update() with two instances
* pointing to the same underlying log entry name will likely result in unexpected results.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update({{ AppendType }} value, long timestamp) {
{%- if AppendType == "String" %}
if (!m_hasLastValue || !m_lastValue.equals(value)) {
{%- else %}
if (!m_hasLastValue || m_lastValue != value) {
{%- endif %}
m_lastValue = value;
m_hasLastValue = true;
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* <p>Note: the last value is local to this class instance; using update() with two instances
* pointing to the same underlying log entry name will likely result in unexpected results.
*
* @param value Value to record
*/
public void update({{ AppendType }} value) {
update(value, 0);
}

/**
* Gets whether there is a last value.
*
* <p>Note: the last value is local to this class instance and updated only with update(), not
* append().
*
* @return True if last value exists, false otherwise.
*/
public synchronized boolean hasLastValue() {
return m_hasLastValue;
}

/**
* Gets the last value.
*
* <p>Note: the last value is local to this class instance and updated only with update(), not
* append().
*
* @return Last value, or false if none.
*/
public synchronized {{ AppendType }} getLastValue() {
return m_lastValue;
}

private boolean m_hasLastValue;
{% endif %}

private {{ AppendType }} m_lastValue;
}
64 changes: 64 additions & 0 deletions wpiutil/src/main/generate/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"types": [
{
"name": "Double",
"DataType": "double",
"AppendType": "double",
"IsArray": false
},
{
"name": "DoubleArray",
"DataType": "double[]",
"AppendType": "double[]",
"IsArray": true
},
{
"name": "Float",
"DataType": "float",
"AppendType": "float",
"IsArray": false
},
{
"name": "FloatArray",
"DataType": "float[]",
"AppendType": "float[]",
"IsArray": true
},
{
"name": "Integer",
"DataType": "int64",
"AppendType": "long",
"IsArray": false
},
{
"name": "IntegerArray",
"DataType": "int64[]",
"AppendType": "long[]",
"IsArray": true
},
{
"name": "Boolean",
"DataType": "boolean",
"AppendType": "boolean",
"IsArray": false
},
{
"name": "BooleanArray",
"DataType": "boolean[]",
"AppendType": "boolean[]",
"IsArray": true
},
{
"name": "String",
"DataType": "String",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"DataType": "String",
"DataType": "string",

"AppendType": "String",
"IsArray": false
},
{
"name": "StringArray",
"DataType": "string[]",
"AppendType": "String[]",
"IsArray": true
}
]
}
Loading
Loading