Trouble in understanding what should be a subclass or just an instance of a class - owl

Hi im having a bit of trouble understanding what should be a subclass or just merely an instance of a class in OWL ontologies
If i use an example of World Leaders
You have Presidents, Prime ministers, Chancellors etc
Every one of these is a world leader
So far i have tried to suggest that
All three of these are a subclass of World leader
But if i want to throw in Head Of State its where i start to get confused because some world leaders are also heads of state but some arent for example some prime ministers arent head of state
So if i were to throw in head of state would i be correct in assuming that
Head of state would be a subclass of world leader because not every leader is a head of state but every head of state is a leader?
And would President be an instance of head of state or its own subclass? And Also would Prime minister and chancellor be seperated from President like that so Prime minister and Chancellor be subclasses of World leader or actually instances of
Is there a general rule of thumb or guideline of how best to approach this?
Any help would be appreciated thanks

World Leaders, Presidents, Prime ministers, Chancellors, Heads of State - all of these represent sets which can have many members. Whenever what you are modelling can have multiple members, it is usually best to model using class axioms like subclass rather than individuals.
If you have A subclass B and C subclass D there is nothing stopping you from having an individual x that is a member of both A and D, except if you have an axiom stating for example stating A disjoint C or B disjoint D.

Related

Using Same Object Property between Multiple Classes

I tried to use the same object property between multiple classes, but I got a warning that the same object property has been set multiple times as follows, can you please let me know what is wrong with that and how to solve it? Does this restrict reasoning later on (i.e. confuse the reasoner since the same object property is set multiple times)?
Thanks
Contrary to the comments it actually is very problematic to use the same object property between multiple classes.
What you don't see in your visualization is that in RDF/OWL, the starting point of your relation arrows is modelled as rdfs:domain and the target point of the arrows is modelled as rdfs:range.
The semantic of an OWL class is that it is a set of individuals and rdfs:domain and rdfs:range specify the domain, respectively range of a relation. That is, they specify the sets of allowed values in subject, respectively object position of a triple with that relation in the predicate position.
If you express your example in RDF Turtle it would look like this:
:hasPart rdfs:domain :ClassA, :ClassB;
rdfs:range :ClassB, :ClassC, :ClassD.
This causes multiple problems:
Intersection
The semantic of multiple domains, respectively ranges, is the intersection of those classes, not the union!
Even if we disregard the relation between :ClassB and :ClassD, this means :hasPart is only allowed by individuals that are instances of class C and class D at the same time!
For example, lets say class A is "car", class B is "tire" and class C is "motor". Now you want to say that a car can have tires and motors but what you actually specify is that a car can only have things that are a motor and a tire at the same time!
Unwanted but allowed usage
If you fix the first problem but specifying the union instead of the intersection, it will be very messy in serialized RDF because you need a large amount of triples to represent OWL restrictions such as unions. But even if you do that, you could now connect an instance of class A with an instance of class D, which is not allowed in the image.
Solution
The solution is simple, just separate those relations, for example into :hasB, :hasC and :hasD, although in a real scenario you can probably find a more suitable naming scheme, where the "has" prefix is often not used. For example, in DBpedia, the property for the country of a city is expressed using dbo:country, not dbo:hasCountry.

What is the main difference between instances and sub-classes?

This question is about Ontologies , I am implementing an ontology about potential physical attacks on wireless sensors as devices.
I created class Modification_Attack meaning attacks which modify in the sensor itself. I created Programming_Modification as an instance of this class, is this logical, is it better a sub-class or neither an instance nor a sub-class?
What is the main difference between instances and sub-classes?
This question isn't really unique to OWL ontologies; it comes up in object-oriented programming, and in set theory in mathematics. A class is a collection of its members. E.g.,
Person ≡ { person1, person2, … }
Suppose you have another class:
TallPerson ≡ { person63, person102, … }
A class A is a subclass of the class B if every element of A is also an element of B:
(A ⊆ B) ≡ (x ∈ A → x ∈ B)
So, for instance, if every member of TallPerson is also a member of Person, then TallPerson is a subclass of Person.
I created class Modification_Attack meaning attacks which modify in
the sensor itself.I created Programming_Modification as an instance of
this class , is this logical , is it better a sub-class or neither an
instance nor a sub-class?
This is a choice that you need to make. It really depends on the context and how you intend to use the ontology. If you're observing attacks in the wild and trying to categorize them, you probably want a subclass, but you'll want to have instances of Programming_Modification. That is, you'll want something like:
Programming_Modification ≡ { attack24, attack89, … }
But if you've got some other kind of use case, where you you want to talk about Programming_Modification as a single entity, then it might make more sense for it to be an individual.
I have been thinking through the same issue. And I think the same thing can be both an instance and a subclass depending on whether you plan to use it conceptually or not, which can be simplified to whether you would describe it using the or a.
For example, I am working on a materials ontology, where the main class is Material. A particular type of material is Steel. If I say the steel then steel an instance of Material. If I say a steel then Steel is a subclass of Material. Further, steel is an instance of Steel. And it would be more appropriate to also give it some kind of meaningful identifier like steel-1.
It all seems to come down whether you are using a term as a conceptual class or as a name for some instance of that class. In your case, I think it would probably be more appropriate to say Programming_Modification is a subclass as you could probably have many of such modifications which you would refer to as the first Programming_Modification or the second Programming_Modification . Would love to see what you came up with though if you have a solution.

Why has Pellet inferred an inconsistant "Nothing" subclass with infinite "Entailment" justifications?

I'm learning OWL 2.0 using Protégé 4.0 and Pellet 2.2, and I'm trying to understand underlying concepts progressively.
So, starting with class hierarchies, I made one class "Cat" as a SubClassOf "Things".
If I start Pellet, everything works fine, but if I DL query "Cat", I have Directsubclasses "Nothing" and Subclasses "Nothing" appearing in red.
What does it mean?
Why is it red?
Justifications are like :
Explanation for: Nothing SubClassOf Cat
<Entailment1252345325436>SubClassOf Nothing and (not (Cat))
What does it mean?
Why are there like an infinity of these "Entailment" justifications (I mean I only created 1 class).
Is there an error I made I have to correct to avoid this phenomenon? Or should I just don't care about it?
Thank you ^_^!
Nothing is the empty class, sometimes written as &bottom; in DL notation. In most cases, if you have a class that's equivalent to Nothing, it's a modeling error. E.g., if you accidentally define
Car EquivalentClass (hasDoors exactly 2)
Car EquivalentClass (hasDoors exactly 4)
you'll find that Car is now equivalent to Nothing because something can't have exactly 2 and exactly 4 doors. That's an accident. There's nothing logically incorrect with it, but usually you don't want your classes to necessarily be empty. So, Protege shows necessarily empty classes in red.
Of course, the empty class, Nothing, is a subclass of every class, just like the empty set is a subset of every set, and it's necessarily empty.
So, there's actually nothing wrong with what you're seeing. Nothing should be red, and it should be a subclass of every class. Everything is working the way that it should.

is it wasteful/bad design to use a vector/list where in most instances it will only have one element?

is it wasteful/bad design to use a vector/list where in most instances it will only have one element?
example:
class dragon
{
ArrayList<head> = new ArrayList<head> Heads;
tail Tail = new tail();
body Body = new body();
dragon()
{
theHead=new head();
Heads.add(theHead);
}
void nod()
{
for (int i=0;i<Heads.size();i++)
{
heads.get(i).GoUpAndDown();
}
}
}
class firedragon extends dragon
{
}
class icedragon extends dragon
{
}
class lightningdragon extends dragon
{
}
// 10 other one-headed dragon declarations here
class hydra extends dragon
{
hydra()
{
anotherHead=new head();
for (int i=0;i<2;i++)
{
Heads.add(anotherHead);
}
}
}
class superhydra extends dragon
{
superhydra()
{
anotherHead=new head();
for (int i=0;i<4;i++)
{
Heads.add(anotherHead);
}
}
}
EDIT:(part 2 of the question)
Thanks for the replies. They have been very helpful.
I've actually run into this situation more than once, and I'd like to give a second example that is unrelated to inheritance. This is actually a real-world example, and though I've decided my current project is small scale enough to be safe with using vectors based on your answers, it's a concept that I imagine I'll use on a much larger scale at some point.
In creating an engine for my current Android game project, I found it necessary to create an Imagepoint object that is basically a set of XY coordinates that are used to track significant parts of a sprite image. Say you have a bone character made up of several bitmaps, it's useful to know where the neck attaches to the torso, the forearm to the bicep, etc. This xy offset data is used later to calculate the position for various purposes, such as where to position other sprites by using trigonometric functions to find the xy offset given the current angle.
Most sprite objects will need only one image point. The bicep needs one for the forearm, the forearm needs one for the hand, the neck needs one for the head, etc. The torsos in my current character models are the exception, and they need several for the shoulders, the legs, the neck, and any removable decorative sprites.
The sprite is the object that contains the imagepoint vectors, there will be no inherited classes, because each : torso, leg, etc is simply an instance of the sprite class, which is strictly a graphical object, containing a pointer to the bitmap, these imagepoints, and assorted position/orientation data, and will not be used for any specialized purposes. If I were to use this concept on a large scale game with massive numbers of instances, where most would require only a single imagepoint, with a few objects requiring several or none, and where there would be no special cases meriting the use of inheritance. What would your thoughts be?
Yes and no.
I mean your question really depends on how are you going to use these objects.. personally I like to split behaviours and attributes as much as possible to "clusterize" them according to the kind of object they represent without caring about reusing some code a little bit less compared to a really inheritance approach like the one you proposed. Something like:
interface BigLizard
{
void nod();
}
class Dragon implements BigLizard
{
Head head;
void nod() { head.upAndDown(); }
}
class ReallyScaryDragon extends Dragon { ... }
class Hydra implements BigLizard
{
ArrayList<Head> heads;
void nod() { for (Head h : heads) h.upAndDown(); }
}
And so on. This approach is like "having just exactly what you need for your objects without force anything to be a specific case". Of course this means that it will have two different implementations of nod() but when you don't need to optimize (and you should care about it only at the end) just stick with your personal preference, assuming that it doesn't go against any standard, widely accepted convention.
You don't have to make anything inherit from a single object, just customize the hierarchy as you like.. also a composite solution (like already suggested) would be a solution also if the concept of "head part" is not the same for your Dragon and for your Hydra..
A third solution for your specific case could be to use a LinkedList instead that an ArrayList. In this case whenever you have just one head you won't waste space (except for instantiation of the list) and you'll have just the next pointer of the head (of the list :D ) pointing nowhere.
If the head is publicly accessible, it'll need to be available as an array/list/collection so that consumers can deal with it.
Another way of dealing with this may be to switch to a more behavioral design rather than a stateful design. In other words, instead of having code telling the dragon to nod, have the dragon respond to an event that would cause him to nod, if that makes sense.
say("Dragon, would you like a yummy princess?");
if(dragon.Stomach.Contents.IsEmpty)
{
dragon.Nod();
}
compared with:
say("Dragon, would you like a yummy princess?");
dragon.offerFood(princess);
// in dragon class
void offerFood(Food yummyFood)
{
if(Stomach.Contents.Empty)
{
head.Nod(); // can be overridden by subclasses.
}
}
In the second example, the consumer of the dragon class isn't asserting authority to make the dragon do things... all the consumer is doing is things (saying things, offering a princess) that he has the authority to do - the dragon gets to decide whether it wants to nod its head or not (which makes sense. I doubt that I'd be able to get a dragon to nod its head if it didn't want to!)
I call this 'behavioral' because the interfaces focus on the interactions between the objects (the dragon and whatever is offering the yummy princess) rather than the state of the dragon (its heads).
I don't think there is a single answer to this question. It seems to me like it would be application dependent. If the application is a relatively lightweight dragon simulation that will never deal with more than a few thousand dragons at any one time, then it probably isn't a big deal to use a vector. If, though, it is a game involving millions of dragons, then it might make sense to optimize it slightly differently and maybe save a bit of memory and possibly shorten access time. But even that probably depends on the underlying class implementation. It is quite possible that the vector class you use could be optimized to handle a single element without extra overhead.
In this particular case, it'd probably be better to override nod() for your multi-headed dragons. Whether they have 1 head or 5 is a detail that really shouldn't be worried about in the base class (which should be as simple as you can reasonably make it).
You don't want to have to account for every possibility that every one of your code's users (even you) will ever need at every point in time. Otherwise, you'd need to account for 2-headed dogs (which are a lot more real than even 1-headed dragons). People extending your classes and such will be developers, too; let them do the work to account for the more exotic/bizarre cases, if they need to.
You could change the Head implementation to be a Composite type. That way, each extension of Dragon needs only one instance of Head and the Head takes care of GoUpAndDown().
I would implement such features in deriving classes. To make this behaviour work, you have to be able to override methods (in your case, the nod() method). The baseclass shouldn't cater for all possibilities, it should present the base case, which can be extended. Just make the appropiate methods overrideable, so the deriving classes can implement the specific behaviour, and everything will work out fine.

