Browse Source
Refactoring of to-PRISM language translations: addition of PrismLanguageTranslator abstract class (preliminary version of) and connection with SBML/reactions/PEPA translators. Also, add support for SBML import to Prism class (but not used elsewhere yet).
Refactoring of to-PRISM language translations: addition of PrismLanguageTranslator abstract class (preliminary version of) and connection with SBML/reactions/PEPA translators. Also, add support for SBML import to Prism class (but not used elsewhere yet).
git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@10687 bbc10eb1-c90d-0410-af57-cb519fbb1720master
6 changed files with 387 additions and 141 deletions
-
91prism/src/prism/PEPA2Prism.java
-
94prism/src/prism/Prism.java
-
94prism/src/prism/PrismLanguageTranslator.java
-
5prism/src/prism/Reactions2Prism.java
-
118prism/src/prism/ReactionsText2Prism.java
-
126prism/src/prism/SBML2Prism.java
@ -0,0 +1,91 @@ |
|||
package prism; |
|||
|
|||
import java.io.File; |
|||
import java.io.FileWriter; |
|||
import java.io.IOException; |
|||
import java.io.InputStream; |
|||
import java.io.PrintStream; |
|||
|
|||
public class PEPA2Prism extends PrismLanguageTranslator |
|||
{ |
|||
private File modelFile; |
|||
|
|||
@Override |
|||
public String getName() |
|||
{ |
|||
return "pepa"; |
|||
} |
|||
|
|||
@Override |
|||
public void load(File file) throws PrismException |
|||
{ |
|||
modelFile = file; |
|||
} |
|||
|
|||
@Override |
|||
public void load(String s) throws PrismException |
|||
{ |
|||
try { |
|||
// Create temporary file containing pepa model |
|||
modelFile = File.createTempFile("tempPepa" + System.currentTimeMillis(), ".pepa"); |
|||
FileWriter write = new FileWriter(modelFile); |
|||
write.write(s); |
|||
write.close(); |
|||
} catch (IOException e) { |
|||
if (modelFile != null) { |
|||
modelFile.delete(); |
|||
modelFile = null; |
|||
} |
|||
throw new PrismException("Couldn't create temporary file for PEPA conversion"); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void load(InputStream in) throws PrismException |
|||
{ |
|||
try { |
|||
// Create temporary file containing pepa model |
|||
modelFile = File.createTempFile("tempPepa" + System.currentTimeMillis(), ".pepa"); |
|||
FileWriter write = new FileWriter(modelFile); |
|||
int byteIn = in.read(); |
|||
while (byteIn != -1) { |
|||
write.write(byteIn); |
|||
byteIn = in.read(); |
|||
} |
|||
write.close(); |
|||
} catch (IOException e) { |
|||
if (modelFile != null) { |
|||
modelFile.delete(); |
|||
modelFile = null; |
|||
} |
|||
throw new PrismException("Couldn't create temporary file for PEPA conversion"); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public String translateToString() throws PrismException |
|||
{ |
|||
// Translate PEPA model to PRISM model string |
|||
String prismModelString = null; |
|||
try { |
|||
prismModelString = pepa.compiler.Main.compile("" + modelFile); |
|||
} catch (pepa.compiler.InternalError e) { |
|||
if (modelFile != null) { |
|||
modelFile.delete(); |
|||
modelFile = null; |
|||
} |
|||
throw new PrismException("Could not import PEPA model:\n" + e.getMessage()); |
|||
} |
|||
if (modelFile != null) { |
|||
modelFile.delete(); |
|||
modelFile = null; |
|||
} |
|||
return prismModelString; |
|||
} |
|||
|
|||
@Override |
|||
public void translate(PrintStream out) throws PrismException |
|||
{ |
|||
out.print(translateToString()); |
|||
} |
|||
} |
|||
@ -0,0 +1,94 @@ |
|||
//============================================================================== |
|||
// |
|||
// Copyright (c) 2015- |
|||
// Authors: |
|||
// * Dave Parker <d.a.parker@cs.bham.ac.uk> (University of Birmingham) |
|||
// |
|||
//------------------------------------------------------------------------------ |
|||
// |
|||
// This file is part of PRISM. |
|||
// |
|||
// PRISM is free software; you can redistribute it and/or modify |
|||
// it under the terms of the GNU General Public License as published by |
|||
// the Free Software Foundation; either version 2 of the License, or |
|||
// (at your option) any later version. |
|||
// |
|||
// PRISM is distributed in the hope that it will be useful, |
|||
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
// GNU General Public License for more details. |
|||
// |
|||
// You should have received a copy of the GNU General Public License |
|||
// along with PRISM; if not, write to the Free Software Foundation, |
|||
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|||
// |
|||
//============================================================================== |
|||
|
|||
package prism; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.File; |
|||
import java.io.FileInputStream; |
|||
import java.io.FileNotFoundException; |
|||
import java.io.InputStream; |
|||
import java.io.PrintStream; |
|||
import java.io.UnsupportedEncodingException; |
|||
|
|||
/** |
|||
* Base class for classes that convert to PRISM language models from other languages. |
|||
*/ |
|||
public abstract class PrismLanguageTranslator |
|||
{ |
|||
/** |
|||
* Get a name associated with the language to be translated from. |
|||
*/ |
|||
public abstract String getName(); |
|||
|
|||
/** |
|||
* Load a model to be translated from an input stream. |
|||
*/ |
|||
public abstract void load(InputStream in) throws PrismException; |
|||
|
|||
/** |
|||
* Load a model to be translated from a string. |
|||
*/ |
|||
public void load(String modelString) throws PrismException |
|||
{ |
|||
load(new ByteArrayInputStream(modelString.getBytes())); |
|||
} |
|||
|
|||
/** |
|||
* Load a model to be translated from a file. |
|||
*/ |
|||
public void load(File file) throws PrismException |
|||
{ |
|||
FileInputStream in; |
|||
try { |
|||
in = new FileInputStream(file); |
|||
} catch (FileNotFoundException e) { |
|||
throw new PrismException("File \"" + file.getPath() + "\" not found"); |
|||
} |
|||
load(in); |
|||
} |
|||
|
|||
/** |
|||
* Translate the loaded model to a PRISM language model and write it to an output stream. |
|||
*/ |
|||
public abstract void translate(PrintStream out) throws PrismException; |
|||
|
|||
/** |
|||
* Translate the loaded model to a PRISM language model and return it as a string. |
|||
*/ |
|||
public String translateToString() throws PrismException |
|||
{ |
|||
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|||
PrintStream out = new PrintStream(os); |
|||
translate(out); |
|||
try { |
|||
return os.toString("UTF-8"); |
|||
} catch (UnsupportedEncodingException e) { |
|||
throw new PrismException("Error translating output stream to string: " + e.getMessage()); |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue