Adding ObjectPropety for an Ontclass using Jena API - jena

I intend to add object properties to classes using Jena API.
I can't find a proper way how to do this. I would like to achieve something similar to what can be done in Protege:
ExampleObjectProperty is my own ObjectProperty.
I tried adding this property using ontClass.addProperty, also adding a new statement to ontModel, but the result wasn't the same.
As far as I know, in Protege, blank node is generated (saying that :blank_node has some onProperty ExampleObjectProperty and ExampleClass has someValuesOf :blank_node... I'm not sure about this though).

loopasam's comment is correct; you're not trying to "add a property to a class" or anything like that. What you're trying to do is add a subclass axiom. In the Manchester OWL syntax, it would look more or less like:
ExampleResource subClassOf (ExampleObjectProperty some ExampleClass)
Jena's OntModel API makes it pretty easy to create that kind of axiom though. Here's how you can do it:
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class SubclassOfRestriction {
public static void main(String[] args) {
final String NS = "https://stackoverflow.com/q/20476826/";
final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
// Create the two classes and the property that we'll use.
final OntClass ec = model.createClass( NS+"ExampleClass" );
final OntClass er = model.createClass( NS+"ExampleResource" );
final OntProperty eop = model.createOntProperty( NS+"ExampleObjectProperty" );
// addSuperClass and createSomeValuesFromRestriction should be pretty straight-
// forward, especially if you look at the argument names in the Javadoc. The
// null just indicates that the restriction class will be anonymous; it doesn't
// have an URI of its own.
er.addSuperClass( model.createSomeValuesFromRestriction( null, eop, ec ));
// Write the model.
model.write( System.out, "RDF/XML-ABBREV" );
model.write( System.out, "TTL" );
}
}
The output is:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Class rdf:about="https://stackoverflow.com/q/20476826/ExampleResource">
<rdfs:subClassOf>
<owl:Restriction>
<owl:someValuesFrom>
<owl:Class rdf:about="https://stackoverflow.com/q/20476826/ExampleClass"/>
</owl:someValuesFrom>
<owl:onProperty>
<rdf:Property rdf:about="https://stackoverflow.com/q/20476826/ExampleObjectProperty"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
</rdf:RDF>
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<https://stackoverflow.com/q/20476826/ExampleObjectProperty>
a rdf:Property .
<https://stackoverflow.com/q/20476826/ExampleClass>
a owl:Class .
<https://stackoverflow.com/q/20476826/ExampleResource>
a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://stackoverflow.com/q/20476826/ExampleObjectProperty> ;
owl:someValuesFrom <https://stackoverflow.com/q/20476826/ExampleClass>
] .
In Protégé that appears as follows:
] .

Related

How to highlight QScintilla using ANTLR4?

I'm trying to learn ANTLR4 and I'm already having some issues with my first experiment.
The goal here is to learn how to use ANTLR to syntax highlight a QScintilla component. To practice a little bit I've decided I'd like to learn how to properly highlight *.ini files.
First things first, in order to run the mcve you'll need:
Download antlr4 and make sure it works, read the instructions on the main site
Install python antlr runtime, just do: pip install antlr4-python3-runtime
Generate the lexer/parser of ini.g4:
grammar ini;
start : section (option)*;
section : '[' STRING ']';
option : STRING '=' STRING;
COMMENT : ';' ~[\r\n]*;
STRING : [a-zA-Z0-9]+;
WS : [ \t\n\r]+;
by running antlr ini.g4 -Dlanguage=Python3 -o ini
Finally, save main.py:
import textwrap
from PyQt5.Qt import *
from PyQt5.Qsci import QsciScintilla, QsciLexerCustom
from antlr4 import *
from ini.iniLexer import iniLexer
from ini.iniParser import iniParser
class QsciIniLexer(QsciLexerCustom):
def __init__(self, parent=None):
super().__init__(parent=parent)
lst = [
{'bold': False, 'foreground': '#f92472', 'italic': False}, # 0 - deeppink
{'bold': False, 'foreground': '#e7db74', 'italic': False}, # 1 - khaki (yellowish)
{'bold': False, 'foreground': '#74705d', 'italic': False}, # 2 - dimgray
{'bold': False, 'foreground': '#f8f8f2', 'italic': False}, # 3 - whitesmoke
]
style = {
"T__0": lst[3],
"T__1": lst[3],
"T__2": lst[3],
"COMMENT": lst[2],
"STRING": lst[0],
"WS": lst[3],
}
for token in iniLexer.ruleNames:
token_style = style[token]
foreground = token_style.get("foreground", None)
background = token_style.get("background", None)
bold = token_style.get("bold", None)
italic = token_style.get("italic", None)
underline = token_style.get("underline", None)
index = getattr(iniLexer, token)
if foreground:
self.setColor(QColor(foreground), index)
if background:
self.setPaper(QColor(background), index)
def defaultPaper(self, style):
return QColor("#272822")
def language(self):
return self.lexer.grammarFileName
def styleText(self, start, end):
view = self.editor()
code = view.text()
lexer = iniLexer(InputStream(code))
stream = CommonTokenStream(lexer)
parser = iniParser(stream)
tree = parser.start()
print('parsing'.center(80, '-'))
print(tree.toStringTree(recog=parser))
lexer.reset()
self.startStyling(0)
print('lexing'.center(80, '-'))
while True:
t = lexer.nextToken()
print(lexer.ruleNames[t.type-1], repr(t.text))
if t.type != -1:
len_value = len(t.text)
self.setStyling(len_value, t.type)
else:
break
def description(self, style_nr):
return str(style_nr)
if __name__ == '__main__':
app = QApplication([])
v = QsciScintilla()
lexer = QsciIniLexer(v)
v.setLexer(lexer)
v.setText(textwrap.dedent("""\
; Comment outside
[section s1]
; Comment inside
a = 1
b = 2
[section s2]
c = 3 ; Comment right side
d = e
"""))
v.show()
app.exec_()
and run it, if everything went well you should get this outcome:
Here's my questions:
As you can see, the outcome of the demo is far away from being usable, you definitely don't want that, it's really disturbing. Instead, you'd like to get a similar behaviour than all IDEs out there. Unfortunately I don't know how to achieve that, how would you modify the snippet providing such a behaviour?
Right now I'm trying to mimick a similar highlighting than the below snapshot:
you can see on that screenshot the highlighting is different on variable assignments (variable=deeppink and values=yellowish) but I don't know how to achieve that, I've tried using this slightly modified grammar:
grammar ini;
start : section (option)*;
section : '[' STRING ']';
option : VARIABLE '=' VALUE;
COMMENT : ';' ~[\r\n]*;
VARIABLE : [a-zA-Z0-9]+;
VALUE : [a-zA-Z0-9]+;
WS : [ \t\n\r]+;
and then changing the styles to:
style = {
"T__0": lst[3],
"T__1": lst[3],
"T__2": lst[3],
"COMMENT": lst[2],
"VARIABLE": lst[0],
"VALUE": lst[1],
"WS": lst[3],
}
but if you look at the lexing output you'll see there won't be distinction between VARIABLE and VALUES because order precedence in the ANTLR grammar. So my question is, how would you modify the grammar/snippet to achieve such visual appearance?
The problem is that the lexer needs to be context sensitive: everything on the left hand side of the = needs to be a variable, and to the right of it a value. You can do this by using ANTLR's lexical modes. You start off by classifying successive non-spaces as being a variable, and when encountering a =, you move into your value-mode. When inside the value-mode, you pop out of this mode whenever you encounter a line break.
Note that lexical modes only work in a lexer grammar, not the combined grammar you now have. Also, for syntax highlighting, you probably only need the lexer.
Here's a quick demo of how this could work (stick it in a file called IniLexer.g4):
lexer grammar IniLexer;
SECTION
: '[' ~[\]]+ ']'
;
COMMENT
: ';' ~[\r\n]*
;
ASSIGN
: '=' -> pushMode(VALUE_MODE)
;
KEY
: ~[ \t\r\n]+
;
SPACES
: [ \t\r\n]+ -> skip
;
UNRECOGNIZED
: .
;
mode VALUE_MODE;
VALUE_MODE_SPACES
: [ \t]+ -> skip
;
VALUE
: ~[ \t\r\n]+
;
VALUE_MODE_COMMENT
: ';' ~[\r\n]* -> type(COMMENT)
;
VALUE_MODE_NL
: [\r\n]+ -> skip, popMode
;
If you now run the following script:
source = """
; Comment outside
[section s1]
; Comment inside
a = 1
b = 2
[section s2]
c = 3 ; Comment right side
d = e
"""
lexer = IniLexer(InputStream(source))
stream = CommonTokenStream(lexer)
stream.fill()
for token in stream.tokens[:-1]:
print("{0:<25} '{1}'".format(IniLexer.symbolicNames[token.type], token.text))
you will see the following output:
COMMENT '; Comment outside'
SECTION '[section s1]'
COMMENT '; Comment inside'
KEY 'a'
ASSIGN '='
VALUE '1'
KEY 'b'
ASSIGN '='
VALUE '2'
SECTION '[section s2]'
KEY 'c'
ASSIGN '='
VALUE '3'
COMMENT '; Comment right side'
KEY 'd'
ASSIGN '='
VALUE 'e'
And an accompanying parser grammar could look like this:
parser grammar IniParser;
options {
tokenVocab=IniLexer;
}
sections
: section* EOF
;
section
: COMMENT
| SECTION section_atom*
;
section_atom
: COMMENT
| KEY ASSIGN VALUE
;
which would parse your example input in the following parse tree:
I already implemented something like this in C++.
https://github.com/tora-tool/tora/blob/master/src/editor/tosqltext.cpp
Sub-classed QScintilla class and implemented custom Lexer based on ANTLR generated source.
You might even use ANTLR parser (I did not use it), QScitilla allows you to have more than one analyzer (having different weight), so you can periodically perform some semantic check on text. What can not be done easily in QScintilla is to associate token with some additional data.
Syntax highlighting in Sctintilla is done by dedicated highlighter classes, which are lexers. A parser is not well suited for such kind of work, because the syntax highlighting feature must work, even if the input contains errors. A parser is a tool to verify the correctness of the input - 2 totally different tasks.
So I recommend you stop thinking about using ANTLR4 for that and just take one of the existing Lex classes and create a new one for the language you want to highlight.

Fail to load turtle files using jena assembler file

I define an assembler file with name dataset2.ttl. The content of this file is:
#prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> .
[] ja:loadClass "com.hp.hpl.jena.tdb.TDB" .
tdb:DatasetTDB rdfs:subClassOf ja:RDFDataset .
tdb:GraphTDB rdfs:subClassOf ja:Model .
<#dataset> rdf:type tdb:DatasetTDB ;
tdb:location "DB" ;
tdb:unionDefaultGraph true ;
.
<#data1> rdf:type tdb:GraphTDB ;
tdb:dataset <#dataset> ;
tdb:graphName <http://example.org/data1> ;
ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>;];
.
The related jena code to create a datase is:
public class TDB {
public static void main(String[] args) {
Dataset ds = null;
try {
ds = TDBFactory.assembleDataset("Dataset2.ttl");
if(ds == null) {
System.out.println("initial tdb failed");
} else {
System.out.println("Default Model:");
Model model = ds.getDefaultModel();
ds.begin(ReadWrite.WRITE);
model.write(System.out, "TURTLE");
}
} finally {
if(ds != null) {
ds.close();
}
}
}
The content in data1.ttl is:
#prefix : <http://example.org/> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice
a foaf:Person ;
foaf:name "Alice" ;
foaf:mbox <mailto:alice#example.org> ;
foaf:knows :bob ;
foaf:knows :charlie ;
foaf:knows :snoopy ;
.
:bob
foaf:name "Bob" ;
foaf:knows :charlie ;
.
:charlie
foaf:name "Charlie" ;
foaf:knows :alice ;
.
A dataset has been created using this code. However, the content in the file of "data1.ttl" has not been read into the model. What is the problem of my code?
You also have
<#dataset> rdf:type tdb:DatasetTDB ;
tdb:location "DB" ;
tdb:unionDefaultGraph true ;
.
and
ds = TDBFactory.assembleDataset("Dataset2.ttl");
so you are asking Jena to assemble a dataset. That dataset will be <#dataset> (find by the type). It is not connected the graph you define so that's ignored; you can remove that part. Assembling the dataset is the way to do this.
You have tdb:unionDefaultGraph true so the default graph for query is the combination of all named graphs in the dataset.
Pick one out with model.getNamedModel.
If you use SPARQL, use the GRAPH keyword.
I would try validating your ttl file online to make sure they dataset2.ttl and data.ttl are both valid. I noticed you seem to add an extra semi-colon at the end when it's not needed (it should end with just a period).
try changing your line to this:
ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>] .
<#data1> rdf:type tdb:GraphTDB ;
tdb:dataset <#dataset> ;
tdb:graphName <http://example.org/data1> ;
ja:content [ja:externalContent <file:///C:/Users/data/data1.ttl>;];
.
Note the tdb:GraphTDB which means attach to a graph in the database. It does not load data with ja:content.
As a persistent store, it is expected that the data is already loaded, e.g.by tdbloader, not every time the assembler is used.

rdf reasoning rules via Jena

I want to express this relation: if article X has author Y, and the author has the influenceFactor medium or high(3 classes: low , medium, high) then this article is regarded as highly recommended.
(?x computer-science#hasAuthor ?y)(?y computer-science#hasInfluenceFactor computer-science#High) -> (?x computer-science#isImportant computer-science#importantfactor)
is my thought right?
here is some snippet of the ontology
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasAuthor">
<rdf:type rdf:resource="&owl;TransitiveProperty"/>
<owl:propertyDisjointWith rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#isAuthorOf"/>
<rdfs:range>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasAuthor"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#author"/>
</owl:Restriction>
</rdfs:range>
<rdfs:domain>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasAuthor"/>
<owl:someValuesFrom rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#article"/>
</owl:Restriction>
</rdfs:domain>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasInfluenceFactor">
<rdf:type rdf:resource="&owl;TransitiveProperty"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#author"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#influenceFator"/>
</owl:ObjectProperty>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#High">
<rdf:type rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#influenceFator"/>
</owl:NamedIndividual>
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#isImportant">
<rdf:type rdf:resource="&owl;TransitiveProperty"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#importantFactor"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#influenceFator"/>
</owl:ObjectProperty>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#importantFactor">
<rdf:type rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#importantFactor"/>
</owl:NamedIndividual>
sincere thanks to any viewer of my question :)
You don't need to create a rule to express your ontology, you can do it entirely in OWL. First we define an ontology based on your example, but with some new axioms. In particular, we define two new class expressions: InfluentialArticle and ImportantArticle. An influential article has high or medium impact, an important author wrote at least one influential article:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
#prefix owl: <http://www.w3.org/2002/07/owl#>.
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
#prefix cs: <http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#>.
# properties
cs:hasAuthor
a owl:ObjectProperty ;
rdfs:domain cs:Article ;
rdfs:range cs:Author .
cs:wrote
a owl:ObjectPropert ;
owl:inverseOf cs:hasAuthor .
# classes
cs:hasInfluenceFactor
a owl:ObjectProperty ;
rdfs:domain cs:Article ;
rdfs:range cs:InfluenceFactor .
cs:InfluenceFactor
owl:equivalentClass
[a owl:Class ;
owl:oneOf ( cs:high cs:medium cs:low )
].
cs:Author a owl:Class.
cs:Article a owl:Class.
# axioms
# an influential article has a high or medium impact
cs:InfluentialArticle
rdfs:subClassOf cs:Article ;
owl:equivalentClass [
owl:unionOf (
[a owl:Restriction ;
owl:onProperty cs:hasInfluenceFactor ;
owl:hasValue cs:high]
[a owl:Restriction ;
owl:onProperty cs:hasInfluenceFactor ;
owl:hasValue cs:medium
]
)
].
# an important author wrote an influential article
cs:ImportantAuthor
rdfs:subClassOf cs:Author ;
owl:equivalentClass [
a owl:Restriction ;
owl:onProperty cs:wrote ;
owl:someValuesFrom cs:InfluentialArticle
].
# individuals
# fred wrote a lousy paper
cs:fred
a cs:Author.
cs:article1
a cs:Article ;
cs:hasInfluenceFactor cs:low ;
cs:hasAuthor cs:fred.
# jane wrote a good paper
cs:jane
a cs:Author.
cs:article2
a cs:Article ;
cs:hasInfluenceFactor cs:high ;
cs:hasAuthor cs:jane.
Now we can write some Jena code to load this ontology and process it with a built-in reasoner:
package examples;
import java.util.Iterator;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
public class ReasonerExample
{
String NS = "http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#";
public static void main( String[] args ) {
new ReasonerExample().run();
}
public void run() {
OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
FileManager.get().readModel( m, "src/main/resources/comp-sci.ttl" );
// list all authors
System.out.println( "All authors:" );
OntClass author = m.getOntClass( NS + "Author" );
for (Iterator<? extends OntResource> i = author.listInstances(); i.hasNext(); ) {
System.out.println( " " + i.next().getURI() );
}
// list high impact authors
OntClass importantAuthor = m.getOntClass( NS + "ImportantAuthor" );
System.out.println( "Important authors:" );
for (Iterator<? extends OntResource> i = importantAuthor.listInstances(); i.hasNext(); ) {
System.out.println( " " + i.next().getURI() );
}
}
}
Which gives the following output:
All authors:
http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#jane
http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#fred
Important authors:
http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#jane
Note I also fixed your names to use OWL conventions: leading capital letter for classes, lower case for everything else. I also simplified the domain and range constraints, which were a bit weird.
Update
Following a comment from the original poster, I translated the ontology to RDF/XML using Jena's rdfcat tool as follows:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cs="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Class rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Article"/>
<owl:Class rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Author"/>
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasAuthor">
<rdfs:domain rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Article"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Author"/>
</owl:ObjectProperty>
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasInfluenceFactor">
<rdfs:domain rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Article"/>
<rdfs:range>
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#InfluenceFactor">
<owl:equivalentClass>
<owl:Class>
<owl:oneOf rdf:parseType="Collection">
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#high"/>
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#medium"/>
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#low"/>
</owl:oneOf>
</owl:Class>
</owl:equivalentClass>
</rdf:Description>
</rdfs:range>
</owl:ObjectProperty>
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#ImportantAuthor">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Author"/>
<owl:equivalentClass>
<owl:Restriction>
<owl:onProperty>
<owl:ObjectPropert rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#wrote">
<owl:inverseOf rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasAuthor"/>
</owl:ObjectPropert>
</owl:onProperty>
<owl:someValuesFrom>
<rdf:Description rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#InfluentialArticle">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Article"/>
<owl:equivalentClass rdf:parseType="Resource">
<owl:unionOf rdf:parseType="Collection">
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasInfluenceFactor"/>
<owl:hasValue rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#high"/>
</owl:Restriction>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#hasInfluenceFactor"/>
<owl:hasValue rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#medium"/>
</owl:Restriction>
</owl:unionOf>
</owl:equivalentClass>
</rdf:Description>
</owl:someValuesFrom>
</owl:Restriction>
</owl:equivalentClass>
</rdf:Description>
<cs:Article rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#article2">
<cs:hasInfluenceFactor rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#high"/>
<cs:hasAuthor>
<cs:Author rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#jane"/>
</cs:hasAuthor>
</cs:Article>
<cs:Article rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#article1">
<cs:hasInfluenceFactor rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#low"/>
<cs:hasAuthor>
<cs:Author rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#fred"/>
</cs:hasAuthor>
</cs:Article>
</rdf:RDF>

How to avoid building intermediates and useless AST nodes with ANTLR3?

I wrote an ANTLR3 grammar subdivided into smaller rules to increase readability.
For example:
messageSequenceChart:
'msc' mscHead bmsc 'endmsc' end
;
# Where mscHead is a shortcut to :
mscHead:
mscName mscParameterDecl? timeOffset? end
mscInstInterface? mscGateInterface
;
I know the built-in ANTLR AST building feature allows the user to declare intermediate AST nodes that won't be in the final AST. But what if you build the AST by hand?
messageSequenceChart returns [msc::MessageSequenceChart* n = 0]:
'msc' mscHead bmsc'endmsc' end
{
$n = new msc::MessageSequenceChart(/* mscHead subrules accessors like $mscHead.mscName.n ? */
$bmsc.n);
}
;
mscHead:
mscName mscParameterDecl? timeOffset? end
;
The documentation does not talk about such a thing. So it looks like I will have to create nodes for every intermediate rules to be able to access their subrules result.
Does anyone know a better solution ?
Thank you.
You can solve this by letting your sub-rule(s) return multiple values and accessing only those you're interested in.
The following demo shows how to do it. Although it is not in C, I am confident that you'll be able to adjust it so that it fits your needs:
grammar Test;
parse
: sub EOF {System.out.printf("second=\%s\n", $sub.second);}
;
sub returns [String first, String second, String third]
: a=INT b=INT c=INT
{
$first = $a.text;
$second = $b.text;
$third = $c.text;
}
;
INT
: '0'..'9'+
;
SPACE
: ' ' {$channel=HIDDEN;}
;
And if your parse the input "12 34 56" with the generated parser, second=34 is printed to the console, as you can see after running:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
TestLexer lex = new TestLexer(new ANTLRStringStream("12 34 56"));
TokenStream tokens = new TokenRewriteStream(lex);
TestParser parser = new TestParser(tokens);
parser.parse();
}
}
So, a shortcut from the parse rule like $sub.INT, or $sub.$a to access one of the three INT tokens, in not possible, unfortunately.

Can anyone help me convert this ANTLR 2.0 grammar file to ANTLR 3.0 syntax?

I've converted the 'easy' parts (fragment, #header and #member
declerations etc.), but since I'm new to Antlr I have a really hard
time converting the Tree statements etc.
I use the following migration guide.
The grammar file can be found here....
Below you can find some examples where I run into problems:
For instance, I have problems with:
n3Directive0!:
d:AT_PREFIX ns:nsprefix u:uriref
{directive(#d, #ns, #u);}
;
or
propertyList![AST subj]
: NAME_OP! anonnode[subj] propertyList[subj]
| propValue[subj] (SEMI propertyList[subj])?
| // void : allows for [ :a :b ] and empty list "; .".
;
propValue [AST subj]
: v1:verb objectList[subj, #v1]
// Reverse the subject and object
| v2:verbReverse subjectList[subj, #v2]
;
subjectList![AST oldSub, AST prop]
: obj:item { emitQuad(#obj, prop, oldSub) ; }
(COMMA subjectList[oldSub, prop])? ;
objectList! [AST subj, AST prop]
: obj:item { emitQuad(subj,prop,#obj) ; }
(COMMA objectList[subj, prop])?
| // Allows for empty list ", ."
;
n3Directive0!:
d=AT_PREFIX ns=nsprefix u=uriref
{directive($d, $ns, $u);}
;
You have to use '=' for assignments.
Tokens can then be used as '$tokenname.getText()', ...
Rule results can then be used in your code as 'rulename.result'
If you have rules having declared result names, you have to use these names iso.
'result'.

Resources