123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- 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<Supplier> supplierList = new ArrayList<Supplier>();
- private ArrayList<Passiv> passivList= new ArrayList<Passiv>();
- private ArrayList<Consumer> consumerList= new ArrayList<Consumer>();
- private ArrayList<CpsNode> nodeList= new ArrayList<CpsNode>();
- private ArrayList<DecoratedCable> cableList= new ArrayList<DecoratedCable>();
- private ArrayList<DecoratedSwitch> switchList= new ArrayList<DecoratedSwitch>();
- private ArrayList<DecoratedGroupNode> groupNodeList= new ArrayList<DecoratedGroupNode>();
- private ArrayList<ExitCable> exitCableList= new ArrayList<ExitCable>();
-
- //ForFastAccessIndividualGroupNodes:
- private HashMap<CpsUpperNode, DecoratedGroupNode> createdGroupNodes;
-
- public VisualRepresentationalState(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) {
- reassignObjects(stateFromThisTimestep, minimumModel);
- }
- //Getter:
- public ArrayList<Supplier> getSupplierList() {
- return supplierList;
- }
- public ArrayList<Passiv> getPassivList() {
- return passivList;
- }
- public ArrayList<Consumer> getConsumerList() {
- return consumerList;
- }
- public ArrayList<CpsNode> getNodeList() {
- return nodeList;
- }
- public ArrayList<DecoratedCable> getCableList() {
- return cableList;
- }
- public ArrayList<DecoratedSwitch> getSwitchList() {
- return switchList;
- }
- public ArrayList<DecoratedGroupNode> getGroupNodeList() {
- return groupNodeList;
- }
-
- //Reassignments:
- private void reassignObjects(DecoratedState stateFromThisTimestep, MinimumModel minimumModel) {
- HashMap<AbstractCpsObject, CpsUpperNode> inGroupObjects = minimumModel.getInGroupObjects();
- HashMap<CpsEdge, CpsUpperNode> inGroupEdges = minimumModel.getInGroupEdges();
- createdGroupNodes = new HashMap<CpsUpperNode, DecoratedGroupNode>();
-
- ArrayList<IntermediateCalculationCable> exitCables = new ArrayList<IntermediateCalculationCable>();
- //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<CpsUpperNode, TreeNode<TreeGroupNodeData>> fastaccess= new HashMap<CpsUpperNode, TreeNode<TreeGroupNodeData>>();
- TreeNode<TreeGroupNodeData> root = new TreeNode<TreeGroupNodeData>(null, new ArrayList<TreeNode<TreeGroupNodeData>>(), 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<TreeGroupNodeData> root, DecoratedCable cable, AbstractCpsObject insideObject,
- CpsUpperNode insideUpperNode, AbstractCpsObject ousideObject, CpsUpperNode outsideUpperNode, HashMap<CpsUpperNode, TreeNode<TreeGroupNodeData>> fastaccess) {
- //Create Up List
- LinkedList<TreeNode<TreeGroupNodeData>> listFromStart = createList(insideUpperNode, fastaccess);
- LinkedList<TreeNode<TreeGroupNodeData>> listFromEnd = createList(outsideUpperNode, fastaccess);
- LinkedList<TreeNode<TreeGroupNodeData>> common = new LinkedList<TreeNode<TreeGroupNodeData>>(listFromStart);
- common.retainAll(listFromEnd);
- TreeNode<TreeGroupNodeData> firstCommon = common.getFirst();
- LinkedList<TreeNode<TreeGroupNodeData>> resultList = new LinkedList<TreeNode<TreeGroupNodeData>>();
- //Add from listFromStart till firstCommon
- createresultList(listFromStart, firstCommon, resultList);
- //Add firstCommon
- resultList.add(firstCommon);
- //Add from listFromEnd till firstCommon
- createresultList(listFromEnd, firstCommon, resultList);
- LinkedList<NodeInfo> infoList = new LinkedList<NodeInfo>();
- //Categorize:
- ListIterator<TreeNode<TreeGroupNodeData>> iter = resultList.listIterator();
-
- while(iter.hasNext()) {
- //categorize
- TreeNode<TreeGroupNodeData> 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<ExitCable> 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<TreeNode<TreeGroupNodeData>> list,
- TreeNode<TreeGroupNodeData> firstCommon, LinkedList<TreeNode<TreeGroupNodeData>> resultList) {
- for(TreeNode<TreeGroupNodeData> node: list) {
- if(node == firstCommon) {
- break;
- }
- resultList.add(node);
- }
- }
- private LinkedList<TreeNode<TreeGroupNodeData>> createList(CpsUpperNode insideUpperNode,
- HashMap<CpsUpperNode, TreeNode<TreeGroupNodeData>> fastaccess) {
- TreeNode<TreeGroupNodeData> actualNode = fastaccess.get(insideUpperNode);
- LinkedList<TreeNode<TreeGroupNodeData>> list = new LinkedList<TreeNode<TreeGroupNodeData>>();
- list.add(actualNode);
- while(actualNode.getParent() != null) {
- actualNode = actualNode.getParent();
- list.add(actualNode);
- }
- return list;
- }
- private void addCable(HashMap<AbstractCpsObject, CpsUpperNode> inGroupObjects, DecoratedCable cable,
- DecoratedGroupNode groupNodeFromObject,ArrayList<IntermediateCalculationCable> 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 <DecoratedObject> void addToGroupNode(DecoratedObject object, ArrayList<DecoratedObject> groupNodeListPar) {
- groupNodeListPar.add(object);
- }
-
-
-
-
- public int getAmountOfHolons() {
- return 1 + groupNodeList.stream().map(groupNode -> groupNode.getAmountOfHolons()).reduce(0, Integer::sum);
- }
-
-
- //Generics
- private <ModelOfObject, DecoratedObject> DecoratedGroupNode addObject(HashMap<ModelOfObject, CpsUpperNode> inGroupObjects, ModelOfObject modelOfObject, ArrayList<DecoratedObject> listToAdd, DecoratedObject object, HashMap<CpsUpperNode, DecoratedGroupNode> 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<CpsUpperNode, DecoratedGroupNode> getCreatedGroupNodes() {
- return createdGroupNodes;
- }
- private class TreeNode<T> {
- private TreeNode<T> parentNode;
- private List<TreeNode<T>> children;
- private T data;
-
- public TreeNode( TreeNode<T> parentNode, List<TreeNode<T>> children, T data) {
- this.parentNode = parentNode;
- this.children = children;
- this.data = data;
- }
-
- //Methods
- public TreeNode<T> getParent(){
- return parentNode;
- }
- public List<TreeNode<T>> 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<TreeGroupNodeData> node, DecoratedGroupNode dGroupNode, int layer, HashMap<CpsUpperNode, TreeNode<TreeGroupNodeData>> fastaccess) {
- TreeNode<TreeGroupNodeData> newNode = new TreeNode<TreeGroupNodeData> (node, new ArrayList<TreeNode<TreeGroupNodeData>>() , 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<ExitCable> getExitCableList() {
- return exitCableList;
- }
- }
|