/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */


package edu.stanford.smi.protegex.owl.swrl.bridge.impl;

import edu.stanford.smi.protegex.owl.swrl.bridge.*;
import edu.stanford.smi.protegex.owl.swrl.bridge.exceptions.*;
import edu.stanford.smi.protegex.owl.swrl.model.SWRLClassAtom;
import edu.stanford.smi.protegex.owl.swrl.model.SWRLVariable;

/*
** Class representing a SWRL class atom
*/
public class ClassAtomImpl extends AtomImpl implements ClassAtom
{
  private AtomArgument argument1;
  private String className, prefixedClassName;
  
  public ClassAtomImpl(SWRLClassAtom atom) throws OWLFactoryException
  {
    className = (atom.getClassPredicate() != null) ? atom.getClassPredicate().getName() : null;
    prefixedClassName = (atom.getClassPredicate() != null) ? atom.getClassPredicate().getPrefixedName() : null;

    if (className == null) throw new OWLFactoryException("empty class name in SWRLClassAtom: " + atom);

    addReferencedClassName(className);
    
    if (atom.getArgument1() instanceof SWRLVariable) {
      SWRLVariable variable = (SWRLVariable)atom.getArgument1();
      AtomArgument argument = OWLFactory.createVariableAtomArgument(variable.getName(), variable.getPrefixedName());
      addReferencedVariableName(variable.getName());
      argument1 = argument;
    } else if (atom.getArgument1() instanceof edu.stanford.smi.protegex.owl.model.OWLIndividual) {
      OWLIndividual argument = OWLFactory.createOWLIndividual((edu.stanford.smi.protegex.owl.model.OWLIndividual)atom.getArgument1());
      addReferencedIndividualName(argument.getIndividualName());
      argument1 = argument;
    } else if (atom.getArgument1() instanceof edu.stanford.smi.protegex.owl.model.OWLNamedClass) {
      OWLClass argument = OWLFactory.createOWLClass((edu.stanford.smi.protegex.owl.model.OWLNamedClass)atom.getArgument1());
      addReferencedClassName(argument.getClassName());
      argument1 = argument;
    } else if (atom.getArgument1() instanceof edu.stanford.smi.protegex.owl.model.OWLObjectProperty) {
      OWLObjectProperty argument = OWLFactory.createOWLObjectProperty((edu.stanford.smi.protegex.owl.model.OWLObjectProperty)atom.getArgument1());
      addReferencedPropertyName(argument.getPropertyName());
      argument1 = argument;
    } else if (atom.getArgument1() instanceof edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty) {
      OWLDatatypeProperty argument = OWLFactory.createOWLDatatypeProperty((edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty)atom.getArgument1());
      addReferencedPropertyName(argument.getPropertyName());
      argument1 = argument;
    } else throw new OWLFactoryException("unexpected argument to class atom '" + atom.getBrowserText() + "'; expecting " +
                                         "variable or individual, got instance of '" + atom.getArgument1().getClass() + "'");
  } // ClassAtomImpl
  
  public String getClassName() { return className; }
  public String getPrefixedClassName() { return prefixedClassName; }
  public AtomArgument getArgument1() { return argument1; }

  public String toString() { return getPrefixedClassName() + "(" + getArgument1() + ")"; }
} // ClassAtomImpl

