Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Sigma knowledge engineering system is an system for developing, viewing and debugging theories in first
order logic. It works with Knowledge Interchange Format (KIF) and is optimized for the Suggested Upper Merged
Ontology (SUMO) www.ontologyportal.org.
package com.articulate.sigma;
/* This code is copyright Articulate Software (c) 2003-2011. Some portions
copyright Teknowledge (c) 2003 and reused under the terms of the GNU license.
This software is released under the GNU Public License .
Users of this code also consent, by use of this code, to credit Articulate Software
and Teknowledge in any writings, briefings, publications, presentations, or
other representations of any software which incorporates, builds on, or uses this
code. Please cite the following article in any publication with references:
Pease, A., (2003). The Sigma Ontology Development Environment,
in Working Notes of the IJCAI-2003 Workshop on Ontology and Distributed Systems,
August 9, Acapulco, Mexico. See also http://sigmakee.sourceforge.net
*/
import com.articulate.sigma.nlg.NLGUtils;
import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** A utility class that creates HTML-formatting Strings for various purposes. */
public class HTMLformatter {
public static String htmlDivider =
("
"
+ "
"
+ ""
+ "
"
+ "
\n");
// set by BrowseBody.jsp or SimpleBrowseBody.jsp
public static String kbHref = "";
// set by BrowseBody.jsp or SimpleBrowseBody.jsp
public static String language = "EnglishLanguage";
public static ArrayList availableFormalLanguages =
new ArrayList(Arrays.asList("SUO-KIF","TPTP","traditionalLogic","OWL"));
/** *************************************************************
* Create the HTML for the labeled divider between the sections
* of the term display. Each section displays a sorted list of
* the Formulae for which a term appears in a specified argument
* position.
*/
public static String htmlDivider(String label) {
String result = "";
try {
StringBuilder sb = new StringBuilder();
sb.append("
");
sb.append("
");
sb.append(StringUtil.getLineSeparator());
if (StringUtil.isNonEmptyString(label)) {
sb.append("
");
sb.append(StringUtil.getLineSeparator());
sb.append(" ");
sb.append(" ");
sb.append(StringUtil.getLineSeparator());
result = sb.toString();
}
catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/** *************************************************************
* Create the HTML for a single step in a proof.
*/
public static String createKBHref(String kbName, String language) {
String hostname = KBmanager.getMgr().getPref("hostname");
if (hostname == null)
hostname = "localhost";
String port = KBmanager.getMgr().getPref("port");
if (port == null)
port = "8080";
return "http://" + hostname + ":" + port + "/sigma/Browse.jsp?lang=" + language + "&kb=" + kbName;
}
/** *************************************************************
* Create the HTML for a single step in a proof.
*/
public static String proofTableFormat(String query, ProofStep step, String kbName, String language) {
// System.out.println("Info in HTMLformatter.proofTableFormat(): " + step);
StringBuilder result = new StringBuilder();
Formula f = new Formula();
KB kb = KBmanager.getMgr().getKB(kbName);
f.read(step.axiom);
f.theFormula = Formula.postProcess(f.theFormula);
f.theFormula = ProofProcessor.removeNestedAnswerClause(f.theFormula);
String kbHref = HTMLformatter.createKBHref(kbName,language);
if (f.theFormula.equalsIgnoreCase("FALSE")) { // Successful resolution theorem proving results in a contradiction.
f.theFormula = "True"; // Change "FALSE" to "True" so it makes more sense to the user.
result.append("
" + "True" + "
");
}
else
result.append("
" + f.htmlFormat(kbHref) + "
");
result.append("
");
// System.out.println("Info in HTMLformatter.proofTableFormat(): premises : " + step.premises);
if (step.inferenceType!=null && step.inferenceType.equals("assume_negation")) {
result.append("[Negated Query]");
} else {
for (int i = 0; i < step.premises.size(); i++) {
Integer stepNum = (Integer) step.premises.get(i);
result.append(stepNum.toString() + " ");
}
if (step.premises.size() == 0) {
if (step.formulaType != null && step.formulaType.equals("conjecture"))
result.append("[Query]");
else if (step.formulaRole != null)
result.append(step.formulaRole);
else
result.append("[KB]");
}
}
result.append("
");
markup = show.toString();
}
catch (Exception ex) {
ex.printStackTrace();
}
return markup;
}
/** *****************************************************
*/
public static ArrayList getAllRelTerms(KB kb, ArrayList matchesList) {
ArrayList result = new ArrayList();
for (int i = 0; i < matchesList.size(); i++)
if (kb.kbCache.relations.contains(matchesList.get(i)))
result.add(matchesList.get(i));
return result;
}
/** *****************************************************
*/
public static ArrayList getAllNonRelTerms(KB kb, ArrayList matchesList) {
ArrayList result = new ArrayList();
for (int i = 0; i < matchesList.size(); i++)
if (!kb.kbCache.relations.contains(matchesList.get(i)))
result.add(matchesList.get(i));
return result;
}
/** *****************************************************
* Show list of 30 relation & nonRelation terms that contain a match to the input RE term. The inputed Strings
* relREmatch and nonRelREmatch are the two relation and nonRelation terms respectively that are the first terms
* at the top of the list. They are passed into the method to keep track of what 30 terms are being viewed.
*/
public static String showREMatches(KB kb, String relREmatch, String nonRelREmatch, String term) {
String markup = "";
try {
StringBuilder show = new StringBuilder();
ArrayList matchesList = kb.getREMatch(term);
ArrayList relTermsList = getAllRelTerms(kb,matchesList);
ArrayList nonRelTermsList = getAllNonRelTerms(kb,matchesList);
ArrayList largerList = (relTermsList.size()>nonRelTermsList.size())?relTermsList:nonRelTermsList;
ArrayList smallerList = (relTermsList.size()>nonRelTermsList.size())?nonRelTermsList:relTermsList;
int sizeDiff = largerList.size() - smallerList.size();
for (int i = 0; i < sizeDiff; i++) { //buffer smaller list
smallerList.add("");
}
show.append("
");
show.append("
");
show.append("
" + term + "
");
show.append("
\n
");
for (String t : largerList) {
if (t.equals((largerList==relTermsList?relREmatch:nonRelREmatch))) { //keeps track of which term is at the top
int matchIndex = largerList.indexOf(t); //matchIndex is the index of an REmatch in the larger list
int listLength = largerList.size(); //listLength is the the larger count of either relMatches or nonRelMatches
int finalIndex = (listLength>(matchIndex + 29) ? (matchIndex + 30) : listLength); //finalIndex is 1 + the index of the final match that will be displayed
//If there are at least 30 more matches after REmatch, then finalIndex=matchIndex+30, otherwise finalIndex = listLength
for (int i=matchIndex;i
");
return result.toString();
}
/** *************************************************************
* Show a hyperlinked list of WordNet synsets.
*/
public static String synsetList(ArrayList synsets, String kbHref) {
StringBuilder show = new StringBuilder();
for (int i = 0; i < synsets.size(); i++) {
String synset = (String) synsets.get(i);
if (Character.isDigit(synset.charAt(0)))
show.append("" + synset + "");
else
show.append(synset);
if (i < synsets.size()-1)
show.append(", ");
if (i % 10 == 0)
show.append("\n");
}
return show.toString();
}
/** *************************************************************
* Create the HTML for a section of the Sigma term browser page.
* Needs a
...
enclosure to format HTML properly.
*/
public static String formatFormulaList(ArrayList forms, String header, KB kb,
String language, String flang, int start, int localLimit, String limitString) {
boolean traditionalLogic = false;
if (flang.equals("traditionalLogic"))
traditionalLogic = true;
StringBuilder show = new StringBuilder();
boolean isArabic = (language.matches(".*(?i)arabic.*") || language.equalsIgnoreCase("ar"));
if (forms.size() < localLimit || localLimit == 0)
localLimit = forms.size();
for (int i = start; i < localLimit; i++) {
System.out.println(forms.get(i).getClass().getName());
String strForm = forms.get(i).theFormula;
//System.out.println("INFO in HTMLformatter.formatFormulaList(): formula: " + strForm);
Formula f = (Formula) kb.formulaMap.get(strForm);
//System.out.println("INFO in HTMLformatter.formatFormulaList(): structured formula: " + f);
if (KBmanager.getMgr().getPref("showcached").equalsIgnoreCase("yes") ||
!f.sourceFile.endsWith(KB._cacheFileSuffix) ) {
String arg0 = f.getArgument(0);
show.append("
\n");
}
}
show.append(limitString);
return show.toString();
}
/** *************************************************************
* Create the HTML for a section of the Sigma term browser page.
*/
public static String browserSectionFormatLimit(String term, String header, KB kb,
String language, String flang, int start, int limit,
int arg, String type) {
ArrayList forms = kb.ask(type,arg,term);
StringBuilder show = new StringBuilder();
String limitString = "";
int localLimit = start + limit;
if (forms != null && !KBmanager.getMgr().getPref("showcached").equalsIgnoreCase("yes"))
forms = TaxoModel.removeCached(forms);
if (forms != null && !forms.isEmpty()) {
Collections.sort(forms);
show.append(htmlDivider(header));
show.append("
\n");
}
return show.toString();
}
/** *************************************************************
* Create the HTML for a section of the Sigma term browser page.
*/
public static String browserSectionFormat(String term, String header,
KB kb, String language, String flang, int arg, String type) {
return browserSectionFormatLimit(term, header,kb, language, flang, 0, 50, arg,type);
}
/** *************************************************************
* Change spaces to "%20" along with many other URL codes. (for passing regex expressions through hyperlinks)
*/
public static String encodeForURL(String s) {
s = s.replaceAll(" ","%20");
s = s.replaceAll("\\!","%21");
s = s.replaceAll("\\$","%24");
s = s.replaceAll("\\(","%28");
s = s.replaceAll("\\)","%29");
s = s.replaceAll("\\*","%2A");
s = s.replaceAll("\\+","%2B");
s = s.replaceAll("\\.","%2E");
s = s.replaceAll("\\?","%3F");
s = s.replaceAll("\\[","%5B");
s = s.replaceAll("\\]","%5D");
s = s.replaceAll("\\^","%5E");
return s;
}
/** *************************************************************
* Change spaces to "%20"
*/
public static String decodeFromURL(String s) {
return s.replaceAll("%20"," ");
}
/** *************************************************************
* change reserved characters from '&' tags
*/
public static String encodeForHTML(String s) {
s = s.replaceAll("<","<");
s = s.replaceAll(">",">");
return s;
}
/** *************************************************************
* change reserved characters to '&' tags
*/
public static String decodeFromHTML(String s) {
s = s.replaceAll("<","<");
s = s.replaceAll(">",">");
return s;
}
/** *************************************************************
* Create an HTML menu, given an ArrayList of Strings where the
* value(s) are String representations of int(s) but the displayed
* menu items are String(s).
*/
public static String createNumberedMenu(String menuName, String selectedOption,
ArrayList options) {
StringBuilder result = new StringBuilder();
String menuNameProcessed = encodeForURL(menuName);
result.append("\n");
return result.toString();
}
/** *************************************************************
* Create an HTML menu, given an ArrayList of Strings.
*/
public static String createMenu(String menuName, String selectedOption, ArrayList options) {
String params = null;
return createMenu(menuName, selectedOption, options, params);
}
/** *************************************************************
* Create an HTML menu of KB names
*/
public static String createKBMenu(String kbName) {
ArrayList kbnames = new ArrayList();
kbnames.addAll(KBmanager.getMgr().getKBnames());
return(HTMLformatter.createMenu("kb",kbName,kbnames));
}
/** *************************************************************
* hyperlink formulas in error messages. It assumes that the errors
* are in and TreeSet of Strings in kb.errors. It further
* assumes that the error message is given first, followed by
* a colon, and then the axiom. There must be no other colon
* characters.
*/
//public static String formatErrors(KB kb, String kbHref) {
public static String formatErrors(KB kb) {
System.out.println("INFO in HTMLformatter.formatErrors(): href: " + kbHref);
StringBuilder result = new StringBuilder();
Iterator it = kb.errors.iterator();
while (it.hasNext()) {
String err = it.next();
err = err.replaceAll("\\n", " ");
int p = err.indexOf(":");
String begin = " ";
String end = "";
if (p > -1) {
begin += err.substring(0, p + 1);
end = err.substring(p + 1);
Formula f = new Formula();
f.theFormula = end;
//end = f.htmlFormat(kbHref);
end = f.htmlFormat(kb);
}
else
begin = err;
result.append(begin + end + "
");
}
return result.toString();
}
/** *************************************************************
* Create an HTML menu with an ID, given an ArrayList of
* Strings, and possibly multiple selections.
*/
public static String createMultiMenu(String menuName, TreeMap options) {
StringBuilder result = new StringBuilder();
String menuNameProcessed = encodeForURL(menuName);
result.append("\n");
return result.toString();
}
/** *************************************************************
* Create an HTML menu with an ID, given an ArrayList of Strings.
*/
public static String createMenu(String menuName, String selectedOption,
ArrayList options, String params) {
StringBuilder result = new StringBuilder();
TreeSet menuOptions = new TreeSet();
menuOptions.addAll(options);
String menuNameProcessed = encodeForURL(menuName);
result.append("\n");
return result.toString();
}
/** *************************************************************
* Create an HTML formatted result of a query.
*/
public static String formatProofResult(String result, String stmt, String processedStmt,
String lineHtml, String kbName, String language) {
return formatProofResult(result, stmt, processedStmt, lineHtml, kbName, language, 1);
}
/** *************************************************************
*/
public static String formatProofResult(String result, String stmt, String processedStmt,
String lineHtml, String kbName, String language, int answerOffset) {
if (result != null && result.toString().length() > 0) {
BasicXMLparser res = new BasicXMLparser(result.toString());
if (res != null) {
ArrayList elements = res.elements;
return formatProofResult(elements, stmt, processedStmt, lineHtml, kbName, language, answerOffset);
}
}
return null;
}
/** *************************************************************
*/
public static String formatTPTP3ProofResult(TPTP3ProofProcessor tpp, String stmt,
String lineHtml, String kbName, String language) {
System.out.println("INFO in HTMLformatter.formatTPTP3ProofResult(): number steps" + tpp.proof.size());
StringBuffer html = new StringBuffer();
for (int i = 0; i < tpp.bindings.size(); i++) {
if (i != 0)
html.append(lineHtml + "\n");
html.append("Answer " + "\n");
html.append(i+1);
html.append(". ");
String term = TPTP2SUMO.transformTerm(tpp.bindings.get(i));
String kbHref = HTMLformatter.createKBHref(kbName,language);
html.append("" + term + "");
html.append(" ");
}
html.append("
" + "\n");
for (int l = 0; l < tpp.proof.size(); l++) {
if (l % 2 == 1)
html.append("