krssconverter.java
来自「Semantic Web Ontology Editor」· Java 代码 · 共 902 行 · 第 1/4 页
JAVA
902 行
else if(peekToken(in, '(')) {
skipToken(in);
count++;
}
else
terms.add(parseExpr(in, ontology));
}
// for(int i = 0; i < count; i++)
// parseToken(in, ')');
return (terms);
}
OWLDescription parseExpr(StreamTokenizer in, OWLOntology ontology) throws Exception {
OWLDescription a = null;
int token = in.nextToken();
String s = in.sval;
if(token == '(') {
token = in.nextToken();
assertTrue(token == StreamTokenizer.TT_WORD);
s = in.sval;
if(s.equalsIgnoreCase("NOT")) {
OWLDescription c = parseExpr(in, ontology);
a = ontology.getOWLDataFactory().getOWLNot(c);
if(c instanceof OWLClass) this.addEntity(ontology, ((OWLClass) c).getURI(), CLASS);
}
else if(s.equalsIgnoreCase("AND")) {
Set list = new HashSet();
while(!peekToken(in, ')')) {
OWLDescription c = parseExpr(in, ontology);
if(c instanceof OWLClass) this.addEntity(ontology, ((OWLClass) c).getURI(), CLASS);
list.add(c);
}
a = ontology.getOWLDataFactory().getOWLAnd(list);
}
else if(s.equalsIgnoreCase("OR")) {
Set list = new HashSet();
while(!peekToken(in, ')')) {
OWLDescription c = parseExpr(in, ontology);
if(c instanceof OWLClass) this.addEntity(ontology, ((OWLClass) c).getURI(), CLASS);
list.add(c);
}
a = ontology.getOWLDataFactory().getOWLOr(list);
}
else if(s.equalsIgnoreCase("ALL")) {
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
// kb.addObjectProperty(r);
ATermUtils.assertTrue( prop != null );
OWLDescription c = parseExpr(in, ontology);
if(c instanceof OWLClass) this.addEntity(ontology, ((OWLClass) c).getURI(), CLASS);
a = ontology.getOWLDataFactory().getOWLObjectAllRestriction(prop, c);
}
else if(s.equalsIgnoreCase("SOME")) {
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
ATermUtils.assertTrue( prop != null );
OWLDescription c = parseExpr(in, ontology);
if(c instanceof OWLClass) this.addEntity(ontology, ((OWLClass) c).getURI(), CLASS);
a = ontology.getOWLDataFactory().getOWLObjectSomeRestriction(prop, c);
}
else if(s.equalsIgnoreCase("AT-LEAST")) {
int n = getInt(in);
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
a = ontology.getOWLDataFactory().getOWLObjectCardinalityAtLeastRestriction(prop, n);
}
else if(s.equalsIgnoreCase("AT-MOST")) {
int n = getInt(in);
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
a = ontology.getOWLDataFactory().getOWLObjectCardinalityAtMostRestriction(prop, n);
}
else if(s.equalsIgnoreCase("A")) {
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
// TODO what does term 'A' stand for
// kb.addProperty(r);
// kb.addFunctionalProperty(r);
this.setPropAttribute(ontology, prop.getURI(), OBJECT_PROP, FUNC, true);
// a = ATermUtils.makeMin(r, 1);
a = ontology.getOWLDataFactory().getOWLObjectCardinalityAtLeastRestriction(prop, 1);
}
else if(s.equalsIgnoreCase("MIN") || s.equals(">=")) {
ATermAppl r = getTerm(in);
// kb.addDatatypeProperty(r);
this.addEntity(ontology, new URI(r.getName()), DATA_PROP);
String val = getNumber(in)+"";
// DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
Datatype dt = XSDDecimal.instance.deriveByRestriction( XSSimpleType.FACET_MININCLUSIVE, val );
// String dtName = dtReasoner.defineDatatype(dt);
// ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
//TODO
// a = ATermUtils.makeAllValues(r, datatype);
}
else if(s.equalsIgnoreCase("MAX")
|| s.equals("<=")) {
ATermAppl r = getTerm(in);
// kb.addDatatypeProperty(r);
this.addEntity(ontology, new URI(r.getName()), DATA_PROP);
String val = getNumber(in)+"";
// DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
Datatype dt = XSDDecimal.instance.deriveByRestriction( XSSimpleType.FACET_MAXEXCLUSIVE, val );
// String dtName = dtReasoner.defineDatatype(dt);
// ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
//TODO
// a = ATermUtils.makeAllValues(r, datatype);
}
else if(s.equals("=")) {
ATermAppl r = getTerm(in);
// kb.addDatatypeProperty(r);
this.addEntity(ontology, new URI(r.getName()), DATA_PROP);
String val = getNumber(in)+"";
// DatatypeReasoner dtReasoner = kb.getDatatypeReasoner();
XSDDecimal dt = (XSDDecimal) XSDDecimal.instance.deriveByRestriction( XSSimpleType.FACET_MININCLUSIVE, val );
dt = (XSDDecimal) dt.deriveByRestriction( XSSimpleType.FACET_MAXEXCLUSIVE, val );
// String dtName = dtReasoner.defineDatatype(dt);
// ATermAppl datatype = ATermUtils.makeTermAppl(dtName);
//TODO
// a = ATermUtils.makeAllValues(r, datatype);
}
else if(s.equalsIgnoreCase("EXACTLY")) {
int n = getInt(in);
OWLObjectProperty prop = (OWLObjectProperty) getProperty(in, ontology, OBJECT_PROP);
a = ontology.getOWLDataFactory().getOWLObjectCardinalityRestriction(prop, n, n);
}
else if(s.equalsIgnoreCase("INV")) {
// not needed as it is handled in getProperty
}
else {
throw new RuntimeException("Unknown expression " + s);
}
if(in.nextToken() != ')') {
if(s.equalsIgnoreCase("AT-LEAST") || s.equalsIgnoreCase("AT-MOST"))
throw new UnsupportedFeatureException("Qualified cardinality restrictions");
else
throw new RuntimeException("Parse exception at term " + s);
}
}
else if(token == '#') {
int n = getInt(in);
if( peekToken(in, '#') ) {
skipToken(in);
//TODO
// a = (ATermAppl) terms.get( n );
if(a == null)
throw new RuntimeException("Parse exception: #" + n + "# is not defined");
}
else {
parseToken(in, "=");
a = parseExpr(in, ontology);
while(terms.size() <= n)
terms.add(null);
// ATermAppl previous = (ATermAppl)
terms.set(n, a);
// if( previous != null)
// System.err.println(
// "WARNING: Redfining #" + n + "# as " + a + ", previous was:" + previous);
}
}
else if(token == StreamTokenizer.TT_EOF)
a = null;
else if(s.equalsIgnoreCase("TOP") || s.equalsIgnoreCase("*TOP*"))
a = ontology.getOWLDataFactory().getOWLThing();
else if(s.equalsIgnoreCase("BOTTOM") || s.equalsIgnoreCase("*BOTTOM*"))
a = ontology.getOWLDataFactory().getOWLNothing();
else {
if(FORCE_UPPERCASE)
s = s.toUpperCase();
// make a class?
// a = ATermUtils.makeTermAppl(s);
a = (OWLClass) this.addEntity(ontology, new URI(s), CLASS);
}
return a;
}
public OWLOntology readTBox( String file ) throws Exception {
StreamTokenizer in = initTokenizer( file );
URI uri = new URI( base );
// create new OWL ontology object
OWLBuilder builder = new OWLBuilder();
builder.createOntology(uri, uri);
OWLOntology ontology = builder.getOntology();
Map disjoints = new HashMap();
int token = in.nextToken();
while(token != StreamTokenizer.TT_EOF && token != ')') {
if(token == '#') {
in.ordinaryChar('|');
token = in.nextToken();
while(token != '#')
token = in.nextToken();
in.quoteChar('|');
token = in.nextToken();
if(token == StreamTokenizer.TT_EOF)
break;
}
assertTrue(token == '(');
String str = getToken(in);
//functional propertirs
if(str.equalsIgnoreCase("DEFINE-PRIMITIVE-ROLE")
|| str.equalsIgnoreCase("DEFINE-PRIMITIVE-ATTRIBUTE")
|| str.equalsIgnoreCase("DEFPRIMROLE")
|| str.equalsIgnoreCase("DEFPRIMATTRIBUTE")) {
ATermAppl r = getTerm(in);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?