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

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

There is a newer version: 5.5.1
Show newest version
//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 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();
        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: {  : 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: {  | "_" | "." | "-" | ["0"-"9"] | "\u00B7" | "%"> }
TOKEN: { 
}
TOKEN: {  }

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

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; }
{
      subClass=ConceptName() (superClass=ConceptExpression())? {
        if (superClass == null) superClass = df.getOWLThing();
        addAxiom(df.getOWLSubClassOfAxiom(subClass, superClass));
      }
}

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

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

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

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

// Roles

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

void DefinePrimitiveRole() throws KRSS2OWLParserException :
{
    OWLObjectProperty subProp;
    OWLObjectProperty superProp;
    OWLClassExpression desc;
    Set roles = new HashSet();
    Set descs = new HashSet();
}
{ (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)))) }

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

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

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

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

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

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

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

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

void Parents(OWLObjectProperty subProp) throws KRSS2OWLParserException :
{
    Set roles;
    OWLObjectProperty superProp;
}
{
((|roles=RoleNameSet()
{ for (OWLObjectProperty prop: roles) { addAxiom(df.getOWLSubObjectPropertyOfAxiom(subProp, prop)); }})
|((|(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;
}
{ exp1 =RoleExpression() exp2=RoleExpression(){ addAxiom(df.getOWLDisjointObjectPropertiesAxiom(exp1, exp2)); } }

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

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

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

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

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

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

void Range() throws KRSS2OWLParserException :
{
    OWLObjectProperty prop;
    OWLClassExpression rng;
}
{ prop=RoleName() rng=ConceptExpression() { 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 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 df.getOWLObjectIntersectionOf(operands); } }

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

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

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

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

OWLClassExpression AtLeast() throws KRSS2OWLParserException:
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler=null;
    int card;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?
    {
        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;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?
    {
        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;
}
{
    card=Integer() prop=RoleExpression() (filler=ConceptExpression())?
    {
        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 RoleNameSet() throws KRSS2OWLParserException :
{
    Set roles = new HashSet();
    OWLObjectProperty role;
}
{ ((role=RoleName() {roles.add(role);})+) { return roles; } }

OWLObjectPropertyExpression RoleExpression() throws KRSS2OWLParserException :
{
    OWLObjectPropertyExpression exp;
    OWLObjectProperty p;
}
{  ((p=RoleName()) {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;
}
{ ind=IndividualName() type=ConceptExpression() { addAxiom( df.getOWLClassAssertionAxiom(type, ind)); } }

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

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

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

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

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

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

IRI Name():
{
  IRI i;
  Token t;
}
{ ((t=| t=) {i = getIRI(t.image);} |(t= { i = OWLRDFVocabulary.OWL_THING.getIRI();} | t= { i = OWLRDFVocabulary.OWL_NOTHING.getIRI();})) { return i; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy