Model (Explanation of Model.java) - Wait-For-It-123/logiCAD GitHub Wiki
This page explains model in general and various important methods and variables that exist within Model.java file / class definition.
Model Class Purpose:
Model handles all back-end logic and tasks, such as creating and removing circuit elements from the workspace, propagating and evaluating signals, and making connections between circuit elements.
Important Variables:
private ArrayList<Object> workspaceElements
-- Holds the circuit element objects.
Important Note: this ArrayList holds objects of type Object
. Because every class in Java is a child of class Object, any type of object can be added to workspaceElements; however, for our use, only objects of type andGate, orGate, xorGate, xnorGae, notGate, norGate, nandGate (note: all of these previous types inherit from abstract class Gate), Input, or Output are stored within. This was not the best design decision, because when accessing any object already inside of workspaceElements, you first need to determine what type of object you are dealing with. To make this check, you should use the instanceof
operator.
For example, the following code accesses the 0th element in workspaceElements, then the if else block determines whether the object is of type Gate, Input or Output:
Object obj = workspaceElements.get(0);
if(obj instanceof Gate) {
// obj is of type Gate
}
else if(obj instanceof Input) {
// obj is of type Input
}
else if(obj instanceof Output) {
// obj is of type Output
}
Important Methods:
public String serializeModelData()
-- converts circuit element objects in the workspace and important global variables maintaining program state into a text based representation in the form of a String. Used in the save function.
public void removeCircuitElementHelper(String id)
-- removes circuit element whose id matches argument id
public String createandAddCircuitElement(int type)
-- Makes an object according to int value and returns its id. See code for int value to object correspondences.
public void removeCircuitElement(Object element)
-- removes element from workspaceElements
public int makeCircuitConnection(Object parent, Object child)
-- makes connection between a parent circuit element and child circuit element. Returns an int corresponding to type or connection made or error code if the connection was unsuccessful.
private void propagateFamilyTreeToAllChildren(Gate childGate)
-- the ancestors of childGate are added to the family trees of all of its children. For example, if childGate's parent is and5, now childGate's children will know they are related to and5 as well.
public boolean evaluateCircuitNetwork()
-- Propagates input value signals from Inputs through the entire network and updates the output values. Will return a true if successful and false if the circuit is not fully connected (if there are "dangling wires" somewhere).
public static boolean areListsEqual(ArrayList<Integer> list1, ArrayList<Integer> list2)
-- compares two lists to see if they are the same.
public String addObjectToWorkspace(Object obj)
-- adds obj to workspaceElements, assigns it an id, and returns its id.
public ArrayList<Connection> queryAndGetConnections()
-- returns an ArrayList that contains Connection objects corresponding to each connection in the circuit(s).
public int toggleInputFromID(String InputID)
-- flips an input's value from 0 to 1 or 1 to 0 for an input whose id matches InputID. Returns the new input value as int.
public int getOutputValueFromID(String id)
-- gets output value for output whose id matches argument id.
public int makeConnectionFromIDs(String parentID, String childID)
-- Similar to makeCircuitConnection method but uses parent's and child's ids instead of the objects themselves.
The following methods are used to print information to the console for debugging:
public void printAllInputsAndValues()
public String allInputsAndValuesToString()
public void printAllOutputsAndValues()
public String allOutputsAndValuesToString()
public void printAllInputAndOutputValuesForAllCircuitElements()
public void printAllConnectionsForAllCircuitElements()
public void printAllWorkspaceElements()
public void printAllFamilyTrees()
public void printElementMatching(String id)