Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Collectors;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -558,9 +559,21 @@ public static void write(final Model model, final OutputStream out) throws Excep
writer.writeEndElement();

// PVs (Formulas)
// All PVs must appear before formulas
writer.writeStartElement(TAG_PVLIST);
for (ModelItem item : model.getItems())
item.write(writer);

List<PVItem> pvItems =
model.getItems().stream().filter(i -> i instanceof PVItem).map(PVItem.class::cast).toList();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could do this as a forEach rather than a map then you don't need the lower for loop. Same for the formulaItems.

Copy link
Copy Markdown
Collaborator Author

@georgweiss georgweiss Jun 2, 2026

Choose a reason for hiding this comment

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

But then you would need a try/catch block inside the forEach, which impairs readability.

for(PVItem pvItem : pvItems){
pvItem.write(writer);
}

List<FormulaItem> formulaItems =
model.getItems().stream().filter(i -> i instanceof FormulaItem).map(FormulaItem.class::cast).toList();
for(FormulaItem formulaItem : formulaItems){
formulaItem.write(writer);
}

writer.writeEndElement();
}
writer.writeEndElement();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2025 European Spallation Source ERIC.
*/

package org.csstudio.trends.databrowser3.persistence;

import org.csstudio.trends.databrowser3.model.FormulaInput;
import org.csstudio.trends.databrowser3.model.FormulaItem;
import org.csstudio.trends.databrowser3.model.Model;
import org.csstudio.trends.databrowser3.model.PVItem;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class XMLPersistenceTest {

@Test
public void testPvAndFormulaOrdering() throws Exception {

Model model = new Model();

PVItem pvItem1 = new PVItem("pvitem1", 1.0);
FormulaInput formulaInput1 = new FormulaInput(pvItem1, "x1");
FormulaItem formulaItem1 = new FormulaItem("formula1", "x1 * 2", new FormulaInput[]{formulaInput1});

model.addItem(pvItem1);
model.addItem(formulaItem1);

PVItem pvItem2 = new PVItem("pvitem2", 1.0);
FormulaInput formulaInput2 = new FormulaInput(pvItem2, "x2");

formulaItem1 = (FormulaItem) model.getItem("formula1");

formulaItem1.updateFormula("x1 + x2", new FormulaInput[]{formulaInput1, formulaInput2});

// Add PV item referenced in formula after the update
model.addItem(pvItem2);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLPersistence.write(model, baos);

// Make sure the XML is readable
XMLPersistence.load(new Model(), new ByteArrayInputStream(baos.toByteArray()));

}
}
Loading