-
Notifications
You must be signed in to change notification settings - Fork 3
Creating an OntoUML model programmatically
Hi folks, John here.
In this short tutorial I'll explain you how to create a OntoUML model programmatically and serialize it in JSON format. First attach to your project the following library: net.menthor.ontouml.jar . Any other JAR library available for download as part of our Menthor API has OntoUML support, so you can choose that which suits you the most.
1) OntoUMLModel
An OntoUML model can be created using the OntoUML factory class. For instance, create a model named "Car Accident" as it follows:
OntoUMLModel m = OntoUMLFactory.createModel("Car Accident")
2) OntoUMLClass
Then, using the factory, create an OntoUML class named Person and stereotype it as a Kind. Don't forget to pass as argument the container too (model or package). NOTE: the class Person will be created inside that container.
OntoUMLClass person = OntoUMLFactory.createClass(ClassStereotype.KIND,"Person",m)
Alternatively you can use the model 'm' itself (or any other package) to create the Kind Person and other elements too:
OntoUMLClass person = m.createKind("Person")
OntoUMLClass vehicle = m.createKind("Vehicle")
These are the Class stereotypes allowed: Kind, Collective, Quantity, Relator, Mode, Quality, Role, Phase, SubKind, Category, Mixin, RoleMixin, PhaseMixin, Event and HighOrder.
3) OntoUMLAttribute
Create an attribute to the class Person named "age" stereotyped with the primitive value Integer.
def age = OntoUMLFactory.createAttribute(person, "age", PrimitiveStereotype.INTEGER, 1, 1)
These are the allowed Primitive stereotypes: Integer, Boolen, String, Real, DateTime, and Date.
4) OntoUMLGeneralization
Create a specialization between the Role Traveler and the Kind Person, as it follows:
OntoUMLClass traveler = m.createRole("Traveler")
m.createGeneralization(traveler, person)
You can of course use the OntoUMLFactory to do that too.
OntoUMLFactory.createGeneralization(traveler, person, m)
The main idea is that all these methods that create model elements should be found at the "OntoUMLFactory" or within a container such as an "OntoUMLPackage" or "OntoUMLModel".
5) OntoUMLGeneralizationSet
A generalization set groups two or more generalizations. Let us assume two Subkinds (Man and Woman) which are subtypes of Person. Create a generalization set {disjoint,covering} called "gender" as it follows:
OntoUMLClass man = m.createSubKind("Man")
OntoUMLClass woman = m.createSubKind("Woman")
OntoUMLGeneralization g1 = m.createGeneralization(man, person)
OntoUMLGeneralization g2 = m.createGeneralization(woman, person)
List gens = [g1, g2]
m.createGeneralizationSet("gender", true, true, gens) //isCovering and isDisjoint
An easy way to create partitions as the above is to use the method "createPartition" over specifics and general types as coded below:
List specifics = [man, woman]
m.createPartition("gender", specifics, person)
6) OntoUMLRelationship
Create a mediation named "has" between the Relator Travel[1..1] and the Role Traveler[1..-1].
def travel = m.createRelator("Travel")
OntoUMLFactory.createRelationship(RelationshipStereotype.MEDIATION, travel, 1, 1, "has", traveler, 1, -1, m)
Aternatively, just use the model 'm' for that.
m.createMediation(travel, 1, 1, "has", traveler, 1, -1)
These are the allowed Relationship stereotypes: ComponentOf, MemberOf, SubCollectionOf, SubQuantityOf, QuaPartOf, Constitution, Characterization, Mediation, Material, Formal, Derivation, Structuration, Participation, SubEventOf, Causation, Temporal, and InstanceOf.
Now, create a material relationship between Role Traveler[1..-1] and Kind Vehicle[1..1] and set this material relationship derived from the Relator Travel, as it follows:
OntoUMLRelationship material = m.createMaterial(traveler, 1, -1, "travels in", vehicle, 1, 1)
m.createDerivation(material, travel);
7) Serializing in JSON
To serialize the newly created model 'm' as JSON, create first an "OntoUMLSerializer". Then just pass model 'm' as argument to the following method:
OntoUMLSerializer s = new OntoUMLSerializer()
println s.toFormattedJSONString(m)
So this is it guys. I hope this first tutorial gives you a better understanding about our API and how it works regarding OntoUML. I'll probably continue to write more short tutorials in the following days so please feel free to comment and give us feedback too.
Cheers!