-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
509 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/////////////////////////////////////////////////////////////////////////// | ||
// __ _ _ ________ // | ||
// / / ____ ____ _(_)____/ | / / ____/ // | ||
// / / / __ \/ __ `/ / ___/ |/ / / __ // | ||
// / /___/ /_/ / /_/ / / /__/ /| / /_/ / // | ||
// /_____/\____/\__, /_/\___/_/ |_/\____/ // | ||
// /____/ // | ||
// // | ||
// The Next Generation Logic Library // | ||
// // | ||
/////////////////////////////////////////////////////////////////////////// | ||
// // | ||
// Copyright 2015-2016 Christoph Zengler // | ||
// // | ||
// Licensed under the Apache License, Version 2.0 (the "License"); // | ||
// you may not use this file except in compliance with the License. // | ||
// You may obtain a copy of the License at // | ||
// // | ||
// http://www.apache.org/licenses/LICENSE-2.0 // | ||
// // | ||
// Unless required by applicable law or agreed to in writing, software // | ||
// distributed under the License is distributed on an "AS IS" BASIS, // | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // | ||
// implied. See the License for the specific language governing // | ||
// permissions and limitations under the License. // | ||
// // | ||
/////////////////////////////////////////////////////////////////////////// | ||
|
||
package org.logicng.io.parsers; | ||
|
||
import org.logicng.formulas.Formula; | ||
import org.logicng.formulas.FormulaFactory; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Super class for a formula parser. | ||
* @version 1.2 | ||
* @since 1.2 | ||
*/ | ||
public abstract class FormulaParser { | ||
|
||
private final FormulaFactory f; | ||
|
||
/** | ||
* Constructor. | ||
* @param f the formula factory | ||
*/ | ||
public FormulaParser(FormulaFactory f) { | ||
this.f = f; | ||
} | ||
|
||
/** | ||
* Parses and returns a given input stream. | ||
* @param inputStream an input stream | ||
* @return the {@link Formula} representation of this stream | ||
* @throws ParserException if there was a problem with the input stream | ||
*/ | ||
public abstract Formula parse(final InputStream inputStream) throws ParserException; | ||
|
||
/** | ||
* Parses and returns a given string. | ||
* @param in a string | ||
* @return the {@link Formula} representation of this string | ||
* @throws ParserException if the string was not a valid formula | ||
*/ | ||
public Formula parse(final String in) throws ParserException { | ||
if (in == null) | ||
return f.verum(); | ||
return this.parse(new ByteArrayInputStream(in.getBytes())); | ||
} | ||
|
||
/** | ||
* Returns the factory of this parser. | ||
* @return the factory of this parser | ||
*/ | ||
public FormulaFactory factory() { | ||
return this.f; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.getClass().getSimpleName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/org/logicng/io/readers/FormulaReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/////////////////////////////////////////////////////////////////////////// | ||
// __ _ _ ________ // | ||
// / / ____ ____ _(_)____/ | / / ____/ // | ||
// / / / __ \/ __ `/ / ___/ |/ / / __ // | ||
// / /___/ /_/ / /_/ / / /__/ /| / /_/ / // | ||
// /_____/\____/\__, /_/\___/_/ |_/\____/ // | ||
// /____/ // | ||
// // | ||
// The Next Generation Logic Library // | ||
// // | ||
/////////////////////////////////////////////////////////////////////////// | ||
// // | ||
// Copyright 2015-2016 Christoph Zengler // | ||
// // | ||
// Licensed under the Apache License, Version 2.0 (the "License"); // | ||
// you may not use this file except in compliance with the License. // | ||
// You may obtain a copy of the License at // | ||
// // | ||
// http://www.apache.org/licenses/LICENSE-2.0 // | ||
// // | ||
// Unless required by applicable law or agreed to in writing, software // | ||
// distributed under the License is distributed on an "AS IS" BASIS, // | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // | ||
// implied. See the License for the specific language governing // | ||
// permissions and limitations under the License. // | ||
// // | ||
/////////////////////////////////////////////////////////////////////////// | ||
|
||
package org.logicng.io.readers; | ||
|
||
import org.logicng.formulas.Formula; | ||
import org.logicng.formulas.FormulaFactory; | ||
import org.logicng.io.parsers.FormulaParser; | ||
import org.logicng.io.parsers.ParserException; | ||
import org.logicng.io.parsers.PropositionalParser; | ||
import org.logicng.io.parsers.PseudoBooleanParser; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.LinkedHashSet; | ||
|
||
/** | ||
* A reader for formulas. | ||
* <p></p> | ||
* Reads a formula from an input file. If the file has more than one line, the lines will be co-joined. | ||
* @version 1.2 | ||
* @since 1.2 | ||
*/ | ||
public final class FormulaReader { | ||
|
||
/** | ||
* Private constructor. | ||
*/ | ||
private FormulaReader() { | ||
// Intentionally left empty. | ||
} | ||
|
||
/** | ||
* Reads a given file and returns the contained propositional formula. | ||
* @param fileName the file name | ||
* @param f the formula factory | ||
* @return the parsed formula | ||
* @throws IOException if there was a problem reading the file | ||
* @throws ParserException if there was a problem parsing the formula | ||
*/ | ||
public static Formula readPropositionalFormula(final String fileName, final FormulaFactory f) throws IOException, ParserException { | ||
return read(new File(fileName), new PropositionalParser(f)); | ||
} | ||
|
||
/** | ||
* Reads a given file and returns the contained propositional formula. | ||
* @param file the file | ||
* @param f the formula factory | ||
* @return the parsed formula | ||
* @throws IOException if there was a problem reading the file | ||
* @throws ParserException if there was a problem parsing the formula | ||
*/ | ||
public static Formula readPropositionalFormula(final File file, final FormulaFactory f) throws IOException, ParserException { | ||
return read(file, new PropositionalParser(f)); | ||
} | ||
|
||
/** | ||
* Reads a given file and returns the contained pseudo-Boolean formula. | ||
* @param fileName the file name | ||
* @param f the formula factory | ||
* @return the parsed formula | ||
* @throws IOException if there was a problem reading the file | ||
* @throws ParserException if there was a problem parsing the formula | ||
*/ | ||
public static Formula readPseudoBooleanFormula(final String fileName, final FormulaFactory f) throws IOException, ParserException { | ||
return read(new File(fileName), new PseudoBooleanParser(f)); | ||
} | ||
|
||
/** | ||
* Reads a given file and returns the contained pseudo-Boolean formula. | ||
* @param file the file | ||
* @param f the formula factory | ||
* @return the parsed formula | ||
* @throws IOException if there was a problem reading the file | ||
* @throws ParserException if there was a problem parsing the formula | ||
*/ | ||
public static Formula readPseudoBooleanFormula(final File file, final FormulaFactory f) throws IOException, ParserException { | ||
return read(file, new PseudoBooleanParser(f)); | ||
} | ||
|
||
/** | ||
* Internal read function. | ||
* @param file the file | ||
* @param parser the parser | ||
* @return the parsed formula | ||
* @throws IOException if there was a problem reading the file | ||
* @throws ParserException if there was a problem parsing the formula | ||
*/ | ||
private static Formula read(final File file, final FormulaParser parser) throws IOException, ParserException { | ||
try (final BufferedReader br = new BufferedReader(new FileReader(file))) { | ||
final LinkedHashSet<Formula> ops = new LinkedHashSet<>(); | ||
while (br.ready()) | ||
ops.add(parser.parse(br.readLine())); | ||
return parser.factory().and(ops); | ||
} | ||
} | ||
} |
Oops, something went wrong.