EOF Error Parsing Manchester Syntax in OWL-API - owl

I have an API that receive a JSON document with classes, properties and axioms of an ontology. The file looks like this:
{
"id": "myontologyid",
"outformat": "OWL",
"ontoclass": ["Person", "Man", "Woman", "Animal", "Rational", "Arm"],
"ontoaxioms": ["Man subClassOf (Person)", "Person EquivalentTo: (Man OR Woman)", "hasBrother max 2 xsd:integer"],
"ontoproperties": ["hasPart", "isBrotherOf", "hasBrother"]
}
The ontoaxioms key is an array with all the axioms of the ontology. The values of this array MUST be in Manchester syntax as I will use the ManchesterOWLSyntaxParser to parse.
When I try to parse this code, I get the following error on hasBrother max 2 xsd:integer axiom:
[apache-tomcat-8.5.69-2]: org.semanticweb.owlapi.manchestersyntax.renderer.ParserException: Encountered |EOF| at line 1 column 29. Expected one of:
SubClassOf:
or
and
DisjointWith:
EquivalentTo:
I believe the Manchester syntax is incorrect. But I couldn't find any reference or documentation of OWL-API which indicates how to use it. Is there some?
Below is part of my code which tries to parse the axioms:
ManchesterOWLSyntaxParserImpl parser = (ManchesterOWLSyntaxParserImpl) OWLManager.createManchesterParser();
parser.setOWLEntityChecker(entityChecker);
try {
for (int i = 0; i < this.axiomas.length(); i++) {
parser.setStringToParse(this.axiomas.getString(i));
owlOntology.addAxiom(parser.parseAxiom());
}
} catch (Exception e) {
System.out.print(e.toString());
return null;
}
The questions are:
How to solve this EOF error?
How to insert correctly Manchester Syntax into OWL-API?
Where can I find some documentation on how to use Manchester Syntax to parse ontologies?
Many thanks in advance.

Your use of OWLAPI classes appears correct. The problem with the input that something else is expected to follow, i.e., that's not a full axiom.
Is the intent to say that hasBrother can only appear twice for an individual and has integer range?
As it happens, there's a unit test in the OWLAPI contract module that uses this string as input for parsing:
String in = "p max 1 owl:real";
ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
parser.setStringToParse(in);
OWLClassExpression cl = parser.parseClassExpression();
The string has the same format as what you're trying to parse, and it gives a class expression, not an axiom - specifically, a qualified max cardinality restriction for a data property. This can be the superclass or the subclass in a subclass axiom, for example, but the rest of the axiom is not present.

Related

Swift : multi-dimensional structure organization and init

