/**
 * Copyright (c) 2017-2018, the Alpha Team.
 * All rights reserved.
 * 
 * Additional changes made by Siemens.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2) Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package at.ac.tuwien.kr.alpha.grounder.atoms;

import at.ac.tuwien.kr.alpha.common.Predicate;
import at.ac.tuwien.kr.alpha.common.atoms.Atom;
import at.ac.tuwien.kr.alpha.common.atoms.VariableNormalizableAtom;
import at.ac.tuwien.kr.alpha.common.terms.IntervalTerm;
import at.ac.tuwien.kr.alpha.common.terms.Term;
import at.ac.tuwien.kr.alpha.grounder.Substitution;

import java.util.Arrays;
import java.util.List;

import static at.ac.tuwien.kr.alpha.Util.join;
import static at.ac.tuwien.kr.alpha.Util.oops;

/**
 * Helper for treating IntervalTerms in rules.
 *
 * Each IntervalTerm is replaced by a variable and this special IntervalAtom is added to the rule body for generating all bindings of the variable.
 *
 * The first term of this atom is an IntervalTerm while the second term is any Term (if it is a VariableTerm, this will bind to all elements of the interval,
 * otherwise it is a simple check whether the Term is a ConstantTerm<Integer> with the Integer being inside the interval. Copyright (c) 2017, the Alpha Team.
 */
public class IntervalAtom extends Atom implements VariableNormalizableAtom {
	private static final Predicate PREDICATE = Predicate.getInstance("_interval", 2, true);

	private final List<Term> terms;

	public IntervalAtom(IntervalTerm intervalTerm, Term intervalRepresentingVariable) {
		this.terms = Arrays.asList(intervalTerm, intervalRepresentingVariable);
	}

	@Override
	public Predicate getPredicate() {
		return PREDICATE;
	}

	@Override
	public List<Term> getTerms() {
		return terms;
	}

	@Override
	public boolean isGround() {
		for (Term t : this.terms) {
			if (!t.isGround()) {
				return false;
			}
		}
		return true;
	}

	@Override
	public IntervalLiteral toLiteral(boolean positive) {
		if (!positive) {
			throw oops("IntervalLiteral cannot be negated");
		}
		return new IntervalLiteral(this);
	}

	@Override
	public IntervalLiteral toLiteral() {
		return toLiteral(true);
	}

	@Override
	public String toString() {
		return join(PREDICATE.getName() + "(", terms, ")");
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		IntervalAtom that = (IntervalAtom) o;

		return terms.equals(that.terms);
	}

	@Override
	public int hashCode() {
		return terms.hashCode();
	}

	@Override
	public IntervalAtom substitute(Substitution substitution) {
		return new IntervalAtom((IntervalTerm) terms.get(0).substitute(substitution), terms.get(1).substitute(substitution));
	}

	@Override
	public IntervalAtom normalizeVariables(String prefix, int counterStartingValue) {
		List<Term> renamedTerms = Term.renameTerms(terms, prefix, counterStartingValue);
		return new IntervalAtom((IntervalTerm) renamedTerms.get(0), renamedTerms.get(1));
	}

	@Override
	public Atom withTerms(List<Term> terms) {
		throw new UnsupportedOperationException("IntervalAtoms do not support setting ofd terms!");
	}
}

@@ -1468,16 +1468,160 @@
 atoms;%0A%0A
+import static at.ac.tuwien.kr.alpha.Util.join;%0Aimport static at.ac.tuwien.kr.alpha.Util.oops;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0A
 import a
@@ -1936,152 +1936,8 @@
 n;%0A%0A
-import java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport static at.ac.tuwien.kr.alpha.Util.join;%0Aimport static at.ac.tuwien.kr.alpha.Util.oops;%0A%0A
 /**%0A
@@ -2103,16 +2103,19 @@
 nerating
+%0A *
  all bin
@@ -2259,16 +2259,19 @@
 his will
+%0A *
  bind to
@@ -2300,19 +2300,16 @@
 nterval,
-%0A *
  otherwi
@@ -2376,24 +2376,27 @@
 nteger%3E with
+%0A *
  the Integer
@@ -2422,16 +2422,23 @@
 nterval.
+%0A * %0A *
  Copyrig
@@ -4363,17 +4363,16 @@
 tting of
-d
  terms!%22

improve javadoc formatting