jcreek.representation
Class LocalKnowledgeModel

java.lang.Object
  extended byjcreek.representation.AbstractKnowledgeModel
      extended byjcreek.representation.LocalKnowledgeModel
All Implemented Interfaces:
KnowledgeModel, java.io.Serializable

public class LocalKnowledgeModel
extends AbstractKnowledgeModel
implements java.io.Serializable

The KnowledgeModel is the main interface to the model of entities and relations. As the entities can be compared to a node and a relation to an edge, the KnowledgeModel can be thought of as the graph containing these nodes and edges. The LocalKnowledgeModel class implements a KnowledgeModel where all the data is read from a binary or XML file and stored permanently in memory until explicitly saved by using the various save() commands. It can be accessed concurrently by multiple threads (it is thread safe), but this is done by only allowing one thread to do any access to the model at a time.

Version:
1.0
Author:
Frode Sørmo
See Also:
Serialized Form

Field Summary
protected  java.lang.String author
          The author of the KnowledgeModel
protected  java.lang.String description
          A description of this current Knowledge Model.
protected  boolean dirty
          Marks if this model has been changed since last save.
protected  java.io.File file
          File to read and write to.
protected  LRUCache inferenceCache
           
protected  java.lang.String name
          The name of the Knowledge Model.
protected  java.lang.String title
          The title of the knowledgemodel.
 
Fields inherited from class jcreek.representation.AbstractKnowledgeModel
modelChangeListeners
 
Constructor Summary
LocalKnowledgeModel()
          Create a new, empty knowledge model.
LocalKnowledgeModel(EpistemologicalModel epistemologicalModel)
          Creates a new knowledge model based on the epistemological model supplied.
LocalKnowledgeModel(java.io.File file)
          Creates a new KnowledgeModel instance by loading it from the file supplied.
LocalKnowledgeModel(java.io.InputStream input)
          Creates a new KnowledgeModel instance by loading it from the supplied stream.
LocalKnowledgeModel(KnowledgeModel model)
          Creates a new KnowledgeModel by importing the contents of the supplied model.
LocalKnowledgeModel(java.lang.String filename)
          Creates a new KnowledgeModel instance by loading it from the filename supplied.
 
Method Summary
 void addContext(Context c)
          Registers a context with the KnowledgeModel.
 int entitySize()
          Returns the number of entities in the model.
 void fireModelChangeEvent(ModelChangeEvent event)
          Notifies the listeners that a change has occured in the model.
 java.lang.String getAuthor()
          Returns the author of the model.
 Constraint getConstraint(java.lang.Object id)
          Find a relation in the model with the given ID.
 java.util.Iterator getConstraintIterator()
          Returns an iterator of all the constraints in the model.
 Context[] getContexts()
          Returns an array of all registered contexts.
 Context[] getContexts(java.lang.Class c)
          Returns all context object registrered with the KnowledgeModel that is of the Class given as a parameter to the method.
 java.lang.String getDescription()
          Returns the description of the model.
 Entity getEntity(java.lang.Object id)
          Find an entity in the model with the given name or ID.
 java.util.Iterator getEntityIterator()
          Returns an iterator of all the entities in the model.
 java.io.File getFile()
          Returns the file this KnowledgeModel is associated with (if any).
 LRUCache getInferenceCache()
          Returns this models inference cache.
 java.lang.String getName()
          Returns the name of the model.
 Partition getPartition(java.lang.Object id)
          Returns the partition with the given name or ID.
 java.util.Iterator getPartitionIterator()
          Returns an iterator for all the partitions in this KnowledgeModel.
 Relation getRelation(java.lang.Object id)
          Find a relation in the model with the given ID.
 java.util.Iterator getRelationIterator()
          Returns an iterator of all the realtions in the model.
 RelationType getRelationType(java.lang.String name)
          Find a relation class in the model of the given name.
 java.lang.String getTitle()
          Returns the title of the model.
 boolean hasContext(Context c)
          Returns true if the give context is registred with the KnowledgeModel.
 boolean hasEntity(Entity entity)
          Returns true if this model contains the supplied entity.
 boolean hasEntity(java.lang.Object id)
          Returns true if an entity with the given name or id exists in the KnowledgeModel.
 boolean hasFile()
          Returns true if this KnowledgeModel has a file associated with it.
 boolean hasPartition(java.lang.Object id)
          Returns true if the KnowledgeModel contains a Partition with the given ID or name.
 boolean hasRelation(Relation relation)
          Returns true if this model contains the supplied relation.
 boolean isDirty()
          Returns true if this knowledgemodel has been changed since last save.
 void load()
          Loads the model from the file associated with the model.
 void load(java.io.File file)
          Loads a model from disk.
 void load(java.io.InputStream in)
          Loads a model from a stream.
 void load(java.lang.String name)
          Load a model from disk.
