Updated vertex load function to split in to more functions so to hopefully make it easier to follow
@@ -74,34 +74,61 @@ public class ProtobufReader implements NetworkReader {
Map<KeanuSavedBayesNet.VertexID, Vertex> instantiatedVertices,
BayesianNetwork bayesNet) {
for (KeanuSavedBayesNet.StoredValue value : parsedNet.getDefaultStateList()) {
- Vertex targetVertex = null;
+ Vertex targetVertex = getTargetVertex(value, instantiatedVertices, bayesNet);
- if (value.hasId()) {
- targetVertex = instantiatedVertices.get(value.getId());
+ savedValues.put(targetVertex, value.getValue());
+ targetVertex.loadValue(this);
+ }
}
- if (!value.getVertexLabel().isEmpty()) {
- Vertex newTarget = bayesNet.getVertexByLabel(new VertexLabel(value.getVertexLabel()));
+ private Vertex getTargetVertex(KeanuSavedBayesNet.StoredValue storedValue,
+ Map<KeanuSavedBayesNet.VertexID, Vertex> instantiatedVertices,
+ BayesianNetwork bayesNet) {
+ Vertex idTarget = getTargetByID(storedValue, instantiatedVertices);
+ Vertex labelTarget = getTargetByLabel(storedValue, instantiatedVertices, bayesNet);
- if (targetVertex != null && newTarget != targetVertex) {
- throw new IllegalArgumentException(
- "Label and VertexID don't refer to same Vertex: ("
- + value.getVertexLabel() + ") ("
- + value.getId() + ")");
+ return checkTargetsAreValid(idTarget, labelTarget, storedValue);
+ }
+
+ private Vertex getTargetByID(KeanuSavedBayesNet.StoredValue storedValue,
+ Map<KeanuSavedBayesNet.VertexID, Vertex> instantiatedVertices) {
+ if (storedValue.hasId()) {
+ return instantiatedVertices.get(storedValue.getId());
} else {
- targetVertex = newTarget;
+ return null;
}
}
- if (targetVertex == null) {
- throw new IllegalArgumentException("Value specified for unknown Vertex: ("
- + value.getVertexLabel() + ") ("
- + value.getId() + ")");
+ private Vertex getTargetByLabel(KeanuSavedBayesNet.StoredValue storedValue,
+ Map<KeanuSavedBayesNet.VertexID, Vertex> instantiatedVertices,
+ BayesianNetwork bayesNet) {
+ if (!storedValue.getVertexLabel().isEmpty()) {
+ return bayesNet.getVertexByLabel(new VertexLabel(storedValue.getVertexLabel()));
+ } else {
+ return null;
+ }
}
- savedValues.put(targetVertex, value.getValue());
- targetVertex.loadValue(this);
+ private Vertex checkTargetsAreValid(Vertex idTarget, Vertex labelTarget, KeanuSavedBayesNet.StoredValue storedValue) {
+ Vertex targetVertex;
+
+ if (idTarget != null && labelTarget != null) {
+ if (idTarget != labelTarget) {
+ throw new IllegalArgumentException("Label and VertexID don't refer to same Vertex: ("
+ + storedValue.getVertexLabel() + ") ("
+ + storedValue.getId() + ")");
+ } else {
+ targetVertex = idTarget;
}
+ } else if (idTarget == null && labelTarget == null) {
+ throw new IllegalArgumentException("Value specified for unknown Vertex: ("
+ + storedValue.getVertexLabel() + ") ("
+ + storedValue.getId() + ")");
+ } else {
+ targetVertex = (idTarget != null) ? idTarget : labelTarget;
+ }
+
+ return targetVertex;
}
@Override
