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] Use codegen for Datalog entry types #6765

Closed
Closed
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
1 change: 1 addition & 0 deletions wpiutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ if(WITH_JAVA_SOURCE)
file(GLOB WPIUTIL_CLEANUP_SOURCES src/main/java/edu/wpi/first/util/cleanup/*.java)
file(GLOB WPIUTIL_CONCURRENT_SOURCES src/main/java/edu/wpi/first/util/concurrent/*.java)
file(GLOB WPIUTIL_DATALOG_SOURCES src/main/java/edu/wpi/first/util/datalog/*.java)
file(GLOB WPIUTIL_DATALOG_SOURCES src/generated/main/java/edu/wpi/first/util/datalog/*.java)
file(GLOB WPIUTIL_FUNCTION_SOURCES src/main/java/edu/wpi/first/util/function/*.java)
file(GLOB WPIUTIL_SENDABLE_SOURCES src/main/java/edu/wpi/first/util/sendable/*.java)
add_jar(
Expand Down
2 changes: 2 additions & 0 deletions wpiutil/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ model {
}
}

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

sourceSets {
printlog
}
Expand Down
68 changes: 68 additions & 0 deletions wpiutil/generate_log_entries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/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 Dict, Any

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


def render_template(
template: Template, output_dir: Path, filename: str, ty: Dict[str, Any]
):
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)

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

root_path = Path(output_root) / "main/java/edu/wpi/first/util/datalog"
template = env.get_template(
"main/java/edu/wpi/first/util/datalog/LogEntry.java.jinja"
)

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


def main(argv):
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/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/generate",
type=Path,
)
args = parser.parse_args(argv)

generate_log_entries(args.output_directory, args.template_root)


if __name__ == "__main__":
main(sys.argv[1:])
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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_log_entries.py DO NOT EDIT IT MANUALLY

package edu.wpi.first.util.datalog;

/** Log {{ DataType }} values. */
public class {{ name }}LogEntry extends DataLogEntry {
/** The data type for double 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
* @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);
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
* @param timestamp Time stamp (0 to indicate now)
*/
public synchronized void update({{ AppendType }} value, long timestamp) {
if (!m_hasLastValue || m_lastValue != value) {
m_lastValue = value;
m_hasLastValue = true;
append(value, timestamp);
}
}

/**
* Updates the last value and appends a record to the log if it has changed.
*
* @param value Value to record
*/
public void update({{ AppendType }} value) {
update(value, 0);
}

/**
* Gets the last value.
*
* @return Last value, or 0 if none.
*/
public synchronized {{ AppendType }} getLastValue() {
return m_lastValue;
}

boolean m_hasLastValue;
{{ AppendType }} m_lastValue;
}
52 changes: 52 additions & 0 deletions wpiutil/src/generate/types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"name": "Double",
"DataType": "double",
"AppendType": "double"
},
{
"name": "DoubleArray",
"DataType": "double[]",
"AppendType": "double[]"
},
{
"name": "Float",
"DataType": "float",
"AppendType": "float"
},
{
"name": "FloatArray",
"DataType": "float[]",
"AppendType": "float[]"
},
{
"name": "Integer",
"DataType": "int64",
"AppendType": "long"
},
{
"name": "IntegerArray",
"DataType": "int64[]",
"AppendType": "long[]"
},
{
"name": "Boolean",
"DataType": "boolean",
"AppendType": "boolean"
},
{
"name": "BooleanArray",
"DataType": "boolean[]",
"AppendType": "boolean[]"
},
{
"name": "String",
"DataType": "String",
"AppendType": "String"
},
{
"name": "StringArray",
"DataType": "string[]",
"AppendType": "String[]"
}
]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading