edu.umbc.cs.daml4jess
Class Daml4Jess

java.lang.Object
  |
  +--edu.umbc.cs.daml4jess.Daml4Jess
All Implemented Interfaces:
D4jLogger

public class Daml4Jess
extends java.lang.Object
implements D4jLogger

Coming soon - out-of-dated stuff deleted.


Field Summary
static java.lang.String ANONYMOUS_PREDICATE
          The relation used to denote anonymous objects.
protected  org.w3c.dom.Element configRoot_
          The root element of the xml-based configuration document.
protected  java.lang.String currentDependencyId_
          An id that the asserted facts depend on.
protected  int d4jLogLevel_
          Controls the level of daml4jess (not Jess) log output.
protected  java.io.BufferedWriter d4jLogWriter_
          Daml4Jess log writer.
static java.lang.String DEPENDENCY_PREDICATE
          Predicate name used to build dependency facts on which other facts depend (in the sense of the logical CE in Jess).
static java.lang.String DEPENDENCY_PREFIX
          A prefix string used to construct dependency (with logical CEs) rules.
protected  boolean insertAnonymous_
          Indicator of whether to insert anonymous predicates to Jess (from the RDF data).
static java.lang.String RDF_OBJ_SLOT
          Jess template slot name for the "object" in an RDF statement.
static java.lang.String RDF_PRED_SLOT
          Jess template slot name for the "predicate" in an RDF statement.
static java.lang.String RDF_SUBJ_SLOT
          Jess template slot name for the "subject" in an RDF statement.
static java.lang.String RDF_TEMPLATE
          Jess template name for RDF statements.
protected  D4jRdf2Jess rdf2Facts_
          Used to load RDF files and to break RDF statements into Jess facts or whatever format daml4jess can take.
protected  boolean recursiveImports_
          Indicator of whether to load resources imported by the resource being loaded.
protected  jess.Rete rete_
          The core Jess data structure, responsible for inference.
protected  boolean runJessAfterLoad_
          Indicator of whether to do Rete.run() after loading a DAML resource.
 
Fields inherited from interface edu.umbc.cs.daml4jess.D4jLogger
LOG_LEVEL_BRIEF, LOG_LEVEL_ERR, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE, LOG_LEVEL_WARN, STDERR, STDIN, STDOUT
 
Constructor Summary
Daml4Jess(java.io.InputStream configInputStream)
          Constructor.
 
Method Summary
 void applyDAMLSemantics()
          Applies DAML semantics such as error checking as well as calling Rete.run() to apply basic semantics rules of the language.
 void assertWithDependency(java.lang.String facts, java.lang.String depId)
          Assert the given facts with the given dependency id, such that these facts can be retracted via the dependency id.
protected  java.util.List getImports4Uri(java.lang.String uri)
          Get the list of resoruces (no matter loaded or not) imported by the given resource (@ uri).
 jess.Rete getRete()
          Returns the rete being used by the Daml4Jess object.
protected  java.io.Writer getWriterForName(java.lang.String fileName)
          Get the writer for the given file name.
 void inputJessFile(java.lang.String filename)
          Load Jess code from a file, using Jess batch command.
protected  boolean isResourceLoaded(java.lang.String uri)
          check if the given resource has been loaded.
 void loadDAMLResource(java.io.InputStream istream, java.lang.String xmlBase, boolean useAssert)
          Load daml resource from the given input stream.
protected  void loadDAMLResource(java.io.InputStream istream, java.lang.String xmlBase, boolean useAssert, int nestLevel)
          Load daml resource from the given input stream.
 void loadDAMLResource(java.lang.String uri)
          Load daml resource from the given URI.
 void loadDAMLResource(java.lang.String uri, boolean useAssert)
          Load daml resource from the given URI.
