Skip to content

Commit

Permalink
Added formula writers and readers
Browse files Browse the repository at this point in the history
  • Loading branch information
czengler committed Sep 23, 2016
1 parent b1296fd commit ca4588b
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 63 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/logicng/formulas/FormulaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,17 @@ public String string(final Formula formula) {
*/
public String string(final Formula formula, final FormulaStringRepresentation stringRepresentation) {
return stringRepresentation.toString(formula);
} @Override
}

/**
* Returns the formula formatter of this factory.
* @return the formula formatter of this factory
*/
public FormulaStringRepresentation stringRepresentation() {
return this.stringRepresentation;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(this.name).append("\n");
Expand Down Expand Up @@ -1058,8 +1068,4 @@ public boolean equals(final Object other) {
return false;
}
}




}
86 changes: 86 additions & 0 deletions src/main/java/org/logicng/io/parsers/FormulaParser.java
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();
}
}
34 changes: 5 additions & 29 deletions src/main/java/org/logicng/io/parsers/PropositionalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.logicng.formulas.Formula;
import org.logicng.formulas.FormulaFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -58,12 +57,11 @@
* <li>can only contain alphanumerical character, or {@code _}</li>
* <li>{@code @} is only allowed at the beginning of the variable name and is reserved for special internal variables</li>
* </ul>
* @version 1.0
* @version 1.2
* @since 1.0
*/
public final class PropositionalParser {
public final class PropositionalParser extends FormulaParser {

private final FormulaFactory f;
private final PropositionalLexer lexer;
private final LogicNGPropositionalParser parser;

Expand All @@ -72,7 +70,7 @@ public final class PropositionalParser {
* @param f the formula factory
*/
public PropositionalParser(final FormulaFactory f) {
this.f = f;
super(f);
ANTLRInputStream input = new ANTLRInputStream();
this.lexer = new PropositionalLexer(input);
CommonTokenStream tokens = new CommonTokenStream(this.lexer);
Expand All @@ -83,13 +81,8 @@ public PropositionalParser(final FormulaFactory f) {
this.parser.setErrorHandler(new BailErrorStrategy());
}

/**
* 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 Formula parse(InputStream inputStream) throws ParserException {
@Override
public Formula parse(final InputStream inputStream) throws ParserException {
try {
ANTLRInputStream input = new ANTLRInputStream(inputStream);
this.lexer.setInputStream(input);
Expand All @@ -104,21 +97,4 @@ public Formula parse(InputStream inputStream) throws ParserException {
throw new ParserException("Lexer exception when parsing the formula.", e);
}
}

/**
* 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()));
}

@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
34 changes: 5 additions & 29 deletions src/main/java/org/logicng/io/parsers/PseudoBooleanParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.logicng.formulas.Formula;
import org.logicng.formulas.FormulaFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -68,12 +67,11 @@
* <p>
* A valid pseudo Boolean expression is of the form {@code c_1 * l_1 + ... + c_n * l_n R k} where the {@code c_i} are coefficients,
* {@code l_i} are literals, and {@code R} is one of {@code =, >, >=, <, <=}.
* @version 1.0
* @version 1.2
* @since 1.0
*/
public final class PseudoBooleanParser {
public final class PseudoBooleanParser extends FormulaParser {

private final FormulaFactory f;
private final PseudoBooleanLexer lexer;
private final LogicNGPseudoBooleanParser parser;

Expand All @@ -82,7 +80,7 @@ public final class PseudoBooleanParser {
* @param f the formula factory
*/
public PseudoBooleanParser(final FormulaFactory f) {
this.f = f;
super(f);
ANTLRInputStream input = new ANTLRInputStream();
this.lexer = new PseudoBooleanLexer(input);
CommonTokenStream tokens = new CommonTokenStream(this.lexer);
Expand All @@ -93,13 +91,8 @@ public PseudoBooleanParser(final FormulaFactory f) {
this.parser.setErrorHandler(new BailErrorStrategy());
}

/**
* 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 Formula parse(InputStream inputStream) throws ParserException {
@Override
public Formula parse(final InputStream inputStream) throws ParserException {
try {
ANTLRInputStream input = new ANTLRInputStream(inputStream);
this.lexer.setInputStream(input);
Expand All @@ -114,21 +107,4 @@ public Formula parse(InputStream inputStream) throws ParserException {
throw new ParserException("Lexer exception when parsing the formula.", e);
}
}

/**
* 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()));
}

@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
124 changes: 124 additions & 0 deletions src/main/java/org/logicng/io/readers/FormulaReader.java
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);
}
}
}
Loading

0 comments on commit ca4588b

Please sign in to comment.