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

uk.ac.manchester.cs.owlapi.dlsyntax.parser.DLSyntaxParser.jj Maven / Gradle / Ivy

There is a newer version: 3.4.9.2-ansell
Show newest version
options {
    STATIC=false;
    JAVA_UNICODE_ESCAPE=true;
    LOOKAHEAD=5;
    //FORCE_LA_CHECK=true;
    //DEBUG_PARSER=true;
    //DEBUG_TOKEN_MANAGER=true;
}

PARSER_BEGIN(DLSyntaxParser)

/*
 * This file is part of the OWL API.
 *
 * The contents of this file are subject to the LGPL License, Version 3.0.
 *
 * Copyright (C) 2011, The University of Manchester
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/.
 *
 *
 * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0
 * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
 *
 * Copyright 2011, University of Manchester
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.ac.manchester.cs.owlapi.dlsyntax.parser;

import java.io.Reader;
import java.util.Set;
import java.util.HashSet;
import java.util.*;
import org.semanticweb.owlapi.vocab.XSDVocabulary;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
import org.semanticweb.owlapi.util.*;

import org.semanticweb.owlapi.model.*;

@SuppressWarnings("all")
public class DLSyntaxParser {

    private String defaultNamespace = "http://www.sematicweb.org/ontologies/Ontology" + System.nanoTime();

    private Map namespaceMap = new HashMap();

    private OWLDataFactory factory;

    private Map iriMap = new HashMap();

    private Map qnameIRIMap = new HashMap();

    public void setOWLDataFactory(OWLDataFactory factory) {
        this.factory = factory;
    }

    public void setPrefixMapping(String prefix, String namespace) {
        namespaceMap.put(prefix, namespace);
    }

    public void setDefaultNamespace(String ns) {
        defaultNamespace = ns;
    }

    public IRI getIRI(String val) {
        IRI iri = iriMap.get(val);
        if(iri == null) {
                iri = IRI.create(val);
                iriMap.put(val, iri);
        }
        return iri;
    }

    public IRI getIRIFromId(String qname) {
        if(qname.equals("top") || qname.equals("\u22A4")) {
            return OWLRDFVocabulary.OWL_THING.getIRI();
        }
        if(qname.equals("bottom") || qname.equals("\u22A5")) {
            return OWLRDFVocabulary.OWL_NOTHING.getIRI();
        }
        IRI iri = qnameIRIMap.get(qname);
        if(iri == null) {
            iri = getIRI(defaultNamespace + "#" + qname);
            qnameIRIMap.put(qname, iri);
        }
        return iri;
    }

}

PARSER_END(DLSyntaxParser)



SKIP: {" "}

// We skip over any white space

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




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


TOKEN:
{
    " | "sub" | "\\sqsubseteq")>
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}
TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    " | "\\geq")>
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}

TOKEN:
{
    
}


TOKEN:
{
    
}

TOKEN:
{
    )+()*>
}


TOKEN: {
    ", ".", "\u207B", "\u00AC", "\u2208"]))+>
}



OWLClassExpression parseDescription() :
{
    OWLClassExpression desc;
}
{
    desc = parseClassDescription()  {
        return desc;
    }
}

public Set parseAxioms() :
{
    OWLAxiom ax;
    Set axioms = new LinkedHashSet();
}
{
    (ax=parseAxiom(){axioms.add(ax);}) (("\n" ax=parseAxiom(){axioms.add(ax);}) | "\n")*  {
        return axioms;
    }
}

public OWLAxiom parseAxiom() :
{
    OWLAxiom ax;
}
{
    ( ax = parsePropertyAxiom() {return ax;})
    |
    (ax = parseClassAxiom() {return ax;})
    |
    (ax = parseIndividualAxiom() {return ax;})
    |
    (ax = parseObjectPropertyAssertion() {return ax;})
    |
    (ax = parseDataPropertyAssertion() {return ax;})
    |
    (ax = parseDifferentIndividualsAxiom() {return ax;})
}

public OWLAxiom parseIndividualAxiom() :
{
    OWLAxiom ax;
}
{
    (ax=parseClassAssertion()
    |
    ax=parseSameIndividual()) {
        return ax;
    }

}

public OWLAxiom parseDifferentIndividualsAxiom() :
{
    Set inds = new HashSet();
    OWLIndividual ind;
}
{
    (ind=parseIndividualId(){inds.add(ind);}  ind=parseIndividualId(){inds.add(ind);} ( ind=parseIndividualId(){inds.add(ind);})*) {
        return factory.getOWLDifferentIndividualsAxiom(inds);
    }
}

public OWLAxiom parseObjectPropertyAssertion() :
{
    OWLIndividual subj, obj;
    OWLObjectPropertyExpression prop;
}
{
    prop=parseObjectPropertyId()
    
    subj=parseIndividualId()
    ","
    obj=parseIndividualId()
     {
        return factory.getOWLObjectPropertyAssertionAxiom(prop, subj, obj);
    }
}

public OWLAxiom parseDataPropertyAssertion() :
{
    OWLIndividual subj;
    OWLDataPropertyExpression prop;
    OWLLiteral obj;
}
{
    prop=parseDataPropertyId()
    
    subj=parseIndividualId()
    ","
    obj=parseLiteral()
     {
        return factory.getOWLDataPropertyAssertionAxiom(prop, subj, obj);
    }


}

public OWLAxiom parseSameIndividual() :
{
    OWLIndividual indA;
    OWLIndividual indB;
}
{
    indA=parseIndividualId()
    
    indB=parseIndividualId() {
        return factory.getOWLSameIndividualAxiom(CollectionFactory.createSet(indA, indB));
    }
}

public OWLAxiom parseClassAssertion() :
{
    OWLIndividual ind;
    OWLClassExpression desc;
}
{

    (

    
    desc = parseDescription()
    
    )
    |
    (
    desc = parseClassDescription()
    )
    
    ind=parseIndividualId()
     ("\n"{jj_input_stream.backup(1);} | )
    {
        return factory.getOWLClassAssertionAxiom(desc, ind);
    }
}

public OWLAxiom parseClassAxiom() :
{
    OWLClassExpression lhs;
    OWLClassExpression rhs;
    boolean subClassAxiom = false;
}
{
    (lhs = parseClassDescription()

    (
        rhs = parseClassDescription() {
               if(lhs.isOWLThing()) {
                        if(rhs instanceof OWLObjectAllValuesFrom) {
                            // Interpret as Range
                            OWLObjectAllValuesFrom restriction = (OWLObjectAllValuesFrom) rhs;
                            return factory.getOWLObjectPropertyRangeAxiom(restriction.getProperty(), restriction.getFiller());
                        }
                        if(rhs instanceof OWLObjectMaxCardinality) {
                            OWLObjectMaxCardinality restriction = (OWLObjectMaxCardinality) rhs;
                            if(restriction.getCardinality() == 1 && restriction.getFiller().isOWLThing()) {
                                return factory.getOWLFunctionalObjectPropertyAxiom(restriction.getProperty());
                            }
                        }
                    }
                    else if(lhs instanceof OWLObjectSomeValuesFrom) {
                        OWLObjectSomeValuesFrom restriction = (OWLObjectSomeValuesFrom) lhs;
                        if(restriction.getFiller().isOWLThing()) {
                            return factory.getOWLObjectPropertyDomainAxiom(restriction.getProperty(), rhs);
                        }
                    }
                    if(rhs instanceof OWLObjectComplementOf && !rhs.isAnonymous()) {
                        return factory.getOWLDisjointClassesAxiom(lhs, ((OWLObjectComplementOf) rhs).getOperand());
                    }
                    return factory.getOWLSubClassOfAxiom(lhs, rhs);
     }
     |
      rhs = parseClassDescription(){

        return factory.getOWLEquivalentClassesAxiom(lhs, rhs);

     })
    )
}

public OWLAxiom parsePropertyChain() :
{
    OWLObjectPropertyExpression prop;
    OWLObjectPropertyExpression supProp;
    List props = new ArrayList();

}
{
    prop=parseObjectPropertyId(){props.add(prop);}  prop=parseObjectPropertyId(){props.add(prop);} ( prop=parseObjectPropertyId(){props.add(prop);})*  supProp=parseObjectPropertyId() {
        return factory.getOWLSubPropertyChainOfAxiom(props, supProp);
    }

}

public OWLAxiom parsePropertyAxiom() :
{
    OWLObjectPropertyExpression lhs;
    OWLObjectPropertyExpression rhs;
    OWLAxiom ax;
}
{
    lhs = parseObjectPropertyId()
    (
        ( rhs = parseObjectPropertyId() {
            return factory.getOWLSubObjectPropertyOfAxiom(lhs, rhs);
        })
        |
        ( rhs = parseObjectPropertyId() {
            if(rhs instanceof OWLObjectInverseOf) {
                OWLObjectInverseOf inv = (OWLObjectInverseOf) rhs;
                return factory.getOWLInverseObjectPropertiesAxiom(lhs, inv.getInverse());
            }
            return factory.getOWLEquivalentObjectPropertiesAxiom(CollectionFactory.createSet(lhs, rhs));
        })
        |
        ( {
            return factory.getOWLTransitiveObjectPropertyAxiom(lhs);
        })
    )
    |
    ax=parsePropertyChain() {
        return ax;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// The class classExpression parser
//

public OWLClassExpression parseClassDescription():
{
    OWLClassExpression desc;
}
{
    desc = Or() {
        return desc;
    }
}

OWLClassExpression Or():
{
    OWLClassExpression desc;
    Set operands = new HashSet();
}
{
    (desc=And(){operands.add(desc);} ( desc=And(){operands.add(desc);})*) {
        if(operands.size() == 1) {
            return desc;
        }
        else {
            return factory.getOWLObjectUnionOf(operands);
        }
    }
}

OWLClassExpression And():
{
    OWLClassExpression desc;
    Set operands = new HashSet();
}
{
    (desc=NonNaryBooleanDescription(){operands.add(desc);} ( desc=NonNaryBooleanDescription(){operands.add(desc);})*) {
        if(operands.size() == 1) {
            return desc;
        }
        else {
            return factory.getOWLObjectIntersectionOf(operands);
        }
    }
}

OWLClassExpression NonNaryBooleanDescription():
{
    OWLClassExpression desc;
}
{

    (desc=parseRestriction()
    |
    desc=parseObjectComplementOf()
    |
    desc=NamedClassOrNestedDescription()) {
        return desc;
    }

}

OWLObjectPropertyExpression parseObjectPropertyId():
{
    IRI iri;
    boolean inverse = false;
}
{
    iri=parseId() ({inverse = true;})?  {
        if(inverse) {
            OWLObjectProperty prop = factory.getOWLObjectProperty(iri);
            return factory.getOWLObjectInverseOf(prop);
        }
        else {
            return factory.getOWLObjectProperty(iri);
        }
    }
}

OWLDataPropertyExpression parseDataPropertyId():
{
    IRI iri;
    boolean inverse = false;
}
{
    iri=parseId() {
            return factory.getOWLDataProperty(iri);
        }

}

OWLClassExpression parseRestriction():
{
    OWLClassExpression desc;
}
{
    (desc=parseSomeRestriction()
    |
    desc=parseDataSomeRestriction()
    |
    desc=parseAllRestriction()
    |
    desc=parseCardinalityRestriction()
    ){
        return desc;
    }
}

OWLClassExpression parseSomeRestriction():
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{
    (  prop=parseObjectPropertyId() ()? (filler=NamedClassOrNestedDescription())) {
        return factory.getOWLObjectSomeValuesFrom(prop, filler);
    }
}

OWLClassExpression parseDataSomeRestriction():
{
    OWLDataPropertyExpression prop;
    OWLDataRange filler;
}
{
    (  prop=parseDataPropertyId() ()? (filler=parseDataOneOf())) {
        return factory.getOWLDataSomeValuesFrom(prop, filler);
    }
}

OWLClassExpression parseAllRestriction():
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler;
}
{
    ( prop=parseObjectPropertyId() ()? (filler=NamedClassOrNestedDescription())) {
        return factory.getOWLObjectAllValuesFrom(prop, filler);
    }
}

OWLClassExpression parseCardinalityRestriction():
{
    OWLObjectPropertyExpression prop;
    OWLClassExpression filler = null;
    boolean min = false;
    boolean exactly = false;
    boolean max = false;
    Token t;
}
{
    (({min=true;} | {exactly=true;} | {max=true;}) t= prop=parseObjectPropertyId() (()? filler=NamedClassOrNestedDescription())?) {
        int card = Integer.parseInt(t.image);
        if(filler == null) {
            filler = factory.getOWLThing();
        }
        if(min) {
            return factory.getOWLObjectMinCardinality(card, prop, filler);
        }
        else if(exactly) {
            return factory.getOWLObjectExactCardinality(card, prop, filler);
        }
        else {
            return factory.getOWLObjectMaxCardinality(card, prop, filler);
        }
    }
}

OWLIndividual parseIndividualId():
{
    IRI iri;
}
{
    iri=parseId() {return factory.getOWLNamedIndividual(iri);}
}


OWLClassExpression parseObjectComplementOf():
{
    OWLClassExpression op;
}
{
     op=NamedClassOrNestedDescription() {
        return factory.getOWLObjectComplementOf(op);
    }
}

OWLClassExpression parseObjectOneOf():
{
    OWLIndividual ind;
    Set inds = new HashSet();
}
{
    (ind=parseIndividualId(){inds.add(ind);} (ind=parseIndividualId(){inds.add(ind);})*){
        return factory.getOWLObjectOneOf(inds);
    }
}

OWLDataRange parseDataOneOf():
{
    OWLLiteral val;
    Set values = new HashSet();
}
{
    (val=parseLiteral(){values.add(val);} (val=parseLiteral(){values.add(val);})*){
        return factory.getOWLDataOneOf(values);
    }
}

OWLClassExpression NamedClassOrNestedDescription():
{
    OWLClassExpression desc;
}
{
    (desc=parseClassId() | desc=NestedClassDescription()) {
        return desc;
    }
}

OWLClass parseClassId():
{
    IRI iri;
}
{
    iri=parseId() {
        return factory.getOWLClass(iri);
    }
}

OWLClassExpression NestedClassDescription():
{
    OWLClassExpression desc;
}
{
    (desc=Or(){return desc;}) | (desc = parseObjectOneOf() {return desc;})
}

OWLLiteral parseLiteral() :
{
    Token t;
}
{
    t={return factory.getOWLLiteral(Integer.parseInt(t.image));}
    |
    t={return factory.getOWLLiteral(Double.parseDouble(t.image));}
}

IRI parseId() :
{
    Token t;
}
{
    t= {
        String name = t.image;
        return getIRIFromId(name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy