/*
 * 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.*;

/**
 * DefeasibleClause
 * 
 * Translates RuleML 0.86 Defeasible Rules into ContractLog / Prova meta program
 * 
 * @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 DefeasibleClause
{
    private Element rule;
    private DefRuleHead head;
    private boolean hasHead;
    private DefRuleBody body;
    private boolean hasBody;
	private HashSet appRuleSet;
	private String ruleIdentity;

	public DefeasibleClause(Element _rule)
    {

    	rule = _rule;
		
		
        try {ruleIdentity = rule.getChild("oid").getValue();
		} catch (Exception ex) {}
		        
		
        try
        {
            hasHead = true;
            head= new DefRuleHead(rule.getChild("_head").getChild("atom"));
        }
        catch(Exception e)
        {
            hasHead = false;
        }
        try
        {
            hasBody = true;
			try {
				body = new DefRuleBody(rule.getChild("_body").getChild("and"),head.toString(), ruleIdentity);
			} catch (Exception e) {
				body = new DefRuleBody(rule.getChild("_body").getChild("atom"), head.toString(), ruleIdentity);
			}
        }
        catch(Exception e)
        {
            hasBody = false;
        }
		
       
    }

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

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

}

class DefRuleHead
{
    private Atom atom;

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

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

//Body DTD as <!ELEMENT And (Atom)*>
class DefRuleBody
{
    private Element and;
    private List atomElements;
    private List atomObjects;	
	private String head;
	private String oid;

    DefRuleBody(Element _and, String _head, String _oid)
    {
        head=_head;
		oid=_oid;
		and = _and;
        atomObjects = new ArrayList();
		if (and.getName().equals("atom")) {
			atomElements = new ArrayList();
			atomElements.add(and);
		} else atomElements = and.getChildren("atom");
        for(int n = 0; n < atomElements.size(); n++)
        {
            Element atom = (Element)atomElements.get(n);
            atomObjects.add(new Atom(atom));
        }
    }

    public String toString()
    {
        StringBuffer sb = new StringBuffer(" :- ");
		sb.append("testIntegrity("+head+"), ");
		
        for(int n = 0; n < atomObjects.size(); n++)
        {
            Atom atom = (Atom)atomObjects.get(n);
			sb.append("defeasible(");
            sb.append(atom.toString());
			sb.append(")");
            sb.append(", ");
          
        }		
		sb.append("not(defeated(\""+oid+"\", "+head+")).\r\n");
		sb.append("neg(blocked(defeasible("+head+"))) :- testIntegrity("+head+"), ");
		
		for(int m = 0; m < atomObjects.size(); m++)
		{
		        Atom atom = (Atom)atomObjects.get(m);
				sb.append("defeasible(");
		        sb.append(atom.toString());
				sb.append(")");
		        if(m!= atomObjects.size() - 1)
		        {
		            sb.append(", ");
		        }
		}
		sb.append(".\r\n");
		
		sb.append("body(defeasible("+head+")) :- ");
				
		for(int k = 0; k < atomObjects.size(); k++)
		{
		        Atom atom = (Atom)atomObjects.get(k);
				sb.append("defeasible(");
		        sb.append(atom.toString());
				sb.append(")");
		        if(k!= atomObjects.size() - 1)
		        {
		            sb.append(", ");
		        }
		}
		
        
        return sb.toString();
    }

}
