Skip to content

Commit

Permalink
Enhanced test cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Emmons committed Apr 26, 2019
1 parent 1914d73 commit 95204ff
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 326 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -2,7 +2,6 @@ bin/
indexes/
lib/
log/
union-test-kb-data/

/build.properties
/dependencies/
Expand Down
34 changes: 17 additions & 17 deletions Parliament/KbCore/KbConfig.cpp
Expand Up @@ -255,26 +255,26 @@ void pmnt::KbConfig::timeoutUnit(const string& newValue) {
}
}

void pmnt::KbConfig::disableAllRules()
void pmnt::KbConfig::setStatusOfAllRules(bool enableAllRules)
{
m_enableSWRLRuleEngine = false;
m_enableSWRLRuleEngine = enableAllRules;

m_isSubclassRuleOn = false;
m_isSubpropertyRuleOn = false;
m_isDomainRuleOn = false;
m_isRangeRuleOn = false;
m_isEquivalentClassRuleOn = false;
m_isEquivalentPropRuleOn = false;
m_isInverseOfRuleOn = false;
m_isSymmetricPropRuleOn = false;
m_isFunctionalPropRuleOn = false;
m_isInvFunctionalPropRuleOn = false;
m_isTransitivePropRuleOn = false;
m_isSubclassRuleOn = enableAllRules;
m_isSubpropertyRuleOn = enableAllRules;
m_isDomainRuleOn = enableAllRules;
m_isRangeRuleOn = enableAllRules;
m_isEquivalentClassRuleOn = enableAllRules;
m_isEquivalentPropRuleOn = enableAllRules;
m_isInverseOfRuleOn = enableAllRules;
m_isSymmetricPropRuleOn = enableAllRules;
m_isFunctionalPropRuleOn = enableAllRules;
m_isInvFunctionalPropRuleOn = enableAllRules;
m_isTransitivePropRuleOn = enableAllRules;

m_inferRdfsClass = false;
m_inferOwlClass = false;
m_inferRdfsResource = false;
m_inferOwlThing = false;
m_inferRdfsClass = enableAllRules;
m_inferOwlClass = enableAllRules;
m_inferRdfsResource = enableAllRules;
m_inferOwlThing = enableAllRules;
}

const pmnt::KbConfig& pmnt::KbConfig::ensureKbDirExists() const
Expand Down
30 changes: 8 additions & 22 deletions Parliament/KbCore/KbInstance.cpp
Expand Up @@ -1335,34 +1335,20 @@ string pmnt::KbInstance::formatRsrcUri(ResourceId rsrcId, bool includeRsrcId) co
}
}

void pmnt::KbInstance::deleteKb(const KbConfig& cfg, const bfs::path& dir)
{
remove(dir / cfg.uriTableFileName());
remove(dir / cfg.uriToIntFileName());
remove(dir / cfg.stmtFileName());
remove(dir / cfg.rsrcFileName());
}

void pmnt::KbInstance::deleteKb(const KbConfig& cfg)
void pmnt::KbInstance::deleteKb(const KbConfig& cfg, bool deleteContainingDir)
{
remove(cfg.uriTableFilePath());
remove(cfg.uriToIntFilePath());
remove(cfg.stmtFilePath());
remove(cfg.rsrcFilePath());
}

void pmnt::KbInstance::deleteKb(const bfs::path& dir)
{
KbConfig config;
config.readFromFile();
deleteKb(config, dir);
}

void pmnt::KbInstance::deleteKb()
{
KbConfig config;
config.readFromFile();
deleteKb(config);
if (deleteContainingDir
&& exists(cfg.kbDirectoryPath())
&& is_directory(cfg.kbDirectoryPath())
&& is_empty(cfg.kbDirectoryPath()))
{
remove(cfg.kbDirectoryPath());
}
}

size_t pmnt::KbInstance::ruleCount() const
Expand Down
31 changes: 11 additions & 20 deletions Parliament/KbCore/KbInstanceJNI.cpp
Expand Up @@ -202,35 +202,26 @@ JNIEXPORT jshort JNICALL Java_com_bbn_parliament_jni_KbInstance_determineDisposi
}

JNIEXPORT void JNICALL Java_com_bbn_parliament_jni_KbInstance_deleteKb(
JNIEnv* pEnv, jclass /* cls */, jobject javaConfig, jstring directory)
JNIEnv* pEnv, jclass /* cls */, jobject javaConfig, jstring directory, jboolean deleteContainingDir)
{
BEGIN_JNI_EXCEPTION_HANDLER(pEnv)
KbConfig cppConfig;
if (javaConfig == 0)
{
if (directory == 0)
{
KbInstance::deleteKb();
}
else
{
string dir = JNIHelper::jstringToCstring<char>(pEnv, directory);
KbInstance::deleteKb(dir);
}
cppConfig.readFromFile();
}
else
{
KbConfig cppConfig;
assignJavaConfigToCppConfig(cppConfig, pEnv, javaConfig);
if (directory == 0)
{
KbInstance::deleteKb(cppConfig);
}
else
{
string dir = JNIHelper::jstringToCstring<char>(pEnv, directory);
KbInstance::deleteKb(cppConfig, dir);
}
}

if (directory != 0)
{
cppConfig.kbDirectoryPath(
JNIHelper::jstringToCstring<char>(pEnv, directory));
}

KbInstance::deleteKb(cppConfig, !!deleteContainingDir);
END_JNI_EXCEPTION_HANDLER(pEnv)
}

Expand Down
6 changes: 5 additions & 1 deletion Parliament/KbCore/parliament/KbConfig.h
Expand Up @@ -243,7 +243,11 @@ class KbConfig : public Config
void inferOwlThing(bool newValue)
{ m_inferOwlThing = newValue; }

void disableAllRules();
void disableAllRules()
{ setStatusOfAllRules(false); }
void enableAllRules()
{ setStatusOfAllRules(true); }
void setStatusOfAllRules(bool enableAllRules);

const KbConfig& ensureKbDirExists() const;

Expand Down
5 changes: 1 addition & 4 deletions Parliament/KbCore/parliament/KbInstance.h
Expand Up @@ -147,10 +147,7 @@ class KbInstance

PARLIAMENT_EXPORT static KbDisposition determineDisposition(
const KbConfig& config, bool throwIfIndeterminate = false);
PARLIAMENT_EXPORT static void deleteKb(const KbConfig& cfg, const ::boost::filesystem::path& dir);
PARLIAMENT_EXPORT static void deleteKb(const KbConfig& cfg);
PARLIAMENT_EXPORT static void deleteKb(const ::boost::filesystem::path& dir);
PARLIAMENT_EXPORT static void deleteKb();
PARLIAMENT_EXPORT static void deleteKb(const KbConfig& cfg, bool deleteContainingDir);

PARLIAMENT_EXPORT size_t ruleCount() const;
PARLIAMENT_EXPORT void printRules(::std::ostream& s) const;
Expand Down
178 changes: 88 additions & 90 deletions Parliament/Test/DeftLoadTest.cpp
Expand Up @@ -22,6 +22,7 @@
#include "parliament/RegEx.h"
#include "parliament/UnicodeIterator.h"
#include "parliament/Util.h"
#include "TestUtils.h"

namespace bdata = ::boost::unit_test::data;
namespace bfs = ::boost::filesystem;
Expand All @@ -39,6 +40,7 @@ using BlankNodeMap = ::std::unordered_map<::std::string, ResourceId>;
BOOST_AUTO_TEST_SUITE(DeftTestSuite)

static const TChar k_pmntDepsEnvVar[] = _T("PARLIAMENT_DEPENDENCIES");
static const TChar k_defaultDepsDir[] = _T("../../dependencies");
static const TChar k_dataFileDir[] = _T("data");
static const TChar k_deftDataFileName[] = _T("deft-data-load.nt");
static const char k_blankOrCommentRegExStr[] = "^[ \t]*(?:#.*)?$";
Expand Down Expand Up @@ -70,9 +72,10 @@ static ::std::vector<bfs::path> filesToLoad()
auto envVarValue = tGetEnvVar(k_pmntDepsEnvVar);
if (envVarValue.empty())
{
PMNT_LOG(g_log, log::Level::warn) << "Skipping test: Environment variable "
<< convertTCharToUtf8(k_pmntDepsEnvVar) << " is not defined.";
return result;
envVarValue = k_defaultDepsDir;
PMNT_LOG(g_log, log::Level::debug) << "Environment variable "
<< convertTCharToUtf8(k_pmntDepsEnvVar) << " is not defined. Defaulting to "
<< convertTCharToUtf8(envVarValue);
}

