//KRSS2Parser by Olaf Noppens, based on KRSSParser by Matthew Horridge
options {
JAVA_UNICODE_ESCAPE=true;
    STATIC=false;
    JAVA_TEMPLATE_TYPE = "modern";
    EXCEPTIONS_SUPER_CLASS = "org.semanticweb.owlapi.io.OWLParserException";
    SUPPORT_CLASS_VISIBILITY_PUBLIC=false;
}

PARSER_BEGIN(KRSS2Parser)

package org.semanticweb.owlapi.krss2.parser;

import java.net.URISyntaxException;
import java.util.*;

import org.semanticweb.owlapi.io.XMLUtils;
import org.semanticweb.owlapi.krss1.parser.NameResolverStrategy;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

// Suppress warnings in auto-generated code
@SuppressWarnings("all")
public class KRSS2Parser {
    private OWLOntology ontology;
    private OWLDataFactory df;
    private Map<String, IRI> string2IRI;
    private boolean ignoreAnnotationsAndDeclarations = false;
    private String base;
     private NameResolverStrategy nameResolution;
    //all fields for the adaptive strategy
    private int adaptiveMaxRuns = 10;
    private boolean alwaysIRIs = false;
    private boolean alwaysNames = false;
    //
    private String[] splitted=new String[2];

    public KRSS2Parser setOntology(OWLOntology ontology)  {
        this.ontology = ontology;
        this.df = ontology.getOWLOntologyManager().getOWLDataFactory();
        this.string2IRI = new HashMap<String, IRI>();
        OWLOntologyID id = ontology.getOntologyID();
        if(!id.isAnonymous()) {
            this.base = id.getOntologyIRI().toString() + "#";
        }
        else {
            this.base = id.toString() + "#";
        }
        this.nameResolution = NameResolverStrategy.CHECK;
        return this;
    }

     protected void addAxiom(OWLAxiom ax) throws KRSS2OWLParserException {
        try {
            ontology.addAxiom(ax);
        }
        catch(OWLOntologyChangeException e) {
            throw new KRSS2OWLParserException(e);
        }
    }

     public IRI getIRI(final String s) {
            IRI iri = string2IRI.get(s);
            if (iri == null) {
                switch(nameResolution) {
                    case IRI: 
                        iri = IRI.create(s);
                        break;
                    case NAME: 
                        iri = IRI.create(base+s);
                        break;
                    case ADAPTIVE: 
                        if (adaptiveMaxRuns <=1) {
                            if (alwaysIRIs && !alwaysNames)
                                this.nameResolution = NameResolverStrategy.IRI;
                            else if (alwaysNames && !alwaysIRIs)
                                this.nameResolution = NameResolverStrategy.NAME;
                            else
                                this.nameResolution = NameResolverStrategy.CHECK;
                        }
                        adaptiveMaxRuns--;
                    case CHECK: 
                        String ns = XMLUtils.getNCNamePrefix(s);
                    	if (ns.equals("")) {
                        	iri = IRI.create(base + s);
                    	} else {
                        	iri = IRI.create(s);
                    	}
                        break;
                }
                string2IRI.put(s,iri);
            }
            return iri;
        }

     public void setNameResolution(NameResolverStrategy nameResolution) {
        this.nameResolution = nameResolution;
    }

    public void setIgnoreAnnotationsAndDeclarations(boolean b) {
        ignoreAnnotationsAndDeclarations = b;
    }
}

PARSER_END(KRSS2Parser)

SKIP: {" " | "\t" | "\r"| "\"" | "|" | "\\"|"\n"}
////////////////////////////////////////////////////////////////////////////////////////////
//
// COMMENTS
MORE: { <COMMENT_START: ";"> : IN_COMMENT }
<IN_COMMENT>SKIP: { <"\n"> : DEFAULT }
<IN_COMMENT>MORE: { <~[]> }

