From: <jen...@us...> - 2008-02-08 08:57:48
|
Revision: 526 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=526&view=rev Author: jenslehmann Date: 2008-02-08 00:57:28 -0800 (Fri, 08 Feb 2008) Log Message: ----------- added classes for supporting Prolog syntax (originally written by Sebastian Bader) Added Paths: ----------- trunk/src/dl-learner/org/dllearner/prolog/ trunk/src/dl-learner/org/dllearner/prolog/ArrayListSet.java trunk/src/dl-learner/org/dllearner/prolog/Atom.java trunk/src/dl-learner/org/dllearner/prolog/Body.java trunk/src/dl-learner/org/dllearner/prolog/Clause.java trunk/src/dl-learner/org/dllearner/prolog/Constant.java trunk/src/dl-learner/org/dllearner/prolog/Function.java trunk/src/dl-learner/org/dllearner/prolog/FunctionDefinition.java trunk/src/dl-learner/org/dllearner/prolog/List.java trunk/src/dl-learner/org/dllearner/prolog/Literal.java trunk/src/dl-learner/org/dllearner/prolog/Number.java trunk/src/dl-learner/org/dllearner/prolog/PredicateDefinition.java trunk/src/dl-learner/org/dllearner/prolog/Program.java trunk/src/dl-learner/org/dllearner/prolog/PrologConstant.java trunk/src/dl-learner/org/dllearner/prolog/StringConstant.java trunk/src/dl-learner/org/dllearner/prolog/Term.java trunk/src/dl-learner/org/dllearner/prolog/Variable.java Added: trunk/src/dl-learner/org/dllearner/prolog/ArrayListSet.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/ArrayListSet.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/ArrayListSet.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; + +/** + * + * @author Sebastian Bader + * + * @param <T> + */ +public class ArrayListSet<T> extends ArrayList<T> implements Set<T> { + + private static final long serialVersionUID = 1530739499015312204L; + + public ArrayListSet() { + this(10); + } + + public ArrayListSet(int initialCapacity) { + super(initialCapacity); + } + + public ArrayListSet(Collection<T> c) { + this(c.size()); + addAll(c); + } + + @Override + public boolean add(T o) { + if (contains(o)) + return false; + return super.add(o); + } + + @Override + public boolean addAll(Collection<? extends T> c) { + Iterator<? extends T> iter = c.iterator(); + boolean ret = false; + while (iter.hasNext()) { + if (add(iter.next())) { + ret = true; + } + } + return ret; + } + +} Added: trunk/src/dl-learner/org/dllearner/prolog/Atom.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Atom.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Atom.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,131 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; + +/** + * + * @author Sebastian Bader + * + */ +public class Atom { + String name; + ArrayList<Term> arguments; + + public Atom(String name, ArrayList<Term> arguments) { + super(); + this.name = name; + this.arguments = arguments; + } + + public ArrayList<Term> getArguments() { + return arguments; + } + + public String getName() { + return name; + } + + public boolean isGround() { + for (int i = 0; i < arguments.size(); i++) { + if (!getArgument(i).isGround()) + return false; + } + return true; + } + + public Term getArgument(int index) { + return arguments.get(index); + } + + public int getArity() { + return arguments.size(); + } + + /** + * + * @param variable + * Substitution variable. + * @param term + * A term. + * @return Returns a new instance of this term, where the variable is + * replaced by the term. + */ + public Atom getInstance(Variable variable, Term term) { + ArrayList<Term> newArgs = new ArrayList<Term>(arguments.size()); + for (int i = 0; i < arguments.size(); i++) { + Term argument = (Term) arguments.get(i); + newArgs.add(argument.getInstance(variable, term)); + } + return new Atom(name, newArgs); + } + + @Override + public String toString() { + StringBuffer ret = new StringBuffer("A[" + name + "/" + getArity() + "("); + for (int i = 0; i < arguments.size(); i++) { + ret.append(arguments.get(i).toString()); + if (i + 1 < arguments.size()) + ret.append(", "); + } + ret.append(")]"); + return ret.toString(); + } + + public String toPLString() { + StringBuffer ret = new StringBuffer(name + "("); + for (int i = 0; i < arguments.size(); i++) { + ret.append(((Term) arguments.get(i)).toPLString()); + if (i + 1 < arguments.size()) + ret.append(", "); + } + ret.append(")"); + return ret.toString(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + Atom a; + + try { + a = (Atom) obj; + } catch (ClassCastException cce) { + return false; + } + + if (!name.equals(a.name)) + return false; + + if (arguments == null) + return a.arguments == null; + else + return arguments.equals(a.arguments); + } + + @Override + public int hashCode() { + return name.hashCode() * (getArity() + 1); + } + +} Added: trunk/src/dl-learner/org/dllearner/prolog/Body.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Body.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Body.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,93 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; + +/** + * + * @author Sebastian Bader + * + */ +public class Body { + private ArrayList<Literal> literals; + + public Body() { + literals = new ArrayList<Literal>(); + } + + public void addLiteral(Literal literal) { + literals.add(literal); + } + + public ArrayList<Literal> getLiterals() { + return literals; + } + + public boolean isEmpty() { + return literals.isEmpty(); + } + + public boolean isGround() { + for (int i = 0; i < literals.size(); i++) { + if (!((Literal) literals.get(i)).isGround()) + return false; + } + + return true; + } + + public Body getInstance(Variable variable, Term term) { + Body newbody = new Body(); + + for (int i = 0; i < literals.size(); i++) { + Literal literal = (Literal) literals.get(i); + newbody.addLiteral(literal.getInstance(variable, term)); + } + + return newbody; + } + + @Override + public String toString() { + StringBuffer ret = new StringBuffer(); + + for (int i = 0; i < literals.size(); i++) { + ret.append(literals.get(i)); + if (i + 1 < literals.size()) + ret.append(", "); + } + + return ret.toString(); + } + + public String toPLString() { + StringBuffer ret = new StringBuffer(); + + for (int i = 0; i < literals.size(); i++) { + ret.append(((Literal) literals.get(i)).toPLString()); + if (i + 1 < literals.size()) + ret.append(", "); + } + + return ret.toString(); + } + +} Added: trunk/src/dl-learner/org/dllearner/prolog/Clause.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Clause.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Clause.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + + + +/** + * + * @author Sebastian Bader + * + */ +public class Clause { + private Atom head; + private Body body; + + public Clause(Atom head, Body body) { + this.head = head; + this.body = body; + if (body == null) + this.body = new Body(); + } + + @Override + public String toString() { + if (body.isEmpty()) + return head+"."; + return head + " :- " + body +"."; + } + + public String toPLString() { + if (body.isEmpty()) + return head.toPLString()+"."; + return head.toPLString() + " :- " + body.toPLString() +"."; + } + + public boolean isGround() { + if (!head.isGround()) + return false; + + return body.isGround(); + } + + public Body getBody() { + return body; + } + + public Atom getHead() { + return head; + } + + /** + * + * @param variable + * Substitution variable. + * @param term + * A term. + * @return Returns a new instance of this term, where the variable is + * replaced by the term. + */ + public Clause getInstance(Variable variable, Term term) { + Atom newhead = head.getInstance(variable, term); + Body newbody = body.getInstance(variable, term); + + return new Clause(newhead, newbody); + } + +} + Added: trunk/src/dl-learner/org/dllearner/prolog/Constant.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Constant.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Constant.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public abstract class Constant extends Term { +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/Function.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Function.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Function.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,189 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; + +/** + * + * @author Sebastian Bader + * + */ +public class Function extends Term { + + private String name; + private ArrayList<Term> arguments; + private int type; + + private Function(String name, int type) { + this.name = name; + this.type = type; + } + + private Function(String name, int type, ArrayList<Term> arguments) { + this.name = name; + this.type = type; + this.arguments = arguments; + } + + public Function(Function source) { + this(source.name, source.type); + arguments = new ArrayList<Term>(); + for (int i = 0; i < source.getArity(); i++) + arguments.add((Term) (source.getArgument(i)).clone()); + } + + public Function(String name, ArrayList<Term> arguments) { + this(name, FunctionDefinition.TYPE_USUAL); + this.arguments = arguments; + } + + public Function(String name, Term term2) { + this(name, FunctionDefinition.TYPE_PREFIX); + this.arguments = new ArrayList<Term>(1); + arguments.add(term2); + } + + public Function(Term term1, String name) { + this(name, FunctionDefinition.TYPE_POSTFIX); + this.arguments = new ArrayList<Term>(1); + arguments.add(term1); + } + + public Function(Term term1, String name, Term term2) { + this(name, FunctionDefinition.TYPE_INFIX); + this.arguments = new ArrayList<Term>(2); + arguments.add(term1); + arguments.add(term2); + } + + public Function(FunctionDefinition functionDefinition, ArrayList<Term> arguments) { + this(functionDefinition.getName(), functionDefinition.getType(), arguments); + } + + @Override + public Object clone() { + return new Function(this); + } + + public String getName() { + return name; + } + + public int getArity() { + return arguments.size(); + } + + public int getType() { + return type; + } + + public Term getArgument(int index) { + return (Term) arguments.get(index); + } + + public void setArgument(int index, Term term) { + arguments.set(index, term); + } + + @Override + public boolean isGround() { + for (int i = 0; i < arguments.size(); i++) { + if (!getArgument(i).isGround()) + return false; + } + return true; + } + + @Override + public String toString() { + StringBuffer ret = new StringBuffer("F" + FunctionDefinition.TYPE_NAMES[type] + "[" + name + + "/" + getArity() + "("); + for (int i = 0; i < arguments.size(); i++) { + ret.append(arguments.get(i).toString()); + if (i + 1 < arguments.size()) + ret.append(", "); + } + ret.append(")]"); + return ret.toString(); + } + + @Override + public String toPLString() { + if ((type == FunctionDefinition.TYPE_PREFIX) && (getArity() == 1)) { + return name + ((Term) arguments.get(0)).toPLString(); + } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 1)) { + return ((Term) arguments.get(0)).toPLString() + name; + } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 2)) { + return ((Term) arguments.get(0)).toPLString() + name + + ((Term) arguments.get(1)).toPLString(); + } else { + StringBuffer ret = new StringBuffer(name + "("); + for (int i = 0; i < arguments.size(); i++) { + ret.append(((Term) arguments.get(i)).toPLString()); + if (i + 1 < arguments.size()) + ret.append(", "); + } + ret.append(")"); + return ret.toString(); + } + } + + @Override + public Term getInstance(Variable variable, Term term) { + ArrayList<Term> newArgs = new ArrayList<Term>(arguments.size()); + for (int i = 0; i < arguments.size(); i++) { + Term argument = (Term) arguments.get(i); + newArgs.add(argument.getInstance(variable, term)); + } + return new Function(name, this.type, newArgs); + } + + @Override + public boolean equals(Object obj) { + + if (obj == null) + return false; + + Function f; + + try { + f = (Function) obj; + } catch (ClassCastException cce) { + return false; + } + + if (!name.equals(f.name)) + return false; + if (type != f.type) + return false; + + if (arguments == null) + return f.arguments == null; + else + return arguments.equals(f.arguments); + } + + @Override + public int hashCode() { + return name.hashCode() * (type + 1); + } + +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/FunctionDefinition.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/FunctionDefinition.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/FunctionDefinition.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public class FunctionDefinition { + public static int TYPE_USUAL = 0; + public static int TYPE_INFIX = 1; + public static int TYPE_POSTFIX = 2; + public static int TYPE_PREFIX = 3; + + public static String[] TYPE_NAMES = new String[]{"usual", "infix", "postfix", "prefix"}; + + private String name; + private int arity; + private int type; + + public FunctionDefinition(String name, int arity, int type) { + super(); + this.name = name; + this.arity = arity; + } + + public FunctionDefinition(Function function) { + this(function.getName(), function.getArity(), function.getType()); + } + + public int getArity() { + return arity; + } + public String getName() { + return name; + } + public int getType() { + return type; + } + + @Override + public int hashCode() { + return name.hashCode() * (arity + 1); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + try { + FunctionDefinition fd = (FunctionDefinition) obj; + if (fd.getArity() != getArity()) + return false; + if (!fd.getName().equals(getName())) + return false; + if (fd.getType() != getType()) + return false; + } catch (ClassCastException cce) { + return false; + } + return true; + } + + @Override + public String toString() { + return name+TYPE_NAMES[type]+"/"+arity; + } +} Added: trunk/src/dl-learner/org/dllearner/prolog/List.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/List.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/List.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,122 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; + + +/** + * + * @author Sebastian Bader + * + */ +public class List extends Term { + private Term head; + private List tail; + + public List() { + head = null; + tail = null; + } + + public List(Term head, List tail) { + this.head = head; + this.tail = tail; + if (tail == null) + this.tail = new List(); + } + + public static List compose(ArrayList<Term> content) { + if (content.isEmpty()) { + return new List(); + } else { + Term head = (Term) content.remove(0); + List body = compose(content); + return new List(head, body); + } + } + + @Override + public boolean isGround() { + if (!head.isGround()) + return false; + return tail.isGround(); + } + + + @Override + public String toString() { + return "L["+((head != null)?head.toString()+"|"+tail:"")+"]"; + } + + @Override + public String toPLString() { + return "["+((head != null)?head.toPLString()+"|"+tail.toPLString():"")+"]"; + } + + @Override + public Term getInstance(Variable variable, Term term) { + if (head != null) { + Term newhead = head.getInstance(variable, term); + List newtail = (List) tail.getInstance(variable, term); + return new List(newhead, newtail); + } + return new List(null, null); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + List list; + try { + list = (List) obj; + } catch (ClassCastException cce) { + return false; + } + + if (head == null) { + if (list.head != null) + return false; + } else { + if (!head.equals(list.head)) + return false; + } + + if (tail == null) { + return (list.tail == null); + } else { + return tail.equals(list.tail); + } + } + + @Override + public int hashCode() { + if (head == null) + return 0; + return head.hashCode(); + } + + @Override + public Object clone() { + return new List((Term) head.clone(), (List) tail.clone()); + } +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/Literal.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Literal.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Literal.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + + + +/** + * + * @author Sebastian Bader + * + */ +public class Literal { + private Atom atom; + private boolean positive; + + public Literal(Atom atom, boolean state) { + this.atom = atom; + this.positive = state; + } + + public Atom getAtom() { + return atom; + } + + public boolean isPositive() { + return positive; + } + + public boolean isGround() { + return atom.isGround(); + } + + public Literal getInstance(Variable variable, Term term) { + return new Literal(atom.getInstance(variable, term), positive); + } + + @Override + public String toString() { + return (positive?"+":"-")+atom.toString(); + } + + public String toPLString() { + return (positive?"":"not ")+atom.toPLString(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + Literal l; + + try { + l = (Literal) obj; + } catch (ClassCastException cce) { + return false; + } + + if (positive != l.positive) + return false; + + return atom.equals(l.atom); + } + + @Override + public int hashCode() { + return atom.hashCode() * (positive?1:2); + } +} Added: trunk/src/dl-learner/org/dllearner/prolog/Number.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Number.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Number.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,92 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + + +/** + * + * @author Sebastian Bader + * + */ +public class Number extends Constant { + private double value; + + public Number(String src) { + value = Double.parseDouble(src); + } + + public Number(double value) { + this.value = value; + } + + public int getIntValue() { + return (int) value; + } + public double getDoubleValue() { + return value; + } + + @Override + public boolean isGround() { + return true; + } + + @Override + public String toString() { + return "C["+toPLString()+"]"; + } + @Override + public String toPLString() { + if (((double)((int)value)) == value) + return ""+(int) value; + return ""+value; + } + + + @Override + public Term getInstance(Variable variable, Term term) { + return new Number(value); + } + + @Override + public int hashCode() { + return (int) value; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + Number number; + try { + number = (Number) obj; + } catch (ClassCastException cce) { + return false; + } + + return value == number.value; + } + + @Override + public Object clone() { + return new Number(value); + } +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/PredicateDefinition.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/PredicateDefinition.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/PredicateDefinition.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public class PredicateDefinition { + private String name; + private int arity; + + public PredicateDefinition(String name, int arity) { + super(); + this.name = name; + this.arity = arity; + } + + public PredicateDefinition(Atom atom) { + this(atom.getName(), atom.getArity()); + } + + public int getArity() { + return arity; + } + + public String getName() { + return name; + } + + @Override + public int hashCode() { + return name.hashCode() * (arity + 1); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + try { + PredicateDefinition pd = (PredicateDefinition) obj; + if (pd.getArity() != getArity()) + return false; + if (!pd.getName().equals(getName())) + return false; + } catch (ClassCastException cce) { + return false; + } + return true; + } + + @Override + public String toString() { + return name + "/" + arity; + } + +} Added: trunk/src/dl-learner/org/dllearner/prolog/Program.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Program.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Program.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,79 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +import java.util.ArrayList; + +/** + * + * @author Sebastian Bader + * + */ +public class Program { + private ArrayList<Clause> clauses; + + public Program() { + clauses = new ArrayList<Clause>(); + } + + public void addClause(Clause clause) { + clauses.add(clause); + } + + public ArrayList<Clause> getClauses() { + return clauses; + } + + public boolean isGround() { + for (int c = 0; c < clauses.size(); c++) { + Clause clause = (Clause) clauses.get(c); + if (!clause.isGround()) + return false; + } + + return true; + } + + @Override + public String toString() { + StringBuffer ret = new StringBuffer(); + + for (int i = 0; i < clauses.size(); i++) { + ret.append(clauses.get(i)); + if (i + 1 < clauses.size()) + ret.append(" "); + } + + return ret.toString(); + } + + public String toPLString() { + StringBuffer ret = new StringBuffer(); + + for (int i = 0; i < clauses.size(); i++) { + ret.append(((Clause) clauses.get(i)).toPLString()); + if (i + 1 < clauses.size()) + ret.append("\n"); + } + + return ret.toString(); + } + +} Added: trunk/src/dl-learner/org/dllearner/prolog/PrologConstant.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/PrologConstant.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/PrologConstant.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,76 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + + +/** + * + * @author Sebastian Bader + * + */ +public class PrologConstant extends Constant { + private String name; + + public PrologConstant(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean isGround() { + return true; + } + + @Override + public String toString() { + return "C["+name+"]"; + } + @Override + public String toPLString() { + return name; + } + + @Override + public Term getInstance(Variable variable, Term term) { + return new PrologConstant(name); + } + + @Override + public boolean equals(Object obj) { + return name.equals(obj); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public Object clone() { + return new PrologConstant(name); + } +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/StringConstant.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/StringConstant.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/StringConstant.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,72 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public class StringConstant extends Constant { + private String string; + + public StringConstant(String src) { + string = src; + } + + public String getString() { + return string; + } + + @Override + public boolean isGround() { + return true; + } + + @Override + public String toString() { + return "C[" + string + "]"; + } + + @Override + public String toPLString() { + return string; + } + + @Override + public Term getInstance(Variable variable, Term term) { + return new StringConstant(string); + } + + @Override + public boolean equals(Object obj) { + return string.equals(obj); + } + + @Override + public int hashCode() { + return string.hashCode(); + } + + @Override + public Object clone() { + return new StringConstant(string); + } +} \ No newline at end of file Added: trunk/src/dl-learner/org/dllearner/prolog/Term.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Term.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Term.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,60 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public abstract class Term implements Cloneable { + + /** + * + * @return Returns true iff this term is ground + */ + public abstract boolean isGround(); + + /** + * + * @param variable + * Substitution variable. + * @param term + * A term. + * @return Returns a new instance of this term, where the variable is + * replaced by the term. + */ + public abstract Term getInstance(Variable variable, Term term); + + @Override + public abstract boolean equals(Object obj); + + @Override + public abstract int hashCode(); + + @Override + public abstract Object clone(); + + @Override + public abstract String toString(); + + public abstract String toPLString(); + +} Added: trunk/src/dl-learner/org/dllearner/prolog/Variable.java =================================================================== --- trunk/src/dl-learner/org/dllearner/prolog/Variable.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/prolog/Variable.java 2008-02-08 08:57:28 UTC (rev 526) @@ -0,0 +1,89 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.prolog; + +/** + * + * @author Sebastian Bader + * + */ +public class Variable extends Term { + private String name; + + public Variable(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean isGround() { + return false; + } + + @Override + public String toString() { + return "V[" + name + "]"; + } + + @Override + public String toPLString() { + return name; + } + + @Override + public Term getInstance(Variable variable, Term term) { + if (this.equals(variable)) { + return term; + } + return this; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + + Variable v; + try { + v = (Variable) obj; + } catch (ClassCastException cce) { + return false; + } + + return name.equals(v.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public Object clone() { + return new Variable(name); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |