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

org.semanticweb.owlapi.krss2.parser.KRSS2Parser.jj Maven / Gradle / Ivy

The newest version!
//KRSS2Parser by Olaf Noppens, based on KRSSParser by Matthew Horridge
options {
JAVA_UNICODE_ESCAPE=true;
    STATIC=false;
    LOOKAHEAD=2;
    //FORCE_LA_CHECK=true;
}


PARSER_BEGIN(KRSS2Parser)

package de.uulm.ecs.ai.owlapi.krssparser;

import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.semanticweb.owlapi.util.*;
import org.coode.owl.krssparser.NameResolverStrategy;

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

// Suppress warnings in auto-generated code
@SuppressWarnings("unchecked")
public class KRSS2Parser {

    private OWLOntology ontology;

    private OWLDataFactory dataFactory;

    private Map string2IRI;

    private boolean ignoreAnnotationsAndDeclarations = false;

    private String base;

    private NamespaceUtil utils;
     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 void setOntology(OWLOntology ontology, OWLDataFactory dataFactory)  {
        this.ontology = ontology;
        this.dataFactory = dataFactory;
        this.string2IRI = new HashMap();
        OWLOntologyID id = ontology.getOntologyID();
        if(!id.isAnonymous()) {
            this.base = id.getOntologyIRI().toString() + "#";
        }
        else {
            this.base = id.toString() + "#";
        }
        this.utils = new NamespaceUtil();
        this.nameResolution = NameResolverStrategy.CHECK;

    }

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

     public IRI getIRI(final String s) throws URISyntaxException {
            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: {
                        splitted = utils.split(s, splitted);
                        if (splitted[0]=="") {
                            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:
{
     : IN_COMMENT
}

SKIP:
{
    <"\n"> : DEFAULT
}

MORE:
{
    <~[]>
}


////////////////////////////////////////////////////////////////////////////////////////////
//
// IRIs

MORE:
{
     : IN_IRI
}

TOKEN:
{
    "> : DEFAULT
}

MORE:
{
    <~[]>
}





TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}



TOKEN[IGNORE_CASE]:
{
    
}

//concepts

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}

//roles

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}
TOKEN[IGNORE_CASE]:
{
    
}
TOKEN:
{
    
}
TOKEN:
{
    
}
TOKEN:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}
TOKEN:
{
    
}

//individuals

TOKEN[IGNORE_CASE]:
{
    
}
TOKEN[IGNORE_CASE]:
{
    
}
TOKEN[IGNORE_CASE]:
{
    
}
TOKEN[IGNORE_CASE]:
{
    
}

TOKEN[IGNORE_CASE]:
{
    
}


TOKEN:
{
    
}


TOKEN[IGNORE_CASE]:
{
    
}


TOKEN:
 {
     
 }



//TOKEN:
 //{
//     )+>
 //}

 TOKEN:
{
    )+>
}

TOKEN:
{
     | "_" | "." | "-" | ["0"-"9"] | "\u00B7" | "%">
}

TOKEN:
{
    
}

TOKEN:
{
    
}


public void parse() throws KRSS2OWLParserException :
{
}
{
    (TBoxStatement())* ()? (ABoxStatement())* ()? 
}


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


// Concepts

void DefinePrimitiveConcept() throws KRSS2OWLParserException:
{
    OWLClassExpression subClass = null;
    OWLClassExpression superClass = null;

}
{
      subClass=ConceptName() (superClass=ConceptExpression())? {
        if (superClass == null)
            addAxiom(dataFactory.getOWLSubClassOfAxiom(subClass, dataFactory.getOWLThing()));
        else
            addAxiom(dataFactory.getOWLSubClassOfAxiom(subClass, superClass));
      }
}

void DefineConcept() throws KRSS2OWLParserException:
{
    OWLClassExpression clsA;
    OWLClassExpression clsB;
}
{
    clsA=ConceptName() clsB=ConceptExpression() {
        if (clsB == null)
            clsB = dataFactory.getOWLThing();
        addAxiom(dataFactory.getOWLEquivalentClassesAxiom(clsA, clsB));
    }
}

void Disjoint() throws KRSS2OWLParserException:
{
    OWLClassExpression desc1;
    OWLClassExpression desc2;
}
{
    desc1=ConceptExpression() desc2=ConceptExpression() {
        addAxiom(dataFactory.getOWLDisjointClassesAxiom(desc1, desc2));
    }
}

void Equivalent() throws KRSS2OWLParserException:
{
    OWLClassExpression desc1;
    OWLClassExpression desc2;
}
{
    desc1=ConceptExpression() desc2=ConceptExpression() {
        addAxiom(dataFactory.getOWLEquivalentClassesAxiom(desc1, desc2));
    }
}

void Implies() throws KRSS2OWLParserException :
{
    OWLClassExpression subDescription;
    OWLClassExpression superDescription;
}
{
    subDescription=ConceptExpression() superDescription = ConceptExpression() {
        addAxiom( dataFactory.getOWLSubClassOfAxiom(subDescription, superDescription));
    }
}




// Roles