protected  void loadDAMLResource(java.lang.String uri, boolean useAssert, int nestLevel)
          Load daml resource from the given URI.
 void loadJessResourceWithClassLoader(java.lang.ClassLoader cl, java.lang.String res)
          Load Jess program (file) using the given class loader.
 void loadRDFResource(java.io.InputStream istream, java.lang.String xmlBase, boolean useAssert)
          Load the RDF resource from the given input stream.
 void loadRDFResource(java.lang.String uri)
          Load the RDF resource at the given URI.
 void loadRDFResource(java.lang.String uri, boolean useAssert)
          Load the RDF resource at the given URI.
 void log(java.lang.String msg, int level)
          Log the given message.
 void log(java.lang.String msg, java.lang.Throwable t, int level)
          Log the given message and the exception.
static void main(java.lang.String[] args)
          Test program.
 boolean objectIsType(java.lang.String object, java.lang.String type)
          Query if the given object has the given type.
 java.util.Iterator query(java.lang.String queryName, java.lang.String[] args)
          Run the given (Jess) query with the given arguments and return the iterator of the results.
 java.util.List queryRdfSlot(java.lang.String queryName, java.lang.String[] args, java.lang.String slot)
          Run a Jess query and return the list of values for the given RDF slot name of the first facts in each matching pattern.
 void setD4jLogLevel(int level)
          Set daml4jess output level.
 java.io.Writer setD4jLogWriter(java.io.Writer w)
          Set the log output writer.
 void setDependencyId(java.lang.String id)
          Set the dependency id of the resources to be loaded.
 java.io.Reader setJessInputRouter(java.lang.String routerId, java.io.Reader reader)
          Set Jess input router.
 java.io.Writer setJessOutputRouter(java.lang.String routerId, java.io.Writer w)
          Set Jess output router.
protected  void sureLoadDAML(java.io.InputStream istream, java.lang.String xmlBase, boolean useAssert, int nestLevel)
          Load daml resource from the given input stream.
protected  void sureLoadRDF(java.io.InputStream istream, java.lang.String xmlBase, boolean useAssert)
          Load the RDF resource from the given input stream.
 void typeCheck()
          Checks to make sure all resources present in the KB have been given a type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RDF_TEMPLATE

public static final java.lang.String RDF_TEMPLATE
Jess template name for RDF statements. A RDF statement is translated to a Jess template with three slots: subject, predicate, and object.

See Also:
Constant Field Values

RDF_SUBJ_SLOT

public static final java.lang.String RDF_SUBJ_SLOT
Jess template slot name for the "subject" in an RDF statement.

See Also:
Constant Field Values

RDF_PRED_SLOT

public static final java.lang.String RDF_PRED_SLOT
Jess template slot name for the "predicate" in an RDF statement.

See Also:
Constant Field Values

RDF_OBJ_SLOT

public static final java.lang.String RDF_OBJ_SLOT
Jess template slot name for the "object" in an RDF statement.

See Also:
Constant Field Values

DEPENDENCY_PREDICATE

public static final java.lang.String DEPENDENCY_PREDICATE
Predicate name used to build dependency facts on which other facts depend (in the sense of the logical CE in Jess).

See Also:
Constant Field Values

DEPENDENCY_PREFIX

public static final java.lang.String DEPENDENCY_PREFIX
A prefix string used to construct dependency (with logical CEs) rules.

See Also:
Constant Field Values

ANONYMOUS_PREDICATE

public static final java.lang.String ANONYMOUS_PREDICATE
The relation used to denote anonymous objects. This is done so users can, if they wish, reason about anonymous nodes either to support some notion of existentialism or to infer that the ontology writer didn't deem the object to be a first class citizen in the ontology.

See Also:
Constant Field Values

insertAnonymous_

protected boolean insertAnonymous_
Indicator of whether to insert anonymous predicates to Jess (from the RDF data).


runJessAfterLoad_

protected boolean runJessAfterLoad_
Indicator of whether to do Rete.run() after loading a DAML resource.


recursiveImports_

protected boolean recursiveImports_
Indicator of whether to load resources imported by the resource being loaded.


d4jLogLevel_

protected int d4jLogLevel_
Controls the level of daml4jess (not Jess) log output. Valid levels are: LOG_LEVEL_ERR, LOG_LEVEL_WARN, LOG_LEVEL_BRIEF, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE.


configRoot_

protected org.w3c.dom.Element configRoot_
The root element of the xml-based configuration document.


currentDependencyId_

protected java.lang.String currentDependencyId_
An id that the asserted facts depend on. That is, those facts that are asserted with this dependency can be retracted using this id. It's implemented based on Jess's logical CE construct.


rete_

protected jess.Rete rete_
The core Jess data structure, responsible for inference. Rete objects are thread-safe although somewhat bothersome. In particular, it is difficult to rollback a Rete with multiple threads operating on it because it's not clear which facts have been asserted by what thread.


rdf2Facts_

protected D4jRdf2Jess rdf2Facts_
Used to load RDF files and to break RDF statements into Jess facts or whatever format daml4jess can take.


d4jLogWriter_

protected java.io.BufferedWriter d4jLogWriter_
Daml4Jess log writer.

Constructor Detail

Daml4Jess

public Daml4Jess(java.io.InputStream configInputStream)
          throws D4jException
Constructor.

Parameters:
configInputStream - the XML configuration input stream.
Throws:
D4jException - if anything goes wrong.
Method Detail

getRete

public jess.Rete getRete()
Returns the rete being used by the Daml4Jess object. The rete contains all facts and rules currently loaded.

Returns:
the Jess engine (Rete object).

setD4jLogLevel

public void setD4jLogLevel(int level)
Set daml4jess output level. Valid levels are: LOG_LEVEL_ERR, LOG_LEVEL_WARN, LOG_LEVEL_BRIEF, LOG_LEVEL_INFO, LOG_LEVEL_VERBOSE.

Parameters:
level - daml4jess output level.

setD4jLogWriter

public java.io.Writer setD4jLogWriter(java.io.Writer w)
Set the log output writer.

Parameters:
w - the output stream writer.
Returns:
the old log writer, if any.

log

public void log(java.lang.String msg,
                int level)
Log the given message. The message will be logged if and only if the given level is greater than or equal to the current log level.

Specified by:
log in interface D4jLogger
Parameters:
msg - the message to be logged.
level - the log level of this message.

log

public void log(java.lang.String msg,
                java.lang.Throwable t,
                int level)
Log the given message and the exception. The message and exception will be logged if and only if the given level is greater than or equal to the current log level.

Specified by:
log in interface D4jLogger
Parameters:
msg - the message to be logged, can be null.
t - a the throwable to be logged, can be null.
level - the log level of this message.

setJessInputRouter

public java.io.Reader setJessInputRouter(java.lang.String routerId,
                                         java.io.Reader reader)
Set Jess input router.

Parameters:
routerId - the name of the router.
reader - the reader for the input router.
Returns:
the current router (reader), if any; return null if no current router with the given id.

setJessOutputRouter

public java.io.Writer setJessOutputRouter(java.lang.String routerId,
                                          java.io.Writer w)
Set Jess output router.

Parameters:
routerId - the name of the router.
w - the writer for the output router.
Returns:
the current router, if any; return null if no current router with the given id.

query

public java.util.Iterator query(java.lang.String queryName,
                                java.lang.String[] args)
                         throws D4jException
Run the given (Jess) query with the given arguments and return the iterator of the results.

Parameters:
queryName - name of the Jess query to be executed.
args - the argument list for the query.
Throws:
D4jException - if anything goes wrong.

queryRdfSlot

public java.util.List queryRdfSlot(java.lang.String queryName,
                                   java.lang.String[] args,
                                   java.lang.String slot)
                            throws D4jException
