From 6fc35d1e3565e18444740ad067430f506b53dd35 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 31 Jul 2012 06:58:45 +0000 Subject: [PATCH] Added prints to console to tell that mapping has failed. This does not fix the fact that the mappings fail, but at least user knows that something is wrong. (refs #3580) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25372 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/manager/SysdynConsole.java | 206 +++++++++++++++++ .../simantics/sysdyn/manager/SysdynModel.java | 24 +- .../representation/LoadRepresentation.java | 41 +++- .../sysdyn/simulation/SimulationJob.java | 207 +----------------- 4 files changed, 272 insertions(+), 206 deletions(-) create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynConsole.java diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynConsole.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynConsole.java new file mode 100644 index 00000000..90d73058 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynConsole.java @@ -0,0 +1,206 @@ +package org.simantics.sysdyn.manager; + +import java.text.SimpleDateFormat; +import java.util.Calendar; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.console.ConsolePlugin; +import org.eclipse.ui.console.IConsole; +import org.eclipse.ui.console.IConsoleManager; +import org.eclipse.ui.console.IHyperlink; +import org.eclipse.ui.console.IPatternMatchListener; +import org.eclipse.ui.console.MessageConsole; +import org.eclipse.ui.console.MessageConsoleStream; +import org.eclipse.ui.console.PatternMatchEvent; +import org.eclipse.ui.console.TextConsole; +import org.simantics.modelica.IModelicaMonitor; + +public class SysdynConsole implements IModelicaMonitor { + + private static String SYSDYN_CONSOLE = "Sysyn modelica console"; + private MessageConsole console; + + public static SysdynConsole INSTANCE = new SysdynConsole(); + + public SysdynConsole() { + ConsolePlugin plugin = ConsolePlugin.getDefault(); + IConsoleManager conMan = plugin.getConsoleManager(); + IConsole[] existing = conMan.getConsoles(); + for (int i = 0; i < existing.length; i++) + if (existing[i].getName().equals(SYSDYN_CONSOLE)) + console = (MessageConsole) existing[i]; + if(console == null) { + MessageConsole myConsole = new MessageConsole(SYSDYN_CONSOLE, null); + conMan.addConsoles(new IConsole[]{myConsole}); + console = myConsole; + console.addPatternMatchListener(new IPatternMatchListener() { + + @Override + public void matchFound(PatternMatchEvent event) { + try { + console.addHyperlink(new IssueLink(), event.getOffset(), event.getLength()); + } catch (BadLocationException e) { + e.printStackTrace(); + } + } + + @Override + public void disconnect() { + } + + @Override + public void connect(TextConsole console) { + } + + @Override + public String getPattern() { + + return "Issues view"; + } + + @Override + public String getLineQualifier() { + return null; + } + + @Override + public int getCompilerFlags() { + return 0; + } + }); + + /* Link to the modelica document: to be opened to a modelica code viewer. + console.addPatternMatchListener(new IPatternMatchListener() { + + @Override + public void matchFound(PatternMatchEvent event) { + try { + if(event.getSource() instanceof MessageConsole) { + MessageConsole console = (MessageConsole) event.getSource(); + IDocument document = console.getDocument(); + String s = document.get(event.getOffset(), event.getLength()); + s = s.substring(s.indexOf(":") + 1, s.lastIndexOf(":")); + String[] split = s.split(":|-"); + + //TODO: Find the modelica document and its locations + int startOffset = modelicaDocument.getLineOffset(Integer.parseInt(split[0])) + Integer.parseInt(split[1]); + int endOffset = modelicaDocument.getLineOffset(Integer.parseInt(split[2])) + Integer.parseInt(split[3]); + System.out.println("Open modelica editor for model at: " + startOffset + ", " + (endOffset - startOffset)); + + } + + console.addHyperlink(new ModelicaLink(), event.getOffset(), event.getLength()); + } catch (BadLocationException e) { + e.printStackTrace(); + } + } + + @Override + public void disconnect() { + } + + @Override + public void connect(TextConsole console) { + } + + @Override + public String getPattern() { + return "\\[[^\\[]*:[\\d]*:[\\d]*\\-[\\d]*:[\\d]*:writable\\]"; + + } + + @Override + public String getLineQualifier() { + return null; + } + + @Override + public int getCompilerFlags() { + return 0; + } + }); + */ + + + } + } + + @Override + public void message(String message) { + message(message, "hh:mm:ss"); + } + + /** + * Print message to a console with a specified time stamp format + * + * @param message the message to be printed + * @param timeStampFormat simpledateformat timestamp format. null if no timestamp wanted + */ + public void message(String message, String timeStampFormat) { + Calendar cal = Calendar.getInstance(); + SimpleDateFormat sdf = new SimpleDateFormat(timeStampFormat); + String time = sdf.format(cal.getTime()); + + MessageConsoleStream out = this.console.newMessageStream(); + if(message.length() > 1) + out.println("[" + time +"] " + message); + + + + if(message.contains("Error")) { + showConsole(); + out.println("See Issues view"); + } + } + + public void clearConsole() { + this.console.clearConsole(); + } + + public void showConsole() { + ConsolePlugin plugin = ConsolePlugin.getDefault(); + IConsoleManager conMan = plugin.getConsoleManager(); + conMan.showConsoleView(console); + } + + + class IssueLink implements IHyperlink { + + @Override + public void linkEntered() { + } + + @Override + public void linkExited() { + } + + @Override + public void linkActivated() { + try { + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.simantics.issues.ui.issueview"); + } catch (PartInitException e) { + e.printStackTrace(); + } + } + } + + class ModelicaLink implements IHyperlink { + + @Override + public void linkEntered() { + } + + @Override + public void linkExited() { + } + + @Override + public void linkActivated() { + System.out.println("MODELICA LINK"); + } + + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 00379a9c..b5aebdae 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -36,6 +36,7 @@ import org.simantics.db.service.VirtualGraphSupport; import org.simantics.layer0.Layer0; import org.simantics.objmap.IMapping; import org.simantics.objmap.IMappingListener; +import org.simantics.objmap.MappingException; import org.simantics.objmap.Mappings; import org.simantics.project.IProject; import org.simantics.simulation.experiment.IDynamicExperiment; @@ -139,7 +140,16 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti SysdynSchema schema = new SysdynSchema(g); mapping = Mappings.createWithListening(schema); mapping.addMappingListener(SysdynModel.this); - configuration = (Configuration)mapping.map(g, configurationResource); + try { + configuration = (Configuration)mapping.map(g, configurationResource); + } catch (MappingException e) { + SysdynConsole.INSTANCE.message( + "Error: Mapping is broken! Find the problem, " + + "fix it and restart the program." + + "\nJava error message:\n" + + e.getMessage()); + throw e; + } for(Resource config : readModules(g, configurationResource)) { modules.add((Configuration)mapping.map(g, config)); } @@ -256,7 +266,17 @@ public class SysdynModel implements IMappingListener, IModel, VariableSubscripti */ public synchronized boolean update(ReadGraph graph) throws DatabaseException { if(mapping.isDomainModified()) { - mapping.updateRange(graph); + + try { + mapping.updateRange(graph); + } catch (MappingException e) { + SysdynConsole.INSTANCE.message( + "Error: Mapping is broken! Find the problem, " + + "fix it and restart the program." + + "\nJava error message:\n" + + e.getMessage()); + throw e; + } // Remove all unnecessary module configurations from modules Set configs = readModules(graph, configurationResource); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java index 912f2e9b..fd5aac59 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/LoadRepresentation.java @@ -17,7 +17,9 @@ import org.simantics.db.Session; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.objmap.IMapping; +import org.simantics.objmap.MappingException; import org.simantics.objmap.Mappings; +import org.simantics.sysdyn.manager.SysdynConsole; public class LoadRepresentation { @@ -29,7 +31,18 @@ public class LoadRepresentation { throws DatabaseException { SysdynSchema schema = new SysdynSchema(graph); IMapping mapping = Mappings.createWithoutListening(schema); - mapping.map(graph, configuration); + + try { + mapping.map(graph, configuration); + } catch (MappingException e) { + SysdynConsole.INSTANCE.message( + "Error: Mapping is broken! Find the problem, " + + "fix it and restart the program." + + "\nJava error message:\n" + + e.getMessage()); + throw e; + } + return mapping; } @@ -44,7 +57,18 @@ public class LoadRepresentation { throws DatabaseException { SysdynSchema schema = new SysdynSchema(graph); IMapping mapping = Mappings.createWithoutListening(schema); - return (Configuration)mapping.map(graph, configuration); + + try { + return (Configuration)mapping.map(graph, configuration); + } catch (MappingException e) { + SysdynConsole.INSTANCE.message( + "Error: Mapping is broken! Find the problem, " + + "fix it and restart the program." + + "\nJava error message:\n" + + e.getMessage()); + throw e; + } + } }); @@ -58,7 +82,18 @@ public class LoadRepresentation { throws DatabaseException { SysdynSchema schema = new SysdynSchema(graph); IMapping mapping = Mappings.createWithoutListening(schema); - return (IElement)mapping.map(graph, element); + + try { + return (IElement)mapping.map(graph, element); + } catch (MappingException e) { + SysdynConsole.INSTANCE.message( + "Error: Mapping is broken! Find the problem, " + + "fix it and restart the program." + + "\nJava error message:\n" + + e.getMessage()); + throw e; + } + } }); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java index ccb4d92e..f61c40c1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -18,35 +18,25 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.console.ConsolePlugin; -import org.eclipse.ui.console.IConsole; -import org.eclipse.ui.console.IConsoleManager; -import org.eclipse.ui.console.IHyperlink; -import org.eclipse.ui.console.IPatternMatchListener; -import org.eclipse.ui.console.MessageConsole; -import org.eclipse.ui.console.MessageConsoleStream; -import org.eclipse.ui.console.PatternMatchEvent; -import org.eclipse.ui.console.TextConsole; import org.simantics.modelica.IModelicaMonitor; import org.simantics.simulation.experiment.Experiment; +import org.simantics.sysdyn.manager.SysdynConsole; import org.simantics.sysdyn.manager.SysdynExperiment; import org.simantics.sysdyn.manager.SysdynModel; public class SimulationJob extends Job { - SysdynModel model; - Experiment experiment; - IModelicaMonitor monitor; - + private SysdynModel model; + private Experiment experiment; + private IModelicaMonitor monitor; + public SimulationJob(SysdynModel model, Experiment experiment) { super("Simulate " + model.getConfiguration().getLabel()); this.model = model; this.experiment = experiment; if(PlatformUI.isWorkbenchRunning()) { - this.monitor = new ModelicaMonitor(); + this.monitor = SysdynConsole.INSTANCE; } else { // Fallback to headless this.monitor = new HeadlessModelicaMonitor(); @@ -109,153 +99,6 @@ public class SimulationJob extends Job { public void showConsole() { } - } - //org.simantics.sysdyn.ui.modelicaEditor - private class ModelicaMonitor implements IModelicaMonitor { - - MessageConsole console; - - public ModelicaMonitor() { - ConsolePlugin plugin = ConsolePlugin.getDefault(); - IConsoleManager conMan = plugin.getConsoleManager(); - IConsole[] existing = conMan.getConsoles(); - for (int i = 0; i < existing.length; i++) - if (existing[i].getName().equals("Simulation")) - console = (MessageConsole) existing[i]; - if(console == null) { - MessageConsole myConsole = new MessageConsole("Simulation", null); - conMan.addConsoles(new IConsole[]{myConsole}); - console = myConsole; - console.addPatternMatchListener(new IPatternMatchListener() { - - @Override - public void matchFound(PatternMatchEvent event) { - try { - console.addHyperlink(new IssueLink(), event.getOffset(), event.getLength()); - } catch (BadLocationException e) { - e.printStackTrace(); - } - } - - @Override - public void disconnect() { - } - - @Override - public void connect(TextConsole console) { - } - - @Override - public String getPattern() { - - return "Issues view"; - } - - @Override - public String getLineQualifier() { - return null; - } - - @Override - public int getCompilerFlags() { - return 0; - } - }); - - /* Link to the modelica document: to be opened to a modelica code viewer. - console.addPatternMatchListener(new IPatternMatchListener() { - - @Override - public void matchFound(PatternMatchEvent event) { - try { - if(event.getSource() instanceof MessageConsole) { - MessageConsole console = (MessageConsole) event.getSource(); - IDocument document = console.getDocument(); - String s = document.get(event.getOffset(), event.getLength()); - s = s.substring(s.indexOf(":") + 1, s.lastIndexOf(":")); - String[] split = s.split(":|-"); - - //TODO: Find the modelica document and its locations - int startOffset = modelicaDocument.getLineOffset(Integer.parseInt(split[0])) + Integer.parseInt(split[1]); - int endOffset = modelicaDocument.getLineOffset(Integer.parseInt(split[2])) + Integer.parseInt(split[3]); - System.out.println("Open modelica editor for model at: " + startOffset + ", " + (endOffset - startOffset)); - - } - - console.addHyperlink(new ModelicaLink(), event.getOffset(), event.getLength()); - } catch (BadLocationException e) { - e.printStackTrace(); - } - } - - @Override - public void disconnect() { - } - - @Override - public void connect(TextConsole console) { - } - - @Override - public String getPattern() { - return "\\[[^\\[]*:[\\d]*:[\\d]*\\-[\\d]*:[\\d]*:writable\\]"; - - } - - @Override - public String getLineQualifier() { - return null; - } - - @Override - public int getCompilerFlags() { - return 0; - } - }); - */ - - - } - } - - @Override - public void message(String message) { - message(message, "hh:mm:ss"); - } - - /** - * Print message to a console with a specified time stamp format - * - * @param message the message to be printed - * @param timeStampFormat simpledateformat timestamp format. null if no timestamp wanted - */ - public void message(String message, String timeStampFormat) { - Calendar cal = Calendar.getInstance(); - SimpleDateFormat sdf = new SimpleDateFormat(timeStampFormat); - String time = sdf.format(cal.getTime()); - - MessageConsoleStream out = this.console.newMessageStream(); - if(message.length() > 1) - out.println("[" + time +"] " + message); - - - - if(message.contains("Error")) { - showConsole(); - out.println("See Issues view"); - } - } - - public void clearConsole() { - this.console.clearConsole(); - } - - public void showConsole() { - ConsolePlugin plugin = ConsolePlugin.getDefault(); - IConsoleManager conMan = plugin.getConsoleManager(); - conMan.showConsoleView(console); - } - } @Override @@ -263,42 +106,4 @@ public class SimulationJob extends Job { } - - class IssueLink implements IHyperlink { - - @Override - public void linkEntered() { - } - - @Override - public void linkExited() { - } - - @Override - public void linkActivated() { - try { - PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.simantics.issues.ui.issueview"); - } catch (PartInitException e) { - e.printStackTrace(); - } - } - } - - class ModelicaLink implements IHyperlink { - - @Override - public void linkEntered() { - } - - @Override - public void linkExited() { - } - - @Override - public void linkActivated() { - System.out.println("MODELICA LINK"); - } - - } - } -- 2.47.1