📄 adapter4rules_1_1.java
字号:
package org.mandarax.zkb.framework;
/**
* Copyright (C) 1999-2004 Jens Dietrich (mailto:mandarax@jbdietrich.com)
*
* 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
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Element;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.LogicFactory;
import org.mandarax.kernel.Prerequisite;
import org.mandarax.kernel.Rule;
import org.mandarax.zkb.ObjectPersistencyService;
import org.mandarax.zkb.ZKBException;
/**
* An adapter class for rules.
* @see org.mandarax.kernel.Rule
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 2.3
*/
public class Adapter4Rules_1_1 extends AbstractAdapter {
/**
* Export an object, i.e., convert it to an element in the DOM.
* @param obj an object
* @param driver the generic driver
* @param ops the object persistency service
* @exception a ZKBException is thrown if export fails
*/
public Element exportObject(Object obj,GenericDriver driver,ObjectPersistencyService ops) throws ZKBException {
check(obj,Rule.class);
Rule rule = (Rule)obj;
Element e = new Element(RULE);
Element eIf = new Element(IF);
e.addContent(eIf);
eIf.setAttribute(CONNECTIVE,rule.isBodyOrConnected()?OR:AND);
for (Iterator it = rule.getBody().iterator();it.hasNext();) {
Element el = exportObject(it.next(),PREREQUISITE,driver,ops);
eIf.addContent(el);
}
Element eThen = new Element(THEN);
e.addContent(eThen);
Element el = exportObject(rule.getHead(),TagAndAttributeNames.FACT,driver,ops);
eThen.addContent(el);
exportProperties(e,rule);
return e;
}
/**
* Build an object from an XML element.
* @param e an element
* @param driver the generic driver
* @param ops the object persistency service
* @param lfactory the logic factory used to create objects
* @exception a ZKBException is thrown if import fails
*/
public Object importObject(Element e,GenericDriver driver,ObjectPersistencyService ops,LogicFactory lfactory) throws ZKBException {
// get body
Element eIf = e.getChild(IF);
boolean isOrConnected = OR.equals(eIf.getAttributeValue(CONNECTIVE));
List body = new ArrayList();
Adapter adapter4Prereq = driver.getAdapter(PREREQUISITE);
List children = eIf.getChildren();
for (Iterator it = children.iterator();it.hasNext();) {
Element eFact = (Element)it.next();
Prerequisite premisse = (Prerequisite)adapter4Prereq.importObject(eFact,driver,ops,lfactory);
body.add(premisse);
}
// get head
Element eThen = e.getChild(THEN);
Fact head = (Fact)importChild(eThen,FACT,driver,ops,lfactory);
// build rule
Rule rule = lfactory.createRule(body,head,isOrConnected);
importProperties(e,rule);
return rule;
}
/**
* Get the name of the associated tag (element).
* @return a string
*/
public String getTagName() {
return RULE;
}
/**
* Print the DTD associated with this adapter on a string buffer.
* @param out the buffer to print on.
*/
public void printDTD(StringBuffer out) {
// warning : properties are declared here and only here
// this is sensitive w.r.t. the order in which adapters are registered
// here we assume that this is the first adapter registered that needs property support
out.append("<!ELEMENT rule (if,then,properties?)>\n");
out.append("<!ELEMENT if (prereq*)>\n");
out.append("<!ATTLIST if connective (and | or) \"and\">\n");
out.append("<!ELEMENT then (atom)>\n");
out.append("<!ELEMENT properties (property*)>\n");
out.append("<!ELEMENT property EMPTY>\n");
out.append("<!ATTLIST property key CDATA #REQUIRED>\n");
out.append("<!ATTLIST property value CDATA #REQUIRED>\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -