| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "COWLPigeonholePrincipleGenerator.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
COWLPigeonholePrincipleGenerator::COWLPigeonholePrincipleGenerator() : CLogIdentifier("::Konclude::Test::OWLPigeonholePrincipleGenerator",this) { |
| 31 |
owllinkNS = "http://www.owllink.org/owllink#"; |
| 32 |
owlNS = "http://www.w3.org/2002/07/owl#"; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
COWLPigeonholePrincipleGenerator::~COWLPigeonholePrincipleGenerator() { |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
void COWLPigeonholePrincipleGenerator::generatePigeonholePrinciple(cint64 size, const QString& filename) { |
| 42 |
|
| 43 |
QFile file(filename); |
| 44 |
if (file.open(QIODevice::WriteOnly)) { |
| 45 |
|
| 46 |
|
| 47 |
rootNode = document.createElementNS(owllinkNS,"RequestMessage"); |
| 48 |
rootNode.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd"); |
| 49 |
rootNode.setAttribute("xmlns:owl",owlNS); |
| 50 |
document.appendChild(rootNode); |
| 51 |
|
| 52 |
|
| 53 |
QDomElement createOntologyNode = document.createElement("CreateKB"); |
| 54 |
createOntologyNode.setAttribute(QString("kb"),QString("http://www.owllink.org/testsuite/PigeonholePrinciple-%1").arg(size)); |
| 55 |
rootNode.appendChild(createOntologyNode); |
| 56 |
|
| 57 |
QDomElement tellOntologyNode = document.createElement("Tell"); |
| 58 |
tellOntologyNode.setAttribute(QString("kb"),QString("http://www.owllink.org/testsuite/PigeonholePrinciple-%1").arg(size)); |
| 59 |
rootNode.appendChild(tellOntologyNode); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
cint64 pigeonSize = std::log((double)(size+1))/std::log((double)10)+1; |
| 64 |
QStringList pigeonStringList; |
| 65 |
for (cint64 i = 0; i <= size; ++i) { |
| 66 |
QString pigeonString = QString::number(i); |
| 67 |
while (pigeonString.count() < pigeonSize) { |
| 68 |
pigeonString.prepend("0"); |
| 69 |
} |
| 70 |
pigeonString = QString("P%1").arg(pigeonString); |
| 71 |
pigeonStringList.append(pigeonString); |
| 72 |
} |
| 73 |
QStringList holeStringList; |
| 74 |
for (cint64 i = 0; i < size; ++i) { |
| 75 |
QString holeString = QString::number(i); |
| 76 |
while (holeString.count() < pigeonSize) { |
| 77 |
holeString.prepend("0"); |
| 78 |
} |
| 79 |
holeString = QString("H%1").arg(holeString); |
| 80 |
holeStringList.append(holeString); |
| 81 |
} |
| 82 |
|
| 83 |
QString satTestClass = QString("CSAT"); |
| 84 |
tellOntologyNode.appendChild(getOWLClassDeclaration(satTestClass)); |
| 85 |
|
| 86 |
|
| 87 |
QList<QDomElement> clauselList; |
| 88 |
// generate step 1 |
| 89 |
foreach (const QString& pigeonString, pigeonStringList) { |
| 90 |
QList<QDomElement> atLeastOneHoleList; |
| 91 |
foreach (const QString& holeString, holeStringList) { |
| 92 |
QString ph = pigeonString+holeString; |
| 93 |
tellOntologyNode.appendChild(getOWLClassDeclaration(ph)); |
| 94 |
atLeastOneHoleList.append(getOWLClass(ph)); |
| 95 |
} |
| 96 |
clauselList.append(getOWLUnion(atLeastOneHoleList)); |
| 97 |
} |
| 98 |
// generate step 2 |
| 99 |
foreach (const QString& holeString, holeStringList) { |
| 100 |
for (QStringList::const_iterator it = pigeonStringList.constBegin(), itEnd = pigeonStringList.constEnd(); it != itEnd; ++it) { |
| 101 |
QStringList::const_iterator it2 = it; |
| 102 |
for (++it2; it2 != itEnd; ++it2) { |
| 103 |
QString ph1 = *it+holeString; |
| 104 |
QString ph2 = *it2+holeString; |
| 105 |
clauselList.append(getOWLUnion(getOWLComplementClass(ph1),getOWLComplementClass(ph2))); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
tellOntologyNode.appendChild(getOWLEquivalentClass(getOWLClass("CSAT"),getOWLIntersection(clauselList))); |
| 111 |
|
| 112 |
|
| 113 |
QDomElement queryOntologyNode = document.createElement("IsClassSatisfiable"); |
| 114 |
queryOntologyNode.setAttribute(QString("kb"),QString("http://www.owllink.org/testsuite/PigeonholePrinciple-%1").arg(size)); |
| 115 |
queryOntologyNode.setAttribute(QString("timing"),QString("simple")); |
| 116 |
queryOntologyNode.appendChild(getOWLClass("CSAT")); |
| 117 |
|
| 118 |
QDomElement expectedResultNode = document.createElement("ExpectedResult"); |
| 119 |
QDomElement boolResultNode = document.createElement("BooleanResponse"); |
| 120 |
boolResultNode.setAttribute("result","false"); |
| 121 |
expectedResultNode.appendChild(boolResultNode); |
| 122 |
queryOntologyNode.appendChild(expectedResultNode); |
| 123 |
|
| 124 |
rootNode.appendChild(queryOntologyNode); |
| 125 |
|
| 126 |
file.write(document.toByteArray()); |
| 127 |
|
| 128 |
file.close(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLUnion(const QList<QDomElement>& list) { |
| 136 |
QDomElement dmDl = document.createElement("owl:ObjectUnionOf"); |
| 137 |
foreach (QDomElement dmEl, list) { |
| 138 |
dmDl.appendChild(dmEl); |
| 139 |
} |
| 140 |
return dmDl; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLUnion(const QDomElement& u1, const QDomElement& u2) { |
| 145 |
QDomElement dmDl = document.createElement("owl:ObjectUnionOf"); |
| 146 |
dmDl.appendChild(u1); |
| 147 |
dmDl.appendChild(u2); |
| 148 |
return dmDl; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLIntersection(const QList<QDomElement>& list) { |
| 153 |
QDomElement dmDl = document.createElement("owl:ObjectIntersectionOf"); |
| 154 |
foreach (QDomElement dmEl, list) { |
| 155 |
dmDl.appendChild(dmEl); |
| 156 |
} |
| 157 |
return dmDl; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLClass(const QString& className, bool negated) { |
| 162 |
if (negated) { |
| 163 |
return getOWLComplementClass(className); |
| 164 |
} else { |
| 165 |
return getOWLClass(className); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLComplementClass(const QString& className) { |
| 171 |
QDomElement dmDl = document.createElement("owl:ObjectComplementOf"); |
| 172 |
dmDl.appendChild(getOWLClass(className)); |
| 173 |
return dmDl; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLClassDeclaration(const QString& className) { |
| 178 |
QDomElement dmDl = document.createElement("owl:Declaration"); |
| 179 |
dmDl.appendChild(getOWLClass(className)); |
| 180 |
return dmDl; |
| 181 |
} |
| 182 |
|
| 183 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLClass(const QString& className) { |
| 184 |
QDomElement dmEl = document.createElement("owl:Class"); |
| 185 |
dmEl.setAttribute("IRI",className); |
| 186 |
return dmEl; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
QDomElement COWLPigeonholePrincipleGenerator::getOWLEquivalentClass(const QDomElement& eq1, const QDomElement& eq2) { |
| 191 |
QDomElement dmEl = document.createElement("owl:EquivalentClasses"); |
| 192 |
dmEl.appendChild(eq1); |
| 193 |
dmEl.appendChild(eq2); |
| 194 |
return dmEl; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
}; // end namespace Test |
| 201 |
|
| 202 |
}; // end namespace Konclude |