📄 person.java
字号:
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package test.org.mandarax.reference;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Vector;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.Function;
import org.mandarax.kernel.LogicFactory;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.Term;
import org.mandarax.kernel.meta.JFunction;
import org.mandarax.kernel.meta.JPredicate;
/**
* Test class providing a domain model for the test cases.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 1.1
*/
public class Person extends org.mandarax.kernel.LObject {
private static Hashtable instances = new Hashtable ();
private String name = null;
private static String[] varNames = { "x", "y", "z", "u", "v" };
private static String[] functionNames = { "father", "son" };
private static LogicFactory logicFactory =
LogicFactory.getDefaultFactory ();
/**
* Constructor.
*/
public Person() {
super ();
}
/**
* Constructor.
* @param n the name of the person
*/
public Person(String n) {
super ();
name = n;
}
/**
* Indicates whether the object equals the receiver.
* @param obj the object to compare this object with
* @return true if the objects are equal, false otherwise
*/
public boolean equals(Object obj) {
if(obj instanceof Person) {
Person p = (Person) obj;
if(p.name == null) {
return name == null;
} else {
return p.name.equals (name);
}
}
return false;
}
/**
* Indicates whether the object does not equal the receiver.
* @param obj the object to compare this object with
* @return false if the objects are equal, true otherwise
*/
public boolean equalsNot(Object obj) {
return !equals (obj);
}
/**
* Get the father of this person.
* @return the instance representing the father of this object
*/
public Person father() {
if(name.equals ("Max")) {
return get ("Jens");
}
if(name.equals ("Jens")) {
return get ("Klaus");
}
if(name.equals ("Sabine")) {
return get ("Klaus");
}
if(name.equals ("Klaus")) {
return get ("Otto");
}
if(name.equals ("Lutz")) {
return get ("Otto");
}
if(name.equals ("Frank")) {
return get ("Lutz");
}
if(name.equals ("Ralf")) {
return get ("Lutz");
}
return null;
}
/**
* Get the person with the name.
* @return an instance
* @param n a name
*/
public static Person get(String n) {
Person p = (Person) instances.get (n);
if(p == null) {
p = new Person (n);
instances.put (n, p);
}
return p;
}
/**
* Build a complex term from a string.
* @return a complex term
* @param n the string representing the term
* @param functName the name of the function
*/
static Term getComplexTerm(String n, String functName) {
try {
Method meth = Person.class.getMethod (functName,
new Class[0]);
Term innerTerm = getTerm (n.substring (functName.length () + 1,
n.length () - 1));
Term[] innerTerms = new Term[1];
innerTerms[0] = innerTerm;
Function funct = new JFunction (meth);
return logicFactory.createComplexTerm (funct, innerTerms);
} catch(Throwable t) {
System.out.println ("Error building complex term from " + n);
}
return null;
}
/**
* Get a constant with the provided name.
* @return a constant term
* @param n the name of the person that will be wrapped by the term
*/
static Term getConstant(String n) {
return logicFactory.createConstantTerm (get (n));
}
/**
* Get a fact.
* @return a fact
* @param predicate the string representation of the predicate
* @param name1 the string representation of the first term
* @param name2 the string representation of the second term
*/
public static Fact getFact(String predicate, String name1, String name2) {
try {
Term[] t = new Term[2];
t[0] = getTerm (name1);
t[1] = getTerm (name2);
return logicFactory.createFact(new JPredicate (getPredicate (predicate)), t);
} catch(Throwable t) {
return null;
}
}
/**
* Get a query.
* @return a query
* @param predicate the string representation of the predicate
* @param name1 the string representation of the first term
* @param name2 the string representation of the second term
*/
public static Query getQuery(String predicate, String name1, String name2) {
Fact fact = getFact(predicate,name1,name2);
if (fact==null) return null;
else return logicFactory.createQuery(fact,"a query");
}
/**
* Get the name.
* @return the name of the person
*/
public String getName() {
return name;
}
/**
* Get a type one method with one parameter.
* @return a java method
* @param name the string representation of the predicate
*/
static Method getPredicate(String name) {
if(name.equals ("equals")) {
return getPredicate (name, Object.class);
}
if(name.equals ("equalsNot")) {
return getPredicate (name, Object.class);
}
return getPredicate (name, Person.class);
}
/**
* Get a type one method with one parameter (of the type specified).
* @return a method
* @param name the string representation of the predicate
* @param parType the type of the parameter
*/
static Method getPredicate(String name, Class parType) {
try {
Class[] par = new Class[1];
par[0] = parType;
return Person.class.getMethod (name, par);
} catch(Throwable t) {
System.out.println (t.getMessage ());
return null;
}
}
/**
* Build a rule.
* @return a rule
* @param headPredicate the string representation of the head predicate
* @param headName1 the string representation of a term
* @param headName2 the string representation of a term
* @param bodyPredicate the string representation of the predicate of the first (and only) fact in the body
* @param bodyName1 the string representation of a term
* @param bodyName2 the string representation of a term
*/
public static Rule getRule(String headPredicate, String headName1,
String headName2, String bodyPredicate,
String bodyName1, String bodyName2) {
Fact head = getFact (headPredicate, headName1, headName2);
Vector body = new Vector ();
body.addElement (getFact (bodyPredicate, bodyName1, bodyName2));
return logicFactory.createRule (body, head);
}
/**
* Build a rule.
* @return a rule
* @param headPredicate the string representation of the head predicate
* @param headName1 the string representation of a term
* @param headName2 the string representation of a term
* @param bodyPredicate1 the string representation of the predicate of the first fact in the body
* @param bodyName11 the string representation of a term
* @param bodyName12 the string representation of a term
* @param bodyPredicate2 the string representation of the predicate of the second fact in the body
* @param bodyName21 the string representation of a term
* @param bodyName22 the string representation of a term
*/
public static Rule getRule(String headPredicate, String headName1,
String headName2, String bodyPredicate1,
String bodyName11, String bodyName12,
String bodyPredicate2, String bodyName21,
String bodyName22) {
Fact head = getFact (headPredicate, headName1, headName2);
Vector body = new Vector ();
body.addElement (getFact (bodyPredicate1, bodyName11, bodyName12));
body.addElement (getFact (bodyPredicate2, bodyName21, bodyName22));
return logicFactory.createRule (body, head);
}
/**
* Build a rule.
* @return a rule
* @param headPredicate the string representation of the head predicate
* @param headName1 the string representation of a term
* @param headName2 the string representation of a term
* @param bodyPredicate1 the string representation of the predicate of the first fact in the body
* @param bodyName11 the string representation of a term
* @param bodyName12 the string representation of a term
* @param bodyPredicate2 the string representation of the predicate of the second fact in the body
* @param bodyName21 the string representation of a term
* @param bodyName22 the string representation of a term
* @param bodyPredicate3 the string representation of the predicate of the third fact in the body
* @param bodyName31 the string representation of a term
* @param bodyName32 the string representation of a term
*/
public static Rule getRule(String headPredicate, String headName1,
String headName2, String bodyPredicate1,
String bodyName11, String bodyName12,
String bodyPredicate2, String bodyName21,
String bodyName22, String bodyPredicate3,
String bodyName31, String bodyName32) {
Fact head = getFact (headPredicate, headName1, headName2);
Vector body = new Vector ();
body.addElement (getFact (bodyPredicate1, bodyName11, bodyName12));
body.addElement (getFact (bodyPredicate2, bodyName21, bodyName22));
body.addElement (getFact (bodyPredicate3, bodyName31, bodyName32));
return logicFactory.createRule (body, head);
}
/**
* Get the term with the name.
* We build a variable if the string is one of the registered variable names.
* We build a function if the string is one of the registered function names followed by
* a string in brackets. Otherwise we build a constant.
* @return a term
* @param n the string representation of a term
*/
public static Term getTerm(String n) {
// first check whether the string is a variable
for(int i = 0; i < varNames.length; i++) {
if(varNames[i].equals (n)) {
return getVariable (n);
}
}
// next check whether the string is a function
for(int i = 0; i < functionNames.length; i++) {
if(n.startsWith (functionNames[i] + "(") && n.endsWith (")")) {
return getComplexTerm (n, functionNames[i]);
}
}
// otherwise the string is interpreted as a constant name
return getConstant (n);
}
/**
* Get a variable with the provided name.
* @return a variable term
* @param x the variable name
*/
public static Term getVariable(String x) {
return logicFactory.createVariableTerm (x, Person.class);
}
/**
* Get the hash code of the object.
* @return the hash value
*/
public int hashCode() {
if(name == null) {
return 0;
} else {
return name.hashCode ();
}
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @return true if p represents the brother of this instance
* @param p another person
*/
public boolean isBrotherOf(Person p) {
return false;
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @param p another person
*/
public boolean isFatherOf(Person p) {
return false;
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @param p another person
*/
public boolean isGrandFatherOf(Person p) {
return false;
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @param p another person
*/
public boolean isOncleOf(Person p) {
return false;
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @param p another person
*/
public boolean isRelativeOf(Person p) {
return false;
}
/**
* Method used to build a test predicate .
* @see org.mandarax.kernel.meta.JPredicate
* @param p another person
*/
public boolean isSonOf(Person p) {
return false;
}
/**
* Get the son of this person.
* @return the instance representing the son of this object
*/
public Person son() {
if(name.equals ("Jens")) {
return get ("Max");
}
if(name.equals ("Klaus")) {
return get ("Jens");
}
if(name.equals ("Otto")) {
return get ("Klaus");
}
return null;
}
/**
* Convert the receiver to a string.
* @return the string representation of this object
*/
public String toString() {
return name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -