package ui.model; import java.util.ArrayList; import java.util.stream.Stream; import classes.Node; import jdk.jfr.Unsigned; import classes.GroupNode; import classes.HolonElement; import classes.Flexibility; import ui.model.DecoratedHolonObject.HolonObjectState; /** * For the @VisualRepresentationalState only. * * @author Tom * */ public class DecoratedGroupNode { private GroupNode model; private ArrayList supplierList; private ArrayList passivList; private ArrayList consumerList; private ArrayList nodeList; /** * Cables that only exist on that group node. From a object in that group node * to a object in that group Node. Not exit the group node (a layer down). */ private ArrayList internCableList; /** * Cables that exit this group node (a Layer Up). From a object in this group * node to a object in a upper layer. */ private ArrayList exitCableList; private ArrayList switchList; private ArrayList groupNodeList; public DecoratedGroupNode(GroupNode model) { this.model = model; this.supplierList = new ArrayList(); this.passivList = new ArrayList(); this.consumerList = new ArrayList(); this.nodeList = new ArrayList(); this.internCableList = new ArrayList(); this.exitCableList = new ArrayList(); this.switchList = new ArrayList(); this.groupNodeList = new ArrayList(); } public GroupNode getModel() { return model; } public ArrayList getSupplierList() { return supplierList; } public ArrayList getPassivList() { return passivList; } public ArrayList getConsumerList() { return consumerList; } public ArrayList getNodeList() { return nodeList; } public ArrayList getInternCableList() { return internCableList; } public ArrayList getExitCableList() { return exitCableList; } public ArrayList getSwitchList() { return switchList; } public ArrayList getGroupNodeList() { return groupNodeList; } // Returns the amount of holons and count himself public int getAmountOfGroupNodes() { return 1 + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfGroupNodes()).reduce(0, Integer::sum); } public Stream getFlexibilitiesStream() { Stream flexInChildGorupNode = this.groupNodeList.stream() .flatMap(groupNode -> groupNode.getFlexibilitiesStream()); Stream flexInThisGorupNode = objectStream() .flatMap(object -> object.getModel().getElements().stream().flatMap(ele -> ele.flexList.stream())); return Stream.concat(flexInChildGorupNode, flexInThisGorupNode); } public Stream objectStream() { return Stream.concat(Stream.concat(this.consumerList.stream(), this.supplierList.stream()), this.passivList.stream()); } // Gather Informations: public int getAmountOfSupplier() { return supplierList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfSupplier()).reduce(0, Integer::sum); } public int getAmountOfConsumer() { return consumerList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumer()).reduce(0, Integer::sum); } public int getAmountOfPassiv() { return passivList.size() + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfPassiv()).reduce(0, Integer::sum); } public int getAmountOfConsumerWithState(HolonObjectState state) { return ((int) consumerList.stream().map(con -> con.getState()).filter(rightState -> (rightState == state)) .count()) + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfConsumerWithState(state)).reduce(0, Integer::sum); } public int getAmountOfElemntsFromHolonObjects() { return objectStream().map(object -> object.getModel().getElements().size()).reduce(0, Integer::sum) + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfElemntsFromHolonObjects()).reduce(0, Integer::sum); } public PriorityCounts getPriorityCounts() { PriorityCounts priority = new PriorityCounts(); objectStream().forEach(object -> object.getModel().getElements().stream().forEach(ele -> priority.Count(ele))); groupNodeList.stream().forEach(groupNode -> priority.Add(groupNode.getPriorityCounts())); return priority; } public class PriorityCounts { @Unsigned public int low, medium, high, essential; public void Add(PriorityCounts other) { low += other.low; medium += other.medium; high += other.high; essential += other.essential; } public void Count(HolonElement element) { switch (element.getPriority()) { case Essential: essential++; break; case High: high++; break; case Medium: medium++; break; case Low: low++; break; default: break; } } } public int getAmountOfAktiveElemntsFromHolonObjects() { return objectStream().map(object -> object.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum) + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfAktiveElemntsFromHolonObjects()) .reduce(0, Integer::sum); } public float getConsumptionFromConsumer() { return consumerList.stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.f, Float::sum) + groupNodeList.stream().map(groupNode -> groupNode.getConsumptionFromConsumer()).reduce(0.f, Float::sum); } public float getProductionFromSupplier() { return supplierList.stream().map(sup -> sup.getEnergyToSupplyNetwork()).reduce(0.f, Float::sum) + groupNodeList .stream().map(groupNode -> groupNode.getProductionFromSupplier()).reduce(0.f, Float::sum); } public float getAverageConsumption() { return getConsumptionFromConsumer() / (float) getAmountOfGroupNodes(); } public float getAverageProduction() { return getProductionFromSupplier() / (float) getAmountOfGroupNodes(); } public String toString() { return "GroupNode" + model.getId() + " with [Supplier:" + getAmountOfSupplier() + ", NotSupplied:" + getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED) + ", PartiallySupplied:" + getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED) + ", Supplied:" + getAmountOfConsumerWithState(HolonObjectState.SUPPLIED) + ", OverSupplied:" + getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED) + ", Passiv:" + getAmountOfPassiv() + "]"; } }