OOP Best Practices When One Object Needs to Modify Another

(this is a C-like environment) Say I have two instance objects, a car and a bodyShop. The car has a color iVar and corresponding accesors. The bodyShop has a method named "paintCar" that will take in a car object and change its color.
As far as implementation, in order to get the bodyShop to actually be able to change a car object's color, I see two ways to go about it.
Use the "&" operator to pass in a pointer to the car. Then the bodyShop can either tell the car to perform some method that it has to change color, or it can use the car's accessors directly.
Pass in the car object by value, do the same sort of thing to get the color changed, then have the method return a car object with a new color. Then assign the original car object to the new car object.
Option 1 seems more straightforward to me, but I'm wondering if it is in-line with OOP best practices. In general for "maximum OOP", is the "&" operator good or bad? Or, maybe I'm completely missing a better option that would make this super OOPer. Please advise :)
Option 1 is prefered:
The bodyShop can either tell the car
to perform some method that it has to
change color, or it can use the car's
accessors directly.
Even better still...create an IPaintable interface. Have Car implement IPaintable. Have BodyShop depend on IPaintable instead of Car. The benefits of this are:
Now BodyShop can paint anything that implements IPaintable (Cars, Boats, Planes, Scooters)
BodyShop is no longer tightly coupled to Car.
BodyShop has a more testable design.
I would assume that the responsibility of the bodyShop is to modify car objects, so #1 seems like the right way to go to me. I've never used a language where the "&" operator is necessary. Normally, my bodyShop object would call car.setColor(newColor) and that would be that. This way you don't have to worry about the rest of the original car's attributes, including persistence issues - you just leave them alone.
Since you're interested in the best OOP practice, you should ignore the performance hit you get with option 2. The only things you should be interested in is do either option unnecessarily increase coupling between the two classes, is encapsulation violated and is identity preserved.
Given this, option 2 is less desirable since you can't determine which other objects are holding references to the original car or worse, contain the car. In short you violate the identity constraint since two objects in the system may have different ideas of the state of the car. You run the risk of making the overall system inconsistent.
Of-course your particular environment may avoid this but it certainly would be best practice to avoid it.
Last point, does your bodyShop object have state; behaviour and identity? I realise that you have explained only the minimum necessary but possibly the bodyShop isn't really an object.
Functional v OO approaches
As an interesting aside, option 2 would close to the approach in a functional programming environment - since state changes are not allowed, your only approach would be to create a new car if it's colour changed. That's not quite what you're suggesting but it's close.
That may sound like complete overkill but it does have some interesting implications for proving the correctness of the code and parallelism.
Option 1 wins for me. The & operator is implicit in many OO languages (like Java, Python etc). You don't use "passing by value" in that languages often - only primitive types are passed in that way.
Option 2 comes with multiple problems: You might have a collection of cars, and some function unaware of it might send a car to bodyShop for painting, receive new car in return and don't update your collection of cars. See? And from more ideologic point of view - you don't create new object each time you want to modify it in real world - why should you do so in virtual one? This will lead to confusion, because it's just counterintuitive. :-)
I am not sure what this "C-like environment" mean. In C, you need this:
int paintCar(const bodyShop_t *bs, car_t *car);
where you modify the contents pointed by car. For big struct in C, you should always pass the pointer, rather than the value to a function. So, use solution 1 (if by "&" you mean the C operator).
I too agree with the first 1. I can't say it's best practice because i'm never really sure what best practice is in other peoples minds... I can tell you that best practice in my mind is the most simple method that works for the job. I've also seen this aproach taken in the hunspell win api and other c-ish api's that i've had to use. So yea i agree with scott.
http://hunspell.sourceforge.net/
//just in-case your interested in looking at other peoples code
It depends on whether the body shop's method can fail and leave the car in an indeterminate state. In that case, you're better off operating on a copy of the car, or a copy of all relevant attributes of the car. Then, only when the operation succeeds, you copy those values to the car. So you end up assigning the new car to the old car within the body shop method. Doing this correctly is necessary for exception safety in C++, and can get nasty.
It's also possible and sometimes desirable to use the other pattern - returning a new object on modification. This is useful for interactive systems which require Undo/Redo, backtracking search, and for anything involving modelling how a system of objects evolves over time.
In addition to other optinions, option 1 lets paintCar method return a completion code that indicates if the car has changed the color successfully or there were problems with it

Resources