package ui.model; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.stream.Collectors; import classes.AbstractCanvasObject; import classes.Edge; import classes.Node; import classes.GroupNode; import classes.IntermediateCalculationCable; import classes.ExitCable; import classes.ExitCable.ExitCableState; public class VisualRepresentationalState { private ArrayList supplierList = new ArrayList(); private ArrayList passivList= new ArrayList(); private ArrayList consumerList= new ArrayList(); private ArrayList nodeList= new ArrayList(); private ArrayList cableList= new ArrayList(); private ArrayList switchList= new ArrayList(); private ArrayList groupNodeList= new ArrayList(); private ArrayList exitCableList= new ArrayList(); //ForFastAccessIndividualGroupNodes: private HashMap createdGroupNodes; public VisualRepresentationalState(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) { reassignObjects(stateFromThisTimestep, minimumModel); } //Getter: public ArrayList getSupplierList() { return supplierList; } public ArrayList getPassivList() { return passivList; } public ArrayList getConsumerList() { return consumerList; } public ArrayList getNodeList() { return nodeList; } public ArrayList getCableList() { return cableList; } public ArrayList getSwitchList() { return switchList; } public ArrayList getGroupNodeList() { return groupNodeList; } //Reassignments: private void reassignObjects(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) { HashMap inGroupObjects = minimumModel.getInGroupObjects(); //generate CableLookUp HashMap> inGroupEdges = minimumModel.getInGroupEdges(); createdGroupNodes = new HashMap(); ArrayList exitCables = new ArrayList(); //createThem for(GroupNode groupNode : minimumModel.getUppderNodeList()) { createdGroupNodes.put(groupNode, new DecoratedGroupNode(groupNode)); } //unrolling Networks for(DecoratedNetwork net : stateFromThisTimestep.getNetworkList()) { for(Consumer con : net.getConsumerList()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, con.getModel(), consumerList, con, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(con, groupNodeFromObject.getConsumerList()); } } for(Consumer con : net.getConsumerSelfSuppliedList()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, con.getModel(), consumerList, con, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(con, groupNodeFromObject.getConsumerList()); } } for(Supplier sup : net.getSupplierList()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, sup.getModel(), supplierList, sup, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(sup, groupNodeFromObject.getSupplierList()); } } for(Passiv pas : net.getPassivNoEnergyList()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, pas.getModel(), passivList, pas, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(pas, groupNodeFromObject.getPassivList()); } } for(DecoratedCable cable : net.getDecoratedCableList()) { addCable(cable, inGroupEdges, inGroupObjects,createdGroupNodes, exitCables); } } for(DecoratedCable cable : stateFromThisTimestep.getLeftOverEdges()) { addCable(cable, inGroupEdges, inGroupObjects, createdGroupNodes, exitCables); } for(Node node : minimumModel.getNodeList()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, node, nodeList ,node, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(node, groupNodeFromObject.getNodeList()); } } for(DecoratedSwitch dSwitch: stateFromThisTimestep.getDecoratedSwitches()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, dSwitch.getModel(), switchList, dSwitch, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(dSwitch, groupNodeFromObject.getSwitchList()); } } for(DecoratedGroupNode dGroupNode: createdGroupNodes.values()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupObjects, dGroupNode.getModel(), groupNodeList, dGroupNode, createdGroupNodes); if(groupNodeFromObject != null) { addToGroupNode(dGroupNode, groupNodeFromObject.getGroupNodeList()); } } //Create TreeNodeModel: HashMap> fastaccess= new HashMap>(); TreeNode root = new TreeNode(null, new ArrayList>(), new TreeGroupNodeData(null, 0)); fastaccess.put(null, root); for(DecoratedGroupNode dGroupNode: getGroupNodeList()) { addTreeNode(root, dGroupNode, 1, fastaccess); } for(IntermediateCalculationCable cable : exitCables) { createExitEdgesV2(root,cable.getCable() , cable.getInsideObject(),cable.getInsideUpperNode(),cable.getOusideObject(),cable.getOutsideUpperNode(), fastaccess); } } private void createExitEdgesV2(TreeNode root, DecoratedCable cable, AbstractCanvasObject insideObject, GroupNode insideUpperNode, AbstractCanvasObject ousideObject, GroupNode outsideUpperNode, HashMap> fastaccess) { //Create Up List LinkedList> listFromStart = createList(insideUpperNode, fastaccess); LinkedList> listFromEnd = createList(outsideUpperNode, fastaccess); LinkedList> common = new LinkedList>(listFromStart); common.retainAll(listFromEnd); TreeNode firstCommon = common.getFirst(); LinkedList> resultList = new LinkedList>(); //Add from listFromStart till firstCommon createresultList(listFromStart, firstCommon, resultList); //Add firstCommon resultList.add(firstCommon); //Add from listFromEnd till firstCommon createresultList(listFromEnd, firstCommon, resultList); LinkedList infoList = new LinkedList(); //Categorize: ListIterator> iter = resultList.listIterator(); while(iter.hasNext()) { //categorize TreeNode actual, next = null, previous = null; if(iter.hasPrevious()) { previous =iter.previous(); iter.next(); } actual = iter.next(); if(iter.hasNext()) { next =iter.next(); iter.previous(); } NodeInfo actualInfo = new NodeInfo(actual.getData().groupNode); if(previous!= null) { actualInfo.previousGroupNode = previous.getData().groupNode; if(previous == actual.getParent()) { actualInfo.previous = Info.Parent; }else { actualInfo.previous = Info.Child; } } if(next!= null) { actualInfo.nextGroupNode = next.getData().groupNode; if(next == actual.getParent()) { actualInfo.next = Info.Parent; }else { actualInfo.next = Info.Child; } } infoList.add(actualInfo); } for(NodeInfo info: infoList) { DecoratedGroupNode group = this.createdGroupNodes.get(info.groupNode); ArrayList mylist; if(group == null) { mylist = this.getExitCableList(); }else{ mylist = group.getExitCableList(); } ExitCableState state =null; if(info.previous == Info.Nothing) { if(info.next == Info.Child) { state = ExitCableState.DOWN; mylist.add(new ExitCable(state, insideObject, info.nextGroupNode, cable)); }else if(info.next == Info.Parent) { state = ExitCableState.UP; mylist.add(new ExitCable(state, insideObject, ousideObject, cable)); }else { System.out.println("Error in VisualState"); } }else if(info.previous == Info.Child) { if(info.next == Info.Child) { state = ExitCableState.DOWNDOWN; mylist.add(new ExitCable(state, info.previousGroupNode, info.nextGroupNode, cable)); }else if(info.next == Info.Parent) { state = ExitCableState.DOWNUP; mylist.add(new ExitCable(state, info.previousGroupNode, ousideObject, cable)); }else { state = ExitCableState.DOWN; mylist.add(new ExitCable(state, info.previousGroupNode, ousideObject, cable)); } }else {//(info.previous == Info.Parent) if(info.next == Info.Child) { state = ExitCableState.DOWNUP; mylist.add(new ExitCable(state, info.nextGroupNode, insideObject, cable)); }else if(info.next == Info.Parent) { System.out.println("Error in VisualState"); }else { state = ExitCableState.UP; mylist.add(new ExitCable(state, ousideObject, insideObject, cable)); } } } } private void createresultList(LinkedList> list, TreeNode firstCommon, LinkedList> resultList) { for(TreeNode node: list) { if(node == firstCommon) { break; } resultList.add(node); } } private LinkedList> createList(GroupNode insideUpperNode, HashMap> fastaccess) { TreeNode actualNode = fastaccess.get(insideUpperNode); LinkedList> list = new LinkedList>(); list.add(actualNode); while(actualNode.getParent() != null) { actualNode = actualNode.getParent(); list.add(actualNode); } return list; } private void addCable(HashMap inGroupObjects, DecoratedCable cable, DecoratedGroupNode groupNodeFromObject,ArrayList exitCables) { if(groupNodeFromObject != null) { boolean isIntern = inGroupObjects.get(cable.getModel().getA()) == inGroupObjects.get(cable.getModel().getB()); //Case null == null is not possible trough before Filtering MinimumModel#addUpperObjects(CpsUpperNode) if(isIntern) { addToGroupNode(cable, groupNodeFromObject.getInternCableList()); }else { if(inGroupObjects.get(cable.getModel().getA()) == groupNodeFromObject.getModel() && inGroupObjects.get(cable.getModel().getB()) != groupNodeFromObject.getModel()) { //addToGroupNode(new ExitCable(cable, groupNodeFromObject.getModel(),inGroupObjects.get(cable.getModel().getB()), cable.getModel().getA(), cable.getModel().getB()), groupNodeFromObject.getExitCableList()); exitCables.add(new IntermediateCalculationCable(cable, groupNodeFromObject.getModel(),inGroupObjects.get(cable.getModel().getB()), cable.getModel().getA(), cable.getModel().getB())); }else if(inGroupObjects.get(cable.getModel().getA()) != groupNodeFromObject.getModel() && inGroupObjects.get(cable.getModel().getB()) == groupNodeFromObject.getModel()) { //addToGroupNode(new ExitCable(cable, groupNodeFromObject.getModel(),inGroupObjects.get(cable.getModel().getA()), cable.getModel().getB(), cable.getModel().getA()), groupNodeFromObject.getExitCableList()); exitCables.add(new IntermediateCalculationCable(cable, groupNodeFromObject.getModel(),inGroupObjects.get(cable.getModel().getA()), cable.getModel().getB(), cable.getModel().getA())); } } } } private void addCable(DecoratedCable cable, HashMap> inGroupEdges, HashMap inGroupObjects, HashMap createdGroupNodes, ArrayList exitCables) { boolean isInGroup = false; if(inGroupObjects.containsKey(cable.getModel().getA())) { isInGroup = true; } if(inGroupObjects.containsKey(cable.getModel().getB())) { isInGroup = true; } if(isInGroup) { boolean isIntern = inGroupObjects.get(cable.getModel().getA()) == inGroupObjects.get(cable.getModel().getB()); //Case null == null is not possible trough before Filtering MinimumModel#addUpperObjects(CpsUpperNode) if(isIntern) { DecoratedGroupNode groupNodeFromBoth = createdGroupNodes.get(inGroupObjects.get(cable.getModel().getA())); groupNodeFromBoth.getInternCableList().add(cable); }else { if(inGroupObjects.containsKey(cable.getModel().getA())) { exitCables.add(new IntermediateCalculationCable(cable, inGroupObjects.get(cable.getModel().getA()),inGroupObjects.get(cable.getModel().getB()), cable.getModel().getA(), cable.getModel().getB())); } else if(inGroupObjects.containsKey(cable.getModel().getB())) { exitCables.add(new IntermediateCalculationCable(cable, inGroupObjects.get(cable.getModel().getB()),inGroupObjects.get(cable.getModel().getA()), cable.getModel().getB(), cable.getModel().getA())); } } }else { cableList.add(cable); } } private void addToGroupNode(DecoratedObject object, ArrayList groupNodeListPar) { groupNodeListPar.add(object); } public int getAmountfOfGroupNodes() { return groupNodeList.stream().map(groupNode -> groupNode.getAmountOfGroupNodes()).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)getAmountfOfGroupNodes(); } public float getAverageProduction() { return getProductionFromSupplier() / (float)getAmountfOfGroupNodes(); } //Generics private DecoratedGroupNode addObject(HashMap inGroupObjects, ModelOfObject modelOfObject, ArrayList listToAdd, DecoratedObject object, HashMap createdGroupNodes) { if(inGroupObjects.containsKey(modelOfObject)) { return createdGroupNodes.get(inGroupObjects.get(modelOfObject)); } listToAdd.add(object); return null; } public enum Info{ Nothing, Parent, Child } private class NodeInfo{ public GroupNode groupNode; public Info previous = Info.Nothing; public Info next = Info.Nothing; public GroupNode previousGroupNode = null; public GroupNode nextGroupNode = null; public NodeInfo(GroupNode groupNode) { this.groupNode = groupNode; } public String toString() { return "Previuos: " + previous.toString() + "|Next: " + next.toString(); } } public HashMap getCreatedGroupNodes() { return createdGroupNodes; } private class TreeNode { private TreeNode parentNode; private List> children; private T data; public TreeNode( TreeNode parentNode, List> children, T data) { this.parentNode = parentNode; this.children = children; this.data = data; } //Methods public TreeNode getParent(){ return parentNode; } public List> getChildren(){ return children; } public T getData() { return data; } public String toString() { return "[" + data.toString() + " Children(" + children.stream().map(Object::toString).collect(Collectors.joining(", ")) + ")]"; } } private class TreeGroupNodeData{ public GroupNode groupNode; public int layer; public TreeGroupNodeData(GroupNode groupNode, int layer) { this.groupNode = groupNode; this.layer = layer; } public String toString() { return "Layer:" + layer; } } private void addTreeNode(TreeNode node, DecoratedGroupNode dGroupNode, int layer, HashMap> fastaccess) { TreeNode newNode = new TreeNode (node, new ArrayList>() , new TreeGroupNodeData(dGroupNode.getModel(), layer)); node.getChildren().add(newNode); fastaccess.put(newNode.data.groupNode, newNode); for(DecoratedGroupNode dGroupNodeIntern: dGroupNode.getGroupNodeList()) { addTreeNode(newNode, dGroupNodeIntern, layer+1, fastaccess); } } public ArrayList getExitCableList() { return exitCableList; } }