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.AbstractCpsObject; import classes.CpsEdge; import classes.CpsNode; import classes.CpsUpperNode; 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(); HashMap inGroupEdges = minimumModel.getInGroupEdges(); createdGroupNodes = new HashMap(); ArrayList exitCables = new ArrayList(); //createThem for(CpsUpperNode 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()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupEdges, cable.getModel(), cableList ,cable, createdGroupNodes); addCable(inGroupObjects, cable, groupNodeFromObject, exitCables); } } for(DecoratedCable cable : stateFromThisTimestep.getLeftOverEdges()) { DecoratedGroupNode groupNodeFromObject = addObject(inGroupEdges, cable.getModel(), cableList ,cable, createdGroupNodes); addCable(inGroupObjects, cable, groupNodeFromObject, exitCables); } for(CpsNode 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, AbstractCpsObject insideObject, CpsUpperNode insideUpperNode, AbstractCpsObject ousideObject, CpsUpperNode 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(CpsUpperNode 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 addToGroupNode(DecoratedObject object, ArrayList groupNodeListPar) { groupNodeListPar.add(object); } public int getAmountOfHolons() { return 1 + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfHolons()).reduce(0, Integer::sum); } //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 CpsUpperNode groupNode; public Info previous = Info.Nothing; public Info next = Info.Nothing; public CpsUpperNode previousGroupNode = null; public CpsUpperNode nextGroupNode = null; public NodeInfo(CpsUpperNode 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 CpsUpperNode groupNode; public int layer; public TreeGroupNodeData(CpsUpperNode 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; } }