package it.univr.di.cstnu.graph; import it.univr.di.cstnu.graph.Edge.ConstraintType; import it.univr.di.labeledvalue.ALabel; import it.univr.di.labeledvalue.Label; import it.univr.di.labeledvalue.LabeledALabelIntTreeMap; import it.univr.di.labeledvalue.Literal; import org.junit.Test; import java.io.*; import java.nio.charset.StandardCharsets; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; /** * test for CSTNUGraphMLWriter * * @author posenato */ @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeStatic"}) public class CSTNUGraphMLWriterTest { private final String fileName = "src/test/resources/testGraphML.cstnu"; private final String fileOk = """ Number of contingents in the graph 0 Number of observed propositions in the graph 0 Network Type CSTNU Number of edges in the graph 0 Number of vertices in the graph 0 Graph Name Proposition Observed. Value specification: [a-zA-F] The x coordinate for the visualization. A positive value. 0 Label. Format: [¬[a-zA-F]|[a-zA-F]]+|⊡ The y coordinate for the visualization. A positive value. 0 Labeled Potential Values. Format: {[('node name (no case modification)', 'integer', 'label') ]+}|{} Type: Possible values: contingent|requirement|derived|internal. requirement Labeled Lower-Case Values. Format: {[('node name (no case modification)', 'integer', 'label') ]+}|{} Labeled Upper-Case Values. Format: {[('node name (no case modification)', 'integer', 'label') ]+}|{} Labeled Values. Format: {[('integer', 'label') ]+}|{} 1 1 CSTNU 2 4 testGraphML.cstnu 100.0 60.0 p 100.0 160.0 400.0 60.0 400.0 ¬p 160.0 contingent {(Y, -5, ¬p) } {} contingent {(Y, 2, ¬p) } {} """; /** * testGraphMLWriterAbstractLayoutOfLabeledNodeLabeledIntEdge * * @throws IOException clear */ @Test public void testGraphMLWriterAbstractLayoutOfLabeledNodeLabeledIntEdge() throws IOException { final Label p = Label.valueOf('p', Literal.NEGATED); final TNGraph g = new TNGraph<>("testGraphML.cstnu", CSTNUEdgePluggable.class); final LabeledNode Z = LabeledNodeSupplier.get("Z"); Z.setX(100); Z.setY(60); final LabeledNode Ω = LabeledNodeSupplier.get("Ω"); Ω.setX(400); Ω.setY(60); final LabeledNode X = LabeledNodeSupplier.get("X"); X.setX(100); X.setY(160); X.setObservable('p'); final LabeledNode Y = LabeledNodeSupplier.get("Y"); Y.setLabel(p); Y.setX(400); Y.setY(160); final CSTNUEdge xy = new CSTNUEdgePluggable("XY"); xy.setLowerCaseValue(p, new ALabel("Y", g.getALabelAlphabet()), 2); xy.setConstraintType(ConstraintType.contingent); final CSTNUEdge yx = new CSTNUEdgePluggable("YX"); final LabeledALabelIntTreeMap uc = new LabeledALabelIntTreeMap(null); uc.mergeTriple(p, new ALabel("Y", g.getALabelAlphabet()), -5); yx.setUpperCaseValueMap(uc); yx.setConstraintType(ConstraintType.contingent); g.addVertex(Z); g.addVertex(Ω); g.addVertex(X); g.addVertex(Y); g.addEdge(xy, X, Y); g.addEdge(yx, Y, X); // A test should not depend on other class! :-) // CSTNU cstnu = new CSTNU(g); // cstnu.initAndCheck(); final TNGraphMLWriter graphWriter = new TNGraphMLWriter(null); graphWriter.save(g, new File(fileName)); try (final BufferedReader input = new BufferedReader( new InputStreamReader(new FileInputStream(fileName), StandardCharsets.UTF_8))) { // don't use new FileReader(this.fileName) // because for Java 8 it does not accept "UTF-8" @SuppressWarnings("CheckForOutOfMemoryOnLargeArrayAllocation") final char[] fileAsChar = new char[4200]; if (input.read(fileAsChar) == -1) { fail("Problem reading " + fileName); } final String fileAsString = new String(fileAsChar); input.close(); assertEquals(fileOk, fileAsString.trim()); } } }