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

fr.boreal.model.formula.api.CompoundFOFormula Maven / Gradle / Ivy

The newest version!
package fr.boreal.model.formula.api;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

import fr.boreal.model.logicalElements.api.Atom;
import fr.boreal.model.logicalElements.api.Constant;
import fr.boreal.model.logicalElements.api.Literal;
import fr.boreal.model.logicalElements.api.Predicate;
import fr.boreal.model.logicalElements.api.Variable;

/**
 * Abstract implementation for compound formula
 * 
 * @author Florent Tornil
 * 
 */
public abstract non-sealed class CompoundFOFormula implements FOFormula {

	private final Collection subformulas;

	private Set flat;

	/////////////////////////////////////////////////
	// Constructors
	/////////////////////////////////////////////////

	/**
	 * Constructor using sub formulas
	 * @param subformulas the sub formulas
	 */
	public CompoundFOFormula(Collection subformulas) {
		this.subformulas = subformulas;
	}

	/////////////////////////////////////////////////
	// Getters
	/////////////////////////////////////////////////

	/**
	 * @return the string representation of the connector
	 */
	public abstract String getConnectorRepresentation();

	/**
	 * @return the sub formulas
	 */
	public Collection getSubElements() {
		return this.subformulas;
	}

	@Override
	public Set asAtomSet() {
		if (this.flat == null) {
			this.flat = this.getSubElements().stream().flatMap(f -> f.asAtomSet().stream())
					.collect(Collectors.toSet());
		}
		return this.flat;
	}
	
	private Set predicates = null;
	@Override
	public Set getPredicates() {
		if (this.predicates == null) {
			this.predicates = this.getSubElements().stream().flatMap(f -> f.getPredicates().stream())
					.collect(Collectors.toSet());
		}
		return this.predicates;
	}
	
	private Set variables = null;
	@Override
	public Set getVariables() {
		if (this.variables == null) {
			this.variables = this.getSubElements().stream().flatMap(f -> f.getVariables().stream())
					.collect(Collectors.toUnmodifiableSet());
		}
		return this.variables;
	}
	
	private Set constants = null;
	@Override
	public Set getConstants() {
		if (this.constants == null) {
			this.constants = this.getSubElements().stream().flatMap(f -> f.getConstants().stream())
					.collect(Collectors.toUnmodifiableSet());
		}
		return this.constants;
	}
	
	private Set> literals = null;
	@Override
	public Set> getLiterals() {
		if (this.literals == null) {
			this.literals = this.getSubElements().stream().flatMap(f -> f.getLiterals().stream())
					.collect(Collectors.>toUnmodifiableSet());
		}
		return this.literals;
	}
	
	/////////////////////////////////////////////////
	// Object methods
	/////////////////////////////////////////////////

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (FOFormula q : this.getSubElements()) {
			if (!first) {
				sb.append(" ");
				sb.append(this.getConnectorRepresentation());
				sb.append(" ");
			}
			sb.append(q.toString());
			first = false;
		}
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy