/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The Regression Test is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package rbsla.rmltoprova;

import org.jdom.*;
import java.util.*;

/**
 * Clause
 * 
 * Translates RuleML 0.86 Clauses (rules, facts and queries) into Prova
 * 
 * @author <A HREF="mailto:adrian.paschke@gmx.de">Adrian Paschke</A>
 * @version 0.1 <1 Feb 2005>
 * @since 0.1
 * @deprecated Since RuleML 0.87 and RBSLA 0.1 XSLT Stylesheets are provided for translation
 */

public class Clause
{
    private Element rule;
    private RuleHead head;
    private boolean hasHead;
    private RuleBody body;
    private boolean hasBody;
	private boolean isQuery;
	private HashSet appRuleSet;
	private String ruleIdentity;
	private Namespace ns;

	public Clause(Element _rule)
    {

    	rule = _rule;
    	ns=rule.getNamespace();
		
		if (rule.getName().equals("query")) isQuery=true;
		else isQuery=false;
		
        try {ruleIdentity = rule.getChild("oid",ns).getValue();
		} catch (Exception ex) {}
		        
		
        try
        {
            hasHead = true;
            head= new RuleHead(rule.getChild("_head",ns).getChild("atom",ns));
        }
        catch(Exception e)
        {
            hasHead = false;
        }
        try
        {
            hasBody = true;
			try {
				body = new RuleBody(rule.getChild("_body",ns).getChild("and",ns),isQuery);
			} catch (Exception e) {
				body = new RuleBody(rule.getChild("_body",ns).getChild("atom",ns),isQuery);
			}
        }
        catch(Exception e)
        {
            hasBody = false;
        }
		
       
    }

    public String toString()
    {
        StringBuffer result = new StringBuffer();
		if (ruleIdentity!=null ) result.append("%" + ruleIdentity + "\n");
        if(hasHead)
        {
            result.append(head.toString());
        }
        if(hasBody)
        {
            result.append(body.toString());
        }

        result.append(".");
        return result.toString();
    }

}

class RuleHead
{
    private Atom atom;

    RuleHead(Element _atom)
    {
        atom = new Atom(_atom);
    }

    public String toString()
    {
        return atom.toString();
    }
}

//Body DTD as <!ELEMENT And (Atom)*>
class RuleBody
{
    private Element and;
    private List atomElements;
    private List atomObjects;
	private boolean isQuery;
	private Namespace ns;

    RuleBody(Element _and, boolean _isQuery)
    {
		isQuery=_isQuery;
        and = _and;
        ns = and.getNamespace();
        atomObjects = new ArrayList();
		if (and.getName().equals("atom")) {
			atomElements = new ArrayList();
			atomElements.add(and);
		} else atomElements = and.getChildren("atom",ns);
        for(int n = 0; n < atomElements.size(); n++)
        {
            Element atom = (Element)atomElements.get(n);
            atomObjects.add(new Atom(atom));
        }
    }

    public String toString()
    {
        StringBuffer sb;
		if (isQuery) sb = new StringBuffer(":-solve("); 
		else sb=new StringBuffer(" :- ");
		
        for(int n = 0; n < atomObjects.size(); n++)
        {
            Atom atom = (Atom)atomObjects.get(n);
            sb.append(atom.toString());
            if(n != atomObjects.size() - 1)
            {
                sb.append(" , ");
            }
        }
		
		if (isQuery) sb.append(")");
        
        return sb.toString();
    }

}
