Revision: d25fdcd2f3
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 13:49:48 2010
Log: Improve efficiency of edge storage in Library
http://code.google.com/p/noop/source/detail?r=d25fdcd2f3
Revision: da4967e7ea
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 15:34:38 2010
Log: Fix the TODO in the interpreter system test where the XML files were
h...
http://code.google.com/p/noop/source/detail?r=da4967e7ea
Revision: 00fd27c7c1
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 15:35:07 2010
Log: Fix the TODO in the interpreter system test where the XML files were
h...
http://code.google.com/p/noop/source/detail?r=00fd27c7c1
==============================================================================
Revision: d25fdcd2f3
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 13:49:48 2010
Log: Improve efficiency of edge storage in Library
http://code.google.com/p/noop/source/detail?r=d25fdcd2f3
Modified:
/core/src/main/java/noop/graph/Workspace.java
/core/src/main/java/noop/model/Library.java
=======================================
--- /core/src/main/java/noop/graph/Workspace.java Sun Jun 27 13:24:51 2010
+++ /core/src/main/java/noop/graph/Workspace.java Sun Jun 27 13:49:48 2010
@@ -19,13 +19,14 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.inject.Singleton;
-import noop.model.LanguageElement;
-import noop.model.Library;
-import noop.model.Project;
import java.util.List;
import java.util.UUID;
+import noop.model.LanguageElement;
+import noop.model.Library;
+import noop.model.Project;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
@@ -53,6 +54,7 @@
return ImmutableList.copyOf(projects);
}
+ // TODO: cache these lookups
public Library lookupLibrary(UUID libraryUid) {
for (Project project : projects) {
for (Library library : project.getLibraries()) {
=======================================
--- /core/src/main/java/noop/model/Library.java Sun Jun 27 13:24:51 2010
+++ /core/src/main/java/noop/model/Library.java Sun Jun 27 13:49:48 2010
@@ -16,29 +16,28 @@
package noop.model;
-import com.google.common.base.Nullable;
-import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
-import noop.graph.Edge;
-import noop.graph.ModelVisitor;
-import noop.graph.Vertex;
import java.util.List;
import java.util.UUID;
+import static com.google.common.collect.Iterables.emptyIterable;
+import static com.google.common.collect.Lists.newArrayList;
+import noop.graph.Edge;
+import noop.graph.ModelVisitor;
+import noop.graph.Vertex;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
public class Library extends LanguageElement<Library> {
public final UUID uid;
public final String name;
- // TODO list of list of edges, first list indexed same as 'src' node
- private final List<Edge> edges = Lists.newArrayList();
- private final List<LanguageElement> elements = Lists.newArrayList();
- private final List<Clazz> classes = Lists.newArrayList();
- private final List<Block> functions = Lists.newArrayList();
+ private final List<List<Edge>> edges = newArrayList();
+ private final List<LanguageElement> elements = newArrayList();
+ private final List<Clazz> classes = newArrayList();
+ private final List<Block> functions = newArrayList();
public Library(UUID uid, String name) {
this.uid = uid;
@@ -71,12 +70,10 @@
public Iterable<Edge> edgesFrom(final Vertex src) {
- return Iterables.filter(edges, new Predicate<Edge>() {
- @Override
- public boolean apply(@Nullable Edge input) {
- return input.src == src;
- }
- });
+ if (src.index >= edges.size()) {
+ return emptyIterable();
+ }
+ return edges.get(src.index);
}
public List<LanguageElement> getElements() {
@@ -94,6 +91,10 @@
}
public void addEdge(Edge edge) {
- edges.add(edge);
+ int index = edge.src.index;
+ while (edges.size() <= index) {
+ edges.add(Lists.<Edge>newLinkedList());
+ }
+ edges.get(index).add(edge);
}
}
==============================================================================
Revision: da4967e7ea
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 15:34:38 2010
Log: Fix the TODO in the interpreter system test where the XML files were
hardcoded.
To do this, extracted a persistence layer based around XStream, and used it
to create the standard library and test program on-the-fly, writing them to
the on-disk XML storage location.
http://code.google.com/p/noop/source/detail?r=da4967e7ea
Added:
/core/src/main/java/noop/persistence/ModelSerializer.java
Deleted:
/core/src/test/java/noop/graph/ModelSerializer.java
=======================================
--- /dev/null
+++ /core/src/main/java/noop/persistence/ModelSerializer.java Sun Jun 27
15:34:38 2010
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package noop.persistence;
+
+import com.google.inject.Inject;
+
+import com.thoughtworks.xstream.XStream;
+
+import java.io.PrintStream;
+import java.io.Reader;
+
+import noop.graph.DotGraphPrintingVisitor;
+import noop.graph.OutlinePrintingVisitor;
+import noop.model.LanguageElement;
+import noop.model.Library;
+
+/**
+ * Writes the content of a language element in one of a few possible
formats.
+ * Reads a library back from the XML format only.
+ * TODO: a "SOURCE" format would allow text-based editing of noop programs
+ * @author alex...@google.com (Alex Eagle)
+ */
+public class ModelSerializer {
+ private final SerializationFormat serializationFormat;
+ private final XStream xStream;
+
+ @Inject
+ public ModelSerializer(SerializationFormat serializationFormat, XStream
xStream) {
+ this.serializationFormat = serializationFormat;
+ this.xStream = xStream;
+ }
+
+ public enum SerializationFormat {
+ TXT, XML, DOT;
+ }
+ public void write(LanguageElement element, PrintStream out) {
+ switch (serializationFormat) {
+ case DOT:
+ DotGraphPrintingVisitor visitor = new DotGraphPrintingVisitor(out);
+ visitor.enter(element);
+ element.accept(visitor);
+ visitor.leave(element);
+ break;
+ case TXT:
+ OutlinePrintingVisitor v = new OutlinePrintingVisitor(out);
+ v.enter(element);
+ element.accept(v);
+ v.leave(element);
+ break;
+ case XML:
+ out.append(xStream.toXML(element));
+ break;
+ default:
+ throw new RuntimeException("unknown output type " +
serializationFormat);
+ }
+ }
+
+ public Library read(Reader libraryFile) {
+ return (Library) xStream.fromXML(libraryFile);
+ }
+}
=======================================
--- /core/src/test/java/noop/graph/ModelSerializer.java Sat May 8 13:26:32
2010
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright 2010 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package noop.graph;
-
-import com.thoughtworks.xstream.XStream;
-import noop.model.LanguageElement;
-
-import java.io.PrintStream;
-
-/**
- * Dumps the content of a workspace in one of a few possible formats.
- * @author alex...@google.com (Alex Eagle)
- */
-public class ModelSerializer {
- private final Output output;
- private final PrintStream out;
-
- public ModelSerializer(Output output, PrintStream out) {
- this.output = output;
- this.out = out;
- }
-
- public enum Output {
- TXT, XML, DOT
- }
-
- public void dump(LanguageElement element) {
- switch (output) {
- case DOT:
- DotGraphPrintingVisitor visitor = new DotGraphPrintingVisitor(out);
- visitor.enter(element);
- element.accept(visitor);
- visitor.leave(element);
- break;
- case TXT:
- OutlinePrintingVisitor v = new OutlinePrintingVisitor(out);
- v.enter(element);
- element.accept(v);
- v.leave(element);
- break;
- case XML:
- XStream xStream = new XStream();
- out.append(xStream.toXML(element));
- break;
- default:
- throw new RuntimeException("unknown output type " + output);
- }
- }
-}
==============================================================================
Revision: 00fd27c7c1
Author: Alex Eagle <alex...@google.com>
Date: Sun Jun 27 15:35:07 2010
Log: Fix the TODO in the interpreter system test where the XML files were
hardcoded.
To do this, extracted a persistence layer based around XStream, and used it
to create the standard library and test program on-the-fly, writing them to
the on-disk XML storage location.
http://code.google.com/p/noop/source/detail?r=00fd27c7c1
Added:
/core/src/main/java/noop/persistence/LibraryRepository.java
/core/src/main/java/noop/persistence/XmlDiskCache.java
/interpreter/src/main/java/noop/interpreter/CommandLineLibraryNameParser.java
/interpreter/src/main/java/noop/interpreter/config/OptionsBuilder.java
/interpreter/src/test/java/noop/interpreter/CommandLineLibraryNameParserTest.java
Modified:
/core/src/main/java/noop/graph/Controller.java
/core/src/main/java/noop/model/Library.java
/core/src/main/java/noop/model/Project.java
/core/src/main/java/noop/stdlib/StandardLibraryBuilder.java
/core/src/test/java/noop/graph/ArithmeticExampleTest.java
/core/src/test/java/noop/graph/ControlFlowExampleTest.java
/core/src/test/java/noop/graph/DumpExamplesMain.java
/core/src/test/java/noop/graph/HelloWorldExampleTest.java
/interpreter/src/main/java/noop/interpreter/Interpreter.java
/interpreter/src/main/java/noop/interpreter/config/InterpreterModule.java
/interpreter/src/test/java/noop/interpreter/InterpreterSystemTest.java
/pom.xml
=======================================
--- /dev/null
+++ /core/src/main/java/noop/persistence/LibraryRepository.java Sun Jun 27
15:35:07 2010
@@ -0,0 +1,54 @@
+package noop.persistence;
+
+import com.google.inject.Inject;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.PrintStream;
+
+import noop.model.Library;
+import noop.model.Project;
+
+/**
+ * Saves and loads library objects from local disk.
+ * @author Alex Eagle (alex...@google.com)
+ */
+public class LibraryRepository {
+ private final File xmlDiskCache;
+ private final ModelSerializer modelSerializer;
+
+ @Inject
+ public LibraryRepository(@XmlDiskCache File xmlDiskCache,
ModelSerializer modelSerializer) {
+ this.xmlDiskCache = xmlDiskCache;
+ this.modelSerializer = modelSerializer;
+ }
+
+ public void save(Project project) {
+ for (Library library : project.getLibraries()) {
+ File outFile = locateLibrary(project, library.name);
+ try {
+ PrintStream stream = new PrintStream(new
FileOutputStream(outFile));
+ modelSerializer.write(library, stream);
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException("Failed to write to disk", e);
+ }
+ }
+ }
+
+ public Library load(Project project, String libraryName) {
+ try {
+ return modelSerializer.read(new FileReader(locateLibrary(project,
libraryName)));
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException("Library file not found", e);
+ }
+ }
+
+ private File locateLibrary(Project project, String libraryName) {
+ File projectDir = new File(new File(xmlDiskCache,
project.getNamespace()), project.getName());
+ projectDir.mkdirs();
+ File outFile = new File(projectDir, libraryName + ".xml");
+ return outFile;
+ }
+}
=======================================
--- /dev/null
+++ /core/src/main/java/noop/persistence/XmlDiskCache.java Sun Jun 27
15:35:07 2010
@@ -0,0 +1,15 @@
+package noop.persistence;
+
+import com.google.inject.BindingAnnotation;
+
+import java.lang.annotation.Retention;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * @author Alex Eagle (alex...@google.com)
+ */
+@BindingAnnotation
+@Retention(RUNTIME)
+public @interface XmlDiskCache {
+}
=======================================
--- /dev/null
+++
/interpreter/src/main/java/noop/interpreter/CommandLineLibraryNameParser.java
Sun Jun 27 15:35:07 2010
@@ -0,0 +1,37 @@
+package noop.interpreter;
+
+import noop.model.Project;
+
+/**
+ * When specifying a library on the command line, a string-valued
representation is needed.
+ * This Parser looks for a format such as com.google.Noop/lang and
provides the
+ * Project and library name for the lang library.
+ * @author Alex Eagle (alex...@google.com)
+ */
+class CommandLineLibraryNameParser {
+ private String libraryPath;
+ private String libraryName;
+ private Project project;
+
+ public CommandLineLibraryNameParser(String libraryPath) {
+ this.libraryPath = libraryPath;
+ }
+
+ public String getLibraryName() {
+ return libraryName;
+ }
+
+ public Project getProject() {
+ return project;
+ }
+
+ public CommandLineLibraryNameParser invoke() {
+ String[] parts = libraryPath.split("/");
+ String project = parts[0];
+ libraryName = parts[1];
+ String namespace = project.substring(0, project.lastIndexOf("."));
+ String projectName = project.substring(project.lastIndexOf(".") + 1);
+ this.project = new Project(projectName, namespace, "");
+ return this;
+ }
+}
=======================================
--- /dev/null
+++ /interpreter/src/main/java/noop/interpreter/config/OptionsBuilder.java
Sun Jun 27 15:35:07 2010
@@ -0,0 +1,43 @@
+package noop.interpreter.config;
+
+import java.util.List;
+
+import static java.util.Collections.emptyList;
+
+/**
+ * Test-friendly way to create interpreter options without command-line
parsing.
+ * @author Alex Eagle (alex...@google.com)
+ */
+public class OptionsBuilder {
+ private Integer entryPoint = 1;
+ private String mainLib = "Default from OptionsBuilder";
+ private List<String> libraryPaths = emptyList();
+ private boolean verbose;
+
+ public InterpreterOptions build() {
+ return new ConfigurableOptions();
+ }
+
+ public class ConfigurableOptions implements InterpreterOptions {
+
+ @Override
+ public Integer getEntryPoint() {
+ return entryPoint;
+ }
+
+ @Override
+ public String getMainLib() {
+ return mainLib;
+ }
+
+ @Override
+ public List<String> getLibraryPaths() {
+ return libraryPaths;
+ }
+
+ @Override
+ public boolean isVerbose() {
+ return verbose;
+ }
+ }
+}
=======================================
--- /dev/null
+++
/interpreter/src/test/java/noop/interpreter/CommandLineLibraryNameParserTest.java
Sun Jun 27 15:35:07 2010
@@ -0,0 +1,19 @@
+package noop.interpreter;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author Alex Eagle (alex...@google.com)
+ */
+public class CommandLineLibraryNameParserTest {
+ @Test public void shouldParseLibraryName() {
+ CommandLineLibraryNameParser parser =
+ new CommandLineLibraryNameParser("com.google.Noop/foo").invoke();
+ assertEquals("com.google", parser.getProject().getNamespace());
+ assertEquals("Noop", parser.getProject().getName());
+ assertEquals("foo", parser.getLibraryName());
+
+ }
+}
=======================================
--- /core/src/main/java/noop/graph/Controller.java Sun Apr 25 07:46:55 2010
+++ /core/src/main/java/noop/graph/Controller.java Sun Jun 27 15:35:07 2010
@@ -16,6 +16,8 @@
package noop.graph;
+import com.google.inject.Inject;
+
import noop.model.LanguageElement;
import noop.model.Library;
import noop.operations.EditNodeOperation;
@@ -30,7 +32,8 @@
private Workspace workspace;
private final ModelVisitor addVertices;
- public Controller(Workspace workspace, ModelVisitor addVertices) {
+ @Inject
+ public Controller(Workspace workspace, VertexCreatingVisitor
addVertices) {
this.workspace = workspace;
this.addVertices = addVertices;
}
=======================================
--- /core/src/main/java/noop/model/Library.java Sun Jun 27 13:49:48 2010
+++ /core/src/main/java/noop/model/Library.java Sun Jun 27 15:35:07 2010
@@ -22,8 +22,8 @@
import java.util.List;
import java.util.UUID;
-import static com.google.common.collect.Iterables.emptyIterable;
import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Collections.emptyList;
import noop.graph.Edge;
import noop.graph.ModelVisitor;
import noop.graph.Vertex;
@@ -71,7 +71,7 @@
public Iterable<Edge> edgesFrom(final Vertex src) {
if (src.index >= edges.size()) {
- return emptyIterable();
+ return emptyList();
}
return edges.get(src.index);
}
=======================================
--- /core/src/main/java/noop/model/Project.java Tue May 18 22:10:03 2010
+++ /core/src/main/java/noop/model/Project.java Sun Jun 27 15:35:07 2010
@@ -18,12 +18,14 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
-import noop.graph.ModelVisitor;
+
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import java.util.List;
+import noop.graph.ModelVisitor;
+
/**
* TODO: seems like the library could be the top level entity.
* @author alex...@google.com (Alex Eagle)
@@ -52,8 +54,9 @@
this.copyright = copyright;
}
- public void addLibrary(Library library) {
+ public Library addLibrary(Library library) {
this.libraries.add(library);
+ return library;
}
@Override
=======================================
--- /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Sat May 8
12:33:33 2010
+++ /core/src/main/java/noop/stdlib/StandardLibraryBuilder.java Sun Jun 27
15:35:07 2010
@@ -16,15 +16,20 @@
package noop.stdlib;
-import noop.graph.Controller;
-import noop.model.*;
-import noop.operations.NewEdgeOperation;
-import noop.operations.NewProjectOperation;
import org.joda.time.Instant;
import java.util.UUID;
+import noop.graph.Controller;
import static noop.graph.Edge.EdgeType.TYPEOF;
+import noop.model.Clazz;
+import noop.model.Comment;
+import noop.model.Library;
+import noop.model.Method;
+import noop.model.Parameter;
+import noop.model.Project;
+import noop.operations.NewEdgeOperation;
+import noop.operations.NewProjectOperation;
/**
* TODO: when we have a way to share serialized noop code, remove this
class
@@ -39,13 +44,16 @@
public Method printMethod;
public Method integerPlus;
public Method integerEquals;
-
- public void build(Controller controller) {
-
- Project project = new Project("Noop", "com.google.noop", "Apache 2");
-
- Library lang = new Library(UUID.randomUUID(), "lang");
- project.addLibrary(lang);
+ public Project noop;
+ public Library lang;
+ public Library io;
+
+ public StandardLibraryBuilder build(Controller controller) {
+
+ noop = new Project("Noop", "com.google", "Apache 2");
+
+ lang = new Library(UUID.randomUUID(), "lang");
+ noop.addLibrary(lang);
stringClazz = new Clazz("String");
lang.addClazz(stringClazz);
@@ -53,8 +61,8 @@
voidClazz = new Clazz("Void");
lang.addClazz(voidClazz);
- Library io = new Library(UUID.randomUUID(), "io");
- project.addLibrary(io);
+ io = new Library(UUID.randomUUID(), "io");
+ noop.addLibrary(io);
consoleClazz = new Clazz("Console");
io.addClazz(consoleClazz);
@@ -83,11 +91,13 @@
Parameter integerPlusArg = new Parameter("i");
integerPlus.addParameter(integerPlusArg);
- controller.apply(new NewProjectOperation(project));
+ controller.apply(new NewProjectOperation(noop));
controller.apply(new NewEdgeOperation(printMethod, TYPEOF, voidClazz));
controller.apply(new NewEdgeOperation(printArg, TYPEOF, stringClazz));
controller.apply(new NewEdgeOperation(integerPlus, TYPEOF, intClazz));
controller.apply(new NewEdgeOperation(integerEquals, TYPEOF,
booleanClazz));
controller.apply(new NewEdgeOperation(integerPlusArg, TYPEOF,
intClazz));
+
+ return this;
}
}
=======================================
--- /core/src/test/java/noop/graph/ArithmeticExampleTest.java Sat May 8
12:33:33 2010
+++ /core/src/test/java/noop/graph/ArithmeticExampleTest.java Sun Jun 27
15:35:07 2010
@@ -16,40 +16,42 @@
package noop.graph;
-import noop.graph.ModelSerializer.Output;
-import noop.model.Library;
-import noop.stdlib.StandardLibraryBuilder;
import org.junit.Before;
import org.junit.Test;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import noop.model.Library;
+import noop.persistence.ModelSerializer;
+import noop.persistence.ModelSerializer.SerializationFormat;
+import noop.stdlib.StandardLibraryBuilder;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
public class ArithmeticExampleTest {
- Workspace workspace;
- StandardLibraryBuilder stdLib;
- Controller controller;
- private ArithmeticExample arithmeticExample;
+ private PrintStream out = new PrintStream(new ByteArrayOutputStream());
private Library library;
@Before
public void setUp() {
- workspace = new Workspace();
- stdLib = new StandardLibraryBuilder();
- controller = new Controller(workspace, new VertexCreatingVisitor());
+ Workspace workspace = new Workspace();
+ StandardLibraryBuilder stdLib = new StandardLibraryBuilder();
+ Controller controller = new Controller(workspace, new
VertexCreatingVisitor());
stdLib.build(controller);
- arithmeticExample = new ArithmeticExample(stdLib);
+ ArithmeticExample arithmeticExample = new ArithmeticExample(stdLib);
arithmeticExample.createProgram(controller);
library = workspace.lookupLibrary(arithmeticExample.uid);
}
@Test
public void shouldCreateArithmeticDot() {
- new ModelSerializer(Output.DOT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.DOT, null).write(library, out);
}
@Test
public void shouldCreateArithmeticOutline() {
- new ModelSerializer(Output.TXT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.TXT, null).write(library, out);
}
}
=======================================
--- /core/src/test/java/noop/graph/ControlFlowExampleTest.java Sat May 8
13:26:32 2010
+++ /core/src/test/java/noop/graph/ControlFlowExampleTest.java Sun Jun 27
15:35:07 2010
@@ -16,40 +16,42 @@
package noop.graph;
-import noop.graph.ModelSerializer.Output;
-import noop.model.Library;
-import noop.stdlib.StandardLibraryBuilder;
import org.junit.Before;
import org.junit.Test;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import noop.model.Library;
+import noop.persistence.ModelSerializer;
+import noop.persistence.ModelSerializer.SerializationFormat;
+import noop.stdlib.StandardLibraryBuilder;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
public class ControlFlowExampleTest {
- Workspace workspace;
- StandardLibraryBuilder stdLib;
- Controller controller;
- private ControlFlowExample controlFlowExample;
+ private PrintStream out = new PrintStream(new ByteArrayOutputStream());
private Library library;
@Before
public void setUp() {
- workspace = new Workspace();
- stdLib = new StandardLibraryBuilder();
- controller = new Controller(workspace, new VertexCreatingVisitor());
+ Workspace workspace = new Workspace();
+ StandardLibraryBuilder stdLib = new StandardLibraryBuilder();
+ Controller controller = new Controller(workspace, new
VertexCreatingVisitor());
stdLib.build(controller);
- controlFlowExample = new ControlFlowExample(stdLib);
+ ControlFlowExample controlFlowExample = new ControlFlowExample(stdLib);
controlFlowExample.createProgram(controller);
library = workspace.lookupLibrary(controlFlowExample.uid);
}
@Test
public void shouldCreateControlFlowDot() {
- new ModelSerializer(Output.DOT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.DOT, null).write(library, out);
}
@Test
public void shouldCreateControlFlowOutline() {
- new ModelSerializer(Output.TXT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.TXT, null).write(library, out);
}
}
=======================================
--- /core/src/test/java/noop/graph/DumpExamplesMain.java Sun Apr 25
08:41:54 2010
+++ /core/src/test/java/noop/graph/DumpExamplesMain.java Sun Jun 27
15:35:07 2010
@@ -16,10 +16,7 @@
package noop.graph;
-import noop.graph.ModelSerializer.Output;
-import noop.model.Library;
-import noop.model.Project;
-import noop.stdlib.StandardLibraryBuilder;
+import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.io.FileNotFoundException;
@@ -27,6 +24,15 @@
import java.io.PrintStream;
import java.util.Arrays;
+import noop.model.Project;
+import noop.persistence.LibraryRepository;
+import noop.persistence.ModelSerializer;
+import noop.persistence.ModelSerializer.SerializationFormat;
+import static noop.persistence.ModelSerializer.SerializationFormat.DOT;
+import static noop.persistence.ModelSerializer.SerializationFormat.TXT;
+import static noop.persistence.ModelSerializer.SerializationFormat.XML;
+import noop.stdlib.StandardLibraryBuilder;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
@@ -58,19 +64,17 @@
stdLib.build(controller);
example.createProgram(controller);
- for (Output output : Arrays.asList(Output.DOT, Output.TXT)) {
- File outFile = new File(outDir, example.getClass().getName() + "."
+ output.name().toLowerCase());
- new ModelSerializer(output, new PrintStream(new
FileOutputStream(outFile))).dump(workspace);
+ for (SerializationFormat serializationFormat : Arrays.asList(DOT,
TXT)) {
+ String outFileName = example.getClass().getName() + "."
+ + serializationFormat.name().toLowerCase();
+ PrintStream output = new PrintStream(new FileOutputStream(new
File(outDir, outFileName)));
+ new ModelSerializer(serializationFormat, new
XStream()).write(workspace, output);
}
+ LibraryRepository repository = new LibraryRepository(outDir,
+ new ModelSerializer(XML, new XStream()));
for (Project project : workspace.getProjects()) {
- File projectDir = new File(new File(outDir,
project.getNamespace()), project.getName());
- projectDir.mkdirs();
- for (Library library : project.getLibraries()) {
- File outFile = new File(projectDir, library.name + ".xml");
- PrintStream stream = new PrintStream(new
FileOutputStream(outFile));
- new ModelSerializer(Output.XML, stream).dump(library);
- }
+ repository.save(project);
}
}
}
=======================================
--- /core/src/test/java/noop/graph/HelloWorldExampleTest.java Sat May 8
13:26:32 2010
+++ /core/src/test/java/noop/graph/HelloWorldExampleTest.java Sun Jun 27
15:35:07 2010
@@ -16,40 +16,42 @@
package noop.graph;
-import noop.graph.ModelSerializer.Output;
-import noop.model.Library;
-import noop.stdlib.StandardLibraryBuilder;
import org.junit.Before;
import org.junit.Test;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import noop.model.Library;
+import noop.persistence.ModelSerializer;
+import noop.persistence.ModelSerializer.SerializationFormat;
+import noop.stdlib.StandardLibraryBuilder;
+
/**
* @author alex...@google.com (Alex Eagle)
*/
public class HelloWorldExampleTest {
- Workspace workspace;
- StandardLibraryBuilder stdLib;
- Controller controller;
- private HelloWorldExample helloWorldExample;
+ private PrintStream out = new PrintStream(new ByteArrayOutputStream());
private Library library;
@Before
public void setUp() {
- workspace = new Workspace();
- stdLib = new StandardLibraryBuilder();
- controller = new Controller(workspace, new VertexCreatingVisitor());
+ Workspace workspace = new Workspace();
+ StandardLibraryBuilder stdLib = new StandardLibraryBuilder();
+ Controller controller = new Controller(workspace, new
VertexCreatingVisitor());
stdLib.build(controller);
- helloWorldExample = new HelloWorldExample(stdLib);
+ HelloWorldExample helloWorldExample = new HelloWorldExample(stdLib);
helloWorldExample.createProgram(controller);
library = workspace.lookupLibrary(helloWorldExample.uid);
}
@Test
public void shouldCreateHelloWorldDot() {
- new ModelSerializer(Output.DOT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.DOT, null).write(library, out);
}
@Test
public void shouldCreateHelloWorldOutline() {
- new ModelSerializer(Output.TXT, System.out).dump(library);
+ new ModelSerializer(SerializationFormat.TXT, null).write(library, out);
}
}
=======================================
--- /interpreter/src/main/java/noop/interpreter/Interpreter.java Sun Jun 27
13:24:51 2010
+++ /interpreter/src/main/java/noop/interpreter/Interpreter.java Sun Jun 27
15:35:07 2010
@@ -17,7 +17,10 @@
package noop.interpreter;
import com.google.inject.Inject;
-import com.thoughtworks.xstream.XStream;
+
+import java.io.FileNotFoundException;
+import java.util.UUID;
+
import noop.graph.Controller;
import noop.graph.ModelVisitor;
import noop.graph.VertexCreatingVisitor;
@@ -27,11 +30,7 @@
import noop.model.Library;
import noop.model.Project;
import noop.operations.NewProjectOperation;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.util.UUID;
+import noop.persistence.LibraryRepository;
/**
* @author alex...@google.com (Alex Eagle)
@@ -40,23 +39,27 @@
private final InterpreterOptions options;
private final ModelVisitor visitor;
private final Workspace workspace;
+ private final LibraryRepository repository;
@Inject
- public Interpreter(InterpreterOptions options, ModelVisitor visitor,
Workspace workspace) {
+ public Interpreter(InterpreterOptions options, ModelVisitor visitor,
Workspace workspace,
+ LibraryRepository repository) {
this.options = options;
this.visitor = visitor;
this.workspace = workspace;
+ this.repository = repository;
}
public int run() throws FileNotFoundException {
Controller controller = new Controller(workspace, new
VertexCreatingVisitor());
- Project project = new Project("runtime", "", "");
for (String libraryPath : options.getLibraryPaths()) {
- XStream xStream = new XStream();
- project.addLibrary((Library) xStream.fromXML(new FileReader(new
File(libraryPath))));
- }
- controller.addProject(new NewProjectOperation(project));
+ CommandLineLibraryNameParser parser = new
CommandLineLibraryNameParser(libraryPath).invoke();
+ Project project = parser.getProject();
+ Library library = repository.load(project, parser.getLibraryName());
+ project.addLibrary(library);
+ controller.addProject(new NewProjectOperation(project));
+ }
Library mainLib =
workspace.lookupLibrary(UUID.fromString(options.getMainLib()));
if (mainLib == null) {
=======================================
---
/interpreter/src/main/java/noop/interpreter/config/InterpreterModule.java
Sun Jun 27 13:24:51 2010
+++
/interpreter/src/main/java/noop/interpreter/config/InterpreterModule.java
Sun Jun 27 15:35:07 2010
@@ -18,11 +18,16 @@
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
+
+import java.io.File;
+import java.io.PrintStream;
+
import noop.graph.CompositeModelVisitor;
import noop.graph.ModelVisitor;
import noop.interpreter.InterpreterVisitor;
-
-import java.io.PrintStream;
+import noop.persistence.ModelSerializer.SerializationFormat;
+import static noop.persistence.ModelSerializer.SerializationFormat.XML;
+import noop.persistence.XmlDiskCache;
/**
* @author alex...@google.com (Alex Eagle)
@@ -40,6 +45,9 @@
bind(PrintStream.class).annotatedWith(Output.class).toInstance(System.out);
bind(PrintStream.class).annotatedWith(Error.class).toInstance(System.err);
bind(InterpreterOptions.class).toInstance(options);
+ bind(SerializationFormat.class).toInstance(XML);
+ bind(File.class).annotatedWith(XmlDiskCache.class)
+ .toInstance(new File(System.getProperty("java.io.tmpdir")));
bind(ModelVisitor.class).to(CompositeModelVisitor.class);
Multibinder<ModelVisitor> visitors =
Multibinder.newSetBinder(binder(), ModelVisitor.class);
visitors.addBinding().to(InterpreterVisitor.class);
=======================================
--- /interpreter/src/test/java/noop/interpreter/InterpreterSystemTest.java
Sun Jun 27 13:24:51 2010
+++ /interpreter/src/test/java/noop/interpreter/InterpreterSystemTest.java
Sun Jun 27 15:35:07 2010
@@ -16,27 +16,54 @@
package noop.interpreter;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
import org.junit.Before;
import org.junit.Test;
+import java.util.UUID;
+
+import noop.graph.Controller;
+import noop.interpreter.config.InterpreterModule;
+import noop.interpreter.config.OptionsBuilder;
+import noop.model.Function;
+import noop.model.Library;
+import noop.model.Project;
+import noop.operations.NewProjectOperation;
+import noop.persistence.LibraryRepository;
+import noop.stdlib.StandardLibraryBuilder;
import static org.junit.Assert.assertEquals;
/**
* @author alex...@google.com (Alex Eagle)
*/
public class InterpreterSystemTest {
+ private Controller controller;
+ private LibraryRepository repository;
+
@Before public void setUp() {
InterpreterMain.disableSystemExitForTesting();
+ Injector injector = Guice.createInjector(new InterpreterModule(new
OptionsBuilder().build()));
+ controller = injector.getInstance(Controller.class);
+ repository = injector.getInstance(LibraryRepository.class);
}
@Test public void shouldRunTheHelloWorldProgram() throws Exception {
+ UUID uuid = UUID.randomUUID();
+ Project project = new Project("Hello World", "com.example", "");
+ project.addLibrary(new Library(uuid, "hello")).addFunction(new
Function("go"));
+ controller.addProject(new NewProjectOperation(project));
+ repository.save(project);
+ StandardLibraryBuilder stdLib = new
StandardLibraryBuilder().build(controller);
+ repository.save(stdLib.noop);
+
InterpreterMain.main(new String[] {
- // TODO: fix absolute paths on my machine!
- "-lib", "/Users/alexeagle/IdeaProjects/noop/dumps/com.google.noop/Noop/io.xml",
- "-lib", "/Users/alexeagle/IdeaProjects/noop/dumps/com.google.noop/Noop/lang.xml",
- "-lib", "/Users/alexeagle/IdeaProjects/noop/dumps/com.example/Hello
World/hello.xml",
+ "-lib", "com.google.Noop/io",
+ "-lib", "com.google.Noop/lang",
+ "-lib", "com.example.Hello World/hello",
"-v",
- "dcf83cb3-9457-4695-b4b6-94389fef5a5b", "1"
+ uuid.toString(), "1"
});
assertEquals(0, InterpreterMain.exitCodeForTesting);
}
=======================================
--- /pom.xml Tue May 18 22:10:03 2010
+++ /pom.xml Sun Jun 27 15:35:07 2010
@@ -53,9 +53,9 @@
<dependencies>
<dependency>
- <groupId>com.google.collections</groupId>
- <artifactId>google-collections</artifactId>
- <version>0.9</version>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>r03</version>
</dependency>
<dependency>
<groupId>junit</groupId>