static void main(java.lang.String[] argv)
           
 ConstraintData makeConstraint()
          Creates a new data object for storing data for Constraint.
 EntityData makeEntity()
          Creates a new data object for storing data for an Entity.
 PartitionData makePartition()
          Creates a new data object for storing data for a Partition.
 RelationData makeRelation(Entity from, Entity type, Entity value)
          Creates a new data object for storing data for a Relation.
 void registerEntity(java.lang.String id, Entity ent)
          Indexes a given entity with the given id.
 void registerPartition(java.lang.String name, Partition partition)
          Registers a partition under the given name in this models partition index.
 void reindex()
          Recreates the relation type index.
 int relationSize()
          Returns the number of relations in the model.
 void removeContext(Context c)
          Removes the given context from this KnowledgeModel.
 void save()
          Saves this document (the data) to the associated disk file.
 void saveAs(java.io.File file)
          Saves this knowledgemodel (the data) to a new file, and hereinafter is associated with this new file.
 void saveAs(java.lang.String name)
          Saves the model to disk.
 void setAuthor(java.lang.String authorName)
          Sets the author of the model.
 void setDescription(java.lang.String description)
          Sets description of model.
 void setFile(java.io.File file)
          Sets the file this KnowledgeModel is associated with.
 void setName(java.lang.String name)
          Sets name of model.
 void setTitle(java.lang.String title)
          Sets title of model.
 void unregisterEntity(java.lang.String name)
          Removes the index with the given name from the entity index.
 void unregisterPartition(java.lang.String name)
          Unregisters an index entry from the parition index.
 
Methods inherited from class jcreek.representation.AbstractKnowledgeModel
addModelChangeListener, getConstraints, getEntities, getPartitions, getRelations, importModel, matchEntityType, mergeModel, removeModelChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inferenceCache

protected transient LRUCache inferenceCache

name

protected java.lang.String name
The name of the Knowledge Model. Used for filenames and such.


description

protected java.lang.String description
A description of this current Knowledge Model.


title

protected java.lang.String title
The title of the knowledgemodel. More full-length than name.


file

protected transient java.io.File file
File to read and write to.


dirty

protected transient boolean dirty
Marks if this model has been changed since last save.


author

protected java.lang.String author
The author of the KnowledgeModel

Constructor Detail

LocalKnowledgeModel

public LocalKnowledgeModel(EpistemologicalModel epistemologicalModel)
Creates a new knowledge model based on the epistemological model supplied.


LocalKnowledgeModel

public LocalKnowledgeModel()
Create a new, empty knowledge model. This constructor uses the jcreek.representation.cbr.CBRModel as its epistemological model.


LocalKnowledgeModel

public LocalKnowledgeModel(java.lang.String filename)
                    throws java.io.IOException,
                           java.lang.ClassNotFoundException
Creates a new KnowledgeModel instance by loading it from the filename supplied.


LocalKnowledgeModel

public LocalKnowledgeModel(java.io.File file)
                    throws java.io.IOException,
                           java.lang.ClassNotFoundException
Creates a new KnowledgeModel instance by loading it from the file supplied.


LocalKnowledgeModel

public LocalKnowledgeModel(java.io.InputStream input)
                    throws java.io.IOException,
                           java.lang.ClassNotFoundException
Creates a new KnowledgeModel instance by loading it from the supplied stream.


LocalKnowledgeModel

public LocalKnowledgeModel(KnowledgeModel model)
Creates a new KnowledgeModel by importing the contents of the supplied model.

Parameters:
model - The KnowledgeModel to import.
Method Detail

setName

public void setName(java.lang.String name)
Sets name of model.

Specified by:
setName in interface KnowledgeModel
Parameters:
name - The name of the model.

getName

public java.lang.String getName()
Returns the name of the model.

Specified by:
getName in interface KnowledgeModel

setDescription

public void setDescription(java.lang.String description)
Sets description of model.

Specified by:
setDescription in interface KnowledgeModel
Parameters:
description - The description of the model.

getDescription

public java.lang.String getDescription()
Returns the description of the model.

Specified by:
getDescription in interface KnowledgeModel

setTitle

public void setTitle(java.lang.String title)
Sets title of model.

Specified by:
setTitle in interface KnowledgeModel
Parameters:
title - The title of the model.

main

public static void main(java.lang.String[] argv)

getTitle

public java.lang.String getTitle()
Returns the title of the model. If none is set and there is a file associated with the model, the filename is returned.

Specified by:
getTitle in interface KnowledgeModel

getAuthor

public java.lang.String getAuthor()
Returns the author of the model.

Specified by:
getAuthor in interface KnowledgeModel

setAuthor

public void setAuthor(java.lang.String authorName)
Sets the author of the model.

Specified by:
setAuthor in interface KnowledgeModel
Parameters:
authorName - The name of the author.

setFile

public void setFile(java.io.File file)
Sets the file this KnowledgeModel is associated with.

Parameters:
file - file object

getFile

public java.io.File getFile()
Returns the file this KnowledgeModel is associated with (if any).


hasFile

public boolean hasFile()
Returns true if this KnowledgeModel has a file associated with it.


getEntity

public Entity getEntity(java.lang.Object id)
Find an entity in the model with the given name or ID.

Specified by:
getEntity in interface KnowledgeModel
Parameters:
id - The name (String) or identifier (Object) for an entity
Returns:
The entity-object, or null if no entity with that name or ID is found.

getRelation

public Relation getRelation(java.lang.Object id)
Find a relation in the model with the given ID.

Specified by:
getRelation in interface KnowledgeModel
Parameters:
id - The unique identifier for this relation.

getRelationType

public RelationType getRelationType(java.lang.String name)
Find a relation class in the model of the given name. If no relation type is indexed under that name, null is returned.

Specified by:
getRelationType in interface KnowledgeModel
Overrides:
getRelationType in class AbstractKnowledgeModel
Parameters:
name - The name of the relation class to be found
Returns:
The RelationType object for this type

registerEntity

public void registerEntity(java.lang.String id,
                           Entity ent)
                    throws NameAlreadyExistException
Indexes a given entity with the given id. This is automatically done when generating an entity.

Specified by:
registerEntity in interface KnowledgeModel
Parameters:
id - The name of the
ent - The Concept object.
Throws:
NameAlreadyExistException

unregisterEntity

public void unregisterEntity(java.lang.String name)
Removes the index with the given name from the entity index.

Specified by:
unregisterEntity in interface KnowledgeModel
Parameters:
name - The name (key) of the index entry to remove.

hasEntity

public boolean hasEntity(java.lang.Object id)
Returns true if an entity with the given name or id exists in the KnowledgeModel.

Specified by:
hasEntity in interface KnowledgeModel
Parameters:
id - The key of the concept.

hasEntity

public boolean hasEntity(Entity entity)
Returns true if this model contains the supplied entity.

Parameters:
entity - The entity to check if exists in the model.

hasRelation

public boolean hasRelation(Relation relation)
Returns true if this model contains the supplied relation.

Parameters:
relation - The relation to check if exists in the model.

entitySize

public int entitySize()
Returns the number of entities in the model.

Specified by:
entitySize in interface KnowledgeModel

relationSize

public int relationSize()
Returns the number of relations in the model.

Specified by:
relationSize in interface KnowledgeModel

getEntityIterator

public java.util.Iterator getEntityIterator()
Returns an iterator of all the entities in the model. The snapshot of the entities in the model is created from

Specified by:
getEntityIterator in interface KnowledgeModel

getRelationIterator

public java.util.Iterator getRelationIterator()
Returns an iterator of all the realtions in the model.

Specified by:
getRelationIterator in interface KnowledgeModel

makeEntity

public EntityData makeEntity()
Creates a new data object for storing data for an Entity. This is a part of the creation of a new Entity, and should only be used from an Entity constructor.

Specified by:
makeEntity in interface KnowledgeModel

makeRelation

public RelationData makeRelation(Entity from,
                                 Entity type,
                                 Entity value)
Creates a new data object for storing data for a Relation. This is a part of the creation of a new Relation, and should only be used from a Relation constructor. The Relation is required to have from, value and type entities.

Specified by:
makeRelation in interface KnowledgeModel
Parameters:
from - The Entity this relation is from (the origin).
value - The Entity this relation is to (the target).
type - The Entity that represents the type of the relation.

makePartition

public PartitionData makePartition()
                            throws NameAlreadyExistException
Creates a new data object for storing data for a Partition. This is part of the creation of a new Partition and should only be used from the Partition constructor.

Specified by:
makePartition in interface KnowledgeModel
Throws:
NameAlreadyExistException

registerPartition

public void registerPartition(java.lang.String name,
                              Partition partition)
                       throws NameAlreadyExistException
Registers a partition under the given name in this models partition index. The partition can then be accessed through the getPartition(Object id) method using this string. If there is already a partition registered with this name, a NameAlreadyExistException will be thrown.

Specified by:
registerPartition in interface KnowledgeModel
Parameters:
name - The name (key) to register the partition under.
partition - The Partition to register under this name.
Throws:
NameAlreadyExistException

unregisterPartition

public void unregisterPartition(java.lang.String name)
Unregisters an index entry from the parition index.

Specified by:
unregisterPartition in interface KnowledgeModel
Parameters:
name - The name of the index entry to remove.

hasPartition

public boolean hasPartition(java.lang.Object id)
Returns true if the KnowledgeModel contains a Partition with the given ID or name.

Specified by:
hasPartition in interface KnowledgeModel
Parameters:
id - The name (string) or id of a partition to test for.

getPartitionIterator

public java.util.Iterator getPartitionIterator()
Returns an iterator for all the partitions in this KnowledgeModel.

Specified by:
getPartitionIterator in interface KnowledgeModel

getPartition

public Partition getPartition(java.lang.Object id)
Returns the partition with the given name or ID. If no parition with this name is found, null is returned.

Specified by:
getPartition in interface KnowledgeModel
Parameters:
id - The name or ID of the partition to return.

makeConstraint

public ConstraintData makeConstraint()
Creates a new data object for storing data for Constraint. This is a part of the creation of a new Constraint, and should only be used from an Constraint constructor.

Specified by:
makeConstraint in interface KnowledgeModel

getConstraint

public Constraint getConstraint(java.lang.Object id)
Find a relation in the model with the given ID.

Specified by:
getConstraint in interface KnowledgeModel
Parameters:
id - The unique identifier for this relation.

getConstraintIterator

public java.util.Iterator getConstraintIterator()
Returns an iterator of all the constraints in the model.

Specified by:
getConstraintIterator in interface KnowledgeModel

save

public void save()
          throws java.io.IOException
Saves this document (the data) to the associated disk file.

Throws:
java.io.IOException

saveAs

public void saveAs(java.io.File file)
            throws java.io.IOException
Saves this knowledgemodel (the data) to a new file, and hereinafter is associated with this new file.

Parameters:
file - new file to save to
Throws:
java.io.IOException

saveAs

public void saveAs(java.lang.String name)
            throws java.io.IOException
Saves the model to disk.

Parameters:
name - The filename of the model.
Throws:
java.io.IOException

load

public void load(java.lang.String name)
          throws java.lang.ClassNotFoundException,
                 java.io.IOException
Load a model from disk.

Parameters:
name - The filename of the model (without extension)
Throws:
java.lang.ClassNotFoundException
java.io.IOException

load

public void load(java.io.File file)
          throws java.lang.ClassNotFoundException,
                 java.io.IOException
Loads a model from disk. This methods sets the file associated with this model as in setFile() and calls load().

Parameters:
file - The file representing the model.
Throws:
java.lang.ClassNotFoundException
java.io.IOException

load

public void load(java.io.InputStream in)
          throws java.lang.ClassNotFoundException,
                 java.io.IOException
Loads a model from a stream.

Parameters:
in - Loads from the stream in.
Throws:
java.lang.ClassNotFoundException
java.io.IOException

reindex

public void reindex()
Recreates the relation type index. This is necessary because of a bug in 0.65 branch.


load

public void load()
          throws java.lang.ClassNotFoundException,
                 java.io.IOException
Loads the model from the file associated with the model.

Throws:
java.lang.ClassNotFoundException
java.io.IOException

addContext

public void addContext(Context c)
Registers a context with the KnowledgeModel.

Specified by:
addContext in interface KnowledgeModel

getContexts

public Context[] getContexts()
Returns an array of all registered contexts.

Specified by:
getContexts in interface KnowledgeModel

getContexts

public Context[] getContexts(java.lang.Class c)
Returns all context object registrered with the KnowledgeModel that is of the Class given as a parameter to the method. Objects that are subclasses of the given class is also returned.

Specified by:
getContexts in interface KnowledgeModel

removeContext

public void removeContext(Context c)
Removes the given context from this KnowledgeModel.

Specified by:
removeContext in interface KnowledgeModel

hasContext

public boolean hasContext(Context c)
Returns true if the give context is registred with the KnowledgeModel.

Specified by:
hasContext in interface KnowledgeModel

isDirty

public boolean isDirty()
Returns true if this knowledgemodel has been changed since last save.


getInferenceCache

public LRUCache getInferenceCache()
Returns this models inference cache. This cache is flushed each time the model is changed, but can be used to cache inferences done with the basis of the current model.

Specified by:
getInferenceCache in interface KnowledgeModel

fireModelChangeEvent

public void fireModelChangeEvent(ModelChangeEvent event)
Notifies the listeners that a change has occured in the model.

Specified by:
fireModelChangeEvent in interface KnowledgeModel
Parameters:
event - The event object reporting this change.


Created at IDI, NTNU by the Artificial Intelligence and Learning group