From b75f1faea8d0089ab7edf0a34ba8f03394e83c2a Mon Sep 17 00:00:00 2001 From: David Huynh Date: Wed, 3 Mar 2010 04:19:58 +0000 Subject: [PATCH] Changed tabs to spaces. No functionality change. git-svn-id: http://google-refine.googlecode.com/svn/trunk@174 7d457c2a-affb-35e4-300a-418c747d4874 --- .../metaweb/gridworks/GridworksServlet.java | 138 +-- .../com/metaweb/gridworks/Jsonizable.java | 2 +- .../com/metaweb/gridworks/ProjectManager.java | 420 +++++----- .../metaweb/gridworks/ProjectMetadata.java | 86 +- .../browsing/ConjunctiveFilteredRows.java | 142 ++-- .../gridworks/browsing/DecoratedValue.java | 30 +- .../metaweb/gridworks/browsing/Engine.java | 132 +-- .../gridworks/browsing/FilteredRows.java | 2 +- .../gridworks/browsing/RowVisitor.java | 2 +- .../facets/ExpressionNominalRowGrouper.java | 84 +- .../facets/ExpressionNumericRowBinner.java | 78 +- .../gridworks/browsing/facets/Facet.java | 10 +- .../gridworks/browsing/facets/ListFacet.java | 200 ++--- .../browsing/facets/NominalFacetChoice.java | 32 +- .../browsing/facets/NumericBinIndex.java | 162 ++-- .../gridworks/browsing/facets/RangeFacet.java | 278 +++--- .../browsing/facets/TextSearchFacet.java | 126 +-- .../filters/ExpressionEqualRowFilter.java | 90 +- .../ExpressionNumberComparisonRowFilter.java | 58 +- .../ExpressionStringComparisonRowFilter.java | 58 +- .../gridworks/browsing/filters/RowFilter.java | 2 +- .../metaweb/gridworks/commands/Command.java | 226 ++--- .../commands/EngineDependentCommand.java | 42 +- .../commands/edit/AddColumnCommand.java | 34 +- .../commands/edit/AnnotateOneRowCommand.java | 112 +-- .../commands/edit/AnnotateRowsCommand.java | 12 +- .../commands/edit/ApplyOperationsCommand.java | 90 +- .../commands/edit/CreateProjectCommand.java | 364 ++++---- .../commands/edit/DoTextTransformCommand.java | 18 +- .../commands/edit/FacetBasedEditCommand.java | 18 +- .../edit/JoinMultiValueCellsCommand.java | 42 +- .../commands/edit/RemoveColumnCommand.java | 40 +- .../commands/edit/SaveProtographCommand.java | 44 +- .../edit/SplitMultiValueCellsCommand.java | 42 +- .../commands/edit/UndoRedoCommand.java | 24 +- .../commands/info/ComputeFacetsCommand.java | 30 +- .../commands/info/ExportRowsCommand.java | 174 ++-- .../info/GetAllProjectMetadataCommand.java | 52 +- .../commands/info/GetHistoryCommand.java | 22 +- .../commands/info/GetModelsCommand.java | 52 +- .../commands/info/GetOperationsCommand.java | 62 +- .../commands/info/GetProcessesCommand.java | 24 +- .../info/GetProjectMetadataCommand.java | 24 +- .../commands/info/GetRowsCommand.java | 190 ++--- .../recon/ReconDiscardJudgmentsCommand.java | 16 +- .../recon/ReconJudgeOneCellCommand.java | 296 +++---- .../recon/ReconJudgeSimilarCellsCommand.java | 66 +- .../recon/ReconMarkNewTopicsCommand.java | 22 +- .../ReconMatchBestCandidatesCommand.java | 18 +- .../recon/ReconMatchSpecificTopicCommand.java | 32 +- .../commands/recon/ReconcileCommand.java | 26 +- .../commands/util/CancelProcessesCommand.java | 20 +- .../GetExpressionLanguageInfoCommand.java | 56 +- .../util/GuessTypesOfColumnCommand.java | 342 ++++---- .../util/PreviewExpressionCommand.java | 140 ++-- .../util/PreviewProtographCommand.java | 82 +- .../com/metaweb/gridworks/expr/Evaluable.java | 2 +- .../com/metaweb/gridworks/expr/HasFields.java | 2 +- .../metaweb/gridworks/expr/functions/Get.java | 154 ++-- .../gridworks/expr/functions/Length.java | 48 +- .../gridworks/expr/functions/Slice.java | 128 +-- .../gridworks/expr/functions/ToDate.java | 28 +- .../gridworks/expr/functions/ToNumber.java | 32 +- .../gridworks/expr/functions/ToString.java | 30 +- .../gridworks/expr/functions/arrays/Join.java | 68 +- .../expr/functions/arrays/Reverse.java | 50 +- .../gridworks/expr/functions/arrays/Sort.java | 48 +- .../expr/functions/booleans/And.java | 26 +- .../expr/functions/booleans/Not.java | 38 +- .../gridworks/expr/functions/booleans/Or.java | 36 +- .../gridworks/expr/functions/math/Ceil.java | 30 +- .../gridworks/expr/functions/math/Exp.java | 30 +- .../gridworks/expr/functions/math/Floor.java | 30 +- .../gridworks/expr/functions/math/Ln.java | 28 +- .../gridworks/expr/functions/math/Log.java | 28 +- .../gridworks/expr/functions/math/Max.java | 36 +- .../gridworks/expr/functions/math/Min.java | 36 +- .../gridworks/expr/functions/math/Mod.java | 38 +- .../gridworks/expr/functions/math/Round.java | 30 +- .../expr/functions/strings/Contains.java | 38 +- .../expr/functions/strings/EndsWith.java | 36 +- .../expr/functions/strings/Fingerprint.java | 46 +- .../expr/functions/strings/IndexOf.java | 36 +- .../expr/functions/strings/LastIndexOf.java | 38 +- .../gridworks/expr/functions/strings/MD5.java | 34 +- .../expr/functions/strings/Phonetic.java | 24 +- .../expr/functions/strings/Reinterpret.java | 24 +- .../expr/functions/strings/Replace.java | 28 +- .../expr/functions/strings/ReplaceChars.java | 42 +- .../expr/functions/strings/ReplaceRegexp.java | 24 +- .../expr/functions/strings/SHA1.java | 34 +- .../expr/functions/strings/Split.java | 38 +- .../functions/strings/SplitByCharType.java | 38 +- .../expr/functions/strings/StartsWith.java | 36 +- .../expr/functions/strings/ToLowercase.java | 32 +- .../expr/functions/strings/ToTitlecase.java | 60 +- .../expr/functions/strings/ToUppercase.java | 30 +- .../expr/functions/strings/Trim.java | 36 +- .../expr/functions/strings/Unicode.java | 42 +- .../expr/functions/strings/UnicodeType.java | 114 +-- .../com/metaweb/gridworks/gel/Control.java | 6 +- .../com/metaweb/gridworks/gel/Function.java | 2 +- .../com/metaweb/gridworks/gel/Parser.java | 448 +++++----- .../com/metaweb/gridworks/gel/Scanner.java | 448 +++++----- .../gridworks/gel/ast/ControlCallExpr.java | 48 +- .../gridworks/gel/ast/FieldAccessorExpr.java | 48 +- .../gridworks/gel/ast/FunctionCallExpr.java | 62 +- .../gridworks/gel/ast/LiteralExpr.java | 24 +- .../gridworks/gel/ast/OperatorCallExpr.java | 118 +-- .../gridworks/gel/ast/VariableExpr.java | 30 +- .../gridworks/gel/controls/ForEach.java | 32 +- .../gridworks/gel/controls/ForNonBlank.java | 24 +- .../metaweb/gridworks/gel/controls/If.java | 24 +- .../gridworks/gel/controls/IsTest.java | 32 +- .../metaweb/gridworks/gel/controls/With.java | 32 +- .../com/metaweb/gridworks/history/Change.java | 4 +- .../metaweb/gridworks/history/History.java | 236 +++--- .../gridworks/history/HistoryEntry.java | 242 +++--- .../gridworks/history/HistoryProcess.java | 90 +- .../gridworks/importers/ExcelImporter.java | 140 ++-- .../metaweb/gridworks/importers/Importer.java | 8 +- .../importers/ImporterUtilities.java | 134 +-- .../gridworks/importers/TsvCsvImporter.java | 140 ++-- .../gridworks/model/AbstractOperation.java | 30 +- .../com/metaweb/gridworks/model/Cell.java | 64 +- .../com/metaweb/gridworks/model/Column.java | 128 +-- .../metaweb/gridworks/model/ColumnGroup.java | 96 +-- .../metaweb/gridworks/model/ColumnModel.java | 222 ++--- .../com/metaweb/gridworks/model/Project.java | 290 +++---- .../com/metaweb/gridworks/model/Recon.java | 304 +++---- .../gridworks/model/ReconCandidate.java | 92 +- .../metaweb/gridworks/model/ReconStats.java | 52 +- .../java/com/metaweb/gridworks/model/Row.java | 216 ++--- .../gridworks/model/changes/CellChange.java | 38 +- .../model/changes/ColumnAdditionChange.java | 20 +- .../model/changes/MassCellChange.java | 118 +-- .../gridworks/model/changes/MassChange.java | 52 +- .../gridworks/model/changes/ReconChange.java | 18 +- .../model/recon/GuidBasedReconConfig.java | 262 +++--- .../model/recon/HeuristicReconConfig.java | 788 +++++++++--------- .../model/recon/IdBasedReconConfig.java | 270 +++--- .../model/recon/KeyBasedReconConfig.java | 292 +++---- .../gridworks/model/recon/ReconConfig.java | 24 +- .../gridworks/model/recon/ReconJob.java | 2 +- .../model/recon/StrictReconConfig.java | 12 +- .../operations/ColumnAdditionOperation.java | 138 +-- .../operations/ColumnRemovalOperation.java | 48 +- .../EngineDependentMassCellOperation.java | 74 +- .../operations/EngineDependentOperation.java | 48 +- .../operations/FacetBasedEditOperation.java | 214 ++--- .../MultiValuedCellJoinOperation.java | 156 ++-- .../MultiValuedCellSplitOperation.java | 190 ++--- .../ReconDiscardJudgmentsOperation.java | 122 +-- .../ReconJudgeSimilarCellsOperation.java | 274 +++--- .../ReconMarkNewTopicsOperation.java | 174 ++-- .../ReconMatchBestCandidatesOperation.java | 136 +-- .../ReconMatchSpecificTopicOperation.java | 148 ++-- .../gridworks/operations/ReconOperation.java | 368 ++++---- .../operations/RowStarOperation.java | 72 +- .../operations/SaveProtographOperation.java | 72 +- .../operations/TextTransformOperation.java | 110 +-- .../gridworks/process/LongRunningProcess.java | 110 +-- .../metaweb/gridworks/process/Process.java | 18 +- .../gridworks/process/ProcessManager.java | 150 ++-- .../process/QuickHistoryEntryProcess.java | 90 +- .../gridworks/protograph/AnonymousNode.java | 66 +- .../gridworks/protograph/CellKeyNode.java | 40 +- .../gridworks/protograph/CellNode.java | 16 +- .../gridworks/protograph/CellTopicNode.java | 88 +- .../gridworks/protograph/CellValueNode.java | 48 +- .../protograph/FreebaseProperty.java | 14 +- .../gridworks/protograph/FreebaseTopic.java | 36 +- .../protograph/FreebaseTopicNode.java | 66 +- .../gridworks/protograph/FreebaseType.java | 10 +- .../metaweb/gridworks/protograph/Link.java | 48 +- .../gridworks/protograph/NodeWithLinks.java | 10 +- .../gridworks/protograph/Protograph.java | 244 +++--- .../gridworks/protograph/ValueNode.java | 42 +- .../MqlreadLikeTransposedNodeFactory.java | 552 ++++++------ .../transpose/TransposedNodeFactory.java | 48 +- .../protograph/transpose/Transposer.java | 274 +++--- .../TripleLoaderTransposedNodeFactory.java | 594 ++++++------- .../metaweb/gridworks/util/IndentWriter.java | 6 +- .../com/metaweb/gridworks/util/JSObject.java | 66 +- .../gridworks/util/ParsingUtilities.java | 72 +- 185 files changed, 8803 insertions(+), 8803 deletions(-) diff --git a/src/main/java/com/metaweb/gridworks/GridworksServlet.java b/src/main/java/com/metaweb/gridworks/GridworksServlet.java index 263fcc5af..491fe8128 100644 --- a/src/main/java/com/metaweb/gridworks/GridworksServlet.java +++ b/src/main/java/com/metaweb/gridworks/GridworksServlet.java @@ -46,89 +46,89 @@ import com.metaweb.gridworks.commands.util.PreviewProtographCommand; public class GridworksServlet extends HttpServlet { - private static final long serialVersionUID = 2386057901503517403L; - - static protected Map _commands = new HashMap(); - - static { - _commands.put("create-project-from-upload", new CreateProjectCommand()); - _commands.put("export-rows", new ExportRowsCommand()); - - _commands.put("get-project-metadata", new GetProjectMetadataCommand()); - _commands.put("get-all-project-metadata", new GetAllProjectMetadataCommand()); - - _commands.put("get-models", new GetModelsCommand()); - _commands.put("get-rows", new GetRowsCommand()); - _commands.put("get-processes", new GetProcessesCommand()); - _commands.put("get-history", new GetHistoryCommand()); - _commands.put("get-operations", new GetOperationsCommand()); - - _commands.put("undo-redo", new UndoRedoCommand()); - _commands.put("apply-operations", new ApplyOperationsCommand()); - _commands.put("cancel-processes", new CancelProcessesCommand()); - - _commands.put("compute-facets", new ComputeFacetsCommand()); - _commands.put("do-text-transform", new DoTextTransformCommand()); - _commands.put("facet-based-edit", new FacetBasedEditCommand()); - - _commands.put("add-column", new AddColumnCommand()); - _commands.put("remove-column", new RemoveColumnCommand()); + private static final long serialVersionUID = 2386057901503517403L; + + static protected Map _commands = new HashMap(); + + static { + _commands.put("create-project-from-upload", new CreateProjectCommand()); + _commands.put("export-rows", new ExportRowsCommand()); + + _commands.put("get-project-metadata", new GetProjectMetadataCommand()); + _commands.put("get-all-project-metadata", new GetAllProjectMetadataCommand()); + + _commands.put("get-models", new GetModelsCommand()); + _commands.put("get-rows", new GetRowsCommand()); + _commands.put("get-processes", new GetProcessesCommand()); + _commands.put("get-history", new GetHistoryCommand()); + _commands.put("get-operations", new GetOperationsCommand()); + + _commands.put("undo-redo", new UndoRedoCommand()); + _commands.put("apply-operations", new ApplyOperationsCommand()); + _commands.put("cancel-processes", new CancelProcessesCommand()); + + _commands.put("compute-facets", new ComputeFacetsCommand()); + _commands.put("do-text-transform", new DoTextTransformCommand()); + _commands.put("facet-based-edit", new FacetBasedEditCommand()); + + _commands.put("add-column", new AddColumnCommand()); + _commands.put("remove-column", new RemoveColumnCommand()); _commands.put("join-multi-value-cells", new JoinMultiValueCellsCommand()); _commands.put("split-multi-value-cells", new SplitMultiValueCellsCommand()); - - _commands.put("reconcile", new ReconcileCommand()); - _commands.put("recon-match-best-candidates", new ReconMatchBestCandidatesCommand()); - _commands.put("recon-mark-new-topics", new ReconMarkNewTopicsCommand()); - _commands.put("recon-discard-judgments", new ReconDiscardJudgmentsCommand()); - _commands.put("recon-match-specific-topic-to-cells", new ReconMatchSpecificTopicCommand()); - _commands.put("recon-judge-one-cell", new ReconJudgeOneCellCommand()); + + _commands.put("reconcile", new ReconcileCommand()); + _commands.put("recon-match-best-candidates", new ReconMatchBestCandidatesCommand()); + _commands.put("recon-mark-new-topics", new ReconMarkNewTopicsCommand()); + _commands.put("recon-discard-judgments", new ReconDiscardJudgmentsCommand()); + _commands.put("recon-match-specific-topic-to-cells", new ReconMatchSpecificTopicCommand()); + _commands.put("recon-judge-one-cell", new ReconJudgeOneCellCommand()); _commands.put("recon-judge-similar-cells", new ReconJudgeSimilarCellsCommand()); - + _commands.put("annotate-one-row", new AnnotateOneRowCommand()); _commands.put("annotate-rows", new AnnotateRowsCommand()); - _commands.put("save-protograph", new SaveProtographCommand()); - - _commands.put("preview-expression", new PreviewExpressionCommand()); + _commands.put("save-protograph", new SaveProtographCommand()); + + _commands.put("preview-expression", new PreviewExpressionCommand()); _commands.put("get-expression-language-info", new GetExpressionLanguageInfoCommand()); _commands.put("preview-protograph", new PreviewProtographCommand()); _commands.put("guess-types-of-column", new GuessTypesOfColumnCommand()); - } + } - @Override - public void init() throws ServletException { - super.init(); - } - - @Override - public void destroy() { - if (ProjectManager.singleton != null) { - ProjectManager.singleton.saveAllProjects(); - ProjectManager.singleton.save(); - ProjectManager.singleton = null; - } - - super.destroy(); - } - + @Override + public void init() throws ServletException { + super.init(); + } + + @Override + public void destroy() { + if (ProjectManager.singleton != null) { + ProjectManager.singleton.saveAllProjects(); + ProjectManager.singleton.save(); + ProjectManager.singleton = null; + } + + super.destroy(); + } + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - ProjectManager.initialize(); - - String commandName = request.getPathInfo().substring(1); - Command command = _commands.get(commandName); - if (command != null) { - command.doPost(request, response); - } + ProjectManager.initialize(); + + String commandName = request.getPathInfo().substring(1); + Command command = _commands.get(commandName); + if (command != null) { + command.doPost(request, response); + } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - ProjectManager.initialize(); - - String commandName = request.getPathInfo().substring(1); - Command command = _commands.get(commandName); - if (command != null) { - command.doGet(request, response); - } + ProjectManager.initialize(); + + String commandName = request.getPathInfo().substring(1); + Command command = _commands.get(commandName); + if (command != null) { + command.doGet(request, response); + } } } diff --git a/src/main/java/com/metaweb/gridworks/Jsonizable.java b/src/main/java/com/metaweb/gridworks/Jsonizable.java index 8c983cd77..27b5286b5 100644 --- a/src/main/java/com/metaweb/gridworks/Jsonizable.java +++ b/src/main/java/com/metaweb/gridworks/Jsonizable.java @@ -6,5 +6,5 @@ import org.json.JSONException; import org.json.JSONWriter; public interface Jsonizable { - public void write(JSONWriter writer, Properties options) throws JSONException; + public void write(JSONWriter writer, Properties options) throws JSONException; } diff --git a/src/main/java/com/metaweb/gridworks/ProjectManager.java b/src/main/java/com/metaweb/gridworks/ProjectManager.java index 0e6980ffa..dbc141041 100644 --- a/src/main/java/com/metaweb/gridworks/ProjectManager.java +++ b/src/main/java/com/metaweb/gridworks/ProjectManager.java @@ -15,55 +15,55 @@ import com.codeberry.jdatapath.JDataPathSystem; import com.metaweb.gridworks.model.Project; public class ProjectManager implements Serializable { - + private static final long serialVersionUID = -2967415873336723962L; - - protected File _dir; - protected Map _projectsMetadata; - - transient protected Map _projects; - - static public ProjectManager singleton; - - static public void initialize() { - if (singleton == null) { - File dir = getProjectLocation(); - Gridworks.log("Using data directory: " + dir.getAbsolutePath()); - File file = new File(dir, "projects"); - if (file.exists()) { - singleton = load(file); - } else { - singleton = new ProjectManager(dir); - } - } - } - - static protected File getProjectLocation() { - String data_dir = Configurations.get("gridworks.data_dir"); - if (data_dir != null) { - return new File(data_dir); - } - - String os = Configurations.get("os.name").toLowerCase(); - if (os.contains("windows")) { - // NOTE(SM): finding the "local data app" in windows from java is actually a PITA - // see http://stackoverflow.com/questions/1198911/how-to-get-local-application-data-folder-in-java - // so we're using a library that uses JNI to ask directly the win32 APIs, - // it's not elegant but it's the safest bet - DataPath localDataPath = JDataPathSystem.getLocalSystem().getLocalDataPath("Gridworks"); + + protected File _dir; + protected Map _projectsMetadata; + + transient protected Map _projects; + + static public ProjectManager singleton; + + static public void initialize() { + if (singleton == null) { + File dir = getProjectLocation(); + Gridworks.log("Using data directory: " + dir.getAbsolutePath()); + File file = new File(dir, "projects"); + if (file.exists()) { + singleton = load(file); + } else { + singleton = new ProjectManager(dir); + } + } + } + + static protected File getProjectLocation() { + String data_dir = Configurations.get("gridworks.data_dir"); + if (data_dir != null) { + return new File(data_dir); + } + + String os = Configurations.get("os.name").toLowerCase(); + if (os.contains("windows")) { + // NOTE(SM): finding the "local data app" in windows from java is actually a PITA + // see http://stackoverflow.com/questions/1198911/how-to-get-local-application-data-folder-in-java + // so we're using a library that uses JNI to ask directly the win32 APIs, + // it's not elegant but it's the safest bet + DataPath localDataPath = JDataPathSystem.getLocalSystem().getLocalDataPath("Gridworks"); File data = new File(localDataPath.getPath()); data.mkdirs(); - return data; - } else if (os.contains("mac os x")) { - // on macosx, use "~/Library/Application Support" - String home = System.getProperty("user.home"); + return data; + } else if (os.contains("mac os x")) { + // on macosx, use "~/Library/Application Support" + String home = System.getProperty("user.home"); String data_home = (home != null) ? home + "/Library/Application Support/Gridworks" : ".gridworks"; File data = new File(data_home); data.mkdirs(); return data; - } else { // most likely a UNIX flavor - // start with the XDG environment - // see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + } else { // most likely a UNIX flavor + // start with the XDG environment + // see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html String data_home = System.getenv("XDG_DATA_HOME"); if (data_home == null) { // if not found, default back to ~/.local/share String home = System.getProperty("user.home"); @@ -73,172 +73,172 @@ public class ProjectManager implements Serializable { File data = new File(data_home + "/gridworks"); data.mkdirs(); return data; - } - } - - static protected ProjectManager load(File file) { - ProjectManager pm = null; - FileInputStream fis = null; - ObjectInputStream in = null; - try { - fis = new FileInputStream(file); - in = new ObjectInputStream(fis); - - pm = (ProjectManager) in.readObject(); - } catch(IOException e) { - e.printStackTrace(); - } catch(ClassNotFoundException e) { - e.printStackTrace(); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (Exception e) { - } - } - if (in != null) { - try { - in.close(); - } catch (Exception e) { - } - } - } - return pm; - } - - private ProjectManager(File dir) { - _dir = dir; - _dir.mkdirs(); - - _projectsMetadata = new HashMap(); - - internalInitialize(); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - internalInitialize(); - } - - private void internalInitialize() { - _projects = new HashMap(); - } - - public File getDataDir() { - return _dir; - } - - public void registerProject(Project project, ProjectMetadata projectMetadata) { - _projects.put(project.id, project); - _projectsMetadata.put(project.id, projectMetadata); - } - - public ProjectMetadata getProjectMetadata(long id) { - return _projectsMetadata.get(id); - } - - public Map getAllProjectMetadata() { - return _projectsMetadata; - } - - public Project getProject(long id) { - if (_projects.containsKey(id)) { - return _projects.get(id); - } else { - File file = new File(_dir, id + ".project"); - - Project project = null; - FileInputStream fis = null; - ObjectInputStream in = null; - try { - fis = new FileInputStream(file); - in = new ObjectInputStream(fis); - - project = (Project) in.readObject(); - } catch(IOException e) { - e.printStackTrace(); - } catch(ClassNotFoundException e) { - e.printStackTrace(); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (Exception e) { - } - } - if (in != null) { - try { - in.close(); - } catch (Exception e) { - } - } - } - - _projects.put(id, project); - - return project; - } - } - - public void save() { - File file = new File(_dir, "projects"); - - FileOutputStream fos = null; - ObjectOutputStream out = null; - try { - fos = new FileOutputStream(file); - out = new ObjectOutputStream(fos); - out.writeObject(this); - out.flush(); - } catch(IOException e) { - e.printStackTrace(); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (Exception e) { - } - } - if (out != null) { - try { - out.close(); - } catch (Exception e) { - } - } - } - } - - public void saveAllProjects() { - for (Project project : _projects.values()) { - saveProject(project); - } - } - - protected void saveProject(Project project) { - File file = new File(_dir, project.id + ".project"); - - FileOutputStream fos = null; - ObjectOutputStream out = null; - try { - fos = new FileOutputStream(file); - out = new ObjectOutputStream(fos); - out.writeObject(project); - out.flush(); - } catch(IOException e) { - e.printStackTrace(); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (Exception e) { - } - } - if (out != null) { - try { - out.close(); - } catch (Exception e) { - } - } - } - } + } + } + + static protected ProjectManager load(File file) { + ProjectManager pm = null; + FileInputStream fis = null; + ObjectInputStream in = null; + try { + fis = new FileInputStream(file); + in = new ObjectInputStream(fis); + + pm = (ProjectManager) in.readObject(); + } catch(IOException e) { + e.printStackTrace(); + } catch(ClassNotFoundException e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (Exception e) { + } + } + if (in != null) { + try { + in.close(); + } catch (Exception e) { + } + } + } + return pm; + } + + private ProjectManager(File dir) { + _dir = dir; + _dir.mkdirs(); + + _projectsMetadata = new HashMap(); + + internalInitialize(); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + internalInitialize(); + } + + private void internalInitialize() { + _projects = new HashMap(); + } + + public File getDataDir() { + return _dir; + } + + public void registerProject(Project project, ProjectMetadata projectMetadata) { + _projects.put(project.id, project); + _projectsMetadata.put(project.id, projectMetadata); + } + + public ProjectMetadata getProjectMetadata(long id) { + return _projectsMetadata.get(id); + } + + public Map getAllProjectMetadata() { + return _projectsMetadata; + } + + public Project getProject(long id) { + if (_projects.containsKey(id)) { + return _projects.get(id); + } else { + File file = new File(_dir, id + ".project"); + + Project project = null; + FileInputStream fis = null; + ObjectInputStream in = null; + try { + fis = new FileInputStream(file); + in = new ObjectInputStream(fis); + + project = (Project) in.readObject(); + } catch(IOException e) { + e.printStackTrace(); + } catch(ClassNotFoundException e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (Exception e) { + } + } + if (in != null) { + try { + in.close(); + } catch (Exception e) { + } + } + } + + _projects.put(id, project); + + return project; + } + } + + public void save() { + File file = new File(_dir, "projects"); + + FileOutputStream fos = null; + ObjectOutputStream out = null; + try { + fos = new FileOutputStream(file); + out = new ObjectOutputStream(fos); + out.writeObject(this); + out.flush(); + } catch(IOException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (Exception e) { + } + } + if (out != null) { + try { + out.close(); + } catch (Exception e) { + } + } + } + } + + public void saveAllProjects() { + for (Project project : _projects.values()) { + saveProject(project); + } + } + + protected void saveProject(Project project) { + File file = new File(_dir, project.id + ".project"); + + FileOutputStream fos = null; + ObjectOutputStream out = null; + try { + fos = new FileOutputStream(file); + out = new ObjectOutputStream(fos); + out.writeObject(project); + out.flush(); + } catch(IOException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (Exception e) { + } + } + if (out != null) { + try { + out.close(); + } catch (Exception e) { + } + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/ProjectMetadata.java b/src/main/java/com/metaweb/gridworks/ProjectMetadata.java index 7ff7c74b2..b38383b21 100644 --- a/src/main/java/com/metaweb/gridworks/ProjectMetadata.java +++ b/src/main/java/com/metaweb/gridworks/ProjectMetadata.java @@ -9,26 +9,26 @@ import org.json.JSONException; import org.json.JSONWriter; public class ProjectMetadata implements Serializable, Jsonizable { - private static final long serialVersionUID = 7959027046468240844L; - - private final Date _created = new Date(); - private String _name; - private String _password; - private String _encoding; - private int _encodingConfidence; - private Date _modified = new Date(); - - public Date getCreated() { - return _created; - } + private static final long serialVersionUID = 7959027046468240844L; + + private final Date _created = new Date(); + private String _name; + private String _password; + private String _encoding; + private int _encodingConfidence; + private Date _modified = new Date(); + + public Date getCreated() { + return _created; + } - public void setName(String name) { - this._name = name; - } + public void setName(String name) { + this._name = name; + } - public String getName() { - return _name; - } + public String getName() { + return _name; + } public void setEncoding(String encoding) { this._encoding = encoding; @@ -50,31 +50,31 @@ public class ProjectMetadata implements Serializable, Jsonizable { return _encodingConfidence; } - public void setPassword(String password) { - this._password = password; - } + public void setPassword(String password) { + this._password = password; + } - public String getPassword() { - return _password; - } - - public Date getModified() { - return _modified; - } - - public void updateModified() { - _modified = new Date(); - } + public String getPassword() { + return _password; + } + + public Date getModified() { + return _modified; + } + + public void updateModified() { + _modified = new Date(); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); - - writer.object(); - writer.key("name"); writer.value(getName()); - writer.key("created"); writer.value(sdf.format(getCreated())); - writer.key("modified"); writer.value(sdf.format(_modified)); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); + + writer.object(); + writer.key("name"); writer.value(getName()); + writer.key("created"); writer.value(sdf.format(getCreated())); + writer.key("modified"); writer.value(sdf.format(_modified)); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/ConjunctiveFilteredRows.java b/src/main/java/com/metaweb/gridworks/browsing/ConjunctiveFilteredRows.java index af566f8c0..c0a1d72f0 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/ConjunctiveFilteredRows.java +++ b/src/main/java/com/metaweb/gridworks/browsing/ConjunctiveFilteredRows.java @@ -8,75 +8,75 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class ConjunctiveFilteredRows implements FilteredRows { - final protected List _rowFilters = new LinkedList(); - final protected boolean _contextual; - - public ConjunctiveFilteredRows(boolean contextual) { - _contextual = contextual; - } - - public void add(RowFilter rowFilter) { - _rowFilters.add(rowFilter); - } - - public void accept(Project project, RowVisitor visitor) { - if (_contextual) { - contextualAccept(project, visitor); - } else { - simpleAccept(project, visitor); - } - } - - protected void simpleAccept(Project project, RowVisitor visitor) { - for (int i = 0; i < project.rows.size(); i++) { - Row row = project.rows.get(i); - - boolean ok = true; - for (RowFilter rowFilter : _rowFilters) { - if (!rowFilter.filterRow(project, i, row)) { - ok = false; - break; - } - } - - if (ok) { - visitor.visit(project, i, row, false); - } - } - } - - protected void contextualAccept(Project project, RowVisitor visitor) { - int lastVisitedRow = -1; - - for (int i = 0; i < project.rows.size(); i++) { - Row row = project.rows.get(i); - - boolean ok = true; - for (RowFilter rowFilter : _rowFilters) { - if (!rowFilter.filterRow(project, i, row)) { - ok = false; - break; - } - } - - if (ok) { - if (row.contextRows != null && lastVisitedRow < i - 1) { - for (int contextRowIndex : row.contextRows) { - if (contextRowIndex > lastVisitedRow) { - visitor.visit( - project, - contextRowIndex, - project.rows.get(contextRowIndex), - true - ); - lastVisitedRow = contextRowIndex; - } - } - } - - visitor.visit(project, i, row, false); - lastVisitedRow = i; - } - } - } + final protected List _rowFilters = new LinkedList(); + final protected boolean _contextual; + + public ConjunctiveFilteredRows(boolean contextual) { + _contextual = contextual; + } + + public void add(RowFilter rowFilter) { + _rowFilters.add(rowFilter); + } + + public void accept(Project project, RowVisitor visitor) { + if (_contextual) { + contextualAccept(project, visitor); + } else { + simpleAccept(project, visitor); + } + } + + protected void simpleAccept(Project project, RowVisitor visitor) { + for (int i = 0; i < project.rows.size(); i++) { + Row row = project.rows.get(i); + + boolean ok = true; + for (RowFilter rowFilter : _rowFilters) { + if (!rowFilter.filterRow(project, i, row)) { + ok = false; + break; + } + } + + if (ok) { + visitor.visit(project, i, row, false); + } + } + } + + protected void contextualAccept(Project project, RowVisitor visitor) { + int lastVisitedRow = -1; + + for (int i = 0; i < project.rows.size(); i++) { + Row row = project.rows.get(i); + + boolean ok = true; + for (RowFilter rowFilter : _rowFilters) { + if (!rowFilter.filterRow(project, i, row)) { + ok = false; + break; + } + } + + if (ok) { + if (row.contextRows != null && lastVisitedRow < i - 1) { + for (int contextRowIndex : row.contextRows) { + if (contextRowIndex > lastVisitedRow) { + visitor.visit( + project, + contextRowIndex, + project.rows.get(contextRowIndex), + true + ); + lastVisitedRow = contextRowIndex; + } + } + } + + visitor.visit(project, i, row, false); + lastVisitedRow = i; + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/DecoratedValue.java b/src/main/java/com/metaweb/gridworks/browsing/DecoratedValue.java index 9a40fff8a..9c2f5d25e 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/DecoratedValue.java +++ b/src/main/java/com/metaweb/gridworks/browsing/DecoratedValue.java @@ -8,19 +8,19 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class DecoratedValue implements Jsonizable { - final public Object value; - final public String label; - - public DecoratedValue(Object value, String label) { - this.value = value; - this.label = label; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - writer.object(); - writer.key("v"); writer.value(value); - writer.key("l"); writer.value(label); - writer.endObject(); - } + final public Object value; + final public String label; + + public DecoratedValue(Object value, String label) { + this.value = value; + this.label = label; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("v"); writer.value(value); + writer.key("l"); writer.value(label); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/Engine.java b/src/main/java/com/metaweb/gridworks/browsing/Engine.java index 0ba2f4ad0..f3de287f5 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/Engine.java +++ b/src/main/java/com/metaweb/gridworks/browsing/Engine.java @@ -18,71 +18,71 @@ import com.metaweb.gridworks.browsing.filters.RowFilter; import com.metaweb.gridworks.model.Project; public class Engine implements Jsonizable { - protected Project _project; - protected List _facets = new LinkedList(); - - public Engine(Project project) { - _project = project; - } - - public FilteredRows getAllFilteredRows(boolean contextual) { - return getFilteredRows(null, contextual); - } + protected Project _project; + protected List _facets = new LinkedList(); + + public Engine(Project project) { + _project = project; + } + + public FilteredRows getAllFilteredRows(boolean contextual) { + return getFilteredRows(null, contextual); + } - public FilteredRows getFilteredRows(Facet except, boolean contextual) { - ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows(contextual); - for (Facet facet : _facets) { - if (facet != except) { - RowFilter rowFilter = facet.getRowFilter(); - if (rowFilter != null) { - cfr.add(rowFilter); - } - } - } - return cfr; - } - - public void initializeFromJSON(JSONObject o) throws Exception { - JSONArray a = o.getJSONArray("facets"); - int length = a.length(); - - for (int i = 0; i < length; i++) { - JSONObject fo = a.getJSONObject(i); - String type = fo.has("type") ? fo.getString("type") : "list"; - - Facet facet = null; - if ("list".equals(type)) { - facet = new ListFacet(); - } else if ("range".equals(type)) { - facet = new RangeFacet(); - } else if ("text".equals(type)) { - facet = new TextSearchFacet(); - } - - if (facet != null) { - facet.initializeFromJSON(_project, fo); - _facets.add(facet); - } - } - } - - public void computeFacets() throws JSONException { - for (Facet facet : _facets) { - FilteredRows filteredRows = getFilteredRows(facet, false); - - facet.computeChoices(_project, filteredRows); - } - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("facets"); writer.array(); - for (Facet facet : _facets) { - facet.write(writer, options); - } - writer.endArray(); - writer.endObject(); - } + public FilteredRows getFilteredRows(Facet except, boolean contextual) { + ConjunctiveFilteredRows cfr = new ConjunctiveFilteredRows(contextual); + for (Facet facet : _facets) { + if (facet != except) { + RowFilter rowFilter = facet.getRowFilter(); + if (rowFilter != null) { + cfr.add(rowFilter); + } + } + } + return cfr; + } + + public void initializeFromJSON(JSONObject o) throws Exception { + JSONArray a = o.getJSONArray("facets"); + int length = a.length(); + + for (int i = 0; i < length; i++) { + JSONObject fo = a.getJSONObject(i); + String type = fo.has("type") ? fo.getString("type") : "list"; + + Facet facet = null; + if ("list".equals(type)) { + facet = new ListFacet(); + } else if ("range".equals(type)) { + facet = new RangeFacet(); + } else if ("text".equals(type)) { + facet = new TextSearchFacet(); + } + + if (facet != null) { + facet.initializeFromJSON(_project, fo); + _facets.add(facet); + } + } + } + + public void computeFacets() throws JSONException { + for (Facet facet : _facets) { + FilteredRows filteredRows = getFilteredRows(facet, false); + + facet.computeChoices(_project, filteredRows); + } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("facets"); writer.array(); + for (Facet facet : _facets) { + facet.write(writer, options); + } + writer.endArray(); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/FilteredRows.java b/src/main/java/com/metaweb/gridworks/browsing/FilteredRows.java index aa7f6a882..cffd1d292 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/FilteredRows.java +++ b/src/main/java/com/metaweb/gridworks/browsing/FilteredRows.java @@ -3,5 +3,5 @@ package com.metaweb.gridworks.browsing; import com.metaweb.gridworks.model.Project; public interface FilteredRows { - public void accept(Project project, RowVisitor visitor); + public void accept(Project project, RowVisitor visitor); } diff --git a/src/main/java/com/metaweb/gridworks/browsing/RowVisitor.java b/src/main/java/com/metaweb/gridworks/browsing/RowVisitor.java index 146bab9b7..80b3263c5 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/RowVisitor.java +++ b/src/main/java/com/metaweb/gridworks/browsing/RowVisitor.java @@ -4,6 +4,6 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public interface RowVisitor { - public boolean visit(Project project, int rowIndex, Row row, boolean contextual); + public boolean visit(Project project, int rowIndex, Row row, boolean contextual); } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNominalRowGrouper.java b/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNominalRowGrouper.java index 9d345b355..0a8469e4c 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNominalRowGrouper.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNominalRowGrouper.java @@ -13,55 +13,55 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class ExpressionNominalRowGrouper implements RowVisitor { - final protected Evaluable _evaluable; - final protected int _cellIndex; - - final public Map choices = new HashMap(); - public int blankCount = 0; - public int errorCount = 0; - - public ExpressionNominalRowGrouper(Evaluable evaluable, int cellIndex) { - _evaluable = evaluable; - _cellIndex = cellIndex; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - Cell cell = row.getCell(_cellIndex); + final protected Evaluable _evaluable; + final protected int _cellIndex; + + final public Map choices = new HashMap(); + public int blankCount = 0; + public int errorCount = 0; + + public ExpressionNominalRowGrouper(Evaluable evaluable, int cellIndex) { + _evaluable = evaluable; + _cellIndex = cellIndex; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + Cell cell = row.getCell(_cellIndex); Properties bindings = ExpressionUtils.createBindings(project); ExpressionUtils.bind(bindings, row, rowIndex, cell); - - Object value = _evaluable.evaluate(bindings); - if (value != null && value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - processValue(v); - } - } else { - processValue(value); - } - return false; - } - - protected void processValue(Object value) { + + Object value = _evaluable.evaluate(bindings); + if (value != null && value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + processValue(v); + } + } else { + processValue(value); + } + return false; + } + + protected void processValue(Object value) { if (ExpressionUtils.isError(value)) { errorCount++; } else if (ExpressionUtils.isNonBlankData(value)) { - String valueString = value.toString(); - String label = value.toString(); - - DecoratedValue dValue = new DecoratedValue(value, label); - - if (choices.containsKey(valueString)) { - choices.get(valueString).count++; - } else { - NominalFacetChoice choice = new NominalFacetChoice(dValue); - choice.count = 1; - - choices.put(valueString, choice); - } + String valueString = value.toString(); + String label = value.toString(); + + DecoratedValue dValue = new DecoratedValue(value, label); + + if (choices.containsKey(valueString)) { + choices.get(valueString).count++; + } else { + NominalFacetChoice choice = new NominalFacetChoice(dValue); + choice.count = 1; + + choices.put(valueString, choice); + } } else { blankCount++; } - } + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNumericRowBinner.java b/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNumericRowBinner.java index e43f9cc12..7e4e9bee8 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNumericRowBinner.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/ExpressionNumericRowBinner.java @@ -10,46 +10,46 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class ExpressionNumericRowBinner implements RowVisitor { - final protected Evaluable _evaluable; - final protected int _cellIndex; - final protected NumericBinIndex _index; - - final public int[] bins; - - public ExpressionNumericRowBinner(Evaluable evaluable, int cellIndex, NumericBinIndex index) { - _evaluable = evaluable; - _cellIndex = cellIndex; - _index = index; - bins = new int[_index.getBins().length]; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - Cell cell = row.getCell(_cellIndex); + final protected Evaluable _evaluable; + final protected int _cellIndex; + final protected NumericBinIndex _index; + + final public int[] bins; + + public ExpressionNumericRowBinner(Evaluable evaluable, int cellIndex, NumericBinIndex index) { + _evaluable = evaluable; + _cellIndex = cellIndex; + _index = index; + bins = new int[_index.getBins().length]; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + Cell cell = row.getCell(_cellIndex); Properties bindings = ExpressionUtils.createBindings(project); ExpressionUtils.bind(bindings, row, rowIndex, cell); - - Object value = _evaluable.evaluate(bindings); - if (value != null) { - if (value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - processValue(v); - } - } else { - processValue(value); - } - } - return false; - } - - protected void processValue(Object value) { - if (value instanceof Number) { - double d = ((Number) value).doubleValue(); - - int bin = (int) Math.round((d - _index.getMin()) / _index.getStep()); - - bins[bin]++; - } - } + + Object value = _evaluable.evaluate(bindings); + if (value != null) { + if (value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + processValue(v); + } + } else { + processValue(value); + } + } + return false; + } + + protected void processValue(Object value) { + if (value instanceof Number) { + double d = ((Number) value).doubleValue(); + + int bin = (int) Math.round((d - _index.getMin()) / _index.getStep()); + + bins[bin]++; + } + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/Facet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/Facet.java index af8385424..7dc6d75d0 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/Facet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/Facet.java @@ -8,9 +8,9 @@ import com.metaweb.gridworks.browsing.filters.RowFilter; import com.metaweb.gridworks.model.Project; public interface Facet extends Jsonizable { - public RowFilter getRowFilter(); - - public void computeChoices(Project project, FilteredRows filteredRows); - - public void initializeFromJSON(Project project, JSONObject o) throws Exception; + public RowFilter getRowFilter(); + + public void computeChoices(Project project, FilteredRows filteredRows); + + public void initializeFromJSON(Project project, JSONObject o) throws Exception; } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java index 7d467d86b..b46c29373 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/ListFacet.java @@ -18,45 +18,45 @@ import com.metaweb.gridworks.expr.MetaParser; import com.metaweb.gridworks.model.Project; public class ListFacet implements Facet { - protected List _selection = new LinkedList(); - protected boolean _selectBlank; - protected boolean _selectError; - - protected String _name; - protected String _expression; - protected String _columnName; - protected int _cellIndex; - protected Evaluable _eval; - - // computed + protected List _selection = new LinkedList(); + protected boolean _selectBlank; + protected boolean _selectError; + + protected String _name; + protected String _expression; + protected String _columnName; + protected int _cellIndex; + protected Evaluable _eval; + + // computed protected List _choices = new LinkedList(); protected int _blankCount; protected int _errorCount; - public ListFacet() { - } + public ListFacet() { + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("name"); writer.value(_name); - writer.key("expression"); writer.value(_expression); - writer.key("columnName"); writer.value(_columnName); - - writer.key("choices"); writer.array(); - for (NominalFacetChoice choice : _choices) { - choice.write(writer, options); - } - writer.endArray(); - - if (_selectBlank || _blankCount > 0) { - writer.key("blankChoice"); - writer.object(); - writer.key("s"); writer.value(_selectBlank); - writer.key("c"); writer.value(_blankCount); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("name"); writer.value(_name); + writer.key("expression"); writer.value(_expression); + writer.key("columnName"); writer.value(_columnName); + + writer.key("choices"); writer.array(); + for (NominalFacetChoice choice : _choices) { + choice.write(writer, options); + } + writer.endArray(); + + if (_selectBlank || _blankCount > 0) { + writer.key("blankChoice"); + writer.object(); + writer.key("s"); writer.value(_selectBlank); + writer.key("c"); writer.value(_blankCount); + writer.endObject(); + } if (_selectError || _errorCount > 0) { writer.key("errorChoice"); writer.object(); @@ -64,76 +64,76 @@ public class ListFacet implements Facet { writer.key("c"); writer.value(_errorCount); writer.endObject(); } - - writer.endObject(); - } + + writer.endObject(); + } - public void initializeFromJSON(Project project, JSONObject o) throws Exception { - _name = o.getString("name"); - _expression = o.getString("expression"); - _columnName = o.getString("columnName"); - _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); - - _eval = MetaParser.parse(_expression); - _selection.clear(); - - JSONArray a = o.getJSONArray("selection"); - int length = a.length(); - - for (int i = 0; i < length; i++) { - JSONObject oc = a.getJSONObject(i); - JSONObject ocv = oc.getJSONObject("v"); - - DecoratedValue decoratedValue = new DecoratedValue( - ocv.get("v"), ocv.getString("l")); - - NominalFacetChoice nominalFacetChoice = new NominalFacetChoice(decoratedValue); - nominalFacetChoice.selected = true; - - _selection.add(nominalFacetChoice); - } - - if (o.has("selectBlank")) { - _selectBlank = o.getBoolean("selectBlank"); - } + public void initializeFromJSON(Project project, JSONObject o) throws Exception { + _name = o.getString("name"); + _expression = o.getString("expression"); + _columnName = o.getString("columnName"); + _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); + + _eval = MetaParser.parse(_expression); + _selection.clear(); + + JSONArray a = o.getJSONArray("selection"); + int length = a.length(); + + for (int i = 0; i < length; i++) { + JSONObject oc = a.getJSONObject(i); + JSONObject ocv = oc.getJSONObject("v"); + + DecoratedValue decoratedValue = new DecoratedValue( + ocv.get("v"), ocv.getString("l")); + + NominalFacetChoice nominalFacetChoice = new NominalFacetChoice(decoratedValue); + nominalFacetChoice.selected = true; + + _selection.add(nominalFacetChoice); + } + + if (o.has("selectBlank")) { + _selectBlank = o.getBoolean("selectBlank"); + } if (o.has("selectError")) { _selectError = o.getBoolean("selectError"); } - } + } - public RowFilter getRowFilter() { - return _selection.size() == 0 && !_selectBlank && !_selectError ? null : - new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches(), _selectBlank, _selectError); - } + public RowFilter getRowFilter() { + return _selection.size() == 0 && !_selectBlank && !_selectError ? null : + new ExpressionEqualRowFilter(_eval, _cellIndex, createMatches(), _selectBlank, _selectError); + } - public void computeChoices(Project project, FilteredRows filteredRows) { - ExpressionNominalRowGrouper grouper = - new ExpressionNominalRowGrouper(_eval, _cellIndex); - - filteredRows.accept(project, grouper); - - _choices.clear(); - _choices.addAll(grouper.choices.values()); - - for (NominalFacetChoice choice : _selection) { - String valueString = choice.decoratedValue.value.toString(); - if (grouper.choices.containsKey(valueString)) { - grouper.choices.get(valueString).selected = true; - } else { - choice.count = 0; - _choices.add(choice); - } - } - - _blankCount = grouper.blankCount; - _errorCount = grouper.errorCount; - } - - protected Object[] createMatches() { - Object[] a = new Object[_selection.size()]; - for (int i = 0; i < a.length; i++) { - a[i] = _selection.get(i).decoratedValue.value; - } - return a; - } + public void computeChoices(Project project, FilteredRows filteredRows) { + ExpressionNominalRowGrouper grouper = + new ExpressionNominalRowGrouper(_eval, _cellIndex); + + filteredRows.accept(project, grouper); + + _choices.clear(); + _choices.addAll(grouper.choices.values()); + + for (NominalFacetChoice choice : _selection) { + String valueString = choice.decoratedValue.value.toString(); + if (grouper.choices.containsKey(valueString)) { + grouper.choices.get(valueString).selected = true; + } else { + choice.count = 0; + _choices.add(choice); + } + } + + _blankCount = grouper.blankCount; + _errorCount = grouper.errorCount; + } + + protected Object[] createMatches() { + Object[] a = new Object[_selection.size()]; + for (int i = 0; i < a.length; i++) { + a[i] = _selection.get(i).decoratedValue.value; + } + return a; + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/NominalFacetChoice.java b/src/main/java/com/metaweb/gridworks/browsing/facets/NominalFacetChoice.java index 7e9ea962c..dcdf88adf 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/NominalFacetChoice.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/NominalFacetChoice.java @@ -9,20 +9,20 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.browsing.DecoratedValue; public class NominalFacetChoice implements Jsonizable { - final public DecoratedValue decoratedValue; - public int count; - public boolean selected; - - public NominalFacetChoice(DecoratedValue decoratedValue) { - this.decoratedValue = decoratedValue; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - writer.object(); - writer.key("v"); decoratedValue.write(writer, options); - writer.key("c"); writer.value(count); - writer.key("s"); writer.value(selected); - writer.endObject(); - } + final public DecoratedValue decoratedValue; + public int count; + public boolean selected; + + public NominalFacetChoice(DecoratedValue decoratedValue) { + this.decoratedValue = decoratedValue; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("v"); decoratedValue.write(writer, options); + writer.key("c"); writer.value(count); + writer.key("s"); writer.value(selected); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/NumericBinIndex.java b/src/main/java/com/metaweb/gridworks/browsing/facets/NumericBinIndex.java index 1aa2ab595..ad953e438 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/NumericBinIndex.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/NumericBinIndex.java @@ -11,92 +11,92 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class NumericBinIndex { - private double _min; - private double _max; - private double _step; - private int[] _bins; - - public NumericBinIndex(Project project, int cellIndex, Evaluable eval) { + private double _min; + private double _max; + private double _step; + private int[] _bins; + + public NumericBinIndex(Project project, int cellIndex, Evaluable eval) { Properties bindings = ExpressionUtils.createBindings(project); - - _min = Double.POSITIVE_INFINITY; - _max = Double.NEGATIVE_INFINITY; - - List allValues = new ArrayList(); - for (int i = 0; i < project.rows.size(); i++) { - Row row = project.rows.get(i); - Cell cell = row.getCell(cellIndex); + + _min = Double.POSITIVE_INFINITY; + _max = Double.NEGATIVE_INFINITY; + + List allValues = new ArrayList(); + for (int i = 0; i < project.rows.size(); i++) { + Row row = project.rows.get(i); + Cell cell = row.getCell(cellIndex); ExpressionUtils.bind(bindings, row, i, cell); - - Object value = eval.evaluate(bindings); - if (value != null) { - if (value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - if (v instanceof Number) { - processValue(((Number) v).doubleValue(), allValues); - } - } - } else if (value instanceof Number) { - processValue(((Number) value).doubleValue(), allValues); - } - } - } - - if (_min >= _max) { - _step = 0; - _bins = new int[1]; - return; - } - - double diff = getMax() - getMin(); - _step = 1; - if (diff > 10) { - while (getStep() * 100 < diff) { - _step *= 10; - } - } else { - while (getStep() * 100 > diff) { - _step /= 10; - } - } - - _min = (Math.floor(_min / _step) * _step); - _max = (Math.ceil(_max / _step) * _step); - - int binCount = 1 + (int) Math.ceil((getMax() - getMin()) / getStep()); - if (binCount > 100) { - _step *= 2; - binCount = Math.round((1 + binCount) / 2); - } - - _bins = new int[binCount]; - for (double d : allValues) { - int bin = (int) Math.round((d - _min) / _step); - _bins[bin]++; - } - } - - public double getMin() { - return _min; - } + + Object value = eval.evaluate(bindings); + if (value != null) { + if (value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + if (v instanceof Number) { + processValue(((Number) v).doubleValue(), allValues); + } + } + } else if (value instanceof Number) { + processValue(((Number) value).doubleValue(), allValues); + } + } + } + + if (_min >= _max) { + _step = 0; + _bins = new int[1]; + return; + } + + double diff = getMax() - getMin(); + _step = 1; + if (diff > 10) { + while (getStep() * 100 < diff) { + _step *= 10; + } + } else { + while (getStep() * 100 > diff) { + _step /= 10; + } + } + + _min = (Math.floor(_min / _step) * _step); + _max = (Math.ceil(_max / _step) * _step); + + int binCount = 1 + (int) Math.ceil((getMax() - getMin()) / getStep()); + if (binCount > 100) { + _step *= 2; + binCount = Math.round((1 + binCount) / 2); + } + + _bins = new int[binCount]; + for (double d : allValues) { + int bin = (int) Math.round((d - _min) / _step); + _bins[bin]++; + } + } + + public double getMin() { + return _min; + } - public double getMax() { - return _max; - } + public double getMax() { + return _max; + } - public double getStep() { - return _step; - } + public double getStep() { + return _step; + } - public int[] getBins() { - return _bins; - } + public int[] getBins() { + return _bins; + } - protected void processValue(double v, List allValues) { - _min = Math.min(getMin(), v); - _max = Math.max(getMax(), v); - allValues.add(v); - } + protected void processValue(double v, List allValues) { + _min = Math.min(getMin(), v); + _max = Math.max(getMax(), v); + allValues.add(v); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java index e7c9cb92e..2921530f6 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/RangeFacet.java @@ -15,147 +15,147 @@ import com.metaweb.gridworks.model.Column; import com.metaweb.gridworks.model.Project; public class RangeFacet implements Facet { - protected String _name; - protected String _expression; - protected String _columnName; - protected int _cellIndex; - protected Evaluable _eval; - - protected String _mode; - protected double _min; - protected double _max; - protected double _step; - protected int[] _baseBins; - protected int[] _bins; - - protected double _from; - protected double _to; - protected boolean _selected; - - public RangeFacet() { - } + protected String _name; + protected String _expression; + protected String _columnName; + protected int _cellIndex; + protected Evaluable _eval; + + protected String _mode; + protected double _min; + protected double _max; + protected double _step; + protected int[] _baseBins; + protected int[] _bins; + + protected double _from; + protected double _to; + protected boolean _selected; + + public RangeFacet() { + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("name"); writer.value(_name); - writer.key("expression"); writer.value(_expression); - writer.key("columnName"); writer.value(_columnName); - writer.key("mode"); writer.value(_mode); - - if (!Double.isInfinite(_min) && !Double.isInfinite(_max)) { - writer.key("min"); writer.value(_min); - writer.key("max"); writer.value(_max); - writer.key("step"); writer.value(_step); - - writer.key("bins"); writer.array(); - for (int b : _bins) { - writer.value(b); - } - writer.endArray(); - - writer.key("baseBins"); writer.array(); - for (int b : _baseBins) { - writer.value(b); - } - writer.endArray(); - - if ("min".equals(_mode)) { - writer.key("from"); writer.value(_from); - } else if ("max".equals(_mode)) { - writer.key("to"); writer.value(_to); - } else { - writer.key("from"); writer.value(_from); - writer.key("to"); writer.value(_to); - } - } - - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("name"); writer.value(_name); + writer.key("expression"); writer.value(_expression); + writer.key("columnName"); writer.value(_columnName); + writer.key("mode"); writer.value(_mode); + + if (!Double.isInfinite(_min) && !Double.isInfinite(_max)) { + writer.key("min"); writer.value(_min); + writer.key("max"); writer.value(_max); + writer.key("step"); writer.value(_step); + + writer.key("bins"); writer.array(); + for (int b : _bins) { + writer.value(b); + } + writer.endArray(); + + writer.key("baseBins"); writer.array(); + for (int b : _baseBins) { + writer.value(b); + } + writer.endArray(); + + if ("min".equals(_mode)) { + writer.key("from"); writer.value(_from); + } else if ("max".equals(_mode)) { + writer.key("to"); writer.value(_to); + } else { + writer.key("from"); writer.value(_from); + writer.key("to"); writer.value(_to); + } + } + + writer.endObject(); + } - public void initializeFromJSON(Project project, JSONObject o) throws Exception { - _name = o.getString("name"); - _expression = o.getString("expression"); - _columnName = o.getString("columnName"); - _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); - - _eval = MetaParser.parse(_expression); - - _mode = o.getString("mode"); - if ("min".equals(_mode)) { - if (o.has("from")) { - _from = o.getDouble("from"); - _selected = true; - } - } else if ("max".equals(_mode)) { - if (o.has("to")) { - _to = o.getDouble("to"); - _selected = true; - } - } else { - if (o.has("from") && o.has("to")) { - _from = o.getDouble("from"); - _to = o.getDouble("to"); - _selected = true; - } - } - } + public void initializeFromJSON(Project project, JSONObject o) throws Exception { + _name = o.getString("name"); + _expression = o.getString("expression"); + _columnName = o.getString("columnName"); + _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); + + _eval = MetaParser.parse(_expression); + + _mode = o.getString("mode"); + if ("min".equals(_mode)) { + if (o.has("from")) { + _from = o.getDouble("from"); + _selected = true; + } + } else if ("max".equals(_mode)) { + if (o.has("to")) { + _to = o.getDouble("to"); + _selected = true; + } + } else { + if (o.has("from") && o.has("to")) { + _from = o.getDouble("from"); + _to = o.getDouble("to"); + _selected = true; + } + } + } - public RowFilter getRowFilter() { - if (_selected) { - if ("min".equals(_mode)) { - return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { - protected boolean checkValue(double d) { - return d >= _from; - }; - }; - } else if ("max".equals(_mode)) { - return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { - protected boolean checkValue(double d) { - return d <= _to; - }; - }; - } else { - return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { - protected boolean checkValue(double d) { - return d >= _from && d <= _to; - }; - }; - } - } else { - return null; - } - } + public RowFilter getRowFilter() { + if (_selected) { + if ("min".equals(_mode)) { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { + protected boolean checkValue(double d) { + return d >= _from; + }; + }; + } else if ("max".equals(_mode)) { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { + protected boolean checkValue(double d) { + return d <= _to; + }; + }; + } else { + return new ExpressionNumberComparisonRowFilter(_eval, _cellIndex) { + protected boolean checkValue(double d) { + return d >= _from && d <= _to; + }; + }; + } + } else { + return null; + } + } - public void computeChoices(Project project, FilteredRows filteredRows) { - Column column = project.columnModel.getColumnByCellIndex(_cellIndex); - - String key = "numeric-bin:" + _expression; - NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key); - if (index == null) { - index = new NumericBinIndex(project, _cellIndex, _eval); - column.setPrecompute(key, index); - } - - _min = index.getMin(); - _max = index.getMax(); - _step = index.getStep(); - _baseBins = index.getBins(); - - if (_selected) { - _from = Math.max(_from, _min); - _to = Math.min(_to, _max); - } else { - _from = _min; - _to = _max; - } - - ExpressionNumericRowBinner binner = - new ExpressionNumericRowBinner(_eval, _cellIndex, index); - - filteredRows.accept(project, binner); - - _bins = binner.bins; - } + public void computeChoices(Project project, FilteredRows filteredRows) { + Column column = project.columnModel.getColumnByCellIndex(_cellIndex); + + String key = "numeric-bin:" + _expression; + NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key); + if (index == null) { + index = new NumericBinIndex(project, _cellIndex, _eval); + column.setPrecompute(key, index); + } + + _min = index.getMin(); + _max = index.getMax(); + _step = index.getStep(); + _baseBins = index.getBins(); + + if (_selected) { + _from = Math.max(_from, _min); + _to = Math.min(_to, _max); + } else { + _from = _min; + _to = _max; + } + + ExpressionNumericRowBinner binner = + new ExpressionNumericRowBinner(_eval, _cellIndex, index); + + filteredRows.accept(project, binner); + + _bins = binner.bins; + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/facets/TextSearchFacet.java b/src/main/java/com/metaweb/gridworks/browsing/facets/TextSearchFacet.java index 5bebe4089..826344214 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/facets/TextSearchFacet.java +++ b/src/main/java/com/metaweb/gridworks/browsing/facets/TextSearchFacet.java @@ -14,71 +14,71 @@ import com.metaweb.gridworks.gel.ast.VariableExpr; import com.metaweb.gridworks.model.Project; public class TextSearchFacet implements Facet { - protected String _name; - protected String _columnName; - protected int _cellIndex; - protected String _query; - - protected String _mode; - protected boolean _caseSensitive; - - public TextSearchFacet() { - } + protected String _name; + protected String _columnName; + protected int _cellIndex; + protected String _query; + + protected String _mode; + protected boolean _caseSensitive; + + public TextSearchFacet() { + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("name"); writer.value(_name); - writer.key("columnName"); writer.value(_columnName); - writer.key("query"); writer.value(_query); - writer.key("mode"); writer.value(_mode); - writer.key("caseSensitive"); writer.value(_caseSensitive); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("name"); writer.value(_name); + writer.key("columnName"); writer.value(_columnName); + writer.key("query"); writer.value(_query); + writer.key("mode"); writer.value(_mode); + writer.key("caseSensitive"); writer.value(_caseSensitive); + writer.endObject(); + } - public void initializeFromJSON(Project project, JSONObject o) throws Exception { - _name = o.getString("name"); - _columnName = o.getString("columnName"); - _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); - - if (!o.isNull("query")) { - _query = o.getString("query"); - } - - _mode = o.getString("mode"); - _caseSensitive = o.getBoolean("caseSensitive"); - if (_query != null) { - _query = _query.trim(); - if (!_caseSensitive) { - _query = _query.toLowerCase(); - } - } - } + public void initializeFromJSON(Project project, JSONObject o) throws Exception { + _name = o.getString("name"); + _columnName = o.getString("columnName"); + _cellIndex = project.columnModel.getColumnByName(_columnName).getCellIndex(); + + if (!o.isNull("query")) { + _query = o.getString("query"); + } + + _mode = o.getString("mode"); + _caseSensitive = o.getBoolean("caseSensitive"); + if (_query != null) { + _query = _query.trim(); + if (!_caseSensitive) { + _query = _query.toLowerCase(); + } + } + } - public RowFilter getRowFilter() { - if (_query == null || _query.length() == 0) { - return null; - } - - Evaluable eval = new VariableExpr("value"); - - if ("regex".equals(_mode)) { - return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { - protected boolean checkValue(String s) { - return s.matches(_query); - }; - }; - } else { - return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { - protected boolean checkValue(String s) { - return (_caseSensitive ? s : s.toLowerCase()).contains(_query); - }; - }; - } - } + public RowFilter getRowFilter() { + if (_query == null || _query.length() == 0) { + return null; + } + + Evaluable eval = new VariableExpr("value"); + + if ("regex".equals(_mode)) { + return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { + protected boolean checkValue(String s) { + return s.matches(_query); + }; + }; + } else { + return new ExpressionStringComparisonRowFilter(eval, _cellIndex) { + protected boolean checkValue(String s) { + return (_caseSensitive ? s : s.toLowerCase()).contains(_query); + }; + }; + } + } - public void computeChoices(Project project, FilteredRows filteredRows) { - // nothing to do - } + public void computeChoices(Project project, FilteredRows filteredRows) { + // nothing to do + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionEqualRowFilter.java b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionEqualRowFilter.java index 6c038c083..e9585c128 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionEqualRowFilter.java +++ b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionEqualRowFilter.java @@ -9,57 +9,57 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class ExpressionEqualRowFilter implements RowFilter { - final protected Evaluable _evaluable; - final protected int _cellIndex; - final protected Object[] _matches; - final protected boolean _selectBlank; - final protected boolean _selectError; - - public ExpressionEqualRowFilter(Evaluable evaluable, int cellIndex, Object[] matches, boolean selectBlank, boolean selectError) { - _evaluable = evaluable; - _cellIndex = cellIndex; - _matches = matches; - _selectBlank = selectBlank; - _selectError = selectError; - } + final protected Evaluable _evaluable; + final protected int _cellIndex; + final protected Object[] _matches; + final protected boolean _selectBlank; + final protected boolean _selectError; + + public ExpressionEqualRowFilter(Evaluable evaluable, int cellIndex, Object[] matches, boolean selectBlank, boolean selectError) { + _evaluable = evaluable; + _cellIndex = cellIndex; + _matches = matches; + _selectBlank = selectBlank; + _selectError = selectError; + } - public boolean filterRow(Project project, int rowIndex, Row row) { - Cell cell = row.getCell(_cellIndex); + public boolean filterRow(Project project, int rowIndex, Row row) { + Cell cell = row.getCell(_cellIndex); Properties bindings = ExpressionUtils.createBindings(project); ExpressionUtils.bind(bindings, row, rowIndex, cell); - - Object value = _evaluable.evaluate(bindings); - if (value != null && value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - if (testValue(v)) { - return true; - } - } - } else { - return testValue(value); - } - return false; - } - - protected boolean testValue(Object v) { - if (ExpressionUtils.isError(v)) { - return _selectError; - } else if (ExpressionUtils.isNonBlankData(v)) { + + Object value = _evaluable.evaluate(bindings); + if (value != null && value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + if (testValue(v)) { + return true; + } + } + } else { + return testValue(value); + } + return false; + } + + protected boolean testValue(Object v) { + if (ExpressionUtils.isError(v)) { + return _selectError; + } else if (ExpressionUtils.isNonBlankData(v)) { for (Object match : _matches) { if (testValue(v, match)) { return true; } } - return false; - } else { - return _selectBlank; - } - } - - protected boolean testValue(Object v, Object match) { - return (v instanceof Number && match instanceof Number) ? - ((Number) match).doubleValue() == ((Number) v).doubleValue() : - match.equals(v); - } + return false; + } else { + return _selectBlank; + } + } + + protected boolean testValue(Object v, Object match) { + return (v instanceof Number && match instanceof Number) ? + ((Number) match).doubleValue() == ((Number) v).doubleValue() : + match.equals(v); + } } diff --git a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionNumberComparisonRowFilter.java b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionNumberComparisonRowFilter.java index 017ebffed..9a5196bae 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionNumberComparisonRowFilter.java +++ b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionNumberComparisonRowFilter.java @@ -9,37 +9,37 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; abstract public class ExpressionNumberComparisonRowFilter implements RowFilter { - final protected Evaluable _evaluable; - final protected int _cellIndex; - - public ExpressionNumberComparisonRowFilter(Evaluable evaluable, int cellIndex) { - _evaluable = evaluable; - _cellIndex = cellIndex; - } + final protected Evaluable _evaluable; + final protected int _cellIndex; + + public ExpressionNumberComparisonRowFilter(Evaluable evaluable, int cellIndex) { + _evaluable = evaluable; + _cellIndex = cellIndex; + } - public boolean filterRow(Project project, int rowIndex, Row row) { - Cell cell = row.getCell(_cellIndex); + public boolean filterRow(Project project, int rowIndex, Row row) { + Cell cell = row.getCell(_cellIndex); Properties bindings = ExpressionUtils.createBindings(project); ExpressionUtils.bind(bindings, row, rowIndex, cell); - - Object value = _evaluable.evaluate(bindings); - if (value != null) { - if (value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - if (v instanceof Number && checkValue(((Number) v).doubleValue())) { - return true; - } - } - } else { - if (value instanceof Number && checkValue(((Number) value).doubleValue())) { - return true; - } - } - } - return false; - } - - abstract protected boolean checkValue(double d); + + Object value = _evaluable.evaluate(bindings); + if (value != null) { + if (value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + if (v instanceof Number && checkValue(((Number) v).doubleValue())) { + return true; + } + } + } else { + if (value instanceof Number && checkValue(((Number) value).doubleValue())) { + return true; + } + } + } + return false; + } + + abstract protected boolean checkValue(double d); } diff --git a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionStringComparisonRowFilter.java b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionStringComparisonRowFilter.java index 8ab7279f6..3d3c18bbf 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionStringComparisonRowFilter.java +++ b/src/main/java/com/metaweb/gridworks/browsing/filters/ExpressionStringComparisonRowFilter.java @@ -9,36 +9,36 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; abstract public class ExpressionStringComparisonRowFilter implements RowFilter { - final protected Evaluable _evaluable; - final protected int _cellIndex; - - public ExpressionStringComparisonRowFilter(Evaluable evaluable, int cellIndex) { - _evaluable = evaluable; - _cellIndex = cellIndex; - } + final protected Evaluable _evaluable; + final protected int _cellIndex; + + public ExpressionStringComparisonRowFilter(Evaluable evaluable, int cellIndex) { + _evaluable = evaluable; + _cellIndex = cellIndex; + } - public boolean filterRow(Project project, int rowIndex, Row row) { - Cell cell = row.getCell(_cellIndex); + public boolean filterRow(Project project, int rowIndex, Row row) { + Cell cell = row.getCell(_cellIndex); Properties bindings = ExpressionUtils.createBindings(project); ExpressionUtils.bind(bindings, row, rowIndex, cell); - - Object value = _evaluable.evaluate(bindings); - if (value != null) { - if (value.getClass().isArray()) { - Object[] a = (Object[]) value; - for (Object v : a) { - if (checkValue(v instanceof String ? ((String) v) : v.toString())) { - return true; - } - } - } else { - if (checkValue(value instanceof String ? ((String) value) : value.toString())) { - return true; - } - } - } - return false; - } - - abstract protected boolean checkValue(String s); + + Object value = _evaluable.evaluate(bindings); + if (value != null) { + if (value.getClass().isArray()) { + Object[] a = (Object[]) value; + for (Object v : a) { + if (checkValue(v instanceof String ? ((String) v) : v.toString())) { + return true; + } + } + } else { + if (checkValue(value instanceof String ? ((String) value) : value.toString())) { + return true; + } + } + } + return false; + } + + abstract protected boolean checkValue(String s); } diff --git a/src/main/java/com/metaweb/gridworks/browsing/filters/RowFilter.java b/src/main/java/com/metaweb/gridworks/browsing/filters/RowFilter.java index cf3f0cb5c..aaeb23eaf 100644 --- a/src/main/java/com/metaweb/gridworks/browsing/filters/RowFilter.java +++ b/src/main/java/com/metaweb/gridworks/browsing/filters/RowFilter.java @@ -4,5 +4,5 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public interface RowFilter { - public boolean filterRow(Project project, int rowIndex, Row row); + public boolean filterRow(Project project, int rowIndex, Row row); } diff --git a/src/main/java/com/metaweb/gridworks/commands/Command.java b/src/main/java/com/metaweb/gridworks/commands/Command.java index 21bbc4523..c2cdb8680 100644 --- a/src/main/java/com/metaweb/gridworks/commands/Command.java +++ b/src/main/java/com/metaweb/gridworks/commands/Command.java @@ -32,160 +32,160 @@ import com.oreilly.servlet.multipart.Part; public abstract class Command { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - throw new NotImplementedException(); + throw new NotImplementedException(); }; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - throw new NotImplementedException(); + throw new NotImplementedException(); }; protected Project getProject(HttpServletRequest request) throws ServletException { - try { - Project p = ProjectManager.singleton.getProject(Long.parseLong(request.getParameter("project"))); - if (p != null) { - return p; - } - } catch (Exception e) { - // ignore - } - throw new ServletException("Missing or bad project URL parameter"); + try { + Project p = ProjectManager.singleton.getProject(Long.parseLong(request.getParameter("project"))); + if (p != null) { + return p; + } + } catch (Exception e) { + // ignore + } + throw new ServletException("Missing or bad project URL parameter"); } protected int getIntegerParameter(HttpServletRequest request, String name, int def) throws ServletException { - try { - return Integer.parseInt(request.getParameter(name)); - } catch (Exception e) { - // ignore - } - return def; + try { + return Integer.parseInt(request.getParameter(name)); + } catch (Exception e) { + // ignore + } + return def; } protected void respond(HttpServletResponse response, String content) throws IOException { - response.setStatus(HttpServletResponse.SC_OK); - - OutputStream os = response.getOutputStream(); - OutputStreamWriter osw = new OutputStreamWriter(os); - try { - osw.write(content); - } finally { - osw.flush(); - osw.close(); - } + response.setStatus(HttpServletResponse.SC_OK); + + OutputStream os = response.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os); + try { + osw.write(content); + } finally { + osw.flush(); + osw.close(); + } } protected void respondJSON(HttpServletResponse response, Jsonizable o) throws IOException, JSONException { - respondJSON(response, o, new Properties()); + respondJSON(response, o, new Properties()); } protected void respondJSON(HttpServletResponse response, Jsonizable o, Properties options) throws IOException, JSONException { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - - o.write(writer, options); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + JSONWriter writer = new JSONWriter(response.getWriter()); + + o.write(writer, options); } protected void respondException(HttpServletResponse response, Exception e) throws IOException { e.printStackTrace(); - try { - JSONObject o = new JSONObject(); - o.put("code", "error"); - o.put("message", e.getMessage()); - - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - pw.flush(); - sw.flush(); - - o.put("stack", sw.toString()); - - response.setHeader("Content-Type", "application/json"); - respond(response, o.toString()); - } catch (JSONException e1) { - e.printStackTrace(response.getWriter()); - } + try { + JSONObject o = new JSONObject(); + o.put("code", "error"); + o.put("message", e.getMessage()); + + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + pw.flush(); + sw.flush(); + + o.put("stack", sw.toString()); + + response.setHeader("Content-Type", "application/json"); + respond(response, o.toString()); + } catch (JSONException e1) { + e.printStackTrace(response.getWriter()); + } } protected void redirect(HttpServletResponse response, String url) throws IOException { - response.setStatus(HttpServletResponse.SC_OK); - - String content = ""; - response.getWriter().print(content); - } + response.setStatus(HttpServletResponse.SC_OK); + + String content = ""; + response.getWriter().print(content); + } protected String readFileUpload(HttpServletRequest request, Properties properties) throws IOException { - StringBuffer sb = new StringBuffer(); - try { - MultipartParser parser = new MultipartParser(request, 20 * 1024 * 1024); - Part part = null; - while ((part = parser.readNextPart()) != null) { - - if (part.isFile()) { - Reader reader = new InputStreamReader(((FilePart) part).getInputStream()); - LineNumberReader lnr = new LineNumberReader(reader); - try { - String line = null; - while ((line = lnr.readLine()) != null) { - sb.append(line); - sb.append('\n'); - } - } finally { - lnr.close(); - } - } else if (part.isParam()) { - ParamPart paramPart = (ParamPart) part; - properties.put(part.getName(), paramPart.getStringValue()); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - - return sb.toString(); + StringBuffer sb = new StringBuffer(); + try { + MultipartParser parser = new MultipartParser(request, 20 * 1024 * 1024); + Part part = null; + while ((part = parser.readNextPart()) != null) { + + if (part.isFile()) { + Reader reader = new InputStreamReader(((FilePart) part).getInputStream()); + LineNumberReader lnr = new LineNumberReader(reader); + try { + String line = null; + while ((line = lnr.readLine()) != null) { + sb.append(line); + sb.append('\n'); + } + } finally { + lnr.close(); + } + } else if (part.isParam()) { + ParamPart paramPart = (ParamPart) part; + properties.put(part.getName(), paramPart.getStringValue()); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return sb.toString(); } protected JSONObject getJsonParameter(HttpServletRequest request, String name) { - String value = request.getParameter(name); - if (value != null) { - try { - JSONObject o = jsonStringToObject(value); - - return o; - } catch (JSONException e) { - } - } - return null; + String value = request.getParameter(name); + if (value != null) { + try { + JSONObject o = jsonStringToObject(value); + + return o; + } catch (JSONException e) { + } + } + return null; } protected JSONObject jsonStringToObject(String s) throws JSONException { - JSONTokener t = new JSONTokener(s); - JSONObject o = (JSONObject) t.nextValue(); - return o; + JSONTokener t = new JSONTokener(s); + JSONObject o = (JSONObject) t.nextValue(); + return o; } protected JSONArray jsonStringToArray(String s) throws JSONException { - JSONTokener t = new JSONTokener(s); - JSONArray a = (JSONArray) t.nextValue(); - return a; + JSONTokener t = new JSONTokener(s); + JSONArray a = (JSONArray) t.nextValue(); + return a; } protected JSONObject getEngineConfig(HttpServletRequest request) throws Exception { - String json = request.getParameter("engine"); - if (json != null) { - return jsonStringToObject(json); - } - return null; + String json = request.getParameter("engine"); + if (json != null) { + return jsonStringToObject(json); + } + return null; } protected Engine getEngine(HttpServletRequest request, Project project) throws Exception { - Engine engine = new Engine(project); - String json = request.getParameter("engine"); - if (json != null) { - JSONObject o = jsonStringToObject(json); - engine.initializeFromJSON(o); - } - return engine; + Engine engine = new Engine(project); + String json = request.getParameter("engine"); + if (json != null) { + JSONObject o = jsonStringToObject(json); + engine.initializeFromJSON(o); + } + return engine; } } diff --git a/src/main/java/com/metaweb/gridworks/commands/EngineDependentCommand.java b/src/main/java/com/metaweb/gridworks/commands/EngineDependentCommand.java index ac6d63830..6366c08c7 100644 --- a/src/main/java/com/metaweb/gridworks/commands/EngineDependentCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/EngineDependentCommand.java @@ -14,25 +14,25 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.process.Process; abstract public class EngineDependentCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - AbstractOperation op = createOperation(request, getEngineConfig(request)); - Process process = op.createProcess(project, new Properties()); - - boolean done = project.processManager.queueProcess(process); - - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - - } catch (Exception e) { - respondException(response, e); - } - } - - abstract protected AbstractOperation createOperation( - HttpServletRequest request, JSONObject engineConfig) throws Exception; + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + AbstractOperation op = createOperation(request, getEngineConfig(request)); + Process process = op.createProcess(project, new Properties()); + + boolean done = project.processManager.queueProcess(process); + + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + + } catch (Exception e) { + respondException(response, e); + } + } + + abstract protected AbstractOperation createOperation( + HttpServletRequest request, JSONObject engineConfig) throws Exception; } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java index 8cf3ebfb8..7df3ff473 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/AddColumnCommand.java @@ -9,22 +9,22 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.ColumnAdditionOperation; public class AddColumnCommand extends EngineDependentCommand { - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String baseColumnName = request.getParameter("baseColumnName"); - String expression = request.getParameter("expression"); - String headerLabel = request.getParameter("headerLabel"); - int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex")); - - return new ColumnAdditionOperation( - engineConfig, - baseColumnName, - expression, - headerLabel, - columnInsertIndex - ); - } + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String baseColumnName = request.getParameter("baseColumnName"); + String expression = request.getParameter("expression"); + String headerLabel = request.getParameter("headerLabel"); + int columnInsertIndex = Integer.parseInt(request.getParameter("columnInsertIndex")); + + return new ColumnAdditionOperation( + engineConfig, + baseColumnName, + expression, + headerLabel, + columnInsertIndex + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateOneRowCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateOneRowCommand.java index 97cb0e7eb..994688bf8 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateOneRowCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateOneRowCommand.java @@ -15,71 +15,71 @@ import com.metaweb.gridworks.model.changes.RowStarChange; import com.metaweb.gridworks.process.QuickHistoryEntryProcess; public class AnnotateOneRowCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); - try { - Project project = getProject(request); - - int rowIndex = Integer.parseInt(request.getParameter("row")); - - String starredString = request.getParameter("starred"); - if (starredString != null) { - boolean starred = "true".endsWith(starredString); - String description = starred ? "Star row " + rowIndex : "Unstar row " + rowIndex; + try { + Project project = getProject(request); + + int rowIndex = Integer.parseInt(request.getParameter("row")); + + String starredString = request.getParameter("starred"); + if (starredString != null) { + boolean starred = "true".endsWith(starredString); + String description = starred ? "Star row " + rowIndex : "Unstar row " + rowIndex; - StarOneRowProcess process = new StarOneRowProcess( + StarOneRowProcess process = new StarOneRowProcess( project, description, rowIndex, starred ); - - boolean done = project.processManager.queueProcess(process); - if (done) { - JSONWriter writer = new JSONWriter(response.getWriter()); - - writer.object(); - writer.key("code"); writer.value("ok"); - writer.endObject(); - } else { - respond(response, "{ \"code\" : \"pending\" }"); - } - } else { + + boolean done = project.processManager.queueProcess(process); + if (done) { + JSONWriter writer = new JSONWriter(response.getWriter()); + + writer.object(); + writer.key("code"); writer.value("ok"); + writer.endObject(); + } else { + respond(response, "{ \"code\" : \"pending\" }"); + } + } else { respond(response, "{ \"code\" : \"error\", \"message\" : \"invalid command parameters\" }"); - } - } catch (Exception e) { - respondException(response, e); - } - } - - protected class StarOneRowProcess extends QuickHistoryEntryProcess { - final int rowIndex; - final boolean starred; - - StarOneRowProcess( - Project project, - String briefDescription, - int rowIndex, - boolean starred - ) { - super(project, briefDescription); - - this.rowIndex = rowIndex; - this.starred = starred; - } + } + } catch (Exception e) { + respondException(response, e); + } + } + + protected class StarOneRowProcess extends QuickHistoryEntryProcess { + final int rowIndex; + final boolean starred; + + StarOneRowProcess( + Project project, + String briefDescription, + int rowIndex, + boolean starred + ) { + super(project, briefDescription); + + this.rowIndex = rowIndex; + this.starred = starred; + } - protected HistoryEntry createHistoryEntry() throws Exception { - return new HistoryEntry( - _project, - starred ? "Star row " + rowIndex : "Unstar row " + rowIndex, - null, - new RowStarChange(rowIndex, starred) - ); - } - } + protected HistoryEntry createHistoryEntry() throws Exception { + return new HistoryEntry( + _project, + starred ? "Star row " + rowIndex : "Unstar row " + rowIndex, + null, + new RowStarChange(rowIndex, starred) + ); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateRowsCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateRowsCommand.java index 6c0df9326..efd052d9c 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateRowsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/AnnotateRowsCommand.java @@ -9,11 +9,11 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.RowStarOperation; public class AnnotateRowsCommand extends EngineDependentCommand { - - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - + + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + String starredString = request.getParameter("starred"); if (starredString != null) { boolean starred = "true".endsWith(starredString); @@ -21,5 +21,5 @@ public class AnnotateRowsCommand extends EngineDependentCommand { return new RowStarOperation(engineConfig, starred); } return null; - } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/ApplyOperationsCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/ApplyOperationsCommand.java index 4279a128a..fc75b66e7 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/ApplyOperationsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/ApplyOperationsCommand.java @@ -19,55 +19,55 @@ import com.metaweb.gridworks.operations.OperationRegistry; import com.metaweb.gridworks.process.Process; public class ApplyOperationsCommand extends Command { - - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - String jsonString = request.getParameter("operations"); - try { - JSONArray a = jsonStringToArray(jsonString); - int count = a.length(); - for (int i = 0; i < count; i++) { - JSONObject obj = a.getJSONObject(i); - - reconstructOperation(project, obj); - } + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + String jsonString = request.getParameter("operations"); + try { + JSONArray a = jsonStringToArray(jsonString); + int count = a.length(); + for (int i = 0; i < count; i++) { + JSONObject obj = a.getJSONObject(i); + + reconstructOperation(project, obj); + } - if (project.processManager.hasPending()) { - respond(response, "{ \"code\" : \"pending\" }"); - } else { - respond(response, "{ \"code\" : \"ok\" }"); - } - } catch (JSONException e) { - respondException(response, e); - } - } - - protected void reconstructOperation(Project project, JSONObject obj) { - try { - String op = obj.getString("op"); - - Class klass = OperationRegistry.s_opNameToClass.get(op); - if (klass == null) { - return; - } - - Method reconstruct = klass.getMethod("reconstruct", Project.class, JSONObject.class); - if (reconstruct == null) { - return; - } - - AbstractOperation operation = (AbstractOperation) reconstruct.invoke(null, project, obj); + if (project.processManager.hasPending()) { + respond(response, "{ \"code\" : \"pending\" }"); + } else { + respond(response, "{ \"code\" : \"ok\" }"); + } + } catch (JSONException e) { + respondException(response, e); + } + } + + protected void reconstructOperation(Project project, JSONObject obj) { + try { + String op = obj.getString("op"); + + Class klass = OperationRegistry.s_opNameToClass.get(op); + if (klass == null) { + return; + } + + Method reconstruct = klass.getMethod("reconstruct", Project.class, JSONObject.class); + if (reconstruct == null) { + return; + } + + AbstractOperation operation = (AbstractOperation) reconstruct.invoke(null, project, obj); if (operation != null) { Process process = operation.createProcess(project, new Properties()); project.processManager.queueProcess(process); } - } catch (Exception e) { - e.printStackTrace(); - } - - } + } catch (Exception e) { + e.printStackTrace(); + } + + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java index 7f39dcb3d..6125672fe 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java @@ -34,79 +34,79 @@ import com.oreilly.servlet.multipart.Part; public class CreateProjectCommand extends Command { @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Properties options = parseUrlParameters(request); - Project project = new Project(); - - internalImport(request, project, options); + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Properties options = parseUrlParameters(request); + Project project = new Project(); + + internalImport(request, project, options); - ProjectMetadata pm = new ProjectMetadata(); - pm.setName(options.getProperty("project-name")); - pm.setPassword(options.getProperty("project-password")); + ProjectMetadata pm = new ProjectMetadata(); + pm.setName(options.getProperty("project-name")); + pm.setPassword(options.getProperty("project-password")); pm.setEncoding(options.getProperty("encoding")); pm.setEncodingConfidence(options.getProperty("encoding_confidence")); - ProjectManager.singleton.registerProject(project, pm); + ProjectManager.singleton.registerProject(project, pm); - project.columnModel.update(); + project.columnModel.update(); project.recomputeRowContextDependencies(); - - redirect(response, "/project.html?project=" + project.id); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - protected Properties parseUrlParameters(HttpServletRequest request) { - Properties options = new Properties(); - - String query = request.getQueryString(); - if (query != null) { - if (query.startsWith("?")) { - query = query.substring(1); - } - - String[] pairs = query.split("&"); - for (String pairString : pairs) { - int equal = pairString.indexOf('='); - String name = equal >= 0 ? pairString.substring(0, equal) : ""; - String value = equal >= 0 ? ParsingUtilities.decode(pairString.substring(equal + 1)) : ""; - - options.put(name, value); - } - } - return options; - } - - protected void internalImport( - HttpServletRequest request, - Project project, - Properties options - ) throws Exception { - MultipartParser parser = null; - try { - parser = new MultipartParser(request, 20 * 1024 * 1024); - } catch (Exception e) { - // silent - } - - if (parser != null) { - Part part = null; - String url = null; - - int limit = -1; - int skip = 0; - - if (options.containsKey("limit")) { - String s = options.getProperty("limit"); - try { - limit = Integer.parseInt(s); - } catch (Exception e) { - } - } + + redirect(response, "/project.html?project=" + project.id); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + protected Properties parseUrlParameters(HttpServletRequest request) { + Properties options = new Properties(); + + String query = request.getQueryString(); + if (query != null) { + if (query.startsWith("?")) { + query = query.substring(1); + } + + String[] pairs = query.split("&"); + for (String pairString : pairs) { + int equal = pairString.indexOf('='); + String name = equal >= 0 ? pairString.substring(0, equal) : ""; + String value = equal >= 0 ? ParsingUtilities.decode(pairString.substring(equal + 1)) : ""; + + options.put(name, value); + } + } + return options; + } + + protected void internalImport( + HttpServletRequest request, + Project project, + Properties options + ) throws Exception { + MultipartParser parser = null; + try { + parser = new MultipartParser(request, 20 * 1024 * 1024); + } catch (Exception e) { + // silent + } + + if (parser != null) { + Part part = null; + String url = null; + + int limit = -1; + int skip = 0; + + if (options.containsKey("limit")) { + String s = options.getProperty("limit"); + try { + limit = Integer.parseInt(s); + } catch (Exception e) { + } + } if (options.containsKey("skip")) { String s = options.getProperty("skip"); try { @@ -114,15 +114,15 @@ public class CreateProjectCommand extends Command { } catch (Exception e) { } } - - while ((part = parser.readNextPart()) != null) { - - if (part.isFile()) { - FilePart filePart = (FilePart) part; - - Importer importer = guessImporter(options, null, filePart.getFileName()); - - if (importer.takesReader()) { + + while ((part = parser.readNextPart()) != null) { + + if (part.isFile()) { + FilePart filePart = (FilePart) part; + + Importer importer = guessImporter(options, null, filePart.getFileName()); + + if (importer.takesReader()) { CharsetDetector detector = new CharsetDetector(); detector.setDeclaredEncoding("utf8"); // the content on the web is encoded in UTF-8 so assume that CharsetMatch charsetMatch = detector.setText(enforceMarking(filePart.getInputStream())).detect(); @@ -135,120 +135,120 @@ public class CreateProjectCommand extends Command { } finally { reader.close(); } - } else { - InputStream inputStream = filePart.getInputStream(); - try { - importer.read(inputStream, project, options, skip, limit); - } finally { - inputStream.close(); - } - } - } else if (part.isParam()) { - ParamPart paramPart = (ParamPart) part; - String paramName = paramPart.getName(); - if (paramName.equals("raw-text")) { - StringReader reader = new StringReader(paramPart.getStringValue()); - try { - new TsvCsvImporter().read(reader, project, options, skip, limit); - } finally { - reader.close(); - } - } else if (paramName.equals("url")) { - url = paramPart.getStringValue(); - } else { - options.put(paramName, paramPart.getStringValue()); - } - } - } - - if (url != null && url.length() > 0) { - internalImportURL(request, project, options, url, skip, limit); - } - } - } - - protected void internalImportURL( - HttpServletRequest request, - Project project, - Properties options, - String urlString, - int skip, - int limit - ) throws Exception { - URL url = new URL(urlString); - URLConnection connection = null; - - try { - connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - } catch (Exception e) { - throw new Exception("Cannot connect to " + urlString, e); - } - + } else { + InputStream inputStream = filePart.getInputStream(); + try { + importer.read(inputStream, project, options, skip, limit); + } finally { + inputStream.close(); + } + } + } else if (part.isParam()) { + ParamPart paramPart = (ParamPart) part; + String paramName = paramPart.getName(); + if (paramName.equals("raw-text")) { + StringReader reader = new StringReader(paramPart.getStringValue()); + try { + new TsvCsvImporter().read(reader, project, options, skip, limit); + } finally { + reader.close(); + } + } else if (paramName.equals("url")) { + url = paramPart.getStringValue(); + } else { + options.put(paramName, paramPart.getStringValue()); + } + } + } + + if (url != null && url.length() > 0) { + internalImportURL(request, project, options, url, skip, limit); + } + } + } + + protected void internalImportURL( + HttpServletRequest request, + Project project, + Properties options, + String urlString, + int skip, + int limit + ) throws Exception { + URL url = new URL(urlString); + URLConnection connection = null; + + try { + connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + } catch (Exception e) { + throw new Exception("Cannot connect to " + urlString, e); + } + InputStream inputStream = null; try { - inputStream = connection.getInputStream(); + inputStream = connection.getInputStream(); } catch (Exception e) { - throw new Exception("Cannot retrieve content from " + url, e); + throw new Exception("Cannot retrieve content from " + url, e); } try { - Importer importer = guessImporter( - options, - connection.getContentType(), - url.getPath() - ); + Importer importer = guessImporter( + options, + connection.getContentType(), + url.getPath() + ); - if (importer.takesReader()) { - String encoding = connection.getContentEncoding(); - - Reader reader = new InputStreamReader( - inputStream, (encoding == null) ? "ISO-8859-1" : encoding); - - importer.read(reader, project, options, skip, limit); - } else { - importer.read(inputStream, project, options, skip, limit); - } + if (importer.takesReader()) { + String encoding = connection.getContentEncoding(); + + Reader reader = new InputStreamReader( + inputStream, (encoding == null) ? "ISO-8859-1" : encoding); + + importer.read(reader, project, options, skip, limit); + } else { + importer.read(inputStream, project, options, skip, limit); + } } finally { - inputStream.close(); + inputStream.close(); } - } - - protected Importer guessImporter( - Properties options, String contentType, String fileName) { - - if (contentType != null) { - contentType = contentType.toLowerCase().trim(); - - if ("application/msexcel".equals(contentType) || - "application/x-msexcel".equals(contentType) || - "application/x-ms-excel".equals(contentType) || - "application/vnd.ms-excel".equals(contentType) || - "application/x-excel".equals(contentType) || - "application/xls".equals(contentType)) { - - return new ExcelImporter(false); - } else if("application/x-xls".equals(contentType)) { - return new ExcelImporter(true); - } - } else if (fileName != null) { - fileName = fileName.toLowerCase(); - if (fileName.endsWith(".xls")) { - return new ExcelImporter(false); - } else if (fileName.endsWith(".xlsx")) { - return new ExcelImporter(true); - } - } - - return new TsvCsvImporter(); - } + } + + protected Importer guessImporter( + Properties options, String contentType, String fileName) { + + if (contentType != null) { + contentType = contentType.toLowerCase().trim(); + + if ("application/msexcel".equals(contentType) || + "application/x-msexcel".equals(contentType) || + "application/x-ms-excel".equals(contentType) || + "application/vnd.ms-excel".equals(contentType) || + "application/x-excel".equals(contentType) || + "application/xls".equals(contentType)) { + + return new ExcelImporter(false); + } else if("application/x-xls".equals(contentType)) { + return new ExcelImporter(true); + } + } else if (fileName != null) { + fileName = fileName.toLowerCase(); + if (fileName.endsWith(".xls")) { + return new ExcelImporter(false); + } else if (fileName.endsWith(".xlsx")) { + return new ExcelImporter(true); + } + } + + return new TsvCsvImporter(); + } - /* - * NOTE(SM): The ICU4J char detection code requires the input stream to support mark/reset. Unfortunately, not - * all ServletInputStream implementations are marking, so we need do this memory-expensive wrapping to make - * it work. It's far from ideal but I don't have a more efficient solution. - */ + /* + * NOTE(SM): The ICU4J char detection code requires the input stream to support mark/reset. Unfortunately, not + * all ServletInputStream implementations are marking, so we need do this memory-expensive wrapping to make + * it work. It's far from ideal but I don't have a more efficient solution. + */ private static InputStream enforceMarking(InputStream input) throws IOException { if (input.markSupported()) { return input; @@ -267,5 +267,5 @@ public class CreateProjectCommand extends Command { return new ByteArrayInputStream(output.toByteArray()); } } - + } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/DoTextTransformCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/DoTextTransformCommand.java index 321df800c..548735be3 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/DoTextTransformCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/DoTextTransformCommand.java @@ -9,13 +9,13 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.TextTransformOperation; public class DoTextTransformCommand extends EngineDependentCommand { - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - String expression = request.getParameter("expression"); - - return new TextTransformOperation(engineConfig, columnName, expression); - } + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + String expression = request.getParameter("expression"); + + return new TextTransformOperation(engineConfig, columnName, expression); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/FacetBasedEditCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/FacetBasedEditCommand.java index 6dae079fd..81be2b7ae 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/FacetBasedEditCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/FacetBasedEditCommand.java @@ -10,19 +10,19 @@ import com.metaweb.gridworks.operations.FacetBasedEditOperation; import com.metaweb.gridworks.util.ParsingUtilities; public class FacetBasedEditCommand extends EngineDependentCommand { - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - String expression = request.getParameter("expression"); - String editsString = request.getParameter("edits"); - + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + String expression = request.getParameter("expression"); + String editsString = request.getParameter("edits"); + return new FacetBasedEditOperation( engineConfig, columnName, expression, FacetBasedEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString)) ); - } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/JoinMultiValueCellsCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/JoinMultiValueCellsCommand.java index b1abd9a7e..6c09c4c8e 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/JoinMultiValueCellsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/JoinMultiValueCellsCommand.java @@ -14,26 +14,26 @@ import com.metaweb.gridworks.operations.MultiValuedCellJoinOperation; import com.metaweb.gridworks.process.Process; public class JoinMultiValueCellsCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - String columnName = request.getParameter("columnName"); - String keyColumnName = request.getParameter("keyColumnName"); + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + String columnName = request.getParameter("columnName"); + String keyColumnName = request.getParameter("keyColumnName"); String separator = request.getParameter("separator"); - - AbstractOperation op = new MultiValuedCellJoinOperation(columnName, keyColumnName, separator); - Process process = op.createProcess(project, new Properties()); - - boolean done = project.processManager.queueProcess(process); - - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - - } catch (Exception e) { - respondException(response, e); - } - } + + AbstractOperation op = new MultiValuedCellJoinOperation(columnName, keyColumnName, separator); + Process process = op.createProcess(project, new Properties()); + + boolean done = project.processManager.queueProcess(process); + + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/RemoveColumnCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/RemoveColumnCommand.java index 42b51597a..198b451f3 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/RemoveColumnCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/RemoveColumnCommand.java @@ -14,24 +14,24 @@ import com.metaweb.gridworks.operations.ColumnRemovalOperation; import com.metaweb.gridworks.process.Process; public class RemoveColumnCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - String columnName = request.getParameter("columnName"); - - AbstractOperation op = new ColumnRemovalOperation(columnName); - Process process = op.createProcess(project, new Properties()); - - boolean done = project.processManager.queueProcess(process); - - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - - } catch (Exception e) { - respondException(response, e); - } - } + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + String columnName = request.getParameter("columnName"); + + AbstractOperation op = new ColumnRemovalOperation(columnName); + Process process = op.createProcess(project, new Properties()); + + boolean done = project.processManager.queueProcess(process); + + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/SaveProtographCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/SaveProtographCommand.java index 37a6024d3..a199ed159 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/SaveProtographCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/SaveProtographCommand.java @@ -17,26 +17,26 @@ import com.metaweb.gridworks.process.Process; import com.metaweb.gridworks.protograph.Protograph; public class SaveProtographCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - String jsonString = request.getParameter("protograph"); - JSONObject json = jsonStringToObject(jsonString); - Protograph protograph = Protograph.reconstruct(json); - - AbstractOperation op = new SaveProtographOperation(protograph); - Process process = op.createProcess(project, new Properties()); - - boolean done = project.processManager.queueProcess(process); - - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - - } catch (Exception e) { - respondException(response, e); - } - } + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + String jsonString = request.getParameter("protograph"); + JSONObject json = jsonStringToObject(jsonString); + Protograph protograph = Protograph.reconstruct(json); + + AbstractOperation op = new SaveProtographOperation(protograph); + Process process = op.createProcess(project, new Properties()); + + boolean done = project.processManager.queueProcess(process); + + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/SplitMultiValueCellsCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/SplitMultiValueCellsCommand.java index 94e413055..ae9e5a049 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/SplitMultiValueCellsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/SplitMultiValueCellsCommand.java @@ -14,27 +14,27 @@ import com.metaweb.gridworks.operations.MultiValuedCellSplitOperation; import com.metaweb.gridworks.process.Process; public class SplitMultiValueCellsCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - String columnName = request.getParameter("columnName"); - String keyColumnName = request.getParameter("keyColumnName"); + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + String columnName = request.getParameter("columnName"); + String keyColumnName = request.getParameter("keyColumnName"); String separator = request.getParameter("separator"); String mode = request.getParameter("mode"); - - AbstractOperation op = new MultiValuedCellSplitOperation(columnName, keyColumnName, separator, mode); - Process process = op.createProcess(project, new Properties()); - - boolean done = project.processManager.queueProcess(process); - - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - - } catch (Exception e) { - respondException(response, e); - } - } + + AbstractOperation op = new MultiValuedCellSplitOperation(columnName, keyColumnName, separator, mode); + Process process = op.createProcess(project, new Properties()); + + boolean done = project.processManager.queueProcess(process); + + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/UndoRedoCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/UndoRedoCommand.java index be8934f3f..ab2e9d406 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/UndoRedoCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/UndoRedoCommand.java @@ -11,17 +11,17 @@ import com.metaweb.gridworks.history.HistoryProcess; import com.metaweb.gridworks.model.Project; public class UndoRedoCommand extends Command { - - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - long lastDoneID = Long.parseLong(request.getParameter("lastDoneID")); - - boolean done = project.processManager.queueProcess( - new HistoryProcess(project, lastDoneID)); + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + long lastDoneID = Long.parseLong(request.getParameter("lastDoneID")); + + boolean done = project.processManager.queueProcess( + new HistoryProcess(project, lastDoneID)); - respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); - } + respond(response, "{ \"code\" : " + (done ? "\"ok\"" : "\"pending\"") + " }"); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/ComputeFacetsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/ComputeFacetsCommand.java index 8632700ab..7db1bf36b 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/ComputeFacetsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/ComputeFacetsCommand.java @@ -11,19 +11,19 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class ComputeFacetsCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - Engine engine = getEngine(request, project); - - engine.computeFacets(); - - respondJSON(response, engine); - } catch (Exception e) { - respondException(response, e); - } - } + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + Engine engine = getEngine(request, project); + + engine.computeFacets(); + + respondJSON(response, engine); + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java index 2b658f1e0..f5bdb1e10 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java @@ -19,91 +19,91 @@ import com.metaweb.gridworks.model.Row; public class ExportRowsCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - Engine engine = getEngine(request, project); - - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "text/plain"); - - PrintWriter writer = response.getWriter(); - - boolean first = true; - for (Column column : project.columnModel.columns) { - if (first) { - first = false; - } else { - writer.print("\t"); - } - writer.print(column.getHeaderLabel()); - } - writer.print("\n"); - - { - RowVisitor visitor = new RowVisitor() { - PrintWriter writer; - - public RowVisitor init(PrintWriter writer) { - this.writer = writer; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - boolean first = true; - for (Column column : project.columnModel.columns) { - if (first) { - first = false; - } else { - writer.print("\t"); - } - - int cellIndex = column.getCellIndex(); - if (cellIndex < row.cells.size()) { - Cell cell = row.cells.get(cellIndex); - if (cell != null && cell.value != null) { - writer.print(cell.value); - } - } - } - writer.print("\n"); - - return false; - } - }.init(writer); - - FilteredRows filteredRows = engine.getAllFilteredRows(true); - filteredRows.accept(project, visitor); - } - } catch (Exception e) { - respondException(response, e); - } - } - - static protected class RowAccumulator implements RowVisitor { - final public int start; - final public int limit; - - public int total; - - public RowAccumulator(int start, int limit) { - this.start = start; - this.limit = limit; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - boolean r = false; - - if (total >= start && total < start + limit) { - r = internalVisit(rowIndex, row); - } - total++; - return r; - } - - protected boolean internalVisit(int rowIndex, Row row) { - return false; - } - } + throws ServletException, IOException { + + try { + Project project = getProject(request); + Engine engine = getEngine(request, project); + + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "text/plain"); + + PrintWriter writer = response.getWriter(); + + boolean first = true; + for (Column column : project.columnModel.columns) { + if (first) { + first = false; + } else { + writer.print("\t"); + } + writer.print(column.getHeaderLabel()); + } + writer.print("\n"); + + { + RowVisitor visitor = new RowVisitor() { + PrintWriter writer; + + public RowVisitor init(PrintWriter writer) { + this.writer = writer; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + boolean first = true; + for (Column column : project.columnModel.columns) { + if (first) { + first = false; + } else { + writer.print("\t"); + } + + int cellIndex = column.getCellIndex(); + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + if (cell != null && cell.value != null) { + writer.print(cell.value); + } + } + } + writer.print("\n"); + + return false; + } + }.init(writer); + + FilteredRows filteredRows = engine.getAllFilteredRows(true); + filteredRows.accept(project, visitor); + } + } catch (Exception e) { + respondException(response, e); + } + } + + static protected class RowAccumulator implements RowVisitor { + final public int start; + final public int limit; + + public int total; + + public RowAccumulator(int start, int limit) { + this.start = start; + this.limit = limit; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + boolean r = false; + + if (total >= start && total < start + limit) { + r = internalVisit(rowIndex, row); + } + total++; + return r; + } + + protected boolean internalVisit(int rowIndex, Row row) { + return false; + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetAllProjectMetadataCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetAllProjectMetadataCommand.java index 89fdabdfa..92f5e881e 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetAllProjectMetadataCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetAllProjectMetadataCommand.java @@ -16,30 +16,30 @@ import com.metaweb.gridworks.ProjectMetadata; import com.metaweb.gridworks.commands.Command; public class GetAllProjectMetadataCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - Properties options = new Properties(); - - writer.object(); - - writer.key("projects"); writer.object(); - - Map m = ProjectManager.singleton.getAllProjectMetadata(); - for (Long id : m.keySet()) { - writer.key(id.toString()); - m.get(id).write(writer, options); - } - writer.endObject(); - writer.endObject(); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + JSONWriter writer = new JSONWriter(response.getWriter()); + Properties options = new Properties(); + + writer.object(); + + writer.key("projects"); writer.object(); + + Map m = ProjectManager.singleton.getAllProjectMetadata(); + for (Long id : m.keySet()) { + writer.key(id.toString()); + m.get(id).write(writer, options); + } + writer.endObject(); + writer.endObject(); + } catch (JSONException e) { + respondException(response, e); + } + } } \ No newline at end of file diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetHistoryCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetHistoryCommand.java index e0073be05..0400bc55b 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetHistoryCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetHistoryCommand.java @@ -12,16 +12,16 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class GetHistoryCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - try { - respondJSON(response, project.history); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + try { + respondJSON(response, project.history); + } catch (JSONException e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetModelsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetModelsCommand.java index 6da1da20e..e1cdbdff6 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetModelsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetModelsCommand.java @@ -14,31 +14,31 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class GetModelsCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - Properties options = new Properties(); - JSONWriter writer = new JSONWriter(response.getWriter()); - - writer.object(); - writer.key("columnModel"); project.columnModel.write(writer, options); - writer.key("protograph"); - if (project.protograph == null) { - writer.value(null); - } else { - project.protograph.write(writer, options); - } - writer.endObject(); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + Properties options = new Properties(); + JSONWriter writer = new JSONWriter(response.getWriter()); + + writer.object(); + writer.key("columnModel"); project.columnModel.write(writer, options); + writer.key("protograph"); + if (project.protograph == null) { + writer.value(null); + } else { + project.protograph.write(writer, options); + } + writer.endObject(); + } catch (JSONException e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetOperationsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetOperationsCommand.java index 13552d6c0..06e5b05e3 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetOperationsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetOperationsCommand.java @@ -15,36 +15,36 @@ import com.metaweb.gridworks.history.HistoryEntry; import com.metaweb.gridworks.model.Project; public class GetOperationsCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - - try { - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - Properties options = new Properties(); - JSONWriter writer = new JSONWriter(response.getWriter()); - - writer.object(); - writer.key("entries"); writer.array(); - - for (HistoryEntry entry : project.history.getLastPastEntries(-1)) { - writer.object(); - writer.key("description"); writer.value(entry.description); - if (entry.operation != null) { - writer.key("operation"); - entry.operation.write(writer, options); - } - writer.endObject(); - } - writer.endArray(); - writer.endObject(); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + Properties options = new Properties(); + JSONWriter writer = new JSONWriter(response.getWriter()); + + writer.object(); + writer.key("entries"); writer.array(); + + for (HistoryEntry entry : project.history.getLastPastEntries(-1)) { + writer.object(); + writer.key("description"); writer.value(entry.description); + if (entry.operation != null) { + writer.key("operation"); + entry.operation.write(writer, options); + } + writer.endObject(); + } + writer.endArray(); + writer.endObject(); + } catch (JSONException e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetProcessesCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetProcessesCommand.java index 8a4443db6..9761036c2 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetProcessesCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetProcessesCommand.java @@ -12,16 +12,16 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class GetProcessesCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - - try { - respondJSON(response, project.processManager); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + + try { + respondJSON(response, project.processManager); + } catch (JSONException e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetProjectMetadataCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetProjectMetadataCommand.java index 8f00beca0..38337329d 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetProjectMetadataCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetProjectMetadataCommand.java @@ -13,16 +13,16 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class GetProjectMetadataCommand extends Command { - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - Project project = getProject(request); - - try { - respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id)); - } catch (JSONException e) { - respondException(response, e); - } - } + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = getProject(request); + + try { + respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id)); + } catch (JSONException e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/GetRowsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/GetRowsCommand.java index fa92930f6..c6b4851e4 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/GetRowsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/GetRowsCommand.java @@ -20,99 +20,99 @@ import com.metaweb.gridworks.model.Row; public class GetRowsCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - Engine engine = getEngine(request, project); - - int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0))); - int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20))); - Properties options = new Properties(); - - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - writer.object(); - - { - RowAccumulator acc = new RowAccumulator(start, limit) { - JSONWriter writer; - Properties options; - Properties extra; - - public RowAccumulator init(JSONWriter writer, Properties options) { - this.writer = writer; - this.options = options; - - this.extra = new Properties(); - this.extra.put("contextual", true); - - return this; - } - - @Override - public boolean internalVisit(int rowIndex, Row row, boolean contextual) { - try { - if (contextual) { - options.put("extra", extra); - } else { - options.remove("extra"); - } - options.put("rowIndex", rowIndex); - - row.write(writer, options); - } catch (JSONException e) { - } - return false; - } - }.init(writer, options); - - FilteredRows filteredRows = engine.getAllFilteredRows(true); - - writer.key("rows"); writer.array(); - filteredRows.accept(project, acc); - writer.endArray(); - - writer.key("filtered"); writer.value(acc.total); - } - - writer.key("start"); writer.value(start); - writer.key("limit"); writer.value(limit); - writer.key("total"); writer.value(project.rows.size()); - - writer.endObject(); - } catch (Exception e) { - respondException(response, e); - } - } - - static protected class RowAccumulator implements RowVisitor { - final public int start; - final public int limit; - - public int total; - - public RowAccumulator(int start, int limit) { - this.start = start; - this.limit = limit; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - boolean r = false; - - if (total >= start && total < start + limit) { - r = internalVisit(rowIndex, row, contextual); - } - if (!contextual) { - total++; - } - return r; - } - - protected boolean internalVisit(int rowIndex, Row row, boolean contextual) { - return false; - } - } + throws ServletException, IOException { + + try { + Project project = getProject(request); + Engine engine = getEngine(request, project); + + int start = Math.min(project.rows.size(), Math.max(0, getIntegerParameter(request, "start", 0))); + int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20))); + Properties options = new Properties(); + + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); + + { + RowAccumulator acc = new RowAccumulator(start, limit) { + JSONWriter writer; + Properties options; + Properties extra; + + public RowAccumulator init(JSONWriter writer, Properties options) { + this.writer = writer; + this.options = options; + + this.extra = new Properties(); + this.extra.put("contextual", true); + + return this; + } + + @Override + public boolean internalVisit(int rowIndex, Row row, boolean contextual) { + try { + if (contextual) { + options.put("extra", extra); + } else { + options.remove("extra"); + } + options.put("rowIndex", rowIndex); + + row.write(writer, options); + } catch (JSONException e) { + } + return false; + } + }.init(writer, options); + + FilteredRows filteredRows = engine.getAllFilteredRows(true); + + writer.key("rows"); writer.array(); + filteredRows.accept(project, acc); + writer.endArray(); + + writer.key("filtered"); writer.value(acc.total); + } + + writer.key("start"); writer.value(start); + writer.key("limit"); writer.value(limit); + writer.key("total"); writer.value(project.rows.size()); + + writer.endObject(); + } catch (Exception e) { + respondException(response, e); + } + } + + static protected class RowAccumulator implements RowVisitor { + final public int start; + final public int limit; + + public int total; + + public RowAccumulator(int start, int limit) { + this.start = start; + this.limit = limit; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + boolean r = false; + + if (total >= start && total < start + limit) { + r = internalVisit(rowIndex, row, contextual); + } + if (!contextual) { + total++; + } + return r; + } + + protected boolean internalVisit(int rowIndex, Row row, boolean contextual) { + return false; + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconDiscardJudgmentsCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconDiscardJudgmentsCommand.java index 9205eb666..b3f18e83a 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconDiscardJudgmentsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconDiscardJudgmentsCommand.java @@ -9,12 +9,12 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.ReconDiscardJudgmentsOperation; public class ReconDiscardJudgmentsCommand extends EngineDependentCommand { - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - - return new ReconDiscardJudgmentsOperation(engineConfig, columnName); - } + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + + return new ReconDiscardJudgmentsOperation(engineConfig, columnName); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeOneCellCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeOneCellCommand.java index 41085e814..3e30fa453 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeOneCellCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeOneCellCommand.java @@ -25,154 +25,154 @@ import com.metaweb.gridworks.model.changes.ReconChange; import com.metaweb.gridworks.process.QuickHistoryEntryProcess; public class ReconJudgeOneCellCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - int rowIndex = Integer.parseInt(request.getParameter("row")); - int cellIndex = Integer.parseInt(request.getParameter("cell")); - Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment")); - - ReconCandidate match = null; - String topicID = request.getParameter("topicID"); - if (topicID != null) { - String scoreString = request.getParameter("score"); - - match = new ReconCandidate( - topicID, - request.getParameter("topicGUID"), - request.getParameter("topicName"), - request.getParameter("types").split(","), - scoreString != null ? Double.parseDouble(scoreString) : 100 - ); - } - - JudgeOneCellProcess process = new JudgeOneCellProcess( - project, - "Judge one cell's recon result", - judgment, - rowIndex, - cellIndex, - match - ); - - boolean done = project.processManager.queueProcess(process); - if (done) { - JSONWriter writer = new JSONWriter(response.getWriter()); - writer.object(); - writer.key("code"); writer.value("ok"); - writer.key("cell"); process.newCell.write(writer, new Properties()); - writer.endObject(); - } else { - respond(response, "{ \"code\" : \"pending\" }"); - } - } catch (Exception e) { - respondException(response, e); - } - } - - protected class JudgeOneCellProcess extends QuickHistoryEntryProcess { - final int rowIndex; - final int cellIndex; - final Judgment judgment; - final ReconCandidate match; - Cell newCell; - - - JudgeOneCellProcess( - Project project, - String briefDescription, - Judgment judgment, - int rowIndex, - int cellIndex, - ReconCandidate match - ) { - super(project, briefDescription); - - this.judgment = judgment; - this.rowIndex = rowIndex; - this.cellIndex = cellIndex; - this.match = match; - } + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + int rowIndex = Integer.parseInt(request.getParameter("row")); + int cellIndex = Integer.parseInt(request.getParameter("cell")); + Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment")); + + ReconCandidate match = null; + String topicID = request.getParameter("topicID"); + if (topicID != null) { + String scoreString = request.getParameter("score"); + + match = new ReconCandidate( + topicID, + request.getParameter("topicGUID"), + request.getParameter("topicName"), + request.getParameter("types").split(","), + scoreString != null ? Double.parseDouble(scoreString) : 100 + ); + } + + JudgeOneCellProcess process = new JudgeOneCellProcess( + project, + "Judge one cell's recon result", + judgment, + rowIndex, + cellIndex, + match + ); + + boolean done = project.processManager.queueProcess(process); + if (done) { + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); + writer.key("code"); writer.value("ok"); + writer.key("cell"); process.newCell.write(writer, new Properties()); + writer.endObject(); + } else { + respond(response, "{ \"code\" : \"pending\" }"); + } + } catch (Exception e) { + respondException(response, e); + } + } + + protected class JudgeOneCellProcess extends QuickHistoryEntryProcess { + final int rowIndex; + final int cellIndex; + final Judgment judgment; + final ReconCandidate match; + Cell newCell; + + + JudgeOneCellProcess( + Project project, + String briefDescription, + Judgment judgment, + int rowIndex, + int cellIndex, + ReconCandidate match + ) { + super(project, briefDescription); + + this.judgment = judgment; + this.rowIndex = rowIndex; + this.cellIndex = cellIndex; + this.match = match; + } - protected HistoryEntry createHistoryEntry() throws Exception { - Cell cell = _project.rows.get(rowIndex).getCell(cellIndex); + protected HistoryEntry createHistoryEntry() throws Exception { + Cell cell = _project.rows.get(rowIndex).getCell(cellIndex); if (cell == null || !ExpressionUtils.isNonBlankData(cell.value)) { - throw new Exception("Cell is blank or error"); - } - - Column column = _project.columnModel.getColumnByCellIndex(cellIndex); - if (column == null) { - throw new Exception("No such column"); - } - - Judgment oldJudgment = cell.recon == null ? Judgment.None : cell.recon.judgment; - - newCell = new Cell( - cell.value, - cell.recon == null ? new Recon() : cell.recon.dup() - ); - - String cellDescription = - "single cell on row " + (rowIndex + 1) + - ", column " + column.getHeaderLabel() + - ", containing \"" + cell.value + "\""; - - String description = null; - if (judgment == Judgment.None) { - newCell.recon.judgment = Recon.Judgment.None; - newCell.recon.match = null; - description = "Discard recon judgment for " + cellDescription; - } else if (judgment == Judgment.New) { - newCell.recon.judgment = Recon.Judgment.New; - description = "Mark to create new topic for " + cellDescription; - } else { - newCell.recon.judgment = Recon.Judgment.Matched; - newCell.recon.match = this.match; - - description = "Match " + this.match.topicName + - " (" + match.topicID + ") to " + - cellDescription; - } - - ReconStats stats = column.getReconStats(); - if (stats == null) { - stats = ReconStats.create(_project, cellIndex); - } else { - int newChange = 0; - int matchChange = 0; - - if (oldJudgment == Judgment.New) { - newChange--; - } - if (oldJudgment == Judgment.Matched) { - matchChange--; - } - if (newCell.recon.judgment == Judgment.New) { - newChange++; - } - if (newCell.recon.judgment == Judgment.Matched) { - matchChange++; - } - - stats = new ReconStats( - stats.nonBlanks, - stats.newTopics + newChange, - stats.matchedTopics + matchChange); - } - - Change change = new ReconChange( - new CellChange(rowIndex, cellIndex, cell, newCell), - column.getHeaderLabel(), - column.getReconConfig(), - stats - ); - - return new HistoryEntry( - _project, description, null, change); - } - } + throw new Exception("Cell is blank or error"); + } + + Column column = _project.columnModel.getColumnByCellIndex(cellIndex); + if (column == null) { + throw new Exception("No such column"); + } + + Judgment oldJudgment = cell.recon == null ? Judgment.None : cell.recon.judgment; + + newCell = new Cell( + cell.value, + cell.recon == null ? new Recon() : cell.recon.dup() + ); + + String cellDescription = + "single cell on row " + (rowIndex + 1) + + ", column " + column.getHeaderLabel() + + ", containing \"" + cell.value + "\""; + + String description = null; + if (judgment == Judgment.None) { + newCell.recon.judgment = Recon.Judgment.None; + newCell.recon.match = null; + description = "Discard recon judgment for " + cellDescription; + } else if (judgment == Judgment.New) { + newCell.recon.judgment = Recon.Judgment.New; + description = "Mark to create new topic for " + cellDescription; + } else { + newCell.recon.judgment = Recon.Judgment.Matched; + newCell.recon.match = this.match; + + description = "Match " + this.match.topicName + + " (" + match.topicID + ") to " + + cellDescription; + } + + ReconStats stats = column.getReconStats(); + if (stats == null) { + stats = ReconStats.create(_project, cellIndex); + } else { + int newChange = 0; + int matchChange = 0; + + if (oldJudgment == Judgment.New) { + newChange--; + } + if (oldJudgment == Judgment.Matched) { + matchChange--; + } + if (newCell.recon.judgment == Judgment.New) { + newChange++; + } + if (newCell.recon.judgment == Judgment.Matched) { + matchChange++; + } + + stats = new ReconStats( + stats.nonBlanks, + stats.newTopics + newChange, + stats.matchedTopics + matchChange); + } + + Change change = new ReconChange( + new CellChange(rowIndex, cellIndex, cell, newCell), + column.getHeaderLabel(), + column.getReconConfig(), + stats + ); + + return new HistoryEntry( + _project, description, null, change); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeSimilarCellsCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeSimilarCellsCommand.java index 7eeef1e57..2e3475bb2 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeSimilarCellsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconJudgeSimilarCellsCommand.java @@ -13,37 +13,37 @@ import com.metaweb.gridworks.operations.ReconJudgeSimilarCellsOperation; public class ReconJudgeSimilarCellsCommand extends EngineDependentCommand { - @Override - protected AbstractOperation createOperation( - HttpServletRequest request, JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - String similarValue = request.getParameter("similarValue"); - Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment")); - - ReconCandidate match = null; - String topicID = request.getParameter("topicID"); - if (topicID != null) { - String scoreString = request.getParameter("score"); - - match = new ReconCandidate( - topicID, - request.getParameter("topicGUID"), - request.getParameter("topicName"), - request.getParameter("types").split(","), - scoreString != null ? Double.parseDouble(scoreString) : 100 - ); - } - - String shareNewTopics = request.getParameter("shareNewTopics"); - - return new ReconJudgeSimilarCellsOperation( - engineConfig, - columnName, - similarValue, - judgment, - match, - "true".equals(shareNewTopics) - ); - } + @Override + protected AbstractOperation createOperation( + HttpServletRequest request, JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + String similarValue = request.getParameter("similarValue"); + Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment")); + + ReconCandidate match = null; + String topicID = request.getParameter("topicID"); + if (topicID != null) { + String scoreString = request.getParameter("score"); + + match = new ReconCandidate( + topicID, + request.getParameter("topicGUID"), + request.getParameter("topicName"), + request.getParameter("types").split(","), + scoreString != null ? Double.parseDouble(scoreString) : 100 + ); + } + + String shareNewTopics = request.getParameter("shareNewTopics"); + + return new ReconJudgeSimilarCellsOperation( + engineConfig, + columnName, + similarValue, + judgment, + match, + "true".equals(shareNewTopics) + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMarkNewTopicsCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMarkNewTopicsCommand.java index c9fe69781..5913ba6fa 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMarkNewTopicsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMarkNewTopicsCommand.java @@ -9,15 +9,15 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.ReconMarkNewTopicsOperation; public class ReconMarkNewTopicsCommand extends EngineDependentCommand { - - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - return new ReconMarkNewTopicsOperation( - engineConfig, - request.getParameter("columnName"), - "true".equals(request.getParameter("shareNewTopics")) - ); - } + + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + return new ReconMarkNewTopicsOperation( + engineConfig, + request.getParameter("columnName"), + "true".equals(request.getParameter("shareNewTopics")) + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchBestCandidatesCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchBestCandidatesCommand.java index cf822e8d1..d7a8b6d48 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchBestCandidatesCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchBestCandidatesCommand.java @@ -9,13 +9,13 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.operations.ReconMatchBestCandidatesOperation; public class ReconMatchBestCandidatesCommand extends EngineDependentCommand { - - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - - return new ReconMatchBestCandidatesOperation(engineConfig, columnName); - } + + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + + return new ReconMatchBestCandidatesOperation(engineConfig, columnName); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchSpecificTopicCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchSpecificTopicCommand.java index 0493e48b6..7b5df7676 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchSpecificTopicCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconMatchSpecificTopicCommand.java @@ -10,20 +10,20 @@ import com.metaweb.gridworks.model.ReconCandidate; import com.metaweb.gridworks.operations.ReconMatchSpecificTopicOperation; public class ReconMatchSpecificTopicCommand extends EngineDependentCommand { - - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - ReconCandidate match = new ReconCandidate( - request.getParameter("topicID"), - request.getParameter("topicGUID"), - request.getParameter("topicName"), - request.getParameter("types").split(","), - 100 - ); - - return new ReconMatchSpecificTopicOperation(engineConfig, columnName, match); - } + + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + ReconCandidate match = new ReconCandidate( + request.getParameter("topicID"), + request.getParameter("topicGUID"), + request.getParameter("topicName"), + request.getParameter("types").split(","), + 100 + ); + + return new ReconMatchSpecificTopicOperation(engineConfig, columnName, match); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/recon/ReconcileCommand.java b/src/main/java/com/metaweb/gridworks/commands/recon/ReconcileCommand.java index a1f67cf9a..22a116a1a 100644 --- a/src/main/java/com/metaweb/gridworks/commands/recon/ReconcileCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/recon/ReconcileCommand.java @@ -11,17 +11,17 @@ import com.metaweb.gridworks.model.recon.ReconConfig; import com.metaweb.gridworks.operations.ReconOperation; public class ReconcileCommand extends EngineDependentCommand { - - @Override - protected AbstractOperation createOperation(HttpServletRequest request, - JSONObject engineConfig) throws Exception { - - String columnName = request.getParameter("columnName"); - String configString = request.getParameter("config"); - - JSONTokener t = new JSONTokener(configString); - JSONObject config = (JSONObject) t.nextValue(); - - return new ReconOperation(engineConfig, columnName, ReconConfig.reconstruct(config)); - } + + @Override + protected AbstractOperation createOperation(HttpServletRequest request, + JSONObject engineConfig) throws Exception { + + String columnName = request.getParameter("columnName"); + String configString = request.getParameter("config"); + + JSONTokener t = new JSONTokener(configString); + JSONObject config = (JSONObject) t.nextValue(); + + return new ReconOperation(engineConfig, columnName, ReconConfig.reconstruct(config)); + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/util/CancelProcessesCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/CancelProcessesCommand.java index f1906c63e..1f0417211 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/CancelProcessesCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/CancelProcessesCommand.java @@ -10,20 +10,20 @@ import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.model.Project; public class CancelProcessesCommand extends Command { - - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { Project project = getProject(request); project.processManager.cancelAll(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); response.getWriter().write("{ \"code\" : \"ok\" }"); - } catch (Exception e) { - respondException(response, e); - } - } + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/util/GetExpressionLanguageInfoCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/GetExpressionLanguageInfoCommand.java index 34eca1e31..53abd67c5 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/GetExpressionLanguageInfoCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/GetExpressionLanguageInfoCommand.java @@ -16,43 +16,43 @@ import com.metaweb.gridworks.gel.ControlFunctionRegistry; import com.metaweb.gridworks.gel.Function; public class GetExpressionLanguageInfoCommand extends Command { - - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - Properties options = new Properties(); - - writer.object(); - - writer.key("functions"); - writer.object(); - { - for (Entry entry : ControlFunctionRegistry.getFunctionMapping()) { - writer.key(entry.getKey()); - entry.getValue().write(writer, options); - } - } - writer.endObject(); - + + JSONWriter writer = new JSONWriter(response.getWriter()); + Properties options = new Properties(); + + writer.object(); + + writer.key("functions"); + writer.object(); + { + for (Entry entry : ControlFunctionRegistry.getFunctionMapping()) { + writer.key(entry.getKey()); + entry.getValue().write(writer, options); + } + } + writer.endObject(); + writer.key("controls"); writer.object(); { for (Entry entry : ControlFunctionRegistry.getControlMapping()) { writer.key(entry.getKey()); - entry.getValue().write(writer, options); + entry.getValue().write(writer, options); } } writer.endObject(); - writer.endObject(); - } catch (Exception e) { - respondException(response, e); - } - } + writer.endObject(); + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/util/GuessTypesOfColumnCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/GuessTypesOfColumnCommand.java index aaecfd495..186c299b0 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/GuessTypesOfColumnCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/GuessTypesOfColumnCommand.java @@ -30,181 +30,181 @@ import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.util.ParsingUtilities; public class GuessTypesOfColumnCommand extends Command { - - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - String columnName = request.getParameter("columnName"); - + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + String columnName = request.getParameter("columnName"); + response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); - - JSONWriter writer = new JSONWriter(response.getWriter()); - writer.object(); - - Column column = project.columnModel.getColumnByName(columnName); - if (column == null) { - writer.key("code"); writer.value("error"); - writer.key("message"); writer.value("No such column"); - } else { - try { - writer.key("code"); writer.value("ok"); - writer.key("types"); writer.array(); - - List typeGroups = guessTypes(project, column); - for (TypeGroup tg : typeGroups) { - writer.object(); - writer.key("id"); writer.value(tg.id); - writer.key("name"); writer.value(tg.name); + + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); + + Column column = project.columnModel.getColumnByName(columnName); + if (column == null) { + writer.key("code"); writer.value("error"); + writer.key("message"); writer.value("No such column"); + } else { + try { + writer.key("code"); writer.value("ok"); + writer.key("types"); writer.array(); + + List typeGroups = guessTypes(project, column); + for (TypeGroup tg : typeGroups) { + writer.object(); + writer.key("id"); writer.value(tg.id); + writer.key("name"); writer.value(tg.name); writer.key("score"); writer.value(tg.score); writer.key("count"); writer.value(tg.count); - writer.endObject(); - } - - writer.endArray(); - } catch (Exception e) { - writer.key("code"); writer.value("error"); - } - } - - writer.endObject(); - } catch (Exception e) { - respondException(response, e); - } - } - + writer.endObject(); + } + + writer.endArray(); + } catch (Exception e) { + writer.key("code"); writer.value("error"); + } + } + + writer.endObject(); + } catch (Exception e) { + respondException(response, e); + } + } + final static int s_sampleSize = 20; - protected List guessTypes(Project project, Column column) { - Map map = new HashMap(); - - int cellIndex = column.getCellIndex(); - - List samples = new ArrayList(s_sampleSize); - Set sampleSet = new HashSet(); - - for (Row row : project.rows) { - Object value = row.getCellValue(cellIndex); - if (ExpressionUtils.isNonBlankData(value)) { - String s = value.toString().trim(); - if (!sampleSet.contains(s)) { - samples.add(s); - sampleSet.add(s); - if (samples.size() >= s_sampleSize) { - break; - } - } - } - } - - try { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - for (int i = 0; i < samples.size(); i++) { - jsonWriter.key("q" + i + ":search"); - jsonWriter.object(); - - jsonWriter.key("query"); jsonWriter.value(samples.get(i)); - jsonWriter.key("limit"); jsonWriter.value(3); - - jsonWriter.endObject(); - } - jsonWriter.endObject(); - - StringBuffer sb = new StringBuffer(); - sb.append("http://api.freebase.com/api/service/search?indent=1&queries="); - sb.append(ParsingUtilities.encode(stringWriter.toString())); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - - for (int i = 0; i < samples.size(); i++) { - String key = "q" + i + ":search"; - if (!o.has(key)) { - continue; - } - - JSONObject o2 = o.getJSONObject(key); - if (!(o2.has("result"))) { - continue; - } - - JSONArray results = o2.getJSONArray("result"); - int count = results.length(); - - for (int j = 0; j < count; j++) { - JSONObject result = results.getJSONObject(j); - double score = 1.0 / (1 + j); //result.getDouble("relevance:score"); - - JSONArray types = result.getJSONArray("type"); - int typeCount = types.length(); - - for (int t = 0; t < typeCount; t++) { - JSONObject type = types.getJSONObject(t); - String id = type.getString("id"); - if (id.equals("/common/topic") || - id.equals("/base/ontologies/ontology_instance") || - (id.startsWith("/base/") && id.endsWith("/topic")) || - id.startsWith("/user/") || - id.startsWith("/freebase/") - ) { - continue; - } - - if (map.containsKey(id)) { - TypeGroup tg = map.get(id); - tg.score += score; - tg.count++; - } else { - map.put(id, new TypeGroup(id, type.getString("name"), score)); - } - } - } - } - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - List types = new ArrayList(map.values()); - Collections.sort(types, new Comparator() { - public int compare(TypeGroup o1, TypeGroup o2) { - int c = Math.min(s_sampleSize, o2.count) - Math.min(s_sampleSize, o1.count); - if (c != 0) { - return c; - } - return (int) Math.signum(o2.score / o2.count - o1.score / o1.count); - } - }); - - return types; - } - - static protected class TypeGroup { - String id; - String name; - int count; - double score; - - TypeGroup(String id, String name, double score) { - this.id = id; - this.name = name; - this.score = score; - this.count = 1; - } - } + protected List guessTypes(Project project, Column column) { + Map map = new HashMap(); + + int cellIndex = column.getCellIndex(); + + List samples = new ArrayList(s_sampleSize); + Set sampleSet = new HashSet(); + + for (Row row : project.rows) { + Object value = row.getCellValue(cellIndex); + if (ExpressionUtils.isNonBlankData(value)) { + String s = value.toString().trim(); + if (!sampleSet.contains(s)) { + samples.add(s); + sampleSet.add(s); + if (samples.size() >= s_sampleSize) { + break; + } + } + } + } + + try { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + for (int i = 0; i < samples.size(); i++) { + jsonWriter.key("q" + i + ":search"); + jsonWriter.object(); + + jsonWriter.key("query"); jsonWriter.value(samples.get(i)); + jsonWriter.key("limit"); jsonWriter.value(3); + + jsonWriter.endObject(); + } + jsonWriter.endObject(); + + StringBuffer sb = new StringBuffer(); + sb.append("http://api.freebase.com/api/service/search?indent=1&queries="); + sb.append(ParsingUtilities.encode(stringWriter.toString())); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); + + for (int i = 0; i < samples.size(); i++) { + String key = "q" + i + ":search"; + if (!o.has(key)) { + continue; + } + + JSONObject o2 = o.getJSONObject(key); + if (!(o2.has("result"))) { + continue; + } + + JSONArray results = o2.getJSONArray("result"); + int count = results.length(); + + for (int j = 0; j < count; j++) { + JSONObject result = results.getJSONObject(j); + double score = 1.0 / (1 + j); //result.getDouble("relevance:score"); + + JSONArray types = result.getJSONArray("type"); + int typeCount = types.length(); + + for (int t = 0; t < typeCount; t++) { + JSONObject type = types.getJSONObject(t); + String id = type.getString("id"); + if (id.equals("/common/topic") || + id.equals("/base/ontologies/ontology_instance") || + (id.startsWith("/base/") && id.endsWith("/topic")) || + id.startsWith("/user/") || + id.startsWith("/freebase/") + ) { + continue; + } + + if (map.containsKey(id)) { + TypeGroup tg = map.get(id); + tg.score += score; + tg.count++; + } else { + map.put(id, new TypeGroup(id, type.getString("name"), score)); + } + } + } + } + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + List types = new ArrayList(map.values()); + Collections.sort(types, new Comparator() { + public int compare(TypeGroup o1, TypeGroup o2) { + int c = Math.min(s_sampleSize, o2.count) - Math.min(s_sampleSize, o1.count); + if (c != 0) { + return c; + } + return (int) Math.signum(o2.score / o2.count - o1.score / o1.count); + } + }); + + return types; + } + + static protected class TypeGroup { + String id; + String name; + int count; + double score; + + TypeGroup(String id, String name, double score) { + this.id = id; + this.name = name; + this.score = score; + this.count = 1; + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java index fdd599b58..171b3ebe1 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/PreviewExpressionCommand.java @@ -22,81 +22,81 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class PreviewExpressionCommand extends Command { - - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - - int cellIndex = Integer.parseInt(request.getParameter("cellIndex")); - - String expression = request.getParameter("expression"); - String rowIndicesString = request.getParameter("rowIndices"); - if (rowIndicesString == null) { - respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }"); - return; - } - + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + int cellIndex = Integer.parseInt(request.getParameter("cellIndex")); + + String expression = request.getParameter("expression"); + String rowIndicesString = request.getParameter("rowIndices"); + if (rowIndicesString == null) { + respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }"); + return; + } + response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); - - JSONArray rowIndices = jsonStringToArray(rowIndicesString); - int length = rowIndices.length(); - - JSONWriter writer = new JSONWriter(response.getWriter()); - writer.object(); - - try { - Evaluable eval = MetaParser.parse(expression); - - writer.key("code"); writer.value("ok"); - writer.key("results"); writer.array(); - - Properties bindings = ExpressionUtils.createBindings(project); - for (int i = 0; i < length; i++) { - Object result = null; - - int rowIndex = rowIndices.getInt(i); - if (rowIndex >= 0 && rowIndex < project.rows.size()) { - Row row = project.rows.get(rowIndex); - Cell cell = row.getCell(cellIndex); - - ExpressionUtils.bind(bindings, row, rowIndex, cell); - - try { - result = eval.evaluate(bindings); - } catch (Exception e) { - // ignore - } - } - - if (ExpressionUtils.isError(result)) { - writer.object(); - writer.key("message"); writer.value(((EvalError) result).message); - writer.endObject(); - } else { - if (result != null && result instanceof HasFields) { - result = "[object " + result.getClass().getSimpleName() + "]"; - } - writer.value(result); - } - } - writer.endArray(); - } catch (ParsingException e) { + + JSONArray rowIndices = jsonStringToArray(rowIndicesString); + int length = rowIndices.length(); + + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); + + try { + Evaluable eval = MetaParser.parse(expression); + + writer.key("code"); writer.value("ok"); + writer.key("results"); writer.array(); + + Properties bindings = ExpressionUtils.createBindings(project); + for (int i = 0; i < length; i++) { + Object result = null; + + int rowIndex = rowIndices.getInt(i); + if (rowIndex >= 0 && rowIndex < project.rows.size()) { + Row row = project.rows.get(rowIndex); + Cell cell = row.getCell(cellIndex); + + ExpressionUtils.bind(bindings, row, rowIndex, cell); + + try { + result = eval.evaluate(bindings); + } catch (Exception e) { + // ignore + } + } + + if (ExpressionUtils.isError(result)) { + writer.object(); + writer.key("message"); writer.value(((EvalError) result).message); + writer.endObject(); + } else { + if (result != null && result instanceof HasFields) { + result = "[object " + result.getClass().getSimpleName() + "]"; + } + writer.value(result); + } + } + writer.endArray(); + } catch (ParsingException e) { writer.key("code"); writer.value("error"); writer.key("type"); writer.value("parser"); writer.key("message"); writer.value(e.getMessage()); - } catch (Exception e) { - writer.key("code"); writer.value("error"); + } catch (Exception e) { + writer.key("code"); writer.value("error"); writer.key("type"); writer.value("other"); writer.key("message"); writer.value(e.getMessage()); - } - - writer.endObject(); - } catch (Exception e) { - respondException(response, e); - } - } + } + + writer.endObject(); + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/commands/util/PreviewProtographCommand.java b/src/main/java/com/metaweb/gridworks/commands/util/PreviewProtographCommand.java index dd844ab61..7669132ae 100644 --- a/src/main/java/com/metaweb/gridworks/commands/util/PreviewProtographCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/util/PreviewProtographCommand.java @@ -17,48 +17,48 @@ import com.metaweb.gridworks.protograph.transpose.Transposer; import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory; public class PreviewProtographCommand extends Command { - @Override - public void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - try { - Project project = getProject(request); - + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", "application/json"); - - String jsonString = request.getParameter("protograph"); - JSONObject json = jsonStringToObject(jsonString); - Protograph protograph = Protograph.reconstruct(json); - - StringBuffer sb = new StringBuffer(); - sb.append("{ "); - - { - TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory(); - - Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory); - - sb.append("\"tripleloader\" : "); - sb.append(JSONObject.quote(nodeFactory.getLoad())); - } - - { - MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory(); - - Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory); - - JSONArray results = nodeFactory.getJSON(); - - sb.append(", \"mqllike\" : "); - sb.append(results.toString()); - } + + String jsonString = request.getParameter("protograph"); + JSONObject json = jsonStringToObject(jsonString); + Protograph protograph = Protograph.reconstruct(json); + + StringBuffer sb = new StringBuffer(); + sb.append("{ "); + + { + TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory(); + + Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory); + + sb.append("\"tripleloader\" : "); + sb.append(JSONObject.quote(nodeFactory.getLoad())); + } + + { + MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory(); + + Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory); + + JSONArray results = nodeFactory.getJSON(); + + sb.append(", \"mqllike\" : "); + sb.append(results.toString()); + } - sb.append(" }"); - - respond(response, sb.toString()); - } catch (Exception e) { - respondException(response, e); - } - } + sb.append(" }"); + + respond(response, sb.toString()); + } catch (Exception e) { + respondException(response, e); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/Evaluable.java b/src/main/java/com/metaweb/gridworks/expr/Evaluable.java index 27267bdaf..1984e66fd 100644 --- a/src/main/java/com/metaweb/gridworks/expr/Evaluable.java +++ b/src/main/java/com/metaweb/gridworks/expr/Evaluable.java @@ -3,5 +3,5 @@ package com.metaweb.gridworks.expr; import java.util.Properties; public interface Evaluable { - public Object evaluate(Properties bindings); + public Object evaluate(Properties bindings); } diff --git a/src/main/java/com/metaweb/gridworks/expr/HasFields.java b/src/main/java/com/metaweb/gridworks/expr/HasFields.java index 42de4cd6b..2267413a7 100644 --- a/src/main/java/com/metaweb/gridworks/expr/HasFields.java +++ b/src/main/java/com/metaweb/gridworks/expr/HasFields.java @@ -3,5 +3,5 @@ package com.metaweb.gridworks.expr; import java.util.Properties; public interface HasFields { - public Object getField(String name, Properties bindings); + public Object getField(String name, Properties bindings); } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/Get.java b/src/main/java/com/metaweb/gridworks/expr/functions/Get.java index e0d623884..f89d95e42 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/Get.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/Get.java @@ -10,85 +10,85 @@ import com.metaweb.gridworks.gel.Function; public class Get implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length > 1 && args.length <= 3) { - Object v = args[0]; - Object from = args[1]; - Object to = args.length == 3 ? args[2] : null; - - if (v != null && from != null) { - if (v instanceof HasFields) { - if (from instanceof String) { - return ((HasFields) v).getField((String) from, bindings); - } - } else { - if (from instanceof Number && (to == null || to instanceof Number)) { - if (v.getClass().isArray()) { - Object[] a = (Object[]) v; - int start = ((Number) from).intValue(); - if (start < 0) { - start = a.length + start; - } - start = Math.min(a.length, Math.max(0, start)); - - if (to == null) { - return start < a.length ? a[start] : null; - } else { - int end = to != null && to instanceof Number ? - ((Number) to).intValue() : a.length; - - if (end < 0) { - end = a.length + end; - } - end = Math.min(a.length, Math.max(start, end)); - - if (end > start) { + public Object call(Properties bindings, Object[] args) { + if (args.length > 1 && args.length <= 3) { + Object v = args[0]; + Object from = args[1]; + Object to = args.length == 3 ? args[2] : null; + + if (v != null && from != null) { + if (v instanceof HasFields) { + if (from instanceof String) { + return ((HasFields) v).getField((String) from, bindings); + } + } else { + if (from instanceof Number && (to == null || to instanceof Number)) { + if (v.getClass().isArray()) { + Object[] a = (Object[]) v; + int start = ((Number) from).intValue(); + if (start < 0) { + start = a.length + start; + } + start = Math.min(a.length, Math.max(0, start)); + + if (to == null) { + return start < a.length ? a[start] : null; + } else { + int end = to != null && to instanceof Number ? + ((Number) to).intValue() : a.length; + + if (end < 0) { + end = a.length + end; + } + end = Math.min(a.length, Math.max(start, end)); + + if (end > start) { Object[] a2 = new Object[end - start]; - System.arraycopy(a, start, a2, 0, end - start); - + System.arraycopy(a, start, a2, 0, end - start); + return a2; - } - } - } else { - String s = (v instanceof String ? (String) v : v.toString()); - - int start = ((Number) from).intValue(); - if (start < 0) { - start = s.length() + start; - } - start = Math.min(s.length(), Math.max(0, start)); - - if (to != null && to instanceof Number) { - int end = ((Number) to).intValue(); - if (end < 0) { - end = s.length() + end; - } - end = Math.min(s.length(), Math.max(start, end)); - - return s.substring(start, end); - } else { - return s.substring(start, start + 1); - } - } - } - } - } - } - return null; - } + } + } + } else { + String s = (v instanceof String ? (String) v : v.toString()); + + int start = ((Number) from).intValue(); + if (start < 0) { + start = s.length() + start; + } + start = Math.min(s.length(), Math.max(0, start)); + + if (to != null && to instanceof Number) { + int end = ((Number) to).intValue(); + if (end < 0) { + end = s.length() + end; + } + end = Math.min(s.length(), Math.max(start, end)); + + return s.substring(start, end); + } else { + return s.substring(start, start + 1); + } + } + } + } + } + } + return null; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "If o has fields, returns the field named 'from' of o. " + - "If o is an array, returns o[from, to]. " + - "if o is a string, returns o.substring(from, to)" - ); - writer.key("params"); writer.value("o, number or string from, optional number to"); - writer.key("returns"); writer.value("Depends on actual arguments"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "If o has fields, returns the field named 'from' of o. " + + "If o is an array, returns o[from, to]. " + + "if o is a string, returns o.substring(from, to)" + ); + writer.key("params"); writer.value("o, number or string from, optional number to"); + writer.key("returns"); writer.value("Depends on actual arguments"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/Length.java b/src/main/java/com/metaweb/gridworks/expr/functions/Length.java index 43cf1f50e..ca06c256c 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/Length.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/Length.java @@ -11,30 +11,30 @@ import com.metaweb.gridworks.gel.Function; public class Length implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - Object v = args[0]; - - if (v != null) { - if (v.getClass().isArray()) { - Object[] a = (Object[]) v; - return a.length; - } else { - String s = (v instanceof String ? (String) v : v.toString()); - return s.length(); - } - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + Object v = args[0]; + + if (v != null) { + if (v.getClass().isArray()) { + Object[] a = (Object[]) v; + return a.length; + } else { + String s = (v instanceof String ? (String) v : v.toString()); + return s.length(); + } + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array or a string"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the length of o"); - writer.key("params"); writer.value("array or string o"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the length of o"); + writer.key("params"); writer.value("array or string o"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/Slice.java b/src/main/java/com/metaweb/gridworks/expr/functions/Slice.java index d02d4d7b9..ce39880fe 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/Slice.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/Slice.java @@ -9,69 +9,69 @@ import com.metaweb.gridworks.gel.Function; public class Slice implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length > 1 && args.length <= 3) { - Object v = args[0]; - Object from = args[1]; - Object to = args.length == 3 ? args[2] : null; - - if (v != null && from != null && from instanceof Number && (to == null || to instanceof Number)) { - if (v.getClass().isArray()) { - Object[] a = (Object[]) v; - int start = ((Number) from).intValue(); - int end = to != null && to instanceof Number ? - ((Number) to).intValue() : a.length; - - if (start < 0) { - start = a.length + start; - } - start = Math.min(a.length, Math.max(0, start)); - - if (end < 0) { - end = a.length + end; - } - end = Math.min(a.length, Math.max(start, end)); - - Object[] a2 = new Object[end - start]; - System.arraycopy(a, start, a2, 0, end - start); - - return a2; - } else { - String s = (v instanceof String ? (String) v : v.toString()); - - int start = ((Number) from).intValue(); - if (start < 0) { - start = s.length() + start; - } - start = Math.min(s.length(), Math.max(0, start)); - - if (to != null && to instanceof Number) { - int end = ((Number) to).intValue(); - if (end < 0) { - end = s.length() + end; - } - end = Math.min(s.length(), Math.max(start, end)); - - return s.substring(start, end); - } else { - return s.substring(start); - } - } - } - } - return null; - } + public Object call(Properties bindings, Object[] args) { + if (args.length > 1 && args.length <= 3) { + Object v = args[0]; + Object from = args[1]; + Object to = args.length == 3 ? args[2] : null; + + if (v != null && from != null && from instanceof Number && (to == null || to instanceof Number)) { + if (v.getClass().isArray()) { + Object[] a = (Object[]) v; + int start = ((Number) from).intValue(); + int end = to != null && to instanceof Number ? + ((Number) to).intValue() : a.length; + + if (start < 0) { + start = a.length + start; + } + start = Math.min(a.length, Math.max(0, start)); + + if (end < 0) { + end = a.length + end; + } + end = Math.min(a.length, Math.max(start, end)); + + Object[] a2 = new Object[end - start]; + System.arraycopy(a, start, a2, 0, end - start); + + return a2; + } else { + String s = (v instanceof String ? (String) v : v.toString()); + + int start = ((Number) from).intValue(); + if (start < 0) { + start = s.length() + start; + } + start = Math.min(s.length(), Math.max(0, start)); + + if (to != null && to instanceof Number) { + int end = ((Number) to).intValue(); + if (end < 0) { + end = s.length() + end; + } + end = Math.min(s.length(), Math.max(start, end)); + + return s.substring(start, end); + } else { + return s.substring(start); + } + } + } + } + return null; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "If o is an array, returns o[from, to]. " + - "if o is a string, returns o.substring(from, to)" - ); - writer.key("params"); writer.value("o, number from, optional number to"); - writer.key("returns"); writer.value("Depends on actual arguments"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "If o is an array, returns o[from, to]. " + + "if o is a string, returns o.substring(from, to)" + ); + writer.key("params"); writer.value("o, number from, optional number to"); + writer.key("returns"); writer.value("Depends on actual arguments"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java b/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java index e46203862..2afc70685 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/ToDate.java @@ -11,7 +11,7 @@ import com.metaweb.gridworks.gel.Function; public class ToDate implements Function { - public Object call(Properties bindings, Object[] args) { + public Object call(Properties bindings, Object[] args) { if (args.length == 1 || args.length == 2) { Object o1 = args[0]; if (o1 != null && o1 instanceof String) { @@ -28,18 +28,18 @@ public class ToDate implements Function { // do something about } } - } - return null; - } + } + return null; + } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns o converted to a date object"); - writer.key("params"); writer.value("o, boolean month_first (optional)"); - writer.key("returns"); writer.value("date"); - writer.endObject(); - } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns o converted to a date object"); + writer.key("params"); writer.value("o, boolean month_first (optional)"); + writer.key("returns"); writer.value("date"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/ToNumber.java b/src/main/java/com/metaweb/gridworks/expr/functions/ToNumber.java index 9dc48c3e0..94d65dbd7 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/ToNumber.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/ToNumber.java @@ -9,21 +9,21 @@ import com.metaweb.gridworks.gel.Function; public class ToNumber implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - return args[0] instanceof Number ? args[0] : Double.parseDouble(args[0].toString()); - } - return null; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns o converted to a number"); - writer.key("params"); writer.value("o"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + return args[0] instanceof Number ? args[0] : Double.parseDouble(args[0].toString()); + } + return null; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns o converted to a number"); + writer.key("params"); writer.value("o"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/ToString.java b/src/main/java/com/metaweb/gridworks/expr/functions/ToString.java index 51c7b4d6b..5f95be839 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/ToString.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/ToString.java @@ -11,8 +11,8 @@ import com.metaweb.gridworks.gel.Function; public class ToString implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length >= 1) { + public Object call(Properties bindings, Object[] args) { + if (args.length >= 1) { Object o1 = args[0]; if (o1 != null) { if (o1 instanceof Calendar) { @@ -27,18 +27,18 @@ public class ToString implements Function { return (o1 instanceof String) ? o1 : o1.toString(); } } - } - return null; - } + } + return null; + } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns o converted to a string"); - writer.key("params"); writer.value("o, string format (optional)"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns o converted to a string"); + writer.key("params"); writer.value("o, string format (optional)"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Join.java b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Join.java index 823dd1445..aaef86d50 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Join.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Join.java @@ -11,40 +11,40 @@ import com.metaweb.gridworks.gel.Function; public class Join implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object v = args[0]; - Object s = args[1]; - - if (v != null && v.getClass().isArray() && - s != null && s instanceof String) { - - Object[] a = (Object[]) v; - String separator = (String) s; - - StringBuffer sb = new StringBuffer(); - for (Object o : a) { - if (o != null) { - if (sb.length() > 0) { - sb.append(separator); - } - sb.append(o.toString()); - } - } - - return sb.toString(); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object v = args[0]; + Object s = args[1]; + + if (v != null && v.getClass().isArray() && + s != null && s instanceof String) { + + Object[] a = (Object[]) v; + String separator = (String) s; + + StringBuffer sb = new StringBuffer(); + for (Object o : a) { + if (o != null) { + if (sb.length() > 0) { + sb.append(separator); + } + sb.append(o.toString()); + } + } + + return sb.toString(); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array and a string"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the string obtained by joining the array a with the separator sep"); - writer.key("params"); writer.value("array a, string sep"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the string obtained by joining the array a with the separator sep"); + writer.key("params"); writer.value("array a, string sep"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Reverse.java b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Reverse.java index 03ca446b1..32cd0b964 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Reverse.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Reverse.java @@ -11,31 +11,31 @@ import com.metaweb.gridworks.gel.Function; public class Reverse implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - Object v = args[0]; - - if (v != null && v.getClass().isArray()) { - Object[] a = (Object[]) v; - Object[] r = new Object[a.length]; - - for (int i = 0; i < a.length; i++) { - r[i] = a[r.length - i - 1]; - } - - return r; - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + Object v = args[0]; + + if (v != null && v.getClass().isArray()) { + Object[] a = (Object[]) v; + Object[] r = new Object[a.length]; + + for (int i = 0; i < a.length; i++) { + r[i] = a[r.length - i - 1]; + } + + return r; + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Reverses array a"); - writer.key("params"); writer.value("array a"); - writer.key("returns"); writer.value("array"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Reverses array a"); + writer.key("params"); writer.value("array a"); + writer.key("returns"); writer.value("array"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Sort.java b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Sort.java index a300167dd..3bf4db666 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Sort.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/arrays/Sort.java @@ -12,30 +12,30 @@ import com.metaweb.gridworks.gel.Function; public class Sort implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - Object v = args[0]; - - - if (v != null && v.getClass().isArray()) { - Object[] a = (Object[]) v; - Object[] r = a.clone(); - - Arrays.sort(r, 0, r.length); - - return r; - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + Object v = args[0]; + + + if (v != null && v.getClass().isArray()) { + Object[] a = (Object[]) v; + Object[] r = a.clone(); + + Arrays.sort(r, 0, r.length); + + return r; + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Sorts array a"); - writer.key("params"); writer.value("array a"); - writer.key("returns"); writer.value("array"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Sorts array a"); + writer.key("params"); writer.value("array a"); + writer.key("returns"); writer.value("array"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/And.java b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/And.java index 3608e950c..a0256c140 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/And.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/And.java @@ -9,22 +9,22 @@ import com.metaweb.gridworks.gel.Function; public class And implements Function { - public Object call(Properties bindings, Object[] args) { - for (Object o : args) { - if (!Not.objectToBoolean(o)) { - return false; - } - } - return true; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - + public Object call(Properties bindings, Object[] args) { + for (Object o : args) { + if (!Not.objectToBoolean(o)) { + return false; + } + } + return true; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); writer.key("description"); writer.value("ANDs two boolean values"); writer.key("params"); writer.value("boolean a, boolean b"); writer.key("returns"); writer.value("boolean"); writer.endObject(); - } + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Not.java b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Not.java index 3820ee955..fd329c211 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Not.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Not.java @@ -11,25 +11,25 @@ import com.metaweb.gridworks.gel.Function; public class Not implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - return !objectToBoolean(args[0]); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + return !objectToBoolean(args[0]); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a boolean"); - } + } - public static boolean objectToBoolean(Object o) { - return o == null ? false : ( - (o instanceof Boolean) ? ((Boolean) o).booleanValue() : Boolean.parseBoolean(o.toString())); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the opposite of b"); - writer.key("params"); writer.value("boolean b"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } + public static boolean objectToBoolean(Object o) { + return o == null ? false : ( + (o instanceof Boolean) ? ((Boolean) o).booleanValue() : Boolean.parseBoolean(o.toString())); + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the opposite of b"); + writer.key("params"); writer.value("boolean b"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Or.java b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Or.java index 2f8a070ea..e5d9643c1 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Or.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/booleans/Or.java @@ -9,22 +9,22 @@ import com.metaweb.gridworks.gel.Function; public class Or implements Function { - public Object call(Properties bindings, Object[] args) { - for (Object o : args) { - if (Not.objectToBoolean(o)) { - return true; - } - } - return false; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns a OR b"); - writer.key("params"); writer.value("boolean a, boolean b"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } + public Object call(Properties bindings, Object[] args) { + for (Object o : args) { + if (Not.objectToBoolean(o)) { + return true; + } + } + return false; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns a OR b"); + writer.key("params"); writer.value("boolean a, boolean b"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Ceil.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Ceil.java index b7f83c016..b40873639 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Ceil.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Ceil.java @@ -11,20 +11,20 @@ import com.metaweb.gridworks.gel.Function; public class Ceil implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null && args[0] instanceof Number) { - return (long) Math.ceil(((Number) args[0]).doubleValue()); - } - return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null && args[0] instanceof Number) { + return (long) Math.ceil(((Number) args[0]).doubleValue()); + } + return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the ceiling of a number"); - writer.key("params"); writer.value("number d"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the ceiling of a number"); + writer.key("params"); writer.value("number d"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Exp.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Exp.java index 3af4b22e0..277f817e5 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Exp.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Exp.java @@ -9,20 +9,20 @@ import com.metaweb.gridworks.gel.Function; public class Exp implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] instanceof Number) { - return Math.exp(((Number) args[0]).doubleValue()); - } - return null; - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] instanceof Number) { + return Math.exp(((Number) args[0]).doubleValue()); + } + return null; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns e^n"); - writer.key("params"); writer.value("number n"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns e^n"); + writer.key("params"); writer.value("number n"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Floor.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Floor.java index 9d04f3928..6e1e9fec2 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Floor.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Floor.java @@ -11,21 +11,21 @@ import com.metaweb.gridworks.gel.Function; public class Floor implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null && args[0] instanceof Number) { - return (long) Math.floor(((Number) args[0]).doubleValue()); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null && args[0] instanceof Number) { + return (long) Math.floor(((Number) args[0]).doubleValue()); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the floor of a number"); - writer.key("params"); writer.value("number d"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the floor of a number"); + writer.key("params"); writer.value("number d"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Ln.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Ln.java index 3c9f8f585..c8f94be5a 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Ln.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Ln.java @@ -11,20 +11,20 @@ import com.metaweb.gridworks.gel.Function; public class Ln implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null && args[0] instanceof Number) { - return Math.log(((Number) args[0]).doubleValue()); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null && args[0] instanceof Number) { + return Math.log(((Number) args[0]).doubleValue()); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the natural log of n"); - writer.key("params"); writer.value("number n"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the natural log of n"); + writer.key("params"); writer.value("number n"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Log.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Log.java index c34a28242..de8e909ab 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Log.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Log.java @@ -11,20 +11,20 @@ import com.metaweb.gridworks.gel.Function; public class Log implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null && args[0] instanceof Number) { - return Math.log10(((Number) args[0]).doubleValue()); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null && args[0] instanceof Number) { + return Math.log10(((Number) args[0]).doubleValue()); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the base 10 log of n"); - writer.key("params"); writer.value("number n"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the base 10 log of n"); + writer.key("params"); writer.value("number n"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Max.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Max.java index 7d9dcabd3..c04f1b588 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Max.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Max.java @@ -11,24 +11,24 @@ import com.metaweb.gridworks.gel.Function; public class Max implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2 && - args[0] != null && args[0] instanceof Number && - args[1] != null && args[1] instanceof Number) { - return Math.max( - ((Number) args[0]).doubleValue(), - ((Number) args[1]).doubleValue()); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2 && + args[0] != null && args[0] instanceof Number && + args[1] != null && args[1] instanceof Number) { + return Math.max( + ((Number) args[0]).doubleValue(), + ((Number) args[1]).doubleValue()); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the greater of two numbers"); - writer.key("params"); writer.value("number a, number b"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the greater of two numbers"); + writer.key("params"); writer.value("number a, number b"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Min.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Min.java index e371372e2..22661f203 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Min.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Min.java @@ -11,24 +11,24 @@ import com.metaweb.gridworks.gel.Function; public class Min implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2 && - args[0] != null && args[0] instanceof Number && - args[1] != null && args[1] instanceof Number) { - return Math.min( - ((Number) args[0]).doubleValue(), - ((Number) args[1]).doubleValue()); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2 && + args[0] != null && args[0] instanceof Number && + args[1] != null && args[1] instanceof Number) { + return Math.min( + ((Number) args[0]).doubleValue(), + ((Number) args[1]).doubleValue()); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the smaller of two numbers"); - writer.key("params"); writer.value("number a, number b"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the smaller of two numbers"); + writer.key("params"); writer.value("number a, number b"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Mod.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Mod.java index d367e65c8..08909099b 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Mod.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Mod.java @@ -11,25 +11,25 @@ import com.metaweb.gridworks.gel.Function; public class Mod implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2 && - args[0] != null && args[0] instanceof Number && - args[1] != null && args[1] instanceof Number) { - int a = ((Number) args[0]).intValue(); - int b = ((Number) args[0]).intValue(); - - return a % b; - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2 && + args[0] != null && args[0] instanceof Number && + args[1] != null && args[1] instanceof Number) { + int a = ((Number) args[0]).intValue(); + int b = ((Number) args[0]).intValue(); + + return a % b; + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 numbers"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns a modulus b"); - writer.key("params"); writer.value("number a, number b"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns a modulus b"); + writer.key("params"); writer.value("number a, number b"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/math/Round.java b/src/main/java/com/metaweb/gridworks/expr/functions/math/Round.java index a2e927308..0b73bf1ea 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/math/Round.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/math/Round.java @@ -11,20 +11,20 @@ import com.metaweb.gridworks.gel.Function; public class Round implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null && args[0] instanceof Number) { - return ((Number) args[0]).longValue(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null && args[0] instanceof Number) { + return ((Number) args[0]).longValue(); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a number"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns n rounded"); - writer.key("params"); writer.value("number n"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns n rounded"); + writer.key("params"); writer.value("number n"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Contains.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Contains.java index 49f17c50e..1923fa5d1 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Contains.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Contains.java @@ -9,23 +9,23 @@ import com.metaweb.gridworks.gel.Function; public class Contains implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object s1 = args[0]; - Object s2 = args[1]; - if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { - return ((String) s1).indexOf((String) s2) > -1; - } - } - return null; - } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns whether s contains frag"); - writer.key("params"); writer.value("string s, string frag"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object s1 = args[0]; + Object s2 = args[1]; + if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { + return ((String) s1).indexOf((String) s2) > -1; + } + } + return null; + } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns whether s contains frag"); + writer.key("params"); writer.value("string s, string frag"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/EndsWith.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/EndsWith.java index 4ed10317c..de5d4e13d 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/EndsWith.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/EndsWith.java @@ -11,24 +11,24 @@ import com.metaweb.gridworks.gel.Function; public class EndsWith implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object s1 = args[0]; - Object s2 = args[1]; - if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { - return ((String) s1).endsWith((String) s2); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object s1 = args[0]; + Object s2 = args[1]; + if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { + return ((String) s1).endsWith((String) s2); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns whether s ends with sub"); - writer.key("params"); writer.value("string s, string sub"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns whether s ends with sub"); + writer.key("params"); writer.value("string s, string sub"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Fingerprint.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Fingerprint.java index 743c7da13..c50c235f6 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Fingerprint.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Fingerprint.java @@ -14,28 +14,28 @@ public class Fingerprint implements Function { static final Pattern alphanum = Pattern.compile("\\p{Punct}|\\p{Cntrl}"); - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = (o instanceof String) ? (String) o : o.toString(); - s = s.trim(); // first off, remove whitespace around the string - s = s.toLowerCase(); // then lowercase it - s = alphanum.matcher(s).replaceAll(""); // then remove all punctuation and control chars - String[] frags = StringUtils.split(s); // split by whitespace - Arrays.sort(frags); // sort the fragments - return StringUtils.join(frags," "); // rejoin them with a single space between them - - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = (o instanceof String) ? (String) o : o.toString(); + s = s.trim(); // first off, remove whitespace around the string + s = s.toLowerCase(); // then lowercase it + s = alphanum.matcher(s).replaceAll(""); // then remove all punctuation and control chars + String[] frags = StringUtils.split(s); // split by whitespace + Arrays.sort(frags); // sort the fragments + return StringUtils.join(frags," "); // rejoin them with a single space between them + + } return null; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the fingerprint of s, a derived string that aims to be a more canonical form of it (this is mostly useful for finding clusters of strings related to the same information)."); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the fingerprint of s, a derived string that aims to be a more canonical form of it (this is mostly useful for finding clusters of strings related to the same information)."); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/IndexOf.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/IndexOf.java index 1c9ec614d..f434ccab8 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/IndexOf.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/IndexOf.java @@ -11,24 +11,24 @@ import com.metaweb.gridworks.gel.Function; public class IndexOf implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object s1 = args[0]; - Object s2 = args[1]; - if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { - return ((String) s1).indexOf((String) s2); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object s1 = args[0]; + Object s2 = args[1]; + if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { + return ((String) s1).indexOf((String) s2); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the index of sub first ocurring in s"); - writer.key("params"); writer.value("string s, string sub"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the index of sub first ocurring in s"); + writer.key("params"); writer.value("string s, string sub"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/LastIndexOf.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/LastIndexOf.java index 1f68fb964..d16fc2559 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/LastIndexOf.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/LastIndexOf.java @@ -11,25 +11,25 @@ import com.metaweb.gridworks.gel.Function; public class LastIndexOf implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object s1 = args[0]; - Object s2 = args[1]; - if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { - return ((String) s1).lastIndexOf((String) s2); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object s1 = args[0]; + Object s2 = args[1]; + if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { + return ((String) s1).lastIndexOf((String) s2); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the index of sub last ocurring in s"); - writer.key("params"); writer.value("string s, string sub"); - writer.key("returns"); writer.value("number"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the index of sub last ocurring in s"); + writer.key("params"); writer.value("string s, string sub"); + writer.key("returns"); writer.value("number"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/MD5.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/MD5.java index 0cd29e915..9101fca12 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/MD5.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/MD5.java @@ -12,22 +12,22 @@ import com.metaweb.gridworks.gel.Function; public class MD5 implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = (o instanceof String) ? (String) o : o.toString(); - return DigestUtils.md5Hex(s); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = (o instanceof String) ? (String) o : o.toString(); + return DigestUtils.md5Hex(s); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the MD5 hash of s"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the MD5 hash of s"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Phonetic.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Phonetic.java index f36658aeb..12b3a8583 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Phonetic.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Phonetic.java @@ -18,7 +18,7 @@ public class Phonetic implements Function { private Metaphone metaphone = new Metaphone(); private Soundex soundex = new Soundex(); - public Object call(Properties bindings, Object[] args) { + public Object call(Properties bindings, Object[] args) { if (args.length == 2) { Object o1 = args[0]; Object o2 = args[1]; @@ -37,15 +37,15 @@ public class Phonetic implements Function { } } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the a phonetic encoding of s (optionally indicating which encoding to use')"); - writer.key("params"); writer.value("string s, string encoding (optional, defaults to 'DoubleMetaphone')"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the a phonetic encoding of s (optionally indicating which encoding to use')"); + writer.key("params"); writer.value("string s, string encoding (optional, defaults to 'DoubleMetaphone')"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Reinterpret.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Reinterpret.java index aeaa3e31b..39cfd004d 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Reinterpret.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Reinterpret.java @@ -15,7 +15,7 @@ import com.metaweb.gridworks.model.Project; public class Reinterpret implements Function { - public Object call(Properties bindings, Object[] args) { + public Object call(Properties bindings, Object[] args) { if (args.length == 2) { Object o1 = args[0]; Object o2 = args[1]; @@ -37,15 +37,15 @@ public class Reinterpret implements Function { } } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 arguments"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns s reinterpreted thru the given encoder."); - writer.key("params"); writer.value("string s, string encoder"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns s reinterpreted thru the given encoder."); + writer.key("params"); writer.value("string s, string encoder"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Replace.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Replace.java index a850ecc9c..d3be5d38b 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Replace.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Replace.java @@ -11,8 +11,8 @@ import com.metaweb.gridworks.gel.Function; public class Replace implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 3) { + public Object call(Properties bindings, Object[] args) { + if (args.length == 3) { Object o1 = args[0]; Object o2 = args[1]; Object o3 = args[2]; @@ -20,18 +20,18 @@ public class Replace implements Function { String str = (o1 instanceof String) ? (String) o1 : o1.toString(); return str.replace((String) o2, (String) o3); } - } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings"); - } + } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s"); - writer.key("params"); writer.value("string s, string f, string r"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s"); + writer.key("params"); writer.value("string s, string f, string r"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceChars.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceChars.java index 6fbd34840..ec36777c3 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceChars.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceChars.java @@ -12,27 +12,27 @@ import com.metaweb.gridworks.gel.Function; public class ReplaceChars implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 3) { - Object o1 = args[0]; - Object o2 = args[1]; - Object o3 = args[2]; - if (o1 != null && o2 != null && o3 != null && o2 instanceof String && o3 instanceof String) { - String str = (o1 instanceof String) ? (String) o1 : o1.toString(); - return StringUtils.replaceChars(str, (String) o2, (String) o3); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 3) { + Object o1 = args[0]; + Object o2 = args[1]; + Object o3 = args[2]; + if (o1 != null && o2 != null && o3 != null && o2 instanceof String && o3 instanceof String) { + String str = (o1 instanceof String) ? (String) o1 : o1.toString(); + return StringUtils.replaceChars(str, (String) o2, (String) o3); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings"); - } + } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the string obtained by replacing all chars in f with the char in s at that same position"); - writer.key("params"); writer.value("string s, string f, string r"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the string obtained by replacing all chars in f with the char in s at that same position"); + writer.key("params"); writer.value("string s, string f, string r"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceRegexp.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceRegexp.java index 4f025c1ca..e3a501c50 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceRegexp.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ReplaceRegexp.java @@ -11,7 +11,7 @@ import com.metaweb.gridworks.gel.Function; public class ReplaceRegexp implements Function { - public Object call(Properties bindings, Object[] args) { + public Object call(Properties bindings, Object[] args) { if (args.length == 3) { Object o1 = args[0]; Object o2 = args[1]; @@ -22,16 +22,16 @@ public class ReplaceRegexp implements Function { } } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings"); - } + } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s"); - writer.key("params"); writer.value("string s, string f, string r"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s"); + writer.key("params"); writer.value("string s, string f, string r"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/SHA1.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SHA1.java index 26fcee678..bc45e89c5 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/SHA1.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SHA1.java @@ -12,22 +12,22 @@ import com.metaweb.gridworks.gel.Function; public class SHA1 implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = (o instanceof String) ? (String) o : o.toString(); - return DigestUtils.shaHex(s); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = (o instanceof String) ? (String) o : o.toString(); + return DigestUtils.shaHex(s); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the SHA-1 hash of s"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the SHA-1 hash of s"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Split.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Split.java index a5f66e91a..12731e65a 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Split.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Split.java @@ -11,24 +11,24 @@ import com.metaweb.gridworks.gel.Function; public class Split implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object v = args[0]; - Object split = args[1]; - if (v != null && split != null && split instanceof String) { - return (v instanceof String ? (String) v : v.toString()).split((String) split); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object v = args[0]; + Object split = args[1]; + if (v != null && split != null && split instanceof String) { + return (v instanceof String ? (String) v : v.toString()).split((String) split); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns the array of strings obtained by splitting s with separator sep"); - writer.key("params"); writer.value("string s, string sep"); - writer.key("returns"); writer.value("array"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns the array of strings obtained by splitting s with separator sep"); + writer.key("params"); writer.value("string s, string sep"); + writer.key("returns"); writer.value("array"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByCharType.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByCharType.java index 5e07b08da..805c226fd 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByCharType.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByCharType.java @@ -12,24 +12,24 @@ import com.metaweb.gridworks.gel.Function; public class SplitByCharType implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - Object o = args[0]; - if (o != null) { - String s = (o instanceof String) ? (String) o : o.toString(); - return StringUtils.splitByCharacterType(s); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + Object o = args[0]; + if (o != null) { + String s = (o instanceof String) ? (String) o : o.toString(); + return StringUtils.splitByCharacterType(s); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns an array of strings obtained by splitting s grouping consecutive chars by their unicode type"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("array"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns an array of strings obtained by splitting s grouping consecutive chars by their unicode type"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("array"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/StartsWith.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/StartsWith.java index 7d470c7bb..981090570 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/StartsWith.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/StartsWith.java @@ -11,23 +11,23 @@ import com.metaweb.gridworks.gel.Function; public class StartsWith implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 2) { - Object s1 = args[0]; - Object s2 = args[1]; - if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { - return ((String) s1).startsWith((String) s2); - } - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 2) { + Object s1 = args[0]; + Object s2 = args[1]; + if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) { + return ((String) s1).startsWith((String) s2); + } + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings"); - } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns whether s starts with sub"); - writer.key("params"); writer.value("string s, string sub"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } + } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns whether s starts with sub"); + writer.key("params"); writer.value("string s, string sub"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToLowercase.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToLowercase.java index 604dc72fd..2fb8fa6d0 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToLowercase.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToLowercase.java @@ -11,21 +11,21 @@ import com.metaweb.gridworks.gel.Function; public class ToLowercase implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - return (o instanceof String ? (String) o : o.toString()).toLowerCase(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + return (o instanceof String ? (String) o : o.toString()).toLowerCase(); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns s converted to lowercase"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns s converted to lowercase"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToTitlecase.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToTitlecase.java index 49413aef9..c7b073d0d 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToTitlecase.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToTitlecase.java @@ -11,36 +11,36 @@ import com.metaweb.gridworks.gel.Function; public class ToTitlecase implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = o instanceof String ? (String) o : o.toString(); - String[] words = s.split("\\s+"); - - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < words.length; i++) { - String word = words[i]; - if (word.length() > 0) { - if (sb.length() > 0) { - sb.append(' '); - } - sb.append(word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()); - } - } - - return sb.toString(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = o instanceof String ? (String) o : o.toString(); + String[] words = s.split("\\s+"); + + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < words.length; i++) { + String word = words[i]; + if (word.length() > 0) { + if (sb.length() > 0) { + sb.append(' '); + } + sb.append(word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()); + } + } + + return sb.toString(); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string"); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns s converted to titlecase"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns s converted to titlecase"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToUppercase.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToUppercase.java index 991ade76a..37b0729e6 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToUppercase.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/ToUppercase.java @@ -11,21 +11,21 @@ import com.metaweb.gridworks.gel.Function; public class ToUppercase implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - return (o instanceof String ? (String) o : o.toString()).toUpperCase(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + return (o instanceof String ? (String) o : o.toString()).toUpperCase(); + } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string"); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns s converted to uppercase"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns s converted to uppercase"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Trim.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Trim.java index bb1ab0398..21c345f98 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Trim.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Trim.java @@ -9,22 +9,22 @@ import com.metaweb.gridworks.gel.Function; public class Trim implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1) { - Object s1 = args[0]; - if (s1 != null && s1 instanceof String) { - return ((String) s1).trim(); - } - } - return null; - } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns copy of the string, with leading and trailing whitespace omitted."); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1) { + Object s1 = args[0]; + if (s1 != null && s1 instanceof String) { + return ((String) s1).trim(); + } + } + return null; + } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns copy of the string, with leading and trailing whitespace omitted."); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Unicode.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Unicode.java index b51c408f1..208c4a2dc 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/Unicode.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/Unicode.java @@ -9,26 +9,26 @@ import com.metaweb.gridworks.gel.Function; public class Unicode implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = (o instanceof String) ? (String) o : o.toString(); - Integer[] output = new Integer[s.length()]; - for (int i = 0; i < s.length(); i++) { - output[i] = s.codePointAt(i); - } - return output; - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = (o instanceof String) ? (String) o : o.toString(); + Integer[] output = new Integer[s.length()]; + for (int i = 0; i < s.length(); i++) { + output[i] = s.codePointAt(i); + } + return output; + } return null; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns an array of strings describing each character of s in their full unicode notation"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns an array of strings describing each character of s in their full unicode notation"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/UnicodeType.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/UnicodeType.java index 496127dd6..0c47dc064 100644 --- a/src/main/java/com/metaweb/gridworks/expr/functions/strings/UnicodeType.java +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/UnicodeType.java @@ -9,63 +9,63 @@ import com.metaweb.gridworks.gel.Function; public class UnicodeType implements Function { - public Object call(Properties bindings, Object[] args) { - if (args.length == 1 && args[0] != null) { - Object o = args[0]; - String s = (o instanceof String) ? (String) o : o.toString(); - String[] output = new String[s.length()]; - for (int i = 0; i < s.length(); i++) { - output[i] = translateType(Character.getType(s.codePointAt(i))); - } - return output; - } + public Object call(Properties bindings, Object[] args) { + if (args.length == 1 && args[0] != null) { + Object o = args[0]; + String s = (o instanceof String) ? (String) o : o.toString(); + String[] output = new String[s.length()]; + for (int i = 0; i < s.length(); i++) { + output[i] = translateType(Character.getType(s.codePointAt(i))); + } + return output; + } return null; - } - - private String translateType(int type) { - switch(type) { - case 0: return "unassigned"; - case 1: return "uppercase letter"; - case 2: return "lowercase letter"; - case 3: return "titlecase letter"; - case 4: return "modifier letter"; - case 5: return "other letter"; - case 6: return "non spacing mark"; - case 7: return "enclosing mark"; - case 8: return "combining spacing mark"; - case 9: return "decimal digit number"; - case 10: return "letter number"; - case 11: return "other number"; - case 12: return "space separator"; - case 13: return "line separator"; - case 14: return "paragraph separator"; - case 15: return "control"; - case 16: return "format"; - // 17 does not seem to be used - case 18: return "private use"; - case 19: return "surrogate"; - case 20: return "dash punctuation"; - case 21: return "start punctuation"; - case 22: return "end punctuation"; - case 23: return "connector punctuation"; - case 24: return "other punctuation"; - case 25: return "math symbol"; - case 26: return "currency symbol"; - case 27: return "modifier symbol"; - case 28: return "other symbol"; - case 29: return "initial quote punctuation"; - case 30: return "final quote punctuation"; - default: return "unknown"; - } - } + } + + private String translateType(int type) { + switch(type) { + case 0: return "unassigned"; + case 1: return "uppercase letter"; + case 2: return "lowercase letter"; + case 3: return "titlecase letter"; + case 4: return "modifier letter"; + case 5: return "other letter"; + case 6: return "non spacing mark"; + case 7: return "enclosing mark"; + case 8: return "combining spacing mark"; + case 9: return "decimal digit number"; + case 10: return "letter number"; + case 11: return "other number"; + case 12: return "space separator"; + case 13: return "line separator"; + case 14: return "paragraph separator"; + case 15: return "control"; + case 16: return "format"; + // 17 does not seem to be used + case 18: return "private use"; + case 19: return "surrogate"; + case 20: return "dash punctuation"; + case 21: return "start punctuation"; + case 22: return "end punctuation"; + case 23: return "connector punctuation"; + case 24: return "other punctuation"; + case 25: return "math symbol"; + case 26: return "currency symbol"; + case 27: return "modifier symbol"; + case 28: return "other symbol"; + case 29: return "initial quote punctuation"; + case 30: return "final quote punctuation"; + default: return "unknown"; + } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value("Returns an array of strings describing each character of s in their full unicode notation"); - writer.key("params"); writer.value("string s"); - writer.key("returns"); writer.value("string"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value("Returns an array of strings describing each character of s in their full unicode notation"); + writer.key("params"); writer.value("string s"); + writer.key("returns"); writer.value("string"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/Control.java b/src/main/java/com/metaweb/gridworks/gel/Control.java index abc906a29..83598c0ae 100644 --- a/src/main/java/com/metaweb/gridworks/gel/Control.java +++ b/src/main/java/com/metaweb/gridworks/gel/Control.java @@ -6,7 +6,7 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.expr.Evaluable; public interface Control extends Jsonizable { - public Object call(Properties bindings, Evaluable[] args); - - public String checkArguments(Evaluable[] args); + public Object call(Properties bindings, Evaluable[] args); + + public String checkArguments(Evaluable[] args); } diff --git a/src/main/java/com/metaweb/gridworks/gel/Function.java b/src/main/java/com/metaweb/gridworks/gel/Function.java index 139a5a53a..0bbafbfa2 100644 --- a/src/main/java/com/metaweb/gridworks/gel/Function.java +++ b/src/main/java/com/metaweb/gridworks/gel/Function.java @@ -5,5 +5,5 @@ import java.util.Properties; import com.metaweb.gridworks.Jsonizable; public interface Function extends Jsonizable { - public Object call(Properties bindings, Object[] args); + public Object call(Properties bindings, Object[] args); } diff --git a/src/main/java/com/metaweb/gridworks/gel/Parser.java b/src/main/java/com/metaweb/gridworks/gel/Parser.java index 3c7bfcee8..387c20160 100644 --- a/src/main/java/com/metaweb/gridworks/gel/Parser.java +++ b/src/main/java/com/metaweb/gridworks/gel/Parser.java @@ -16,229 +16,229 @@ import com.metaweb.gridworks.gel.ast.OperatorCallExpr; import com.metaweb.gridworks.gel.ast.VariableExpr; public class Parser { - protected Scanner _scanner; - protected Token _token; - protected Evaluable _root; - - public Parser(String s) throws ParsingException { - this(s, 0, s.length()); - } - - public Parser(String s, int from, int to) throws ParsingException { - _scanner = new Scanner(s, from, to); - _token = _scanner.next(); - - _root = parseExpression(); - } - - public Evaluable getExpression() { - return _root; - } - - protected void next() { - _token = _scanner.next(); - } - - protected ParsingException makeException(String desc) { - int index = _token != null ? _token.start : _scanner.getIndex(); - - return new ParsingException("Parsing error at offset " + index + ": " + desc); - } - - protected Evaluable parseExpression() throws ParsingException { - Evaluable sub = parseSubExpression(); - - while (_token != null && - _token.type == TokenType.Operator && - ">=<==!=".indexOf(_token.text) >= 0) { - - String op = _token.text; - - next(); - - Evaluable sub2 = parseSubExpression(); - - sub = new OperatorCallExpr(new Evaluable[] { sub, sub2 }, op); - } - - return sub; - } - - protected Evaluable parseSubExpression() throws ParsingException { - Evaluable sub = parseTerm(); - - while (_token != null && - _token.type == TokenType.Operator && - "+-".indexOf(_token.text) >= 0) { - - String op = _token.text; - - next(); - - Evaluable sub2 = parseSubExpression(); - - sub = new OperatorCallExpr(new Evaluable[] { sub, sub2 }, op); - } - - return sub; - } - - protected Evaluable parseTerm() throws ParsingException { - Evaluable factor = parseFactor(); - - while (_token != null && - _token.type == TokenType.Operator && - "*/".indexOf(_token.text) >= 0) { - - String op = _token.text; - - next(); - - Evaluable factor2 = parseFactor(); - - factor = new OperatorCallExpr(new Evaluable[] { factor, factor2 }, op); - } - - return factor; - } - - protected Evaluable parseFactor() throws ParsingException { - if (_token == null) { - throw makeException("Expression ends too early"); - } - - Evaluable eval = null; - - if (_token.type == TokenType.String) { - eval = new LiteralExpr(_token.text); - next(); - } else if (_token.type == TokenType.Number) { - eval = new LiteralExpr(((NumberToken)_token).value); - next(); - } else if (_token.type == TokenType.Operator && _token.text.equals("-")) { // unary minus? - next(); - - if (_token != null && _token.type == TokenType.Number) { - eval = new LiteralExpr(-((NumberToken)_token).value); - next(); - } else { - throw makeException("Bad negative number"); - } - } else if (_token.type == TokenType.Identifier) { - String text = _token.text; - next(); - - if (_token == null || _token.type != TokenType.Delimiter || !_token.text.equals("(")) { - eval = new VariableExpr(text); - } else { - Function f = ControlFunctionRegistry.getFunction(text); - Control c = ControlFunctionRegistry.getControl(text); - if (f == null && c == null) { - throw makeException("Unknown function or control named " + text); - } - - next(); // swallow ( - - List args = parseExpressionList(")"); - - if (c != null) { - Evaluable[] argsA = makeArray(args); - String errorMessage = c.checkArguments(argsA); - if (errorMessage != null) { - throw makeException(errorMessage); - } + protected Scanner _scanner; + protected Token _token; + protected Evaluable _root; + + public Parser(String s) throws ParsingException { + this(s, 0, s.length()); + } + + public Parser(String s, int from, int to) throws ParsingException { + _scanner = new Scanner(s, from, to); + _token = _scanner.next(); + + _root = parseExpression(); + } + + public Evaluable getExpression() { + return _root; + } + + protected void next() { + _token = _scanner.next(); + } + + protected ParsingException makeException(String desc) { + int index = _token != null ? _token.start : _scanner.getIndex(); + + return new ParsingException("Parsing error at offset " + index + ": " + desc); + } + + protected Evaluable parseExpression() throws ParsingException { + Evaluable sub = parseSubExpression(); + + while (_token != null && + _token.type == TokenType.Operator && + ">=<==!=".indexOf(_token.text) >= 0) { + + String op = _token.text; + + next(); + + Evaluable sub2 = parseSubExpression(); + + sub = new OperatorCallExpr(new Evaluable[] { sub, sub2 }, op); + } + + return sub; + } + + protected Evaluable parseSubExpression() throws ParsingException { + Evaluable sub = parseTerm(); + + while (_token != null && + _token.type == TokenType.Operator && + "+-".indexOf(_token.text) >= 0) { + + String op = _token.text; + + next(); + + Evaluable sub2 = parseSubExpression(); + + sub = new OperatorCallExpr(new Evaluable[] { sub, sub2 }, op); + } + + return sub; + } + + protected Evaluable parseTerm() throws ParsingException { + Evaluable factor = parseFactor(); + + while (_token != null && + _token.type == TokenType.Operator && + "*/".indexOf(_token.text) >= 0) { + + String op = _token.text; + + next(); + + Evaluable factor2 = parseFactor(); + + factor = new OperatorCallExpr(new Evaluable[] { factor, factor2 }, op); + } + + return factor; + } + + protected Evaluable parseFactor() throws ParsingException { + if (_token == null) { + throw makeException("Expression ends too early"); + } + + Evaluable eval = null; + + if (_token.type == TokenType.String) { + eval = new LiteralExpr(_token.text); + next(); + } else if (_token.type == TokenType.Number) { + eval = new LiteralExpr(((NumberToken)_token).value); + next(); + } else if (_token.type == TokenType.Operator && _token.text.equals("-")) { // unary minus? + next(); + + if (_token != null && _token.type == TokenType.Number) { + eval = new LiteralExpr(-((NumberToken)_token).value); + next(); + } else { + throw makeException("Bad negative number"); + } + } else if (_token.type == TokenType.Identifier) { + String text = _token.text; + next(); + + if (_token == null || _token.type != TokenType.Delimiter || !_token.text.equals("(")) { + eval = new VariableExpr(text); + } else { + Function f = ControlFunctionRegistry.getFunction(text); + Control c = ControlFunctionRegistry.getControl(text); + if (f == null && c == null) { + throw makeException("Unknown function or control named " + text); + } + + next(); // swallow ( + + List args = parseExpressionList(")"); + + if (c != null) { + Evaluable[] argsA = makeArray(args); + String errorMessage = c.checkArguments(argsA); + if (errorMessage != null) { + throw makeException(errorMessage); + } eval = new ControlCallExpr(argsA, c); - } else { - eval = new FunctionCallExpr(makeArray(args), f); - } - } - } else if (_token.type == TokenType.Delimiter && _token.text.equals("(")) { - next(); - - eval = parseExpression(); - - if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(")")) { - next(); - } else { - throw makeException("Missing )"); - } - } else { - throw makeException("Missing number, string, identifier, or parenthesized expression"); - } - - while (_token != null) { - if (_token.type == TokenType.Operator && _token.text.equals(".")) { - next(); // swallow . - - if (_token == null || _token.type != TokenType.Identifier) { - throw makeException("Missing function name"); - } - - String identifier = _token.text; - next(); - - if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals("(")) { - next(); // swallow ( - - Function f = ControlFunctionRegistry.getFunction(identifier); - if (f == null) { - throw makeException("Unknown function " + identifier); - } - - List args = parseExpressionList(")"); - args.add(0, eval); - - eval = new FunctionCallExpr(makeArray(args), f); - } else { - eval = new FieldAccessorExpr(eval, identifier); - } - } else if (_token.type == TokenType.Delimiter && _token.text.equals("[")) { - next(); // swallow [ - - List args = parseExpressionList("]"); - args.add(0, eval); - - eval = new FunctionCallExpr(makeArray(args), ControlFunctionRegistry.getFunction("get")); - } else { - break; - } - } - - return eval; - } - - protected List parseExpressionList(String closingDelimiter) throws ParsingException { - List l = new LinkedList(); - - if (_token != null && - (_token.type != TokenType.Delimiter || !_token.text.equals(closingDelimiter))) { - - while (_token != null) { - Evaluable eval = parseExpression(); - - l.add(eval); - - if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(",")) { - next(); // swallow comma, loop back for more - } else { - break; - } - } - } - - if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(closingDelimiter)) { - next(); // swallow closing delimiter - } else { - throw makeException("Missing " + closingDelimiter); - } - - return l; - } - - protected Evaluable[] makeArray(List l) { - Evaluable[] a = new Evaluable[l.size()]; - l.toArray(a); - - return a; - } + } else { + eval = new FunctionCallExpr(makeArray(args), f); + } + } + } else if (_token.type == TokenType.Delimiter && _token.text.equals("(")) { + next(); + + eval = parseExpression(); + + if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(")")) { + next(); + } else { + throw makeException("Missing )"); + } + } else { + throw makeException("Missing number, string, identifier, or parenthesized expression"); + } + + while (_token != null) { + if (_token.type == TokenType.Operator && _token.text.equals(".")) { + next(); // swallow . + + if (_token == null || _token.type != TokenType.Identifier) { + throw makeException("Missing function name"); + } + + String identifier = _token.text; + next(); + + if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals("(")) { + next(); // swallow ( + + Function f = ControlFunctionRegistry.getFunction(identifier); + if (f == null) { + throw makeException("Unknown function " + identifier); + } + + List args = parseExpressionList(")"); + args.add(0, eval); + + eval = new FunctionCallExpr(makeArray(args), f); + } else { + eval = new FieldAccessorExpr(eval, identifier); + } + } else if (_token.type == TokenType.Delimiter && _token.text.equals("[")) { + next(); // swallow [ + + List args = parseExpressionList("]"); + args.add(0, eval); + + eval = new FunctionCallExpr(makeArray(args), ControlFunctionRegistry.getFunction("get")); + } else { + break; + } + } + + return eval; + } + + protected List parseExpressionList(String closingDelimiter) throws ParsingException { + List l = new LinkedList(); + + if (_token != null && + (_token.type != TokenType.Delimiter || !_token.text.equals(closingDelimiter))) { + + while (_token != null) { + Evaluable eval = parseExpression(); + + l.add(eval); + + if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(",")) { + next(); // swallow comma, loop back for more + } else { + break; + } + } + } + + if (_token != null && _token.type == TokenType.Delimiter && _token.text.equals(closingDelimiter)) { + next(); // swallow closing delimiter + } else { + throw makeException("Missing " + closingDelimiter); + } + + return l; + } + + protected Evaluable[] makeArray(List l) { + Evaluable[] a = new Evaluable[l.size()]; + l.toArray(a); + + return a; + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/Scanner.java b/src/main/java/com/metaweb/gridworks/gel/Scanner.java index 5082c1a6e..714a3504d 100644 --- a/src/main/java/com/metaweb/gridworks/gel/Scanner.java +++ b/src/main/java/com/metaweb/gridworks/gel/Scanner.java @@ -1,228 +1,228 @@ package com.metaweb.gridworks.gel; public class Scanner { - static public enum TokenType { - Error, - Delimiter, - Operator, - Identifier, - Number, - String - } - - static public class Token { - final public int start; - final public int end; - final public TokenType type; - final public String text; - - Token(int start, int end, TokenType type, String text) { - this.start = start; - this.end = end; - this.type = type; - this.text = text; - } - } - - static public class ErrorToken extends Token { - final public String detail; // error detail - - public ErrorToken(int start, int end, String text, String detail) { - super(start, end, TokenType.Error, text); - this.detail = detail; - } - } - - static public class NumberToken extends Token { - final public double value; - - public NumberToken(int start, int end, String text, double value) { - super(start, end, TokenType.Number, text); - this.value = value; - } - } - - protected String _text; - protected int _index; - protected int _limit; - - public Scanner(String s) { - this(s, 0, s.length()); - } - - public Scanner(String s, int from, int to) { - _text = s; - _index = from; - _limit = to; - } - - public int getIndex() { - return _index; - } - - public Token next() { - // skip whitespace - while (_index < _limit && Character.isWhitespace(_text.charAt(_index))) { - _index++; - } - if (_index == _limit) { - return null; - } - - char c = _text.charAt(_index); - int start = _index; - String detail = null; - - if (Character.isDigit(c)) { // number literal - double value = 0; - - while (_index < _limit && Character.isDigit(c = _text.charAt(_index))) { - value = value * 10 + (c - '0'); - _index++; - } - - if (_index < _limit && c == '.') { - _index++; - - double division = 1; - while (_index < _limit && Character.isDigit(c = _text.charAt(_index))) { - value = value * 10 + (c - '0'); - division *= 10; - _index++; - } - - value /= division; - } - - // TODO: support exponent e notation - - return new NumberToken( - start, - _index, - _text.substring(start, _index), - value - ); - } else if (c == '"' || c == '\'') { - /* - * String Literal - */ - - StringBuffer sb = new StringBuffer(); - char delimiter = c; - - _index++; // skip opening delimiter - - while (_index < _limit) { - c = _text.charAt(_index); - if (c == delimiter) { - _index++; // skip closing delimiter - - return new Token( - start, - _index, - TokenType.String, - sb.toString() - ); - } else if (c == '\\') { - _index++; // skip escaping marker - if (_index < _limit) { - sb.append(_text.charAt(_index)); - } - } else { - sb.append(c); - } - _index++; - } - - detail = "String not properly closed"; - // fall through - - } else if (Character.isLetter(c)) { // identifier - while (_index < _limit && Character.isLetterOrDigit(_text.charAt(_index))) { - _index++; - } - - return new Token( - start, - _index, - TokenType.Identifier, - _text.substring(start, _index) - ); - } else if ("+-*/.".indexOf(c) >= 0) { // operator - _index++; - - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } else if ("()[],".indexOf(c) >= 0) { // delimiter - _index++; - - return new Token( - start, - _index, - TokenType.Delimiter, - _text.substring(start, _index) - ); - } else if (c == '!' && _index < _limit - 1 && _text.charAt(_index + 1) == '=') { - _index += 2; - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } else if (c == '<') { - if (_index < _limit - 1 && - (_text.charAt(_index + 1) == '=' || - _text.charAt(_index + 1) == '>')) { - - _index += 2; - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } else { - _index++; - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } - } else if (">=".indexOf(c) >= 0) { // operator - if (_index < _limit - 1 && _text.charAt(_index + 1) == '=') { - _index += 2; - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } else { - _index++; - return new Token( - start, - _index, - TokenType.Operator, - _text.substring(start, _index) - ); - } - } else { - _index++; - detail = "Unrecognized symbol"; - } - - return new ErrorToken( - start, - _index, - _text.substring(start, _index), - detail - ); - } + static public enum TokenType { + Error, + Delimiter, + Operator, + Identifier, + Number, + String + } + + static public class Token { + final public int start; + final public int end; + final public TokenType type; + final public String text; + + Token(int start, int end, TokenType type, String text) { + this.start = start; + this.end = end; + this.type = type; + this.text = text; + } + } + + static public class ErrorToken extends Token { + final public String detail; // error detail + + public ErrorToken(int start, int end, String text, String detail) { + super(start, end, TokenType.Error, text); + this.detail = detail; + } + } + + static public class NumberToken extends Token { + final public double value; + + public NumberToken(int start, int end, String text, double value) { + super(start, end, TokenType.Number, text); + this.value = value; + } + } + + protected String _text; + protected int _index; + protected int _limit; + + public Scanner(String s) { + this(s, 0, s.length()); + } + + public Scanner(String s, int from, int to) { + _text = s; + _index = from; + _limit = to; + } + + public int getIndex() { + return _index; + } + + public Token next() { + // skip whitespace + while (_index < _limit && Character.isWhitespace(_text.charAt(_index))) { + _index++; + } + if (_index == _limit) { + return null; + } + + char c = _text.charAt(_index); + int start = _index; + String detail = null; + + if (Character.isDigit(c)) { // number literal + double value = 0; + + while (_index < _limit && Character.isDigit(c = _text.charAt(_index))) { + value = value * 10 + (c - '0'); + _index++; + } + + if (_index < _limit && c == '.') { + _index++; + + double division = 1; + while (_index < _limit && Character.isDigit(c = _text.charAt(_index))) { + value = value * 10 + (c - '0'); + division *= 10; + _index++; + } + + value /= division; + } + + // TODO: support exponent e notation + + return new NumberToken( + start, + _index, + _text.substring(start, _index), + value + ); + } else if (c == '"' || c == '\'') { + /* + * String Literal + */ + + StringBuffer sb = new StringBuffer(); + char delimiter = c; + + _index++; // skip opening delimiter + + while (_index < _limit) { + c = _text.charAt(_index); + if (c == delimiter) { + _index++; // skip closing delimiter + + return new Token( + start, + _index, + TokenType.String, + sb.toString() + ); + } else if (c == '\\') { + _index++; // skip escaping marker + if (_index < _limit) { + sb.append(_text.charAt(_index)); + } + } else { + sb.append(c); + } + _index++; + } + + detail = "String not properly closed"; + // fall through + + } else if (Character.isLetter(c)) { // identifier + while (_index < _limit && Character.isLetterOrDigit(_text.charAt(_index))) { + _index++; + } + + return new Token( + start, + _index, + TokenType.Identifier, + _text.substring(start, _index) + ); + } else if ("+-*/.".indexOf(c) >= 0) { // operator + _index++; + + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } else if ("()[],".indexOf(c) >= 0) { // delimiter + _index++; + + return new Token( + start, + _index, + TokenType.Delimiter, + _text.substring(start, _index) + ); + } else if (c == '!' && _index < _limit - 1 && _text.charAt(_index + 1) == '=') { + _index += 2; + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } else if (c == '<') { + if (_index < _limit - 1 && + (_text.charAt(_index + 1) == '=' || + _text.charAt(_index + 1) == '>')) { + + _index += 2; + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } else { + _index++; + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } + } else if (">=".indexOf(c) >= 0) { // operator + if (_index < _limit - 1 && _text.charAt(_index + 1) == '=') { + _index += 2; + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } else { + _index++; + return new Token( + start, + _index, + TokenType.Operator, + _text.substring(start, _index) + ); + } + } else { + _index++; + detail = "Unrecognized symbol"; + } + + return new ErrorToken( + start, + _index, + _text.substring(start, _index), + detail + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/ControlCallExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/ControlCallExpr.java index eb9c2f886..0fcb38b45 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/ControlCallExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/ControlCallExpr.java @@ -6,29 +6,29 @@ import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.gel.Control; public class ControlCallExpr implements Evaluable { - final protected Evaluable[] _args; - final protected Control _control; - - public ControlCallExpr(Evaluable[] args, Control c) { - _args = args; - _control = c; - } - - public Object evaluate(Properties bindings) { - return _control.call(bindings, _args); - } + final protected Evaluable[] _args; + final protected Control _control; + + public ControlCallExpr(Evaluable[] args, Control c) { + _args = args; + _control = c; + } + + public Object evaluate(Properties bindings) { + return _control.call(bindings, _args); + } - @Override - public String toString() { - StringBuffer sb = new StringBuffer(); - - for (Evaluable ev : _args) { - if (sb.length() > 0) { - sb.append(", "); - } - sb.append(ev.toString()); - } - - return _control.getClass().getSimpleName() + "(" + sb.toString() + ")"; - } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + + for (Evaluable ev : _args) { + if (sb.length() > 0) { + sb.append(", "); + } + sb.append(ev.toString()); + } + + return _control.getClass().getSimpleName() + "(" + sb.toString() + ")"; + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/FieldAccessorExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/FieldAccessorExpr.java index 654b4ebee..586598a59 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/FieldAccessorExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/FieldAccessorExpr.java @@ -8,29 +8,29 @@ import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.HasFields; public class FieldAccessorExpr implements Evaluable { - final protected Evaluable _inner; - final protected String _fieldName; - - public FieldAccessorExpr(Evaluable inner, String fieldName) { - _inner = inner; - _fieldName = fieldName; - } - - public Object evaluate(Properties bindings) { - Object o = _inner.evaluate(bindings); - if (ExpressionUtils.isError(o)) { - return o; - } else if (o == null) { - return new EvalError("Cannot retrieve field from null"); - } else if (o instanceof HasFields) { - return ((HasFields) o).getField(_fieldName, bindings); - } else { - return new EvalError("Object does not have any field, including " + _fieldName); - } - } + final protected Evaluable _inner; + final protected String _fieldName; + + public FieldAccessorExpr(Evaluable inner, String fieldName) { + _inner = inner; + _fieldName = fieldName; + } + + public Object evaluate(Properties bindings) { + Object o = _inner.evaluate(bindings); + if (ExpressionUtils.isError(o)) { + return o; + } else if (o == null) { + return new EvalError("Cannot retrieve field from null"); + } else if (o instanceof HasFields) { + return ((HasFields) o).getField(_fieldName, bindings); + } else { + return new EvalError("Object does not have any field, including " + _fieldName); + } + } - @Override - public String toString() { - return _inner.toString() + "." + _fieldName; - } + @Override + public String toString() { + return _inner.toString() + "." + _fieldName; + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/FunctionCallExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/FunctionCallExpr.java index 2ecce6df2..70f770bd1 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/FunctionCallExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/FunctionCallExpr.java @@ -7,36 +7,36 @@ import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.gel.Function; public class FunctionCallExpr implements Evaluable { - final protected Evaluable[] _args; - final protected Function _function; - - public FunctionCallExpr(Evaluable[] args, Function f) { - _args = args; - _function = f; - } - - public Object evaluate(Properties bindings) { - Object[] args = new Object[_args.length]; - for (int i = 0; i < _args.length; i++) { - Object v = _args[i].evaluate(bindings); - if (ExpressionUtils.isError(v)) { - return v; - } - args[i] = v; - } - return _function.call(bindings, args); - } + final protected Evaluable[] _args; + final protected Function _function; + + public FunctionCallExpr(Evaluable[] args, Function f) { + _args = args; + _function = f; + } + + public Object evaluate(Properties bindings) { + Object[] args = new Object[_args.length]; + for (int i = 0; i < _args.length; i++) { + Object v = _args[i].evaluate(bindings); + if (ExpressionUtils.isError(v)) { + return v; + } + args[i] = v; + } + return _function.call(bindings, args); + } - public String toString() { - StringBuffer sb = new StringBuffer(); - - for (Evaluable ev : _args) { - if (sb.length() > 0) { - sb.append(", "); - } - sb.append(ev.toString()); - } - - return _function.getClass().getSimpleName() + "(" + sb.toString() + ")"; - } + public String toString() { + StringBuffer sb = new StringBuffer(); + + for (Evaluable ev : _args) { + if (sb.length() > 0) { + sb.append(", "); + } + sb.append(ev.toString()); + } + + return _function.getClass().getSimpleName() + "(" + sb.toString() + ")"; + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/LiteralExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/LiteralExpr.java index f0b6dc939..6c3b70ab3 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/LiteralExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/LiteralExpr.java @@ -7,17 +7,17 @@ import org.json.JSONObject; import com.metaweb.gridworks.expr.Evaluable; public class LiteralExpr implements Evaluable { - final protected Object _value; - - public LiteralExpr(Object value) { - _value = value; - } - - public Object evaluate(Properties bindings) { - return _value; - } + final protected Object _value; + + public LiteralExpr(Object value) { + _value = value; + } + + public Object evaluate(Properties bindings) { + return _value; + } - public String toString() { - return _value instanceof String ? JSONObject.quote((String) _value) : _value.toString(); - } + public String toString() { + return _value instanceof String ? JSONObject.quote((String) _value) : _value.toString(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/OperatorCallExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/OperatorCallExpr.java index c26abe8cc..309e7caac 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/OperatorCallExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/OperatorCallExpr.java @@ -6,81 +6,81 @@ import com.metaweb.gridworks.expr.Evaluable; import com.metaweb.gridworks.expr.ExpressionUtils; public class OperatorCallExpr implements Evaluable { - final protected Evaluable[] _args; - final protected String _op; - - public OperatorCallExpr(Evaluable[] args, String op) { - _args = args; - _op = op; - } - - public Object evaluate(Properties bindings) { - Object[] args = new Object[_args.length]; - for (int i = 0; i < _args.length; i++) { - Object v = _args[i].evaluate(bindings); + final protected Evaluable[] _args; + final protected String _op; + + public OperatorCallExpr(Evaluable[] args, String op) { + _args = args; + _op = op; + } + + public Object evaluate(Properties bindings) { + Object[] args = new Object[_args.length]; + for (int i = 0; i < _args.length; i++) { + Object v = _args[i].evaluate(bindings); if (ExpressionUtils.isError(v)) { return v; } args[i] = v; - } - - if (args.length == 2) { - if (args[0] != null && args[1] != null) { + } + + if (args.length == 2) { + if (args[0] != null && args[1] != null) { if (args[0] instanceof Number && args[1] instanceof Number) { - if ("+".equals(_op)) { - return ((Number) args[0]).doubleValue() + ((Number) args[1]).doubleValue(); - } else if ("-".equals(_op)) { - return ((Number) args[0]).doubleValue() - ((Number) args[1]).doubleValue(); - } else if ("*".equals(_op)) { - return ((Number) args[0]).doubleValue() * ((Number) args[1]).doubleValue(); - } else if ("/".equals(_op)) { - return ((Number) args[0]).doubleValue() / ((Number) args[1]).doubleValue(); - } else if (">".equals(_op)) { - return ((Number) args[0]).doubleValue() > ((Number) args[1]).doubleValue(); - } else if (">=".equals(_op)) { - return ((Number) args[0]).doubleValue() >= ((Number) args[1]).doubleValue(); - } else if ("<".equals(_op)) { - return ((Number) args[0]).doubleValue() < ((Number) args[1]).doubleValue(); - } else if ("<=".equals(_op)) { - return ((Number) args[0]).doubleValue() <= ((Number) args[1]).doubleValue(); - } + if ("+".equals(_op)) { + return ((Number) args[0]).doubleValue() + ((Number) args[1]).doubleValue(); + } else if ("-".equals(_op)) { + return ((Number) args[0]).doubleValue() - ((Number) args[1]).doubleValue(); + } else if ("*".equals(_op)) { + return ((Number) args[0]).doubleValue() * ((Number) args[1]).doubleValue(); + } else if ("/".equals(_op)) { + return ((Number) args[0]).doubleValue() / ((Number) args[1]).doubleValue(); + } else if (">".equals(_op)) { + return ((Number) args[0]).doubleValue() > ((Number) args[1]).doubleValue(); + } else if (">=".equals(_op)) { + return ((Number) args[0]).doubleValue() >= ((Number) args[1]).doubleValue(); + } else if ("<".equals(_op)) { + return ((Number) args[0]).doubleValue() < ((Number) args[1]).doubleValue(); + } else if ("<=".equals(_op)) { + return ((Number) args[0]).doubleValue() <= ((Number) args[1]).doubleValue(); + } } - if ("+".equals(_op)) { - return args[0].toString() + args[1].toString(); - } - } - - if ("==".equals(_op)) { + if ("+".equals(_op)) { + return args[0].toString() + args[1].toString(); + } + } + + if ("==".equals(_op)) { if (args[0] != null) { return args[0].equals(args[1]); } else { return args[1] == null; } - } else if ("!=".equals(_op)) { + } else if ("!=".equals(_op)) { if (args[0] != null) { return !args[0].equals(args[1]); } else { return args[1] != null; } - } - } - return null; - } + } + } + return null; + } - @Override - public String toString() { - StringBuffer sb = new StringBuffer(); - - for (Evaluable ev : _args) { - if (sb.length() > 0) { - sb.append(' '); - sb.append(_op); - sb.append(' '); - } - sb.append(ev.toString()); - } - - return sb.toString(); - } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + + for (Evaluable ev : _args) { + if (sb.length() > 0) { + sb.append(' '); + sb.append(_op); + sb.append(' '); + } + sb.append(ev.toString()); + } + + return sb.toString(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/ast/VariableExpr.java b/src/main/java/com/metaweb/gridworks/gel/ast/VariableExpr.java index b5e990088..2a05e7702 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ast/VariableExpr.java +++ b/src/main/java/com/metaweb/gridworks/gel/ast/VariableExpr.java @@ -5,21 +5,21 @@ import java.util.Properties; import com.metaweb.gridworks.expr.Evaluable; public class VariableExpr implements Evaluable { - final protected String _name; - - public VariableExpr(String name) { - _name = name; - } - - public Object evaluate(Properties bindings) { + final protected String _name; + + public VariableExpr(String name) { + _name = name; + } + + public Object evaluate(Properties bindings) { return bindings.get(_name); - } + } - public String toString() { - return _name; - } - - public String getName() { - return _name; - } + public String toString() { + return _name; + } + + public String getName() { + return _name; + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/controls/ForEach.java b/src/main/java/com/metaweb/gridworks/gel/controls/ForEach.java index 1d99c302b..2627d982c 100644 --- a/src/main/java/com/metaweb/gridworks/gel/controls/ForEach.java +++ b/src/main/java/com/metaweb/gridworks/gel/controls/ForEach.java @@ -49,23 +49,23 @@ public class ForEach implements Control { return results.toArray(); } finally { - if (oldValue != null) { - bindings.put(name, oldValue); - } else { - bindings.remove(name); - } + if (oldValue != null) { + bindings.put(name, oldValue); + } else { + bindings.remove(name); + } } } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression e, and pushes the result onto the result array." - ); - writer.key("params"); writer.value("expression a, variable v, expression e"); - writer.key("returns"); writer.value("array"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression e, and pushes the result onto the result array." + ); + writer.key("params"); writer.value("expression a, variable v, expression e"); + writer.key("returns"); writer.value("array"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/controls/ForNonBlank.java b/src/main/java/com/metaweb/gridworks/gel/controls/ForNonBlank.java index b51ed006a..0f8572fdf 100644 --- a/src/main/java/com/metaweb/gridworks/gel/controls/ForNonBlank.java +++ b/src/main/java/com/metaweb/gridworks/gel/controls/ForNonBlank.java @@ -45,16 +45,16 @@ public class ForNonBlank implements Control { } } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "Evaluates expression o. If it is non-blank, binds its value to variable name v, evaluates expression eNonBlank and returns the result. " + - "Otherwise (if o evaluates to blank), evaluates expression eBlank and returns that result instead." - ); - writer.key("params"); writer.value("expression o, variable v, expression eNonBlank, expression eBlank"); - writer.key("returns"); writer.value("Depends on actual arguments"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "Evaluates expression o. If it is non-blank, binds its value to variable name v, evaluates expression eNonBlank and returns the result. " + + "Otherwise (if o evaluates to blank), evaluates expression eBlank and returns that result instead." + ); + writer.key("params"); writer.value("expression o, variable v, expression eNonBlank, expression eBlank"); + writer.key("returns"); writer.value("Depends on actual arguments"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/controls/If.java b/src/main/java/com/metaweb/gridworks/gel/controls/If.java index d5f42267a..b851d66d5 100644 --- a/src/main/java/com/metaweb/gridworks/gel/controls/If.java +++ b/src/main/java/com/metaweb/gridworks/gel/controls/If.java @@ -29,16 +29,16 @@ public class If implements Control { } } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " + - "Otherwise, evaluates expression eFalse and returns that result instead." - ); - writer.key("params"); writer.value("expression o, expression eTrue, expression eFalse"); - writer.key("returns"); writer.value("Depends on actual arguments"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " + + "Otherwise, evaluates expression eFalse and returns that result instead." + ); + writer.key("params"); writer.value("expression o, expression eTrue, expression eFalse"); + writer.key("returns"); writer.value("Depends on actual arguments"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/gel/controls/IsTest.java b/src/main/java/com/metaweb/gridworks/gel/controls/IsTest.java index 1a9e568ba..9bc5357f4 100644 --- a/src/main/java/com/metaweb/gridworks/gel/controls/IsTest.java +++ b/src/main/java/com/metaweb/gridworks/gel/controls/IsTest.java @@ -23,20 +23,20 @@ abstract class IsTest implements Control { return test(o); } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " + - "Otherwise, evaluates expression eFalse and returns that result instead." - ); - writer.key("params"); writer.value("expression o"); - writer.key("returns"); writer.value("boolean"); - writer.endObject(); - } - - abstract protected boolean test(Object v); - - abstract protected String getDescription(); + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " + + "Otherwise, evaluates expression eFalse and returns that result instead." + ); + writer.key("params"); writer.value("expression o"); + writer.key("returns"); writer.value("boolean"); + writer.endObject(); + } + + abstract protected boolean test(Object v); + + abstract protected String getDescription(); } diff --git a/src/main/java/com/metaweb/gridworks/gel/controls/With.java b/src/main/java/com/metaweb/gridworks/gel/controls/With.java index 08ab60f3a..8bd760f9f 100644 --- a/src/main/java/com/metaweb/gridworks/gel/controls/With.java +++ b/src/main/java/com/metaweb/gridworks/gel/controls/With.java @@ -34,23 +34,23 @@ public class With implements Control { return args[2].evaluate(bindings); } finally { - if (oldValue != null) { - bindings.put(name, oldValue); - } else { - bindings.remove(name); - } + if (oldValue != null) { + bindings.put(name, oldValue); + } else { + bindings.remove(name); + } } } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value( - "Evaluates expression o and binds its value to variable name v. Then evaluates expression e and returns that result" - ); - writer.key("params"); writer.value("expression o, variable v, expression e"); - writer.key("returns"); writer.value("Depends on actual arguments"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value( + "Evaluates expression o and binds its value to variable name v. Then evaluates expression e and returns that result" + ); + writer.key("params"); writer.value("expression o, variable v, expression e"); + writer.key("returns"); writer.value("Depends on actual arguments"); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/history/Change.java b/src/main/java/com/metaweb/gridworks/history/Change.java index b9b737a00..475fa4091 100644 --- a/src/main/java/com/metaweb/gridworks/history/Change.java +++ b/src/main/java/com/metaweb/gridworks/history/Change.java @@ -5,6 +5,6 @@ import java.io.Serializable; import com.metaweb.gridworks.model.Project; public interface Change extends Serializable { - public void apply(Project project); - public void revert(Project project); + public void apply(Project project); + public void revert(Project project); } diff --git a/src/main/java/com/metaweb/gridworks/history/History.java b/src/main/java/com/metaweb/gridworks/history/History.java index c5ac8e1f0..f4862c04d 100644 --- a/src/main/java/com/metaweb/gridworks/history/History.java +++ b/src/main/java/com/metaweb/gridworks/history/History.java @@ -14,122 +14,122 @@ import com.metaweb.gridworks.ProjectManager; import com.metaweb.gridworks.model.Project; public class History implements Serializable, Jsonizable { - private static final long serialVersionUID = -1529783362243627391L; - - protected long _projectID; - protected List _pastEntries; - protected List _futureEntries; - - public History(Project project) { - _projectID = project.id; - _pastEntries = new ArrayList(); - _futureEntries = new ArrayList(); - } - - public void addEntry(HistoryEntry entry) { - for (HistoryEntry entry2 : _futureEntries) { - entry2.delete(); - } - _futureEntries.clear(); - - _pastEntries.add(entry); - - entry.apply(ProjectManager.singleton.getProject(_projectID)); - setModified(); - } - - protected void setModified() { - ProjectManager.singleton.getProjectMetadata(_projectID).updateModified(); - } - - public List getLastPastEntries(int count) { - if (count <= 0) { - return new LinkedList(_pastEntries); - } else { - return _pastEntries.subList(Math.max(_pastEntries.size() - count, 0), _pastEntries.size()); - } - } - - public void undoRedo(long lastDoneEntryID) { - if (lastDoneEntryID == 0) { - undo(_pastEntries.size()); - } else { - for (int i = 0; i < _pastEntries.size(); i++) { - if (_pastEntries.get(i).id == lastDoneEntryID) { - undo(_pastEntries.size() - i - 1); - return; - } - } - - for (int i = 0; i < _futureEntries.size(); i++) { - if (_futureEntries.get(i).id == lastDoneEntryID) { - redo(i + 1); - return; - } - } - } - } - - protected HistoryEntry getEntry(long entryID) { - for (int i = 0; i < _pastEntries.size(); i++) { - if (_pastEntries.get(i).id == entryID) { - return _pastEntries.get(i); - } - } - - for (int i = 0; i < _futureEntries.size(); i++) { - if (_futureEntries.get(i).id == entryID) { - return _futureEntries.get(i); - } - } - return null; - } - - protected void undo(int times) { - Project project = ProjectManager.singleton.getProject(_projectID); - - while (times > 0 && _pastEntries.size() > 0) { - HistoryEntry entry = _pastEntries.remove(_pastEntries.size() - 1); - times--; - - entry.revert(project); - - _futureEntries.add(0, entry); - } - setModified(); - } - - protected void redo(int times) { - Project project = ProjectManager.singleton.getProject(_projectID); - - while (times > 0 && _futureEntries.size() > 0) { - HistoryEntry entry = _futureEntries.remove(0); - times--; - - entry.apply(project); - - _pastEntries.add(entry); - } - setModified(); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - - writer.key("past"); writer.array(); - for (HistoryEntry entry : _pastEntries) { - entry.write(writer, options); - } - writer.endArray(); - - writer.key("future"); writer.array(); - for (HistoryEntry entry : _futureEntries) { - entry.write(writer, options); - } - writer.endArray(); - - writer.endObject(); - } + private static final long serialVersionUID = -1529783362243627391L; + + protected long _projectID; + protected List _pastEntries; + protected List _futureEntries; + + public History(Project project) { + _projectID = project.id; + _pastEntries = new ArrayList(); + _futureEntries = new ArrayList(); + } + + public void addEntry(HistoryEntry entry) { + for (HistoryEntry entry2 : _futureEntries) { + entry2.delete(); + } + _futureEntries.clear(); + + _pastEntries.add(entry); + + entry.apply(ProjectManager.singleton.getProject(_projectID)); + setModified(); + } + + protected void setModified() { + ProjectManager.singleton.getProjectMetadata(_projectID).updateModified(); + } + + public List getLastPastEntries(int count) { + if (count <= 0) { + return new LinkedList(_pastEntries); + } else { + return _pastEntries.subList(Math.max(_pastEntries.size() - count, 0), _pastEntries.size()); + } + } + + public void undoRedo(long lastDoneEntryID) { + if (lastDoneEntryID == 0) { + undo(_pastEntries.size()); + } else { + for (int i = 0; i < _pastEntries.size(); i++) { + if (_pastEntries.get(i).id == lastDoneEntryID) { + undo(_pastEntries.size() - i - 1); + return; + } + } + + for (int i = 0; i < _futureEntries.size(); i++) { + if (_futureEntries.get(i).id == lastDoneEntryID) { + redo(i + 1); + return; + } + } + } + } + + protected HistoryEntry getEntry(long entryID) { + for (int i = 0; i < _pastEntries.size(); i++) { + if (_pastEntries.get(i).id == entryID) { + return _pastEntries.get(i); + } + } + + for (int i = 0; i < _futureEntries.size(); i++) { + if (_futureEntries.get(i).id == entryID) { + return _futureEntries.get(i); + } + } + return null; + } + + protected void undo(int times) { + Project project = ProjectManager.singleton.getProject(_projectID); + + while (times > 0 && _pastEntries.size() > 0) { + HistoryEntry entry = _pastEntries.remove(_pastEntries.size() - 1); + times--; + + entry.revert(project); + + _futureEntries.add(0, entry); + } + setModified(); + } + + protected void redo(int times) { + Project project = ProjectManager.singleton.getProject(_projectID); + + while (times > 0 && _futureEntries.size() > 0) { + HistoryEntry entry = _futureEntries.remove(0); + times--; + + entry.apply(project); + + _pastEntries.add(entry); + } + setModified(); + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + + writer.key("past"); writer.array(); + for (HistoryEntry entry : _pastEntries) { + entry.write(writer, options); + } + writer.endArray(); + + writer.key("future"); writer.array(); + for (HistoryEntry entry : _futureEntries) { + entry.write(writer, options); + } + writer.endArray(); + + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/history/HistoryEntry.java b/src/main/java/com/metaweb/gridworks/history/HistoryEntry.java index ba1f30aba..d16372bcd 100644 --- a/src/main/java/com/metaweb/gridworks/history/HistoryEntry.java +++ b/src/main/java/com/metaweb/gridworks/history/HistoryEntry.java @@ -20,127 +20,127 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.model.Project; public class HistoryEntry implements Serializable, Jsonizable { - private static final long serialVersionUID = 532766467813930262L; - - final public long id; - final public long projectID; - final public String description; - final public AbstractOperation operation; - final public Date time; - - transient protected Change _change; - - public HistoryEntry(Project project, String description, AbstractOperation operation, Change change) { - this.id = Math.round(Math.random() * 1000000) + System.currentTimeMillis(); - this.projectID = project.id; - this.description = description; - this.operation = operation; - this.time = new Date(); - - _change = change; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); - - writer.object(); - writer.key("id"); writer.value(id); - writer.key("description"); writer.value(description); - writer.key("time"); writer.value(sdf.format(time)); - writer.endObject(); - } - - public void apply(Project project) { - if (_change == null) { - loadChange(); - } - _change.apply(project); + private static final long serialVersionUID = 532766467813930262L; + + final public long id; + final public long projectID; + final public String description; + final public AbstractOperation operation; + final public Date time; + + transient protected Change _change; + + public HistoryEntry(Project project, String description, AbstractOperation operation, Change change) { + this.id = Math.round(Math.random() * 1000000) + System.currentTimeMillis(); + this.projectID = project.id; + this.description = description; + this.operation = operation; + this.time = new Date(); + + _change = change; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); + + writer.object(); + writer.key("id"); writer.value(id); + writer.key("description"); writer.value(description); + writer.key("time"); writer.value(sdf.format(time)); + writer.endObject(); + } + + public void apply(Project project) { + if (_change == null) { + loadChange(); + } + _change.apply(project); + + // When a change is applied, it can hang on to old data (in order to be able + // to revert later). Hence, we need to save the change out. - // When a change is applied, it can hang on to old data (in order to be able - // to revert later). Hence, we need to save the change out. - saveChange(); - } - - public void revert(Project project) { - if (_change == null) { - loadChange(); - } - _change.revert(project); - } - - public void delete() { - File file = getFile(); - if (file.exists()) { - file.delete(); - } - } - - protected void loadChange() { - File file = getFile(); - - FileInputStream fis = null; - ObjectInputStream in = null; - try { - fis = new FileInputStream(file); - in = new ObjectInputStream(fis); - - _change = (Change) in.readObject(); - } catch(IOException e) { - e.printStackTrace(); - } catch(ClassNotFoundException e) { - e.printStackTrace(); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (Exception e) { - } - } - if (in != null) { - try { - in.close(); - } catch (Exception e) { - } - } - } - } - - protected void saveChange() { - File file = getFile(); - - FileOutputStream fos = null; - ObjectOutputStream out = null; - try { - fos = new FileOutputStream(file); - out = new ObjectOutputStream(fos); - - out.writeObject(_change); - out.flush(); - } catch(IOException e) { - e.printStackTrace(); - } finally { - if (fos != null) { - try { - fos.close(); - } catch (Exception e) { - } - } - if (out != null) { - try { - out.close(); - } catch (Exception e) { - } - } - } - } - - protected File getFile() { - File dir = new File(ProjectManager.singleton.getDataDir(), projectID + ".history"); - dir.mkdirs(); - - return new File(dir, id + ".entry"); - } + } + + public void revert(Project project) { + if (_change == null) { + loadChange(); + } + _change.revert(project); + } + + public void delete() { + File file = getFile(); + if (file.exists()) { + file.delete(); + } + } + + protected void loadChange() { + File file = getFile(); + + FileInputStream fis = null; + ObjectInputStream in = null; + try { + fis = new FileInputStream(file); + in = new ObjectInputStream(fis); + + _change = (Change) in.readObject(); + } catch(IOException e) { + e.printStackTrace(); + } catch(ClassNotFoundException e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (Exception e) { + } + } + if (in != null) { + try { + in.close(); + } catch (Exception e) { + } + } + } + } + + protected void saveChange() { + File file = getFile(); + + FileOutputStream fos = null; + ObjectOutputStream out = null; + try { + fos = new FileOutputStream(file); + out = new ObjectOutputStream(fos); + + out.writeObject(_change); + out.flush(); + } catch(IOException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (Exception e) { + } + } + if (out != null) { + try { + out.close(); + } catch (Exception e) { + } + } + } + } + + protected File getFile() { + File dir = new File(ProjectManager.singleton.getDataDir(), projectID + ".history"); + dir.mkdirs(); + + return new File(dir, id + ".entry"); + } } diff --git a/src/main/java/com/metaweb/gridworks/history/HistoryProcess.java b/src/main/java/com/metaweb/gridworks/history/HistoryProcess.java index d78b8676a..e054febd4 100644 --- a/src/main/java/com/metaweb/gridworks/history/HistoryProcess.java +++ b/src/main/java/com/metaweb/gridworks/history/HistoryProcess.java @@ -10,56 +10,56 @@ import com.metaweb.gridworks.process.Process; import com.metaweb.gridworks.process.ProcessManager; public class HistoryProcess extends Process { - final protected Project _project; - final protected long _lastDoneID; - final protected String _description; - - protected boolean _done = false; + final protected Project _project; + final protected long _lastDoneID; + final protected String _description; + + protected boolean _done = false; - public HistoryProcess(Project project, long lastDoneID) { - _project = project; - _lastDoneID = lastDoneID; - - if (_lastDoneID == 0) { - _description = "Undo all"; - } else { - HistoryEntry entry = _project.history.getEntry(_lastDoneID); - _description = "Undo/redo until after " + entry.description; - } - } - - public void cancel() { - throw new RuntimeException("Not a long-running process"); - } + public HistoryProcess(Project project, long lastDoneID) { + _project = project; + _lastDoneID = lastDoneID; + + if (_lastDoneID == 0) { + _description = "Undo all"; + } else { + HistoryEntry entry = _project.history.getEntry(_lastDoneID); + _description = "Undo/redo until after " + entry.description; + } + } + + public void cancel() { + throw new RuntimeException("Not a long-running process"); + } - public boolean isImmediate() { - return true; - } + public boolean isImmediate() { + return true; + } - public void performImmediate() { - _project.history.undoRedo(_lastDoneID); - _done = true; - } + public void performImmediate() { + _project.history.undoRedo(_lastDoneID); + _done = true; + } - public void startPerforming(ProcessManager manager) { - throw new RuntimeException("Not a long-running process"); - } + public void startPerforming(ProcessManager manager) { + throw new RuntimeException("Not a long-running process"); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value(_description); - writer.key("immediate"); writer.value(true); - writer.key("status"); writer.value(_done ? "done" : "pending"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value(_description); + writer.key("immediate"); writer.value(true); + writer.key("status"); writer.value(_done ? "done" : "pending"); + writer.endObject(); + } - public boolean isDone() { - throw new RuntimeException("Not a long-running process"); - } + public boolean isDone() { + throw new RuntimeException("Not a long-running process"); + } - public boolean isRunning() { - throw new RuntimeException("Not a long-running process"); - } + public boolean isRunning() { + throw new RuntimeException("Not a long-running process"); + } } diff --git a/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java b/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java index b9f2f99b7..249e93f77 100644 --- a/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java +++ b/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java @@ -19,37 +19,37 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class ExcelImporter implements Importer { - final protected boolean _xmlBased; - - public ExcelImporter(boolean xmlBased) { - _xmlBased = xmlBased; - } + final protected boolean _xmlBased; + + public ExcelImporter(boolean xmlBased) { + _xmlBased = xmlBased; + } - public boolean takesReader() { - return false; - } - - public void read(Reader reader, Project project, Properties options, int skip, int limit) - throws Exception { - - throw new NotImplementedException(); - } + public boolean takesReader() { + return false; + } + + public void read(Reader reader, Project project, Properties options, int skip, int limit) + throws Exception { + + throw new NotImplementedException(); + } - public void read(InputStream inputStream, Project project, - Properties options, int skip, int limit) throws Exception { - + public void read(InputStream inputStream, Project project, + Properties options, int skip, int limit) throws Exception { + Workbook wb = _xmlBased ? - new XSSFWorkbook(inputStream) : - new HSSFWorkbook(new POIFSFileSystem(inputStream)); - + new XSSFWorkbook(inputStream) : + new HSSFWorkbook(new POIFSFileSystem(inputStream)); + Sheet sheet = wb.getSheetAt(0); int firstRow = sheet.getFirstRowNum(); int lastRow = sheet.getLastRowNum(); int r = firstRow; - List nonBlankIndices = null; - List nonBlankHeaderStrings = null; + List nonBlankIndices = null; + List nonBlankHeaderStrings = null; /* * Find the header row @@ -63,37 +63,37 @@ public class ExcelImporter implements Importer { short firstCell = row.getFirstCellNum(); short lastCell = row.getLastCellNum(); if (firstCell >= 0 && firstCell <= lastCell) { - nonBlankIndices = new ArrayList(lastCell - firstCell + 1); - nonBlankHeaderStrings = new ArrayList(lastCell - firstCell + 1); - + nonBlankIndices = new ArrayList(lastCell - firstCell + 1); + nonBlankHeaderStrings = new ArrayList(lastCell - firstCell + 1); + for (int c = firstCell; c <= lastCell; c++) { org.apache.poi.ss.usermodel.Cell cell = row.getCell(c); if (cell != null) { String text = cell.getStringCellValue().trim(); if (text.length() > 0) { - nonBlankIndices.add((int) c); - nonBlankHeaderStrings.add(text); + nonBlankIndices.add((int) c); + nonBlankHeaderStrings.add(text); } } } if (nonBlankIndices.size() > 0) { - r++; - break; + r++; + break; } } } if (nonBlankIndices == null || nonBlankIndices.size() == 0) { - return; + return; } /* * Create columns */ for (int c = 0; c < nonBlankIndices.size(); c++) { - Column column = new Column(c, nonBlankHeaderStrings.get(c)); - project.columnModel.columns.add(column); + Column column = new Column(c, nonBlankHeaderStrings.get(c)); + project.columnModel.columns.add(column); } /* @@ -109,54 +109,54 @@ public class ExcelImporter implements Importer { short firstCell = row.getFirstCellNum(); short lastCell = row.getLastCellNum(); if (firstCell >= 0 && firstCell <= lastCell) { - Row newRow = new Row(nonBlankIndices.size()); - boolean hasData = false; - - for (int c = 0; c < nonBlankIndices.size(); c++) { - if (c < firstCell || c > lastCell) { - continue; - } - - org.apache.poi.ss.usermodel.Cell cell = row.getCell(c); - if (cell == null) { - continue; - } - - int cellType = cell.getCellType(); - if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR || - cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK) { - continue; - } - if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA) { - cellType = cell.getCachedFormulaResultType(); - } - - Object value = null; - if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN) { - value = cell.getBooleanCellValue(); - } else if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC) { - value = cell.getNumericCellValue(); - } else { + Row newRow = new Row(nonBlankIndices.size()); + boolean hasData = false; + + for (int c = 0; c < nonBlankIndices.size(); c++) { + if (c < firstCell || c > lastCell) { + continue; + } + + org.apache.poi.ss.usermodel.Cell cell = row.getCell(c); + if (cell == null) { + continue; + } + + int cellType = cell.getCellType(); + if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR || + cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK) { + continue; + } + if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA) { + cellType = cell.getCachedFormulaResultType(); + } + + Object value = null; + if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN) { + value = cell.getBooleanCellValue(); + } else if (cellType == org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC) { + value = cell.getNumericCellValue(); + } else { String text = cell.getStringCellValue().trim(); if (text.length() > 0) { - value = text; + value = text; } } - - if (value != null) { - newRow.setCell(c, new Cell(value, null)); - hasData = true; - } + + if (value != null) { + newRow.setCell(c, new Cell(value, null)); + hasData = true; + } } if (hasData) { rowsWithData++; if (skip <= 0 || rowsWithData > skip) { - project.rows.add(newRow); - if (limit > 0 && project.rows.size() >= limit) { - break; - } + project.rows.add(newRow); + if (limit > 0 && project.rows.size() >= limit) { + break; + } } } } diff --git a/src/main/java/com/metaweb/gridworks/importers/Importer.java b/src/main/java/com/metaweb/gridworks/importers/Importer.java index af3e645c6..32b9032db 100644 --- a/src/main/java/com/metaweb/gridworks/importers/Importer.java +++ b/src/main/java/com/metaweb/gridworks/importers/Importer.java @@ -7,8 +7,8 @@ import java.util.Properties; import com.metaweb.gridworks.model.Project; public interface Importer { - public boolean takesReader(); - - public void read(Reader reader, Project project, Properties options, int skip, int limit) throws Exception; - public void read(InputStream inputStream, Project project, Properties options, int skip, int limit) throws Exception; + public boolean takesReader(); + + public void read(Reader reader, Project project, Properties options, int skip, int limit) throws Exception; + public void read(InputStream inputStream, Project project, Properties options, int skip, int limit) throws Exception; } diff --git a/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java b/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java index 96d232290..9674ce0be 100644 --- a/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java +++ b/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java @@ -6,71 +6,52 @@ import com.metaweb.gridworks.model.Row; public class ImporterUtilities { - static public Object parseCellValue(String text) { - if (text.length() > 0) { - if (text.length() > 1 && text.startsWith("\"") && text.endsWith("\"")) { - return text.substring(1, text.length() - 1); - } - - try { - return Long.parseLong(text); - } catch (NumberFormatException e) { - } - - try { - return Double.parseDouble(text); - } catch (NumberFormatException e) { - } - } - return text; - } + static public Object parseCellValue(String text) { + if (text.length() > 0) { + if (text.length() > 1 && text.startsWith("\"") && text.endsWith("\"")) { + return text.substring(1, text.length() - 1); + } + + try { + return Long.parseLong(text); + } catch (NumberFormatException e) { + } + + try { + return Double.parseDouble(text); + } catch (NumberFormatException e) { + } + } + return text; + } - static public boolean parseCSVIntoRow(Row row, String line) { - boolean hasData = false; - - int start = 0; - while (start < line.length()) { - String text = null; - - if (line.charAt(start) == '"') { - int next = line.indexOf('"', start + 1); - if (next < 0) { - text = line.substring(start); - start = line.length(); - } else { - text = line.substring(start, next + 1); - start = next + 2; - } - } else { - int next = line.indexOf(',', start); - if (next < 0) { - text = line.substring(start); - start = line.length(); - } else { - text = line.substring(start, next); - start = next + 1; - } - } - - Object value = parseCellValue(text); - if (ExpressionUtils.isNonBlankData(value)) { - row.cells.add(new Cell(value, null)); - hasData = true; - } else { - row.cells.add(null); - } - } - - return hasData; - } - - static public boolean parseTSVIntoRow(Row row, String line) { - boolean hasData = false; - - String[] cells = line.split("\t"); - for (int c = 0; c < cells.length; c++) { - String text = cells[c]; - + static public boolean parseCSVIntoRow(Row row, String line) { + boolean hasData = false; + + int start = 0; + while (start < line.length()) { + String text = null; + + if (line.charAt(start) == '"') { + int next = line.indexOf('"', start + 1); + if (next < 0) { + text = line.substring(start); + start = line.length(); + } else { + text = line.substring(start, next + 1); + start = next + 2; + } + } else { + int next = line.indexOf(',', start); + if (next < 0) { + text = line.substring(start); + start = line.length(); + } else { + text = line.substring(start, next); + start = next + 1; + } + } + Object value = parseCellValue(text); if (ExpressionUtils.isNonBlankData(value)) { row.cells.add(new Cell(value, null)); @@ -78,8 +59,27 @@ public class ImporterUtilities { } else { row.cells.add(null); } - } - return hasData; - } + } + + return hasData; + } + + static public boolean parseTSVIntoRow(Row row, String line) { + boolean hasData = false; + + String[] cells = line.split("\t"); + for (int c = 0; c < cells.length; c++) { + String text = cells[c]; + + Object value = parseCellValue(text); + if (ExpressionUtils.isNonBlankData(value)) { + row.cells.add(new Cell(value, null)); + hasData = true; + } else { + row.cells.add(null); + } + } + return hasData; + } } diff --git a/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java b/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java index d146616f5..895fdffb0 100644 --- a/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java +++ b/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java @@ -13,76 +13,76 @@ import com.metaweb.gridworks.model.Row; public class TsvCsvImporter implements Importer { - public void read(Reader reader, Project project, Properties options, int skip, int limit) - throws Exception { - - LineNumberReader lnReader = new LineNumberReader(reader); - try { - String sep = null; // auto-detect TSV or CSV - String line = null; - boolean first = true; - int cellCount = 1; - - int rowsWithData = 0; - while ((line = lnReader.readLine()) != null) { - if (line.trim().length() == 0) { - continue; - } - - if (sep == null) { - int tab = line.indexOf('\t'); - if (tab >= 0) { - sep = "\t"; - } else { - sep = ","; - } - } - - if (first) { - String[] cells = line.split(sep); - - first = false; - for (int c = 0; c < cells.length; c++) { - String cell = cells[c]; - if (cell.startsWith("\"") && cell.endsWith("\"")) { - cell = cell.substring(1, cell.length() - 1); - } - - Column column = new Column(c, cell); - - project.columnModel.columns.add(column); - } - - cellCount = cells.length; - } else { - Row row = new Row(cellCount); - - if ((sep.charAt(0) == ',') ? ImporterUtilities.parseCSVIntoRow(row, line) : ImporterUtilities.parseTSVIntoRow(row, line)) { - rowsWithData++; - - if (skip <= 0 || rowsWithData > skip) { - project.rows.add(row); - project.columnModel.setMaxCellIndex(Math.max(project.columnModel.getMaxCellIndex(), row.cells.size())); - - if (limit > 0 && project.rows.size() >= limit) { - break; - } - } - } - } - } - } finally { - lnReader.close(); - } - } + public void read(Reader reader, Project project, Properties options, int skip, int limit) + throws Exception { + + LineNumberReader lnReader = new LineNumberReader(reader); + try { + String sep = null; // auto-detect TSV or CSV + String line = null; + boolean first = true; + int cellCount = 1; + + int rowsWithData = 0; + while ((line = lnReader.readLine()) != null) { + if (line.trim().length() == 0) { + continue; + } + + if (sep == null) { + int tab = line.indexOf('\t'); + if (tab >= 0) { + sep = "\t"; + } else { + sep = ","; + } + } + + if (first) { + String[] cells = line.split(sep); + + first = false; + for (int c = 0; c < cells.length; c++) { + String cell = cells[c]; + if (cell.startsWith("\"") && cell.endsWith("\"")) { + cell = cell.substring(1, cell.length() - 1); + } + + Column column = new Column(c, cell); + + project.columnModel.columns.add(column); + } + + cellCount = cells.length; + } else { + Row row = new Row(cellCount); + + if ((sep.charAt(0) == ',') ? ImporterUtilities.parseCSVIntoRow(row, line) : ImporterUtilities.parseTSVIntoRow(row, line)) { + rowsWithData++; + + if (skip <= 0 || rowsWithData > skip) { + project.rows.add(row); + project.columnModel.setMaxCellIndex(Math.max(project.columnModel.getMaxCellIndex(), row.cells.size())); + + if (limit > 0 && project.rows.size() >= limit) { + break; + } + } + } + } + } + } finally { + lnReader.close(); + } + } - public void read(InputStream inputStream, Project project, - Properties options, int skip, int limit) throws Exception { - - throw new NotImplementedException(); - } + public void read(InputStream inputStream, Project project, + Properties options, int skip, int limit) throws Exception { + + throw new NotImplementedException(); + } - public boolean takesReader() { - return true; - } + public boolean takesReader() { + return true; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/AbstractOperation.java b/src/main/java/com/metaweb/gridworks/model/AbstractOperation.java index 0dcd40b8f..d3fed04fb 100644 --- a/src/main/java/com/metaweb/gridworks/model/AbstractOperation.java +++ b/src/main/java/com/metaweb/gridworks/model/AbstractOperation.java @@ -18,19 +18,19 @@ abstract public class AbstractOperation implements Serializable, Jsonizable { private static final long serialVersionUID = 3916055862440019600L; public Process createProcess(Project project, Properties options) throws Exception { - return new QuickHistoryEntryProcess(project, getBriefDescription(null)) { - @Override - protected HistoryEntry createHistoryEntry() throws Exception { - return AbstractOperation.this.createHistoryEntry(_project); - } - }; - } - - protected HistoryEntry createHistoryEntry(Project project) throws Exception { - throw new NotImplementedException(); - } - - protected String getBriefDescription(Project project) { - throw new NotImplementedException(); - } + return new QuickHistoryEntryProcess(project, getBriefDescription(null)) { + @Override + protected HistoryEntry createHistoryEntry() throws Exception { + return AbstractOperation.this.createHistoryEntry(_project); + } + }; + } + + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + throw new NotImplementedException(); + } + + protected String getBriefDescription(Project project) { + throw new NotImplementedException(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/Cell.java b/src/main/java/com/metaweb/gridworks/model/Cell.java index 8af556b32..5b5eecb4d 100644 --- a/src/main/java/com/metaweb/gridworks/model/Cell.java +++ b/src/main/java/com/metaweb/gridworks/model/Cell.java @@ -12,39 +12,39 @@ import com.metaweb.gridworks.expr.ExpressionUtils; import com.metaweb.gridworks.expr.HasFields; public class Cell implements Serializable, HasFields, Jsonizable { - private static final long serialVersionUID = -5891067829205458102L; - - final public Object value; - final public Recon recon; - - public Cell(Object value, Recon recon) { - this.value = value; - this.recon = recon; - } - - public Object getField(String name, Properties bindings) { - if ("value".equals(name)) { - return value; - } else if ("recon".equals(name)) { - return recon; - } - return null; - } + private static final long serialVersionUID = -5891067829205458102L; + + final public Object value; + final public Recon recon; + + public Cell(Object value, Recon recon) { + this.value = value; + this.recon = recon; + } + + public Object getField(String name, Properties bindings) { + if ("value".equals(name)) { + return value; + } else if ("recon".equals(name)) { + return recon; + } + return null; + } - public void write(JSONWriter writer, Properties options) throws JSONException { - writer.object(); - if (ExpressionUtils.isError(value)) { + public void write(JSONWriter writer, Properties options) throws JSONException { + writer.object(); + if (ExpressionUtils.isError(value)) { writer.key("e"); writer.value(((EvalError) value).message); - } else { - writer.key("v"); - writer.value(value); - } - - if (recon != null) { - writer.key("r"); - recon.write(writer, options); - } - writer.endObject(); - } + } else { + writer.key("v"); + writer.value(value); + } + + if (recon != null) { + writer.key("r"); + recon.write(writer, options); + } + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/Column.java b/src/main/java/com/metaweb/gridworks/model/Column.java index 0589a2c9e..5ea0e3883 100644 --- a/src/main/java/com/metaweb/gridworks/model/Column.java +++ b/src/main/java/com/metaweb/gridworks/model/Column.java @@ -12,36 +12,36 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.model.recon.ReconConfig; public class Column implements Serializable, Jsonizable { - private static final long serialVersionUID = -1063342490951563563L; - - final private int _cellIndex; - final private String _originalHeaderLabel; - private String _headerLabel; - private ReconConfig _reconConfig; - private ReconStats _reconStats; - - transient protected Map _precomputes; - - public Column(int cellIndex, String headerLabel) { - _cellIndex = cellIndex; - _originalHeaderLabel = _headerLabel = headerLabel; - } - - public int getCellIndex() { - return _cellIndex; - } + private static final long serialVersionUID = -1063342490951563563L; + + final private int _cellIndex; + final private String _originalHeaderLabel; + private String _headerLabel; + private ReconConfig _reconConfig; + private ReconStats _reconStats; + + transient protected Map _precomputes; + + public Column(int cellIndex, String headerLabel) { + _cellIndex = cellIndex; + _originalHeaderLabel = _headerLabel = headerLabel; + } + + public int getCellIndex() { + return _cellIndex; + } - public String getOriginalHeaderLabel() { - return _originalHeaderLabel; - } - - public void setHeaderLabel(String headerLabel) { - this._headerLabel = headerLabel; - } + public String getOriginalHeaderLabel() { + return _originalHeaderLabel; + } + + public void setHeaderLabel(String headerLabel) { + this._headerLabel = headerLabel; + } - public String getHeaderLabel() { - return _headerLabel; - } + public String getHeaderLabel() { + return _headerLabel; + } public void setReconConfig(ReconConfig config) { this._reconConfig = config; @@ -59,40 +59,40 @@ public class Column implements Serializable, Jsonizable { return _reconStats; } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("cellIndex"); writer.value(getCellIndex()); - writer.key("headerLabel"); writer.value(getHeaderLabel()); - if (_reconConfig != null) { - writer.key("reconConfig"); - _reconConfig.write(writer, options); - } - if (_reconStats != null) { - writer.key("reconStats"); - _reconStats.write(writer, options); - } - writer.endObject(); - } - - public void clearPrecomputes() { - if (_precomputes != null) { - _precomputes.clear(); - } - } - - public Object getPrecompute(String key) { - if (_precomputes != null) { - return _precomputes.get(key); - } - return null; - } - - public void setPrecompute(String key, Object value) { - if (_precomputes == null) { - _precomputes = new HashMap(); - } - _precomputes.put(key, value); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("cellIndex"); writer.value(getCellIndex()); + writer.key("headerLabel"); writer.value(getHeaderLabel()); + if (_reconConfig != null) { + writer.key("reconConfig"); + _reconConfig.write(writer, options); + } + if (_reconStats != null) { + writer.key("reconStats"); + _reconStats.write(writer, options); + } + writer.endObject(); + } + + public void clearPrecomputes() { + if (_precomputes != null) { + _precomputes.clear(); + } + } + + public Object getPrecompute(String key) { + if (_precomputes != null) { + return _precomputes.get(key); + } + return null; + } + + public void setPrecompute(String key, Object value) { + if (_precomputes == null) { + _precomputes = new HashMap(); + } + _precomputes.put(key, value); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/ColumnGroup.java b/src/main/java/com/metaweb/gridworks/model/ColumnGroup.java index 2a30d31fa..272e599a9 100644 --- a/src/main/java/com/metaweb/gridworks/model/ColumnGroup.java +++ b/src/main/java/com/metaweb/gridworks/model/ColumnGroup.java @@ -13,53 +13,53 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class ColumnGroup implements Serializable, Jsonizable { - private static final long serialVersionUID = 2161780779920066118L; + private static final long serialVersionUID = 2161780779920066118L; - final public int startColumnIndex; - final public int columnSpan; - final public int keyColumnIndex; // could be -1 if there is no key cell - - transient public ColumnGroup parentGroup; - transient public List subgroups; - - public ColumnGroup(int startColumnIndex, int columnSpan, int keyColumnIndex) { - this.startColumnIndex = startColumnIndex; - this.columnSpan = columnSpan; - this.keyColumnIndex = keyColumnIndex; - internalInitialize(); - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - - writer.key("startColumnIndex"); writer.value(startColumnIndex); - writer.key("columnSpan"); writer.value(columnSpan); - writer.key("keyColumnIndex"); writer.value(keyColumnIndex); - - if (subgroups != null && subgroups.size() > 0) { - writer.key("subgroups"); writer.array(); - for (ColumnGroup g : subgroups) { - g.write(writer, options); - } - writer.endArray(); - } - - writer.endObject(); - } - - public boolean contains(ColumnGroup g) { - return (g.startColumnIndex >= startColumnIndex && - g.startColumnIndex < startColumnIndex + columnSpan); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - internalInitialize(); - } - - protected void internalInitialize() { - subgroups = new LinkedList(); - } + final public int startColumnIndex; + final public int columnSpan; + final public int keyColumnIndex; // could be -1 if there is no key cell + + transient public ColumnGroup parentGroup; + transient public List subgroups; + + public ColumnGroup(int startColumnIndex, int columnSpan, int keyColumnIndex) { + this.startColumnIndex = startColumnIndex; + this.columnSpan = columnSpan; + this.keyColumnIndex = keyColumnIndex; + internalInitialize(); + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + + writer.key("startColumnIndex"); writer.value(startColumnIndex); + writer.key("columnSpan"); writer.value(columnSpan); + writer.key("keyColumnIndex"); writer.value(keyColumnIndex); + + if (subgroups != null && subgroups.size() > 0) { + writer.key("subgroups"); writer.array(); + for (ColumnGroup g : subgroups) { + g.write(writer, options); + } + writer.endArray(); + } + + writer.endObject(); + } + + public boolean contains(ColumnGroup g) { + return (g.startColumnIndex >= startColumnIndex && + g.startColumnIndex < startColumnIndex + columnSpan); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + internalInitialize(); + } + + protected void internalInitialize() { + subgroups = new LinkedList(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/ColumnModel.java b/src/main/java/com/metaweb/gridworks/model/ColumnModel.java index 504be7c50..50fe9f038 100644 --- a/src/main/java/com/metaweb/gridworks/model/ColumnModel.java +++ b/src/main/java/com/metaweb/gridworks/model/ColumnModel.java @@ -17,125 +17,125 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class ColumnModel implements Serializable, Jsonizable { - private static final long serialVersionUID = 7679639795211544511L; - - final public List columns = new LinkedList(); - final public List columnGroups = new LinkedList(); - - private int _maxCellIndex; - private int _keyColumnIndex; - - transient protected Map _nameToColumn; - transient protected Map _cellIndexToColumn; - transient protected List _rootColumnGroups; - - public ColumnModel() { - internalInitialize(); - } - - public void setMaxCellIndex(int maxCellIndex) { - this._maxCellIndex = Math.max(this._maxCellIndex, maxCellIndex); - } + private static final long serialVersionUID = 7679639795211544511L; + + final public List columns = new LinkedList(); + final public List columnGroups = new LinkedList(); + + private int _maxCellIndex; + private int _keyColumnIndex; + + transient protected Map _nameToColumn; + transient protected Map _cellIndexToColumn; + transient protected List _rootColumnGroups; + + public ColumnModel() { + internalInitialize(); + } + + public void setMaxCellIndex(int maxCellIndex) { + this._maxCellIndex = Math.max(this._maxCellIndex, maxCellIndex); + } - public int getMaxCellIndex() { - return _maxCellIndex; - } + public int getMaxCellIndex() { + return _maxCellIndex; + } public int allocateNewCellIndex() { return ++_maxCellIndex; } - public void setKeyColumnIndex(int keyColumnIndex) { - // TODO: check validity of new cell index, e.g., it's not in any group - this._keyColumnIndex = keyColumnIndex; - } + public void setKeyColumnIndex(int keyColumnIndex) { + // TODO: check validity of new cell index, e.g., it's not in any group + this._keyColumnIndex = keyColumnIndex; + } - public int getKeyColumnIndex() { - return _keyColumnIndex; - } + public int getKeyColumnIndex() { + return _keyColumnIndex; + } - public void update() { - generateMaps(); - } - - public Column getColumnByName(String name) { - return _nameToColumn.get(name); - } - - public Column getColumnByCellIndex(int cellIndex) { - return _cellIndexToColumn.get(cellIndex); - } + public void update() { + generateMaps(); + } + + public Column getColumnByName(String name) { + return _nameToColumn.get(name); + } + + public Column getColumnByCellIndex(int cellIndex) { + return _cellIndexToColumn.get(cellIndex); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - - writer.key("columns"); - writer.array(); - for (Column column : columns) { - column.write(writer, options); - } - writer.endArray(); - - writer.key("keyCellIndex"); writer.value(getKeyColumnIndex()); - writer.key("keyColumnName"); writer.value(columns.get(_keyColumnIndex).getHeaderLabel()); - writer.key("columnGroups"); - writer.array(); - for (ColumnGroup g : _rootColumnGroups) { - g.write(writer, options); - } - writer.endArray(); - - writer.endObject(); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - internalInitialize(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + + writer.key("columns"); + writer.array(); + for (Column column : columns) { + column.write(writer, options); + } + writer.endArray(); + + writer.key("keyCellIndex"); writer.value(getKeyColumnIndex()); + writer.key("keyColumnName"); writer.value(columns.get(_keyColumnIndex).getHeaderLabel()); + writer.key("columnGroups"); + writer.array(); + for (ColumnGroup g : _rootColumnGroups) { + g.write(writer, options); + } + writer.endArray(); + + writer.endObject(); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + internalInitialize(); + } - protected void internalInitialize() { - generateMaps(); - - // Turn the flat list of column groups into a tree - - _rootColumnGroups = new LinkedList(columnGroups); - Collections.sort(_rootColumnGroups, new Comparator() { - public int compare(ColumnGroup o1, ColumnGroup o2) { - int firstDiff = o1.startColumnIndex - o2.startColumnIndex; - return firstDiff != 0 ? - firstDiff : // whichever group that starts first goes first - (o2.columnSpan - o1.columnSpan); // otherwise, the larger group goes first - } - }); - - for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) { - ColumnGroup g = _rootColumnGroups.get(i); - - for (int j = i + 1; j < _rootColumnGroups.size(); j++) { - ColumnGroup g2 = _rootColumnGroups.get(j); - if (g2.parentGroup == null && g.contains(g2)) { - g2.parentGroup = g; - g.subgroups.add(g2); - } - } - } - - for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) { - if (_rootColumnGroups.get(i).parentGroup != null) { - _rootColumnGroups.remove(i); - } - } - } - - protected void generateMaps() { - _nameToColumn = new HashMap(); - _cellIndexToColumn = new HashMap(); - - for (Column column : columns) { - _nameToColumn.put(column.getHeaderLabel(), column); - _cellIndexToColumn.put(column.getCellIndex(), column); - } - } + protected void internalInitialize() { + generateMaps(); + + // Turn the flat list of column groups into a tree + + _rootColumnGroups = new LinkedList(columnGroups); + Collections.sort(_rootColumnGroups, new Comparator() { + public int compare(ColumnGroup o1, ColumnGroup o2) { + int firstDiff = o1.startColumnIndex - o2.startColumnIndex; + return firstDiff != 0 ? + firstDiff : // whichever group that starts first goes first + (o2.columnSpan - o1.columnSpan); // otherwise, the larger group goes first + } + }); + + for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) { + ColumnGroup g = _rootColumnGroups.get(i); + + for (int j = i + 1; j < _rootColumnGroups.size(); j++) { + ColumnGroup g2 = _rootColumnGroups.get(j); + if (g2.parentGroup == null && g.contains(g2)) { + g2.parentGroup = g; + g.subgroups.add(g2); + } + } + } + + for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) { + if (_rootColumnGroups.get(i).parentGroup != null) { + _rootColumnGroups.remove(i); + } + } + } + + protected void generateMaps() { + _nameToColumn = new HashMap(); + _cellIndexToColumn = new HashMap(); + + for (Column column : columns) { + _nameToColumn.put(column.getHeaderLabel(), column); + _cellIndexToColumn.put(column.getCellIndex(), column); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/model/Project.java b/src/main/java/com/metaweb/gridworks/model/Project.java index 694709f0c..d132c1546 100644 --- a/src/main/java/com/metaweb/gridworks/model/Project.java +++ b/src/main/java/com/metaweb/gridworks/model/Project.java @@ -14,149 +14,149 @@ import com.metaweb.gridworks.process.ProcessManager; import com.metaweb.gridworks.protograph.Protograph; public class Project implements Serializable { - private static final long serialVersionUID = -5089046824819472570L; - - final public long id; - - final public ColumnModel columnModel = new ColumnModel(); - final public List rows = new ArrayList(); - final public History history; - public Protograph protograph; - - transient public ProcessManager processManager; - - public Project() { - id = Math.round(Math.random() * 1000000) + System.currentTimeMillis(); - history = new History(this); - - internalInitialize(); - } - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - internalInitialize(); - } - - protected void internalInitialize() { - processManager = new ProcessManager(); - - recomputeRowContextDependencies(); - } - - static protected class Group { - int[] cellIndices; - int keyCellIndex; - } - - public void recomputeRowContextDependencies() { - List keyedGroups = new ArrayList(); - - addRootKeyedGroup(keyedGroups); - - for (ColumnGroup group : columnModel.columnGroups) { - if (group.keyColumnIndex >= 0) { - Group keyedGroup = new Group(); - keyedGroup.keyCellIndex = columnModel.columns.get(group.keyColumnIndex).getCellIndex(); - keyedGroup.cellIndices = new int[group.columnSpan - 1]; - - int c = 0; - for (int i = 0; i < group.columnSpan; i++) { - int columnIndex = group.startColumnIndex + i; - if (columnIndex != group.keyColumnIndex) { - int cellIndex = columnModel.columns.get(columnIndex).getCellIndex(); - keyedGroup.cellIndices[c++] = cellIndex; - } - } - - keyedGroups.add(keyedGroup); - } - } - - Collections.sort(keyedGroups, new Comparator() { - public int compare(Group o1, Group o2) { - return o2.cellIndices.length - o1.cellIndices.length; // larger groups first - } - }); - - int[] lastNonBlankRowsByGroup = new int[keyedGroups.size()]; - for (int i = 0; i < lastNonBlankRowsByGroup.length; i++) { - lastNonBlankRowsByGroup[i] = -1; - } - - int recordIndex = 0; - for (int r = 0; r < rows.size(); r++) { - Row row = rows.get(r); - row.contextRowSlots = null; - row.contextCellSlots = null; - - for (int g = 0; g < keyedGroups.size(); g++) { - Group group = keyedGroups.get(g); - - if (!ExpressionUtils.isNonBlankData(row.getCellValue(group.keyCellIndex))) { - int contextRowIndex = lastNonBlankRowsByGroup[g]; - if (contextRowIndex >= 0) { - for (int dependentCellIndex : group.cellIndices) { - if (ExpressionUtils.isNonBlankData(row.getCellValue(dependentCellIndex))) { - setRowDependency( - row, - dependentCellIndex, - contextRowIndex, - group.keyCellIndex - ); - } - } - } - } else { - lastNonBlankRowsByGroup[g] = r; - } - } - - if (row.contextRowSlots != null) { - row.recordIndex = -1; - row.contextRows = new ArrayList(); - for (int index : row.contextRowSlots) { - if (index >= 0) { - row.contextRows.add(index); - } - } - Collections.sort(row.contextRows); - } else { - row.recordIndex = recordIndex++; - } - } - } - - protected void addRootKeyedGroup(List keyedGroups) { - int count = columnModel.getMaxCellIndex() + 1; - if (count > 0 && columnModel.getKeyColumnIndex() < columnModel.columns.size()) { - Group rootKeyedGroup = new Group(); - - rootKeyedGroup.cellIndices = new int[count - 1]; - rootKeyedGroup.keyCellIndex = columnModel.columns.get(columnModel.getKeyColumnIndex()).getCellIndex(); - for (int i = 0; i < count; i++) { - if (i < rootKeyedGroup.keyCellIndex) { - rootKeyedGroup.cellIndices[i] = i; - } else if (i > rootKeyedGroup.keyCellIndex) { - rootKeyedGroup.cellIndices[i - 1] = i; - } - } - keyedGroups.add(rootKeyedGroup); - } - } - - protected void setRowDependency(Row row, int cellIndex, int contextRowIndex, int contextCellIndex) { - int count = columnModel.getMaxCellIndex() + 1; - if (row.contextRowSlots == null || row.contextCellSlots == null) { - row.contextRowSlots = new int[count]; - row.contextCellSlots = new int[count]; - - for (int i = 0; i < count; i++) { - row.contextRowSlots[i] = -1; - row.contextCellSlots[i] = -1; - } - } - - row.contextRowSlots[cellIndex] = contextRowIndex; - row.contextCellSlots[cellIndex] = contextCellIndex; - } + private static final long serialVersionUID = -5089046824819472570L; + + final public long id; + + final public ColumnModel columnModel = new ColumnModel(); + final public List rows = new ArrayList(); + final public History history; + public Protograph protograph; + + transient public ProcessManager processManager; + + public Project() { + id = Math.round(Math.random() * 1000000) + System.currentTimeMillis(); + history = new History(this); + + internalInitialize(); + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.defaultReadObject(); + internalInitialize(); + } + + protected void internalInitialize() { + processManager = new ProcessManager(); + + recomputeRowContextDependencies(); + } + + static protected class Group { + int[] cellIndices; + int keyCellIndex; + } + + public void recomputeRowContextDependencies() { + List keyedGroups = new ArrayList(); + + addRootKeyedGroup(keyedGroups); + + for (ColumnGroup group : columnModel.columnGroups) { + if (group.keyColumnIndex >= 0) { + Group keyedGroup = new Group(); + keyedGroup.keyCellIndex = columnModel.columns.get(group.keyColumnIndex).getCellIndex(); + keyedGroup.cellIndices = new int[group.columnSpan - 1]; + + int c = 0; + for (int i = 0; i < group.columnSpan; i++) { + int columnIndex = group.startColumnIndex + i; + if (columnIndex != group.keyColumnIndex) { + int cellIndex = columnModel.columns.get(columnIndex).getCellIndex(); + keyedGroup.cellIndices[c++] = cellIndex; + } + } + + keyedGroups.add(keyedGroup); + } + } + + Collections.sort(keyedGroups, new Comparator() { + public int compare(Group o1, Group o2) { + return o2.cellIndices.length - o1.cellIndices.length; // larger groups first + } + }); + + int[] lastNonBlankRowsByGroup = new int[keyedGroups.size()]; + for (int i = 0; i < lastNonBlankRowsByGroup.length; i++) { + lastNonBlankRowsByGroup[i] = -1; + } + + int recordIndex = 0; + for (int r = 0; r < rows.size(); r++) { + Row row = rows.get(r); + row.contextRowSlots = null; + row.contextCellSlots = null; + + for (int g = 0; g < keyedGroups.size(); g++) { + Group group = keyedGroups.get(g); + + if (!ExpressionUtils.isNonBlankData(row.getCellValue(group.keyCellIndex))) { + int contextRowIndex = lastNonBlankRowsByGroup[g]; + if (contextRowIndex >= 0) { + for (int dependentCellIndex : group.cellIndices) { + if (ExpressionUtils.isNonBlankData(row.getCellValue(dependentCellIndex))) { + setRowDependency( + row, + dependentCellIndex, + contextRowIndex, + group.keyCellIndex + ); + } + } + } + } else { + lastNonBlankRowsByGroup[g] = r; + } + } + + if (row.contextRowSlots != null) { + row.recordIndex = -1; + row.contextRows = new ArrayList(); + for (int index : row.contextRowSlots) { + if (index >= 0) { + row.contextRows.add(index); + } + } + Collections.sort(row.contextRows); + } else { + row.recordIndex = recordIndex++; + } + } + } + + protected void addRootKeyedGroup(List keyedGroups) { + int count = columnModel.getMaxCellIndex() + 1; + if (count > 0 && columnModel.getKeyColumnIndex() < columnModel.columns.size()) { + Group rootKeyedGroup = new Group(); + + rootKeyedGroup.cellIndices = new int[count - 1]; + rootKeyedGroup.keyCellIndex = columnModel.columns.get(columnModel.getKeyColumnIndex()).getCellIndex(); + for (int i = 0; i < count; i++) { + if (i < rootKeyedGroup.keyCellIndex) { + rootKeyedGroup.cellIndices[i] = i; + } else if (i > rootKeyedGroup.keyCellIndex) { + rootKeyedGroup.cellIndices[i - 1] = i; + } + } + keyedGroups.add(rootKeyedGroup); + } + } + + protected void setRowDependency(Row row, int cellIndex, int contextRowIndex, int contextCellIndex) { + int count = columnModel.getMaxCellIndex() + 1; + if (row.contextRowSlots == null || row.contextCellSlots == null) { + row.contextRowSlots = new int[count]; + row.contextCellSlots = new int[count]; + + for (int i = 0; i < count; i++) { + row.contextRowSlots[i] = -1; + row.contextCellSlots[i] = -1; + } + } + + row.contextRowSlots[cellIndex] = contextRowIndex; + row.contextCellSlots[cellIndex] = contextCellIndex; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/Recon.java b/src/main/java/com/metaweb/gridworks/model/Recon.java index ab6138079..8ff0bdfaa 100644 --- a/src/main/java/com/metaweb/gridworks/model/Recon.java +++ b/src/main/java/com/metaweb/gridworks/model/Recon.java @@ -14,158 +14,158 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.expr.HasFields; public class Recon implements Serializable, HasFields, Jsonizable { - private static final long serialVersionUID = 8906257833709315762L; - - static public enum Judgment { - None, - Matched, - New - } - - static public String judgmentToString(Judgment judgment) { - if (judgment == Judgment.Matched) { - return "matched"; - } else if (judgment == Judgment.New) { - return "new"; - } else { - return "none"; - } - } - - static public Judgment stringToJudgment(String s) { - if ("matched".equals(s)) { - return Judgment.Matched; - } else if ("new".equals(s)) { - return Judgment.New; - } else { - return Judgment.None; - } - } - - - static public int Feature_typeMatch = 0; - static public int Feature_nameMatch = 1; - static public int Feature_nameLevenshtein = 2; - static public int Feature_nameWordDistance = 3; - static public int Feature_max = 4; + private static final long serialVersionUID = 8906257833709315762L; + + static public enum Judgment { + None, + Matched, + New + } + + static public String judgmentToString(Judgment judgment) { + if (judgment == Judgment.Matched) { + return "matched"; + } else if (judgment == Judgment.New) { + return "new"; + } else { + return "none"; + } + } + + static public Judgment stringToJudgment(String s) { + if ("matched".equals(s)) { + return Judgment.Matched; + } else if ("new".equals(s)) { + return Judgment.New; + } else { + return Judgment.None; + } + } + + + static public int Feature_typeMatch = 0; + static public int Feature_nameMatch = 1; + static public int Feature_nameLevenshtein = 2; + static public int Feature_nameWordDistance = 3; + static public int Feature_max = 4; - static protected Map s_featureMap; - static { - s_featureMap = new HashMap(); - s_featureMap.put("typeMatch", Feature_typeMatch); - s_featureMap.put("nameMatch", Feature_nameMatch); - s_featureMap.put("nameLevenshtein", Feature_nameLevenshtein); - s_featureMap.put("nameWordDistance", Feature_nameWordDistance); - } - - final public long id; - public Object[] features = new Object[Feature_max]; - public List candidates; - public Judgment judgment = Judgment.None; - public ReconCandidate match = null; - - public Recon() { - id = System.currentTimeMillis() * 1000000 + Math.round(Math.random() * 1000000); - } - - public Recon dup() { - Recon r = new Recon(); - - System.arraycopy(features, 0, r.features, 0, features.length); - - if (candidates != null) { - r.candidates = new ArrayList(candidates); - } - r.judgment = judgment; - r.match = match; - return r; - } - - public void addCandidate(ReconCandidate candidate) { - if (candidates == null) { - candidates = new ArrayList(3); - } - candidates.add(candidate); - } - - public ReconCandidate getBestCandidate() { - if (candidates != null && candidates.size() > 0) { - return candidates.get(0); - } - return null; - } - - public Object getFeature(int feature) { - return feature < features.length ? features[feature] : null; - } - - public void setFeature(int feature, Object v) { - if (feature >= features.length) { - if (feature >= Feature_max) { - return; - } - - // We deserialized this object from an older version of the class - // that had fewer features, so we can just try to extend it - - Object[] newFeatures = new Object[Feature_max]; - - System.arraycopy(features, 0, newFeatures, 0, features.length); - - features = newFeatures; - } - - features[feature] = v; - } - - public Object getField(String name, Properties bindings) { - if ("best".equals(name)) { - return candidates != null && candidates.size() > 0 ? candidates.get(0) : null; - } else if ("judgment".equals(name) || "judgement".equals(name)) { - return judgmentToString(); - } else if ("matched".equals(name)) { - return judgment == Judgment.Matched; - } else if ("new".equals(name)) { - return judgment == Judgment.New; - } else if ("match".equals(name)) { - return match; - } else if ("features".equals(name)) { - return new Features(); - } - return null; - } - - protected String judgmentToString() { - return judgmentToString(judgment); - } - - public class Features implements HasFields { - public Object getField(String name, Properties bindings) { - int index = s_featureMap.get(name); - return index < features.length ? features[index] : null; - } - } + static protected Map s_featureMap; + static { + s_featureMap = new HashMap(); + s_featureMap.put("typeMatch", Feature_typeMatch); + s_featureMap.put("nameMatch", Feature_nameMatch); + s_featureMap.put("nameLevenshtein", Feature_nameLevenshtein); + s_featureMap.put("nameWordDistance", Feature_nameWordDistance); + } + + final public long id; + public Object[] features = new Object[Feature_max]; + public List candidates; + public Judgment judgment = Judgment.None; + public ReconCandidate match = null; + + public Recon() { + id = System.currentTimeMillis() * 1000000 + Math.round(Math.random() * 1000000); + } + + public Recon dup() { + Recon r = new Recon(); + + System.arraycopy(features, 0, r.features, 0, features.length); + + if (candidates != null) { + r.candidates = new ArrayList(candidates); + } + r.judgment = judgment; + r.match = match; + return r; + } + + public void addCandidate(ReconCandidate candidate) { + if (candidates == null) { + candidates = new ArrayList(3); + } + candidates.add(candidate); + } + + public ReconCandidate getBestCandidate() { + if (candidates != null && candidates.size() > 0) { + return candidates.get(0); + } + return null; + } + + public Object getFeature(int feature) { + return feature < features.length ? features[feature] : null; + } + + public void setFeature(int feature, Object v) { + if (feature >= features.length) { + if (feature >= Feature_max) { + return; + } + + // We deserialized this object from an older version of the class + // that had fewer features, so we can just try to extend it + + Object[] newFeatures = new Object[Feature_max]; + + System.arraycopy(features, 0, newFeatures, 0, features.length); + + features = newFeatures; + } + + features[feature] = v; + } + + public Object getField(String name, Properties bindings) { + if ("best".equals(name)) { + return candidates != null && candidates.size() > 0 ? candidates.get(0) : null; + } else if ("judgment".equals(name) || "judgement".equals(name)) { + return judgmentToString(); + } else if ("matched".equals(name)) { + return judgment == Judgment.Matched; + } else if ("new".equals(name)) { + return judgment == Judgment.New; + } else if ("match".equals(name)) { + return match; + } else if ("features".equals(name)) { + return new Features(); + } + return null; + } + + protected String judgmentToString() { + return judgmentToString(judgment); + } + + public class Features implements HasFields { + public Object getField(String name, Properties bindings) { + int index = s_featureMap.get(name); + return index < features.length ? features[index] : null; + } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("j"); - writer.value(judgmentToString()); - - if (match != null) { - writer.key("m"); - match.write(writer, options); - } else { - writer.key("c"); writer.array(); - if (candidates != null) { - for (ReconCandidate c : candidates) { - c.write(writer, options); - } - } - writer.endArray(); - } - - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("j"); + writer.value(judgmentToString()); + + if (match != null) { + writer.key("m"); + match.write(writer, options); + } else { + writer.key("c"); writer.array(); + if (candidates != null) { + for (ReconCandidate c : candidates) { + c.write(writer, options); + } + } + writer.endArray(); + } + + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/ReconCandidate.java b/src/main/java/com/metaweb/gridworks/model/ReconCandidate.java index c73f449dc..aab23344d 100644 --- a/src/main/java/com/metaweb/gridworks/model/ReconCandidate.java +++ b/src/main/java/com/metaweb/gridworks/model/ReconCandidate.java @@ -10,51 +10,51 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.expr.HasFields; public class ReconCandidate implements Serializable, HasFields, Jsonizable { - private static final long serialVersionUID = -8013997214978715606L; - - final public String topicID; - final public String topicGUID; - final public String topicName; - final public String[] typeIDs; - final public double score; - - public ReconCandidate(String topicID, String topicGUID, String topicName, String[] typeIDs, double score) { - this.topicID = topicID; - this.topicGUID = topicGUID; - this.topicName = topicName; - this.typeIDs = typeIDs; - this.score = score; - } - - public Object getField(String name, Properties bindings) { - if ("id".equals(name)) { - return topicName; - } else if ("guid".equals(name)) { - return topicGUID; - } else if ("name".equals(name)) { - return topicName; - } else if ("type".equals(name)) { - return typeIDs; - } else if ("score".equals(name)) { - return score; - } - return null; - } + private static final long serialVersionUID = -8013997214978715606L; + + final public String topicID; + final public String topicGUID; + final public String topicName; + final public String[] typeIDs; + final public double score; + + public ReconCandidate(String topicID, String topicGUID, String topicName, String[] typeIDs, double score) { + this.topicID = topicID; + this.topicGUID = topicGUID; + this.topicName = topicName; + this.typeIDs = typeIDs; + this.score = score; + } + + public Object getField(String name, Properties bindings) { + if ("id".equals(name)) { + return topicName; + } else if ("guid".equals(name)) { + return topicGUID; + } else if ("name".equals(name)) { + return topicName; + } else if ("type".equals(name)) { + return typeIDs; + } else if ("score".equals(name)) { + return score; + } + return null; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("id"); writer.value(topicID); - writer.key("guid"); writer.value(topicGUID); - writer.key("name"); writer.value(topicName); - writer.key("score"); writer.value(score); - writer.key("types"); writer.array(); - for (String typeID : typeIDs) { - writer.value(typeID); - } - writer.endArray(); - - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("id"); writer.value(topicID); + writer.key("guid"); writer.value(topicGUID); + writer.key("name"); writer.value(topicName); + writer.key("score"); writer.value(score); + writer.key("types"); writer.array(); + for (String typeID : typeIDs) { + writer.value(typeID); + } + writer.endArray(); + + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/ReconStats.java b/src/main/java/com/metaweb/gridworks/model/ReconStats.java index 69fec08a2..c4949663a 100644 --- a/src/main/java/com/metaweb/gridworks/model/ReconStats.java +++ b/src/main/java/com/metaweb/gridworks/model/ReconStats.java @@ -13,9 +13,9 @@ import com.metaweb.gridworks.model.Recon.Judgment; public class ReconStats implements Serializable, Jsonizable { private static final long serialVersionUID = -4831409797104437854L; - final public int nonBlanks; - final public int newTopics; - final public int matchedTopics; + final public int nonBlanks; + final public int newTopics; + final public int matchedTopics; public ReconStats(int nonBlanks, int newTopics, int matchedTopics) { this.nonBlanks = nonBlanks; @@ -27,32 +27,32 @@ public class ReconStats implements Serializable, Jsonizable { throws JSONException { writer.object(); - writer.key("nonBlanks"); writer.value(nonBlanks); - writer.key("newTopics"); writer.value(newTopics); - writer.key("matchedTopics"); writer.value(matchedTopics); + writer.key("nonBlanks"); writer.value(nonBlanks); + writer.key("newTopics"); writer.value(newTopics); + writer.key("matchedTopics"); writer.value(matchedTopics); writer.endObject(); } static public ReconStats create(Project project, int cellIndex) { - int nonBlanks = 0; - int newTopics = 0; - int matchedTopics = 0; - - for (Row row : project.rows) { - Cell cell = row.getCell(cellIndex); - if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { - nonBlanks++; - - if (cell.recon != null) { - if (cell.recon.judgment == Judgment.New) { - newTopics++; - } else if (cell.recon.judgment == Judgment.Matched) { - matchedTopics++; - } - } - } - } - - return new ReconStats(nonBlanks, newTopics, matchedTopics); + int nonBlanks = 0; + int newTopics = 0; + int matchedTopics = 0; + + for (Row row : project.rows) { + Cell cell = row.getCell(cellIndex); + if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { + nonBlanks++; + + if (cell.recon != null) { + if (cell.recon.judgment == Judgment.New) { + newTopics++; + } else if (cell.recon.judgment == Judgment.Matched) { + matchedTopics++; + } + } + } + } + + return new ReconStats(nonBlanks, newTopics, matchedTopics); } } diff --git a/src/main/java/com/metaweb/gridworks/model/Row.java b/src/main/java/com/metaweb/gridworks/model/Row.java index 39154c90f..c5b28f8da 100644 --- a/src/main/java/com/metaweb/gridworks/model/Row.java +++ b/src/main/java/com/metaweb/gridworks/model/Row.java @@ -12,59 +12,59 @@ import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.expr.HasFields; public class Row implements Serializable, HasFields, Jsonizable { - private static final long serialVersionUID = -689264211730915507L; - - public boolean flagged; - public boolean starred; - final public List cells; - - transient public int recordIndex; // -1 for rows that are not main record rows - transient public List contextRows; - transient public int[] contextRowSlots; - transient public int[] contextCellSlots; - - public Row(int cellCount) { - cells = new ArrayList(cellCount); - } - - public Row dup() { - Row row = new Row(cells.size()); - row.flagged = flagged; - row.starred = starred; - row.cells.addAll(cells); - return row; - } - - public Object getField(String name, Properties bindings) { - if ("flagged".equals(name)) { - return flagged; - } else if ("starred".equals(name)) { - return starred; - } else if ("cells".equals(name)) { - return new Cells(); - } else if ("index".equals(name)) { - return bindings.get("rowIndex"); - } - return null; - } - - public boolean isEmpty() { - for (Cell cell : cells) { - if (cell != null && cell.value != null && !isValueBlank(cell.value)) { - return false; - } - } - return true; - } - - public Cell getCell(int cellIndex) { + private static final long serialVersionUID = -689264211730915507L; + + public boolean flagged; + public boolean starred; + final public List cells; + + transient public int recordIndex; // -1 for rows that are not main record rows + transient public List contextRows; + transient public int[] contextRowSlots; + transient public int[] contextCellSlots; + + public Row(int cellCount) { + cells = new ArrayList(cellCount); + } + + public Row dup() { + Row row = new Row(cells.size()); + row.flagged = flagged; + row.starred = starred; + row.cells.addAll(cells); + return row; + } + + public Object getField(String name, Properties bindings) { + if ("flagged".equals(name)) { + return flagged; + } else if ("starred".equals(name)) { + return starred; + } else if ("cells".equals(name)) { + return new Cells(); + } else if ("index".equals(name)) { + return bindings.get("rowIndex"); + } + return null; + } + + public boolean isEmpty() { + for (Cell cell : cells) { + if (cell != null && cell.value != null && !isValueBlank(cell.value)) { + return false; + } + } + return true; + } + + public Cell getCell(int cellIndex) { if (cellIndex < cells.size()) { return cells.get(cellIndex); } else { return null; } - } - + } + public Object getCellValue(int cellIndex) { if (cellIndex < cells.size()) { Cell cell = cells.get(cellIndex); @@ -83,66 +83,66 @@ public class Row implements Serializable, HasFields, Jsonizable { return value == null || !(value instanceof String) || ((String) value).trim().length() == 0; } - public void setCell(int cellIndex, Cell cell) { - if (cellIndex < cells.size()) { - cells.set(cellIndex, cell); - } else { - while (cellIndex > cells.size()) { - cells.add(null); - } - cells.add(cell); - } - } - - public class Cells implements HasFields { - private Cells() {}; + public void setCell(int cellIndex, Cell cell) { + if (cellIndex < cells.size()) { + cells.set(cellIndex, cell); + } else { + while (cellIndex > cells.size()) { + cells.add(null); + } + cells.add(cell); + } + } + + public class Cells implements HasFields { + private Cells() {}; - public Object getField(String name, Properties bindings) { - Project project = (Project) bindings.get("project"); - Column column = project.columnModel.getColumnByName(name); - if (column != null) { - int cellIndex = column.getCellIndex(); - return getCell(cellIndex); - } - return null; - } - - } + public Object getField(String name, Properties bindings) { + Project project = (Project) bindings.get("project"); + Column column = project.columnModel.getColumnByName(name); + if (column != null) { + int cellIndex = column.getCellIndex(); + return getCell(cellIndex); + } + return null; + } + + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("flagged"); writer.value(flagged); - writer.key("starred"); writer.value(starred); - - writer.key("cells"); writer.array(); - for (Cell cell : cells) { - if (cell != null) { - cell.write(writer, options); - } else { - writer.value(null); - } - } - writer.endArray(); - - if (recordIndex >= 0) { - writer.key("j"); writer.value(recordIndex); - } - - if (options.containsKey("rowIndex")) { - writer.key("i"); writer.value(options.get("rowIndex")); - } - if (options.containsKey("extra")) { - Properties extra = (Properties) options.get("extra"); - if (extra != null) { - for (Object key : extra.keySet()) { - writer.key((String) key); - writer.value(extra.get(key)); - } - } - } - - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("flagged"); writer.value(flagged); + writer.key("starred"); writer.value(starred); + + writer.key("cells"); writer.array(); + for (Cell cell : cells) { + if (cell != null) { + cell.write(writer, options); + } else { + writer.value(null); + } + } + writer.endArray(); + + if (recordIndex >= 0) { + writer.key("j"); writer.value(recordIndex); + } + + if (options.containsKey("rowIndex")) { + writer.key("i"); writer.value(options.get("rowIndex")); + } + if (options.containsKey("extra")) { + Properties extra = (Properties) options.get("extra"); + if (extra != null) { + for (Object key : extra.keySet()) { + writer.key((String) key); + writer.value(extra.get(key)); + } + } + } + + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/changes/CellChange.java b/src/main/java/com/metaweb/gridworks/model/changes/CellChange.java index 75924fa9c..dbb7a6737 100644 --- a/src/main/java/com/metaweb/gridworks/model/changes/CellChange.java +++ b/src/main/java/com/metaweb/gridworks/model/changes/CellChange.java @@ -5,25 +5,25 @@ import com.metaweb.gridworks.model.Cell; import com.metaweb.gridworks.model.Project; public class CellChange implements Change { - private static final long serialVersionUID = -2637405780084390883L; - - final public int row; - final public int cellIndex; - final public Cell oldCell; - final public Cell newCell; - - public CellChange(int row, int column, Cell oldCell, Cell newCell) { - this.row = row; - this.cellIndex = column; - this.oldCell = oldCell; - this.newCell = newCell; - } + private static final long serialVersionUID = -2637405780084390883L; + + final public int row; + final public int cellIndex; + final public Cell oldCell; + final public Cell newCell; + + public CellChange(int row, int column, Cell oldCell, Cell newCell) { + this.row = row; + this.cellIndex = column; + this.oldCell = oldCell; + this.newCell = newCell; + } - public void apply(Project project) { - project.rows.get(row).setCell(cellIndex, newCell); - } + public void apply(Project project) { + project.rows.get(row).setCell(cellIndex, newCell); + } - public void revert(Project project) { - project.rows.get(row).setCell(cellIndex, oldCell); - } + public void revert(Project project) { + project.rows.get(row).setCell(cellIndex, oldCell); + } } diff --git a/src/main/java/com/metaweb/gridworks/model/changes/ColumnAdditionChange.java b/src/main/java/com/metaweb/gridworks/model/changes/ColumnAdditionChange.java index 7fbd2e500..8436abeef 100644 --- a/src/main/java/com/metaweb/gridworks/model/changes/ColumnAdditionChange.java +++ b/src/main/java/com/metaweb/gridworks/model/changes/ColumnAdditionChange.java @@ -23,19 +23,19 @@ public class ColumnAdditionChange extends ColumnChange { public void apply(Project project) { synchronized (project) { - if (_newCellIndex < 0) { - _newCellIndex = project.columnModel.allocateNewCellIndex(); - } - + if (_newCellIndex < 0) { + _newCellIndex = project.columnModel.allocateNewCellIndex(); + } + Column column = new Column(_newCellIndex, _headerLabel); project.columnModel.columns.add(_columnIndex, column); try { - for (CellAtRow cell : _newCells) { - project.rows.get(cell.row).setCell(_newCellIndex, cell.cell); - } + for (CellAtRow cell : _newCells) { + project.rows.get(cell.row).setCell(_newCellIndex, cell.cell); + } } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); } project.columnModel.update(); project.recomputeRowContextDependencies(); @@ -45,8 +45,8 @@ public class ColumnAdditionChange extends ColumnChange { public void revert(Project project) { synchronized (project) { for (CellAtRow cell : _newCells) { - Row row = project.rows.get(cell.row); - row.setCell(_newCellIndex, null); + Row row = project.rows.get(cell.row); + row.setCell(_newCellIndex, null); } project.columnModel.columns.remove(_columnIndex); diff --git a/src/main/java/com/metaweb/gridworks/model/changes/MassCellChange.java b/src/main/java/com/metaweb/gridworks/model/changes/MassCellChange.java index 35ca70761..3a7b35b90 100644 --- a/src/main/java/com/metaweb/gridworks/model/changes/MassCellChange.java +++ b/src/main/java/com/metaweb/gridworks/model/changes/MassCellChange.java @@ -8,64 +8,64 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.Row; public class MassCellChange implements Change { - private static final long serialVersionUID = -933571199802688027L; - - final protected CellChange[] _cellChanges; - final protected String _commonColumnName; - final protected boolean _updateRowContextDependencies; - - public MassCellChange(List cellChanges, String commonColumnName, boolean updateRowContextDependencies) { - _cellChanges = new CellChange[cellChanges.size()]; - _commonColumnName = commonColumnName; - cellChanges.toArray(_cellChanges); - - _updateRowContextDependencies = updateRowContextDependencies; - } - - public MassCellChange(CellChange cellChange, String commonColumnName, boolean updateRowContextDependencies) { - _cellChanges = new CellChange[1]; - _cellChanges[0] = cellChange; - - _commonColumnName = commonColumnName; - - _updateRowContextDependencies = updateRowContextDependencies; - } - - public void apply(Project project) { - synchronized (project) { - List rows = project.rows; - - for (CellChange cellChange : _cellChanges) { - rows.get(cellChange.row).setCell(cellChange.cellIndex, cellChange.newCell); - } - - if (_commonColumnName != null) { - Column column = project.columnModel.getColumnByName(_commonColumnName); - column.clearPrecomputes(); - } - - if (_updateRowContextDependencies) { - project.recomputeRowContextDependencies(); - } - } - } + private static final long serialVersionUID = -933571199802688027L; + + final protected CellChange[] _cellChanges; + final protected String _commonColumnName; + final protected boolean _updateRowContextDependencies; + + public MassCellChange(List cellChanges, String commonColumnName, boolean updateRowContextDependencies) { + _cellChanges = new CellChange[cellChanges.size()]; + _commonColumnName = commonColumnName; + cellChanges.toArray(_cellChanges); + + _updateRowContextDependencies = updateRowContextDependencies; + } + + public MassCellChange(CellChange cellChange, String commonColumnName, boolean updateRowContextDependencies) { + _cellChanges = new CellChange[1]; + _cellChanges[0] = cellChange; + + _commonColumnName = commonColumnName; + + _updateRowContextDependencies = updateRowContextDependencies; + } + + public void apply(Project project) { + synchronized (project) { + List rows = project.rows; + + for (CellChange cellChange : _cellChanges) { + rows.get(cellChange.row).setCell(cellChange.cellIndex, cellChange.newCell); + } + + if (_commonColumnName != null) { + Column column = project.columnModel.getColumnByName(_commonColumnName); + column.clearPrecomputes(); + } + + if (_updateRowContextDependencies) { + project.recomputeRowContextDependencies(); + } + } + } - public void revert(Project project) { - synchronized (project) { - List rows = project.rows; - - for (CellChange cellChange : _cellChanges) { - rows.get(cellChange.row).setCell(cellChange.cellIndex, cellChange.oldCell); - } - - if (_commonColumnName != null) { - Column column = project.columnModel.getColumnByName(_commonColumnName); - column.clearPrecomputes(); - } - - if (_updateRowContextDependencies) { - project.recomputeRowContextDependencies(); - } - } - } + public void revert(Project project) { + synchronized (project) { + List rows = project.rows; + + for (CellChange cellChange : _cellChanges) { + rows.get(cellChange.row).setCell(cellChange.cellIndex, cellChange.oldCell); + } + + if (_commonColumnName != null) { + Column column = project.columnModel.getColumnByName(_commonColumnName); + column.clearPrecomputes(); + } + + if (_updateRowContextDependencies) { + project.recomputeRowContextDependencies(); + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/model/changes/MassChange.java b/src/main/java/com/metaweb/gridworks/model/changes/MassChange.java index 17659b1ab..713e7f8c3 100644 --- a/src/main/java/com/metaweb/gridworks/model/changes/MassChange.java +++ b/src/main/java/com/metaweb/gridworks/model/changes/MassChange.java @@ -9,34 +9,34 @@ public class MassChange implements Change { private static final long serialVersionUID = -3926239320561407450L; final protected List _changes; - final protected boolean _updateRowContextDependencies; - - public MassChange(List changes, boolean updateRowContextDependencies) { - _changes = changes; - _updateRowContextDependencies = updateRowContextDependencies; - } - - public void apply(Project project) { - synchronized (project) { - for (Change change : _changes) { - change.apply(project); - } - - if (_updateRowContextDependencies) { - project.recomputeRowContextDependencies(); - } - } - } + final protected boolean _updateRowContextDependencies; + + public MassChange(List changes, boolean updateRowContextDependencies) { + _changes = changes; + _updateRowContextDependencies = updateRowContextDependencies; + } + + public void apply(Project project) { + synchronized (project) { + for (Change change : _changes) { + change.apply(project); + } + + if (_updateRowContextDependencies) { + project.recomputeRowContextDependencies(); + } + } + } - public void revert(Project project) { - synchronized (project) { + public void revert(Project project) { + synchronized (project) { for (Change change : _changes) { change.revert(project); } - - if (_updateRowContextDependencies) { - project.recomputeRowContextDependencies(); - } - } - } + + if (_updateRowContextDependencies) { + project.recomputeRowContextDependencies(); + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/model/changes/ReconChange.java b/src/main/java/com/metaweb/gridworks/model/changes/ReconChange.java index 391b6e032..d5d3314a3 100644 --- a/src/main/java/com/metaweb/gridworks/model/changes/ReconChange.java +++ b/src/main/java/com/metaweb/gridworks/model/changes/ReconChange.java @@ -20,10 +20,10 @@ public class ReconChange extends MassCellChange { protected ReconStats _oldReconStats; public ReconChange( - List cellChanges, - String commonColumnName, - ReconConfig newReconConfig, - ReconStats newReconStats // can be null + List cellChanges, + String commonColumnName, + ReconConfig newReconConfig, + ReconStats newReconStats // can be null ) { super(cellChanges, commonColumnName, false); _newReconConfig = newReconConfig; @@ -31,10 +31,10 @@ public class ReconChange extends MassCellChange { } public ReconChange( - CellChange cellChange, - String commonColumnName, - ReconConfig newReconConfig, - ReconStats newReconStats // can be null + CellChange cellChange, + String commonColumnName, + ReconConfig newReconConfig, + ReconStats newReconStats // can be null ) { super(cellChange, commonColumnName, false); _newReconConfig = newReconConfig; @@ -49,7 +49,7 @@ public class ReconChange extends MassCellChange { Column column = project.columnModel.getColumnByName(_commonColumnName); if (_newReconStats == null) { - _newReconStats = ReconStats.create(project, column.getCellIndex()); + _newReconStats = ReconStats.create(project, column.getCellIndex()); } _oldReconConfig = column.getReconConfig(); diff --git a/src/main/java/com/metaweb/gridworks/model/recon/GuidBasedReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/GuidBasedReconConfig.java index 750519b92..95652d9ab 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/GuidBasedReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/GuidBasedReconConfig.java @@ -24,151 +24,151 @@ import com.metaweb.gridworks.model.Recon.Judgment; import com.metaweb.gridworks.util.ParsingUtilities; public class GuidBasedReconConfig extends StrictReconConfig { - private static final long serialVersionUID = 1857895989346775294L; + private static final long serialVersionUID = 1857895989346775294L; - static public ReconConfig reconstruct(JSONObject obj) throws Exception { - return new GuidBasedReconConfig(); + static public ReconConfig reconstruct(JSONObject obj) throws Exception { + return new GuidBasedReconConfig(); } public GuidBasedReconConfig() { - } - - static protected class GuidBasedReconJob extends ReconJob { - String guid; - - public int getKey() { - return guid.hashCode(); - } } - @Override - public ReconJob createJob(Project project, int rowIndex, Row row, - String columnName, Cell cell) { - - GuidBasedReconJob job = new GuidBasedReconJob(); - String s = cell.value.toString(); - - if (s.startsWith("/guid/")) { - s = "#" + s.substring(6); - } else if (!s.startsWith("#")) { - s = "#" + s; - } - - job.guid = s; - - return job; - } + static protected class GuidBasedReconJob extends ReconJob { + String guid; + + public int getKey() { + return guid.hashCode(); + } + } - @Override - public int getBatchSize() { - return 10; - } + @Override + public ReconJob createJob(Project project, int rowIndex, Row row, + String columnName, Cell cell) { + + GuidBasedReconJob job = new GuidBasedReconJob(); + String s = cell.value.toString(); + + if (s.startsWith("/guid/")) { + s = "#" + s.substring(6); + } else if (!s.startsWith("#")) { + s = "#" + s; + } + + job.guid = s; + + return job; + } - @Override - public String getBriefDescription(Project project, String columnName) { + @Override + public int getBatchSize() { + return 10; + } + + @Override + public String getBriefDescription(Project project, String columnName) { return "Reconcile cells in column " + columnName + " as Freebase IDs"; - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); writer.key("mode"); writer.value("strict"); writer.key("match"); writer.value("id"); writer.endObject(); } - - @Override - public List batchRecon(List jobs) { - List recons = new ArrayList(jobs.size()); - Map guidToRecon = new HashMap(); - - try { - String query = null; - { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - jsonWriter.key("query"); - jsonWriter.array(); - jsonWriter.object(); - - jsonWriter.key("id"); jsonWriter.value(null); - jsonWriter.key("name"); jsonWriter.value(null); - jsonWriter.key("guid"); jsonWriter.value(null); - jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); - - jsonWriter.key("guid|="); - jsonWriter.array(); - for (ReconJob job : jobs) { - jsonWriter.value(((GuidBasedReconJob) job).guid); - } - jsonWriter.endArray(); - - jsonWriter.endObject(); - jsonWriter.endArray(); - jsonWriter.endObject(); - - query = stringWriter.toString(); - } - - StringBuffer sb = new StringBuffer(); - sb.append(s_mqlreadService + "?query="); - sb.append(ParsingUtilities.encode(query)); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - JSONArray results = o.getJSONArray("result"); - int count = results.length(); - - for (int i = 0; i < count; i++) { - JSONObject result = results.getJSONObject(i); - - String guid = result.getString("guid"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - result.getString("id"), - guid, - result.getString("name"), - typeIDs, - 100 - ); - - Recon recon = new Recon(); - recon.addCandidate(candidate); - recon.match = candidate; - recon.judgment = Judgment.Matched; - - guidToRecon.put(guid, recon); - } - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - for (int i = 0; i < jobs.size(); i++) { - String guid = ((GuidBasedReconJob) jobs.get(i)).guid; - Recon recon = guidToRecon.get(guid); - recons.add(recon); - } - - return recons; - } + + @Override + public List batchRecon(List jobs) { + List recons = new ArrayList(jobs.size()); + Map guidToRecon = new HashMap(); + + try { + String query = null; + { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + jsonWriter.key("query"); + jsonWriter.array(); + jsonWriter.object(); + + jsonWriter.key("id"); jsonWriter.value(null); + jsonWriter.key("name"); jsonWriter.value(null); + jsonWriter.key("guid"); jsonWriter.value(null); + jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); + + jsonWriter.key("guid|="); + jsonWriter.array(); + for (ReconJob job : jobs) { + jsonWriter.value(((GuidBasedReconJob) job).guid); + } + jsonWriter.endArray(); + + jsonWriter.endObject(); + jsonWriter.endArray(); + jsonWriter.endObject(); + + query = stringWriter.toString(); + } + + StringBuffer sb = new StringBuffer(); + sb.append(s_mqlreadService + "?query="); + sb.append(ParsingUtilities.encode(query)); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); + JSONArray results = o.getJSONArray("result"); + int count = results.length(); + + for (int i = 0; i < count; i++) { + JSONObject result = results.getJSONObject(i); + + String guid = result.getString("guid"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + result.getString("id"), + guid, + result.getString("name"), + typeIDs, + 100 + ); + + Recon recon = new Recon(); + recon.addCandidate(candidate); + recon.match = candidate; + recon.judgment = Judgment.Matched; + + guidToRecon.put(guid, recon); + } + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + for (int i = 0; i < jobs.size(); i++) { + String guid = ((GuidBasedReconJob) jobs.get(i)).guid; + Recon recon = guidToRecon.get(guid); + recons.add(recon); + } + + return recons; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/HeuristicReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/HeuristicReconConfig.java index 223e5d828..6b03388ae 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/HeuristicReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/HeuristicReconConfig.java @@ -28,46 +28,46 @@ import com.metaweb.gridworks.protograph.FreebaseProperty; import com.metaweb.gridworks.util.ParsingUtilities; public class HeuristicReconConfig extends ReconConfig { - private static final long serialVersionUID = 423145327938373362L; - - static public class ColumnDetail implements Serializable { - private static final long serialVersionUID = -8996704822460155543L; - - final public String columnName; - final public FreebaseProperty property; - - public ColumnDetail(String columnName, FreebaseProperty property) { - this.columnName = columnName; - this.property = property; - } - } - + private static final long serialVersionUID = 423145327938373362L; + + static public class ColumnDetail implements Serializable { + private static final long serialVersionUID = -8996704822460155543L; + + final public String columnName; + final public FreebaseProperty property; + + public ColumnDetail(String columnName, FreebaseProperty property) { + this.columnName = columnName; + this.property = property; + } + } + static public ReconConfig reconstruct(JSONObject obj) throws Exception { - List columnDetails = null; - if (obj.has("columnDetails")) { - JSONArray columnDetailsA = obj.getJSONArray("columnDetails"); - int l = columnDetailsA.length(); - - columnDetails = new ArrayList(l); - for (int i = 0; i < l; i++) { - JSONObject o = columnDetailsA.getJSONObject(i); - JSONObject p = o.getJSONObject("property"); - - columnDetails.add(new ColumnDetail( - o.getString("column"), - new FreebaseProperty( - p.getString("id"), - p.getString("name") - ) - )); - } - } else { - columnDetails = new ArrayList(); - } - - JSONObject t = obj.getJSONObject("type"); - - return new HeuristicReconConfig( + List columnDetails = null; + if (obj.has("columnDetails")) { + JSONArray columnDetailsA = obj.getJSONArray("columnDetails"); + int l = columnDetailsA.length(); + + columnDetails = new ArrayList(l); + for (int i = 0; i < l; i++) { + JSONObject o = columnDetailsA.getJSONObject(i); + JSONObject p = o.getJSONObject("property"); + + columnDetails.add(new ColumnDetail( + o.getString("column"), + new FreebaseProperty( + p.getString("id"), + p.getString("name") + ) + )); + } + } else { + columnDetails = new ArrayList(); + } + + JSONObject t = obj.getJSONObject("type"); + + return new HeuristicReconConfig( obj.getString("service"), t.getString("id"), t.getString("name"), @@ -77,28 +77,28 @@ public class HeuristicReconConfig extends ReconConfig { } static protected class HeuristicReconJob extends ReconJob { - String text; - String code; - - public int getKey() { - return code.hashCode(); - } + String text; + String code; + + public int getKey() { + return code.hashCode(); + } } - - final public String service; // either "recon" or "relevance" - final public String typeID; - final public String typeName; - final public boolean autoMatch; + + final public String service; // either "recon" or "relevance" + final public String typeID; + final public String typeName; + final public boolean autoMatch; final public List columnDetails; public HeuristicReconConfig( - String service, - String typeID, - String typeName, - boolean autoMatch, - List columnDetails - ) { - this.service = service; + String service, + String typeID, + String typeName, + boolean autoMatch, + List columnDetails + ) { + this.service = service; this.typeID = typeID; this.typeName = typeName; this.autoMatch = autoMatch; @@ -112,351 +112,351 @@ public class HeuristicReconConfig extends ReconConfig { writer.key("mode"); writer.value("heuristic"); writer.key("service"); writer.value(service); writer.key("type"); - writer.object(); - writer.key("id"); writer.value(typeID); - writer.key("name"); writer.value(typeName); - writer.endObject(); + writer.object(); + writer.key("id"); writer.value(typeID); + writer.key("name"); writer.value(typeName); + writer.endObject(); writer.key("autoMatch"); writer.value(autoMatch); writer.key("columnDetails"); - writer.array(); - for (ColumnDetail c : columnDetails) { - writer.object(); - writer.key("column"); writer.value(c.columnName); - writer.key("property"); c.property.write(writer, options); - writer.endObject(); - } - writer.endArray(); + writer.array(); + for (ColumnDetail c : columnDetails) { + writer.object(); + writer.key("column"); writer.value(c.columnName); + writer.key("property"); c.property.write(writer, options); + writer.endObject(); + } + writer.endArray(); writer.endObject(); } - @Override - public int getBatchSize() { - return 10; - } + @Override + public int getBatchSize() { + return 10; + } - @Override - public String getBriefDescription(Project project, String columnName) { + @Override + public String getBriefDescription(Project project, String columnName) { return "Reconcile cells in column " + columnName + " to type " + typeID; - } + } - @Override - public ReconJob createJob(Project project, int rowIndex, Row row, - String columnName, Cell cell) { - - HeuristicReconJob job = new HeuristicReconJob(); - if ("relevance".equals(service)) { - job.code = job.text = cell.value.toString(); - } else { - try { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - jsonWriter.key("/type/object/name"); jsonWriter.value(cell.value.toString()); - jsonWriter.key("/type/object/type"); jsonWriter.value(typeID); - - for (ColumnDetail c : columnDetails) { - int cellIndex = project.columnModel.getColumnByName(c.columnName).getCellIndex(); - - Cell cell2 = row.getCell(cellIndex); - if (cell2 != null && ExpressionUtils.isNonBlankData(cell2.value)) { - jsonWriter.key(c.property.id); - - if (cell2.recon != null && cell2.recon.match != null) { - jsonWriter.object(); - jsonWriter.key("id"); jsonWriter.value(cell2.recon.match.topicID); - jsonWriter.key("name"); jsonWriter.value(cell2.recon.match.topicName); - jsonWriter.endObject(); - } else { - jsonWriter.value(cell2.value.toString()); - } - } - } - jsonWriter.endObject(); - - job.text = cell.value.toString(); - job.code = stringWriter.toString(); - } catch (JSONException e) { - // - } - } - return job; - } - - @Override - public List batchRecon(List jobs) { - if ("relevance".equals(service)) { - return batchReconUsingRelevance(jobs); - } else { - return batchReconUsingReconService(jobs); - } - } - - protected List batchReconUsingRelevance(List jobs) { - List recons = new ArrayList(jobs.size()); - - try { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - for (int i = 0; i < jobs.size(); i++) { - HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); - - jsonWriter.key("q" + i + ":search"); - - jsonWriter.object(); - jsonWriter.key("query"); jsonWriter.value(job.text); - jsonWriter.key("limit"); jsonWriter.value(3); - jsonWriter.key("type"); jsonWriter.value(typeID); - jsonWriter.key("type_strict"); jsonWriter.value("should"); - jsonWriter.key("type_exclude"); jsonWriter.value("/common/image"); - jsonWriter.key("domain_exclude"); jsonWriter.value("/freebase"); - jsonWriter.endObject(); - } - jsonWriter.endObject(); - - StringBuffer sb = new StringBuffer(); - sb.append("http://api.freebase.com/api/service/search?indent=1&queries="); - sb.append(ParsingUtilities.encode(stringWriter.toString())); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - - for (int i = 0; i < jobs.size(); i++) { - HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); - - String text = job.text; - String key = "q" + i + ":search"; - if (!o.has(key)) { - continue; - } - - Recon recon = null; - - JSONObject o2 = o.getJSONObject(key); - if (o2.has("result")) { - JSONArray results = o2.getJSONArray("result"); - - recon = createReconFromRelevanceResults(text, results); - } else { - recon = new Recon(); - } - - recons.add(recon); - } - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - System.gc(); - - return recons; - } + @Override + public ReconJob createJob(Project project, int rowIndex, Row row, + String columnName, Cell cell) { + + HeuristicReconJob job = new HeuristicReconJob(); + if ("relevance".equals(service)) { + job.code = job.text = cell.value.toString(); + } else { + try { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + jsonWriter.key("/type/object/name"); jsonWriter.value(cell.value.toString()); + jsonWriter.key("/type/object/type"); jsonWriter.value(typeID); + + for (ColumnDetail c : columnDetails) { + int cellIndex = project.columnModel.getColumnByName(c.columnName).getCellIndex(); + + Cell cell2 = row.getCell(cellIndex); + if (cell2 != null && ExpressionUtils.isNonBlankData(cell2.value)) { + jsonWriter.key(c.property.id); + + if (cell2.recon != null && cell2.recon.match != null) { + jsonWriter.object(); + jsonWriter.key("id"); jsonWriter.value(cell2.recon.match.topicID); + jsonWriter.key("name"); jsonWriter.value(cell2.recon.match.topicName); + jsonWriter.endObject(); + } else { + jsonWriter.value(cell2.value.toString()); + } + } + } + jsonWriter.endObject(); + + job.text = cell.value.toString(); + job.code = stringWriter.toString(); + } catch (JSONException e) { + // + } + } + return job; + } + + @Override + public List batchRecon(List jobs) { + if ("relevance".equals(service)) { + return batchReconUsingRelevance(jobs); + } else { + return batchReconUsingReconService(jobs); + } + } + + protected List batchReconUsingRelevance(List jobs) { + List recons = new ArrayList(jobs.size()); + + try { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + for (int i = 0; i < jobs.size(); i++) { + HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); + + jsonWriter.key("q" + i + ":search"); + + jsonWriter.object(); + jsonWriter.key("query"); jsonWriter.value(job.text); + jsonWriter.key("limit"); jsonWriter.value(3); + jsonWriter.key("type"); jsonWriter.value(typeID); + jsonWriter.key("type_strict"); jsonWriter.value("should"); + jsonWriter.key("type_exclude"); jsonWriter.value("/common/image"); + jsonWriter.key("domain_exclude"); jsonWriter.value("/freebase"); + jsonWriter.endObject(); + } + jsonWriter.endObject(); + + StringBuffer sb = new StringBuffer(); + sb.append("http://api.freebase.com/api/service/search?indent=1&queries="); + sb.append(ParsingUtilities.encode(stringWriter.toString())); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); + + for (int i = 0; i < jobs.size(); i++) { + HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); + + String text = job.text; + String key = "q" + i + ":search"; + if (!o.has(key)) { + continue; + } + + Recon recon = null; + + JSONObject o2 = o.getJSONObject(key); + if (o2.has("result")) { + JSONArray results = o2.getJSONArray("result"); + + recon = createReconFromRelevanceResults(text, results); + } else { + recon = new Recon(); + } + + recons.add(recon); + } + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + System.gc(); + + return recons; + } - protected Recon createReconFromRelevanceResults(String text, JSONArray results) { - Recon recon = new Recon(); - try { - int length = results.length(); - int count = 0; - for (int i = 0; i < length && count < 3; i++) { - JSONObject result = results.getJSONObject(i); - if (!result.has("name")) { - continue; - } - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getJSONObject(j).getString("id"); - } - - double score = result.getDouble("relevance:score"); - ReconCandidate candidate = new ReconCandidate( - result.getString("id"), - result.getString("guid"), - result.getString("name"), - typeIDs, - score - ); - - // best match - if (i == 0) { - recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.topicName)); - recon.setFeature(Recon.Feature_nameLevenshtein, StringUtils.getLevenshteinDistance(text, candidate.topicName)); - recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.topicName)); - - recon.setFeature(Recon.Feature_typeMatch, false); - for (String typeID : candidate.typeIDs) { - if (typeID.equals(typeID)) { - recon.setFeature(Recon.Feature_typeMatch, true); - if (autoMatch && score >= 100) { - recon.match = candidate; - recon.judgment = Judgment.Matched; - } - break; - } - } - } - - recon.addCandidate(candidate); - count++; - } - } catch (JSONException e) { - e.printStackTrace(); - } - return recon; - } - - static final String s_reconService = "http://data.labs.freebase.com/recon/query"; - - protected List batchReconUsingReconService(List jobs) { - List recons = new ArrayList(jobs.size()); - - for (int i = 0; i < jobs.size(); i++) { - HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); - Recon recon = null; - try { - StringBuffer sb = new StringBuffer(); - sb.append(s_reconService + "?limit=5&q="); - sb.append(ParsingUtilities.encode(job.code)); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONArray a = ParsingUtilities.evaluateJsonStringToArray(s); - - recon = createReconFromReconResults(job.text, a); - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - if (recon == null) { - recon = new Recon(); - } - recons.add(recon); - } - - System.gc(); - - return recons; - } + protected Recon createReconFromRelevanceResults(String text, JSONArray results) { + Recon recon = new Recon(); + try { + int length = results.length(); + int count = 0; + for (int i = 0; i < length && count < 3; i++) { + JSONObject result = results.getJSONObject(i); + if (!result.has("name")) { + continue; + } + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getJSONObject(j).getString("id"); + } + + double score = result.getDouble("relevance:score"); + ReconCandidate candidate = new ReconCandidate( + result.getString("id"), + result.getString("guid"), + result.getString("name"), + typeIDs, + score + ); + + // best match + if (i == 0) { + recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.topicName)); + recon.setFeature(Recon.Feature_nameLevenshtein, StringUtils.getLevenshteinDistance(text, candidate.topicName)); + recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.topicName)); + + recon.setFeature(Recon.Feature_typeMatch, false); + for (String typeID : candidate.typeIDs) { + if (typeID.equals(typeID)) { + recon.setFeature(Recon.Feature_typeMatch, true); + if (autoMatch && score >= 100) { + recon.match = candidate; + recon.judgment = Judgment.Matched; + } + break; + } + } + } + + recon.addCandidate(candidate); + count++; + } + } catch (JSONException e) { + e.printStackTrace(); + } + return recon; + } + + static final String s_reconService = "http://data.labs.freebase.com/recon/query"; + + protected List batchReconUsingReconService(List jobs) { + List recons = new ArrayList(jobs.size()); + + for (int i = 0; i < jobs.size(); i++) { + HeuristicReconJob job = (HeuristicReconJob) jobs.get(i); + Recon recon = null; + try { + StringBuffer sb = new StringBuffer(); + sb.append(s_reconService + "?limit=5&q="); + sb.append(ParsingUtilities.encode(job.code)); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONArray a = ParsingUtilities.evaluateJsonStringToArray(s); + + recon = createReconFromReconResults(job.text, a); + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + if (recon == null) { + recon = new Recon(); + } + recons.add(recon); + } + + System.gc(); + + return recons; + } - protected Recon createReconFromReconResults(String text, JSONArray results) { - Recon recon = new Recon(); - try { - int length = results.length(); - int count = 0; - for (int i = 0; i < length && count < 3; i++) { - JSONObject result = results.getJSONObject(i); - if (!result.has("name")) { - continue; - } - - String id = result.getString("id"); - JSONArray names = result.getJSONArray("name"); - double score = result.getDouble("score"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - id, - "#" + id.substring(6), - names.getString(0), - typeIDs, - score - ); - - // best match - if (i == 0) { - recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.topicName)); - recon.setFeature(Recon.Feature_nameLevenshtein, StringUtils.getLevenshteinDistance(text, candidate.topicName)); - recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.topicName)); - - recon.setFeature(Recon.Feature_typeMatch, false); - for (String typeID : candidate.typeIDs) { - if (typeID.equals(typeID)) { - recon.setFeature(Recon.Feature_typeMatch, true); - if (autoMatch && - (score > 0.6 || - (result.has("match") && result.getBoolean("match")))) { - recon.match = candidate; - recon.judgment = Judgment.Matched; - } - break; - } - } - } - - recon.addCandidate(candidate); - count++; - } - } catch (JSONException e) { - e.printStackTrace(); - } - return recon; - } - - static protected double wordDistance(String s1, String s2) { - Set words1 = breakWords(s1); - Set words2 = breakWords(s2); - return words1.size() >= words2.size() ? wordDistance(words1, words2) : wordDistance(words2, words1); - } - - static protected double wordDistance(Set longWords, Set shortWords) { - double common = 0; - for (String word : shortWords) { - if (longWords.contains(word)) { - common++; - } - } - return common / longWords.size(); - } - - static protected Set s_stopWords; - static { - s_stopWords = new HashSet(); - s_stopWords.add("the"); - s_stopWords.add("a"); - s_stopWords.add("and"); - s_stopWords.add("of"); - s_stopWords.add("on"); - s_stopWords.add("in"); - s_stopWords.add("at"); - s_stopWords.add("by"); - } - - static protected Set breakWords(String s) { - String[] words = s.toLowerCase().split("\\s+"); - - Set set = new HashSet(words.length); - for (String word : words) { - if (!s_stopWords.contains(word)) { - set.add(word); - } - } - return set; - } + protected Recon createReconFromReconResults(String text, JSONArray results) { + Recon recon = new Recon(); + try { + int length = results.length(); + int count = 0; + for (int i = 0; i < length && count < 3; i++) { + JSONObject result = results.getJSONObject(i); + if (!result.has("name")) { + continue; + } + + String id = result.getString("id"); + JSONArray names = result.getJSONArray("name"); + double score = result.getDouble("score"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + id, + "#" + id.substring(6), + names.getString(0), + typeIDs, + score + ); + + // best match + if (i == 0) { + recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.topicName)); + recon.setFeature(Recon.Feature_nameLevenshtein, StringUtils.getLevenshteinDistance(text, candidate.topicName)); + recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.topicName)); + + recon.setFeature(Recon.Feature_typeMatch, false); + for (String typeID : candidate.typeIDs) { + if (typeID.equals(typeID)) { + recon.setFeature(Recon.Feature_typeMatch, true); + if (autoMatch && + (score > 0.6 || + (result.has("match") && result.getBoolean("match")))) { + recon.match = candidate; + recon.judgment = Judgment.Matched; + } + break; + } + } + } + + recon.addCandidate(candidate); + count++; + } + } catch (JSONException e) { + e.printStackTrace(); + } + return recon; + } + + static protected double wordDistance(String s1, String s2) { + Set words1 = breakWords(s1); + Set words2 = breakWords(s2); + return words1.size() >= words2.size() ? wordDistance(words1, words2) : wordDistance(words2, words1); + } + + static protected double wordDistance(Set longWords, Set shortWords) { + double common = 0; + for (String word : shortWords) { + if (longWords.contains(word)) { + common++; + } + } + return common / longWords.size(); + } + + static protected Set s_stopWords; + static { + s_stopWords = new HashSet(); + s_stopWords.add("the"); + s_stopWords.add("a"); + s_stopWords.add("and"); + s_stopWords.add("of"); + s_stopWords.add("on"); + s_stopWords.add("in"); + s_stopWords.add("at"); + s_stopWords.add("by"); + } + + static protected Set breakWords(String s) { + String[] words = s.toLowerCase().split("\\s+"); + + Set set = new HashSet(words.length); + for (String word : words) { + if (!s_stopWords.contains(word)) { + set.add(word); + } + } + return set; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/IdBasedReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/IdBasedReconConfig.java index 0c5d2cc66..dcc44e3b1 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/IdBasedReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/IdBasedReconConfig.java @@ -24,155 +24,155 @@ import com.metaweb.gridworks.model.Recon.Judgment; import com.metaweb.gridworks.util.ParsingUtilities; public class IdBasedReconConfig extends StrictReconConfig { - private static final long serialVersionUID = 1857895989346775294L; + private static final long serialVersionUID = 1857895989346775294L; - static public ReconConfig reconstruct(JSONObject obj) throws Exception { - return new IdBasedReconConfig(); + static public ReconConfig reconstruct(JSONObject obj) throws Exception { + return new IdBasedReconConfig(); } public IdBasedReconConfig() { - } - - static protected class IdBasedReconJob extends ReconJob { - String id; - - public int getKey() { - return id.hashCode(); - } } - @Override - public ReconJob createJob(Project project, int rowIndex, Row row, - String columnName, Cell cell) { - - IdBasedReconJob job = new IdBasedReconJob(); - String s = cell.value.toString(); - - if (!s.startsWith("/")) { - if (s.startsWith("92")) { - s = "/guid/" + s; - } else if (!s.contains("/")){ - s = "/en/" + s; - } else { - s = "/" + s; - } - } - - job.id = s; - - return job; - } + static protected class IdBasedReconJob extends ReconJob { + String id; + + public int getKey() { + return id.hashCode(); + } + } - @Override - public int getBatchSize() { - return 10; - } + @Override + public ReconJob createJob(Project project, int rowIndex, Row row, + String columnName, Cell cell) { + + IdBasedReconJob job = new IdBasedReconJob(); + String s = cell.value.toString(); + + if (!s.startsWith("/")) { + if (s.startsWith("92")) { + s = "/guid/" + s; + } else if (!s.contains("/")){ + s = "/en/" + s; + } else { + s = "/" + s; + } + } + + job.id = s; + + return job; + } - @Override - public String getBriefDescription(Project project, String columnName) { + @Override + public int getBatchSize() { + return 10; + } + + @Override + public String getBriefDescription(Project project, String columnName) { return "Reconcile cells in column " + columnName + " as Freebase IDs"; - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); writer.key("mode"); writer.value("strict"); writer.key("match"); writer.value("id"); writer.endObject(); } - - @Override - public List batchRecon(List jobs) { - List recons = new ArrayList(jobs.size()); - Map idToRecon = new HashMap(); - - try { - String query = null; - { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - jsonWriter.key("query"); - jsonWriter.array(); - jsonWriter.object(); - - jsonWriter.key("id"); jsonWriter.value(null); - jsonWriter.key("name"); jsonWriter.value(null); - jsonWriter.key("guid"); jsonWriter.value(null); - jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); - - jsonWriter.key("id|="); - jsonWriter.array(); - for (ReconJob job : jobs) { - jsonWriter.value(((IdBasedReconJob) job).id); - } - jsonWriter.endArray(); - - jsonWriter.endObject(); - jsonWriter.endArray(); - jsonWriter.endObject(); - - query = stringWriter.toString(); - } - - StringBuffer sb = new StringBuffer(); - sb.append(s_mqlreadService + "?query="); - sb.append(ParsingUtilities.encode(query)); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - JSONArray results = o.getJSONArray("result"); - int count = results.length(); - - for (int i = 0; i < count; i++) { - JSONObject result = results.getJSONObject(i); - - String id = result.getString("id"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - id, - result.getString("guid"), - result.getString("name"), - typeIDs, - 100 - ); - - Recon recon = new Recon(); - recon.addCandidate(candidate); - recon.match = candidate; - recon.judgment = Judgment.Matched; - - idToRecon.put(id, recon); - } - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - for (int i = 0; i < jobs.size(); i++) { - String id = ((IdBasedReconJob) jobs.get(i)).id; - Recon recon = idToRecon.get(id); - recons.add(recon); - } - - return recons; - } + + @Override + public List batchRecon(List jobs) { + List recons = new ArrayList(jobs.size()); + Map idToRecon = new HashMap(); + + try { + String query = null; + { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + jsonWriter.key("query"); + jsonWriter.array(); + jsonWriter.object(); + + jsonWriter.key("id"); jsonWriter.value(null); + jsonWriter.key("name"); jsonWriter.value(null); + jsonWriter.key("guid"); jsonWriter.value(null); + jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); + + jsonWriter.key("id|="); + jsonWriter.array(); + for (ReconJob job : jobs) { + jsonWriter.value(((IdBasedReconJob) job).id); + } + jsonWriter.endArray(); + + jsonWriter.endObject(); + jsonWriter.endArray(); + jsonWriter.endObject(); + + query = stringWriter.toString(); + } + + StringBuffer sb = new StringBuffer(); + sb.append(s_mqlreadService + "?query="); + sb.append(ParsingUtilities.encode(query)); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); + JSONArray results = o.getJSONArray("result"); + int count = results.length(); + + for (int i = 0; i < count; i++) { + JSONObject result = results.getJSONObject(i); + + String id = result.getString("id"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + id, + result.getString("guid"), + result.getString("name"), + typeIDs, + 100 + ); + + Recon recon = new Recon(); + recon.addCandidate(candidate); + recon.match = candidate; + recon.judgment = Judgment.Matched; + + idToRecon.put(id, recon); + } + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + for (int i = 0; i < jobs.size(); i++) { + String id = ((IdBasedReconJob) jobs.get(i)).id; + Recon recon = idToRecon.get(id); + recons.add(recon); + } + + return recons; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/KeyBasedReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/KeyBasedReconConfig.java index f6087f0f1..6f83cb6e0 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/KeyBasedReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/KeyBasedReconConfig.java @@ -25,168 +25,168 @@ import com.metaweb.gridworks.protograph.FreebaseTopic; import com.metaweb.gridworks.util.ParsingUtilities; public class KeyBasedReconConfig extends StrictReconConfig { - private static final long serialVersionUID = 2363754609522023900L; - - final public FreebaseTopic namespace; - + private static final long serialVersionUID = 2363754609522023900L; + + final public FreebaseTopic namespace; + static public ReconConfig reconstruct(JSONObject obj) throws Exception { - JSONObject ns = obj.getJSONObject("namespace"); - - return new KeyBasedReconConfig( - new FreebaseTopic( - ns.getString("id"), - ns.getString("name") - ) + JSONObject ns = obj.getJSONObject("namespace"); + + return new KeyBasedReconConfig( + new FreebaseTopic( + ns.getString("id"), + ns.getString("name") + ) ); } public KeyBasedReconConfig(FreebaseTopic namespace) { - this.namespace = namespace; - } - - static protected class KeyBasedReconJob extends ReconJob { - String key; - - public int getKey() { - return key.hashCode(); - } + this.namespace = namespace; } - @Override - public ReconJob createJob(Project project, int rowIndex, Row row, - String columnName, Cell cell) { - - KeyBasedReconJob job = new KeyBasedReconJob(); - - job.key = cell.value.toString().replace(' ', '_'); - - return job; - } + static protected class KeyBasedReconJob extends ReconJob { + String key; + + public int getKey() { + return key.hashCode(); + } + } - @Override - public int getBatchSize() { - return 10; - } + @Override + public ReconJob createJob(Project project, int rowIndex, Row row, + String columnName, Cell cell) { + + KeyBasedReconJob job = new KeyBasedReconJob(); + + job.key = cell.value.toString().replace(' ', '_'); + + return job; + } - @Override - public String getBriefDescription(Project project, String columnName) { + @Override + public int getBatchSize() { + return 10; + } + + @Override + public String getBriefDescription(Project project, String columnName) { return "Reconcile cells in column " + columnName + " to topics with keys in namespace " + namespace.id; - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); writer.key("mode"); writer.value("strict"); writer.key("match"); writer.value("key"); writer.key("namespace"); namespace.write(writer, options); writer.endObject(); } - - @Override - public List batchRecon(List jobs) { - List recons = new ArrayList(jobs.size()); - Map keyToRecon = new HashMap(); - - try { - String query = null; - { - StringWriter stringWriter = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - - jsonWriter.object(); - jsonWriter.key("query"); - jsonWriter.array(); - jsonWriter.object(); - - jsonWriter.key("id"); jsonWriter.value(null); - jsonWriter.key("name"); jsonWriter.value(null); - jsonWriter.key("guid"); jsonWriter.value(null); - jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); - - jsonWriter.key("key"); - jsonWriter.array(); - jsonWriter.object(); - - jsonWriter.key("namespace"); - jsonWriter.object(); - jsonWriter.key("id"); jsonWriter.value(namespace.id); - jsonWriter.endObject(); - - jsonWriter.key("value"); jsonWriter.value(null); - jsonWriter.key("value|="); - jsonWriter.array(); - for (ReconJob job : jobs) { - jsonWriter.value(((KeyBasedReconJob) job).key); - } - jsonWriter.endArray(); - - jsonWriter.endObject(); - jsonWriter.endArray(); - - jsonWriter.endObject(); - jsonWriter.endArray(); - jsonWriter.endObject(); - - query = stringWriter.toString(); - } - - StringBuffer sb = new StringBuffer(); - sb.append(s_mqlreadService + "?query="); - sb.append(ParsingUtilities.encode(query)); - - URL url = new URL(sb.toString()); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5000); - connection.connect(); - - InputStream is = connection.getInputStream(); - try { - String s = ParsingUtilities.inputStreamToString(is); - JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - JSONArray results = o.getJSONArray("result"); - int count = results.length(); - - for (int i = 0; i < count; i++) { - JSONObject result = results.getJSONObject(i); - - String key = result.getJSONArray("key").getJSONObject(0).getString("value"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - result.getString("id"), - result.getString("guid"), - result.getString("name"), - typeIDs, - 100 - ); - - Recon recon = new Recon(); - recon.addCandidate(candidate); - recon.match = candidate; - recon.judgment = Judgment.Matched; - - keyToRecon.put(key, recon); - } - } finally { - is.close(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - for (int i = 0; i < jobs.size(); i++) { - String key = ((KeyBasedReconJob) jobs.get(i)).key; - Recon recon = keyToRecon.get(key); - recons.add(recon); - } - - return recons; - } + + @Override + public List batchRecon(List jobs) { + List recons = new ArrayList(jobs.size()); + Map keyToRecon = new HashMap(); + + try { + String query = null; + { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + + jsonWriter.object(); + jsonWriter.key("query"); + jsonWriter.array(); + jsonWriter.object(); + + jsonWriter.key("id"); jsonWriter.value(null); + jsonWriter.key("name"); jsonWriter.value(null); + jsonWriter.key("guid"); jsonWriter.value(null); + jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); + + jsonWriter.key("key"); + jsonWriter.array(); + jsonWriter.object(); + + jsonWriter.key("namespace"); + jsonWriter.object(); + jsonWriter.key("id"); jsonWriter.value(namespace.id); + jsonWriter.endObject(); + + jsonWriter.key("value"); jsonWriter.value(null); + jsonWriter.key("value|="); + jsonWriter.array(); + for (ReconJob job : jobs) { + jsonWriter.value(((KeyBasedReconJob) job).key); + } + jsonWriter.endArray(); + + jsonWriter.endObject(); + jsonWriter.endArray(); + + jsonWriter.endObject(); + jsonWriter.endArray(); + jsonWriter.endObject(); + + query = stringWriter.toString(); + } + + StringBuffer sb = new StringBuffer(); + sb.append(s_mqlreadService + "?query="); + sb.append(ParsingUtilities.encode(query)); + + URL url = new URL(sb.toString()); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(5000); + connection.connect(); + + InputStream is = connection.getInputStream(); + try { + String s = ParsingUtilities.inputStreamToString(is); + JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); + JSONArray results = o.getJSONArray("result"); + int count = results.length(); + + for (int i = 0; i < count; i++) { + JSONObject result = results.getJSONObject(i); + + String key = result.getJSONArray("key").getJSONObject(0).getString("value"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + result.getString("id"), + result.getString("guid"), + result.getString("name"), + typeIDs, + 100 + ); + + Recon recon = new Recon(); + recon.addCandidate(candidate); + recon.match = candidate; + recon.judgment = Judgment.Matched; + + keyToRecon.put(key, recon); + } + } finally { + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + + for (int i = 0; i < jobs.size(); i++) { + String key = ((KeyBasedReconJob) jobs.get(i)).key; + Recon recon = keyToRecon.get(key); + recons.add(recon); + } + + return recons; + } } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/ReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/ReconConfig.java index c36bb45e4..e0a8685b2 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/ReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/ReconConfig.java @@ -17,24 +17,24 @@ abstract public class ReconConfig implements Serializable, Jsonizable { static public ReconConfig reconstruct(JSONObject obj) throws Exception { String mode = obj.getString("mode"); if ("heuristic".equals(mode)) { - return HeuristicReconConfig.reconstruct(obj); + return HeuristicReconConfig.reconstruct(obj); } else if ("strict".equals(mode)) { - return StrictReconConfig.reconstruct(obj); + return StrictReconConfig.reconstruct(obj); } return null; } abstract public int getBatchSize(); - abstract public String getBriefDescription(Project project, String columnName); - - abstract public ReconJob createJob( - Project project, - int rowIndex, - Row row, - String columnName, - Cell cell - ); + abstract public String getBriefDescription(Project project, String columnName); - abstract public List batchRecon(List jobs); + abstract public ReconJob createJob( + Project project, + int rowIndex, + Row row, + String columnName, + Cell cell + ); + + abstract public List batchRecon(List jobs); } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/ReconJob.java b/src/main/java/com/metaweb/gridworks/model/recon/ReconJob.java index 7e8becbe8..64111138f 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/ReconJob.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/ReconJob.java @@ -1,5 +1,5 @@ package com.metaweb.gridworks.model.recon; abstract public class ReconJob { - abstract public int getKey(); + abstract public int getKey(); } diff --git a/src/main/java/com/metaweb/gridworks/model/recon/StrictReconConfig.java b/src/main/java/com/metaweb/gridworks/model/recon/StrictReconConfig.java index cb16935b7..beeb631e8 100644 --- a/src/main/java/com/metaweb/gridworks/model/recon/StrictReconConfig.java +++ b/src/main/java/com/metaweb/gridworks/model/recon/StrictReconConfig.java @@ -3,18 +3,18 @@ package com.metaweb.gridworks.model.recon; import org.json.JSONObject; abstract public class StrictReconConfig extends ReconConfig { - private static final long serialVersionUID = 4454059850557793074L; - - final static protected String s_mqlreadService = "http://api.freebase.com/api/service/mqlread"; + private static final long serialVersionUID = 4454059850557793074L; + + final static protected String s_mqlreadService = "http://api.freebase.com/api/service/mqlread"; static public ReconConfig reconstruct(JSONObject obj) throws Exception { String match = obj.getString("match"); if ("key".equals(match)) { - return KeyBasedReconConfig.reconstruct(obj); + return KeyBasedReconConfig.reconstruct(obj); } else if ("id".equals(match)) { - return IdBasedReconConfig.reconstruct(obj); + return IdBasedReconConfig.reconstruct(obj); } else if ("guid".equals(match)) { - return GuidBasedReconConfig.reconstruct(obj); + return GuidBasedReconConfig.reconstruct(obj); } return null; } diff --git a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java index b012c6d1c..cbeab5537 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ColumnAdditionOperation.java @@ -25,13 +25,13 @@ import com.metaweb.gridworks.model.changes.CellAtRow; import com.metaweb.gridworks.model.changes.ColumnAdditionChange; public class ColumnAdditionOperation extends EngineDependentOperation { - private static final long serialVersionUID = -5672677479629932356L; + private static final long serialVersionUID = -5672677479629932356L; - final protected String _baseColumnName; - final protected String _expression; - - final protected String _headerLabel; - final protected int _columnInsertIndex; + final protected String _baseColumnName; + final protected String _expression; + + final protected String _headerLabel; + final protected int _columnInsertIndex; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -45,50 +45,50 @@ public class ColumnAdditionOperation extends EngineDependentOperation { ); } - public ColumnAdditionOperation( - JSONObject engineConfig, - String baseColumnName, - String expression, - String headerLabel, - int columnInsertIndex - ) { - super(engineConfig); - - _baseColumnName = baseColumnName; - _expression = expression; - - _headerLabel = headerLabel; - _columnInsertIndex = columnInsertIndex; - } + public ColumnAdditionOperation( + JSONObject engineConfig, + String baseColumnName, + String expression, + String headerLabel, + int columnInsertIndex + ) { + super(engineConfig); + + _baseColumnName = baseColumnName; + _expression = expression; + + _headerLabel = headerLabel; + _columnInsertIndex = columnInsertIndex; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("headerLabel"); writer.value(_headerLabel); - writer.key("columnInsertIndex"); writer.value(_columnInsertIndex); - writer.key("baseColumnName"); writer.value(_baseColumnName); - writer.key("expression"); writer.value(_expression); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("headerLabel"); writer.value(_headerLabel); + writer.key("columnInsertIndex"); writer.value(_columnInsertIndex); + writer.key("baseColumnName"); writer.value(_baseColumnName); + writer.key("expression"); writer.value(_expression); + writer.endObject(); + } - protected String getBriefDescription(Project project) { + protected String getBriefDescription(Project project) { return "Create column " + _headerLabel + " at index " + _columnInsertIndex + " based on column " + _baseColumnName + " using expression " + _expression; - } + } - protected String createDescription(Column column, List cellsAtRows) { - return "Create new column " + _headerLabel + - " based on column " + column.getHeaderLabel() + - " by filling " + cellsAtRows.size() + - " rows with " + _expression; - } - + protected String createDescription(Column column, List cellsAtRows) { + return "Create new column " + _headerLabel + + " based on column " + column.getHeaderLabel() + + " by filling " + cellsAtRows.size() + + " rows with " + _expression; + } + protected HistoryEntry createHistoryEntry(Project project) throws Exception { Engine engine = createEngine(project); @@ -110,40 +110,40 @@ public class ColumnAdditionOperation extends EngineDependentOperation { project, description, this, change); } - protected RowVisitor createRowVisitor(Project project, List cellsAtRows) throws Exception { - Column column = project.columnModel.getColumnByName(_baseColumnName); - - Evaluable eval = MetaParser.parse(_expression); + protected RowVisitor createRowVisitor(Project project, List cellsAtRows) throws Exception { + Column column = project.columnModel.getColumnByName(_baseColumnName); + + Evaluable eval = MetaParser.parse(_expression); Properties bindings = ExpressionUtils.createBindings(project); - return new RowVisitor() { - int cellIndex; - Properties bindings; - List cellsAtRows; - Evaluable eval; - - public RowVisitor init(int cellIndex, Properties bindings, List cellsAtRows, Evaluable eval) { - this.cellIndex = cellIndex; - this.bindings = bindings; - this.cellsAtRows = cellsAtRows; - this.eval = eval; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - Cell cell = row.getCell(cellIndex); + return new RowVisitor() { + int cellIndex; + Properties bindings; + List cellsAtRows; + Evaluable eval; + + public RowVisitor init(int cellIndex, Properties bindings, List cellsAtRows, Evaluable eval) { + this.cellIndex = cellIndex; + this.bindings = bindings; + this.cellsAtRows = cellsAtRows; + this.eval = eval; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + Cell cell = row.getCell(cellIndex); ExpressionUtils.bind(bindings, row, rowIndex, cell); - + Object v = eval.evaluate(bindings); if (v != null) { Cell newCell = new Cell(v, null); - + cellsAtRows.add(new CellAtRow(rowIndex, newCell)); } - return false; - } - }.init(column.getCellIndex(), bindings, cellsAtRows, eval); - } + return false; + } + }.init(column.getCellIndex(), bindings, cellsAtRows, eval); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ColumnRemovalOperation.java b/src/main/java/com/metaweb/gridworks/operations/ColumnRemovalOperation.java index d2dc08a28..fb2cebe30 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ColumnRemovalOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ColumnRemovalOperation.java @@ -14,9 +14,9 @@ import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.model.changes.ColumnRemovalChange; public class ColumnRemovalOperation extends AbstractOperation { - private static final long serialVersionUID = 8422079695048733734L; - - final protected String _columnName; + private static final long serialVersionUID = 8422079695048733734L; + + final protected String _columnName; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { return new ColumnRemovalOperation( @@ -24,12 +24,12 @@ public class ColumnRemovalOperation extends AbstractOperation { ); } - public ColumnRemovalOperation( - String columnName - ) { - _columnName = columnName; - } - + public ColumnRemovalOperation( + String columnName + ) { + _columnName = columnName; + } + public void write(JSONWriter writer, Properties options) throws JSONException { @@ -41,20 +41,20 @@ public class ColumnRemovalOperation extends AbstractOperation { } - protected String getBriefDescription(Project project) { - return "Remove column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Remove column " + _columnName; + } - protected HistoryEntry createHistoryEntry(Project project) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - if (column == null) { - throw new Exception("No column named " + _columnName); - } - - String description = "Remove column " + column.getHeaderLabel(); - - Change change = new ColumnRemovalChange(project.columnModel.columns.indexOf(column)); - - return new HistoryEntry(project, description, ColumnRemovalOperation.this, change); - } + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + if (column == null) { + throw new Exception("No column named " + _columnName); + } + + String description = "Remove column " + column.getHeaderLabel(); + + Change change = new ColumnRemovalChange(project.columnModel.columns.indexOf(column)); + + return new HistoryEntry(project, description, ColumnRemovalOperation.this, change); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/EngineDependentMassCellOperation.java b/src/main/java/com/metaweb/gridworks/operations/EngineDependentMassCellOperation.java index bf98e0fd7..9a2ac5a5b 100644 --- a/src/main/java/com/metaweb/gridworks/operations/EngineDependentMassCellOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/EngineDependentMassCellOperation.java @@ -16,42 +16,42 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.MassCellChange; abstract public class EngineDependentMassCellOperation extends EngineDependentOperation { - private static final long serialVersionUID = -8962461328087299452L; - - final protected String _columnName; - final protected boolean _updateRowContextDependencies; - - protected EngineDependentMassCellOperation( - JSONObject engineConfig, String columnName, boolean updateRowContextDependencies) { - super(engineConfig); - _columnName = columnName; - _updateRowContextDependencies = updateRowContextDependencies; - } + private static final long serialVersionUID = -8962461328087299452L; + + final protected String _columnName; + final protected boolean _updateRowContextDependencies; + + protected EngineDependentMassCellOperation( + JSONObject engineConfig, String columnName, boolean updateRowContextDependencies) { + super(engineConfig); + _columnName = columnName; + _updateRowContextDependencies = updateRowContextDependencies; + } - protected HistoryEntry createHistoryEntry(Project project) throws Exception { - Engine engine = createEngine(project); - - Column column = project.columnModel.getColumnByName(_columnName); - if (column == null) { - throw new Exception("No column named " + _columnName); - } - - List cellChanges = new ArrayList(project.rows.size()); - - FilteredRows filteredRows = engine.getAllFilteredRows(false); - filteredRows.accept(project, createRowVisitor(project, cellChanges)); - - String description = createDescription(column, cellChanges); - - return new HistoryEntry( - project, description, this, createChange(project, column, cellChanges)); - } - - protected Change createChange(Project project, Column column, List cellChanges) { - return new MassCellChange( - cellChanges, column.getHeaderLabel(), _updateRowContextDependencies); - } - - abstract protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception; - abstract protected String createDescription(Column column, List cellChanges); + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + Engine engine = createEngine(project); + + Column column = project.columnModel.getColumnByName(_columnName); + if (column == null) { + throw new Exception("No column named " + _columnName); + } + + List cellChanges = new ArrayList(project.rows.size()); + + FilteredRows filteredRows = engine.getAllFilteredRows(false); + filteredRows.accept(project, createRowVisitor(project, cellChanges)); + + String description = createDescription(column, cellChanges); + + return new HistoryEntry( + project, description, this, createChange(project, column, cellChanges)); + } + + protected Change createChange(Project project, Column column, List cellChanges) { + return new MassCellChange( + cellChanges, column.getHeaderLabel(), _updateRowContextDependencies); + } + + abstract protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception; + abstract protected String createDescription(Column column, List cellChanges); } diff --git a/src/main/java/com/metaweb/gridworks/operations/EngineDependentOperation.java b/src/main/java/com/metaweb/gridworks/operations/EngineDependentOperation.java index 46cec5cf5..a86573a58 100644 --- a/src/main/java/com/metaweb/gridworks/operations/EngineDependentOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/EngineDependentOperation.java @@ -12,28 +12,28 @@ abstract public class EngineDependentOperation extends AbstractOperation { private static final long serialVersionUID = -2800091595856881731L; final private String _engineConfigString; - - transient protected JSONObject _engineConfig; - - protected EngineDependentOperation(JSONObject engineConfig) { - _engineConfig = engineConfig; - _engineConfigString = engineConfig.toString(); - } - - protected Engine createEngine(Project project) throws Exception { - Engine engine = new Engine(project); - engine.initializeFromJSON(getEngineConfig()); - return engine; - } - - protected JSONObject getEngineConfig() { - if (_engineConfig == null) { - try { - _engineConfig = ParsingUtilities.evaluateJsonStringToObject(_engineConfigString); - } catch (JSONException e) { - // ignore - } - } - return _engineConfig; - } + + transient protected JSONObject _engineConfig; + + protected EngineDependentOperation(JSONObject engineConfig) { + _engineConfig = engineConfig; + _engineConfigString = engineConfig.toString(); + } + + protected Engine createEngine(Project project) throws Exception { + Engine engine = new Engine(project); + engine.initializeFromJSON(getEngineConfig()); + return engine; + } + + protected JSONObject getEngineConfig() { + if (_engineConfig == null) { + try { + _engineConfig = ParsingUtilities.evaluateJsonStringToObject(_engineConfigString); + } catch (JSONException e) { + // ignore + } + } + return _engineConfig; + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/FacetBasedEditOperation.java b/src/main/java/com/metaweb/gridworks/operations/FacetBasedEditOperation.java index 1d82de4eb..053c6fef3 100644 --- a/src/main/java/com/metaweb/gridworks/operations/FacetBasedEditOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/FacetBasedEditOperation.java @@ -25,37 +25,37 @@ import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.model.changes.CellChange; public class FacetBasedEditOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = -7698202759999537298L; + private static final long serialVersionUID = -7698202759999537298L; - final protected String _expression; - final protected List _edits; - - static public class Edit implements Serializable, Jsonizable { - private static final long serialVersionUID = -4799990738910328002L; - - final public List from; - final public Object to; - - public Edit(List from, Object to) { - this.from = from; - this.to = to; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("from"); - writer.array(); - for (String s : from) { - writer.value(s); - } - writer.endArray(); - writer.key("to"); writer.value(to); - writer.endObject(); - } - } - + final protected String _expression; + final protected List _edits; + + static public class Edit implements Serializable, Jsonizable { + private static final long serialVersionUID = -4799990738910328002L; + + final public List from; + final public Object to; + + public Edit(List from, Object to) { + this.from = from; + this.to = to; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("from"); + writer.array(); + for (String s : from) { + writer.value(s); + } + writer.endArray(); + writer.key("to"); writer.value(to); + writer.endObject(); + } + } + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -72,105 +72,105 @@ public class FacetBasedEditOperation extends EngineDependentMassCellOperation { List edits = new ArrayList(editCount); for (int i = 0; i < editCount; i++) { - JSONObject editO = editsA.getJSONObject(i); - - JSONArray fromA = editO.getJSONArray("from"); + JSONObject editO = editsA.getJSONObject(i); + + JSONArray fromA = editO.getJSONArray("from"); int fromCount = fromA.length(); - List from = new ArrayList(fromCount); - for (int j = 0; j < fromCount; j++) { - from.add(fromA.getString(j)); - } + List from = new ArrayList(fromCount); + for (int j = 0; j < fromCount; j++) { + from.add(fromA.getString(j)); + } - edits.add(new Edit(from, editO.get("to"))); + edits.add(new Edit(from, editO.get("to"))); } return edits; } - public FacetBasedEditOperation(JSONObject engineConfig, String columnName, String expression, List edits) { - super(engineConfig, columnName, true); - _expression = expression; - _edits = edits; - } + public FacetBasedEditOperation(JSONObject engineConfig, String columnName, String expression, List edits) { + super(engineConfig, columnName, true); + _expression = expression; + _edits = edits; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.key("expression"); writer.value(_expression); - writer.key("edits"); - writer.array(); - for (Edit edit : _edits) { - edit.write(writer, options); - } - writer.endArray(); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.key("expression"); writer.value(_expression); + writer.key("edits"); + writer.array(); + for (Edit edit : _edits) { + edit.write(writer, options); + } + writer.endArray(); + writer.endObject(); + } - protected String getBriefDescription(Project project) { - return "Facet-based edit cells in column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Facet-based edit cells in column " + _columnName; + } - protected String createDescription(Column column, - List cellChanges) { - - return "Facet-based edit " + cellChanges.size() + - " cells in column " + column.getHeaderLabel(); - } + protected String createDescription(Column column, + List cellChanges) { + + return "Facet-based edit " + cellChanges.size() + + " cells in column " + column.getHeaderLabel(); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - Evaluable eval = MetaParser.parse(_expression); + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + Evaluable eval = MetaParser.parse(_expression); Properties bindings = ExpressionUtils.createBindings(project); Map fromTo = new HashMap(); for (Edit edit : _edits) { - for (String s : edit.from) { - fromTo.put(s, edit.to); - } + for (String s : edit.from) { + fromTo.put(s, edit.to); + } } - return new RowVisitor() { - int cellIndex; - Properties bindings; - List cellChanges; - Evaluable eval; - Map fromTo; - - public RowVisitor init( - int cellIndex, Properties bindings, List cellChanges, Evaluable eval, Map fromTo) { - this.cellIndex = cellIndex; - this.bindings = bindings; - this.cellChanges = cellChanges; - this.eval = eval; - this.fromTo = fromTo; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - Cell cell = row.getCell(cellIndex); + return new RowVisitor() { + int cellIndex; + Properties bindings; + List cellChanges; + Evaluable eval; + Map fromTo; + + public RowVisitor init( + int cellIndex, Properties bindings, List cellChanges, Evaluable eval, Map fromTo) { + this.cellIndex = cellIndex; + this.bindings = bindings; + this.cellChanges = cellChanges; + this.eval = eval; + this.fromTo = fromTo; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + Cell cell = row.getCell(cellIndex); ExpressionUtils.bind(bindings, row, rowIndex, cell); - + Object v = eval.evaluate(bindings); if (v != null) { - String from = v.toString(); - Object to = fromTo.get(from); - if (to != null) { - Cell newCell = new Cell(to, (cell != null) ? cell.recon : null); - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); - } + String from = v.toString(); + Object to = fromTo.get(from); + if (to != null) { + Cell newCell = new Cell(to, (cell != null) ? cell.recon : null); + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } } - return false; - } - }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo); - } + return false; + } + }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellJoinOperation.java b/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellJoinOperation.java index a220fd745..e315ccf11 100644 --- a/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellJoinOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellJoinOperation.java @@ -20,8 +20,8 @@ import com.metaweb.gridworks.model.changes.MassRowChange; public class MultiValuedCellJoinOperation extends AbstractOperation { private static final long serialVersionUID = 3134524625206033285L; - final protected String _columnName; - final protected String _keyColumnName; + final protected String _columnName; + final protected String _keyColumnName; final protected String _separator; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { @@ -32,15 +32,15 @@ public class MultiValuedCellJoinOperation extends AbstractOperation { ); } - public MultiValuedCellJoinOperation( - String columnName, - String keyColumnName, - String separator - ) { - _columnName = columnName; - _keyColumnName = keyColumnName; - _separator = separator; - } + public MultiValuedCellJoinOperation( + String columnName, + String keyColumnName, + String separator + ) { + _columnName = columnName; + _keyColumnName = keyColumnName; + _separator = separator; + } public void write(JSONWriter writer, Properties options) throws JSONException { @@ -54,77 +54,77 @@ public class MultiValuedCellJoinOperation extends AbstractOperation { writer.endObject(); } - protected String getBriefDescription(Project project) { - return "Join multi-valued cells in column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Join multi-valued cells in column " + _columnName; + } - protected HistoryEntry createHistoryEntry(Project project) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - if (column == null) { - throw new Exception("No column named " + _columnName); - } - int cellIndex = column.getCellIndex(); - - Column keyColumn = project.columnModel.getColumnByName(_keyColumnName); - if (column == null) { - throw new Exception("No key column named " + _keyColumnName); - } - int keyCellIndex = keyColumn.getCellIndex(); - - List newRows = new ArrayList(); - - int oldRowCount = project.rows.size(); - for (int r = 0; r < oldRowCount; r++) { - Row oldRow = project.rows.get(r); - - if (oldRow.isCellBlank(keyCellIndex)) { + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + if (column == null) { + throw new Exception("No column named " + _columnName); + } + int cellIndex = column.getCellIndex(); + + Column keyColumn = project.columnModel.getColumnByName(_keyColumnName); + if (column == null) { + throw new Exception("No key column named " + _keyColumnName); + } + int keyCellIndex = keyColumn.getCellIndex(); + + List newRows = new ArrayList(); + + int oldRowCount = project.rows.size(); + for (int r = 0; r < oldRowCount; r++) { + Row oldRow = project.rows.get(r); + + if (oldRow.isCellBlank(keyCellIndex)) { newRows.add(oldRow.dup()); continue; - } - - int r2 = r + 1; - while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) { - r2++; - } - - if (r2 == r + 1) { + } + + int r2 = r + 1; + while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) { + r2++; + } + + if (r2 == r + 1) { newRows.add(oldRow.dup()); continue; - } - - StringBuffer sb = new StringBuffer(); - for (int r3 = r; r3 < r2; r3++) { - Object value = project.rows.get(r3).getCellValue(cellIndex); - if (ExpressionUtils.isNonBlankData(value)) { - if (sb.length() > 0) { - sb.append(_separator); - } - sb.append(value.toString()); - } - } - - for (int r3 = r; r3 < r2; r3++) { - Row newRow = project.rows.get(r3).dup(); - if (r3 == r) { - newRow.setCell(cellIndex, new Cell(sb.toString(), null)); - } else { - newRow.setCell(cellIndex, null); - } - - if (!newRow.isEmpty()) { - newRows.add(newRow); - } - } - - r = r2 - 1; // r will be incremented by the for loop anyway - } - - return new HistoryEntry( - project, - getBriefDescription(null), - this, - new MassRowChange(newRows) - ); - } + } + + StringBuffer sb = new StringBuffer(); + for (int r3 = r; r3 < r2; r3++) { + Object value = project.rows.get(r3).getCellValue(cellIndex); + if (ExpressionUtils.isNonBlankData(value)) { + if (sb.length() > 0) { + sb.append(_separator); + } + sb.append(value.toString()); + } + } + + for (int r3 = r; r3 < r2; r3++) { + Row newRow = project.rows.get(r3).dup(); + if (r3 == r) { + newRow.setCell(cellIndex, new Cell(sb.toString(), null)); + } else { + newRow.setCell(cellIndex, null); + } + + if (!newRow.isEmpty()) { + newRows.add(newRow); + } + } + + r = r2 - 1; // r will be incremented by the for loop anyway + } + + return new HistoryEntry( + project, + getBriefDescription(null), + this, + new MassRowChange(newRows) + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellSplitOperation.java b/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellSplitOperation.java index f25f5408d..ef22a7c60 100644 --- a/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellSplitOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/MultiValuedCellSplitOperation.java @@ -20,8 +20,8 @@ import com.metaweb.gridworks.model.changes.MassRowChange; public class MultiValuedCellSplitOperation extends AbstractOperation { private static final long serialVersionUID = 8217930220439070322L; - final protected String _columnName; - final protected String _keyColumnName; + final protected String _columnName; + final protected String _keyColumnName; final protected String _separator; final protected String _mode; @@ -34,17 +34,17 @@ public class MultiValuedCellSplitOperation extends AbstractOperation { ); } - public MultiValuedCellSplitOperation( - String columnName, - String keyColumnName, - String separator, - String mode - ) { - _columnName = columnName; - _keyColumnName = keyColumnName; - _separator = separator; - _mode = mode; - } + public MultiValuedCellSplitOperation( + String columnName, + String keyColumnName, + String separator, + String mode + ) { + _columnName = columnName; + _keyColumnName = keyColumnName; + _separator = separator; + _mode = mode; + } public void write(JSONWriter writer, Properties options) throws JSONException { @@ -59,89 +59,89 @@ public class MultiValuedCellSplitOperation extends AbstractOperation { writer.endObject(); } - protected String getBriefDescription(Project project) { - return "Split multi-valued cells in column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Split multi-valued cells in column " + _columnName; + } - @Override - protected HistoryEntry createHistoryEntry(Project project) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - if (column == null) { - throw new Exception("No column named " + _columnName); - } - int cellIndex = column.getCellIndex(); - - Column keyColumn = project.columnModel.getColumnByName(_keyColumnName); - if (column == null) { - throw new Exception("No key column named " + _keyColumnName); - } - int keyCellIndex = keyColumn.getCellIndex(); - - List newRows = new ArrayList(); - - int oldRowCount = project.rows.size(); - for (int r = 0; r < oldRowCount; r++) { - Row oldRow = project.rows.get(r); - if (oldRow.isCellBlank(cellIndex)) { - newRows.add(oldRow.dup()); - continue; - } - - Object value = oldRow.getCellValue(cellIndex); - String s = value instanceof String ? ((String) value) : value.toString(); - String[] values = null; - if (_mode.equals("regex")) { - values = s.split(_separator); - } else { - values = StringUtils.splitByWholeSeparator(s, _separator); - } - - if (values.length < 2) { + @Override + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + if (column == null) { + throw new Exception("No column named " + _columnName); + } + int cellIndex = column.getCellIndex(); + + Column keyColumn = project.columnModel.getColumnByName(_keyColumnName); + if (column == null) { + throw new Exception("No key column named " + _keyColumnName); + } + int keyCellIndex = keyColumn.getCellIndex(); + + List newRows = new ArrayList(); + + int oldRowCount = project.rows.size(); + for (int r = 0; r < oldRowCount; r++) { + Row oldRow = project.rows.get(r); + if (oldRow.isCellBlank(cellIndex)) { newRows.add(oldRow.dup()); continue; - } - - // First value goes into the same row - { - Row firstNewRow = oldRow.dup(); - firstNewRow.setCell(cellIndex, new Cell(values[0].trim(), null)); - - newRows.add(firstNewRow); - } - - int r2 = r + 1; - for (int v = 1; v < values.length; v++) { - Cell newCell = new Cell(values[v].trim(), null); - - if (r2 < project.rows.size()) { - Row oldRow2 = project.rows.get(r2); - if (oldRow2.isCellBlank(cellIndex) && - oldRow2.isCellBlank(keyCellIndex)) { - - Row newRow = oldRow2.dup(); - newRow.setCell(cellIndex, newCell); - - newRows.add(newRow); - r2++; - - continue; - } - } - - Row newRow = new Row(cellIndex + 1); - newRow.setCell(cellIndex, newCell); - - newRows.add(newRow); - } - - r = r2 - 1; // r will be incremented by the for loop anyway - } - - return new HistoryEntry( - project, - getBriefDescription(null), - this, - new MassRowChange(newRows) - ); - } + } + + Object value = oldRow.getCellValue(cellIndex); + String s = value instanceof String ? ((String) value) : value.toString(); + String[] values = null; + if (_mode.equals("regex")) { + values = s.split(_separator); + } else { + values = StringUtils.splitByWholeSeparator(s, _separator); + } + + if (values.length < 2) { + newRows.add(oldRow.dup()); + continue; + } + + // First value goes into the same row + { + Row firstNewRow = oldRow.dup(); + firstNewRow.setCell(cellIndex, new Cell(values[0].trim(), null)); + + newRows.add(firstNewRow); + } + + int r2 = r + 1; + for (int v = 1; v < values.length; v++) { + Cell newCell = new Cell(values[v].trim(), null); + + if (r2 < project.rows.size()) { + Row oldRow2 = project.rows.get(r2); + if (oldRow2.isCellBlank(cellIndex) && + oldRow2.isCellBlank(keyCellIndex)) { + + Row newRow = oldRow2.dup(); + newRow.setCell(cellIndex, newCell); + + newRows.add(newRow); + r2++; + + continue; + } + } + + Row newRow = new Row(cellIndex + 1); + newRow.setCell(cellIndex, newCell); + + newRows.add(newRow); + } + + r = r2 - 1; // r will be incremented by the for loop anyway + } + + return new HistoryEntry( + project, + getBriefDescription(null), + this, + new MassRowChange(newRows) + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconDiscardJudgmentsOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconDiscardJudgmentsOperation.java index 71fefe7ad..1d852a096 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconDiscardJudgmentsOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconDiscardJudgmentsOperation.java @@ -20,7 +20,7 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.ReconChange; public class ReconDiscardJudgmentsOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = 6799029731665369179L; + private static final long serialVersionUID = 6799029731665369179L; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -32,69 +32,69 @@ public class ReconDiscardJudgmentsOperation extends EngineDependentMassCellOpera ); } - public ReconDiscardJudgmentsOperation(JSONObject engineConfig, String columnName) { - super(engineConfig, columnName, false); - } + public ReconDiscardJudgmentsOperation(JSONObject engineConfig, String columnName) { + super(engineConfig, columnName, false); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.endObject(); + } - protected String getBriefDescription(Project project) { - return "Discard recon judgments for cells in column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Discard recon judgments for cells in column " + _columnName; + } - protected String createDescription(Column column, - List cellChanges) { - - return "Discard recon judgments for " + cellChanges.size() + - " cells in column " + column.getHeaderLabel(); - } + protected String createDescription(Column column, + List cellChanges) { + + return "Discard recon judgments for " + cellChanges.size() + + " cells in column " + column.getHeaderLabel(); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - return new RowVisitor() { - int cellIndex; - List cellChanges; - - public RowVisitor init(int cellIndex, List cellChanges) { - this.cellIndex = cellIndex; - this.cellChanges = cellChanges; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - if (cellIndex < row.cells.size()) { - Cell cell = row.cells.get(cellIndex); - if (cell.recon != null) { - Recon recon = cell.recon.dup(); - recon.judgment = Judgment.None; + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + if (cell.recon != null) { + Recon recon = cell.recon.dup(); + recon.judgment = Judgment.None; - Cell newCell = new Cell(cell.value, recon); - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); - } - } - return false; - } - }.init(column.getCellIndex(), cellChanges); - } - - protected Change createChange(Project project, Column column, List cellChanges) { - return new ReconChange( - cellChanges, - _columnName, - column.getReconConfig(), - null - ); - } + Cell newCell = new Cell(cell.value, recon); + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } + + protected Change createChange(Project project, Column column, List cellChanges) { + return new ReconChange( + cellChanges, + _columnName, + column.getReconConfig(), + null + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconJudgeSimilarCellsOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconJudgeSimilarCellsOperation.java index eb5a9d75e..e01400dd3 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconJudgeSimilarCellsOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconJudgeSimilarCellsOperation.java @@ -25,27 +25,27 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.ReconChange; public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = -5205694623711144436L; - - final protected String _similarValue; - final protected Judgment _judgment; - final protected ReconCandidate _match; - final protected boolean _shareNewTopics; + private static final long serialVersionUID = -5205694623711144436L; + + final protected String _similarValue; + final protected Judgment _judgment; + final protected ReconCandidate _match; + final protected boolean _shareNewTopics; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); ReconCandidate match = null; if (obj.has("match")) { - JSONObject matchObj = obj.getJSONObject("match"); - - JSONArray types = matchObj.getJSONArray("types"); - String[] typeIDs = new String[types.length()]; - for (int i = 0; i < typeIDs.length; i++) { - typeIDs[i] = types.getString(i); - } - - match = new ReconCandidate( + JSONObject matchObj = obj.getJSONObject("match"); + + JSONArray types = matchObj.getJSONArray("types"); + String[] typeIDs = new String[types.length()]; + for (int i = 0; i < typeIDs.length; i++) { + typeIDs[i] = types.getString(i); + } + + match = new ReconCandidate( matchObj.getString("id"), matchObj.getString("guid"), matchObj.getString("name"), @@ -56,7 +56,7 @@ public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOper Judgment judgment = Judgment.None; if (obj.has("judgment")) { - judgment = Recon.stringToJudgment(obj.getString("judgment")); + judgment = Recon.stringToJudgment(obj.getString("judgment")); } return new ReconJudgeSimilarCellsOperation( @@ -69,117 +69,117 @@ public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOper ); } - public ReconJudgeSimilarCellsOperation( - JSONObject engineConfig, - String columnName, - String similarValue, - Judgment judgment, - ReconCandidate match, - boolean shareNewTopics - ) { - super(engineConfig, columnName, false); - this._similarValue = similarValue; - this._judgment = judgment; - this._match = match; - this._shareNewTopics = shareNewTopics; - } + public ReconJudgeSimilarCellsOperation( + JSONObject engineConfig, + String columnName, + String similarValue, + Judgment judgment, + ReconCandidate match, + boolean shareNewTopics + ) { + super(engineConfig, columnName, false); + this._similarValue = similarValue; + this._judgment = judgment; + this._match = match; + this._shareNewTopics = shareNewTopics; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.key("similarValue"); writer.value(_similarValue); - writer.key("judgment"); writer.value(Recon.judgmentToString(_judgment)); - if (_match != null) { - writer.key("match"); _match.write(writer, options); - } - writer.key("shareNewTopics"); writer.value(_shareNewTopics); - - writer.endObject(); - } - - protected String getBriefDescription(Project project) { - if (_judgment == Judgment.None) { - return "Discard recon judgments for cells containing \"" + - _similarValue + "\" in column " + _columnName; - } else if (_judgment == Judgment.New) { - if (_shareNewTopics) { - return "Mark to create one single new topic for all cells containing \"" + - _similarValue + "\" in column " + _columnName; - } else { - return "Mark to create one new topic for each cell containing \"" + - _similarValue + "\" in column " + _columnName; - } - } else if (_judgment == Judgment.Matched) { - return "Match topic " + - _match.topicName + " (" + - _match.topicID + ") for cells containing \"" + - _similarValue + "\" in column " + _columnName; - } - throw new InternalError("Can't get here"); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.key("similarValue"); writer.value(_similarValue); + writer.key("judgment"); writer.value(Recon.judgmentToString(_judgment)); + if (_match != null) { + writer.key("match"); _match.write(writer, options); + } + writer.key("shareNewTopics"); writer.value(_shareNewTopics); + + writer.endObject(); + } + + protected String getBriefDescription(Project project) { + if (_judgment == Judgment.None) { + return "Discard recon judgments for cells containing \"" + + _similarValue + "\" in column " + _columnName; + } else if (_judgment == Judgment.New) { + if (_shareNewTopics) { + return "Mark to create one single new topic for all cells containing \"" + + _similarValue + "\" in column " + _columnName; + } else { + return "Mark to create one new topic for each cell containing \"" + + _similarValue + "\" in column " + _columnName; + } + } else if (_judgment == Judgment.Matched) { + return "Match topic " + + _match.topicName + " (" + + _match.topicID + ") for cells containing \"" + + _similarValue + "\" in column " + _columnName; + } + throw new InternalError("Can't get here"); + } - protected String createDescription(Column column, - List cellChanges) { - - if (_judgment == Judgment.None) { - return "Discard recon judgments for " + cellChanges.size() + " cells containing \"" + - _similarValue + "\" in column " + _columnName; - } else if (_judgment == Judgment.New) { - if (_shareNewTopics) { - return "Mark to create one single new topic for " + cellChanges.size() + " cells containing \"" + - _similarValue + "\" in column " + _columnName; - } else { - return "Mark to create one new topic for each of " + cellChanges.size() + " cells containing \"" + - _similarValue + "\" in column " + _columnName; - } - } else if (_judgment == Judgment.Matched) { - return "Match topic " + - _match.topicName + " (" + - _match.topicID + ") for " + - cellChanges.size() + " cells containing \"" + - _similarValue + "\" in column " + _columnName; - } - throw new InternalError("Can't get here"); - } + protected String createDescription(Column column, + List cellChanges) { + + if (_judgment == Judgment.None) { + return "Discard recon judgments for " + cellChanges.size() + " cells containing \"" + + _similarValue + "\" in column " + _columnName; + } else if (_judgment == Judgment.New) { + if (_shareNewTopics) { + return "Mark to create one single new topic for " + cellChanges.size() + " cells containing \"" + + _similarValue + "\" in column " + _columnName; + } else { + return "Mark to create one new topic for each of " + cellChanges.size() + " cells containing \"" + + _similarValue + "\" in column " + _columnName; + } + } else if (_judgment == Judgment.Matched) { + return "Match topic " + + _match.topicName + " (" + + _match.topicID + ") for " + + cellChanges.size() + " cells containing \"" + + _similarValue + "\" in column " + _columnName; + } + throw new InternalError("Can't get here"); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - return new RowVisitor() { - int _cellIndex; - List _cellChanges; - Map _sharedRecons = new HashMap(); - - public RowVisitor init(int cellIndex, List cellChanges) { - _cellIndex = cellIndex; - _cellChanges = cellChanges; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int _cellIndex; + List _cellChanges; + Map _sharedRecons = new HashMap(); + + public RowVisitor init(int cellIndex, List cellChanges) { + _cellIndex = cellIndex; + _cellChanges = cellChanges; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { Cell cell = row.getCell(_cellIndex); if (cell != null && - ExpressionUtils.isNonBlankData(cell.value) && - _similarValue.equals(cell.value)) { - - Recon recon = null; - if (_judgment == Judgment.New && _shareNewTopics) { - String s = cell.value.toString(); - if (_sharedRecons.containsKey(s)) { - recon = _sharedRecons.get(s); - } else { - recon = new Recon(); - recon.judgment = Judgment.New; - - _sharedRecons.put(s, recon); - } - } else { - recon = cell.recon == null ? new Recon() : cell.recon.dup(); + ExpressionUtils.isNonBlankData(cell.value) && + _similarValue.equals(cell.value)) { + + Recon recon = null; + if (_judgment == Judgment.New && _shareNewTopics) { + String s = cell.value.toString(); + if (_sharedRecons.containsKey(s)) { + recon = _sharedRecons.get(s); + } else { + recon = new Recon(); + recon.judgment = Judgment.New; + + _sharedRecons.put(s, recon); + } + } else { + recon = cell.recon == null ? new Recon() : cell.recon.dup(); if (_judgment == Judgment.Matched) { recon.judgment = Recon.Judgment.Matched; recon.match = _match; @@ -190,25 +190,25 @@ public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOper recon.judgment = Recon.Judgment.None; recon.match = null; } - } - + } + Cell newCell = new Cell(cell.value, recon); CellChange cellChange = new CellChange(rowIndex, _cellIndex, cell, newCell); _cellChanges.add(cellChange); } return false; - } - }.init(column.getCellIndex(), cellChanges); - } - - - protected Change createChange(Project project, Column column, List cellChanges) { - return new ReconChange( - cellChanges, - _columnName, - column.getReconConfig(), - null - ); - } + } + }.init(column.getCellIndex(), cellChanges); + } + + + protected Change createChange(Project project, Column column, List cellChanges) { + return new ReconChange( + cellChanges, + _columnName, + column.getReconConfig(), + null + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconMarkNewTopicsOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconMarkNewTopicsOperation.java index a5d9277a3..1a68a3ef9 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconMarkNewTopicsOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconMarkNewTopicsOperation.java @@ -22,11 +22,11 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.ReconChange; public class ReconMarkNewTopicsOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = -5205694623711144436L; - - final protected boolean _shareNewTopics; - - static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { + private static final long serialVersionUID = -5205694623711144436L; + + final protected boolean _shareNewTopics; + + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); return new ReconMarkNewTopicsOperation( @@ -34,92 +34,92 @@ public class ReconMarkNewTopicsOperation extends EngineDependentMassCellOperatio obj.getString("columnName"), obj.has("shareNewTopics") ? obj.getBoolean("shareNewTopics") : false ); - } + } - public ReconMarkNewTopicsOperation(JSONObject engineConfig, String columnName, boolean shareNewTopics) { - super(engineConfig, columnName, false); - _shareNewTopics = shareNewTopics; - } + public ReconMarkNewTopicsOperation(JSONObject engineConfig, String columnName, boolean shareNewTopics) { + super(engineConfig, columnName, false); + _shareNewTopics = shareNewTopics; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.key("shareNewTopics"); writer.value(_shareNewTopics); - writer.endObject(); - } - - protected String getBriefDescription(Project project) { - return "Mark to create new topics for cells in column " + _columnName + - (_shareNewTopics ? - ", one topic for each group of similar cells" : - ", one topic for each cell"); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.key("shareNewTopics"); writer.value(_shareNewTopics); + writer.endObject(); + } + + protected String getBriefDescription(Project project) { + return "Mark to create new topics for cells in column " + _columnName + + (_shareNewTopics ? + ", one topic for each group of similar cells" : + ", one topic for each cell"); + } - protected String createDescription(Column column, - List cellChanges) { - - return "Mark to create new topics for " + cellChanges.size() + - " cells in column " + column.getHeaderLabel() + - (_shareNewTopics ? - ", one topic for each group of similar cells" : - ", one topic for each cell"); - } + protected String createDescription(Column column, + List cellChanges) { + + return "Mark to create new topics for " + cellChanges.size() + + " cells in column " + column.getHeaderLabel() + + (_shareNewTopics ? + ", one topic for each group of similar cells" : + ", one topic for each cell"); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - return new RowVisitor() { - int cellIndex; - List cellChanges; - Map _sharedRecons = new HashMap(); - - public RowVisitor init(int cellIndex, List cellChanges) { - this.cellIndex = cellIndex; - this.cellChanges = cellChanges; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + Map _sharedRecons = new HashMap(); + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { Cell cell = row.getCell(cellIndex); if (cell != null) { - Recon recon = null; - if (_shareNewTopics) { - String s = cell.value == null ? "" : cell.value.toString(); - if (_sharedRecons.containsKey(s)) { - recon = _sharedRecons.get(s); - } else { - recon = new Recon(); - recon.judgment = Judgment.New; - - _sharedRecons.put(s, recon); - } - } else { - recon = cell.recon == null ? new Recon() : cell.recon.dup(); - recon.match = null; - recon.judgment = Judgment.New; - } - - Cell newCell = new Cell(cell.value, recon); - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); - } - return false; - } - }.init(column.getCellIndex(), cellChanges); - } - - protected Change createChange(Project project, Column column, List cellChanges) { - return new ReconChange( - cellChanges, - _columnName, - column.getReconConfig(), - null - ); - } + Recon recon = null; + if (_shareNewTopics) { + String s = cell.value == null ? "" : cell.value.toString(); + if (_sharedRecons.containsKey(s)) { + recon = _sharedRecons.get(s); + } else { + recon = new Recon(); + recon.judgment = Judgment.New; + + _sharedRecons.put(s, recon); + } + } else { + recon = cell.recon == null ? new Recon() : cell.recon.dup(); + recon.match = null; + recon.judgment = Judgment.New; + } + + Cell newCell = new Cell(cell.value, recon); + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } + + protected Change createChange(Project project, Column column, List cellChanges) { + return new ReconChange( + cellChanges, + _columnName, + column.getReconConfig(), + null + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java index 5cbbbfc97..ad763e2e9 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java @@ -20,8 +20,8 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.ReconChange; public class ReconMatchBestCandidatesOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = 5393888241057341155L; - + private static final long serialVersionUID = 5393888241057341155L; + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); String columnName = obj.getString("columnName"); @@ -32,74 +32,74 @@ public class ReconMatchBestCandidatesOperation extends EngineDependentMassCellOp ); } - public ReconMatchBestCandidatesOperation(JSONObject engineConfig, String columnName) { - super(engineConfig, columnName, false); - } + public ReconMatchBestCandidatesOperation(JSONObject engineConfig, String columnName) { + super(engineConfig, columnName, false); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.endObject(); + } - protected String getBriefDescription(Project project) { - return "Match each cell to its best recon candidate in column " + _columnName; - } + protected String getBriefDescription(Project project) { + return "Match each cell to its best recon candidate in column " + _columnName; + } - protected String createDescription(Column column, - List cellChanges) { - - return "Match each of " + cellChanges.size() + - " cells to its best candidate in column " + column.getHeaderLabel(); - } + protected String createDescription(Column column, + List cellChanges) { + + return "Match each of " + cellChanges.size() + + " cells to its best candidate in column " + column.getHeaderLabel(); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - return new RowVisitor() { - int cellIndex; - List cellChanges; - - public RowVisitor init(int cellIndex, List cellChanges) { - this.cellIndex = cellIndex; - this.cellChanges = cellChanges; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - if (cellIndex < row.cells.size()) { - Cell cell = row.cells.get(cellIndex); - if (cell.recon != null) { - ReconCandidate candidate = cell.recon.getBestCandidate(); - if (candidate != null) { - Cell newCell = new Cell( - cell.value, - cell.recon.dup() - ); - newCell.recon.match = candidate; - newCell.recon.judgment = Judgment.Matched; - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); - } - } - } - return false; - } - }.init(column.getCellIndex(), cellChanges); - } - - protected Change createChange(Project project, Column column, List cellChanges) { - return new ReconChange( - cellChanges, - _columnName, - column.getReconConfig(), - null - ); - } + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + if (cell.recon != null) { + ReconCandidate candidate = cell.recon.getBestCandidate(); + if (candidate != null) { + Cell newCell = new Cell( + cell.value, + cell.recon.dup() + ); + newCell.recon.match = candidate; + newCell.recon.judgment = Judgment.Matched; + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } + } + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } + + protected Change createChange(Project project, Column column, List cellChanges) { + return new ReconChange( + cellChanges, + _columnName, + column.getReconConfig(), + null + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconMatchSpecificTopicOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconMatchSpecificTopicOperation.java index 332dfa0d6..e0be7a8e1 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconMatchSpecificTopicOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconMatchSpecificTopicOperation.java @@ -22,9 +22,9 @@ import com.metaweb.gridworks.model.changes.CellChange; import com.metaweb.gridworks.model.changes.ReconChange; public class ReconMatchSpecificTopicOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = -5205694623711144436L; - - final protected ReconCandidate match; + private static final long serialVersionUID = -5205694623711144436L; + + final protected ReconCandidate match; static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -50,22 +50,22 @@ public class ReconMatchSpecificTopicOperation extends EngineDependentMassCellOpe ); } - public ReconMatchSpecificTopicOperation(JSONObject engineConfig, String columnName, ReconCandidate match) { - super(engineConfig, columnName, false); - this.match = match; - } + public ReconMatchSpecificTopicOperation(JSONObject engineConfig, String columnName, ReconCandidate match) { + super(engineConfig, columnName, false); + this.match = match; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.key("match"); - writer.object(); - writer.key("id"); writer.value(match.topicID); + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.key("match"); + writer.object(); + writer.key("id"); writer.value(match.topicID); writer.key("guid"); writer.value(match.topicGUID); writer.key("name"); writer.value(match.topicName); writer.key("types"); @@ -74,62 +74,62 @@ public class ReconMatchSpecificTopicOperation extends EngineDependentMassCellOpe writer.value(typeID); } writer.endArray(); - writer.endObject(); - writer.endObject(); - } - - protected String getBriefDescription(Project project) { - return "Match specific topic " + - match.topicName + " (" + - match.topicID + ") to cells in column " + _columnName; - } + writer.endObject(); + writer.endObject(); + } + + protected String getBriefDescription(Project project) { + return "Match specific topic " + + match.topicName + " (" + + match.topicID + ") to cells in column " + _columnName; + } - protected String createDescription(Column column, - List cellChanges) { - return "Match specific topic " + - match.topicName + " (" + - match.topicID + ") to " + cellChanges.size() + - " cells in column " + column.getHeaderLabel(); - } + protected String createDescription(Column column, + List cellChanges) { + return "Match specific topic " + + match.topicName + " (" + + match.topicID + ") to " + cellChanges.size() + + " cells in column " + column.getHeaderLabel(); + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - return new RowVisitor() { - int cellIndex; - List cellChanges; - - public RowVisitor init(int cellIndex, List cellChanges) { - this.cellIndex = cellIndex; - this.cellChanges = cellChanges; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - if (cellIndex < row.cells.size()) { - Cell cell = row.cells.get(cellIndex); - - Cell newCell = new Cell( - cell.value, - cell.recon != null ? cell.recon.dup() : new Recon() - ); - newCell.recon.match = match; - newCell.recon.judgment = Judgment.Matched; - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); - } - return false; - } - }.init(column.getCellIndex(), cellChanges); - } - - protected Change createChange(Project project, Column column, List cellChanges) { - return new ReconChange( - cellChanges, - _columnName, - column.getReconConfig(), - null - ); - } + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + + Cell newCell = new Cell( + cell.value, + cell.recon != null ? cell.recon.dup() : new Recon() + ); + newCell.recon.match = match; + newCell.recon.judgment = Judgment.Matched; + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } + + protected Change createChange(Project project, Column column, List cellChanges) { + return new ReconChange( + cellChanges, + _columnName, + column.getReconConfig(), + null + ); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java index 5d0c22a73..15717266a 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java @@ -30,11 +30,11 @@ import com.metaweb.gridworks.process.LongRunningProcess; import com.metaweb.gridworks.process.Process; public class ReconOperation extends EngineDependentOperation { - private static final long serialVersionUID = 838795186905314865L; - - final protected String _columnName; - final protected ReconConfig _reconConfig; - + private static final long serialVersionUID = 838795186905314865L; + + final protected String _columnName; + final protected ReconConfig _reconConfig; + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -45,191 +45,191 @@ public class ReconOperation extends EngineDependentOperation { ); } - public ReconOperation( + public ReconOperation( JSONObject engineConfig, String columnName, ReconConfig reconConfig ) { - super(engineConfig); - _columnName = columnName; - _reconConfig = reconConfig; - } + super(engineConfig); + _columnName = columnName; + _reconConfig = reconConfig; + } - public Process createProcess(Project project, Properties options) throws Exception { - return new ReconProcess( - project, - getEngineConfig(), - getBriefDescription(null) - ); - } - - protected String getBriefDescription(Project project) { + public Process createProcess(Project project, Properties options) throws Exception { + return new ReconProcess( + project, + getEngineConfig(), + getBriefDescription(null) + ); + } + + protected String getBriefDescription(Project project) { return _reconConfig.getBriefDescription(project, _columnName); - } + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("columnName"); writer.value(_columnName); - writer.key("config"); _reconConfig.write(writer, options); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("columnName"); writer.value(_columnName); + writer.key("config"); _reconConfig.write(writer, options); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.endObject(); + } - static protected class ReconEntry { - final public int rowIndex; - final public Cell cell; - - public ReconEntry(int rowIndex, Cell cell) { - this.rowIndex = rowIndex; - this.cell = cell; - } - } - static protected class JobGroup { - final public ReconJob job; - final public List entries = new ArrayList(); - - public JobGroup(ReconJob job) { - this.job = job; - } - } - - public class ReconProcess extends LongRunningProcess implements Runnable { - final protected Project _project; - final protected JSONObject _engineConfig; - protected List _entries; - protected int _cellIndex; - - public ReconProcess( - Project project, - JSONObject engineConfig, - String description - ) { - super(description); - _project = project; - _engineConfig = engineConfig; - } - - protected Runnable getRunnable() { - return this; - } - - protected void populateEntries() throws Exception { - Engine engine = new Engine(_project); - engine.initializeFromJSON(_engineConfig); - - Column column = _project.columnModel.getColumnByName(_columnName); - if (column == null) { - throw new Exception("No column named " + _columnName); - } - - _entries = new ArrayList(_project.rows.size()); - _cellIndex = column.getCellIndex(); - - FilteredRows filteredRows = engine.getAllFilteredRows(false); - filteredRows.accept(_project, new RowVisitor() { - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - if (_cellIndex < row.cells.size()) { - Cell cell = row.cells.get(_cellIndex); - if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { - _entries.add(new ReconEntry(rowIndex, cell)); - } - } - return false; - } - }); - } - - public void run() { - try { - populateEntries(); - } catch (Exception e2) { - // TODO : Not sure what to do here? - e2.printStackTrace(); - } - - Map jobKeyToGroup = new HashMap(); - - for (ReconEntry entry : _entries) { - ReconJob job = _reconConfig.createJob( - _project, - entry.rowIndex, - _project.rows.get(entry.rowIndex), - _columnName, - entry.cell - ); - - int key = job.getKey(); - JobGroup group = jobKeyToGroup.get(key); - if (group == null) { - group = new JobGroup(job); - jobKeyToGroup.put(key, group); - } - group.entries.add(entry); - } - - List cellChanges = new ArrayList(_entries.size()); - List groups = new ArrayList(jobKeyToGroup.values()); - - int batchSize = _reconConfig.getBatchSize(); - for (int i = 0; i < groups.size(); i += batchSize) { - int to = Math.min(i + batchSize, groups.size()); - - List jobs = new ArrayList(to - i); - for (int j = i; j < to; j++) { - jobs.add(groups.get(j).job); - } - - List recons = _reconConfig.batchRecon(jobs); - for (int j = i; j < to; j++) { - Recon recon = recons.get(j - i); - if (recon == null) { - recon = new Recon(); - } - - for (ReconEntry entry : groups.get(j).entries) { - Cell oldCell = entry.cell; - Cell newCell = new Cell(oldCell.value, recon); - - CellChange cellChange = new CellChange( - entry.rowIndex, - _cellIndex, - oldCell, - newCell - ); - cellChanges.add(cellChange); - } - } - - _progress = i * 100 / groups.size(); - try { - Thread.sleep(50); - } catch (InterruptedException e) { - if (_canceled) { - break; - } - } - } - - Change reconChange = new ReconChange( - cellChanges, - _columnName, - _reconConfig, - null - ); - - HistoryEntry historyEntry = new HistoryEntry( - _project, - _description, - ReconOperation.this, - reconChange - ); - - _project.history.addEntry(historyEntry); - _project.processManager.onDoneProcess(this); - } - } + static protected class ReconEntry { + final public int rowIndex; + final public Cell cell; + + public ReconEntry(int rowIndex, Cell cell) { + this.rowIndex = rowIndex; + this.cell = cell; + } + } + static protected class JobGroup { + final public ReconJob job; + final public List entries = new ArrayList(); + + public JobGroup(ReconJob job) { + this.job = job; + } + } + + public class ReconProcess extends LongRunningProcess implements Runnable { + final protected Project _project; + final protected JSONObject _engineConfig; + protected List _entries; + protected int _cellIndex; + + public ReconProcess( + Project project, + JSONObject engineConfig, + String description + ) { + super(description); + _project = project; + _engineConfig = engineConfig; + } + + protected Runnable getRunnable() { + return this; + } + + protected void populateEntries() throws Exception { + Engine engine = new Engine(_project); + engine.initializeFromJSON(_engineConfig); + + Column column = _project.columnModel.getColumnByName(_columnName); + if (column == null) { + throw new Exception("No column named " + _columnName); + } + + _entries = new ArrayList(_project.rows.size()); + _cellIndex = column.getCellIndex(); + + FilteredRows filteredRows = engine.getAllFilteredRows(false); + filteredRows.accept(_project, new RowVisitor() { + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + if (_cellIndex < row.cells.size()) { + Cell cell = row.cells.get(_cellIndex); + if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { + _entries.add(new ReconEntry(rowIndex, cell)); + } + } + return false; + } + }); + } + + public void run() { + try { + populateEntries(); + } catch (Exception e2) { + // TODO : Not sure what to do here? + e2.printStackTrace(); + } + + Map jobKeyToGroup = new HashMap(); + + for (ReconEntry entry : _entries) { + ReconJob job = _reconConfig.createJob( + _project, + entry.rowIndex, + _project.rows.get(entry.rowIndex), + _columnName, + entry.cell + ); + + int key = job.getKey(); + JobGroup group = jobKeyToGroup.get(key); + if (group == null) { + group = new JobGroup(job); + jobKeyToGroup.put(key, group); + } + group.entries.add(entry); + } + + List cellChanges = new ArrayList(_entries.size()); + List groups = new ArrayList(jobKeyToGroup.values()); + + int batchSize = _reconConfig.getBatchSize(); + for (int i = 0; i < groups.size(); i += batchSize) { + int to = Math.min(i + batchSize, groups.size()); + + List jobs = new ArrayList(to - i); + for (int j = i; j < to; j++) { + jobs.add(groups.get(j).job); + } + + List recons = _reconConfig.batchRecon(jobs); + for (int j = i; j < to; j++) { + Recon recon = recons.get(j - i); + if (recon == null) { + recon = new Recon(); + } + + for (ReconEntry entry : groups.get(j).entries) { + Cell oldCell = entry.cell; + Cell newCell = new Cell(oldCell.value, recon); + + CellChange cellChange = new CellChange( + entry.rowIndex, + _cellIndex, + oldCell, + newCell + ); + cellChanges.add(cellChange); + } + } + + _progress = i * 100 / groups.size(); + try { + Thread.sleep(50); + } catch (InterruptedException e) { + if (_canceled) { + break; + } + } + } + + Change reconChange = new ReconChange( + cellChanges, + _columnName, + _reconConfig, + null + ); + + HistoryEntry historyEntry = new HistoryEntry( + _project, + _description, + ReconOperation.this, + reconChange + ); + + _project.history.addEntry(historyEntry); + _project.processManager.onDoneProcess(this); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/RowStarOperation.java b/src/main/java/com/metaweb/gridworks/operations/RowStarOperation.java index 331184218..a418ccf26 100644 --- a/src/main/java/com/metaweb/gridworks/operations/RowStarOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/RowStarOperation.java @@ -34,25 +34,25 @@ public class RowStarOperation extends EngineDependentOperation { ); } - public RowStarOperation(JSONObject engineConfig, boolean starred) { - super(engineConfig); - _starred = starred; - } + public RowStarOperation(JSONObject engineConfig, boolean starred) { + super(engineConfig); + _starred = starred; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("starred"); writer.value(_starred); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("starred"); writer.value(_starred); + writer.endObject(); + } - protected String getBriefDescription(Project project) { - return (_starred ? "Star rows" : "Unstar rows"); - } + protected String getBriefDescription(Project project) { + return (_starred ? "Star rows" : "Unstar rows"); + } protected HistoryEntry createHistoryEntry(Project project) throws Exception { Engine engine = createEngine(project); @@ -70,23 +70,23 @@ public class RowStarOperation extends EngineDependentOperation { ); } - protected RowVisitor createRowVisitor(Project project, List changes) throws Exception { - return new RowVisitor() { - List changes; - - public RowVisitor init(List changes) { - this.changes = changes; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - if (row.starred != _starred) { - RowStarChange change = new RowStarChange(rowIndex, _starred); - - changes.add(change); - } - return false; - } - }.init(changes); - } + protected RowVisitor createRowVisitor(Project project, List changes) throws Exception { + return new RowVisitor() { + List changes; + + public RowVisitor init(List changes) { + this.changes = changes; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + if (row.starred != _starred) { + RowStarChange change = new RowStarChange(rowIndex, _starred); + + changes.add(change); + } + return false; + } + }.init(changes); + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/SaveProtographOperation.java b/src/main/java/com/metaweb/gridworks/operations/SaveProtographOperation.java index 5bad8b721..0c8cc53e1 100644 --- a/src/main/java/com/metaweb/gridworks/operations/SaveProtographOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/SaveProtographOperation.java @@ -23,11 +23,11 @@ public class SaveProtographOperation extends AbstractOperation { ); } - public SaveProtographOperation( - Protograph protograph - ) { - _protograph = protograph; - } + public SaveProtographOperation( + Protograph protograph + ) { + _protograph = protograph; + } public void write(JSONWriter writer, Properties options) throws JSONException { @@ -39,40 +39,40 @@ public class SaveProtographOperation extends AbstractOperation { writer.endObject(); } - protected String getBriefDescription(Project project) { - return "Save schema skeleton"; - } + protected String getBriefDescription(Project project) { + return "Save schema skeleton"; + } - @Override - protected HistoryEntry createHistoryEntry(Project project) throws Exception { + @Override + protected HistoryEntry createHistoryEntry(Project project) throws Exception { String description = "Save schema-alignment protograph"; - Change change = new ProtographChange(_protograph); - - return new HistoryEntry(project, description, SaveProtographOperation.this, change); - } + Change change = new ProtographChange(_protograph); + + return new HistoryEntry(project, description, SaveProtographOperation.this, change); + } - static public class ProtographChange implements Change { - private static final long serialVersionUID = -564820111174473901L; - - final protected Protograph _newProtograph; - protected Protograph _oldProtograph; - - public ProtographChange(Protograph protograph) { - _newProtograph = protograph; - } - - public void apply(Project project) { - synchronized (project) { - _oldProtograph = project.protograph; - project.protograph = _newProtograph; - } - } + static public class ProtographChange implements Change { + private static final long serialVersionUID = -564820111174473901L; + + final protected Protograph _newProtograph; + protected Protograph _oldProtograph; + + public ProtographChange(Protograph protograph) { + _newProtograph = protograph; + } + + public void apply(Project project) { + synchronized (project) { + _oldProtograph = project.protograph; + project.protograph = _newProtograph; + } + } - public void revert(Project project) { - synchronized (project) { - project.protograph = _oldProtograph; - } - } - } + public void revert(Project project) { + synchronized (project) { + project.protograph = _oldProtograph; + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java index d803c7da3..bf6951179 100644 --- a/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/TextTransformOperation.java @@ -19,10 +19,10 @@ import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.model.changes.CellChange; public class TextTransformOperation extends EngineDependentMassCellOperation { - private static final long serialVersionUID = -7698202759999537298L; + private static final long serialVersionUID = -7698202759999537298L; - final protected String _expression; - + final protected String _expression; + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { JSONObject engineConfig = obj.getJSONObject("engineConfig"); @@ -33,69 +33,69 @@ public class TextTransformOperation extends EngineDependentMassCellOperation { ); } - public TextTransformOperation(JSONObject engineConfig, String columnName, String expression) { - super(engineConfig, columnName, true); - _expression = expression; - } + public TextTransformOperation(JSONObject engineConfig, String columnName, String expression) { + super(engineConfig, columnName, true); + _expression = expression; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); - writer.key("description"); writer.value(getBriefDescription(null)); - writer.key("engineConfig"); writer.value(getEngineConfig()); - writer.key("columnName"); writer.value(_columnName); - writer.key("expression"); writer.value(_expression); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value(getBriefDescription(null)); + writer.key("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.key("expression"); writer.value(_expression); + writer.endObject(); + } - protected String getBriefDescription(Project project) { - return "Text transform on cells in column " + _columnName + " using expression " + _expression; - } + protected String getBriefDescription(Project project) { + return "Text transform on cells in column " + _columnName + " using expression " + _expression; + } - protected String createDescription(Column column, - List cellChanges) { - - return "Text transform on " + cellChanges.size() + - " cells in column " + column.getHeaderLabel() + ": " + _expression; - } + protected String createDescription(Column column, + List cellChanges) { + + return "Text transform on " + cellChanges.size() + + " cells in column " + column.getHeaderLabel() + ": " + _expression; + } - protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { - Column column = project.columnModel.getColumnByName(_columnName); - - Evaluable eval = MetaParser.parse(_expression); + protected RowVisitor createRowVisitor(Project project, List cellChanges) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + Evaluable eval = MetaParser.parse(_expression); Properties bindings = ExpressionUtils.createBindings(project); - return new RowVisitor() { - int cellIndex; - Properties bindings; - List cellChanges; - Evaluable eval; - - public RowVisitor init(int cellIndex, Properties bindings, List cellChanges, Evaluable eval) { - this.cellIndex = cellIndex; - this.bindings = bindings; - this.cellChanges = cellChanges; - this.eval = eval; - return this; - } - - public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { - Cell cell = row.getCell(cellIndex); + return new RowVisitor() { + int cellIndex; + Properties bindings; + List cellChanges; + Evaluable eval; + + public RowVisitor init(int cellIndex, Properties bindings, List cellChanges, Evaluable eval) { + this.cellIndex = cellIndex; + this.bindings = bindings; + this.cellChanges = cellChanges; + this.eval = eval; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { + Cell cell = row.getCell(cellIndex); ExpressionUtils.bind(bindings, row, rowIndex, cell); - + Object v = eval.evaluate(bindings); if ((cell != null && cell.value != null) || v != null) { Cell newCell = new Cell(v, (cell != null) ? cell.recon : null); - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); } - return false; - } - }.init(column.getCellIndex(), bindings, cellChanges, eval); - } + return false; + } + }.init(column.getCellIndex(), bindings, cellChanges, eval); + } } diff --git a/src/main/java/com/metaweb/gridworks/process/LongRunningProcess.java b/src/main/java/com/metaweb/gridworks/process/LongRunningProcess.java index 0b730cafd..5a1eb3f01 100644 --- a/src/main/java/com/metaweb/gridworks/process/LongRunningProcess.java +++ b/src/main/java/com/metaweb/gridworks/process/LongRunningProcess.java @@ -6,63 +6,63 @@ import org.json.JSONException; import org.json.JSONWriter; abstract public class LongRunningProcess extends Process { - final protected String _description; - protected ProcessManager _manager; - protected Thread _thread; - protected int _progress; // out of 100 - protected boolean _canceled; - - protected LongRunningProcess(String description) { - _description = description; - } + final protected String _description; + protected ProcessManager _manager; + protected Thread _thread; + protected int _progress; // out of 100 + protected boolean _canceled; + + protected LongRunningProcess(String description) { + _description = description; + } - public void cancel() { - _canceled = true; - if (_thread != null && _thread.isAlive()) { - _thread.interrupt(); - } - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value(_description); - writer.key("immediate"); writer.value(false); - writer.key("status"); writer.value(_thread == null ? "pending" : (_thread.isAlive() ? "running" : "done")); - writer.key("progress"); writer.value(_progress); - writer.endObject(); - } + public void cancel() { + _canceled = true; + if (_thread != null && _thread.isAlive()) { + _thread.interrupt(); + } + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value(_description); + writer.key("immediate"); writer.value(false); + writer.key("status"); writer.value(_thread == null ? "pending" : (_thread.isAlive() ? "running" : "done")); + writer.key("progress"); writer.value(_progress); + writer.endObject(); + } - @Override - public boolean isImmediate() { - return false; - } - - @Override - public boolean isRunning() { - return _thread != null && _thread.isAlive(); - } - - @Override - public boolean isDone() { - return _thread != null && !_thread.isAlive(); - } + @Override + public boolean isImmediate() { + return false; + } + + @Override + public boolean isRunning() { + return _thread != null && _thread.isAlive(); + } + + @Override + public boolean isDone() { + return _thread != null && !_thread.isAlive(); + } - @Override - public void performImmediate() { - throw new RuntimeException("Not an immediate process"); - } + @Override + public void performImmediate() { + throw new RuntimeException("Not an immediate process"); + } - @Override - public void startPerforming(ProcessManager manager) { - if (_thread == null) { - _manager = manager; - - _thread = new Thread(getRunnable()); - _thread.start(); - } - } - - abstract protected Runnable getRunnable(); + @Override + public void startPerforming(ProcessManager manager) { + if (_thread == null) { + _manager = manager; + + _thread = new Thread(getRunnable()); + _thread.start(); + } + } + + abstract protected Runnable getRunnable(); } diff --git a/src/main/java/com/metaweb/gridworks/process/Process.java b/src/main/java/com/metaweb/gridworks/process/Process.java index f275702b4..514abccbc 100644 --- a/src/main/java/com/metaweb/gridworks/process/Process.java +++ b/src/main/java/com/metaweb/gridworks/process/Process.java @@ -3,13 +3,13 @@ package com.metaweb.gridworks.process; import com.metaweb.gridworks.Jsonizable; public abstract class Process implements Jsonizable { - abstract public boolean isImmediate(); - - abstract public boolean isRunning(); - abstract public boolean isDone(); - - abstract public void performImmediate() throws Exception; - - abstract public void startPerforming(ProcessManager manager); - abstract public void cancel(); + abstract public boolean isImmediate(); + + abstract public boolean isRunning(); + abstract public boolean isDone(); + + abstract public void performImmediate() throws Exception; + + abstract public void startPerforming(ProcessManager manager); + abstract public void cancel(); } diff --git a/src/main/java/com/metaweb/gridworks/process/ProcessManager.java b/src/main/java/com/metaweb/gridworks/process/ProcessManager.java index e090601eb..84d3efe4c 100644 --- a/src/main/java/com/metaweb/gridworks/process/ProcessManager.java +++ b/src/main/java/com/metaweb/gridworks/process/ProcessManager.java @@ -10,80 +10,80 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class ProcessManager implements Jsonizable { - protected List _processes = new LinkedList(); - - public ProcessManager() { - - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("processes"); writer.array(); - for (Process p : _processes) { - p.write(writer, options); - } - writer.endArray(); - - writer.endObject(); - } + protected List _processes = new LinkedList(); + + public ProcessManager() { + + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("processes"); writer.array(); + for (Process p : _processes) { + p.write(writer, options); + } + writer.endArray(); + + writer.endObject(); + } - public boolean queueProcess(Process process) { - if (process.isImmediate() && _processes.size() == 0) { - try { - process.performImmediate(); - } catch (Exception e) { - // TODO: Not sure what to do yet - e.printStackTrace(); - } - return true; - } else { - _processes.add(process); - - update(); - - return false; - } - } - - public boolean hasPending() { - return _processes.size() > 0; - } - - public void onDoneProcess(Process p) { - _processes.remove(p); - update(); - } - - public void cancelAll() { - for (Process p : _processes) { - if (!p.isImmediate() && p.isRunning()) { - p.cancel(); - } - } - _processes.clear(); - } - - protected void update() { - while (_processes.size() > 0) { - Process p = _processes.get(0); - if (p.isImmediate()) { - try { - p.performImmediate(); - } catch (Exception e) { - // TODO: Not sure what to do yet - e.printStackTrace(); - } - _processes.remove(0); - } else if (p.isDone()) { - _processes.remove(0); - } else { - if (!p.isRunning()) { - p.startPerforming(this); - } - break; - } - } - } + public boolean queueProcess(Process process) { + if (process.isImmediate() && _processes.size() == 0) { + try { + process.performImmediate(); + } catch (Exception e) { + // TODO: Not sure what to do yet + e.printStackTrace(); + } + return true; + } else { + _processes.add(process); + + update(); + + return false; + } + } + + public boolean hasPending() { + return _processes.size() > 0; + } + + public void onDoneProcess(Process p) { + _processes.remove(p); + update(); + } + + public void cancelAll() { + for (Process p : _processes) { + if (!p.isImmediate() && p.isRunning()) { + p.cancel(); + } + } + _processes.clear(); + } + + protected void update() { + while (_processes.size() > 0) { + Process p = _processes.get(0); + if (p.isImmediate()) { + try { + p.performImmediate(); + } catch (Exception e) { + // TODO: Not sure what to do yet + e.printStackTrace(); + } + _processes.remove(0); + } else if (p.isDone()) { + _processes.remove(0); + } else { + if (!p.isRunning()) { + p.startPerforming(this); + } + break; + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/process/QuickHistoryEntryProcess.java b/src/main/java/com/metaweb/gridworks/process/QuickHistoryEntryProcess.java index bb13d8161..712e4831e 100644 --- a/src/main/java/com/metaweb/gridworks/process/QuickHistoryEntryProcess.java +++ b/src/main/java/com/metaweb/gridworks/process/QuickHistoryEntryProcess.java @@ -9,55 +9,55 @@ import com.metaweb.gridworks.history.HistoryEntry; import com.metaweb.gridworks.model.Project; abstract public class QuickHistoryEntryProcess extends Process { - final protected Project _project; - final protected String _briefDescription; - protected HistoryEntry _historyEntry; - boolean _done = false; - - public QuickHistoryEntryProcess(Project project, String briefDescription) { - _project = project; - _briefDescription = briefDescription; - } - - public void cancel() { - throw new RuntimeException("Not a long-running process"); - } + final protected Project _project; + final protected String _briefDescription; + protected HistoryEntry _historyEntry; + boolean _done = false; + + public QuickHistoryEntryProcess(Project project, String briefDescription) { + _project = project; + _briefDescription = briefDescription; + } + + public void cancel() { + throw new RuntimeException("Not a long-running process"); + } - public boolean isImmediate() { - return true; - } - - public boolean isRunning() { - throw new RuntimeException("Not a long-running process"); - } + public boolean isImmediate() { + return true; + } + + public boolean isRunning() { + throw new RuntimeException("Not a long-running process"); + } - public void performImmediate() throws Exception { - if (_historyEntry == null) { - _historyEntry = createHistoryEntry(); - } - _project.history.addEntry(_historyEntry); - _done = true; - } + public void performImmediate() throws Exception { + if (_historyEntry == null) { + _historyEntry = createHistoryEntry(); + } + _project.history.addEntry(_historyEntry); + _done = true; + } - public void startPerforming(ProcessManager manager) { - throw new RuntimeException("Not a long-running process"); - } + public void startPerforming(ProcessManager manager) { + throw new RuntimeException("Not a long-running process"); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("description"); writer.value(_historyEntry != null ? _historyEntry.description : _briefDescription); - writer.key("immediate"); writer.value(true); - writer.key("status"); writer.value(_done ? "done" : "pending"); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("description"); writer.value(_historyEntry != null ? _historyEntry.description : _briefDescription); + writer.key("immediate"); writer.value(true); + writer.key("status"); writer.value(_done ? "done" : "pending"); + writer.endObject(); + } - @Override - public boolean isDone() { - return _done; - } - - abstract protected HistoryEntry createHistoryEntry() throws Exception; + @Override + public boolean isDone() { + return _done; + } + + abstract protected HistoryEntry createHistoryEntry() throws Exception; } diff --git a/src/main/java/com/metaweb/gridworks/protograph/AnonymousNode.java b/src/main/java/com/metaweb/gridworks/protograph/AnonymousNode.java index 66e9f5a1d..3977d96c0 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/AnonymousNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/AnonymousNode.java @@ -8,40 +8,40 @@ import org.json.JSONException; import org.json.JSONWriter; public class AnonymousNode implements Node, NodeWithLinks { - private static final long serialVersionUID = -6956243664838720646L; - - final public FreebaseType type; - final public List links = new LinkedList(); - - public AnonymousNode(FreebaseType type) { - this.type = type; - } + private static final long serialVersionUID = -6956243664838720646L; + + final public FreebaseType type; + final public List links = new LinkedList(); + + public AnonymousNode(FreebaseType type) { + this.type = type; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("nodeType"); writer.value("anonymous"); - writer.key("type"); type.write(writer, options); - if (links != null) { - writer.key("links"); writer.array(); - for (Link link : links) { - link.write(writer, options); - } - writer.endArray(); - } - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("nodeType"); writer.value("anonymous"); + writer.key("type"); type.write(writer, options); + if (links != null) { + writer.key("links"); writer.array(); + for (Link link : links) { + link.write(writer, options); + } + writer.endArray(); + } + writer.endObject(); + } - public void addLink(Link link) { - links.add(link); - } - - public Link getLink(int index) { - return links.get(index); - } + public void addLink(Link link) { + links.add(link); + } + + public Link getLink(int index) { + return links.get(index); + } - public int getLinkCount() { - return links.size(); - } + public int getLinkCount() { + return links.size(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/CellKeyNode.java b/src/main/java/com/metaweb/gridworks/protograph/CellKeyNode.java index 2d77a19b3..1ac1973e0 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/CellKeyNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/CellKeyNode.java @@ -6,26 +6,26 @@ import org.json.JSONException; import org.json.JSONWriter; public class CellKeyNode extends CellNode { - private static final long serialVersionUID = 1684854896739592911L; - - final public FreebaseTopic namespace; + private static final long serialVersionUID = 1684854896739592911L; + + final public FreebaseTopic namespace; - public CellKeyNode( - String columnName, - FreebaseTopic namespace - ) { - super(columnName); - - this.namespace = namespace; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { + public CellKeyNode( + String columnName, + FreebaseTopic namespace + ) { + super(columnName); + + this.namespace = namespace; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { - writer.object(); - writer.key("nodeType"); writer.value("cell-as-key"); - writer.key("columnName"); writer.value(columnName); - writer.key("namespace"); namespace.write(writer, options); - writer.endObject(); - } + writer.object(); + writer.key("nodeType"); writer.value("cell-as-key"); + writer.key("columnName"); writer.value(columnName); + writer.key("namespace"); namespace.write(writer, options); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/CellNode.java b/src/main/java/com/metaweb/gridworks/protograph/CellNode.java index da076828b..ebe5517e7 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/CellNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/CellNode.java @@ -1,13 +1,13 @@ package com.metaweb.gridworks.protograph; abstract public class CellNode implements Node { - private static final long serialVersionUID = 5820786756175547307L; + private static final long serialVersionUID = 5820786756175547307L; - final public String columnName; - - public CellNode( - String columnName - ) { - this.columnName = columnName; - } + final public String columnName; + + public CellNode( + String columnName + ) { + this.columnName = columnName; + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/CellTopicNode.java b/src/main/java/com/metaweb/gridworks/protograph/CellTopicNode.java index 790f6ffda..b3803561c 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/CellTopicNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/CellTopicNode.java @@ -8,53 +8,53 @@ import org.json.JSONException; import org.json.JSONWriter; public class CellTopicNode extends CellNode implements NodeWithLinks { - private static final long serialVersionUID = 1684854896739592911L; - - final public boolean createForNoReconMatch; - final public FreebaseType type; - final public List links = new LinkedList(); + private static final long serialVersionUID = 1684854896739592911L; + + final public boolean createForNoReconMatch; + final public FreebaseType type; + final public List links = new LinkedList(); - public CellTopicNode( - String columnName, - boolean createForNoReconMatch, - FreebaseType type - ) { - super(columnName); - - this.createForNoReconMatch = createForNoReconMatch; - this.type = type; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { + public CellTopicNode( + String columnName, + boolean createForNoReconMatch, + FreebaseType type + ) { + super(columnName); + + this.createForNoReconMatch = createForNoReconMatch; + this.type = type; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { - writer.object(); - writer.key("nodeType"); writer.value("cell-as-topic"); - writer.key("columnName"); writer.value(columnName); - writer.key("createForNoReconMatch"); writer.value(createForNoReconMatch); - if (createForNoReconMatch && type != null) { - writer.key("type"); type.write(writer, options); - } - if (links != null) { - writer.key("links"); writer.array(); - for (Link link : links) { - link.write(writer, options); - } - writer.endArray(); - } - - writer.endObject(); - } + writer.object(); + writer.key("nodeType"); writer.value("cell-as-topic"); + writer.key("columnName"); writer.value(columnName); + writer.key("createForNoReconMatch"); writer.value(createForNoReconMatch); + if (createForNoReconMatch && type != null) { + writer.key("type"); type.write(writer, options); + } + if (links != null) { + writer.key("links"); writer.array(); + for (Link link : links) { + link.write(writer, options); + } + writer.endArray(); + } + + writer.endObject(); + } - public void addLink(Link link) { - links.add(link); - } + public void addLink(Link link) { + links.add(link); + } - public Link getLink(int index) { - return links.get(index); - } + public Link getLink(int index) { + return links.get(index); + } - public int getLinkCount() { - return links.size(); - } + public int getLinkCount() { + return links.size(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/CellValueNode.java b/src/main/java/com/metaweb/gridworks/protograph/CellValueNode.java index 80c0237fe..de937478d 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/CellValueNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/CellValueNode.java @@ -6,31 +6,31 @@ import org.json.JSONException; import org.json.JSONWriter; public class CellValueNode extends CellNode { - private static final long serialVersionUID = 7311884925532708576L; - - final public String valueType; - final public String lang; + private static final long serialVersionUID = 7311884925532708576L; + + final public String valueType; + final public String lang; - public CellValueNode( - String columnName, - String valueType, - String lang - ) { - super(columnName); - - this.valueType = valueType; - this.lang = lang; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { + public CellValueNode( + String columnName, + String valueType, + String lang + ) { + super(columnName); + + this.valueType = valueType; + this.lang = lang; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { - writer.object(); - writer.key("nodeType"); writer.value("cell-as-value"); - writer.key("columnName"); writer.value(columnName); - writer.key("valueType"); writer.value(valueType); - writer.key("lang"); writer.value(lang); - writer.endObject(); - } + writer.object(); + writer.key("nodeType"); writer.value("cell-as-value"); + writer.key("columnName"); writer.value(columnName); + writer.key("valueType"); writer.value(valueType); + writer.key("lang"); writer.value(lang); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/FreebaseProperty.java b/src/main/java/com/metaweb/gridworks/protograph/FreebaseProperty.java index 59691a141..23ddc6c53 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/FreebaseProperty.java +++ b/src/main/java/com/metaweb/gridworks/protograph/FreebaseProperty.java @@ -1,11 +1,11 @@ package com.metaweb.gridworks.protograph; public class FreebaseProperty extends FreebaseTopic { - private static final long serialVersionUID = 7909539492956342421L; - - //final protected FreebaseType _expectedType; - - public FreebaseProperty(String id, String name) { - super(id, name); - } + private static final long serialVersionUID = 7909539492956342421L; + + //final protected FreebaseType _expectedType; + + public FreebaseProperty(String id, String name) { + super(id, name); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopic.java b/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopic.java index 9a01eaacb..e78d7d397 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopic.java +++ b/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopic.java @@ -9,23 +9,23 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class FreebaseTopic implements Serializable, Jsonizable { - private static final long serialVersionUID = -3427885694129112432L; - - final public String id; - final public String name; - - public FreebaseTopic(String id, String name) { - this.id = id; - this.name = name; - } - - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("id"); writer.value(id); - writer.key("name"); writer.value(name); - writer.endObject(); - } + private static final long serialVersionUID = -3427885694129112432L; + + final public String id; + final public String name; + + public FreebaseTopic(String id, String name) { + this.id = id; + this.name = name; + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("id"); writer.value(id); + writer.key("name"); writer.value(name); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopicNode.java b/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopicNode.java index a477ebd8b..a771e071b 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopicNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/FreebaseTopicNode.java @@ -8,41 +8,41 @@ import org.json.JSONException; import org.json.JSONWriter; public class FreebaseTopicNode implements Node, NodeWithLinks { - private static final long serialVersionUID = 8418548867745587387L; + private static final long serialVersionUID = 8418548867745587387L; - final public FreebaseTopic topic; - final public List links = new LinkedList(); - - public FreebaseTopicNode(FreebaseTopic topic) { - this.topic = topic; - } + final public FreebaseTopic topic; + final public List links = new LinkedList(); + + public FreebaseTopicNode(FreebaseTopic topic) { + this.topic = topic; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("nodeType"); writer.value("topic"); - writer.key("topic"); topic.write(writer, options); - if (links != null) { - writer.key("links"); writer.array(); - for (Link link : links) { - link.write(writer, options); - } - writer.endArray(); - } - - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("nodeType"); writer.value("topic"); + writer.key("topic"); topic.write(writer, options); + if (links != null) { + writer.key("links"); writer.array(); + for (Link link : links) { + link.write(writer, options); + } + writer.endArray(); + } + + writer.endObject(); + } - public void addLink(Link link) { - links.add(link); - } - - public Link getLink(int index) { - return links.get(index); - } + public void addLink(Link link) { + links.add(link); + } + + public Link getLink(int index) { + return links.get(index); + } - public int getLinkCount() { - return links.size(); - } + public int getLinkCount() { + return links.size(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/FreebaseType.java b/src/main/java/com/metaweb/gridworks/protograph/FreebaseType.java index d3db01012..d3bf4b1fc 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/FreebaseType.java +++ b/src/main/java/com/metaweb/gridworks/protograph/FreebaseType.java @@ -1,10 +1,10 @@ package com.metaweb.gridworks.protograph; public class FreebaseType extends FreebaseTopic { - private static final long serialVersionUID = -3070300264980791404L; - - public FreebaseType(String id, String name) { - super(id, name); - } + private static final long serialVersionUID = -3070300264980791404L; + + public FreebaseType(String id, String name) { + super(id, name); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/Link.java b/src/main/java/com/metaweb/gridworks/protograph/Link.java index 3c17600b3..e3ec88168 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/Link.java +++ b/src/main/java/com/metaweb/gridworks/protograph/Link.java @@ -9,31 +9,31 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class Link implements Serializable, Jsonizable { - private static final long serialVersionUID = 2908086768260322876L; - - final public FreebaseProperty property; - final public Node target; - - public Link(FreebaseProperty property, Node target) { - this.property = property; - this.target = target; - } - - public FreebaseProperty getProperty() { - return property; - } - - public Node getTarget() { - return target; - } + private static final long serialVersionUID = 2908086768260322876L; + + final public FreebaseProperty property; + final public Node target; + + public Link(FreebaseProperty property, Node target) { + this.property = property; + this.target = target; + } + + public FreebaseProperty getProperty() { + return property; + } + + public Node getTarget() { + return target; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { + public void write(JSONWriter writer, Properties options) + throws JSONException { - writer.object(); - writer.key("property"); property.write(writer, options); - writer.key("target"); target.write(writer, options); - writer.endObject(); - } + writer.object(); + writer.key("property"); property.write(writer, options); + writer.key("target"); target.write(writer, options); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/NodeWithLinks.java b/src/main/java/com/metaweb/gridworks/protograph/NodeWithLinks.java index bc7d2b99f..a13dfc867 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/NodeWithLinks.java +++ b/src/main/java/com/metaweb/gridworks/protograph/NodeWithLinks.java @@ -1,9 +1,9 @@ package com.metaweb.gridworks.protograph; public interface NodeWithLinks { - public void addLink(Link link); - - public int getLinkCount(); - - public Link getLink(int index); + public void addLink(Link link); + + public int getLinkCount(); + + public Link getLink(int index); } diff --git a/src/main/java/com/metaweb/gridworks/protograph/Protograph.java b/src/main/java/com/metaweb/gridworks/protograph/Protograph.java index 9c553a26e..4ec6439df 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/Protograph.java +++ b/src/main/java/com/metaweb/gridworks/protograph/Protograph.java @@ -13,128 +13,128 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; public class Protograph implements Serializable, Jsonizable { - private static final long serialVersionUID = 706700643851582450L; - - final protected List _rootNodes = new LinkedList(); - - public int getRootNodeCount() { - return _rootNodes.size(); - } - - public Node getRootNode(int index) { - return _rootNodes.get(index); - } - - static public Protograph reconstruct(JSONObject o) throws JSONException { - Protograph g = new Protograph(); - - JSONArray rootNodes = o.getJSONArray("rootNodes"); - int count = rootNodes.length(); - - for (int i = 0; i < count; i++) { - JSONObject o2 = rootNodes.getJSONObject(i); - Node node = reconstructNode(o2); - if (node != null) { - g._rootNodes.add(node); - } - } - - return g; - } - - static protected Node reconstructNode(JSONObject o) throws JSONException { - Node node = null; - - String nodeType = o.getString("nodeType"); - if (nodeType.startsWith("cell-as-")) { - String columnName = o.getString("columnName"); - - if ("cell-as-topic".equals(nodeType)) { - if (o.has("type")) { - node = new CellTopicNode( - columnName, - o.getBoolean("createForNoReconMatch"), - reconstructType(o.getJSONObject("type")) - ); - } - } else if ("cell-as-value".equals(nodeType)) { - node = new CellValueNode( - columnName, - o.getString("valueType"), - o.getString("lang") - ); - } else if ("cell-as-key".equals(nodeType)) { - node = new CellKeyNode( - columnName, - reconstructTopic(o.getJSONObject("namespace")) - ); - } - } else if ("topic".equals(nodeType)) { - node = new FreebaseTopicNode(reconstructTopic(o.getJSONObject("topic"))); - } else if ("value".equals(nodeType)) { - node = new ValueNode( - o.get("value"), - o.getString("valueType"), - o.getString("lang") - ); - } else if ("anonymous".equals(nodeType)) { - node = new AnonymousNode(reconstructType(o.getJSONObject("type"))); - } - - if (node != null) { - if (node instanceof NodeWithLinks && o.has("links")) { - NodeWithLinks node2 = (NodeWithLinks) node; - - JSONArray links = o.getJSONArray("links"); - int linkCount = links.length(); - - for (int j = 0; j < linkCount; j++) { - JSONObject oLink = links.getJSONObject(j); - - node2.addLink(new Link( - reconstructProperty(oLink.getJSONObject("property")), - reconstructNode(oLink.getJSONObject("target")) - )); - } - } - } - - return node; - } - - static protected FreebaseProperty reconstructProperty(JSONObject o) throws JSONException { - return new FreebaseProperty( - o.getString("id"), - o.getString("name") - ); - } - - static protected FreebaseType reconstructType(JSONObject o) throws JSONException { - return new FreebaseType( - o.getString("id"), - o.getString("name") - ); - } - - static protected FreebaseTopic reconstructTopic(JSONObject o) throws JSONException { - return new FreebaseTopic( - o.getString("id"), - o.getString("name") - ); - } + private static final long serialVersionUID = 706700643851582450L; + + final protected List _rootNodes = new LinkedList(); + + public int getRootNodeCount() { + return _rootNodes.size(); + } + + public Node getRootNode(int index) { + return _rootNodes.get(index); + } + + static public Protograph reconstruct(JSONObject o) throws JSONException { + Protograph g = new Protograph(); + + JSONArray rootNodes = o.getJSONArray("rootNodes"); + int count = rootNodes.length(); + + for (int i = 0; i < count; i++) { + JSONObject o2 = rootNodes.getJSONObject(i); + Node node = reconstructNode(o2); + if (node != null) { + g._rootNodes.add(node); + } + } + + return g; + } + + static protected Node reconstructNode(JSONObject o) throws JSONException { + Node node = null; + + String nodeType = o.getString("nodeType"); + if (nodeType.startsWith("cell-as-")) { + String columnName = o.getString("columnName"); + + if ("cell-as-topic".equals(nodeType)) { + if (o.has("type")) { + node = new CellTopicNode( + columnName, + o.getBoolean("createForNoReconMatch"), + reconstructType(o.getJSONObject("type")) + ); + } + } else if ("cell-as-value".equals(nodeType)) { + node = new CellValueNode( + columnName, + o.getString("valueType"), + o.getString("lang") + ); + } else if ("cell-as-key".equals(nodeType)) { + node = new CellKeyNode( + columnName, + reconstructTopic(o.getJSONObject("namespace")) + ); + } + } else if ("topic".equals(nodeType)) { + node = new FreebaseTopicNode(reconstructTopic(o.getJSONObject("topic"))); + } else if ("value".equals(nodeType)) { + node = new ValueNode( + o.get("value"), + o.getString("valueType"), + o.getString("lang") + ); + } else if ("anonymous".equals(nodeType)) { + node = new AnonymousNode(reconstructType(o.getJSONObject("type"))); + } + + if (node != null) { + if (node instanceof NodeWithLinks && o.has("links")) { + NodeWithLinks node2 = (NodeWithLinks) node; + + JSONArray links = o.getJSONArray("links"); + int linkCount = links.length(); + + for (int j = 0; j < linkCount; j++) { + JSONObject oLink = links.getJSONObject(j); + + node2.addLink(new Link( + reconstructProperty(oLink.getJSONObject("property")), + reconstructNode(oLink.getJSONObject("target")) + )); + } + } + } + + return node; + } + + static protected FreebaseProperty reconstructProperty(JSONObject o) throws JSONException { + return new FreebaseProperty( + o.getString("id"), + o.getString("name") + ); + } + + static protected FreebaseType reconstructType(JSONObject o) throws JSONException { + return new FreebaseType( + o.getString("id"), + o.getString("name") + ); + } + + static protected FreebaseTopic reconstructTopic(JSONObject o) throws JSONException { + return new FreebaseTopic( + o.getString("id"), + o.getString("name") + ); + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("rootNodes"); writer.array(); - - for (Node node : _rootNodes) { - node.write(writer, options); - } - - writer.endArray(); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("rootNodes"); writer.array(); + + for (Node node : _rootNodes) { + node.write(writer, options); + } + + writer.endArray(); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/ValueNode.java b/src/main/java/com/metaweb/gridworks/protograph/ValueNode.java index 98d42aa30..2fe3b7c52 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/ValueNode.java +++ b/src/main/java/com/metaweb/gridworks/protograph/ValueNode.java @@ -6,26 +6,26 @@ import org.json.JSONException; import org.json.JSONWriter; public class ValueNode implements Node { - private static final long serialVersionUID = -5626883493437735688L; - - final public Object value; - final public String valueType; - final public String lang; - - public ValueNode(Object value, String valueType, String lang) { - this.value = value; - this.valueType = valueType; - this.lang = lang; - } + private static final long serialVersionUID = -5626883493437735688L; + + final public Object value; + final public String valueType; + final public String lang; + + public ValueNode(Object value, String valueType, String lang) { + this.value = value; + this.valueType = valueType; + this.lang = lang; + } - public void write(JSONWriter writer, Properties options) - throws JSONException { - - writer.object(); - writer.key("nodeType"); writer.value("value"); - writer.key("value"); writer.value(value); - writer.key("valueType"); writer.value(valueType); - writer.key("lang"); writer.value(lang); - writer.endObject(); - } + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("nodeType"); writer.value("value"); + writer.key("value"); writer.value(value); + writer.key("valueType"); writer.value(valueType); + writer.key("lang"); writer.value(lang); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/transpose/MqlreadLikeTransposedNodeFactory.java b/src/main/java/com/metaweb/gridworks/protograph/transpose/MqlreadLikeTransposedNodeFactory.java index 81abd148c..6138d3877 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/transpose/MqlreadLikeTransposedNodeFactory.java +++ b/src/main/java/com/metaweb/gridworks/protograph/transpose/MqlreadLikeTransposedNodeFactory.java @@ -19,290 +19,290 @@ import com.metaweb.gridworks.protograph.FreebaseTopicNode; import com.metaweb.gridworks.protograph.ValueNode; public class MqlreadLikeTransposedNodeFactory implements TransposedNodeFactory { - protected List rootObjects = new LinkedList(); - - public JSONArray getJSON() { - return new JSONArray(rootObjects); - } - - abstract protected class JsonTransposedNode implements TransposedNode { - abstract public Object getJSON(); - } - - abstract protected class JsonObjectTransposedNode extends JsonTransposedNode { - abstract public JSONObject getJSONObject(); - - protected JSONObject obj; - - public Object getJSON() { - return getJSONObject(); - } - } - - protected class AnonymousTransposedNode extends JsonObjectTransposedNode { - JsonObjectTransposedNode parent; - FreebaseProperty property; - AnonymousNode node; - - protected AnonymousTransposedNode( - JsonObjectTransposedNode parent, - FreebaseProperty property, - AnonymousNode node - ) { - this.parent = parent; - this.property = property; - this.node = node; - } + protected List rootObjects = new LinkedList(); + + public JSONArray getJSON() { + return new JSONArray(rootObjects); + } + + abstract protected class JsonTransposedNode implements TransposedNode { + abstract public Object getJSON(); + } + + abstract protected class JsonObjectTransposedNode extends JsonTransposedNode { + abstract public JSONObject getJSONObject(); + + protected JSONObject obj; + + public Object getJSON() { + return getJSONObject(); + } + } + + protected class AnonymousTransposedNode extends JsonObjectTransposedNode { + JsonObjectTransposedNode parent; + FreebaseProperty property; + AnonymousNode node; + + protected AnonymousTransposedNode( + JsonObjectTransposedNode parent, + FreebaseProperty property, + AnonymousNode node + ) { + this.parent = parent; + this.property = property; + this.node = node; + } - public JSONObject getJSONObject() { - if (obj == null) { - obj = new JSONObject(); - try { - obj.put("type", this.node.type.id); - obj.put("id", (String) null); - obj.put("create", "unconditional"); - } catch (JSONException e) { - e.printStackTrace(); - } - - linkTransposedNodeJSON(obj, parent, property); - } - - return obj; - } - } - - protected class CellTopicTransposedNode extends JsonObjectTransposedNode { - protected CellTopicNode node; - protected Cell cell; - - public CellTopicTransposedNode(CellTopicNode node, Cell cell) { - this.node = node; - this.cell = cell; - } + public JSONObject getJSONObject() { + if (obj == null) { + obj = new JSONObject(); + try { + obj.put("type", this.node.type.id); + obj.put("id", (String) null); + obj.put("create", "unconditional"); + } catch (JSONException e) { + e.printStackTrace(); + } + + linkTransposedNodeJSON(obj, parent, property); + } + + return obj; + } + } + + protected class CellTopicTransposedNode extends JsonObjectTransposedNode { + protected CellTopicNode node; + protected Cell cell; + + public CellTopicTransposedNode(CellTopicNode node, Cell cell) { + this.node = node; + this.cell = cell; + } - @Override - public JSONObject getJSONObject() { - if (obj == null) { - obj = new JSONObject(); - try { - if (cell.recon != null && - cell.recon.judgment == Recon.Judgment.Matched && - cell.recon.match != null) { - obj.put("id", cell.recon.match.topicID); - } else { - obj.put("id", (String) null); - obj.put("name", cell.value.toString()); - obj.put("type", node.type.id); - obj.put("create", "unless_exists"); - } - } catch (JSONException e) { - e.printStackTrace(); - } - } - return obj; - } - } - - protected class CellValueTransposedNode extends JsonTransposedNode { - protected JSONObject obj; - protected CellValueNode node; - protected Cell cell; - - public CellValueTransposedNode(CellValueNode node, Cell cell) { - this.node = node; - this.cell = cell; - } + @Override + public JSONObject getJSONObject() { + if (obj == null) { + obj = new JSONObject(); + try { + if (cell.recon != null && + cell.recon.judgment == Recon.Judgment.Matched && + cell.recon.match != null) { + obj.put("id", cell.recon.match.topicID); + } else { + obj.put("id", (String) null); + obj.put("name", cell.value.toString()); + obj.put("type", node.type.id); + obj.put("create", "unless_exists"); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + return obj; + } + } + + protected class CellValueTransposedNode extends JsonTransposedNode { + protected JSONObject obj; + protected CellValueNode node; + protected Cell cell; + + public CellValueTransposedNode(CellValueNode node, Cell cell) { + this.node = node; + this.cell = cell; + } - public Object getJSON() { - if (obj == null) { - obj = new JSONObject(); - try { - obj.put("value", cell.value.toString()); - obj.put("type", node.valueType); - if ("/type/text".equals(node.lang)) { - obj.put("lang", node.lang); - } - - obj.put("connect", "insert"); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return obj; - } - } - - protected class CellKeyTransposedNode extends JsonTransposedNode { - protected JSONObject obj; - protected CellKeyNode node; - protected Cell cell; - - public CellKeyTransposedNode(CellKeyNode node, Cell cell) { - this.node = node; - this.cell = cell; - } + public Object getJSON() { + if (obj == null) { + obj = new JSONObject(); + try { + obj.put("value", cell.value.toString()); + obj.put("type", node.valueType); + if ("/type/text".equals(node.lang)) { + obj.put("lang", node.lang); + } + + obj.put("connect", "insert"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return obj; + } + } + + protected class CellKeyTransposedNode extends JsonTransposedNode { + protected JSONObject obj; + protected CellKeyNode node; + protected Cell cell; + + public CellKeyTransposedNode(CellKeyNode node, Cell cell) { + this.node = node; + this.cell = cell; + } - public Object getJSON() { - if (obj == null) { - obj = new JSONObject(); - try { - obj.put("value", cell.value.toString()); - - JSONObject nsObj = new JSONObject(); - nsObj.put("id", node.namespace.id); - - obj.put("namespace", nsObj); - obj.put("connect", "insert"); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return obj; - } - } - - protected class TopicTransposedNode extends JsonObjectTransposedNode { - protected FreebaseTopicNode node; - - public TopicTransposedNode(FreebaseTopicNode node) { - this.node = node; - } + public Object getJSON() { + if (obj == null) { + obj = new JSONObject(); + try { + obj.put("value", cell.value.toString()); + + JSONObject nsObj = new JSONObject(); + nsObj.put("id", node.namespace.id); + + obj.put("namespace", nsObj); + obj.put("connect", "insert"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return obj; + } + } + + protected class TopicTransposedNode extends JsonObjectTransposedNode { + protected FreebaseTopicNode node; + + public TopicTransposedNode(FreebaseTopicNode node) { + this.node = node; + } - @Override - public JSONObject getJSONObject() { - if (obj == null) { - obj = new JSONObject(); - try { - obj.put("id", node.topic.id); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return obj; - } - } + @Override + public JSONObject getJSONObject() { + if (obj == null) { + obj = new JSONObject(); + try { + obj.put("id", node.topic.id); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return obj; + } + } - protected class ValueTransposedNode extends JsonTransposedNode { - protected JSONObject obj; - protected ValueNode node; - - public ValueTransposedNode(ValueNode node) { - this.node = node; - } + protected class ValueTransposedNode extends JsonTransposedNode { + protected JSONObject obj; + protected ValueNode node; + + public ValueTransposedNode(ValueNode node) { + this.node = node; + } - public Object getJSON() { - if (obj == null) { - obj = new JSONObject(); - try { - obj.put("value", node.value); - obj.put("type", node.valueType); - if ("/type/text".equals(node.lang)) { - obj.put("lang", node.lang); - } - - obj.put("connect", "insert"); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return obj; - } - } - public TransposedNode transposeAnonymousNode( - TransposedNode parentNode, - FreebaseProperty property, - AnonymousNode node) { - - return new AnonymousTransposedNode( - parentNode instanceof JsonObjectTransposedNode ? (JsonObjectTransposedNode) parentNode : null, - property, - node - ); - } + public Object getJSON() { + if (obj == null) { + obj = new JSONObject(); + try { + obj.put("value", node.value); + obj.put("type", node.valueType); + if ("/type/text".equals(node.lang)) { + obj.put("lang", node.lang); + } + + obj.put("connect", "insert"); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return obj; + } + } + public TransposedNode transposeAnonymousNode( + TransposedNode parentNode, + FreebaseProperty property, + AnonymousNode node) { + + return new AnonymousTransposedNode( + parentNode instanceof JsonObjectTransposedNode ? (JsonObjectTransposedNode) parentNode : null, + property, + node + ); + } - public TransposedNode transposeCellNode( - TransposedNode parentNode, - FreebaseProperty property, - CellNode node, - Cell cell) { - - JsonTransposedNode tnode = null; - if (node instanceof CellTopicNode) { - tnode = new CellTopicTransposedNode((CellTopicNode) node, cell); - } else if (node instanceof CellValueNode) { - tnode = new CellValueTransposedNode((CellValueNode) node, cell); - } else if (node instanceof CellKeyNode) { - tnode = new CellKeyTransposedNode((CellKeyNode) node, cell); - } - - if (tnode != null) { - processTransposedNode(tnode, parentNode, property); - } - return tnode; - } + public TransposedNode transposeCellNode( + TransposedNode parentNode, + FreebaseProperty property, + CellNode node, + Cell cell) { + + JsonTransposedNode tnode = null; + if (node instanceof CellTopicNode) { + tnode = new CellTopicTransposedNode((CellTopicNode) node, cell); + } else if (node instanceof CellValueNode) { + tnode = new CellValueTransposedNode((CellValueNode) node, cell); + } else if (node instanceof CellKeyNode) { + tnode = new CellKeyTransposedNode((CellKeyNode) node, cell); + } + + if (tnode != null) { + processTransposedNode(tnode, parentNode, property); + } + return tnode; + } - public TransposedNode transposeTopicNode( - TransposedNode parentNode, - FreebaseProperty property, - FreebaseTopicNode node) { - - JsonTransposedNode tnode = new TopicTransposedNode(node); - - processTransposedNode(tnode, parentNode, property); - - return tnode; - } + public TransposedNode transposeTopicNode( + TransposedNode parentNode, + FreebaseProperty property, + FreebaseTopicNode node) { + + JsonTransposedNode tnode = new TopicTransposedNode(node); + + processTransposedNode(tnode, parentNode, property); + + return tnode; + } - public TransposedNode transposeValueNode( - TransposedNode parentNode, - FreebaseProperty property, - ValueNode node) { - - JsonTransposedNode tnode = new ValueTransposedNode(node); - - processTransposedNode(tnode, parentNode, property); - - return tnode; - } - - protected void processTransposedNode( - JsonTransposedNode tnode, - TransposedNode parentNode, - FreebaseProperty property - ) { - - if (!(tnode instanceof AnonymousTransposedNode)) { - linkTransposedNodeJSON(tnode.getJSON(), parentNode, property); - } - } - - protected void linkTransposedNodeJSON( - Object obj, - TransposedNode parentNode, - FreebaseProperty property - ) { - - if (parentNode == null) { - if (obj instanceof JSONObject) { - rootObjects.add((JSONObject) obj); - } - } else if (parentNode instanceof JsonTransposedNode) { - JSONObject parentObj = ((JsonObjectTransposedNode) parentNode).getJSONObject(); - - try { - JSONArray a = null; - if (parentObj.has(property.id)) { - a = parentObj.getJSONArray(property.id); - } else { - a = new JSONArray(); - parentObj.put(property.id, a); - } - - a.put(a.length(), obj); - } catch (JSONException e) { - e.printStackTrace(); - } - } - } + public TransposedNode transposeValueNode( + TransposedNode parentNode, + FreebaseProperty property, + ValueNode node) { + + JsonTransposedNode tnode = new ValueTransposedNode(node); + + processTransposedNode(tnode, parentNode, property); + + return tnode; + } + + protected void processTransposedNode( + JsonTransposedNode tnode, + TransposedNode parentNode, + FreebaseProperty property + ) { + + if (!(tnode instanceof AnonymousTransposedNode)) { + linkTransposedNodeJSON(tnode.getJSON(), parentNode, property); + } + } + + protected void linkTransposedNodeJSON( + Object obj, + TransposedNode parentNode, + FreebaseProperty property + ) { + + if (parentNode == null) { + if (obj instanceof JSONObject) { + rootObjects.add((JSONObject) obj); + } + } else if (parentNode instanceof JsonTransposedNode) { + JSONObject parentObj = ((JsonObjectTransposedNode) parentNode).getJSONObject(); + + try { + JSONArray a = null; + if (parentObj.has(property.id)) { + a = parentObj.getJSONArray(property.id); + } else { + a = new JSONArray(); + parentObj.put(property.id, a); + } + + a.put(a.length(), obj); + } catch (JSONException e) { + e.printStackTrace(); + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/transpose/TransposedNodeFactory.java b/src/main/java/com/metaweb/gridworks/protograph/transpose/TransposedNodeFactory.java index 0a0ef6330..42ca343f6 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/transpose/TransposedNodeFactory.java +++ b/src/main/java/com/metaweb/gridworks/protograph/transpose/TransposedNodeFactory.java @@ -8,28 +8,28 @@ import com.metaweb.gridworks.protograph.FreebaseTopicNode; import com.metaweb.gridworks.protograph.ValueNode; public interface TransposedNodeFactory { - public TransposedNode transposeAnonymousNode( - TransposedNode parentNode, - FreebaseProperty property, - AnonymousNode node - ); - - public TransposedNode transposeCellNode( - TransposedNode parentNode, - FreebaseProperty property, - CellNode node, - Cell cell - ); - - public TransposedNode transposeValueNode( - TransposedNode parentNode, - FreebaseProperty property, - ValueNode node - ); - - public TransposedNode transposeTopicNode( - TransposedNode parentNode, - FreebaseProperty property, - FreebaseTopicNode node - ); + public TransposedNode transposeAnonymousNode( + TransposedNode parentNode, + FreebaseProperty property, + AnonymousNode node + ); + + public TransposedNode transposeCellNode( + TransposedNode parentNode, + FreebaseProperty property, + CellNode node, + Cell cell + ); + + public TransposedNode transposeValueNode( + TransposedNode parentNode, + FreebaseProperty property, + ValueNode node + ); + + public TransposedNode transposeTopicNode( + TransposedNode parentNode, + FreebaseProperty property, + FreebaseTopicNode node + ); } diff --git a/src/main/java/com/metaweb/gridworks/protograph/transpose/Transposer.java b/src/main/java/com/metaweb/gridworks/protograph/transpose/Transposer.java index df07f6bc7..ea19c1185 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/transpose/Transposer.java +++ b/src/main/java/com/metaweb/gridworks/protograph/transpose/Transposer.java @@ -21,141 +21,141 @@ import com.metaweb.gridworks.protograph.Protograph; import com.metaweb.gridworks.protograph.ValueNode; public class Transposer { - static public void transpose( - Project project, - Protograph protograph, - Node rootNode, - TransposedNodeFactory nodeFactory - ) { - Context rootContext = new Context(rootNode, null, null, 20); - - for (Row row : project.rows) { - descend(project, protograph, nodeFactory, row, rootNode, rootContext); - if (rootContext.limit > 0 && rootContext.count > rootContext.limit) { - break; - } - } - } - - static protected void descend( - Project project, - Protograph protograph, - TransposedNodeFactory nodeFactory, - Row row, - Node node, - Context context - ) { - TransposedNode tnode = null; - - TransposedNode parentNode = context.parent == null ? null : context.parent.transposedNode; - FreebaseProperty property = context.parent == null ? null : context.link.property; - - if (node instanceof CellNode) { - CellNode node2 = (CellNode) node; - Column column = project.columnModel.getColumnByName(node2.columnName); - Cell cell = row.getCell(column.getCellIndex()); - if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { - if (node2 instanceof CellTopicNode) { - if (!((CellTopicNode) node2).createForNoReconMatch && - (cell.recon == null || cell.recon.judgment == Judgment.None)) { - return; - } - } - - context.count++; - if (context.limit > 0 && context.count > context.limit) { - return; - } - - tnode = nodeFactory.transposeCellNode( - parentNode, - property, - node2, - cell - ); - } - } else { - if (node instanceof AnonymousNode) { - tnode = nodeFactory.transposeAnonymousNode( - parentNode, - property, - (AnonymousNode) node - ); - } else if (node instanceof FreebaseTopicNode) { - tnode = nodeFactory.transposeTopicNode( - parentNode, - property, - (FreebaseTopicNode) node - ); - } else if (node instanceof ValueNode) { - tnode = nodeFactory.transposeValueNode( - parentNode, - property, - (ValueNode) node - ); - } - } - - if (tnode != null) { - context.transposedNode = tnode; - context.nullifySubContextNodes(); - } /* - else, previous rows might have set the context transposed node already, - and we simply inherit that transposed node. - */ - - if (node instanceof NodeWithLinks && context.transposedNode != null) { - NodeWithLinks node2 = (NodeWithLinks) node; - - int linkCount = node2.getLinkCount(); - - for (int i = 0; i < linkCount; i++) { - descend( - project, - protograph, - nodeFactory, - row, - node2.getLink(i).getTarget(), - context.subContexts.get(i) - ); - } - } - } - - static class Context { - TransposedNode transposedNode; - List subContexts; - Context parent; - Link link; - int count; - int limit; - - Context(Node node, Context parent, Link link, int limit) { - this.parent = parent; - this.link = link; - this.limit = limit; - - if (node instanceof NodeWithLinks) { - NodeWithLinks node2 = (NodeWithLinks) node; - - int subContextCount = node2.getLinkCount(); - - subContexts = new LinkedList(); - for (int i = 0; i < subContextCount; i++) { - Link link2 = node2.getLink(i); - subContexts.add( - new Context(link2.getTarget(), this, link2, -1)); - } - } - } - - public void nullifySubContextNodes() { - if (subContexts != null) { - for (Context context : subContexts) { - context.transposedNode = null; - context.nullifySubContextNodes(); - } - } - } - } + static public void transpose( + Project project, + Protograph protograph, + Node rootNode, + TransposedNodeFactory nodeFactory + ) { + Context rootContext = new Context(rootNode, null, null, 20); + + for (Row row : project.rows) { + descend(project, protograph, nodeFactory, row, rootNode, rootContext); + if (rootContext.limit > 0 && rootContext.count > rootContext.limit) { + break; + } + } + } + + static protected void descend( + Project project, + Protograph protograph, + TransposedNodeFactory nodeFactory, + Row row, + Node node, + Context context + ) { + TransposedNode tnode = null; + + TransposedNode parentNode = context.parent == null ? null : context.parent.transposedNode; + FreebaseProperty property = context.parent == null ? null : context.link.property; + + if (node instanceof CellNode) { + CellNode node2 = (CellNode) node; + Column column = project.columnModel.getColumnByName(node2.columnName); + Cell cell = row.getCell(column.getCellIndex()); + if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) { + if (node2 instanceof CellTopicNode) { + if (!((CellTopicNode) node2).createForNoReconMatch && + (cell.recon == null || cell.recon.judgment == Judgment.None)) { + return; + } + } + + context.count++; + if (context.limit > 0 && context.count > context.limit) { + return; + } + + tnode = nodeFactory.transposeCellNode( + parentNode, + property, + node2, + cell + ); + } + } else { + if (node instanceof AnonymousNode) { + tnode = nodeFactory.transposeAnonymousNode( + parentNode, + property, + (AnonymousNode) node + ); + } else if (node instanceof FreebaseTopicNode) { + tnode = nodeFactory.transposeTopicNode( + parentNode, + property, + (FreebaseTopicNode) node + ); + } else if (node instanceof ValueNode) { + tnode = nodeFactory.transposeValueNode( + parentNode, + property, + (ValueNode) node + ); + } + } + + if (tnode != null) { + context.transposedNode = tnode; + context.nullifySubContextNodes(); + } /* + else, previous rows might have set the context transposed node already, + and we simply inherit that transposed node. + */ + + if (node instanceof NodeWithLinks && context.transposedNode != null) { + NodeWithLinks node2 = (NodeWithLinks) node; + + int linkCount = node2.getLinkCount(); + + for (int i = 0; i < linkCount; i++) { + descend( + project, + protograph, + nodeFactory, + row, + node2.getLink(i).getTarget(), + context.subContexts.get(i) + ); + } + } + } + + static class Context { + TransposedNode transposedNode; + List subContexts; + Context parent; + Link link; + int count; + int limit; + + Context(Node node, Context parent, Link link, int limit) { + this.parent = parent; + this.link = link; + this.limit = limit; + + if (node instanceof NodeWithLinks) { + NodeWithLinks node2 = (NodeWithLinks) node; + + int subContextCount = node2.getLinkCount(); + + subContexts = new LinkedList(); + for (int i = 0; i < subContextCount; i++) { + Link link2 = node2.getLink(i); + subContexts.add( + new Context(link2.getTarget(), this, link2, -1)); + } + } + } + + public void nullifySubContextNodes() { + if (subContexts != null) { + for (Context context : subContexts) { + context.transposedNode = null; + context.nullifySubContextNodes(); + } + } + } + } } diff --git a/src/main/java/com/metaweb/gridworks/protograph/transpose/TripleLoaderTransposedNodeFactory.java b/src/main/java/com/metaweb/gridworks/protograph/transpose/TripleLoaderTransposedNodeFactory.java index 5b3b1ef3a..6460284eb 100644 --- a/src/main/java/com/metaweb/gridworks/protograph/transpose/TripleLoaderTransposedNodeFactory.java +++ b/src/main/java/com/metaweb/gridworks/protograph/transpose/TripleLoaderTransposedNodeFactory.java @@ -20,307 +20,307 @@ import com.metaweb.gridworks.protograph.FreebaseTopicNode; import com.metaweb.gridworks.protograph.ValueNode; public class TripleLoaderTransposedNodeFactory implements TransposedNodeFactory { - protected List rootNodes = new LinkedList(); - protected StringBuffer stringBuffer; - protected Map varPool = new HashMap(); - protected Map newTopicVars = new HashMap(); - - public String getLoad() { - stringBuffer = new StringBuffer(); - for (WritingTransposedNode node : rootNodes) { - node.write(null, null); - } - return stringBuffer.toString(); - } - - protected void writeLine(String line) { - if (stringBuffer.length() > 0) { - stringBuffer.append('\n'); - } - stringBuffer.append(line); - } - protected void writeLine(String subject, String predicate, String object) { - if (subject != null && object != null) { - writeLine("{ 's' : '" + subject + "', 'p' : '" + predicate + "', 'o' : " + object + " }"); - } - } - - protected interface WritingTransposedNode extends TransposedNode { - public String write(String subject, String predicate); - } - - abstract protected class TransposedNodeWithChildren implements WritingTransposedNode { - public List properties = new LinkedList(); - public List children = new LinkedList(); - - protected void writeChildren(String subject) { - for (int i = 0; i < children.size(); i++) { - WritingTransposedNode child = children.get(i); - String predicate = properties.get(i).id; - - child.write(subject, predicate); - } - } - } - - protected class AnonymousTransposedNode extends TransposedNodeWithChildren { - AnonymousNode node; - - protected AnonymousTransposedNode( - AnonymousNode node - ) { - this.node = node; - } - - public String write(String subject, String predicate) { - if (children.size() == 0 || subject == null) { - return null; - } - - StringBuffer sb = new StringBuffer(); - sb.append("{ "); - - boolean first = true; - for (int i = 0; i < children.size(); i++) { - String s = children.get(i).write(null, null); - if (s != null) { - if (first) { - first = false; - } else { - sb.append(", "); - } - sb.append("\"" + properties.get(i).id + "\": "); - sb.append(s); - } - } - sb.append(" }"); - - writeLine(subject, predicate, sb.toString()); - - return null; - } - } - - protected class CellTopicTransposedNode extends TransposedNodeWithChildren { - protected CellTopicNode node; - protected Cell cell; - - public CellTopicTransposedNode(CellTopicNode node, Cell cell) { - this.node = node; - this.cell = cell; - } - - public String write(String subject, String predicate) { - String id = null; - if (cell.recon != null && - cell.recon.judgment == Recon.Judgment.Matched && - cell.recon.match != null) { - - id = cell.recon.match.topicID; - } else if (node.createForNoReconMatch || - (cell.recon != null && cell.recon.judgment == Judgment.New)) { - if (cell.recon != null && newTopicVars.containsKey(cell.recon.id)) { - id = newTopicVars.get(cell.recon.id); - } else { - long var = 0; - if (varPool.containsKey(node.columnName)) { - var = varPool.get(node.columnName); - } - varPool.put(node.columnName, var + 1); - - id = "$" + node.columnName.replaceAll("\\W+", "_") + "_" + var; - - writeLine("{ 's' : '" + id + "', 'p' : 'type', 'o' : '" + node.type.id + "' }"); - writeLine("{ 's' : '" + id + "', 'p' : 'name', 'o' : " + JSONObject.quote(cell.value.toString()) + " }"); - - if (cell.recon != null) { - newTopicVars.put(cell.recon.id, id); - } - } - } else { - return null; - } - - if (subject != null) { - writeLine(subject, predicate, id); - } - - writeChildren(id); - - return JSONObject.quote(id); - } - } - - protected class CellValueTransposedNode implements WritingTransposedNode { - protected JSONObject obj; - protected CellValueNode node; - protected Cell cell; - - public CellValueTransposedNode(CellValueNode node, Cell cell) { - this.node = node; - this.cell = cell; - } - - public String write(String subject, String predicate) { - String object = cell.value instanceof String ? - JSONObject.quote((String) cell.value) : cell.value.toString(); - - if (subject != null) { - if ("/type/text".equals(node.lang)) { - writeLine( - "{ 's' : '" + subject + - "', 'p' : '" + predicate + - "', 'o' : " + object + - ", 'lang' : '" + node.lang + - "' }" - ); - } else { - writeLine( - "{ 's' : '" + subject + - "', 'p' : '" + predicate + - "', 'o' : " + object + " }" - ); - } - } - - return object; - } - } - - protected class CellKeyTransposedNode implements WritingTransposedNode { - protected CellKeyNode node; - protected Cell cell; - - public CellKeyTransposedNode(CellKeyNode node, Cell cell) { - this.node = node; - this.cell = cell; - } - - public String write(String subject, String predicate) { - writeLine(subject, "key", JSONObject.quote(node.namespace.id + "/" + cell.value)); - - return null; - } - } - - protected class TopicTransposedNode extends TransposedNodeWithChildren { - protected FreebaseTopicNode node; - - public TopicTransposedNode(FreebaseTopicNode node) { - this.node = node; - } + protected List rootNodes = new LinkedList(); + protected StringBuffer stringBuffer; + protected Map varPool = new HashMap(); + protected Map newTopicVars = new HashMap(); + + public String getLoad() { + stringBuffer = new StringBuffer(); + for (WritingTransposedNode node : rootNodes) { + node.write(null, null); + } + return stringBuffer.toString(); + } + + protected void writeLine(String line) { + if (stringBuffer.length() > 0) { + stringBuffer.append('\n'); + } + stringBuffer.append(line); + } + protected void writeLine(String subject, String predicate, String object) { + if (subject != null && object != null) { + writeLine("{ 's' : '" + subject + "', 'p' : '" + predicate + "', 'o' : " + object + " }"); + } + } + + protected interface WritingTransposedNode extends TransposedNode { + public String write(String subject, String predicate); + } + + abstract protected class TransposedNodeWithChildren implements WritingTransposedNode { + public List properties = new LinkedList(); + public List children = new LinkedList(); + + protected void writeChildren(String subject) { + for (int i = 0; i < children.size(); i++) { + WritingTransposedNode child = children.get(i); + String predicate = properties.get(i).id; + + child.write(subject, predicate); + } + } + } + + protected class AnonymousTransposedNode extends TransposedNodeWithChildren { + AnonymousNode node; + + protected AnonymousTransposedNode( + AnonymousNode node + ) { + this.node = node; + } + + public String write(String subject, String predicate) { + if (children.size() == 0 || subject == null) { + return null; + } + + StringBuffer sb = new StringBuffer(); + sb.append("{ "); + + boolean first = true; + for (int i = 0; i < children.size(); i++) { + String s = children.get(i).write(null, null); + if (s != null) { + if (first) { + first = false; + } else { + sb.append(", "); + } + sb.append("\"" + properties.get(i).id + "\": "); + sb.append(s); + } + } + sb.append(" }"); + + writeLine(subject, predicate, sb.toString()); + + return null; + } + } + + protected class CellTopicTransposedNode extends TransposedNodeWithChildren { + protected CellTopicNode node; + protected Cell cell; + + public CellTopicTransposedNode(CellTopicNode node, Cell cell) { + this.node = node; + this.cell = cell; + } + + public String write(String subject, String predicate) { + String id = null; + if (cell.recon != null && + cell.recon.judgment == Recon.Judgment.Matched && + cell.recon.match != null) { + + id = cell.recon.match.topicID; + } else if (node.createForNoReconMatch || + (cell.recon != null && cell.recon.judgment == Judgment.New)) { + if (cell.recon != null && newTopicVars.containsKey(cell.recon.id)) { + id = newTopicVars.get(cell.recon.id); + } else { + long var = 0; + if (varPool.containsKey(node.columnName)) { + var = varPool.get(node.columnName); + } + varPool.put(node.columnName, var + 1); + + id = "$" + node.columnName.replaceAll("\\W+", "_") + "_" + var; + + writeLine("{ 's' : '" + id + "', 'p' : 'type', 'o' : '" + node.type.id + "' }"); + writeLine("{ 's' : '" + id + "', 'p' : 'name', 'o' : " + JSONObject.quote(cell.value.toString()) + " }"); + + if (cell.recon != null) { + newTopicVars.put(cell.recon.id, id); + } + } + } else { + return null; + } + + if (subject != null) { + writeLine(subject, predicate, id); + } + + writeChildren(id); + + return JSONObject.quote(id); + } + } + + protected class CellValueTransposedNode implements WritingTransposedNode { + protected JSONObject obj; + protected CellValueNode node; + protected Cell cell; + + public CellValueTransposedNode(CellValueNode node, Cell cell) { + this.node = node; + this.cell = cell; + } + + public String write(String subject, String predicate) { + String object = cell.value instanceof String ? + JSONObject.quote((String) cell.value) : cell.value.toString(); + + if (subject != null) { + if ("/type/text".equals(node.lang)) { + writeLine( + "{ 's' : '" + subject + + "', 'p' : '" + predicate + + "', 'o' : " + object + + ", 'lang' : '" + node.lang + + "' }" + ); + } else { + writeLine( + "{ 's' : '" + subject + + "', 'p' : '" + predicate + + "', 'o' : " + object + " }" + ); + } + } + + return object; + } + } + + protected class CellKeyTransposedNode implements WritingTransposedNode { + protected CellKeyNode node; + protected Cell cell; + + public CellKeyTransposedNode(CellKeyNode node, Cell cell) { + this.node = node; + this.cell = cell; + } + + public String write(String subject, String predicate) { + writeLine(subject, "key", JSONObject.quote(node.namespace.id + "/" + cell.value)); + + return null; + } + } + + protected class TopicTransposedNode extends TransposedNodeWithChildren { + protected FreebaseTopicNode node; + + public TopicTransposedNode(FreebaseTopicNode node) { + this.node = node; + } - public String write(String subject, String predicate) { - String object = JSONObject.quote(node.topic.id); - - writeLine(subject, predicate, object); - writeChildren(object); - - return object; - } - } + public String write(String subject, String predicate) { + String object = JSONObject.quote(node.topic.id); + + writeLine(subject, predicate, object); + writeChildren(object); + + return object; + } + } - protected class ValueTransposedNode implements WritingTransposedNode { - protected ValueNode node; - - public ValueTransposedNode(ValueNode node) { - this.node = node; - } + protected class ValueTransposedNode implements WritingTransposedNode { + protected ValueNode node; + + public ValueTransposedNode(ValueNode node) { + this.node = node; + } - public String write(String subject, String predicate) { - String object = node.value instanceof String ? - JSONObject.quote((String) node.value) : node.value.toString(); - - if ("/type/text".equals(node.lang)) { - writeLine( - "{ 's' : '" + subject + - "', 'p' : '" + predicate + - "', 'o' : " + object + - ", 'lang' : '" + node.lang + - "' }" - ); - } else { - writeLine( - "{ 's' : '" + subject + - "', 'p' : '" + predicate + - "', 'o' : " + object + " }" - ); - } - - return object; - } - } - - public TransposedNode transposeAnonymousNode( - TransposedNode parentNode, - FreebaseProperty property, - AnonymousNode node) { - - WritingTransposedNode tnode = new AnonymousTransposedNode(node); - - processTransposedNode(tnode, parentNode, property); - - return tnode; - } + public String write(String subject, String predicate) { + String object = node.value instanceof String ? + JSONObject.quote((String) node.value) : node.value.toString(); + + if ("/type/text".equals(node.lang)) { + writeLine( + "{ 's' : '" + subject + + "', 'p' : '" + predicate + + "', 'o' : " + object + + ", 'lang' : '" + node.lang + + "' }" + ); + } else { + writeLine( + "{ 's' : '" + subject + + "', 'p' : '" + predicate + + "', 'o' : " + object + " }" + ); + } + + return object; + } + } + + public TransposedNode transposeAnonymousNode( + TransposedNode parentNode, + FreebaseProperty property, + AnonymousNode node) { + + WritingTransposedNode tnode = new AnonymousTransposedNode(node); + + processTransposedNode(tnode, parentNode, property); + + return tnode; + } - public TransposedNode transposeCellNode( - TransposedNode parentNode, - FreebaseProperty property, - CellNode node, - Cell cell) { - - WritingTransposedNode tnode = null; - if (node instanceof CellTopicNode) { - tnode = new CellTopicTransposedNode((CellTopicNode) node, cell); - } else if (node instanceof CellValueNode) { - tnode = new CellValueTransposedNode((CellValueNode) node, cell); - } else if (node instanceof CellKeyNode) { - tnode = new CellKeyTransposedNode((CellKeyNode) node, cell); - } - - if (tnode != null) { - processTransposedNode(tnode, parentNode, property); - } - return tnode; - } + public TransposedNode transposeCellNode( + TransposedNode parentNode, + FreebaseProperty property, + CellNode node, + Cell cell) { + + WritingTransposedNode tnode = null; + if (node instanceof CellTopicNode) { + tnode = new CellTopicTransposedNode((CellTopicNode) node, cell); + } else if (node instanceof CellValueNode) { + tnode = new CellValueTransposedNode((CellValueNode) node, cell); + } else if (node instanceof CellKeyNode) { + tnode = new CellKeyTransposedNode((CellKeyNode) node, cell); + } + + if (tnode != null) { + processTransposedNode(tnode, parentNode, property); + } + return tnode; + } - public TransposedNode transposeTopicNode( - TransposedNode parentNode, - FreebaseProperty property, - FreebaseTopicNode node) { - - WritingTransposedNode tnode = new TopicTransposedNode(node); - - processTransposedNode(tnode, parentNode, property); - - return tnode; - } + public TransposedNode transposeTopicNode( + TransposedNode parentNode, + FreebaseProperty property, + FreebaseTopicNode node) { + + WritingTransposedNode tnode = new TopicTransposedNode(node); + + processTransposedNode(tnode, parentNode, property); + + return tnode; + } - public TransposedNode transposeValueNode( - TransposedNode parentNode, - FreebaseProperty property, - ValueNode node) { - - WritingTransposedNode tnode = new ValueTransposedNode(node); - - processTransposedNode(tnode, parentNode, property); - - return tnode; - } - - protected void processTransposedNode( - WritingTransposedNode tnode, - TransposedNode parentNode, - FreebaseProperty property - ) { - if (parentNode != null) { - if (parentNode instanceof TransposedNodeWithChildren) { - TransposedNodeWithChildren parentNode2 = (TransposedNodeWithChildren) parentNode; - parentNode2.children.add(tnode); - parentNode2.properties.add(property); - } - } else { - rootNodes.add(tnode); - } - } + public TransposedNode transposeValueNode( + TransposedNode parentNode, + FreebaseProperty property, + ValueNode node) { + + WritingTransposedNode tnode = new ValueTransposedNode(node); + + processTransposedNode(tnode, parentNode, property); + + return tnode; + } + + protected void processTransposedNode( + WritingTransposedNode tnode, + TransposedNode parentNode, + FreebaseProperty property + ) { + if (parentNode != null) { + if (parentNode instanceof TransposedNodeWithChildren) { + TransposedNodeWithChildren parentNode2 = (TransposedNodeWithChildren) parentNode; + parentNode2.children.add(tnode); + parentNode2.properties.add(property); + } + } else { + rootNodes.add(tnode); + } + } } diff --git a/src/main/java/com/metaweb/gridworks/util/IndentWriter.java b/src/main/java/com/metaweb/gridworks/util/IndentWriter.java index 9ce68d74f..471c1c6f4 100644 --- a/src/main/java/com/metaweb/gridworks/util/IndentWriter.java +++ b/src/main/java/com/metaweb/gridworks/util/IndentWriter.java @@ -26,12 +26,12 @@ public class IndentWriter { } } - private Writer m_writer; + private Writer m_writer; private int m_count = 0; private boolean m_indent = true; public IndentWriter(Writer writer) { - m_writer = writer; + m_writer = writer; } public void close() throws IOException { @@ -39,7 +39,7 @@ public class IndentWriter { } public void flush() throws IOException { - m_writer.flush(); + m_writer.flush(); } public void print(Object o) throws IOException { diff --git a/src/main/java/com/metaweb/gridworks/util/JSObject.java b/src/main/java/com/metaweb/gridworks/util/JSObject.java index 3b457a460..fe46512cc 100644 --- a/src/main/java/com/metaweb/gridworks/util/JSObject.java +++ b/src/main/java/com/metaweb/gridworks/util/JSObject.java @@ -35,9 +35,9 @@ public class JSObject extends Properties { writeObject(writer, value); if (e.hasMoreElements()) { - writer.println(","); + writer.println(","); } else { - writer.println(); + writer.println(); } } } @@ -47,16 +47,16 @@ public class JSObject extends Properties { @SuppressWarnings("unchecked") static public void writeCollection(IndentWriter writer, Collection c) throws IOException, JSONException { - writer.println("["); - writer.indent(); + writer.println("["); + writer.indent(); { Iterator i = c.iterator(); while (i.hasNext()) { writeObject(writer, i.next()); if (i.hasNext()) { - writer.println(","); + writer.println(","); } else { - writer.println(); + writer.println(); } } } @@ -68,39 +68,39 @@ public class JSObject extends Properties { writer.println("{"); writer.indent(); { - String[] names = JSONObject.getNames(no); - for (int i = 0; i < names.length; i++) { - String name = names[i]; - Object value = no.get(name); - - writer.print("'"); + String[] names = JSONObject.getNames(no); + for (int i = 0; i < names.length; i++) { + String name = names[i]; + Object value = no.get(name); + + writer.print("'"); writer.print(name + "' : "); writeObject(writer, value); if (i < names.length - 1) { - writer.println(","); + writer.println(","); } else { - writer.println(); + writer.println(); } - } + } } writer.unindent(); writer.print("}"); } static public void writeJSONArray(IndentWriter writer, JSONArray na) throws IOException, JSONException { - writer.println("["); - writer.indent(); + writer.println("["); + writer.indent(); { - int count = na.length(); - for (int i = 0; i < count; i++) { - Object element = na.get(i); - + int count = na.length(); + for (int i = 0; i < count; i++) { + Object element = na.get(i); + writeObject(writer, element); if (i < count - 1) { - writer.println(","); + writer.println(","); } else { - writer.println(); + writer.println(); } } } @@ -110,24 +110,24 @@ public class JSObject extends Properties { @SuppressWarnings("unchecked") static public void writeObject(IndentWriter writer, Object o) throws IOException, JSONException { - if (o == null) { - writer.print("null"); - } else if (o instanceof Boolean) { - writer.print(((Boolean) o).booleanValue() ? "true" : "false"); - } else if (o instanceof Number) { - writer.print(((Number) o).toString()); - + if (o == null) { + writer.print("null"); + } else if (o instanceof Boolean) { + writer.print(((Boolean) o).booleanValue() ? "true" : "false"); + } else if (o instanceof Number) { + writer.print(((Number) o).toString()); + } else if (o instanceof Collection) { - writeCollection(writer, (Collection) o); + writeCollection(writer, (Collection) o); } else if (o instanceof JSONArray) { - writeJSONArray(writer, (JSONArray) o); + writeJSONArray(writer, (JSONArray) o); } else if (o instanceof JSObject) { writeJSObject(writer, (JSObject) o); } else if (o instanceof JSONObject) { writeJSONObject(writer, (JSONObject) o); } else { - writer.print("\"" + StringEscapeUtils.escapeJavaScript(o.toString()) + "\""); + writer.print("\"" + StringEscapeUtils.escapeJavaScript(o.toString()) + "\""); } } } diff --git a/src/main/java/com/metaweb/gridworks/util/ParsingUtilities.java b/src/main/java/com/metaweb/gridworks/util/ParsingUtilities.java index 844f422f9..c81737ff9 100644 --- a/src/main/java/com/metaweb/gridworks/util/ParsingUtilities.java +++ b/src/main/java/com/metaweb/gridworks/util/ParsingUtilities.java @@ -14,55 +14,55 @@ import org.json.JSONObject; import org.json.JSONTokener; public class ParsingUtilities { - static public String inputStreamToString(InputStream is) throws IOException { - Reader reader = new InputStreamReader(is, "UTF-8"); - try { - return readerToString(reader); - } finally { - reader.close(); - } - } + static public String inputStreamToString(InputStream is) throws IOException { + Reader reader = new InputStreamReader(is, "UTF-8"); + try { + return readerToString(reader); + } finally { + reader.close(); + } + } - static public String readerToString(Reader reader) throws IOException { - StringBuffer sb = new StringBuffer(); - - char[] chars = new char[8192]; - int c; - - while ((c = reader.read(chars)) > 0) { - sb.insert(sb.length(), chars, 0, c); - } - - return sb.toString(); - } + static public String readerToString(Reader reader) throws IOException { + StringBuffer sb = new StringBuffer(); + + char[] chars = new char[8192]; + int c; + + while ((c = reader.read(chars)) > 0) { + sb.insert(sb.length(), chars, 0, c); + } + + return sb.toString(); + } static public JSONObject evaluateJsonStringToObject(String s) throws JSONException { - JSONTokener t = new JSONTokener(s); - JSONObject o = (JSONObject) t.nextValue(); - return o; + JSONTokener t = new JSONTokener(s); + JSONObject o = (JSONObject) t.nextValue(); + return o; } static public JSONArray evaluateJsonStringToArray(String s) throws JSONException { - JSONTokener t = new JSONTokener(s); - JSONArray a = (JSONArray) t.nextValue(); - return a; + JSONTokener t = new JSONTokener(s); + JSONArray a = (JSONArray) t.nextValue(); + return a; } private static final URLCodec codec = new URLCodec(); static public String encode(String s) { try { - return codec.encode(s, "UTF-8"); - } catch (UnsupportedEncodingException e) { - return s; // should not happen - } + return codec.encode(s, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return s; // should not happen + } } static public String decode(String s) { try { - return codec.decode(s, "UTF-8"); - } catch (UnsupportedEncodingException e) { - return s; // should not happen - } catch (DecoderException e) { - return s; // should not happen - } + return codec.decode(s, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return s; // should not happen + } catch (DecoderException e) { + return s; // should not happen + } } }