Skip to content

Commit

Permalink
added javadoc + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rouven-walter committed Apr 11, 2019
1 parent 9bc4a74 commit 67f0dd2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/org/logicng/util/FormulaHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
import java.util.SortedSet;
import java.util.TreeSet;

/**
* A class which contains utility methods for {@link Formula} objects.
*/
public class FormulaHelper {

/**
* Private empty constructor. Class only contains static utility methods.
*/
private FormulaHelper() {
// Intentionally left empty
}

/**
* Returns all variables occurring in the given formulas.
* @param formulas formulas
* @return all variables occurring in the given formulas
*/
public static SortedSet<Variable> variables(Formula... formulas) {
SortedSet<Variable> variables = new TreeSet<>();
for (Formula f : formulas) {
Expand All @@ -22,6 +33,11 @@ public static SortedSet<Variable> variables(Formula... formulas) {
return variables;
}

/**
* Returns all variables occurring in the given formulas.
* @param formulas formulas
* @return all variables occurring in the given formulas
*/
public static SortedSet<Variable> variables(Collection<? extends Formula> formulas) {
SortedSet<Variable> variables = new TreeSet<>();
for (Formula f : formulas) {
Expand All @@ -30,6 +46,11 @@ public static SortedSet<Variable> variables(Collection<? extends Formula> formul
return variables;
}

/**
* Returns all literals occurring in the given formulas.
* @param formulas formulas
* @return all literals occurring in the given formulas
*/
public static SortedSet<Literal> literals(Formula... formulas) {
SortedSet<Literal> literals = new TreeSet<>();
for (Formula f : formulas) {
Expand All @@ -38,6 +59,11 @@ public static SortedSet<Literal> literals(Formula... formulas) {
return literals;
}

/**
* Returns all literals occurring in the given formulas.
* @param formulas formulas
* @return all literals occurring in the given formulas
*/
public static SortedSet<Literal> literals(Collection<? extends Formula> formulas) {
SortedSet<Literal> literals = new TreeSet<>();
for (Formula f : formulas) {
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/org/logicng/util/FormulaHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
///////////////////////////////////////////////////////////////////////////
// __ _ _ ________ //
// / / ____ ____ _(_)____/ | / / ____/ //
// / / / __ \/ __ `/ / ___/ |/ / / __ //
// / /___/ /_/ / /_/ / / /__/ /| / /_/ / //
// /_____/\____/\__, /_/\___/_/ |_/\____/ //
// /____/ //
// //
// The Next Generation Logic Library //
// //
///////////////////////////////////////////////////////////////////////////
// //
// Copyright 2015-20xx 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.util;

import org.junit.Test;
import org.logicng.formulas.F;

import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link FormulaHelper}.
*/
public class FormulaHelperTest {

@Test
public void testVariables() {
assertThat(FormulaHelper.variables(F.TRUE)).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.variables(F.FALSE)).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.variables(F.A)).isEqualTo(new TreeSet<>(Collections.singletonList(F.A)));
assertThat(FormulaHelper.variables(F.NA)).isEqualTo(new TreeSet<>(Collections.singletonList(F.A)));
assertThat(FormulaHelper.variables(F.IMP1, F.IMP2, F.IMP3)).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.X, F.Y)));
assertThat(FormulaHelper.variables(F.IMP1, F.Y)).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.Y)));

assertThat(FormulaHelper.variables(Arrays.asList(F.TRUE, F.FALSE))).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.variables(Arrays.asList(F.IMP1, F.IMP2, F.IMP3))).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.X, F.Y)));
assertThat(FormulaHelper.variables(Arrays.asList(F.IMP1, F.Y))).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.Y)));
}

@Test
public void testLiterals() {
assertThat(FormulaHelper.literals(F.TRUE)).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.literals(F.FALSE)).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.literals(F.A)).isEqualTo(new TreeSet<>(Collections.singletonList(F.A)));
assertThat(FormulaHelper.literals(F.NA)).isEqualTo(new TreeSet<>(Collections.singletonList(F.NA)));
assertThat(FormulaHelper.literals(F.IMP1, F.IMP2, F.IMP3)).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.X, F.Y, F.NA, F.NB)));
assertThat(FormulaHelper.literals(F.IMP1, F.NY)).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.NY)));

assertThat(FormulaHelper.literals(Arrays.asList(F.TRUE, F.FALSE))).isEqualTo(new TreeSet<>());
assertThat(FormulaHelper.literals(Arrays.asList(F.IMP1, F.IMP2, F.IMP3))).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.X, F.Y, F.NA, F.NB)));
assertThat(FormulaHelper.literals(Arrays.asList(F.IMP1, F.NY))).isEqualTo(
new TreeSet<>(Arrays.asList(F.A, F.B, F.NY)));
}
}

0 comments on commit 67f0dd2

Please sign in to comment.