Commit 34e3ceb2 authored by Jay Jay Billings's avatar Jay Jay Billings
Browse files

Finished the first complete implementation of ComponentBuilder and test.


Signed-off-by: default avatarJay Jay Billings <billingsjj@ornl.gov>
parent c2bb2224
......@@ -26,9 +26,16 @@ import org.apache.jena.rdf.model.Resource;
* configure data members before the build function (build()) is called.
*
* The builder will configure default values for the field variables of the
* components that it constructs. These are reasonable defaults such as
* "NO_NAME" and "NO_DESCRIPTION" for classes that have neither a name nor a
* description, for example.
* components that it constructs. The defaults for this class are:<br>
* name = "NO_NAME"<br>
* description = "NO_DESCRIPTION"<br>
* context = "DEFAULT"<br>
* id = 0<br>
*
* The builder will clear its state between separate calls to the build
* function. That is, successively calling build() will not return two resources
* with the same values. The builder will do it automatically, although clients
* may elect to do it themselves by calling clear().
*
* @author Jay Jay Billings
*
......@@ -43,22 +50,22 @@ public class ComponentBuilder {
/**
* The name of the component that should be built.
*/
private String name = "NO_NAME";
private String name;
/**
* The identifier of the component that should be built.
*/
private long id = 0;
private long id;
/**
* The unique context in which the Component exists
*/
private String context = "DEFAULT";
private String context;
/**
* A description of the component.
*/
private String description = "NO_DESCRIPTION";
private String description;
/**
* And OWL class, managed by the RDF framework, for the root component class in
......@@ -99,6 +106,21 @@ public class ComponentBuilder {
descProp = ontModel.getObjectProperty(ICEConstants.DESCRIPTION);
contextProp = ontModel.getObjectProperty(ICEConstants.CONTEXT);
idProp = ontModel.getObjectProperty(ICEConstants.ID);
// Set the default values.
clear();
}
/**
* This operation clears the configured state from the previous build and
* returns the builder to default values. In general, clients should not have to
* call this but may if they like.
*/
public void clear() {
name = "NO_NAME";
context = "DEFAULT";
description = "NO_DESCRIPTION";
id = 0;
}
/**
......@@ -153,7 +175,8 @@ public class ComponentBuilder {
/**
* This operation executes the construction process for the Component and builds
* the ontological entity.
* the ontological entity. This operation always builds the Component with some
* property values attached, whether the be user-specified or default values.
*
* @param the data model to which the newly created resource should be attached.
* @param the IRI of the new component
......@@ -161,12 +184,13 @@ public class ComponentBuilder {
*/
public Resource build(Model dataModel, final String iri) {
Resource resource = dataModel.createResource(iri, compClass)
.addProperty(nameProp, name)
.addProperty(idProp, String.valueOf(id))
.addProperty(contextProp, context)
Resource resource = dataModel.createResource(iri, compClass).addProperty(nameProp, name)
.addProperty(idProp, String.valueOf(id)).addProperty(contextProp, context)
.addProperty(descProp, description);
// Clear the state
clear();
return resource;
}
......
......@@ -11,6 +11,10 @@
*****************************************************************************/
package org.eclipse.ice.data;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
/**
* This class stores ICE Data Model constants used throughout the code base.
*
......@@ -19,6 +23,11 @@ package org.eclipse.ice.data;
*/
public class ICEConstants {
/**
* A private model used for creating property and resource references.
*/
private static final Model refModel = ModelFactory.createDefaultModel();
/**
* Root Namespace of the ICE ontology
*/
......@@ -34,19 +43,39 @@ public class ICEConstants {
*/
public static String NAME = NAMESPACE + "name";
/**
* A property reference for the ICE Identifiable name property
*/
public static final Property NAME_PROPERTY = refModel.createProperty(NAME);
/**
* Fully qualified type name of ICE Identifiable descriptions
*/
public static String DESCRIPTION = NAMESPACE + "desc";
/**
* A property reference for the ICE Identifiable description property
*/
public static final Property DESC_PROPERTY = refModel.createProperty(DESCRIPTION);
/**
* Fully qualified type name of ICE Identifiable context
*/
public static String CONTEXT = NAMESPACE + "context";
/**
* A property reference for the ICE Identifiable context property
*/
public static final Property CONTEXT_PROPERTY = refModel.createProperty(CONTEXT);
/**
* Fully qualified type name of ICE Identifiable identifier numbers
*/
public static String ID = NAMESPACE + "identifier";
/**
* A property reference for the ICE Identifiable identifier numbers
*/
public static final Property ID_PROPERTY = refModel.createProperty(ID);
}
......@@ -18,9 +18,9 @@ import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.util.FileManager;
import org.eclipse.ice.data.ComponentBuilder;
import org.eclipse.ice.data.ICEConstants;
import org.junit.BeforeClass;
import org.junit.Test;
......@@ -60,26 +60,136 @@ public class ComponentBuilderTest {
dataModel = ModelFactory.createDefaultModel();
}
/**
* This operation insures that the build() operation works in its default
* configuration.
*/
@Test
public void testDefaultBuild() {
String name = "NO_NAME", context = "DEFAULT", desc = "NO_DESCRIPTION";
long id = 0;
// Create the IRI
String iri = namespace + "Comp1";
// Build the component
Resource comp = builder.build(dataModel, iri);
// Make sure the component exists
assertNotNull(comp);
// Check the deets, namespace first
assertEquals(namespace, comp.getNameSpace());
// name
assertEquals(name, comp.getProperty(ICEConstants.NAME_PROPERTY).getObject().toString());
// context
assertEquals(context,
comp.getProperty(ICEConstants.CONTEXT_PROPERTY).getObject().toString());
// description
assertEquals(desc, comp.getProperty(ICEConstants.DESC_PROPERTY).getObject().toString());
// id
assertEquals(id,
comp.getProperty(ICEConstants.ID_PROPERTY).getObject().asLiteral().getLong());
// Have a look at it
dataModel.write(System.out, "TURTLE");
// Clear the data model before using it again to avoid test errors
dataModel.removeAll();
return;
}
/**
* This operation insures that the build() operation works.
*/
@Test
public void testBuild() {
String name = "Tk-421", context = "bay-gaurd",
desc = "Presently guarding a YT-1300 freighter suspected of being the same one that "
+ "blasted out of the Mos Eisley spaceport.";
long id = 421;
// Create the IRI
String iri = namespace + "Comp1";
Resource comp = builder.build(dataModel,iri);
String iri = namespace + "Trooper421";
// Build the component
Resource comp = builder.withName(name).withContext(context).withDescription(desc).withId(id)
.build(dataModel, iri);
// Make sure the component exists
assertNotNull(comp);
// Check the namespace
// Check the deets, namespace first
assertEquals(namespace, comp.getNameSpace());
// name
assertEquals(name, comp.getProperty(ICEConstants.NAME_PROPERTY).getObject().toString());
// context
assertEquals(context,
comp.getProperty(ICEConstants.CONTEXT_PROPERTY).getObject().toString());
// description
assertEquals(desc, comp.getProperty(ICEConstants.DESC_PROPERTY).getObject().toString());
// id
assertEquals(id,
comp.getProperty(ICEConstants.ID_PROPERTY).getObject().asLiteral().getLong());
// Have a look at it
dataModel.write(System.out, "TURTLE");
// Clear the data model before using it again to avoid test errors
dataModel.removeAll();
return;
}
/**
* This operation insures that the build() operation works.
*/
@Test
public void testClearBuild() {
String name = "Tk-422", context = "bay-gaurd",
desc = "Presently guarding a YT-1300 freighter suspected of being the same one that "
+ "blasted out of the Mos Eisley spaceport.";
String defaultName = "NO_NAME", defaultContext = "DEFAULT", defaultDesc = "NO_DESCRIPTION";
long defaultId = 0, id = 422;
// Create the IRI
String iri = namespace + "Trooper422";
// I don't know if namespaces make sense as
// Build the component
Resource comp = builder.withName(name).withContext(context).withDescription(desc).withId(id)
.build(dataModel, iri);
// Make sure the component exists
assertNotNull(comp);
// Check the deets, namespace first
assertEquals(namespace, comp.getNameSpace());
// name
assertEquals(name, comp.getProperty(ICEConstants.NAME_PROPERTY).getObject().toString());
// context
assertEquals(context,
comp.getProperty(ICEConstants.CONTEXT_PROPERTY).getObject().toString());
// description
assertEquals(desc, comp.getProperty(ICEConstants.DESC_PROPERTY).getObject().toString());
// id
assertEquals(id,
comp.getProperty(ICEConstants.ID_PROPERTY).getObject().asLiteral().getLong());
// Print the properties to examine them
System.out.println("-----");
for (StmtIterator foo = comp.listProperties(); foo.hasNext();) {
System.out.println(foo.next().toString());
}
// Build the component
iri = namespace + "Comp2";
Resource resetComp = builder.build(dataModel, iri);
// Make sure the component exists
assertNotNull(resetComp);
// Check the deets, namespace first
assertEquals(namespace, resetComp.getNameSpace());
// name
assertEquals(defaultName, resetComp.getProperty(ICEConstants.NAME_PROPERTY).getObject().toString());
// context
assertEquals(defaultContext,
resetComp.getProperty(ICEConstants.CONTEXT_PROPERTY).getObject().toString());
// description
assertEquals(defaultDesc, resetComp.getProperty(ICEConstants.DESC_PROPERTY).getObject().toString());
// id
assertEquals(defaultId,
resetComp.getProperty(ICEConstants.ID_PROPERTY).getObject().asLiteral().getLong());
// Have a look at it
dataModel.write(System.out, "TURTLE");
// Clear the data model before using it again to avoid test errors
dataModel.removeAll();
return;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment