What is the SWRL Factory?    (6L0)

The SWRL Factory is a component of the SWRLTab that provides a Java API that can be used to manipulate SWRL rules. It can be used to create and modify SWRL rule elements in an OWL ontology programatically. This API can be used by developers who wish to work with SWRL rules in their applications.    (6L1)

This FAQ assumes that you have some familiarity with the Protege-OWL API and, of course, SWRL.    (6L2)

Where is the SWRL Factory located?    (6L3)

The SWRL Factory is part of Protege-OWL SWRLTab and its source code is bundled with the Protege-OWL source code. The Protege-OWL source code is available for download from the Protege Subversion repository. The API for this factory is provided by the SWRLFactory class.    (8L6)

How do I activate the SWRL Factory?    (6L6)

If an OWL ontology refers to the SWRL namespace, then Protege-OWL will automatically activate the SWRL Factory, which ensures that the correct run-time mappings are created between OWL individuals and the Java instances returned by the SWRL Factory.    (6L7)

An instance of the SWRLFactory can be created by passing an OWL model to its constructor:    (6L8)

OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);    (6L9)

How are rules represented by the SWRL Factory?    (6N5)

SWRL rules are stored as OWL individuals. These individuals are described by OWL classes contained in the SWRL Ontology.    (6LA)

The highest level class in this ontology is swrl:Imp, which is used to represent a single SWRL rule. It contains an antecedent part, which is referred to as the body, and a consequent part, which is referred to as the head. Both the body and head are instances of the swrl:AtomList class, which represents a list containing rule atoms.    (6LB)

The abstract swrl:Atom class is used to represent a rule atom. The various types of atoms described in the SWRL Submission are described by subclasses of this class. These include class atoms, built-in atoms, data range atoms, and so on.    (6LC)

The SWRL ontology also includes a class called swrl:builtin to describe built-ins.    (6LD)

Finally, the SWRL ontology includes a class called swrl:Variable that can be used to represent variables. All variables used in SWRL rules are represented as instances on this class.    (6LE)

The SWRL Factory provides a mapping from the OWL individuals that represent SWRL rules to analagous Java objects. All classes described in the SWRL Ontology have a direct Java equivalent class to represent them. The factory has utility functions to create Java instances of all of these classes. When one of these Java instances is created an equivalent OWL individual is also created in the knowledge base.    (6LF)

Factory methods to create these classes are described in the remainder of this FAQ.    (6N6)

How do I create Java rule objects with the SWRL Factory?    (6N7)

The factory provides methods to create SWRLImp and SWRLAtomList Java classes that can be used to represent instances of the equivalent swrl:imp and swrl:AtomList OWL classes. These methods are called createSWRLImp and createSWRLAtomList, respectively.    (6LG)

In general, a class in the SWRL Ontology named swrl:AClass will have an equivalent Java class called SWRLAClass. The SWRLFactory class will provide a method called createSWRLAClass to create Java instances of this class. All of these classes are located in the package:    (6LH)

edu.stanford.smi.protegex.owl.swrl.model    (6N8)

How do I create a rule using the SWRL Factory?    (6N9)

A rule can be created by using the SWRL Factory to create an instance of the SWRLImp class. The constructor for this class is passed the rule body and head that are represented by instances of the SWRLAtomList classes, which are also created using the SWRL Factory:    (6LI)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLAtomList body = factory.createSWRLAtomList()
SWRLAtomList head = factory.createSWRLAtomList();

SWRLImp imp = factory.createImp(body, head);
    (8HB)

Atoms can then be added to the head and body lists:    (6LN)

body.append(anAtom); // Where anAtom is an instance of a concrete subclass of SWRLAtom.
head.append(anotherAtom);    (8H7)

A swrl:Imp instance can also be created from a string containing a SWRL rule:    (9FA)

SWRLImp imp = factory.createImp("Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)");    (9FB)

A SWRLParseException will be thrown if the rule text is not valid.    (9FC)

A variant of this method also accepts a rule name:    (9FD)

SWRLImp imp = factory.createImp("IsAdult-Rule", "Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)");    (9FJ)

It will return null if the rule name is invalid.    (9FF)

All the createImp methods insert the created rule into the underlying OWL model.    (9FH)

The swrl:Imp class is an OWL class and individual rules are insances of this class - so each rule is effectively named . The SWRL Editor, for example, uses this name when displaying rules. However, no special meaning should be attached to the name of a SWRL rule.    (9FI)

    (6NA)

How do I get all SWRL rules using the SWRL Factory?    (6NB)


A list of all SWRL rules can be retrieved using the getImps method in the factory, which returns a collection of SWRLImp objects: 

OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

Collection rules = factory.getImps();    (8H8)

Can I delete all SWRL rules in an ontology using the SWRL Factory?    (8GT)

Yes. The deleteImps method will delete all SWRL rules in an OWL ontology.    (8GU)

OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms. 

SWRLFactory factory = new SWRLFactory(owlModel);

factory.deleteImps();    (8H9)

Can I copy SWRL rules from one ontology to another using the SWRL Factory?    (8GX)

Yes. The copyImps and replaceImps methods copy SWRL rules from one ontology to another using copy and replacement semantics, respectively.    (8GY)


OWLModel owlModelA = ... // Ontology containing one set of SWRL rules
OWLModel owlModelB = ... // Ontology containing another set of SWRL rules

SWRLFactory factoryA = new SWRLFactory(owlModelA);

factoryA.replaceImps(owlModelB); // Replace all rules on ontology A with rules in ontology B
factoryA.copyImps(owlModelB); // Add rules from ontology B to rules in ontology A. 
    (8HC)

The target ontology must import all the OWL entities referred to in the rules and must also use the same prefix for those entities.    (98F)

How do I create rule variables using the SWRL Factory?    (6NC)

A rule variable can be created by using the SWRL Factory to create instances of the SWRLVariable class. The name of the variable is passed as a parameter to the create method.    (6LT)

For example, Java instances representing the rule variables x,y, and z can be created as follows:    (6LU)


SWLRFactory owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
SWRLVariable y = factory.createVariable("y");
SWRLVariable z = factory.createVariable("z");
    (8HD)

The factory also provides the method getVariable to retrieve a previously created variable by name, and the method getVariables that returns a collection of all varibles.    (6LX)

It should be noted that that only one individual is created for a given variable name within an OWL knowledge base - even if that variable name is used in multiple rules.    (6ND)

How do I create built-ins using the SWRL Factory?    (6NE)

Built-ins can be created by using the SWRL Factory to create instances of the SWRLBuiltin class. The name of the built-in is passed as a parameter to the create method.    (6LY)

For example, Java instances representing the built-ins swrlb:greaterThan and swrlb:lessThan can be created as follows:    (6LZ)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWLRFactory factory = new SWRLFactory(owlModel);

SWRLBuiltin lessThanBuiltin = factory.createBuiltin("swrlb:lessThan");
SWRLBuiltin greaterThanBuiltin = factory.createBuiltin("swrlb:greaterThan");
    (8HE)

All built-ins used in rules must be individuals of the swrl:Builtin OWL class.    (6NJ)

How do I create class atoms using the SWRL Factory?    (6NK)

Class atoms can be created by using the SWRL Factory to create instances of the SWRLClassAtom class. An instance of a variable is passed as a parameter to the create method.    (6M2)

For example, a Java instance representing the class atom Pizza(?x) can be created as follows:    (6M3)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
OWLNamedClass pizzaClass = owlModel.createOWLNamedClass("Pizza");

SWRLClassAtom classAtom = factory.createClassAtom(pizzaClass, x);
    (8HI)

How do I create individual property atoms using the SWRL Factory?    (6NM)

Individual property atoms can be created by using the SWRL Factory to create instances of the SWRLIndividualPropertyAtom class. Instances of two variables are passed as a parameter to the create method.    (6M7)

For example, a Java instance representing the individual property atom hasIngredient(?x, cheese) can be created as follows:    (6M8)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
OWLIndividual cheese = owlModel.getOWLIndividual("cheese");
OWLObjectProperty hasIngredient = owlModel.createOWLObjectProperty("hasIngredient");

SWRLIndividualPropertyAtom individualPropertyAtom = factory.createIndividualPropertyAtom(hasIngredient, x, cheese);
    (8HG)

How do I create datavalued property atoms using the SWRL Factory?    (6NO)

Datavalued property atoms can be created by using the SWRL Factory to create instances of the SWRLDatavaluedPropertyAtom class. Instances of two variables are passed as a parameter to the create method.    (6MC)

For example, a Java instance representing the datavalued property atom hasName(?x, "Joe") can be created as follows:    (6MD)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
RDFSLiteral joe = owlModel.createRDFSLiteral("joe");
OWLDatatypeProperty hasName = owlModel.createOWLDatatypeProperty("hasName");
OWLDatatypeProperty datavaluedPropertyAtom = factory.createDatavaluedPropertyAtom(hasName, x, joe);
    (8HH)

How do I create built-in atoms using the SWRL Factory?    (6NQ)

Built-in atoms can be created by using the SWRL Factory to create instances of the SWRLBuiltinAtom class. An instance of SWRLBuiltin for the appropriate built-in function must first be created (described above); then a list of arguments for the builtin function must be assembled.    (6MH)

For example, a Java instance representing the built-in atom swrlb:lessThan(?x, ?y) can be created as follows:    (6MI)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLBuiltin lessThanBuiltin = factory.createBuiltin("lessThan");
SWRLVariable x = factory.createVariable("x");
SWRLVariable y = factory.createVatiable("y");
List arguments = new ArrayList();
SWRLBuilinAtom builtinAtom = factory.createBuiltinAtom(lessThanBuiltin, arguments); 
arguments.add(x);
arguments.add(y);

    (98M)

The create function does not perform any checking on the built-in atom arguments.    (6NZ)

The factory also provides the method getBuiltin to get a previously created built-in by name and the method getBuiltins that returns a collection of all built-ins.    (6O0)

How do I create same individual atoms using the SWRL Factory?    (6NT)

Same individual atoms can be created by using the SWRL Factory to create instances of the SWRLSameIndividualAtom class. Instances of two variables are passed as a parameter to the create method.    (6O1)

For example, a Java instance representing the same individual atom sameAs(?x, ?y) can be created as follows:    (6O2)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
SWRLVariable y = factory.createVatiable("y");

SWRLSameIndividualAtom sameIndividualAtom = factory.createSameIndividualAtom(x, y);
    (98N)

How do I create different individuals atoms using the SWRL Factory?    (6O4)

Different individuals atoms can be created by using the SWRL Factory to create instances of the SWRLDifferentIndividualsAtom class. Instances of two variables are passed as a parameter to the create method.    (6O5)

For example, a Java instance representing the different individuals atom differentFrom(?x, ?y) can be created as follows:    (6O6)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");
SWRLVariable y = factory.createVatiable("y");

SWRLDifferentIndividualsAtom differentIndividualsAtom = factory.create<nowiki>DifferentIndividualsAtom(x, y); 
</nowiki>    (98O)

How do I create data range atoms using the SWRL Factory?    (6O8)

Data range atoms can be created by using the SWRL Factory to create instances of the SWRLDataRangeAtom class and have two forms: (1) a datatype name followed by a variable in parenthesis, and (2) a list of brackets-enclosed comma-separated literals followed by a variable name in parenthesis.    (6O9)

The first form can be created by passing the URI of an XML Schema datatype to the createRDFExternalResource method in the OWLModel and then passing this external resource to the data range create method in the SWRL factory.    (6OA)

For example, a Java instance representing the data range atom xsd:double(?x) can be created as follows:    (6OB)


OWLModel owlModel = ... // Create using normal Protege-OWL mechanisms.

SWRLFactory factory = new SWRLFactory(owlModel);

SWRLVariable x = factory.createVariable("x");

RDFExternalResource rdfExternalResource = owlModel.createRDFExternalResource(XSDDatatype.XSDdouble.getURI());

SWRLDataRangeAtom dataRangeAtom = factory.createDataRangeAtom(rdfExternalResource, x);
    (98P)

The second form of a data range atom is generated by creating an instance of OWL's data range class and using a one-of slot to store the different literal values for the data range.    (6OD)

The second form of data range atoms is not currently implemented; it will be implemented shortly.    (6OE)