There are two simple problems here in initializing this structure.
One is the enunumerated value TS (I get error : Cannot convert value of type 'TournNames' to expected argument type 'TournamentName')
the other is initializing an array of strings (I get the error : Cannot convert value of type '[String]' to expected argument type 'TouramentScores'
Suppose I am trying to set up a structure to model the scores of tennis players and all of their matches in each of the major tournaments (just for fun). Each tournament has a name (e.g. Wimbledon) and a series of scores for that player (for example, in the opening match, their score might be "4-6, 6-4, 7-6, 6-2")... upto 7 matches in each tournament. Each player should have an array of four tournaments (names and array of scores), and eventually there should be an array of players. I am also trying to use enums not too successfully. Ideally, if I want to find how Roger Federer did in his third match of wimbledon this year, I would access something like player.tournament.wim.Roundof32 or something roughly like that. But before I can even get to playing with that, I can't seem to init dummy data for even a single tournament.
Any ideas? I don't think this is that hard of question but I just don't know each. See "*** this line" below for two lines that are problematic
// tournament name enum
enum TournNames : String {
case wim = "Wimbledom"
case fo = "French Open"
case ao = "Australian Open"
case uo = "US Open"
}
//
struct TournamentName {
var Tname : TournNames // = .wim
}
// This is the structure for a tournament score array with some dummy values.
struct TouramentScores {
var Match1 : String = "7-6, 6-4, 3-6, 7-6"
var Match2 : String = "7-6, 6-4, 3-6, 7-6"
}
// This is one entire Tournament record for one player = tournament name + array of scores ... the next goal but not used here until I get over these hurdles
struct TournamentResult {
var TournamentName : TournNames = .wim
var Scores : TouramentScores
}
// ... finally the structure of a player ...
struct DummyTennisPlayer {
var LastName : String // last name
var FirstName : String //first name
var TN : TournamentName
var TS : TouramentScores
// var WimRes : TournamentResult // to start a single tournament
// var SeasonResults : [TournamentResult] // ultimately should be an array of 4 tournaments
}
// trying to initialize some dummy data without success after trying several things
extension DummyTennisPlayer {
static var dummyResults : [DummyTennisPlayer] {
[
DummyTennisPlayer.init(
LastName : "Federer",
FirstName: "Roger",
TN : TournNames.wim // **** this line
,
TS : ["XX", "yy"] /// *** this line
)
]
}
}
As I think you're discovering, a simple series of nested types is unlikely to cut it here. As soon as you get to entities like players, tournaments, matches and lookups like "how Roger Federer did in his third match of wimbledon this year", you've become a candidate for using a database where you can manipulate one-to-many and many-to-many relationships. I can't tell you what database to use, and anyway that's a matter of opinion; from what you've said so far, SQLite would be sufficient (and I am personally not a fan of Core Data just for this kind of thing).
I guess your code is a kind of exercise, so before you go on later to Core Data or SQLite,
extension DummyTennisPlayer {
static var dummyResults: [DummyTennisPlayer] = [
DummyTennisPlayer(LastName: "Federer", FirstName: "Roger", WimbledomResult: TournamentResult(Scores: TouramentScores()))
]
}
should answer your question.
1 - To initialize a Swift struct, use the following syntax:
MyStruct(property1: "my property1 value", property2: "my property2 value")
2 - the tournament name property in TournamentResult is already set to .wim so you just need to initialize the Scores. As your TournamentScores properties are already all set, you just need to pass an instance of TournamentScores() to TournamentResult(Scores:).
By the way, only use lowercases for the first letter of the name of your variables or struct properties: var lastName or TournamentResult(scores:).
I think you are confusing the term "multi-dimensional (array) structures" (which are just arrays nested inside other arrays, like that: [ [1, 2, 3], [2, 3, 4], [3, 4, 5]]) with the struct objects. You are probably not supposed to use structs so extensively here.
Don't hesitate to review the way you decide to use enums, structs, or arrays. Your code may work but will be difficult to read and use (example: how would you access a specific set score if you put all of the set scores in a single String? Why not use an array?)

How can i get a random line from a text file in sml ? How to make a multi choice question program in sml?

I want to make a multi choice question program in SML.
I have a text file whose content is structured as follows:
category1:Basic sml/sql
1-How many subsets does the power set of an empty set have?
A) One
B) Two
C) Three
D) Zero
Ans:A
2-What is the cardinality of the set of odd positive integers less than 10?
A) 20
B) 3
C) 5
D) 10
Ans:C
Each category has more than 5 questions
Each question belongs to a category
For each question there are 4 proposed answers followed by the answer on another line
I would like, to be able to retrieve and display to the user just the question (a random question) and the corresponding proposed answers. I have written this function that allows me to retrieve line by line all the content of my file. But I'm still stuck on how to unload a question-answer block.
fun getFromFile(file_name) =
let
val file = TextIO.openIn file_name
val text = TextIO.inputAll file
val _ = TextIO.closeIn file
in
String.tokens (fn c => c = #"\n") text
end
val table = getFromFile("question_file.txt");
How could I proceed? Is it possible to retrieve the lines of the file without passing them through a table first (retrieve the text directly)?
I'm still stuck on how to unload a question-answer block.
How could I proceed?
Find a way to encode multiple categories that contain multiple questions that contain multiple answers each. And once you have found a way to store that to a file (a file format), write a decoder. Your current decoder is a line decoder. You can encode things within things within things using lines, but you can also do it other ways.
For example, using JSON:
[
{
"category": "Basic sml/sql",
"questions": [
{
"question": "How many subsets does the power set of an empty set have?",
"answers": [
{ "answer": "Zero", "correct": false },
{ "answer": "One", "correct": true },
{ "answer": "Two", "correct": false },
{ "answer": "Three", "correct": false }
]
},
...
]
},
...
]
If relying on third-party libraries seems too difficult, you could come up with a file format yourself, e.g. a line-based one:
CATEGORY Basic sml/sql
QUESTION How many subsets does the power set of an empty set have?
ANSWER Zero
ANSWER_CORRECT One
ANSWER Two
ANSWER Three
QUESTION ...
So given your line-based reader, loop over each line and look at the first word:
If it's CATEGORY, start a new category.
If it's QUESTION, start a new question within the current category.
If it's ANSWER or ANSWER_CORRECT, provide an option in the current question within the current category.
This suggests a recursive function (since it needs to go over each line) that takes a number of parameters: The current category, the current question, and the total set of categories, questions and answers so far.
At this point you probably have to think: How do I store categories of questions with multiple answers in memory? What data type should I be using? E.g. using type aliases, you could express your data model like this:
type answer_option = string * bool
val example_answer_option = ("Zero", false) : answer_option
type question_answers = string * answer_option list
val example_question_answers =
("How many subsets does the power set of an empty set have?",
[
("Zero", false),
("One", true),
("Two", false),
("Three", false)
]
) : question_answers
type category = string * question_answers list
val example_category =
("Basic sml/sql",
[ example_question_answers ]
) : category
val example_categories = [ example_category ] : category list
The way SML type aliases work is that you get all of those expanded into the primitive types they consist of, so they may show up in your REPL as such:
> type answer_option = string * bool
type question_answers = string * (string * bool) list
type category = string * (string * (string * bool) list) list
which is considerably less readable and is one reason to use alternatives like datatype, abstype or opaque modules.
Going with this, however, you may define a stub like:
fun parse (line::lines, currentQuestion, currentAnswers, currentCategory, acc) =
case splitFirstWord line of
("CATEGORY", cat) => ...
| ("QUESTION", q) => ...
| ("ANSWER", aWrong) => ...
| ("ANSWER_CORRECT", aRight) => ...
| _ => raise Fail ("Unknown: " ^ line)
Now there are two sub-problems:
splitFirstWord doesn't actually exist (yet).
There is a whole lot of book-keeping of current state.
Good luck!
Is it possible to retrieve the lines of the file without passing them through a table first (retrieve the text directly)?
I don't really understand this question. Unarguably, yes?
If by "table" you mean some kind of indexable data structure like a list:
Just don't call String.tokens (fn c => ...) on the input.
This gives you a basic string.
Note that table is just the name of a value binding.
If you like, you can pass it through a chair instead:
fun getFromFile(file_name) =
let
val file = TextIO.openIn file_name
val text = TextIO.inputAll file
val _ = TextIO.closeIn file
in
text
end
val chair = getFromFile "question_file.txt"
Note also that the parenthesis around function arguments is not necessary in SML. In fact, if you think they are, you'll probably make a syntax mistake soon enough. Try to avoid redundant syntax for greater clarity.

Find all IRIs used in ontology which are not entities?

Is there any straight-forward way in OWLAPI to find all IRIs used in an ontology which have not been identified as Entities i.e. have not been declared and are not used in a context which would allowed them to be identified as a specific entity type? Hoping for something analogous to OWLOntology.signature(), didn't see anything.
An example of that situation appears in BFO 2.0 (http://purl.obolibrary.org/obo/bfo.owl) :
<rdf:Description rdf:about="http://example.com/bfo-spec-label">
<obo:IAO_0000119>Person:Alan Ruttenberg</obo:IAO_0000119>
</rdf:Description>
Here http://example.com/bfo-spec-label is just a "bare" IRI of unknown entity type and thus does not appear in the ontology signature.
Couldn't find any elegant way to find all of these bare IRI's but these can be found by looking in all places they could possibly occur. A simple method would look like:
private List findBareIRIs(OWLOntology onto) {
List bares = new ArrayList();
// bare IRIs can occur as AnnotationSubjects, AnnotationObjects or the domain/range of AnnotationProperties
List<OWLAnnotationAssertionAxiom> asserts = OWLAPIStreamUtils.asList(onto.axioms(AxiomType.ANNOTATION_ASSERTION));
List<OWLAnnotationPropertyDomainAxiom> domains = OWLAPIStreamUtils.asList(onto.axioms(AxiomType.ANNOTATION_PROPERTY_DOMAIN));
List<OWLAnnotationPropertyRangeAxiom> ranges = OWLAPIStreamUtils.asList(onto.axioms(AxiomType.ANNOTATION_PROPERTY_RANGE));
//check the subject and values of each AnnotationAsertion
for (OWLAnnotationAssertionAxiom ax : asserts) {
OWLAnnotationSubject subj = ax.getSubject();
OWLAnnotationValue value = ax.getValue();
if (subj.isIRI()) {
bares.add((IRI) subj);
}
if (value.isIRI()) {
bares.add((IRI) value);
}
}
// check the domain and ranges of each AnnotationProperty
for (OWLAnnotationPropertyDomainAxiom ax : domains) {
bares.add(ax.getDomain());
}
for (OWLAnnotationPropertyRangeAxiom ax : ranges) {
bares.add(ax.getRange());
}
return bares;
}

Solr query to find one letter without other letter around

I have documents in my solr already indexed. I want to find Producer and model in tire.
I have file with producer and model like this:
Nokian;WR G2 SUV
Nokian;WR SUV
Nokian;V
Query:
((productname:"NOKIAN" OR producer:"NOKIAN") AND (productname:"V" OR description:"V" OR referencenumber:"V"))
But it found for example this:
"2X NOKIAN 215/55 R17 94V LINE (3)"
Because in this product speed index is V and here model is Line. My algorithm take this product for Nokian;V not for Nokian;Line.
How to ask solr to gives me only this product where this V don't have any other letters around?
LETNIE 225/45/17 94V NOKIAN V FINLAND - PŁOTY
This found beautiful. Its Nokian;V.
As far as I understand your question you need to put MUST quantifier before each boolean clause. So query will look like:
(
+(productname:"NOKIAN" OR producer:"NOKIAN") AND
+(productname:"V" OR description:"V" OR referencenumber:"V")
)
If your productname field is of type text it has the WordDelimiterFilter in the analysis chain. One of the default behaviors of this filter is to split terms on letter-number boundaries causing:
2X NOKIAN 215/55 R17 94V LINE (3)
to generate the following tokens:
2 X NOKIAN 215 55 R 17 94 V LINE 3
(which matches the "V" in your query).
You can always run debug=results to get an explanation for why something matches. I think in this particular case, you might construct another field type for your productname field that analyzes your model string less aggressively.
I solved the problem in such a way that sorted out brand,model Dictionary. I used my own comparer.
public class MyComparer : IComparer<string>
{
int IComparer<string>.Compare(string x, string y)
{
if (x == y)
{
return 0;
}
if (x.Contains(y))
{
return -1;
}
else
{
return 1;
}
}
}
All model that have V or H now are on the end of Dcitionary. It's works very well. Because first solr searched Nokian;Line and this product where found add to other list alreadyFound and skip this product where found model. Thanks all for your reply.

How Do I Get A Dataype's EquivalentClass in OWL API?

In the OWL API, I am unable to find a way to retrieve the equivalent class for a datatype that defines an enumeration of valid values. When I have an OWLDatatype in hand, how do I get a set of allowed values?
[I tried pasting RDF/XML as a code block here, but it doesn't work. I even looked at the markdown help. Please tell me how to do that.]
The ontology is using the following construct:
rdfs:Datatype
owl:equivalentClass
rdfs:Datatype
owl:oneOf
rdf:Description
rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"
If understand correctly, you have a specific class "c" that has been defined as equivalent to oneOf many individuals, then I think this is one way to get those "allowed values":
Set<OWLClassAxiom> allAx=localOntology.getAxioms(c);
for(OWLClassAxiom ax: allAx){
if(ax.getAxiomType()==AxiomType.EQUIVALENT_CLASSES)
for(OWLClassExpression nce :ax.getNestedClassExpressions())
if(nce.getClassExpressionType()==ClassExpressionType.OBJECT_ONE_OF)
for(OWLNamedIndividual temp: nce.getIndividualsInSignature())
System.out.println(temp);
}
Here's what I came up with:
for (OWLDatatype dt : o.getDatatypesInSignature(Imports.INCLUDED)) {
logger.info("found datatype {} labeled '{}'", dt, getOWLEntityLabel(dt));
Set<OWLDatatypeDefinitionAxiom> datatypeDefinitions = o.getDatatypeDefinitions(dt);
for (OWLDatatypeDefinitionAxiom definitionAxiom : datatypeDefinitions) {
logger.info("found datatype definition '{}'", definitionAxiom);
OWLDataRange dataRange = definitionAxiom.getDataRange();
if ( ! dataRange.isDatatype()) {
logger.info("looks like an enumeration");
OWLDataOneOf owlDataOneOf = (OWLDataOneOf) dataRange;
Set<OWLLiteral> values = owlDataOneOf.getValues();
for (OWLLiteral value : values) {
logger.info("Found literal value '{}'", value.getLiteral());
}
}
}
}
I really don't like the cast to OWLDataOneOf. There must be a better way.

Resources