Run a Jess query and return the list of values for the given RDF slot name of the first facts in each matching pattern. The results of a Jess run-query is a set of tokens. Each token is considered a result that may have more than one facts. Here we only get the value of the given slot of the first facts of each token (result). A rdf statement is translated into one unordered Jess fact that has three slots, the subject slot, the predicate slot, and the object slot. See Jess documents for details on defquery or run-query.

Parameters:
queryName - name of the Jess query (defined by defquery).
args - the arguments to Jess query.
Throws:
D4jException - if anything goes wrong.

loadJessResourceWithClassLoader

public void loadJessResourceWithClassLoader(java.lang.ClassLoader cl,
                                            java.lang.String res)
                                     throws D4jException
Load Jess program (file) using the given class loader.

Parameters:
cl - the class loader with which to load the given Jess resource.
res - The name of the resource to be loaded.
Throws:
D4jException - if anything goes wrong.

inputJessFile

public void inputJessFile(java.lang.String filename)
                   throws D4jException
Load Jess code from a file, using Jess batch command.

Parameters:
filename - the filename from which to load Jess code.
Throws:
D4jException - if anything goes wrong.

loadRDFResource

public void loadRDFResource(java.lang.String uri)
                     throws D4jException
Load the RDF resource at the given URI. Each RDF statement will be converted to a Jess fact and asserted to Jess. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
uri - The location from which to load the RDF data.
Throws:
D4jException - if anything goes wrong.

loadRDFResource

public void loadRDFResource(java.lang.String uri,
                            boolean useAssert)
                     throws D4jException
Load the RDF resource at the given URI. Each RDF statement will be converted to a Jess fact and asserted to Jess. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
uri - The location from which to load the RDF data.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
Throws:
D4jException - if anything goes wrong.

loadRDFResource

public void loadRDFResource(java.io.InputStream istream,
                            java.lang.String xmlBase,
                            boolean useAssert)
                     throws D4jException
Load the RDF resource from the given input stream. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
istream - the input stream from which to load the document.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
xmlBase - the default xmlBase (uri) of the document. It should be the same as the xml:base in the document (if specified) in order for the system to work properly.
Throws:
D4jException - if anything goes wrong.

sureLoadRDF

protected void sureLoadRDF(java.io.InputStream istream,
                           java.lang.String xmlBase,
                           boolean useAssert)
                    throws D4jException
Load the RDF resource from the given input stream. This method does NOT check if the resource has been loaded, nor does it try to canonicalize the xmlBase(uri).

Parameters:
istream - the input stream from which to load the document.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
xmlBase - the default xmlBase (uri) of the document. It should be the same as the xml:base in the document (if specified) in order for the system to work properly.
Throws:
D4jException - if anything goes wrong.

assertWithDependency

public void assertWithDependency(java.lang.String facts,
                                 java.lang.String depId)
                          throws D4jException
Assert the given facts with the given dependency id, such that these facts can be retracted via the dependency id. This is implemented based on Jess's logical CE construct. Therefore, if a fact has more than one dependencies, it'll be retracted if and only if all the dependencies are gone. If depId is null or empty, no dependency will be generated.

Parameters:
facts - the facts to be asserted (with dependency).
depId - an id (if non null/empty) upon which the facts depend.
Throws:
D4jException - if anything goes wrong.

loadDAMLResource

public void loadDAMLResource(java.lang.String uri)
                      throws D4jException
Load daml resource from the given URI. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Facts geenrated from the daml resource will be asserted to Jess. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
uri - the uri from which to load the daml resource.
Throws:
D4jException - if anything goes wrong.

loadDAMLResource

public void loadDAMLResource(java.lang.String uri,
                             boolean useAssert)
                      throws D4jException
Load daml resource from the given URI. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
uri - the uri from which to load the daml resource.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
Throws:
D4jException - if anything goes wrong.

loadDAMLResource

public void loadDAMLResource(java.io.InputStream istream,
                             java.lang.String xmlBase,
                             boolean useAssert)
                      throws D4jException
