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

org.coode.owl.krssparser.KRSSParser.jj Maven / Gradle / Ivy

options {
JAVA_UNICODE_ESCAPE=true;
    STATIC=false;
    LOOKAHEAD=2;
    //DEBUG_TOKEN_MANAGER=true;
}

PARSER_BEGIN(KRSSParser)

package org.coode.owl.krssparser;

import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.vocab.Namespaces;
import java.net.*;
import java.util.*;


public class KRSSParser {

    private OWLOntology ontology;

    private OWLDataFactory dataFactory;

    private Map string2IRI;

    private boolean ignoreAnnotationsAndDeclarations = false;

    private String base;

    public void setOntology(OWLOntology ontology, OWLDataFactory dataFactory) {
        this.ontology = ontology;
        this.dataFactory = dataFactory;
        string2IRI = new HashMap();
        if (!ontology.isAnonymous()) {
            base = ontology.getOntologyID().getOntologyIRI() + "#";
        }
        else {
            base = Namespaces.OWL.toString();
        }
    }

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

    public IRI getIRI(String s) {
        s = base + s;
        IRI iri = string2IRI.get(s);
        if(iri == null) {
            iri = IRI.create(s);
            string2IRI.put(s, iri);
        }
        return iri;
    }

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

}

PARSER_END(KRSSParser)


SKIP: {" " | "\n" | "\t" | "\r" }

////////////////////////////////////////////////////////////////////////////////////////////
//
// COMMENTS

MORE:
{
     : IN_COMMENT
}

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

MORE:
{
    <~[]>
}

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

MORE:
{
     : IN_IRI
}

TOKEN:
{
    "> : DEFAULT
}

MORE:
{
    <~[]>
}


/////////////////////////////////////////////////////////////////////////////////////////////
//
// String Literals
//
// When we encounter a double quote, we have found a string literal.  The end of the literal
// is marked by an unescaped double quote
//

MORE:
{
     : IN_STRING_LITERAL
}

// Escaped double quote - part of the literal
 MORE:
{
    <"\\\"">
}

// End of the literal
 TOKEN:
{
     :DEFAULT
}


 MORE:
{
    <~[]>
}






/////////////////////////////////////////////////////////////////////////////////////////////

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}



public void parse() throws KRSSOWLParserException :
{
    OWLAxiom ax;
}
{
    ((ax=TBoxStatement(){
        if(ax != null) {
            addAxiom(ax);
        }
    })* ()? (ABoxStatement())* ()? )
}


OWLAxiom TBoxStatement() :
{
    OWLAxiom ax;
}
{
    (ax=DefinePrimitiveConcept() | ax=DefineConcept() | ax=DefinePrimitiveRole() | ax=Transitive() |  ax=Range()) {
        return ax;
    }
}


OWLAxiom DefinePrimitiveConcept() :
{
    OWLClassExpression subClass;
    OWLClassExpression superClass;
}
{
    subClass=ConceptName() superClass=ConceptExpression() {
        return dataFactory.getOWLSubClassOfAxiom(subClass, superClass);
    }
}

OWLAxiom DefineConcept() :
{
    OWLClassExpression clsA;
    OWLClassExpression clsB;
}
{
    clsA=ConceptName() clsB=ConceptExpression() {
        Set ops = new HashSet();
        ops.add(clsA);
        ops.add(clsB);
        return dataFactory.getOWLEquivalentClassesAxiom(ops);
    }
}

OWLAxiom DefinePrimitiveRole() :
{
    OWLObjectProperty subProp;
    OWLObjectProperty superProp;
}
{
    subProp=RoleName() superProp=RoleName() (":right-identity" RoleName())? {
        if(superProp != null) {
            return dataFactory.getOWLSubObjectPropertyOfAxiom(subProp, superProp);
        }
    }
}


OWLAxiom Transitive() :
{
    OWLObjectProperty prop;
}
{
    prop=RoleName() {
        return dataFactory.getOWLTransitiveObjectPropertyAxiom(prop);
    }
}

OWLAxiom Range() :
{
    OWLObjectProperty prop;
    OWLClassExpression rng;
}
{
    prop=RoleName() rng=ConceptExpression() {
        return dataFactory.getOWLObjectPropertyRangeAxiom(prop, rng);
    }
}


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

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


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

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

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

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

OWLClassExpression All() :
{
    OWLObjectProperty prop;
    OWLClassExpression filler;
}
{
    prop=RoleName() filler=ConceptExpression() {
        return dataFactory.getOWLObjectAllValuesFrom(prop, filler);
    }
}

OWLClassExpression Some() :
{
    OWLObjectProperty prop;
    OWLClassExpression filler;
}
{
    prop=RoleName() filler=ConceptExpression(){
        return dataFactory.getOWLObjectSomeValuesFrom(prop, filler);
    }
}

OWLClassExpression AtLeast() :
{
    OWLObjectProperty prop;
    OWLClassExpression filler;
    int card;
}
{
    card=Integer() prop=RoleName() filler=ConceptExpression(){
        return dataFactory.getOWLObjectMinCardinality(card, prop, filler);
    }
}

OWLClassExpression AtMost() :
{
    OWLObjectProperty prop;
    OWLClassExpression filler;
    int card;
}
{
    card=Integer() prop=RoleName() filler=ConceptExpression(){
        return dataFactory.getOWLObjectMaxCardinality(card, prop, filler);
    }
}

OWLClassExpression Exactly() :
{
    OWLObjectProperty prop;
    OWLClassExpression filler;
    int card;
}
{
    card=Integer() prop=RoleName() filler=ConceptExpression(){
        return dataFactory.getOWLObjectExactCardinality(card, prop, filler);
    }
}

OWLObjectProperty RoleName() :
{
    IRI iri;
}
{
    iri=Name() {
        return dataFactory.getOWLObjectProperty(iri);
    }
}

OWLAxiom ABoxStatement() :
{
    OWLAxiom ax;
}
{
    (ax=Instance() | ax=Related() | ax=Equal() | ax=Distinct()) {
        return ax;
    }
}

OWLAxiom Instance() :
{
    OWLIndividual ind;
    OWLClassExpression type;
}
{
    ind=IndividualName() type=ConceptExpression() {
        return dataFactory.getOWLClassAssertionAxiom(type, ind);
    }
}

OWLAxiom Related() :
{
    OWLIndividual subj;
    OWLObjectProperty prop;
    OWLIndividual obj;
}
{
    subj=IndividualName() prop=RoleName() obj=IndividualName() {
        return dataFactory.getOWLObjectPropertyAssertionAxiom(prop, subj, obj);
    }
}

OWLAxiom Equal() :
{
    OWLIndividual indA, indB;
    Set inds = new HashSet();
}
{
    indA=IndividualName() indB=IndividualName() {
        inds.add(indA);
        inds.add(indB);
        return dataFactory.getOWLSameIndividualAxiom(inds);
    }
}

OWLAxiom Distinct() :
{
    OWLIndividual indA, indB;
    Set inds = new HashSet();
}
{
    indA=IndividualName() indB=IndividualName(){
        inds.add(indA);
        inds.add(indB);
        return dataFactory.getOWLDifferentIndividualsAxiom(inds);
    }
}


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

IRI Name() :
{
    Token t;
}
{
    t= {
        return getIRI(t.image);
    }
}

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











© 2015 - 2024 Weber Informatics LLC | Privacy Policy