////////////////////////////////////////////////////////////////////////////////////////////
//
// IRIs
MORE: { <IRI_START: "<"> : IN_IRI }
<IN_IRI>TOKEN: { <IRI_END: ">"> : DEFAULT }
<IN_IRI>MORE: { <~[]> }
TOKEN[IGNORE_CASE]: { <TOP: "TOP" | "*TOP*" > }
TOKEN[IGNORE_CASE]: { <BOTTOM: "BOTTOM" | "*BOTTOM*" | "*bottom*"> }
TOKEN[IGNORE_CASE]: { <NIL: "NIL"> }
TOKEN[IGNORE_CASE]: { <TRUE: "t"> }
TOKEN[IGNORE_CASE]: { <OPENPAR: "("> }
TOKEN[IGNORE_CASE]: { <CLOSEPAR: ")"> }
TOKEN[IGNORE_CASE]: { <ENDTBOX: "end-tbox"> }
TOKEN[IGNORE_CASE]: { <ENDABOX: "end-abox"> }
TOKEN[IGNORE_CASE]: { <COMPOSE: "compose"> }
//concepts
TOKEN[IGNORE_CASE]: { <DEFINECONCEPT: "define-concept"> }
TOKEN[IGNORE_CASE]: { <DEFINEPRIMITIVECONCEPT: "define-primitive-concept"> }
TOKEN[IGNORE_CASE]: { <DISJOINT: "disjoint"> }
TOKEN[IGNORE_CASE]: { <EQUIVALENT: "equivalent"> }
TOKEN[IGNORE_CASE]: { <IMPLIES: "implies"> }
TOKEN[IGNORE_CASE]: { <AND: "AND"> }
TOKEN[IGNORE_CASE]: { <OR: "OR"> }
TOKEN[IGNORE_CASE]: { <NOT: "NOT"> }
TOKEN[IGNORE_CASE]: { <ALL: "ALL"> }
TOKEN[IGNORE_CASE]: { <ONEOF: "one-of"> }
TOKEN[IGNORE_CASE]: { <SOME: "SOME" > }
TOKEN[IGNORE_CASE]: { <ATLEAST: "AT-LEAST"> }
TOKEN[IGNORE_CASE]: { <ATMOST:  "AT-MOST"> }
TOKEN[IGNORE_CASE]: { <EXACTLY: "EXACTLY"> }
//roles
TOKEN[IGNORE_CASE]: { <DEFINEROLE: "define-role"> }
TOKEN[IGNORE_CASE]: { <DEFINEPRIMITIVEROLE: "define-primitive-role"> }
TOKEN[IGNORE_CASE]: { <DISJOINTROLES :"disjoint-roles"> }
TOKEN: { <DOMAIN_ATTRIBUTE: ":domain"> }
TOKEN: { <IMPLIESROLE: "implies-role"> }
TOKEN: { <INV: "inv"> }
TOKEN: { <INVERSE: "inverse"> }
TOKEN: { <INVERSE_ATTRIBUTE: ":inverse"> }
TOKEN: { <LEFT_IDENTITY_ATTRIBUTE: ":left-identity"> }
TOKEN[IGNORE_CASE]: { <PARENT: ":parent"> }
TOKEN[IGNORE_CASE]: { <PARENTS: ":parents"> }
TOKEN[IGNORE_CASE]: { <RANGE: "range"> }
TOKEN: { <RANGE_ATTRIBUTE: ":range"> }
TOKEN: { <REFLEXIVE_ATTRIBUTE : ":reflexive"> }
TOKEN: { <RIGHT_IDENTITY_ATTRIBUTE: ":right-identity"> }
TOKEN[IGNORE_CASE]: { <ROLESEQUIVALENT : "roles-equivalent"> }
TOKEN[IGNORE_CASE]: { <ROLE_INCLUSION : "role-inclusion"> }
TOKEN: { <SUBROLE: "subrole"> }
TOKEN: { <SYMMETRIC_ATTRIBUTE: ":symmetric"> }
TOKEN[IGNORE_CASE]: { <TRANSITIVE: "TRANSITIVE"> }
TOKEN: { <TRANSITIVE_ATTRIBUTE: ":transitive" | ":TRANSITIVE"> }
//individuals
TOKEN[IGNORE_CASE]: { <DEFINEINDIVIDUAL: "define-individual"> }
TOKEN[IGNORE_CASE]: { <DISTINCT: "distinct"> }
TOKEN[IGNORE_CASE]: { <EQUAL: "equal"> }
TOKEN[IGNORE_CASE]: { <INSTANCE: "instance"> }
TOKEN[IGNORE_CASE]: { <RELATED: "related"> }
TOKEN: { <DPOINT: ":"> }
TOKEN[IGNORE_CASE]: { <ENUM: "enum"> }
TOKEN: { <INT: ["0"-"9"](["0"-"9"])*> }
TOKEN: { <NAME: (<NCCHAR_FULL>)+> }
TOKEN: { <NCCHAR_FULL: <NCCHAR1> | "_" | "." | "-" | ["0"-"9"] | "\u00B7" | "%"> }
TOKEN: { <NCCHAR1: ["A"-"Z"] | ["a"-"z"] | [":"] | ["/"] | ["#"] | ["-"] | ["&"] | ["'"]
        | ["\u00C0" - "\u00D6"] | ["\u00D8"-"\u00F6"] | ["\u00F8"-"\u02FF"] | ["\u0370"-"\u037D"]
        | ["\u037F"-"\u1FFF"] | ["\u200C"-"\u200D"] | ["\u2070"-"\u218F"] | ["\u2C00"-"\u2FEF"]
        | ["\u3001"-"\uD7FF"] | ["\uF900"-"\uFFFF"]>
}
TOKEN: { <ERROR: ~[]> }

public void parse() throws KRSS2OWLParserException :
{ }
{ (LOOKAHEAD(2)TBoxStatement())* (LOOKAHEAD(2)<ENDTBOX>)? (LOOKAHEAD(2)ABoxStatement())* (LOOKAHEAD(2)<ENDABOX>)? <EOF> }

void TBoxStatement() throws KRSS2OWLParserException:
{ }
{(LOOKAHEAD(2)DefinePrimitiveConcept()| LOOKAHEAD(2)DefineConcept()| LOOKAHEAD(2)Implies()
| LOOKAHEAD(2)Equivalent()| LOOKAHEAD(2)Disjoint()| LOOKAHEAD(2)ComplexRoleInclusion()| LOOKAHEAD(2)DefinePrimitiveRole()
| LOOKAHEAD(2)DefineRole()| LOOKAHEAD(2)DisjointRoles()| LOOKAHEAD(2)RolesEquivalent()| LOOKAHEAD(2)ImpliesRole()
|LOOKAHEAD(2)Inverses()|LOOKAHEAD(2)Range()|LOOKAHEAD(2)Transitive() )
}

// Concepts

void DefinePrimitiveConcept() throws KRSS2OWLParserException:
{ OWLClassExpression subClass, superClass = null; }
{
      <OPENPAR><DEFINEPRIMITIVECONCEPT>subClass=ConceptName() (superClass=ConceptExpression())?<CLOSEPAR> {
        if (superClass == null) superClass = df.getOWLThing();
        addAxiom(df.getOWLSubClassOfAxiom(subClass, superClass));
      }
}

void DefineConcept() throws KRSS2OWLParserException:
{ OWLClassExpression clsA, clsB; }
{
    <OPENPAR><DEFINECONCEPT>clsA=ConceptName() clsB=ConceptExpression()<CLOSEPAR> {
        if (clsB == null) clsB = df.getOWLThing();
        addAxiom(df.getOWLEquivalentClassesAxiom(clsA, clsB));
    }
}

void Disjoint() throws KRSS2OWLParserException:
{ OWLClassExpression desc1, desc2; }
{ <OPENPAR><DISJOINT>desc1=ConceptExpression() desc2=ConceptExpression() <CLOSEPAR>{ addAxiom(df.getOWLDisjointClassesAxiom(desc1, desc2)); } }

void Equivalent() throws KRSS2OWLParserException:
{ OWLClassExpression desc1, desc2; }
{ <OPENPAR><EQUIVALENT>desc1=ConceptExpression() desc2=ConceptExpression() <CLOSEPAR>{ addAxiom(df.getOWLEquivalentClassesAxiom(desc1, desc2)); } }

void Implies() throws KRSS2OWLParserException :
{ OWLClassExpression subDescription, superDescription; }
{ <OPENPAR><IMPLIES>subDescription=ConceptExpression() superDescription = ConceptExpression() <CLOSEPAR>{ addAxiom( df.getOWLSubClassOfAxiom(subDescription, superDescription)); } }

// Roles

void DefineRole() throws KRSS2OWLParserException:
{ OWLObjectPropertyExpression propA, propB; }
{ <OPENPAR><DEFINEROLE>propA=RoleExpression() propB=RoleExpression()<CLOSEPAR> { addAxiom(df.getOWLEquivalentObjectPropertiesAxiom(propA, propB)); } }

void DefinePrimitiveRole() throws KRSS2OWLParserException :
{
    OWLObjectProperty subProp;
    OWLObjectProperty superProp;
    OWLClassExpression desc;
    Set<OWLObjectProperty> roles = new HashSet<OWLObjectProperty>();
    Set<OWLClassExpression> descs = new HashSet<OWLClassExpression>();
}
{ (<OPENPAR><DEFINEPRIMITIVEROLE>subProp=RoleName() { addAxiom( df.getOWLDeclarationAxiom(subProp)); } ( (Parent(subProp)) (LOOKAHEAD(2)(RightIdentity(subProp))|(LeftIdentity(subProp))) (Parents(subProp))(DomainAttribute(subProp))(RangeAttribute(subProp))(TransitiveAttribute(subProp))(SymmetricAttribute(subProp)) (ReflexiveAttribute(subProp))(InverseProperty(subProp)))<CLOSEPAR>) }

void RightIdentity(OWLObjectProperty r) throws KRSS2OWLParserException :
{ OWLObjectProperty s; }
{ (<RIGHT_IDENTITY_ATTRIBUTE>s=RoleName() { addAxiom(df.getOWLSubPropertyChainOfAxiom(java.util.Arrays.asList(r, s), r)); } )?}

void LeftIdentity(OWLObjectProperty r) throws KRSS2OWLParserException:
{ OWLObjectProperty s; }
{ (<LEFT_IDENTITY_ATTRIBUTE>s=RoleName() { addAxiom(df.getOWLSubPropertyChainOfAxiom(java.util.Arrays.asList(s, r), r));})? }

void InverseProperty(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{ OWLObjectPropertyExpression superProp; }
{ (<INVERSE_ATTRIBUTE>superProp=RoleExpression(){ addAxiom(df.getOWLInverseObjectPropertiesAxiom(subProp, superProp)); })? }

void DomainAttribute(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
      OWLClassExpression desc;
      Set<OWLClassExpression> descs = new HashSet<OWLClassExpression>();
}
{
  (<DOMAIN_ATTRIBUTE>( LOOKAHEAD(2)<OPENPAR>(desc=ConceptExpression(){if (desc != null) descs.add(desc);})+<CLOSEPAR>| (desc=ConceptExpression(){ descs.add(desc);})  )
  { for (OWLClassExpression eachDescription : descs) { addAxiom(df.getOWLObjectPropertyDomainAxiom(subProp, eachDescription)); } } )?
}

void RangeAttribute(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
      OWLClassExpression desc;
      Set<OWLClassExpression> descs = new HashSet<OWLClassExpression>();
}
{
  (<RANGE_ATTRIBUTE>( LOOKAHEAD(2)<OPENPAR>(desc=ConceptExpression(){descs.add(desc); })+<CLOSEPAR>|
  (desc=ConceptExpression(){ descs.add(desc);}))
  { for (OWLClassExpression eachDescription : descs) { addAxiom(df.getOWLObjectPropertyRangeAxiom(subProp, eachDescription)); } } )?
}

void TransitiveAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{ }
{ (<TRANSITIVE_ATTRIBUTE>(<NIL>|<TRUE>{ addAxiom(df.getOWLTransitiveObjectPropertyAxiom(property)); }))? }

void SymmetricAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{ }
{ (<SYMMETRIC_ATTRIBUTE>(<NIL>|<TRUE>{ addAxiom(df.getOWLSymmetricObjectPropertyAxiom(property)); }))? }

void ReflexiveAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{ }
{ (<REFLEXIVE_ATTRIBUTE>(<NIL>|<TRUE>{ addAxiom(df.getOWLReflexiveObjectPropertyAxiom(property)); }))? }

void Parents(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
    Set<OWLObjectProperty> roles;
    OWLObjectProperty superProp;
}
{
(<PARENTS>(<NIL>|<OPENPAR>roles=RoleNameSet()<CLOSEPAR>
{ for (OWLObjectProperty prop: roles) { addAxiom(df.getOWLSubObjectPropertyOfAxiom(subProp, prop)); }})
|(<PARENT>(<NIL>|(superProp=RoleName(){ addAxiom(df.getOWLSubObjectPropertyOfAxiom(subProp, superProp)); }))))?
}

void Parent(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{ OWLObjectProperty superProp; }
{ (superProp = RoleName() { addAxiom(df.getOWLSubObjectPropertyOfAxiom(subProp, superProp)); })? }

void DisjointRoles() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression exp1;
    OWLObjectPropertyExpression exp2;
}
{ <OPENPAR><DISJOINTROLES>exp1 =RoleExpression() exp2=RoleExpression()<CLOSEPAR>{ addAxiom(df.getOWLDisjointObjectPropertiesAxiom(exp1, exp2)); } }

void ImpliesRole() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression subProp;
    OWLObjectPropertyExpression superProp;
}
{ <OPENPAR><IMPLIESROLE>subProp=RoleExpression() superProp=RoleExpression()<CLOSEPAR>{ addAxiom(df.getOWLSubObjectPropertyOfAxiom(subProp, superProp)); } }