Load daml resource from the given input stream. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
istream - the input stream from which to load the daml resource.
xmlBase - the default xml base of the document. It should be consistent with the xml:base in the document, if specified.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
Throws:
D4jException - if anything goes wrong.

setDependencyId

public void setDependencyId(java.lang.String id)
Set the dependency id of the resources to be loaded. That is, those facts that are asserted with this dependency can be retracted using this id. It's implemented based on Jess's logical CE construct.


applyDAMLSemantics

public void applyDAMLSemantics()
                        throws D4jException
Applies DAML semantics such as error checking as well as calling Rete.run() to apply basic semantics rules of the language.

Throws:
D4jException - if anything goes wrong.

typeCheck

public void typeCheck()
Checks to make sure all resources present in the KB have been given a type. This helps significantly with detecting errors in the input. However, it can be very slow. Currently, it does nothing.


objectIsType

public boolean objectIsType(java.lang.String object,
                            java.lang.String type)
                     throws D4jException
Query if the given object has the given type.

Parameters:
object - the object to be checked for whether has the given type.
type - the type for the type check of the given object.
Throws:
D4jException - if anything goes wrong.

loadDAMLResource

protected void loadDAMLResource(java.lang.String uri,
                                boolean useAssert,
                                int nestLevel)
                         throws D4jException
Load daml resource from the given URI. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
uri - the uri from which to load the daml resource.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
nestLevel - the current nested-level - initial level is 0; resources imported by current level is level + 1.
Throws:
D4jException - if anything goes wrong.

loadDAMLResource

protected void loadDAMLResource(java.io.InputStream istream,
                                java.lang.String xmlBase,
                                boolean useAssert,
                                int nestLevel)
                         throws D4jException
Load daml resource from the given input stream. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Note that if a resource is already loaded, it will not be loaded again.

Parameters:
istream - the input stream from which to load the daml resource.
xmlBase - the default xml base of the document. It should be consistent with the xml:base in the document, if specified.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
nestLevel - the current nested-level - initial level is 0; resources imported by current level is level + 1.
Throws:
D4jException - if anything goes wrong.

sureLoadDAML

protected void sureLoadDAML(java.io.InputStream istream,
                            java.lang.String xmlBase,
                            boolean useAssert,
                            int nestLevel)
                     throws D4jException
Load daml resource from the given input stream. DAML semantics will be applied. Whether to recursively load the imported resources is controlled by the system configuration. Note that for the current DAML document being loaded, it doesn't check if it has been loaded, nor does it try to canonicalize the xmlBase (uri).

Parameters:
istream - the input stream from which to load the daml resource.
xmlBase - the default xml base of the document. It should be consistent with the xml:base in the document, if specified.
useAssert - if true, facts will be asserted to Jess; if false, facts will be added via deffacts.
nestLevel - the current nested-level - initial level is 0; resources imported by current level is level + 1.
Throws:
D4jException - if anything goes wrong.

getImports4Uri

protected java.util.List getImports4Uri(java.lang.String uri)
                                 throws D4jException
Get the list of resoruces (no matter loaded or not) imported by the given resource (@ uri).

Parameters:
uri - uri of the resource whose imports list is to be retrieved.
Throws:
D4jException - if anything goes wrong.

isResourceLoaded

protected boolean isResourceLoaded(java.lang.String uri)
                            throws D4jException
check if the given resource has been loaded.

Parameters:
uri - uri of the resource to be checked.
Throws:
D4jException - if anything goes wrong.

getWriterForName

protected java.io.Writer getWriterForName(java.lang.String fileName)
                                   throws D4jException
Get the writer for the given file name. If a writer already exists for the file, it'll be returned; if not, a new writer will be created.

Parameters:
fileName - name of the file whose writer will be retrieved. Note that stdin, stdout, and stderr are used to refer standard input, standard output, and standard err output, respectively.
Returns:
the writer for the given file name.
D4jException

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test program.

java.lang.Exception