├── .gitignore
├── ConstraintBasedLayoutManager
├── .DS_Store
├── .classpath
├── .project
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── libs
│ ├── android-support-v4.jar
│ └── java-cup-11a-runtime.jar
├── proguard-project.txt
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ ├── ic_action_tick.png
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ ├── ic_action_tick.png
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ ├── ic_action_tick.png
│ │ └── ic_launcher.png
│ ├── layout
│ │ ├── activity_app_home.xml
│ │ ├── activity_dynamic_layout_editor.xml
│ │ ├── activity_main.xml
│ │ ├── activity_padding.xml
│ │ └── spinner_item.xml
│ ├── values-large
│ │ └── styles.xml
│ ├── values-sw600dp
│ │ └── dimens.xml
│ ├── values-sw720dp-land
│ │ └── dimens.xml
│ ├── values-v11
│ │ └── styles.xml
│ ├── values-v14
│ │ └── styles.xml
│ └── values
│ │ ├── attrs.xml
│ │ ├── dimens.xml
│ │ ├── ids.xml
│ │ ├── strings.xml
│ │ ├── strings_activity_dynamic_layout_editor.xml
│ │ └── styles.xml
└── src
│ └── edu
│ └── gatech
│ └── constraints
│ ├── cassowary
│ ├── CL.java
│ ├── ClAbstractVariable.java
│ ├── ClConstraint.java
│ ├── ClDouble.java
│ ├── ClDummyVariable.java
│ ├── ClEditConstraint.java
│ ├── ClEditInfo.java
│ ├── ClEditOrStayConstraint.java
│ ├── ClLinearConstraint.java
│ ├── ClLinearEquation.java
│ ├── ClLinearExpression.java
│ ├── ClLinearInequality.java
│ ├── ClObjectiveVariable.java
│ ├── ClParseTest.java
│ ├── ClPoint.java
│ ├── ClReader.cup
│ ├── ClReader.flex
│ ├── ClSimplexSolver.java
│ ├── ClSlackVariable.java
│ ├── ClStayConstraint.java
│ ├── ClStrength.java
│ ├── ClSymbolicWeight.java
│ ├── ClTableau.java
│ ├── ClTests.java
│ ├── ClVariable.java
│ ├── ExCLConstraintNotFound.java
│ ├── ExCLError.java
│ ├── ExCLInternalError.java
│ ├── ExCLNonlinearExpression.java
│ ├── ExCLNotEnoughStays.java
│ ├── ExCLRequiredFailure.java
│ ├── ExCLTooDifficult.java
│ ├── Makefile.am
│ ├── Makefile.in
│ ├── README
│ ├── Set.java
│ ├── Timer.java
│ ├── Yylex.java
│ ├── buildparser.xml
│ ├── parser.java
│ └── sym.java
│ ├── demo
│ ├── AppHome.java
│ ├── CircularLayout.java
│ ├── Constants.java
│ ├── DynamicDemo.java
│ ├── DynamicLayoutEditor.java
│ ├── DynamicLayoutOutput.java
│ ├── DynamicView.java
│ ├── Functions.java
│ ├── PaddingDemo.java
│ └── StaticDemo.java
│ └── library
│ ├── ClDimension.java
│ ├── InfixToPostfix.java
│ ├── LinearConstraintLayout.java
│ ├── MyStackList.java
│ └── ViewElement.java
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | **/*.apk
3 | **/*.ap_
4 |
5 | # files for the dex VM
6 | **/*.dex
7 |
8 | # Java class files
9 | **/*.class
10 |
11 | # generated files
12 | **/bin/
13 | **/gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | **/local.properties
17 |
18 | # Eclipse project files
19 | **/.classpath
20 | **/.project
21 |
22 | # Proguard folder generated by Eclipse
23 | **/proguard/
24 |
25 | # Intellij project files
26 | **/*.iml
27 | **/*.ipr
28 | **/*.iws
29 | **/.idea/
30 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/.DS_Store
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ConstraintBasedLayoutManager
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
23 |
24 |
28 |
29 |
32 |
33 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
45 |
46 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/libs/java-cup-11a-runtime.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/libs/java-cup-11a-runtime.jar
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=Google Inc.:Google APIs:19
15 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-hdpi/ic_action_tick.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-hdpi/ic_action_tick.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-mdpi/ic_action_tick.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-mdpi/ic_action_tick.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-xhdpi/ic_action_tick.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-xhdpi/ic_action_tick.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/anandsainath/constraint-layout/bd568ee8fd44de81c8c6c4ecda3fff926457a074/ConstraintBasedLayoutManager/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/layout/activity_app_home.xml:
--------------------------------------------------------------------------------
1 |
11 |
12 |
19 |
20 |
28 |
29 |
37 |
38 |
46 |
47 |
55 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/layout/activity_dynamic_layout_editor.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
16 |
17 |
22 |
23 |
31 |
32 |
33 |
34 |
35 |
39 |
40 |
43 |
44 |
48 |
49 |
53 |
54 |
58 |
59 |
63 |
64 |
73 |
74 |
84 |
85 |
95 |
96 |
105 |
106 |
115 |
116 |
125 |
126 |
131 |
132 |
136 |
137 |
148 |
149 |
159 |
160 |
161 |
162 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
19 |
20 |
31 |
32 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/layout/activity_padding.xml:
--------------------------------------------------------------------------------
1 |
13 |
14 |
23 |
24 |
34 |
35 |
45 |
46 |
56 |
57 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/layout/spinner_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
18 |
19 |
27 |
28 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values-large/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Constraint Based Layout Manager
5 | Settings
6 | Dummy Text 1
7 | Dummy Text 2 that is way longer than the above dummy text, but isn\'t constraint based layout great?
8 | DynamicActivity
9 | Fix Width
10 | Fix Height
11 | Fix X
12 | Fix Y
13 | Standard Constraints
14 | Secondary Constraints
15 | Constraint Expression
16 | Static Constraint Demo
17 | Dynamic Constraint Demo
18 | Dynamic Layout Editor
19 | Padding Demo
20 | Circular Demo
21 | Text padded 50dp from the top
22 | Text padded 50dp from the bottom
23 | Text padded 50dp from the left
24 | Text padded 50dp from the right
25 |
26 |
27 | - EditText
28 | - TextView
29 | - Button
30 | - Select a component type
31 |
32 |
33 | - Match Parent
34 | - Wrap Content
35 | - Select a width dimension
36 |
37 |
38 | - Match Parent
39 | - Wrap Content
40 | - Select a height dimension
41 |
42 |
43 | - Required
44 | - Strong
45 | - Medium
46 | - Weak
47 | - Select a constraint strength
48 |
49 |
50 | - et1
51 | - et2
52 | - et3
53 | - et4
54 | - et5
55 | - tv1
56 | - tv2
57 | - tv3
58 | - tv4
59 | - tv5
60 | - btn1
61 | - btn2
62 | - btn3
63 | - btn4
64 | - btn5
65 | - Select a component name
66 |
67 |
68 | image
69 | DynamicLayoutOutput
70 | Constraint Layout Manager
71 | CircularLayout
72 | Hello world!
73 |
74 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/strings_activity_dynamic_layout_editor.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Dynamic Constraint Based Layout - Editor
4 |
5 |
6 | Component Name
7 | Password
8 | View layout
9 | Add
10 | Sign in
11 | Signing in…
12 |
13 |
14 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
15 |
16 |
17 |
20 |
21 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/CL.java:
--------------------------------------------------------------------------------
1 | // $Id: CL.java,v 1.2 2008/09/05 05:02:11 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // CL.java
11 | // The enumerations from ClLinearInequality,
12 | // and `global' functions that we want easy to access
13 |
14 | package edu.gatech.constraints.cassowary;
15 |
16 | public class CL {
17 | protected final static boolean fDebugOn = false;
18 | protected final static boolean fTraceOn = false;
19 | protected final static boolean fGC = false;
20 |
21 | protected static void debugprint(String s) {
22 | System.err.println(s);
23 | }
24 |
25 | protected static void traceprint(String s) {
26 | System.err.println(s);
27 | }
28 |
29 | protected static void fnenterprint(String s) {
30 | System.err.println("* " + s);
31 | }
32 |
33 | protected static void fnexitprint(String s) {
34 | System.err.println("- " + s);
35 | }
36 |
37 | protected void assertion(boolean f, String description)
38 | throws ExCLInternalError {
39 | if (!f) {
40 | throw new ExCLInternalError("Assertion failed:" + description);
41 | }
42 | }
43 |
44 | protected void assertion(boolean f) throws ExCLInternalError {
45 | if (!f) {
46 | throw new ExCLInternalError("Assertion failed");
47 | }
48 | }
49 |
50 | // public static final byte GEQ = 1;
51 | // public static final byte LEQ = 2;
52 | public enum Op {
53 | GEQ, // >=
54 | LEQ // <=
55 | }
56 |
57 | public static ClLinearExpression Plus(ClLinearExpression e1,
58 | ClLinearExpression e2) {
59 | return e1.plus(e2);
60 | }
61 |
62 | public static ClLinearExpression Plus(ClLinearExpression e1, double e2) {
63 | return e1.plus(new ClLinearExpression(e2));
64 | }
65 |
66 | public static ClLinearExpression Plus(double e1, ClLinearExpression e2) {
67 | return (new ClLinearExpression(e1)).plus(e2);
68 | }
69 |
70 | public static ClLinearExpression Plus(ClVariable e1, ClLinearExpression e2) {
71 | return (new ClLinearExpression(e1)).plus(e2);
72 | }
73 |
74 | public static ClLinearExpression Plus(ClLinearExpression e1, ClVariable e2) {
75 | return e1.plus(new ClLinearExpression(e2));
76 | }
77 |
78 | public static ClLinearExpression Plus(ClVariable e1, double e2) {
79 | return (new ClLinearExpression(e1)).plus(new ClLinearExpression(e2));
80 | }
81 |
82 | public static ClLinearExpression Plus(double e1, ClVariable e2) {
83 | return (new ClLinearExpression(e1)).plus(new ClLinearExpression(e2));
84 | }
85 |
86 | public static ClLinearExpression Minus(ClLinearExpression e1,
87 | ClLinearExpression e2) {
88 | return e1.minus(e2);
89 | }
90 |
91 | public static ClLinearExpression Minus(double e1, ClLinearExpression e2) {
92 | return (new ClLinearExpression(e1)).minus(e2);
93 | }
94 |
95 | public static ClLinearExpression Minus(ClLinearExpression e1, double e2) {
96 | return e1.minus(new ClLinearExpression(e2));
97 | }
98 |
99 | public static ClLinearExpression Times(ClLinearExpression e1,
100 | ClLinearExpression e2) throws ExCLNonlinearExpression {
101 | return e1.times(e2);
102 | }
103 |
104 | public static ClLinearExpression Times(ClLinearExpression e1, ClVariable e2)
105 | throws ExCLNonlinearExpression {
106 | return e1.times(new ClLinearExpression(e2));
107 | }
108 |
109 | public static ClLinearExpression Times(ClVariable e1, ClLinearExpression e2)
110 | throws ExCLNonlinearExpression {
111 | return (new ClLinearExpression(e1)).times(e2);
112 | }
113 |
114 | public static ClLinearExpression Times(ClLinearExpression e1, double e2)
115 | throws ExCLNonlinearExpression {
116 | return e1.times(new ClLinearExpression(e2));
117 | }
118 |
119 | public static ClLinearExpression Times(double e1, ClLinearExpression e2)
120 | throws ExCLNonlinearExpression {
121 | return (new ClLinearExpression(e1)).times(e2);
122 | }
123 |
124 | public static ClLinearExpression Times(double n, ClVariable clv)
125 | throws ExCLNonlinearExpression {
126 | return (new ClLinearExpression(clv, n));
127 | }
128 |
129 | public static ClLinearExpression Times(ClVariable clv, double n)
130 | throws ExCLNonlinearExpression {
131 | return (new ClLinearExpression(clv, n));
132 | }
133 |
134 | public static ClLinearExpression Divide(ClLinearExpression e1,
135 | ClLinearExpression e2) throws ExCLNonlinearExpression {
136 | return e1.divide(e2);
137 | }
138 |
139 | public static boolean approx(double a, double b) {
140 | double epsilon = 1.0e-8;
141 | if (a == 0.0) {
142 | return (Math.abs(b) < epsilon);
143 | } else if (b == 0.0) {
144 | return (Math.abs(a) < epsilon);
145 | } else {
146 | return (Math.abs(a - b) < Math.abs(a) * epsilon);
147 | }
148 | }
149 |
150 | public static boolean approx(ClVariable clv, double b) {
151 | return approx(clv.value(), b);
152 | }
153 |
154 | static boolean approx(double a, ClVariable clv) {
155 | return approx(a, clv.value());
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClAbstractVariable.java:
--------------------------------------------------------------------------------
1 | // $Id: ClAbstractVariable.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClAbstractVariable
11 |
12 | package edu.gatech.constraints.cassowary;
13 |
14 | public abstract class ClAbstractVariable {
15 | public ClAbstractVariable(String name) {
16 | // hash_code = iVariableNumber;
17 | _name = name;
18 | iVariableNumber++;
19 | }
20 |
21 | public ClAbstractVariable() {
22 | // hash_code = iVariableNumber;
23 | _name = "v" + iVariableNumber;
24 | iVariableNumber++;
25 | }
26 |
27 | public ClAbstractVariable(long varnumber, String prefix) {
28 | // hash_code = iVariableNumber;
29 | _name = prefix + varnumber;
30 | iVariableNumber++;
31 | }
32 |
33 | public String name() {
34 | return _name;
35 | }
36 |
37 | public void setName(String name) {
38 | _name = name;
39 | }
40 |
41 | public boolean isDummy() {
42 | return false;
43 | }
44 |
45 | public abstract boolean isExternal();
46 |
47 | public abstract boolean isPivotable();
48 |
49 | public abstract boolean isRestricted();
50 |
51 | public abstract String toString();
52 |
53 | public static int numCreated() {
54 | return iVariableNumber;
55 | }
56 |
57 | // for debugging
58 | // public final int hashCode() { return hash_code; }
59 |
60 | private String _name;
61 |
62 | // for debugging
63 | // private int hash_code;
64 |
65 | private static int iVariableNumber;
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClConstraint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClConstraint.java,v 1.1 2008/08/12 22:32:42 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClConstraint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public abstract class ClConstraint {
16 |
17 | public ClConstraint(ClStrength strength, double weight) {
18 | _strength = strength;
19 | _weight = weight;
20 | }
21 |
22 | public ClConstraint(ClStrength strength) {
23 | _strength = strength;
24 | _weight = 1.0;
25 | }
26 |
27 | public ClConstraint() {
28 | _strength = ClStrength.required;
29 | _weight = 1.0;
30 | }
31 |
32 | public abstract ClLinearExpression expression();
33 |
34 | public boolean isEditConstraint() {
35 | return false;
36 | }
37 |
38 | public boolean isInequality() {
39 | return false;
40 | }
41 |
42 | public boolean isRequired() {
43 | return _strength.isRequired();
44 | }
45 |
46 | public boolean isStayConstraint() {
47 | return false;
48 | }
49 |
50 | public ClStrength strength() {
51 | return _strength;
52 | }
53 |
54 | public double weight() {
55 | return _weight;
56 | }
57 |
58 | public String toString() {
59 | return _strength.toString() + " {" + weight() + "} (" + expression();
60 | }
61 |
62 | public void setAttachedObject(Object o) {
63 | _attachedObject = o;
64 | }
65 |
66 | public Object getAttachedObject() {
67 | return _attachedObject;
68 | }
69 |
70 | /*
71 | * private void setStrength(ClStrength strength) { _strength = strength; }
72 | *
73 | * private void setWeight(double weight) { _weight = weight; }
74 | */
75 | private ClStrength _strength;
76 | private double _weight;
77 |
78 | private Object _attachedObject;
79 | }
80 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClDouble.java:
--------------------------------------------------------------------------------
1 | // $Id: ClDouble.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClDouble
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClDouble extends Number {
16 | /**
17 | *
18 | */
19 | private static final long serialVersionUID = 1L;
20 |
21 | public ClDouble(double val) {
22 | value = val;
23 | }
24 |
25 | public ClDouble() {
26 | this(0.0);
27 | }
28 |
29 | public final Object clone() {
30 | return new ClDouble(value);
31 | }
32 |
33 | public final double doubleValue() {
34 | return value;
35 | }
36 |
37 | public final int intValue() {
38 | return (int) value;
39 | }
40 |
41 | public final long longValue() {
42 | return (long) value;
43 | }
44 |
45 | public final float floatValue() {
46 | return (float) value;
47 | }
48 |
49 | public final byte byteValue() {
50 | return (byte) value;
51 | }
52 |
53 | public final short shortValue() {
54 | return (short) value;
55 | }
56 |
57 | public final void setValue(double val) {
58 | value = val;
59 | }
60 |
61 | public final String toString() {
62 | return java.lang.Double.toString(value);
63 | }
64 |
65 | public final boolean equals(Object o) {
66 | try {
67 | return value == ((ClDouble) o).value;
68 | } catch (Exception err) {
69 | return false;
70 | }
71 | }
72 |
73 | public final int hashCode() {
74 | System.err.println("ClDouble.hashCode() called!");
75 | return (int) java.lang.Double.doubleToLongBits(value);
76 | }
77 |
78 | private double value;
79 | }
80 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClDummyVariable.java:
--------------------------------------------------------------------------------
1 | // $Id: ClDummyVariable.java,v 1.1 2008/08/12 22:32:42 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClDummyVariable
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | class ClDummyVariable extends ClAbstractVariable {
16 | // friend ClTableau;
17 | // friend ClSimplexSolver;
18 |
19 | public ClDummyVariable(String name) {
20 | super(name);
21 | }
22 |
23 | public ClDummyVariable() {
24 | }
25 |
26 | public ClDummyVariable(long number, String prefix) {
27 | super(number, prefix);
28 | }
29 |
30 | public String toString() {
31 | return "[" + name() + ":dummy]";
32 | }
33 |
34 | public boolean isDummy() {
35 | return true;
36 | }
37 |
38 | public boolean isExternal() {
39 | return false;
40 | }
41 |
42 | public boolean isPivotable() {
43 | return false;
44 | }
45 |
46 | public boolean isRestricted() {
47 | return true;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClEditConstraint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClEditConstraint.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClEditConstraint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClEditConstraint extends ClEditOrStayConstraint {
16 |
17 | public ClEditConstraint(ClVariable clv, ClStrength strength, double weight) {
18 | super(clv, strength, weight);
19 | }
20 |
21 | public ClEditConstraint(ClVariable clv, ClStrength strength) {
22 | super(clv, strength);
23 | }
24 |
25 | public ClEditConstraint(ClVariable clv) {
26 | super(clv);
27 | }
28 |
29 | public boolean isEditConstraint() {
30 | return true;
31 | }
32 |
33 | public String toString() {
34 | return "edit" + super.toString();
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClEditInfo.java:
--------------------------------------------------------------------------------
1 | // $Id: ClEditInfo.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClEditInfo
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | // ClEditInfo is a privately-used class
16 | // that just wraps a constraint, its positive and negative
17 | // error variables, and its prior edit constant.
18 | // It is used as values in _editVarMap, and replaces
19 | // the parallel vectors of error variables and previous edit
20 | // constants from the Smalltalk version of the code.
21 | class ClEditInfo {
22 | public ClEditInfo(ClConstraint cn_, ClSlackVariable eplus_,
23 | ClSlackVariable eminus_, double prevEditConstant_, int i_) {
24 | cn = cn_;
25 | clvEditPlus = eplus_;
26 | clvEditMinus = eminus_;
27 | prevEditConstant = prevEditConstant_;
28 | i = i_;
29 | }
30 |
31 | public int Index() {
32 | return i;
33 | }
34 |
35 | public ClConstraint Constraint() {
36 | return cn;
37 | }
38 |
39 | public ClSlackVariable ClvEditPlus() {
40 | return clvEditPlus;
41 | }
42 |
43 | public ClSlackVariable ClvEditMinus() {
44 | return clvEditMinus;
45 | }
46 |
47 | public double PrevEditConstant() {
48 | return prevEditConstant;
49 | }
50 |
51 | public void SetPrevEditConstant(double prevEditConstant_) {
52 | prevEditConstant = prevEditConstant_;
53 | }
54 |
55 | private ClConstraint cn;
56 | private ClSlackVariable clvEditPlus;
57 | private ClSlackVariable clvEditMinus;
58 | private double prevEditConstant;
59 | private int i;
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClEditOrStayConstraint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClEditOrStayConstraint.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClEditOrStayConstraint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | abstract class ClEditOrStayConstraint extends ClConstraint {
16 |
17 | public ClEditOrStayConstraint(ClVariable var, ClStrength strength,
18 | double weight) {
19 | super(strength, weight);
20 | _variable = var;
21 | _expression = new ClLinearExpression(_variable, -1.0, _variable.value());
22 | }
23 |
24 | public ClEditOrStayConstraint(ClVariable var, ClStrength strength) {
25 | this(var, strength, 1.0);
26 | }
27 |
28 | public ClEditOrStayConstraint(ClVariable var) {
29 | this(var, ClStrength.required, 1.0);
30 | _variable = var;
31 | }
32 |
33 | public ClVariable variable() {
34 | return _variable;
35 | }
36 |
37 | public ClLinearExpression expression() {
38 | return _expression;
39 | }
40 |
41 | /*
42 | * private void setVariable(ClVariable v) { _variable = v; }
43 | */
44 | protected ClVariable _variable;
45 | // cache the expression
46 | private ClLinearExpression _expression;
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClLinearConstraint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClLinearConstraint.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClLinearConstraint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | class ClLinearConstraint extends ClConstraint {
16 |
17 | public ClLinearConstraint(ClLinearExpression cle, ClStrength strength,
18 | double weight) {
19 | super(strength, weight);
20 | _expression = cle;
21 | }
22 |
23 | public ClLinearConstraint(ClLinearExpression cle, ClStrength strength) {
24 | super(strength, 1.0);
25 | _expression = cle;
26 | }
27 |
28 | public ClLinearConstraint(ClLinearExpression cle) {
29 | super(ClStrength.required, 1.0);
30 | _expression = cle;
31 | }
32 |
33 | public ClLinearExpression expression() {
34 | return _expression;
35 | }
36 |
37 | protected void setExpression(ClLinearExpression expr) {
38 | _expression = expr;
39 | }
40 |
41 | protected ClLinearExpression _expression;
42 | }
43 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClLinearEquation.java:
--------------------------------------------------------------------------------
1 | // $Id: ClLinearEquation.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClLinearEquation
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClLinearEquation extends ClLinearConstraint {
16 | public ClLinearEquation(ClLinearExpression cle, ClStrength strength,
17 | double weight) {
18 | super(cle, strength, weight);
19 | }
20 |
21 | public ClLinearEquation(ClLinearExpression cle, ClStrength strength) {
22 | super(cle, strength);
23 | }
24 |
25 | public ClLinearEquation(ClLinearExpression cle) {
26 | super(cle);
27 | }
28 |
29 | public ClLinearEquation(ClAbstractVariable clv, ClLinearExpression cle,
30 | ClStrength strength, double weight) {
31 | super(cle, strength, weight);
32 | _expression.addVariable(clv, -1.0);
33 | }
34 |
35 | public ClLinearEquation(ClAbstractVariable clv, ClLinearExpression cle,
36 | ClStrength strength) {
37 | this(clv, cle, strength, 1.0);
38 | }
39 |
40 | public ClLinearEquation(ClAbstractVariable clv, ClLinearExpression cle) {
41 | this(clv, cle, ClStrength.required, 1.0);
42 | }
43 |
44 | public ClLinearEquation(ClAbstractVariable clv, double val,
45 | ClStrength strength, double weight) {
46 | super(new ClLinearExpression(val), strength, weight);
47 | _expression.addVariable(clv, -1.0);
48 | }
49 |
50 | public ClLinearEquation(ClAbstractVariable clv, double val,
51 | ClStrength strength) {
52 | this(clv, val, strength, 1.0);
53 | }
54 |
55 | public ClLinearEquation(ClAbstractVariable clv, double val) {
56 | this(clv, val, ClStrength.required, 1.0);
57 | }
58 |
59 | public ClLinearEquation(ClLinearExpression cle, ClAbstractVariable clv,
60 | ClStrength strength, double weight) {
61 | super(((ClLinearExpression) cle.clone()), strength, weight);
62 | _expression.addVariable(clv, -1.0);
63 | }
64 |
65 | public ClLinearEquation(ClLinearExpression cle, ClAbstractVariable clv,
66 | ClStrength strength) {
67 | this(cle, clv, strength, 1.0);
68 | }
69 |
70 | public ClLinearEquation(ClLinearExpression cle, ClAbstractVariable clv) {
71 | this(cle, clv, ClStrength.required, 1.0);
72 | }
73 |
74 | public ClLinearEquation(ClLinearExpression cle1, ClLinearExpression cle2,
75 | ClStrength strength, double weight) {
76 | super(((ClLinearExpression) cle1.clone()), strength, weight);
77 | _expression.addExpression(cle2, -1.0);
78 | }
79 |
80 | public ClLinearEquation(ClLinearExpression cle1, ClLinearExpression cle2,
81 | ClStrength strength) {
82 | this(cle1, cle2, strength, 1.0);
83 | }
84 |
85 | public ClLinearEquation(ClLinearExpression cle1, ClLinearExpression cle2) {
86 | this(cle1, cle2, ClStrength.required, 1.0);
87 | }
88 |
89 | public String toString() {
90 | return super.toString() + " = 0 )";
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClLinearExpression.java:
--------------------------------------------------------------------------------
1 | // $Id: ClLinearExpression.java,v 1.1 2008/08/12 22:32:42 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClLinearExpression
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | import java.util.*;
16 |
17 | public class ClLinearExpression extends CL {
18 |
19 | public ClLinearExpression(ClAbstractVariable clv, double value,
20 | double constant) {
21 | if (CL.fGC)
22 | System.err.println("new ClLinearExpression");
23 |
24 | _constant = new ClDouble(constant);
25 | _terms = new Hashtable(1);
26 | if (clv != null)
27 | _terms.put(clv, new ClDouble(value));
28 | }
29 |
30 | public ClLinearExpression(double num) {
31 | this(null, 0, num);
32 | }
33 |
34 | public ClLinearExpression() {
35 | this(0);
36 | }
37 |
38 | public ClLinearExpression(ClAbstractVariable clv, double value) {
39 | this(clv, value, 0.0);
40 | }
41 |
42 | public ClLinearExpression(ClAbstractVariable clv) {
43 | this(clv, 1, 0);
44 | }
45 |
46 | // for use by the clone method
47 | protected ClLinearExpression(ClDouble constant,
48 | Hashtable terms) {
49 | if (CL.fGC)
50 | System.err.println("clone ClLinearExpression");
51 | _constant = (ClDouble) constant.clone();
52 | _terms = new Hashtable();
53 | // need to unalias the ClDouble-s that we clone (do a deep clone)
54 | for (Enumeration e = terms.keys(); e
55 | .hasMoreElements();) {
56 | ClAbstractVariable clv = e.nextElement();
57 | _terms.put(clv, (ClDouble) terms.get(clv).clone());
58 | }
59 | }
60 |
61 | public ClLinearExpression multiplyMe(double x) {
62 | _constant.setValue(_constant.doubleValue() * x);
63 |
64 | for (Enumeration e = _terms.keys(); e
65 | .hasMoreElements();) {
66 | ClAbstractVariable clv = e.nextElement();
67 | ClDouble cld = _terms.get(clv);
68 | cld.setValue(cld.doubleValue() * x);
69 | }
70 | return this;
71 | }
72 |
73 | public final Object clone() {
74 | return new ClLinearExpression(_constant, _terms);
75 | }
76 |
77 | public final ClLinearExpression times(double x) {
78 | return ((ClLinearExpression) clone()).multiplyMe(x);
79 | }
80 |
81 | public final ClLinearExpression times(ClLinearExpression expr)
82 | throws ExCLNonlinearExpression {
83 | if (isConstant()) {
84 | return expr.times(_constant.doubleValue());
85 | } else if (!expr.isConstant()) {
86 | throw new ExCLNonlinearExpression();
87 | }
88 | return times(expr._constant.doubleValue());
89 | }
90 |
91 | public final ClLinearExpression plus(ClLinearExpression expr) {
92 | return ((ClLinearExpression) clone()).addExpression(expr, 1.0);
93 | }
94 |
95 | public final ClLinearExpression plus(ClVariable var)
96 | throws ExCLNonlinearExpression {
97 | return ((ClLinearExpression) clone()).addVariable(var, 1.0);
98 | }
99 |
100 | public final ClLinearExpression minus(ClLinearExpression expr) {
101 | return ((ClLinearExpression) clone()).addExpression(expr, -1.0);
102 | }
103 |
104 | public final ClLinearExpression minus(ClVariable var)
105 | throws ExCLNonlinearExpression {
106 | return ((ClLinearExpression) clone()).addVariable(var, -1.0);
107 | }
108 |
109 | public final ClLinearExpression divide(double x)
110 | throws ExCLNonlinearExpression {
111 | if (CL.approx(x, 0.0)) {
112 | throw new ExCLNonlinearExpression();
113 | }
114 | return times(1.0 / x);
115 | }
116 |
117 | public final ClLinearExpression divide(ClLinearExpression expr)
118 | throws ExCLNonlinearExpression {
119 | if (!expr.isConstant()) {
120 | throw new ExCLNonlinearExpression();
121 | }
122 | return divide(expr._constant.doubleValue());
123 | }
124 |
125 | public final ClLinearExpression divFrom(ClLinearExpression expr)
126 | throws ExCLNonlinearExpression {
127 | if (!isConstant() || CL.approx(_constant.doubleValue(), 0.0)) {
128 | throw new ExCLNonlinearExpression();
129 | }
130 | return expr.divide(_constant.doubleValue());
131 | }
132 |
133 | public final ClLinearExpression subtractFrom(ClLinearExpression expr) {
134 | return expr.minus(this);
135 | }
136 |
137 | // Add n*expr to this expression from another expression expr.
138 | // Notify the solver if a variable is added or deleted from this
139 | // expression.
140 | public final ClLinearExpression addExpression(ClLinearExpression expr,
141 | double n, ClAbstractVariable subject, ClTableau solver) {
142 | incrementConstant(n * expr.constant());
143 |
144 | for (Enumeration e = expr.terms().keys(); e
145 | .hasMoreElements();) {
146 | ClAbstractVariable clv = e.nextElement();
147 | double coeff = expr.terms().get(clv).doubleValue();
148 | addVariable(clv, coeff * n, subject, solver);
149 | }
150 | return this;
151 | }
152 |
153 | // Add n*expr to this expression from another expression expr.
154 | public final ClLinearExpression addExpression(ClLinearExpression expr,
155 | double n) {
156 | incrementConstant(n * expr.constant());
157 |
158 | for (Enumeration e = expr.terms().keys(); e
159 | .hasMoreElements();) {
160 | ClAbstractVariable clv = e.nextElement();
161 | double coeff = expr.terms().get(clv).doubleValue();
162 | addVariable(clv, coeff * n);
163 | }
164 | return this;
165 | }
166 |
167 | public final ClLinearExpression addExpression(ClLinearExpression expr) {
168 | return addExpression(expr, 1.0);
169 | }
170 |
171 | // Add a term c*v to this expression. If the expression already
172 | // contains a term involving v, add c to the existing coefficient.
173 | // If the new coefficient is approximately 0, delete v.
174 | public final ClLinearExpression addVariable(ClAbstractVariable v, double c) { // body
175 | // largely
176 | // duplicated
177 | // below
178 | if (fTraceOn)
179 | fnenterprint("addVariable:" + v + ", " + c);
180 |
181 | ClDouble coeff = _terms.get(v);
182 | if (coeff != null) {
183 | double new_coefficient = coeff.doubleValue() + c;
184 | if (CL.approx(new_coefficient, 0.0)) {
185 | _terms.remove(v);
186 | } else {
187 | coeff.setValue(new_coefficient);
188 | }
189 | } else {
190 | if (!CL.approx(c, 0.0)) {
191 | _terms.put(v, new ClDouble(c));
192 | }
193 | }
194 | return this;
195 | }
196 |
197 | public final ClLinearExpression addVariable(ClAbstractVariable v) {
198 | return addVariable(v, 1.0);
199 | }
200 |
201 | public final ClLinearExpression setVariable(ClAbstractVariable v, double c) {
202 | // assert(c != 0.0);
203 | ClDouble coeff = _terms.get(v);
204 | if (coeff != null)
205 | coeff.setValue(c);
206 | else
207 | _terms.put(v, new ClDouble(c));
208 | return this;
209 | }
210 |
211 | // Add a term c*v to this expression. If the expression already
212 | // contains a term involving v, add c to the existing coefficient.
213 | // If the new coefficient is approximately 0, delete v. Notify the
214 | // solver if v appears or disappears from this expression.
215 | public final ClLinearExpression addVariable(ClAbstractVariable v, double c,
216 | ClAbstractVariable subject, ClTableau solver) { // body largely
217 | // duplicated
218 | // above
219 | if (fTraceOn)
220 | fnenterprint("addVariable:" + v + ", " + c + ", " + subject
221 | + ", ...");
222 |
223 | ClDouble coeff = _terms.get(v);
224 | if (coeff != null) {
225 | double new_coefficient = coeff.doubleValue() + c;
226 | if (CL.approx(new_coefficient, 0.0)) {
227 | solver.noteRemovedVariable(v, subject);
228 | _terms.remove(v);
229 | } else {
230 | coeff.setValue(new_coefficient);
231 | }
232 | } else {
233 | if (!CL.approx(c, 0.0)) {
234 | _terms.put(v, new ClDouble(c));
235 | solver.noteAddedVariable(v, subject);
236 | }
237 | }
238 | return this;
239 | }
240 |
241 | // Return a pivotable variable in this expression. (It is an error
242 | // if this expression is constant -- signal ExCLInternalError in
243 | // that case). Return null if no pivotable variables
244 | public final ClAbstractVariable anyPivotableVariable()
245 | throws ExCLInternalError {
246 | if (isConstant()) {
247 | throw new ExCLInternalError(
248 | "anyPivotableVariable called on a constant");
249 | }
250 |
251 | for (Enumeration e = _terms.keys(); e
252 | .hasMoreElements();) {
253 | ClAbstractVariable clv = e.nextElement();
254 | if (clv.isPivotable())
255 | return clv;
256 | }
257 |
258 | // No pivotable variables, so just return null, and let the caller
259 | // error if needed
260 | return null;
261 | }
262 |
263 | // Replace var with a symbolic expression expr that is equal to it.
264 | // If a variable has been added to this expression that wasn't there
265 | // before, or if a variable has been dropped from this expression
266 | // because it now has a coefficient of 0, inform the solver.
267 | // PRECONDITIONS:
268 | // var occurs with a non-zero coefficient in this expression.
269 | public final void substituteOut(ClAbstractVariable var,
270 | ClLinearExpression expr, ClAbstractVariable subject,
271 | ClTableau solver) {
272 | if (fTraceOn)
273 | fnenterprint("CLE:substituteOut: " + var + ", " + expr + ", "
274 | + subject + ", ...");
275 | if (fTraceOn)
276 | traceprint("this = " + this);
277 |
278 | double multiplier = _terms.remove(var).doubleValue();
279 | incrementConstant(multiplier * expr.constant());
280 |
281 | for (Enumeration e = expr.terms().keys(); e
282 | .hasMoreElements();) {
283 | ClAbstractVariable clv = e.nextElement();
284 | double coeff = expr.terms().get(clv).doubleValue();
285 | ClDouble d_old_coeff = _terms.get(clv);
286 | if (d_old_coeff != null) {
287 | double old_coeff = d_old_coeff.doubleValue();
288 | double newCoeff = old_coeff + multiplier * coeff;
289 | if (CL.approx(newCoeff, 0.0)) {
290 | solver.noteRemovedVariable(clv, subject);
291 | _terms.remove(clv);
292 | } else {
293 | d_old_coeff.setValue(newCoeff);
294 | }
295 | } else {
296 | // did not have that variable already
297 | _terms.put(clv, new ClDouble(multiplier * coeff));
298 | solver.noteAddedVariable(clv, subject);
299 | }
300 | }
301 | if (fTraceOn)
302 | traceprint("Now this is " + this);
303 | }
304 |
305 | // This linear expression currently represents the equation
306 | // oldSubject=self. Destructively modify it so that it represents
307 | // the equation newSubject=self.
308 | //
309 | // Precondition: newSubject currently has a nonzero coefficient in
310 | // this expression.
311 | //
312 | // NOTES
313 | // Suppose this expression is c + a*newSubject + a1*v1 + ... + an*vn.
314 | //
315 | // Then the current equation is
316 | // oldSubject = c + a*newSubject + a1*v1 + ... + an*vn.
317 | // The new equation will be
318 | // newSubject = -c/a + oldSubject/a - (a1/a)*v1 - ... - (an/a)*vn.
319 | // Note that the term involving newSubject has been dropped.
320 | public final void changeSubject(ClAbstractVariable old_subject,
321 | ClAbstractVariable new_subject) {
322 | ClDouble cld = _terms.get(old_subject);
323 | if (cld != null)
324 | cld.setValue(newSubject(new_subject));
325 | else
326 | _terms.put(old_subject, new ClDouble(newSubject(new_subject)));
327 | }
328 |
329 | // This linear expression currently represents the equation self=0.
330 | // Destructively modify it so
331 | // that subject=self represents an equivalent equation.
332 | //
333 | // Precondition: subject must be one of the variables in this expression.
334 | // NOTES
335 | // Suppose this expression is
336 | // c + a*subject + a1*v1 + ... + an*vn
337 | // representing
338 | // c + a*subject + a1*v1 + ... + an*vn = 0
339 | // The modified expression will be
340 | // subject = -c/a - (a1/a)*v1 - ... - (an/a)*vn
341 | // representing
342 | // subject = -c/a - (a1/a)*v1 - ... - (an/a)*vn
343 | //
344 | // Note that the term involving subject has been dropped.
345 | // Returns the reciprocal, so changeSubject can use it, too
346 | public final double newSubject(ClAbstractVariable subject) {
347 | if (fTraceOn)
348 | fnenterprint("newSubject:" + subject);
349 | ClDouble coeff = _terms.remove(subject);
350 | double reciprocal = 1.0 / coeff.doubleValue();
351 | multiplyMe(-reciprocal);
352 | return reciprocal;
353 | }
354 |
355 | // Return the coefficient corresponding to variable var, i.e.,
356 | // the 'ci' corresponding to the 'vi' that var is:
357 | // v1*c1 + v2*c2 + .. + vn*cn + c
358 | public final double coefficientFor(ClAbstractVariable var) {
359 | ClDouble coeff = _terms.get(var);
360 | if (coeff != null)
361 | return coeff.doubleValue();
362 | else
363 | return 0.0;
364 | }
365 |
366 | public final double constant() {
367 | return _constant.doubleValue();
368 | }
369 |
370 | public final void set_constant(double c) {
371 | _constant.setValue(c);
372 | }
373 |
374 | public final Hashtable terms() {
375 | return _terms;
376 | }
377 |
378 | public final void incrementConstant(double c) {
379 | _constant.setValue(_constant.doubleValue() + c);
380 | }
381 |
382 | public final boolean isConstant() {
383 | return _terms.size() == 0;
384 | }
385 |
386 | public final String toString() {
387 | StringBuffer bstr = new StringBuffer();
388 | Enumeration e = _terms.keys();
389 |
390 | if (!CL.approx(_constant.doubleValue(), 0.0) || _terms.size() == 0) {
391 | bstr.append(_constant.toString());
392 | } else {
393 | if (_terms.size() == 0) {
394 | return bstr.toString();
395 | }
396 | ClAbstractVariable clv = e.nextElement();
397 | ClDouble coeff = _terms.get(clv);
398 | bstr.append(coeff.toString() + "*" + clv.toString());
399 | }
400 | for (; e.hasMoreElements();) {
401 | ClAbstractVariable clv = e.nextElement();
402 | ClDouble coeff = _terms.get(clv);
403 | bstr.append(" + " + coeff.toString() + "*" + clv.toString());
404 | }
405 | return bstr.toString();
406 | }
407 |
408 | public final static ClLinearExpression Plus(ClLinearExpression e1,
409 | ClLinearExpression e2) {
410 | return e1.plus(e2);
411 | }
412 |
413 | public final static ClLinearExpression Minus(ClLinearExpression e1,
414 | ClLinearExpression e2) {
415 | return e1.minus(e2);
416 | }
417 |
418 | public final static ClLinearExpression Times(ClLinearExpression e1,
419 | ClLinearExpression e2) throws ExCLNonlinearExpression {
420 | return e1.times(e2);
421 | }
422 |
423 | public final static ClLinearExpression Divide(ClLinearExpression e1,
424 | ClLinearExpression e2) throws ExCLNonlinearExpression {
425 | return e1.divide(e2);
426 | }
427 |
428 | public final static boolean FEquals(ClLinearExpression e1,
429 | ClLinearExpression e2) {
430 | return e1 == e2;
431 | }
432 |
433 | private ClDouble _constant;
434 | private Hashtable _terms; // from ClVariable
435 | // to
436 | // ClDouble
437 |
438 | }
439 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClLinearInequality.java:
--------------------------------------------------------------------------------
1 | // $Id: ClLinearInequality.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClLinearInequality
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClLinearInequality extends ClLinearConstraint {
16 |
17 | public ClLinearInequality(ClLinearExpression cle, ClStrength strength,
18 | double weight) {
19 | super(cle, strength, weight);
20 | }
21 |
22 | public ClLinearInequality(ClLinearExpression cle, ClStrength strength) {
23 | super(cle, strength);
24 | }
25 |
26 | public ClLinearInequality(ClLinearExpression cle) {
27 | super(cle);
28 | }
29 |
30 | public ClLinearInequality(ClVariable clv1, CL.Op op_enum, ClVariable clv2,
31 | ClStrength strength, double weight) throws ExCLInternalError {
32 | super(new ClLinearExpression(clv2), strength, weight);
33 | if (op_enum == CL.Op.GEQ) {
34 | _expression.multiplyMe(-1.0);
35 | _expression.addVariable(clv1);
36 | } else if (op_enum == CL.Op.LEQ) {
37 | _expression.addVariable(clv1, -1.0);
38 | } else
39 | // the operator was invalid
40 | throw new ExCLInternalError(
41 | "Invalid operator in ClLinearInequality constructor");
42 | }
43 |
44 | public ClLinearInequality(ClVariable clv1, CL.Op op_enum, ClVariable clv2,
45 | ClStrength strength) throws ExCLInternalError {
46 | this(clv1, op_enum, clv2, strength, 1.0);
47 | }
48 |
49 | public ClLinearInequality(ClVariable clv1, CL.Op op_enum, ClVariable clv2)
50 | throws ExCLInternalError {
51 | this(clv1, op_enum, clv2, ClStrength.required, 1.0);
52 | }
53 |
54 | public ClLinearInequality(ClVariable clv, CL.Op op_enum, double val,
55 | ClStrength strength, double weight) throws ExCLInternalError {
56 | super(new ClLinearExpression(val), strength, weight);
57 | if (op_enum == CL.Op.GEQ) {
58 | _expression.multiplyMe(-1.0);
59 | _expression.addVariable(clv);
60 | } else if (op_enum == CL.Op.LEQ) {
61 | _expression.addVariable(clv, -1.0);
62 | } else
63 | // the operator was invalid
64 | throw new ExCLInternalError(
65 | "Invalid operator in ClLinearInequality constructor");
66 | }
67 |
68 | public ClLinearInequality(ClVariable clv, CL.Op op_enum, double val,
69 | ClStrength strength) throws ExCLInternalError {
70 | this(clv, op_enum, val, strength, 1.0);
71 | }
72 |
73 | public ClLinearInequality(ClVariable clv, CL.Op op_enum, double val)
74 | throws ExCLInternalError {
75 | this(clv, op_enum, val, ClStrength.required, 1.0);
76 | }
77 |
78 | public ClLinearInequality(ClLinearExpression cle1, CL.Op op_enum,
79 | ClLinearExpression cle2, ClStrength strength, double weight)
80 | throws ExCLInternalError {
81 | super(((ClLinearExpression) cle2.clone()), strength, weight);
82 | if (op_enum == CL.Op.GEQ) {
83 | _expression.multiplyMe(-1.0);
84 | _expression.addExpression(cle1);
85 | } else if (op_enum == CL.Op.LEQ) {
86 | _expression.addExpression(cle1, -1.0);
87 | } else
88 | // the operator was invalid
89 | throw new ExCLInternalError(
90 | "Invalid operator in ClLinearInequality constructor");
91 | }
92 |
93 | public ClLinearInequality(ClLinearExpression cle1, CL.Op op_enum,
94 | ClLinearExpression cle2, ClStrength strength)
95 | throws ExCLInternalError {
96 | this(cle1, op_enum, cle2, strength, 1.0);
97 | }
98 |
99 | public ClLinearInequality(ClLinearExpression cle1, CL.Op op_enum,
100 | ClLinearExpression cle2) throws ExCLInternalError {
101 | this(cle1, op_enum, cle2, ClStrength.required, 1.0);
102 | }
103 |
104 | public ClLinearInequality(ClAbstractVariable clv, CL.Op op_enum,
105 | ClLinearExpression cle, ClStrength strength, double weight)
106 | throws ExCLInternalError {
107 | super(((ClLinearExpression) cle.clone()), strength, weight);
108 | if (op_enum == CL.Op.GEQ) {
109 | _expression.multiplyMe(-1.0);
110 | _expression.addVariable(clv);
111 | } else if (op_enum == CL.Op.LEQ) {
112 | _expression.addVariable(clv, -1.0);
113 | } else
114 | // the operator was invalid
115 | throw new ExCLInternalError(
116 | "Invalid operator in ClLinearInequality constructor");
117 | }
118 |
119 | public ClLinearInequality(ClAbstractVariable clv, CL.Op op_enum,
120 | ClLinearExpression cle, ClStrength strength)
121 | throws ExCLInternalError {
122 | this(clv, op_enum, cle, strength, 1.0);
123 | }
124 |
125 | public ClLinearInequality(ClAbstractVariable clv, CL.Op op_enum,
126 | ClLinearExpression cle) throws ExCLInternalError {
127 | this(clv, op_enum, cle, ClStrength.required, 1.0);
128 | }
129 |
130 | public ClLinearInequality(ClLinearExpression cle, CL.Op op_enum,
131 | ClAbstractVariable clv, ClStrength strength, double weight)
132 | throws ExCLInternalError {
133 | super(((ClLinearExpression) cle.clone()), strength, weight);
134 | if (op_enum == CL.Op.LEQ) {
135 | _expression.multiplyMe(-1.0);
136 | _expression.addVariable(clv);
137 | } else if (op_enum == CL.Op.GEQ) {
138 | _expression.addVariable(clv, -1.0);
139 | } else
140 | // the operator was invalid
141 | throw new ExCLInternalError(
142 | "Invalid operator in ClLinearInequality constructor");
143 | }
144 |
145 | public ClLinearInequality(ClLinearExpression cle, CL.Op op_enum,
146 | ClAbstractVariable clv, ClStrength strength)
147 | throws ExCLInternalError {
148 | this(cle, op_enum, clv, strength, 1.0);
149 | }
150 |
151 | public ClLinearInequality(ClLinearExpression cle, CL.Op op_enum,
152 | ClAbstractVariable clv) throws ExCLInternalError {
153 | this(cle, op_enum, clv, ClStrength.required, 1.0);
154 | }
155 |
156 | public final boolean isInequality() {
157 | return true;
158 | }
159 |
160 | public final String toString() {
161 | return super.toString() + " >= 0 )";
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClObjectiveVariable.java:
--------------------------------------------------------------------------------
1 | // $Id: ClObjectiveVariable.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClObjectiveVariable
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | class ClObjectiveVariable extends ClAbstractVariable {
16 | public ClObjectiveVariable(String name) {
17 | super(name);
18 | }
19 |
20 | public ClObjectiveVariable(long number, String prefix) {
21 | super(number, prefix);
22 | }
23 |
24 | public String toString()
25 | // { return "[" + name() + ":obj:" + hashCode() + "]"; }
26 | {
27 | return "[" + name() + ":obj]";
28 | }
29 |
30 | public boolean isExternal() {
31 | return false;
32 | }
33 |
34 | public boolean isPivotable() {
35 | return false;
36 | }
37 |
38 | public boolean isRestricted() {
39 | return false;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClParseTest.java:
--------------------------------------------------------------------------------
1 | // $Id: ClParseTest.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | // ClParseTest.java
3 | // Needs JavaCUP 10k or newer, from:
4 | // http://www.cs.princeton.edu/~appel/modern/java/CUP/
5 | // And JLex from:
6 | // http://www.cs.princeton.edu/~appel/modern/java/JLex/index.html
7 |
8 | package edu.gatech.constraints.cassowary;
9 |
10 | import java.io.StringReader;
11 | import java_cup.runtime.*;
12 | import java.util.*;
13 |
14 | public class ClParseTest {
15 | @SuppressWarnings("unchecked")
16 | public final static void main(String[] args) {
17 | try {
18 | System.out.println("Parsing: " + args[0]);
19 | StringReader string_reader = new StringReader(args[0]);
20 | Yylex yylex = new Yylex(string_reader);
21 | Hashtable varmap = new Hashtable();
22 | yylex.setVariableNameObjectHash(varmap);
23 | parser constraint_parser = new parser(yylex);
24 | constraint_parser.setVariableNameObjectHash(varmap);
25 | Symbol symbol = constraint_parser.parse();
26 | System.out.println("Parsed as: " + symbol.value.toString());
27 | } catch (Exception exception) {
28 | System.out.println("Exception: " + exception.getMessage());
29 | exception.printStackTrace();
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClPoint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClPoint.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClPoint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClPoint {
16 | public ClPoint(double x, double y) {
17 | _clv_x = new ClVariable(x);
18 | _clv_y = new ClVariable(y);
19 | }
20 |
21 | public ClPoint(double x, double y, int a) {
22 | _clv_x = new ClVariable("x" + a, x);
23 | _clv_y = new ClVariable("y" + a, y);
24 | }
25 |
26 | public ClPoint(ClVariable clv_x, ClVariable clv_y) {
27 | _clv_x = clv_x;
28 | _clv_y = clv_y;
29 | }
30 |
31 | public ClVariable X() {
32 | return _clv_x;
33 | }
34 |
35 | public ClVariable Y() {
36 | return _clv_y;
37 | }
38 |
39 | // use only before adding into the solver
40 | public void SetXY(double x, double y) {
41 | _clv_x.set_value(x);
42 | _clv_y.set_value(y);
43 | }
44 |
45 | public void SetXY(ClVariable clv_x, ClVariable clv_y) {
46 | _clv_x = clv_x;
47 | _clv_y = clv_y;
48 | }
49 |
50 | public double Xvalue() {
51 | return X().value();
52 | }
53 |
54 | public double Yvalue() {
55 | return Y().value();
56 | }
57 |
58 | public String toString() {
59 | return "X: " + _clv_x.toString() + ", Y: " + _clv_y.toString();
60 | }
61 |
62 | private ClVariable _clv_x;
63 |
64 | private ClVariable _clv_y;
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClReader.cup:
--------------------------------------------------------------------------------
1 | // $Id: ClReader.cup,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | // ClReader.cup
3 | // (C) 1999 Will Portnoy and Greg J. Badros
4 | // Needs JavaCup package
5 | // http://www.cs.princeton.edu/~appel/modern/java/CUP/
6 | // See the README file for details.
7 |
8 | package EDU.Washington.grad.gjb.cassowary;
9 |
10 | import java_cup.runtime.*;
11 |
12 | parser code
13 | {:
14 | public java.util.Hashtable m_variable_name_object_hash;
15 | public boolean m_debug_parse = false;
16 |
17 | public void setVariableNameObjectHash(java.util.Hashtable variable_name_object_hash) {
18 | m_variable_name_object_hash = variable_name_object_hash;
19 | }
20 | :}
21 |
22 | terminal GEQ, LEQ, EQ, PLUS, MINUS, UMINUS, TIMES, DIVIDE, LPAREN, RPAREN;
23 | terminal Double NUMBER;
24 | terminal String VARIABLE;
25 |
26 | non terminal constraint, equation, inequality, expr;
27 |
28 | precedence left TIMES, DIVIDE;
29 | precedence left PLUS, MINUS;
30 | precedence left UMINUS;
31 |
32 | constraint ::= equation:a {: RESULT = a; :}
33 | | inequality:a {: RESULT = a; :}
34 | ;
35 |
36 | equation ::= expr:a EQ expr:b {: RESULT = (new ClLinearEquation ((ClLinearExpression) a, (ClLinearExpression) b)); :}
37 | ;
38 |
39 | inequality ::= expr:a GEQ expr:b {: RESULT = (new ClLinearInequality ((ClLinearExpression) a, CL.Op.GEQ, (ClLinearExpression) b)); :}
40 | | expr:a LEQ expr:b {: RESULT = (new ClLinearInequality ((ClLinearExpression) a, CL.Op.LEQ, (ClLinearExpression) b)); :}
41 | ;
42 |
43 | expr ::= NUMBER:a {: RESULT = (new ClLinearExpression(a.doubleValue())); :}
44 | | VARIABLE:a {:
45 |
46 | // look up variable first
47 | if (parser.m_debug_parse) {
48 | System.out.println("Grammar found variable: <" + a + ">");
49 | }
50 |
51 | if (parser.m_variable_name_object_hash == null) {
52 | System.err.println(" Don't have hash.");
53 | }
54 |
55 | if (parser.m_debug_parse) {
56 | if (! parser.m_variable_name_object_hash.containsKey(a)) {
57 | System.out.println(" Unrecognized variable parsed: <" + a + ">");
58 | } else {
59 | System.out.println(" Found variable: <" + a + "> in hash.");
60 | }
61 | }
62 |
63 | ClVariable variable_object = (ClVariable) parser.m_variable_name_object_hash.get(a);
64 |
65 | if (variable_object == null) {
66 | System.err.println(" Could not get hashed variable.");
67 | }
68 |
69 | RESULT = (new ClLinearExpression(variable_object));
70 | :}
71 |
72 | | expr:a PLUS expr:b {: RESULT = (CL.Plus ((ClLinearExpression) a, (ClLinearExpression) b)); :}
73 | | expr:a MINUS expr:b {: RESULT = (CL.Minus ((ClLinearExpression) a, (ClLinearExpression) b)); :}
74 | | expr:a TIMES expr:b {: RESULT = (CL.Times ((ClLinearExpression) a, (ClLinearExpression) b)); :}
75 | | expr:a DIVIDE expr:b {: RESULT = (CL.Divide ((ClLinearExpression) a, (ClLinearExpression) b)); :}
76 | | MINUS expr:a {: RESULT = (CL.Times (-1, (ClLinearExpression) a)); :}
77 | %prec UMINUS
78 | | LPAREN expr:a RPAREN {: RESULT = a; :}
79 | ;
80 |
81 | // Local Variables:
82 | // tab-width: 4
83 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClReader.flex:
--------------------------------------------------------------------------------
1 | // $Id: ClReader.flex,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 |
3 | package EDU.Washington.grad.gjb.cassowary;
4 | import java_cup.runtime.Symbol;
5 |
6 | %%
7 |
8 | %cup
9 |
10 | %public
11 |
12 | DIGIT = [0-9]
13 |
14 | ALPHA = [A-Za-z]
15 |
16 | ALPHANUM = [A-Za-z0-9]
17 |
18 | ID = {ALPHA}{ALPHANUM}*
19 |
20 | ws = [ \t\r\n\f]+
21 |
22 | %init{
23 |
24 | // code to go into constructor
25 |
26 | %init}
27 |
28 | %{
29 | // added code to lexer class
30 |
31 | private java.util.Hashtable m_variable_name_object_hash;
32 | public boolean m_debug_lex = false;
33 |
34 | public void setVariableNameObjectHash(java.util.Hashtable variable_name_object_hash)
35 | {
36 | m_variable_name_object_hash = variable_name_object_hash;
37 | }
38 |
39 | %}
40 |
41 | %%
42 |
43 | {ws} { /* skip white space */ }
44 |
45 | ">=" { return new Symbol(sym.GEQ); }
46 |
47 | "<=" { return new Symbol(sym.LEQ); }
48 |
49 | "=" { return new Symbol(sym.EQ); }
50 |
51 | "+" { return new Symbol(sym.PLUS); }
52 |
53 | "-" { return new Symbol(sym.MINUS); }
54 |
55 | "*" { return new Symbol(sym.TIMES); }
56 |
57 | "/" { return new Symbol(sym.DIVIDE); }
58 |
59 | "(" { return new Symbol(sym.LPAREN); }
60 |
61 | ")" { return new Symbol(sym.RPAREN); }
62 |
63 | {DIGIT}+("."{DIGIT}*)?|("."{DIGIT}+) { return new Symbol(sym.NUMBER, new Double(yytext())); }
64 |
65 | {ID} {
66 | String variable_name = new String(yytext());
67 |
68 | if (m_debug_lex) {
69 | System.out.println("Lexical analysis found <" + variable_name + ">");
70 | }
71 |
72 | if (! m_variable_name_object_hash.containsKey(variable_name)) {
73 | if (m_debug_lex) {
74 | System.out.println(" Putting it in hash for the first time.");
75 | }
76 |
77 | ClVariable variable = new ClVariable();
78 |
79 | Object return_value = m_variable_name_object_hash.put(variable_name, variable);
80 |
81 | if (return_value != null) {
82 | System.err.println("Variable was already in hash!!!!!");
83 | }
84 |
85 | if (m_debug_lex) {
86 | if (m_variable_name_object_hash.containsKey(variable_name)) {
87 | System.out.println(" Hash table now contains object.");
88 | } else {
89 | System.out.println(" Hash table does not contain object.");
90 | }
91 | }
92 |
93 | if (m_variable_name_object_hash.isEmpty()) {
94 | System.err.println(" How can the hashtable be empty after inserting something?");
95 | }
96 | } else {
97 | System.err.println(" Already in Hash.");
98 | }
99 |
100 | return new Symbol(sym.VARIABLE, variable_name);
101 | }
102 |
103 | . { System.err.println("Illegal character: " + yytext() ); }
104 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClSlackVariable.java:
--------------------------------------------------------------------------------
1 | // $Id: ClSlackVariable.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClSlackVariable
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | class ClSlackVariable extends ClAbstractVariable {
16 | // friend ClTableau;
17 | // friend ClSimplexSolver;
18 |
19 | public ClSlackVariable(String name) {
20 | super(name);
21 | }
22 |
23 | public ClSlackVariable() {
24 | }
25 |
26 | public ClSlackVariable(long number, String prefix) {
27 | super(number, prefix);
28 | }
29 |
30 | public String toString() {
31 | return "[" + name() + ":slack]";
32 | }
33 |
34 | public boolean isExternal() {
35 | return false;
36 | }
37 |
38 | public boolean isPivotable() {
39 | return true;
40 | }
41 |
42 | public boolean isRestricted() {
43 | return true;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClStayConstraint.java:
--------------------------------------------------------------------------------
1 | // $Id: ClStayConstraint.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClStayConstraint
11 | //
12 |
13 | package edu.gatech.constraints.cassowary;
14 |
15 | public class ClStayConstraint extends ClEditOrStayConstraint {
16 |
17 | public ClStayConstraint(ClVariable var, ClStrength strength, double weight) {
18 | super(var, strength, weight);
19 | }
20 |
21 | public ClStayConstraint(ClVariable var, ClStrength strength) {
22 | super(var, strength, 1.0);
23 | }
24 |
25 | public ClStayConstraint(ClVariable var) {
26 | super(var, ClStrength.weak, 1.0);
27 | }
28 |
29 | public boolean isStayConstraint() {
30 | return true;
31 | }
32 |
33 | public String toString() {
34 | return "stay " + super.toString();
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClStrength.java:
--------------------------------------------------------------------------------
1 | // $Id: ClStrength.java,v 1.1 2008/08/12 22:32:42 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClStrength
11 |
12 | package edu.gatech.constraints.cassowary;
13 |
14 | public class ClStrength {
15 | public ClStrength(String name, ClSymbolicWeight symbolicWeight) {
16 | _name = name;
17 | _symbolicWeight = symbolicWeight;
18 | }
19 |
20 | public ClStrength(String name, double w1, double w2, double w3) {
21 | _name = name;
22 | _symbolicWeight = new ClSymbolicWeight(w1, w2, w3);
23 | }
24 |
25 | public boolean isRequired() {
26 | return (this == required);
27 | }
28 |
29 | public String toString() {
30 | return name() + (!isRequired() ? (":" + symbolicWeight()) : "");
31 | }
32 |
33 | public ClSymbolicWeight symbolicWeight() {
34 | return _symbolicWeight;
35 | }
36 |
37 | public String name() {
38 | return _name;
39 | }
40 |
41 | public void set_name(String name) {
42 | _name = name;
43 | }
44 |
45 | public void set_symbolicWeight(ClSymbolicWeight symbolicWeight) {
46 | _symbolicWeight = symbolicWeight;
47 | }
48 |
49 | public static final ClStrength required = new ClStrength("",
50 | 1000, 1000, 1000);
51 |
52 | public static final ClStrength strong = new ClStrength("strong", 1.0, 0.0,
53 | 0.0);
54 |
55 | public static final ClStrength medium = new ClStrength("medium", 0.0, 1.0,
56 | 0.0);
57 |
58 | public static final ClStrength weak = new ClStrength("weak", 0.0, 0.0, 1.0);
59 |
60 | private String _name;
61 |
62 | private ClSymbolicWeight _symbolicWeight;
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClSymbolicWeight.java:
--------------------------------------------------------------------------------
1 | // $Id: ClSymbolicWeight.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClSymbolicWeight
11 |
12 | package edu.gatech.constraints.cassowary;
13 |
14 | public class ClSymbolicWeight {
15 |
16 | public ClSymbolicWeight(int cLevels) {
17 | _values = new double[cLevels];
18 | // FIXGJB: ok to assume these get initialized to 0?
19 | // for (int i = 0; i < cLevels; i++) {
20 | // _values[i] = 0;
21 | // }
22 | }
23 |
24 | public ClSymbolicWeight(double w1, double w2, double w3) {
25 | _values = new double[3];
26 | _values[0] = w1;
27 | _values[1] = w2;
28 | _values[2] = w3;
29 | }
30 |
31 | public ClSymbolicWeight(double[] weights) {
32 | final int cLevels = weights.length;
33 | _values = new double[cLevels];
34 | for (int i = 0; i < cLevels; i++) {
35 | _values[i] = weights[i];
36 | }
37 | }
38 |
39 | public static final ClSymbolicWeight clsZero = new ClSymbolicWeight(0.0,
40 | 0.0, 0.0);
41 |
42 | public Object clone() {
43 | return new ClSymbolicWeight(_values);
44 | }
45 |
46 | public ClSymbolicWeight times(double n) {
47 | ClSymbolicWeight clsw = (ClSymbolicWeight) clone();
48 | for (int i = 0; i < _values.length; i++) {
49 | clsw._values[i] *= n;
50 | }
51 | return clsw;
52 | }
53 |
54 | public ClSymbolicWeight divideBy(double n) {
55 | // assert(n != 0);
56 | ClSymbolicWeight clsw = (ClSymbolicWeight) clone();
57 | for (int i = 0; i < _values.length; i++) {
58 | clsw._values[i] /= n;
59 | }
60 | return clsw;
61 | }
62 |
63 | public ClSymbolicWeight add(ClSymbolicWeight cl) {
64 | // assert(cl.cLevels() == cLevels());
65 |
66 | ClSymbolicWeight clsw = (ClSymbolicWeight) clone();
67 | for (int i = 0; i < _values.length; i++) {
68 | clsw._values[i] += cl._values[i];
69 | }
70 | return clsw;
71 | }
72 |
73 | public ClSymbolicWeight subtract(ClSymbolicWeight cl) {
74 | // assert(cl.cLevels() == cLevels());
75 |
76 | ClSymbolicWeight clsw = (ClSymbolicWeight) clone();
77 | for (int i = 0; i < _values.length; i++) {
78 | clsw._values[i] -= cl._values[i];
79 | }
80 | return clsw;
81 | }
82 |
83 | public boolean lessThan(ClSymbolicWeight cl) {
84 | // assert cl.cLevels() == cLevels()
85 | for (int i = 0; i < _values.length; i++) {
86 | if (_values[i] < cl._values[i])
87 | return true;
88 | else if (_values[i] > cl._values[i])
89 | return false;
90 | }
91 | return false; // they are equal
92 | }
93 |
94 | public boolean lessThanOrEqual(ClSymbolicWeight cl) {
95 | // assert cl.cLevels() == cLevels()
96 | for (int i = 0; i < _values.length; i++) {
97 | if (_values[i] < cl._values[i])
98 | return true;
99 | else if (_values[i] > cl._values[i])
100 | return false;
101 | }
102 | return true; // they are equal
103 | }
104 |
105 | public boolean equal(ClSymbolicWeight cl) {
106 | for (int i = 0; i < _values.length; i++) {
107 | if (_values[i] != cl._values[i])
108 | return false;
109 | }
110 | return true; // they are equal
111 | }
112 |
113 | public boolean greaterThan(ClSymbolicWeight cl) {
114 | return !this.lessThanOrEqual(cl);
115 | }
116 |
117 | public boolean greaterThanOrEqual(ClSymbolicWeight cl) {
118 | return !this.lessThan(cl);
119 | }
120 |
121 | public boolean isNegative() {
122 | return this.lessThan(clsZero);
123 | }
124 |
125 | public double asDouble() {
126 | // ClSymbolicWeight clsw = (ClSymbolicWeight) clone(); // LM--Not used
127 | double sum = 0;
128 | double factor = 1;
129 | double multiplier = 1000;
130 | for (int i = _values.length - 1; i >= 0; i--) {
131 | sum += _values[i] * factor;
132 | factor *= multiplier;
133 | }
134 | return sum;
135 | }
136 |
137 | public String toString() {
138 | StringBuffer bstr = new StringBuffer("[");
139 | for (int i = 0; i < _values.length - 1; i++) {
140 | bstr.append(_values[i]);
141 | bstr.append(",");
142 | }
143 | bstr.append(_values[_values.length - 1]);
144 | bstr.append("]");
145 | return bstr.toString();
146 | }
147 |
148 | public int cLevels() {
149 | return _values.length;
150 | }
151 |
152 | private double[] _values;
153 |
154 | }
155 |
--------------------------------------------------------------------------------
/ConstraintBasedLayoutManager/src/edu/gatech/constraints/cassowary/ClTableau.java:
--------------------------------------------------------------------------------
1 | // $Id: ClTableau.java,v 1.1 2008/08/12 22:32:43 larrymelia Exp $
2 | //
3 | // Cassowary Incremental Constraint Solver
4 | // Original Smalltalk Implementation by Alan Borning
5 | // This Java Implementation by Greg J. Badros,
6 | // http://www.cs.washington.edu/homes/gjb
7 | // (C) 1998, 1999 Greg J. Badros and Alan Borning
8 | // See ../LICENSE for legal details regarding this software
9 | //
10 | // ClTableau
11 |
12 | package edu.gatech.constraints.cassowary;
13 |
14 | import java.util.*;
15 |
16 | class ClTableau extends CL {
17 | // ctr is protected, since this only supports an ADT for
18 | // the ClSimplexSolved class
19 | protected ClTableau() {
20 | _columns = new Hashtable();
21 | _rows = new Hashtable();
22 | _infeasibleRows = new Set();
23 | _externalRows = new Set();
24 | _externalParametricVars = new Set();
25 | }
26 |
27 | // Variable v has been removed from an expression. If the
28 | // expression is in a tableau the corresponding basic variable is
29 | // subject (or if subject is nil then it's in the objective function).
30 | // Update the column cross-indices.
31 | public final void noteRemovedVariable(ClAbstractVariable v,
32 | ClAbstractVariable subject) {
33 | if (fTraceOn)
34 | fnenterprint("noteRemovedVariable: " + v + ", " + subject);
35 | if (subject != null) {
36 | _columns.get(v).remove(subject);
37 | }
38 | }
39 |
40 | // v has been added to the linear expression for subject
41 | // update column cross indices
42 | public final void noteAddedVariable(ClAbstractVariable v,
43 | ClAbstractVariable subject) {
44 | if (fTraceOn)
45 | fnenterprint("noteAddedVariable: " + v + ", " + subject);
46 | if (subject != null) {
47 | insertColVar(v, subject);
48 | }
49 | }
50 |
51 | // Originally from Michael Noth
52 | public String getInternalInfo() {
53 | StringBuffer retstr = new StringBuffer("Tableau Information:\n");
54 | retstr.append("Rows: " + _rows.size());
55 | retstr.append(" (= " + (_rows.size() - 1) + " constraints)");
56 | retstr.append("\nColumns: " + _columns.size());
57 | retstr.append("\nInfeasible Rows: " + _infeasibleRows.size());
58 | retstr.append("\nExternal basic variables: " + _externalRows.size());
59 | retstr.append("\nExternal parametric variables: ");
60 | retstr.append(_externalParametricVars.size());
61 | retstr.append("\n");
62 |
63 | return retstr.toString();
64 | }
65 |
66 | public String toString() {
67 | StringBuffer bstr = new StringBuffer("Tableau:\n");
68 | for (Enumeration e = _rows.keys(); e
69 | .hasMoreElements();) {
70 | ClAbstractVariable clv = e.nextElement();
71 | ClLinearExpression expr = _rows.get(clv);
72 | bstr.append(clv.toString());
73 | bstr.append(" <==> ");
74 | bstr.append(expr.toString());
75 | bstr.append("\n");
76 | }
77 |
78 | bstr.append("\nColumns:\n");
79 | bstr.append(_columns.toString());
80 |
81 | bstr.append("\nInfeasible rows: ");
82 | bstr.append(_infeasibleRows.toString());
83 |
84 | bstr.append("External basic variables: ");
85 | bstr.append(_externalRows.toString());
86 |
87 | bstr.append("External parametric variables: ");
88 | bstr.append(_externalParametricVars.toString());
89 |
90 | return bstr.toString();
91 | }
92 |
93 | // Convenience function to insert a variable into
94 | // the set of rows stored at _columns[param_var],
95 | // creating a new set if needed
96 | private final void insertColVar(ClAbstractVariable param_var,
97 | ClAbstractVariable rowvar) {
98 | Set rowset = _columns.get(param_var);
99 | if (rowset == null)
100 | _columns.put(param_var, rowset = new Set());
101 | rowset.insert(rowvar);
102 | }
103 |
104 | // Add v=expr to the tableau, update column cross indices
105 | // v becomes a basic variable
106 | // expr is now owned by ClTableau class,
107 | // and ClTableauis responsible for deleting it
108 | // (also, expr better be allocated on the heap!)
109 | protected final void addRow(ClAbstractVariable var, ClLinearExpression expr) {
110 | if (fTraceOn)
111 | fnenterprint("addRow: " + var + ", " + expr);
112 |
113 | // for each variable in expr, add var to the set of rows which
114 | // have that variable in their expression
115 | _rows.put(var, expr);
116 |
117 | for (Enumeration e = expr.terms().keys(); e
118 | .hasMoreElements();) {
119 | ClAbstractVariable clv = e.nextElement();
120 | insertColVar(clv, var);
121 | if (clv.isExternal()) {
122 | _externalParametricVars.insert(clv);
123 | }
124 | }
125 | if (var.isExternal()) {
126 | _externalRows.insert(var);
127 | }
128 | if (fTraceOn)
129 | traceprint(this.toString());
130 | }
131 |
132 | // Remove v from the tableau -- remove the column cross indices for v
133 | // and remove v from every expression in rows in which v occurs
134 | protected final void removeColumn(ClAbstractVariable var) {
135 | if (fTraceOn)
136 | fnenterprint("removeColumn:" + var);
137 | // remove the rows with the variables in varset
138 |
139 | Set rows = _columns.remove(var);
140 |
141 | if (rows != null) {
142 | for (Enumeration