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

de.invation.code.toval.math.logic.Clause Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.math.logic;


import java.util.Comparator;
import java.util.Iterator;

import de.invation.code.toval.types.HashList;



public class Clause extends HashList implements Comparator{
	
	private static final long serialVersionUID = 1L;
	
	public Clause(){}
	
	public Clause(Literal l){
		add(l);
	}
	
	public Literal getFirst(){
		if(!isEmpty()){
			return super.get(0);
		} else {
			return null;
		}
	}
	
	public HashList getItems(){
		HashList set = new HashList();
		for(Iterator iter=iterator(); iter.hasNext();)
			set.add(iter.next().getItem());
		return set;
	}
	
	public Clause union(Clause k){
		Clause nk = new Clause();
		nk.addAll(k);
		nk.addAll(this);
		return nk;
	}
	
	public boolean containsItem(Object item){
		for(Iterator iter=iterator(); iter.hasNext();)
			if(item.equals(iter.next().getItem()))
				return true;
		return false;
	}
	
	/**
	 * Tries to accomplish a resolution along the variable item.
	 * @param clause
	 * @param item
	 */
	public ResolveResult resolve(Clause clause, Object item){
		Literal lPos = new Literal(item, true);
		Literal lNeg = new Literal(item, false);
		if( (contains(lPos) && clause.contains(lNeg)) || (contains(lNeg) && clause.contains(lPos)) ){
			Clause resolvent = union(clause);
			resolvent.remove(lPos);
			resolvent.remove(lNeg);
			return new ResolveResult(resolvent, item);
		}
		return null;
	}
	
	public boolean hasTwinLiterals(){
		for(int i=0; i i = iterator(); i.hasNext();){
				s += i.next().toString();
				if(i.hasNext()){
					s += ",";
				}
			}
			s += "}";
			return s;
		}
	}


}