added a testcase for the builder

This commit is contained in:
hneemann 2016-05-09 21:14:34 +02:00
parent 6ac73c718d
commit 0dea4acf63
6 changed files with 116 additions and 22 deletions

View File

@ -1,6 +1,8 @@
package de.neemann.digital.draw.builder;
/**
* With and height of a fragment
*
* @author hneemann
*/
public class Box {
@ -8,15 +10,27 @@ public class Box {
private final int width;
private final int height;
/**
* Create a new instance
*
* @param width width
* @param height height
*/
public Box(int width, int height) {
this.width = width;
this.height = height;
}
/**
* @return width
*/
public int getWidth() {
return width;
}
/**
* @return height
*/
public int getHeight() {
return height;
}

View File

@ -11,11 +11,8 @@ import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.draw.elements.Wire;
import de.neemann.digital.draw.graphics.Vector;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.gui.Main;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
@ -143,23 +140,4 @@ public class Builder {
return circuit;
}
public static void main(String[] args) {
Variable a = new Variable("A");
Variable b = new Variable("B");
Variable c = new Variable("C");
Expression y = Operation.or(Not.not(Operation.and(a, Not.not(b), c)), Operation.and(Not.not(a), c), Operation.and(b, Not.not(c)));
Expression y1 = Operation.or(Not.not(Operation.and(a, Not.not(b), c)), Operation.and(Not.not(a), c), Operation.and(b, Not.not(c)), Operation.and(b, Not.not(c)));
Expression l = Operation.and(y, y1, a);
Builder builder = new Builder(new ShapeFactory(new ElementLibrary()));
Circuit circuit = builder
.addExpression("L", l)
.addExpression("Y", y)
.createCircuit();
SwingUtilities.invokeLater(() -> new Main(null, circuit).setVisible(true));
}
}

View File

@ -6,18 +6,43 @@ import de.neemann.digital.draw.graphics.Vector;
import java.util.List;
/**
* A fragemnt used to create a circuit from an expression
*
* @author hneemann
*/
public interface Fragment {
/**
* Layouts this fragment
*
* @return the width and height of this fragment
*/
Box doLayout();
/**
* Sets the position of the fragment.
* Called during layout
*
* @param pos the position
*/
void setPos(Vector pos);
/**
* Fragment is asked to add itself to the given circuit
*
* @param pos the absolute position of the fragment
* @param circuit the circuit
*/
void addToCircuit(Vector pos, Circuit circuit);
/**
* @return the input positions of this fragment
*/
List<Vector> getInputs();
/**
* @return the output positions of this fragment
*/
List<Vector> getOutputs();
}

View File

@ -25,10 +25,22 @@ public class FragmentExpression implements Fragment {
return f;
}
/**
* Creates a new instance
*
* @param fragment a single frgment as an input
* @param merger the merger
*/
public FragmentExpression(Fragment fragment, Fragment merger) {
this(createList(fragment), merger);
}
/**
* The list of fragments is merged by a merger to a single output
*
* @param frags the fragments to merge
* @param merger the merger
*/
public FragmentExpression(ArrayList<Fragment> frags, Fragment merger) {
this.merger = merger;
fragments = new ArrayList<>();

View File

@ -16,6 +16,8 @@ import java.util.ArrayList;
import java.util.List;
/**
* A fragment describing a VisualElement
*
* @author hneemann
*/
public class FragmentVisualElement implements Fragment {
@ -25,10 +27,23 @@ public class FragmentVisualElement implements Fragment {
private final VisualElement visualElement;
private Vector pos;
/**
* Creates a new instance
*
* @param description the elements description
* @param shapeFactory the shapeFactory to use
*/
public FragmentVisualElement(ElementTypeDescription description, ShapeFactory shapeFactory) {
this(description, 1, shapeFactory);
}
/**
* Creates a new instance
*
* @param description the elements description
* @param inputCount number of inputs
* @param shapeFactory the shapeFactory to use
*/
public FragmentVisualElement(ElementTypeDescription description, int inputCount, ShapeFactory shapeFactory) {
visualElement = new VisualElement(description.getName()).setShapeFactory(shapeFactory);
visualElement.getElementAttributes().set(Keys.INPUT_COUNT, inputCount);
@ -44,6 +59,14 @@ public class FragmentVisualElement implements Fragment {
}
}
/**
* Sets an attribute to this VisualElement
*
* @param key the key
* @param value the value
* @param <VALUE> the tyype of the value
* @return this for call chaining
*/
public <VALUE> FragmentVisualElement setAttr(Key<VALUE> key, VALUE value) {
visualElement.getElementAttributes().set(key, value);
return this;

View File

@ -0,0 +1,42 @@
package de.neemann.digital.draw.builder;
import de.neemann.digital.TestExecuter;
import de.neemann.digital.analyse.expression.Expression;
import de.neemann.digital.analyse.expression.Variable;
import de.neemann.digital.draw.elements.Circuit;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.model.ModelDescription;
import de.neemann.digital.draw.shapes.ShapeFactory;
import junit.framework.TestCase;
import static de.neemann.digital.analyse.expression.Not.not;
import static de.neemann.digital.analyse.expression.Operation.and;
import static de.neemann.digital.analyse.expression.Operation.or;
/**
* @author hneemann
*/
public class BuilderTest extends TestCase {
public void testBuilder() throws Exception {
Variable a = new Variable("a");
Variable b = new Variable("b");
// xor
Expression y = and(or(a, b), not(and(a, b)));
ElementLibrary library = new ElementLibrary();
Circuit circuit = new Builder(new ShapeFactory(library))
.addExpression("y", y)
.createCircuit();
ModelDescription m = new ModelDescription(circuit, library);
TestExecuter te = new TestExecuter(m.createModel(false)).setUp(m);
te.check(0, 0, 0);
te.check(0, 1, 1);
te.check(1, 0, 1);
te.check(1, 1, 0);
}
}