void DefineRole() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression propA;
    OWLObjectPropertyExpression propB;
}
{
    propA=RoleExpression() propB=RoleExpression() {
        Set ops = new HashSet();
        ops.add(propA);
        ops.add(propB);
        addAxiom(dataFactory.getOWLEquivalentObjectPropertiesAxiom(ops));
    }
}

void DefinePrimitiveRole() throws KRSS2OWLParserException :
{
    OWLObjectProperty subProp;
    OWLObjectProperty superProp;
    OWLClassExpression desc;
    Set roles = new HashSet();
    Set descs = new HashSet();
}

{
    (subProp=RoleName() {
          if (subProp != null)
            addAxiom( dataFactory.getOWLDeclarationAxiom(subProp));
    }
    ( (Parent(subProp)) ((RightIdentity(subProp))|(LeftIdentity(subProp))) (Parents(subProp))(DomainAttribute(subProp))(RangeAttribute(subProp))(TransitiveAttribute(subProp))(SymmetricAttribute(subProp)) (ReflexiveAttribute(subProp))(InverseProperty(subProp))
    )
    )
}


void RightIdentity(OWLObjectProperty r) throws KRSS2OWLParserException :
{
OWLObjectProperty s;
}
{
    (s=RoleName() {
        List chain = new ArrayList(2);
        chain.add(r); chain.add(s);
        addAxiom(dataFactory.getOWLSubPropertyChainOfAxiom(chain, r));
    }
    )?
}

void LeftIdentity(OWLObjectProperty r) throws KRSS2OWLParserException:
{
OWLObjectProperty s;
}
{
    (s=RoleName() {
        List chain = new ArrayList(2);
        chain.add(s); chain.add(r);
        addAxiom(dataFactory.getOWLSubPropertyChainOfAxiom(chain, r));
    }
    )?
}


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

void DomainAttribute(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
      OWLClassExpression desc;
      Set descs = new HashSet();

}
{
  (( (desc=ConceptExpression(){if (desc != null) descs.add(desc);})+| (desc=ConceptExpression(){ descs.add(desc);})  )
            {
                for (OWLClassExpression eachDescription : descs)
                    addAxiom(dataFactory.getOWLObjectPropertyDomainAxiom(subProp, eachDescription));

            }
        )?
}

void RangeAttribute(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
      OWLClassExpression desc;
      Set descs = new HashSet();

}
{
  (( (desc=ConceptExpression(){if (desc != null) descs.add(desc);})+| (desc=ConceptExpression(){ descs.add(desc);})  )
            {
                for (OWLClassExpression eachDescription : descs) {
                    addAxiom(dataFactory.getOWLObjectPropertyRangeAxiom(subProp, eachDescription));
                }
            }
        )?
}

void TransitiveAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{
}
{
 ((|{
    addAxiom(dataFactory.getOWLTransitiveObjectPropertyAxiom(property));
 }))?
}

void SymmetricAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{
}
{
 ((|{
    addAxiom(dataFactory.getOWLSymmetricObjectPropertyAxiom(property));
 }))?
}

void ReflexiveAttribute(OWLObjectProperty property) throws KRSS2OWLParserException :
{
}
{
 ((|{
    addAxiom(dataFactory.getOWLReflexiveObjectPropertyAxiom(property));
 }))?
}


void Parents(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
    Set roles;
    OWLObjectProperty superProp;

}
{
((|roles=RoleNameSet() {
                if (roles != null)
                    for (OWLObjectProperty prop: roles) {
                        addAxiom(dataFactory.getOWLSubObjectPropertyOfAxiom(subProp, prop));
                    }
            })

            |
                       ((|(superProp=RoleName(){
                            addAxiom(dataFactory.getOWLSubObjectPropertyOfAxiom(subProp, superProp));
                       })))
                    )?
}


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


void DisjointRoles() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression exp1;
    OWLObjectPropertyExpression exp2;
}
{
    exp1 =RoleExpression() exp2=RoleExpression(){
        addAxiom(dataFactory.getOWLDisjointObjectPropertiesAxiom(exp1, exp2));
    }
}

void ImpliesRole() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression subProp;
    OWLObjectPropertyExpression superProp;
}
{
    subProp=RoleExpression() superProp=RoleExpression(){
        addAxiom(dataFactory.getOWLSubObjectPropertyOfAxiom(subProp, superProp));
    }
}


void Inverses() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop1;
    OWLObjectPropertyExpression prop2;
}
{
    prop1=RoleExpression() prop2=RoleExpression(){
        addAxiom(dataFactory.getOWLInverseObjectPropertiesAxiom(prop1, prop2));
    }
}

void RolesEquivalent() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression exp1;
    OWLObjectPropertyExpression exp2;
}
{
    exp1=RoleExpression() exp2=RoleExpression(){
        addAxiom(dataFactory.getOWLEquivalentObjectPropertiesAxiom(exp1, exp2));
    }
}


void ComplexRoleInclusion() throws KRSS2OWLParserException :
{
    OWLObjectProperty superProp;
    List chain;
}
{    chain = propertyChain() superProp=RoleName(){
         addAxiom(dataFactory.getOWLSubPropertyChainOfAxiom(chain, superProp));
    }
}

List propertyChain() throws KRSS2OWLParserException:
{
    List chain = new ArrayList();
    List subChain;
    OWLObjectPropertyExpression prop;
}
{
    (prop=RoleExpression()){chain.add(prop);} (subChain = propertyChain(){chain.addAll(subChain);} | (prop=RoleExpression(){chain.add(prop);}))
    {
        return chain;
    }
}



void Transitive()  throws KRSS2OWLParserException:
{
    OWLObjectProperty prop;
}
{
    prop=RoleName() {
        addAxiom( dataFactory.getOWLTransitiveObjectPropertyAxiom(prop));
    }
}

void Range() throws KRSS2OWLParserException :
{
    OWLObjectProperty prop;
    OWLClassExpression rng;
}
{
    prop=RoleName() rng=ConceptExpression() {
        addAxiom( dataFactory.getOWLObjectPropertyRangeAxiom(prop, rng));
    }
}


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

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


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

OWLClassExpression And()throws KRSS2OWLParserException :
{
    Set operands;
}
{
    (operands=ConceptSet()) {
        return dataFactory.getOWLObjectIntersectionOf(operands);
    }
}

OWLClassExpression Or() throws KRSS2OWLParserException :
{
    Set operands;
}
{
    (operands=ConceptSet()) {
        return dataFactory.getOWLObjectUnionOf(operands);
    }
}

OWLClassExpression Not() throws KRSS2OWLParserException :
{
    OWLClassExpression operand;
}
{
    (operand=ConceptExpression()) {
        return dataFactory.getOWLObjectComplementOf(operand);
    }
}

OWLClassExpression All() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{
    prop=RoleExpression() filler=ConceptExpression() {
        return dataFactory.getOWLObjectAllValuesFrom(prop, filler);
    }
}

OWLClassExpression Some()  throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{
    prop=RoleExpression() filler=ConceptExpression(){
        return dataFactory.getOWLObjectSomeValuesFrom(prop, filler);
    }
}

OWLClassExpression AtLeast() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?{
        if (filler == null)
            return dataFactory.getOWLObjectMinCardinality(card, prop);
        return dataFactory.getOWLObjectMinCardinality(card, prop, filler);
    }
}

OWLClassExpression AtMost() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?{
        if (filler == null)
              return dataFactory.getOWLObjectMaxCardinality(card, prop);
        return dataFactory.getOWLObjectMaxCardinality(card, prop, filler);
    }
}

OWLClassExpression Exactly() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?{
        if (filler == null)
            return dataFactory.getOWLObjectExactCardinality(card, prop);
        return dataFactory.getOWLObjectExactCardinality(card, prop, filler);
    }
}



OWLObjectProperty RoleName() throws KRSS2OWLParserException :
{
    IRI IRI;
}
{
    IRI=Name() {
            if (IRI == null) return null;
        return dataFactory.getOWLObjectProperty(IRI);
    }
}

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

OWLObjectPropertyExpression RoleExpression() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression exp;
}
{
    (
    (exp=RoleExpression()) {return dataFactory.getOWLObjectInverseOf(exp);}
    |
    (exp=RoleName(){return exp;})
    )
    

}



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

void Instance() throws KRSS2OWLParserException :
{
    OWLIndividual ind;
    OWLClassExpression type;
}
{
    ind=IndividualName() type=ConceptExpression() {
            addAxiom( dataFactory.getOWLClassAssertionAxiom(type, ind));
    }
}

void Related()  throws KRSS2OWLParserException:
{
    OWLIndividual subj;
    OWLObjectProperty prop;
    OWLIndividual obj;
}
{
    subj=IndividualName() prop=RoleName() obj=IndividualName() {
        addAxiom( dataFactory.getOWLObjectPropertyAssertionAxiom(prop, subj, obj));
    }
}

void Equal()  throws KRSS2OWLParserException:
{
    OWLIndividual indA, indB;
    Set inds = new HashSet();
}
{
    indA=IndividualName() indB=IndividualName() {
        inds.add(indA);
        inds.add(indB);
        addAxiom( dataFactory.getOWLSameIndividualAxiom(inds));
    }
}

void Distinct() throws KRSS2OWLParserException :
{
    OWLIndividual indA, indB;
    Set inds = new HashSet();
}
{
    indA=IndividualName() indB=IndividualName(){
        inds.add(indA);
        inds.add(indB);
        addAxiom( dataFactory.getOWLDifferentIndividualsAxiom(inds));
    }
}


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


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


boolean parseBoolean():
{
}
{
    ({return true;} |  {return false;})
    {
    }
}



IRI Name():
{
    Token t;
}
{
    ( (t=| t=){
        try {
            return getIRI(t.image);
        }
        catch(URISyntaxException e) {
            return null;
        }
    }
    |
    (t=
        { return OWLRDFVocabulary.OWL_THING.getIRI();}
     | t=
        {return OWLRDFVocabulary.OWL_NOTHING.getIRI();}
    ))

}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy