All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.uni.freiburg.iig.telematik.sepia.parser.PNParsing Maven / Gradle / Ivy

package de.uni.freiburg.iig.telematik.sepia.parser;

import java.io.File;
import java.io.IOException;

import de.invation.code.toval.parser.ParserException;
import de.invation.code.toval.parser.ParserException.ErrorCode;
import de.invation.code.toval.validate.Validate;
import de.uni.freiburg.iig.telematik.sepia.graphic.AbstractGraphicalPN;
import de.uni.freiburg.iig.telematik.sepia.graphic.netgraphics.AbstractPNGraphics;
import de.uni.freiburg.iig.telematik.sepia.parser.petrify.PetrifyParser;
import de.uni.freiburg.iig.telematik.sepia.parser.pnml.PNMLParser;
import de.uni.freiburg.iig.telematik.sepia.petrinet.abstr.AbstractFlowRelation;
import de.uni.freiburg.iig.telematik.sepia.petrinet.abstr.AbstractMarking;
import de.uni.freiburg.iig.telematik.sepia.petrinet.abstr.AbstractPetriNet;
import de.uni.freiburg.iig.telematik.sepia.petrinet.abstr.AbstractPlace;
import de.uni.freiburg.iig.telematik.sepia.petrinet.abstr.AbstractTransition;

/**
 * 

* Parser class which determines the file type and calls the specific parser * implementing the {@link PNParserInterface}. *

*
    *
  • determine file type
  • *
  • call respective parser
  • *
* * @author Adrian Lange * */ public class PNParsing { /** * Parses the given file with the parser respective to the file extension. * * @param

Place type * @param Transition type * @param Flow relation type * @param Marking type * @param Node value type * @param Petri net type * @param Petri net graphics type * @param file File to parse * @return A {@link AbstractGraphicalPN} * @throws IOException If the file can't be found or read * @throws ParserException For exceptions caused by the parsing */ public static synchronized

, T extends AbstractTransition, F extends AbstractFlowRelation, M extends AbstractMarking, S extends Object, N extends AbstractPetriNet, G extends AbstractPNGraphics> AbstractGraphicalPN parse(File file) throws IOException, ParserException { validateFile(file); PNParsingFormat format = guessFormat(file); if (format == null) { throw new ParserException(ErrorCode.UNKNOWN_FILE_EXTENSION); } PNParserInterface parser = getParser(file, format); return parser.parse(file); } /** * Parses the given file with the parser respective to the file extension. * * @param

Place type * @param Transition type * @param Flow relation type * @param Marking type * @param Node value type * @param Petri net type * @param Petri net graphics type * @param fileName File to parse * @return A {@link AbstractGraphicalPN} * @throws IOException If the file can't be found or read * @throws ParserException For exceptions caused by the parsing */ public static synchronized

, T extends AbstractTransition, F extends AbstractFlowRelation, M extends AbstractMarking, S extends Object, N extends AbstractPetriNet, G extends AbstractPNGraphics> AbstractGraphicalPN parse(String fileName) throws IOException, ParserException { Validate.notNull(fileName); return PNParsing.parse(prepareFile(fileName)); } /** * Parses the given file with the parser respective to the file extension. * * @param

Place type * @param Transition type * @param Flow relation type * @param Marking type * @param Node value type * @param Petri net type * @param Petri net graphics type * @param file File to parse * @param format Format to parse from * @return A {@link AbstractGraphicalPN} * @throws IOException If the file can't be found or read * @throws ParserException For exceptions caused by the parsing */ public static synchronized

, T extends AbstractTransition, F extends AbstractFlowRelation, M extends AbstractMarking, S extends Object, N extends AbstractPetriNet, G extends AbstractPNGraphics> AbstractGraphicalPN parse(File file, PNParsingFormat format) throws IOException, ParserException { validateFile(file); Validate.notNull(format); PNParserInterface parser = getParser(file, format); return parser.parse(file); } /** * Parses the given file with the parser respective to the file extension. * * @param

Place type * @param Transition type * @param Flow relation type * @param Marking type * @param Node value type * @param Petri net type * @param Petri net graphics type * @param fileName File to parse * @param format Format to parse from * @return A {@link AbstractGraphicalPN} * @throws IOException If the file can't be found or read * @throws ParserException For exceptions caused by the parsing */ public static synchronized

, T extends AbstractTransition, F extends AbstractFlowRelation, M extends AbstractMarking, S extends Object, N extends AbstractPetriNet, G extends AbstractPNGraphics> AbstractGraphicalPN parse(String fileName, PNParsingFormat format) throws IOException, ParserException { Validate.notNull(fileName); return PNParsing.parse(prepareFile(fileName), format); } private static File prepareFile(String fileName) throws IOException { File file = new File(fileName); validateFile(file); return file; } private static void validateFile(File file) throws IOException { if (!file.exists()) { throw new IOException("I/O Error on opening file: File does not exist!"); } if (file.isDirectory()) { throw new IOException("I/O Error on opening file: File is a directory!"); } if (!file.canRead()) { throw new IOException("I/O Error on opening file: Unable to read file!"); } } /** * @param file File to parse * @param format Format to parse from * @return Returns the for the file's extension suitable parser or * null if no suitable parser could be found * @throws ParserException If the file can't be found */ @SuppressWarnings("rawtypes") public static synchronized PNParserInterface getParser(File file, PNParsingFormat format) throws ParserException { switch (format) { case PNML: return new PNMLParser(); case PETRIFY: return new PetrifyParser(); } throw new ParserException(ErrorCode.UNSUPPORTED_FORMAT); } public static PNParsingFormat guessFormat(File file) { return guessFormat(file.getName()); } public static PNParsingFormat guessFormat(String file) { for (PNParsingFormat format : PNParsingFormat.values()) { if (file.endsWith(format.getFileFormat().getFileExtension().toUpperCase())) { return format; } else if (file.endsWith(format.getFileFormat().getFileExtension().toLowerCase())) { return format; } } return null; } }