Knowledge Base
From ActiveMathWiki
| Table of contents |
How to connect?
To connect to a knowledge base, you need a URL. If you are running your knowledge base locally, you can connect to it straight away by using the URL:
private static final String MBASE = "http://localhost:27000/";
If you start your application via JNLP, retrieve the URL to your knowledge base from the submitted property
System.getProperty("mbaseRef.url", "http://127.0.0.1:27000/"))
Take this URL and connect to the knowledge base by instantiating an OJXmlRpcMBaseRef class. You receive a reference to the knowledge base which you will use for retrieving information.
public static MBaseRef connectToMBase() {
MBaseRef ref = null;
try {
ref = new OJXmlRpcMBaseRef(MBASE);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return ref;
}
Attributes
How to check whether an item in the database has an attribute?
public static boolean hasAttribute(MBaseRef ref, MBaseID id, String attrName) {
String hasValue="";
try {
hasValue = ref.getAttribute(id, attrName);
} catch (MBaseException e) {
e.printStackTrace();
return false;
}
if (hasValue.equals("")) {
return false;
} else {
return true;
}
}
Theories
How to get all theories in a collection (e.g., LeAM_calculus)?
public static Iterator getAllTheories(MBaseRef ref, String collection) {
Iterator theories = null;
Collection c = new LinkedList();
c.add(collection);
try {
// get all Theories from collection first
theories = ref.listTheories(c);
} catch(MBaseException e) {
e.printStackTrace();
}
return theories;
}
How to get all items in a theory?
public static Iterator getAllItemsInTheory(MBaseRef ref, MBaseID theory) {
Iterator theoryItems = null;
try {
theoryItems = ref.listItems(theory);
} catch(MBaseException e) {
e.printStackTrace();
}
return theoryItems;
}
How to list all theories?
public static void listAllTheories(MBaseRef ref, String collection) {
Collection c = new LinkedList();
c.add(collection);
try {
// get all Theories from collection first
Iterator i = ref.listTheories(c);
while (i.hasNext()) {
MBaseID m_id = (MBaseID) i.next();
}
} catch(MBaseException e) {
e.printStackTrace();
}
}
How to list all items in a specific theory?
public static void listAllItemsInTheory(MBaseRef ref, MBaseID theory) {
try {
Iterator theoryItems = ref.listItems(theory);
while (theoryItems.hasNext()) {
MBaseID th_id = (MBaseID) theoryItems.next();
System.out.println(" " + th_id);
}
} catch(MBaseException e) {
e.printStackTrace();
}
}
Relations
How to list all definitions for a symbol?
public static void listAllDefsForSymbol(MBaseRef ref, MBaseID symbol) {
try {
String type = ref.getTypeString(symbol);
if (type.equals("symbol")) {
// write german common name
Map cNameMap = ref.getCommonName(symbol);
if (cNameMap.containsKey("de")) {
System.out.println(" Name: " + cNameMap.get("de"));
}
Iterator it_defs = ref.getDefinitions(symbol, ref.getCollectionsProvided());
while (it_defs.hasNext()) {
MBaseID th_def_id = (MBaseID) it_defs.next();
System.out.println(" Def: " + th_def_id.getAbsoluteForm());
}
}
} catch(MBaseException e) {
e.printStackTrace();
}
}
How to list all relations of a definition?
public static void listAllRelsForDefinition(MBaseRef ref, MBaseID definition) {
System.out.println("\nDEBUG: getAllRelsForDefinition " + definition);
try {
Iterator its = ref.getRelated(definition, ref.getCollectionsProvided());
while (its.hasNext()) {
MBaseRef.Dependency it_id = (MBaseRef.Dependency) its.next();
String depType = it_id.getDependencyTypeString();
if (!depType.equals("SYMBOL_IN_DEFINITION")) {
System.out.println(" Dependency Type: " + depType);
MBaseID idOf = it_id.getIdOf();
MBaseID idOn = it_id.getIdOn();
System.out.println(" Depends of: " + idOf.getAbsoluteForm());
System.out.println(" Depends on: " + idOn.getAbsoluteForm());
}
}
} catch(MBaseException e) {
e.printStackTrace();
}
}
How to list all dependencies a definition might have?
public static void listAllDependsOnForDefinition(MBaseRef ref, MBaseID definition) {
System.out.println("\nDEBUG: getAllRelsDependsOnForDefinition " + definition);
try {
Iterator its = ref.getDependencies(definition);
while (its.hasNext()) {
MBaseRef.Dependency it_id = (MBaseRef.Dependency) its.next();
String depType = it_id.getDependencyTypeString();
if (!depType.equals("SYMBOL_IN_DEFINITION")) {
System.out.println(" Dependency Type: " + depType);
MBaseID idOf = it_id.getIdOf();
MBaseID idOn = it_id.getIdOn();
System.out.println(" Depends of: " + idOf.getAbsoluteForm());
System.out.println(" Depends on: " + idOn.getAbsoluteForm());
}
}
} catch(MBaseException e) {
e.printStackTrace();
}
}
Common Names
How to get the common name of an item?
public static String getCommonName(MBaseRef ref, MBaseID item, String lang) {
String name = null;
try {
Map map = ref.getCommonName(item);
if (map.containsKey(lang)) {
name = (String) map.get(lang);
}
} catch (MBaseException e) {
e.printStackTrace();
}
return name;
}
How to get the German name of an item?
public static String getGermanCommonName(MBaseRef ref, MBaseID item) {
return getCommonName(ref, item, "de");
}
How to get the English name of an item?
public static String getEnglishCommonName(MBaseRef ref, MBaseID item, String lang) {
return getCommonName(ref, item, "en");
}