bfs::path dataDir{envVarValue};
Expand Down Expand Up @@ -123,111 +126,106 @@ BOOST_DATA_TEST_CASE(
HiResTimer timer;

KbConfig config;
config.kbDirectoryPath("test-kb-data");
KbInstance::deleteKb(config);

config.kbDirectoryPath(_T("test-kb-data"));
KbDeleter deleter(config, true);
KbInstance kb(config);

BlankNodeMap bnodeMap;
RegEx blankOrCommentRex = compileRegEx(k_blankOrCommentRegExStr);
RegEx tripleRex = compileRegEx(k_nTripleRegExStr);
for (bfs::ifstream in(dataFile, ::std::ios::in); !in.eof();)
{
KbInstance kb(config);

BlankNodeMap bnodeMap;
RegEx blankOrCommentRex = compileRegEx(k_blankOrCommentRegExStr);
RegEx tripleRex = compileRegEx(k_nTripleRegExStr);
for (bfs::ifstream in(dataFile, ::std::ios::in); !in.eof();)
if (!in)
{
if (!in)
throw runtime_error("IO error reading from data file");
}
else
{
string line;
getline(in, line);
SMatch blankOrCommentCaptures;
SMatch tripleCaptures;
if (regExMatch(line, blankOrCommentCaptures, blankOrCommentRex))
{
throw runtime_error("IO error reading from data file");
// Do nothing
}
else
else if (regExMatch(line, tripleCaptures, tripleRex))
{
string line;
getline(in, line);
SMatch blankOrCommentCaptures;
SMatch tripleCaptures;
if (regExMatch(line, blankOrCommentCaptures, blankOrCommentRex))
string uriSubj = tripleCaptures[1].str();
string blankSubj = tripleCaptures[2].str();
string pred = tripleCaptures[3].str();
string uriObj = tripleCaptures[4].str();
string blankObj = tripleCaptures[5].str();
string litObj = tripleCaptures[6].str();

//if (!uriSubj.empty()) { BOOST_TEST_MESSAGE("URI Subject: '" << uriSubj << "'"); }
//if (!blankSubj.empty()) { BOOST_TEST_MESSAGE("Blank Subject: '" << blankSubj << "'"); }
//BOOST_TEST_MESSAGE("Predicate: '" << pred << "'");
//if (!uriObj.empty()) { BOOST_TEST_MESSAGE("URI Object: '" << uriObj << "'"); }
//if (!blankObj.empty()) { BOOST_TEST_MESSAGE("Blank Object: '" << blankObj << "'"); }
//if (!litObj.empty()) { BOOST_TEST_MESSAGE("Literal Object: '" << litObj << "'"); }

ResourceId subjId;
if (!uriSubj.empty())
{
// Do nothing
subjId = kb.uriToRsrcId(convertToRsrcChar(uriSubj), false, true);
}
else if (regExMatch(line, tripleCaptures, tripleRex))
else
{
subjId = getBNodeId(kb, bnodeMap, blankSubj);
}
ResourceId predId = kb.uriToRsrcId(convertToRsrcChar(pred), false, true);
ResourceId objId;
if (!uriObj.empty())
{
string uriSubj = tripleCaptures[1].str();
string blankSubj = tripleCaptures[2].str();
string pred = tripleCaptures[3].str();
string uriObj = tripleCaptures[4].str();
string blankObj = tripleCaptures[5].str();
string litObj = tripleCaptures[6].str();

//if (!uriSubj.empty()) { BOOST_TEST_MESSAGE("URI Subject: '" << uriSubj << "'"); }
//if (!blankSubj.empty()) { BOOST_TEST_MESSAGE("Blank Subject: '" << blankSubj << "'"); }
//BOOST_TEST_MESSAGE("Predicate: '" << pred << "'");
//if (!uriObj.empty()) { BOOST_TEST_MESSAGE("URI Object: '" << uriObj << "'"); }
//if (!blankObj.empty()) { BOOST_TEST_MESSAGE("Blank Object: '" << blankObj << "'"); }
//if (!litObj.empty()) { BOOST_TEST_MESSAGE("Literal Object: '" << litObj << "'"); }

ResourceId subjId;
if (!uriSubj.empty())
{
subjId = kb.uriToRsrcId(convertToRsrcChar(uriSubj), false, true);
}
else
{
subjId = getBNodeId(kb, bnodeMap, blankSubj);
}
ResourceId predId = kb.uriToRsrcId(convertToRsrcChar(pred), false, true);
ResourceId objId;
if (!uriObj.empty())
{
objId = kb.uriToRsrcId(convertToRsrcChar(uriObj), false, true);
}
else if (!litObj.empty())
{
objId = kb.uriToRsrcId(convertToRsrcChar(litObj), true, true);
}
else
{
objId = getBNodeId(kb, bnodeMap, blankObj);
}

kb.addStmt(subjId, predId, objId, false);
objId = kb.uriToRsrcId(convertToRsrcChar(uriObj), false, true);
}
else if (!litObj.empty())
{
objId = kb.uriToRsrcId(convertToRsrcChar(litObj), true, true);
}
else
{
BOOST_TEST_MESSAGE("Line doesn't match: \"" << line << "\"");
objId = getBNodeId(kb, bnodeMap, blankObj);
}

kb.addStmt(subjId, predId, objId, false);
}
else
{
BOOST_TEST_MESSAGE("Line doesn't match: \"" << line << "\"");
}
}
}

timer.stop();
BOOST_TEST_MESSAGE("Time to load'" << dataFile.generic_string()
<< "': " << timer.getSec() << " sec");
timer.stop();
BOOST_TEST_MESSAGE("Time to load'" << dataFile.generic_string()
<< "': " << timer.getSec() << " sec");

size_t total = 0;
size_t numDel = 0;
size_t numInferred = 0;
size_t numDelAndInferred = 0;
size_t numHidden = 0;
size_t numVirtual = 0;
kb.countStmts(total, numDel, numInferred, numDelAndInferred, numHidden, numVirtual);
size_t total = 0;
size_t numDel = 0;
size_t numInferred = 0;
size_t numDelAndInferred = 0;
size_t numHidden = 0;
size_t numVirtual = 0;
kb.countStmts(total, numDel, numInferred, numDelAndInferred, numHidden, numVirtual);

if (dataFile.filename() == k_deftDataFileName)
{
// Note: The total above does not count virtual statements.
const size_t k_numStmtsInFile = 59837u;
const size_t k_numReifications = 5994u;
BOOST_CHECK_EQUAL(2u, numDel);
BOOST_CHECK_EQUAL(0u, numDelAndInferred);
BOOST_CHECK_EQUAL(k_numReifications, numHidden);
BOOST_CHECK_EQUAL(4 * k_numReifications, numVirtual);
BOOST_CHECK_EQUAL(k_numStmtsInFile, total + numVirtual - numInferred - numDel - numHidden);
BOOST_CHECK_EQUAL(37186u, numInferred);
}
else
{
BOOST_CHECK(total > 0u);
}
if (dataFile.filename() == k_deftDataFileName)
{
// Note: The total above does not count virtual statements.
const size_t k_numStmtsInFile = 59837u;
const size_t k_numReifications = 5994u;
BOOST_CHECK_EQUAL(2u, numDel);
BOOST_CHECK_EQUAL(0u, numDelAndInferred);
BOOST_CHECK_EQUAL(k_numReifications, numHidden);
BOOST_CHECK_EQUAL(4 * k_numReifications, numVirtual);
BOOST_CHECK_EQUAL(k_numStmtsInFile, total + numVirtual - numInferred - numDel - numHidden);
BOOST_CHECK_EQUAL(37186u, numInferred);
}
else
{
BOOST_CHECK(total > 0u);
}

KbInstance::deleteKb(config);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 95204ff

Please sign in to comment.