void Inverses() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop1;
    OWLObjectPropertyExpression prop2;
}
{ <OPENPAR><INVERSE>prop1=RoleExpression() prop2=RoleExpression()<CLOSEPAR>{ addAxiom(df.getOWLInverseObjectPropertiesAxiom(prop1, prop2)); } }

void RolesEquivalent() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression exp1;
    OWLObjectPropertyExpression exp2;
}
{ <OPENPAR><ROLESEQUIVALENT>exp1=RoleExpression() exp2=RoleExpression()<CLOSEPAR>{ addAxiom(df.getOWLEquivalentObjectPropertiesAxiom(exp1, exp2)); } }

void ComplexRoleInclusion() throws KRSS2OWLParserException :
{
    OWLObjectProperty superProp;
    List<OWLObjectPropertyExpression> chain;
}
{ <OPENPAR><ROLE_INCLUSION>chain = propertyChain() superProp=RoleName()<CLOSEPAR>{ addAxiom(df.getOWLSubPropertyChainOfAxiom(chain, superProp)); } }

List<OWLObjectPropertyExpression> propertyChain() throws KRSS2OWLParserException:
{
    List<OWLObjectPropertyExpression> chain = new ArrayList<OWLObjectPropertyExpression>();
    List<OWLObjectPropertyExpression> subChain;
    OWLObjectPropertyExpression prop;
}
{ <OPENPAR><COMPOSE>(prop=RoleExpression()){chain.add(prop);} (LOOKAHEAD(2)subChain = propertyChain(){chain.addAll(subChain);} | (prop=RoleExpression(){chain.add(prop);}))<CLOSEPAR> { return chain; } }

void Transitive()  throws KRSS2OWLParserException:
{ OWLObjectProperty prop; }
{ <OPENPAR><TRANSITIVE>prop=RoleName()<CLOSEPAR> { addAxiom( df.getOWLTransitiveObjectPropertyAxiom(prop)); } }

void Range() throws KRSS2OWLParserException :
{
    OWLObjectProperty prop;
    OWLClassExpression rng;
}
{ <OPENPAR><RANGE>prop=RoleName() rng=ConceptExpression()<CLOSEPAR> { addAxiom( df.getOWLObjectPropertyRangeAxiom(prop, rng)); } }

OWLClassExpression ConceptExpression() throws KRSS2OWLParserException :
{ OWLClassExpression desc; }
{ (LOOKAHEAD(2)desc=ConceptName() |LOOKAHEAD(2) desc=And() |LOOKAHEAD(2) desc=Or() |LOOKAHEAD(2) desc=Not()
    |LOOKAHEAD(2) desc=All() |LOOKAHEAD(2) desc=Some() |LOOKAHEAD(2) desc=AtLeast() |LOOKAHEAD(2) desc=AtMost()
    |LOOKAHEAD(2) desc=Exactly()) { return desc; }
}

OWLClassExpression ConceptName() throws KRSS2OWLParserException :
{ IRI iri; }
{ iri = Name() { return df.getOWLClass(iri); } }

Set<OWLClassExpression> ConceptSet() throws KRSS2OWLParserException :
{
    Set<OWLClassExpression> descs = new HashSet<OWLClassExpression>();
    OWLClassExpression desc;
}
{ ((desc=ConceptExpression() {descs.add(desc);})+) { return descs; } }

OWLClassExpression And()throws KRSS2OWLParserException :
{ Set<OWLClassExpression> operands; }
{ <OPENPAR><AND>(operands=ConceptSet())<CLOSEPAR> { return df.getOWLObjectIntersectionOf(operands); } }

OWLClassExpression Or() throws KRSS2OWLParserException :
{ Set<OWLClassExpression> operands; }
{ <OPENPAR><OR>(operands=ConceptSet())<CLOSEPAR> { return df.getOWLObjectUnionOf(operands); } }

OWLClassExpression Not() throws KRSS2OWLParserException :
{ OWLClassExpression operand; }
{ <OPENPAR><NOT>(operand=ConceptExpression())<CLOSEPAR> { return df.getOWLObjectComplementOf(operand); } }

OWLClassExpression All() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{ <OPENPAR><ALL>prop=RoleExpression() filler=ConceptExpression()<CLOSEPAR> { return df.getOWLObjectAllValuesFrom(prop, filler); } }

OWLClassExpression Some()  throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{ <OPENPAR><SOME>prop=RoleExpression() filler=ConceptExpression()<CLOSEPAR>{ return df.getOWLObjectSomeValuesFrom(prop, filler); } }

OWLClassExpression AtLeast() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    <OPENPAR><ATLEAST>card=Integer() prop=RoleExpression() (filler=ConceptExpression())?<CLOSEPAR>
    {
        if (filler == null) { return df.getOWLObjectMinCardinality(card, prop); }
        return df.getOWLObjectMinCardinality(card, prop, filler);
    }
}

OWLClassExpression AtMost() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    <OPENPAR><ATMOST>card=Integer() prop=RoleExpression() (filler=ConceptExpression())?<CLOSEPAR>
    {
        if (filler == null) { return df.getOWLObjectMaxCardinality(card, prop); }
        return df.getOWLObjectMaxCardinality(card, prop, filler);
    }
}

OWLClassExpression Exactly() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    <OPENPAR><EXACTLY>card=Integer() prop=RoleExpression() (filler=ConceptExpression())?<CLOSEPAR>
    {
        if (filler == null) { return df.getOWLObjectExactCardinality(card, prop); }
        return df.getOWLObjectExactCardinality(card, prop, filler);
    }
}

OWLObjectProperty RoleName() throws KRSS2OWLParserException :
{ IRI iri; }
{ iri=Name() { return df.getOWLObjectProperty(iri); } }

Set<OWLObjectProperty> RoleNameSet() throws KRSS2OWLParserException :
{
    Set<OWLObjectProperty> roles = new HashSet<OWLObjectProperty>();
    OWLObjectProperty role;
}
{ ((role=RoleName() {roles.add(role);})+) { return roles; } }

OWLObjectPropertyExpression RoleExpression() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression exp;
    OWLObjectProperty p;
}
{  ((<OPENPAR><INV>p=RoleName()<CLOSEPAR>) {exp= df.getOWLObjectInverseOf(p);} |(exp=RoleName())) {return exp;}}

void ABoxStatement()throws KRSS2OWLParserException  :
{ }
{ (LOOKAHEAD(2)Instance() | LOOKAHEAD(2)Related() | LOOKAHEAD(2)Equal() | LOOKAHEAD(2)Distinct()) }

void Instance() throws KRSS2OWLParserException :
{
    OWLIndividual ind;
    OWLClassExpression type;
}
{ <OPENPAR><INSTANCE>ind=IndividualName() type=ConceptExpression()<CLOSEPAR> { addAxiom( df.getOWLClassAssertionAxiom(type, ind)); } }

void Related()  throws KRSS2OWLParserException:
{
    OWLIndividual subj;
    OWLObjectProperty prop;
    OWLIndividual obj;
}
{ <OPENPAR><RELATED>subj=IndividualName() prop=RoleName() obj=IndividualName()<CLOSEPAR> { addAxiom( df.getOWLObjectPropertyAssertionAxiom(prop, subj, obj)); } }

void Equal()  throws KRSS2OWLParserException:
{ OWLIndividual indA, indB; }
{ <OPENPAR><EQUAL>indA=IndividualName() indB=IndividualName()<CLOSEPAR> { addAxiom( df.getOWLSameIndividualAxiom(indA, indB)); } }

void Distinct() throws KRSS2OWLParserException :
{ OWLIndividual indA, indB; }
{ <OPENPAR><DISTINCT>indA=IndividualName() indB=IndividualName()<CLOSEPAR>{ addAxiom( df.getOWLDifferentIndividualsAxiom(indA, indB)); } }

OWLIndividual IndividualName() throws KRSS2OWLParserException :
{ IRI name; }
{ name = Name() { return  df.getOWLNamedIndividual(name); } }

int Integer()throws KRSS2OWLParserException :
{ Token t; }
{ t=<INT> { return Integer.parseInt(t.image); } }

boolean parseBoolean():
{ boolean b; }
{ (<TRUE>{b = true;} | <NIL> {b = false;}) { return b; } }

IRI Name():
{
  IRI i;
  Token t;
}
{ ((t=<NAME>| t=<INT>) {i = getIRI(t.image);} |(t=<TOP> { i = OWLRDFVocabulary.OWL_THING.getIRI();} | t=<BOTTOM> { i = OWLRDFVocabulary.OWL_NOTHING.getIRI();})) { return i; } }
