001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.rdf.examples;
020:
021: import org.mandarax.rdf.*;
022: import java.net.URL;
023: import org.mandarax.kernel.*;
024: import org.mandarax.rdf.RDFClauseSet;
025: import org.mandarax.reference.AdvancedKnowledgeBase;
026: import org.mandarax.reference.ResolutionInferenceEngine2;
027: import org.mandarax.util.LogicFactorySupport;
028: import org.mandarax.kernel.Predicate;
029: import org.mandarax.rdf.RDFPredicateRegistry;
030:
031: /**
032: * A simple use case example for the Mandarax RDF API.
033: *
034: * "domain.rdf" describes a general domain model as a taxonomy written in RDF.
035: * The model is structured in (Sub)-Domains and associated Members connected via the
036: * relations subDomain and member. A domain is a closed entity, e.g. a company, a department, a role etc.,
037: * and a member is a concrete instance (e.g. a employee)of one or more domains in this taxonomy.
038: *
039: * This use case example implements some inference rules which allow deriving all members of a domain
040: * inclusive those members which belong to transitive subdomains. The RDF document is used as fact base
041: * for the domain taxonomy ("t-box model") and the associated instances/members ("a-box model").
042: *
043: *
044: * Inference rules:
045: *
046: * isMember(Member, Domain) <-- member(Member,Domain)
047: * isDerivedMember(Member,Domain) <-- isMember(Member,Domain)
048: * isDerivedMember(Member,Domain) <-- isSubDomain(Domain,SubDomain) and isDerivedMember(Member, SubDomain)
049: * isSubDomain(Domain1, Domain2) <-- subDomain(Domain1, Domain2)
050: *
051: * Facts from "domain.rdf":
052: *
053: * member(member_X,domain_Y)
054: * subDomain(domain_A,domain_B)
055: *
056: * Example Queries:
057: *
058: * isMember(member_X, domain_Y)? (direct member of domain Y)
059: * isDerivedMember(member_X, domain_Y) (derived member of trasitive domain Y)
060: *
061: *
062: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
063: * @version 1.1 <17 Dec 2004>
064: * @since 1.1
065: */
066:
067: public class DomainExample {
068:
069: // name spaces used
070: public static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
071: public static final String EXORG = "http://exampleOrg.net/domain_elements/1.0/";
072:
073: public static LogicFactorySupport lfs = new LogicFactorySupport();
074:
075: /**
076: * Tests the RDFClauseSet by building an instance from the test data imported from the
077: * url, iterating over it and comparing each clause with the clauses implicitely defined
078: * the arrays.
079: * @param testDataLocation the test data location (a relative path)
080: * @param expectedFacts the expected clauses
081: * @exception thrown if something goes wrong
082: */
083: private void runExample() throws Exception {
084: try {
085:
086: // add RDFClauset into Knowledgebase
087: KnowledgeBase kb = new AdvancedKnowledgeBase();
088:
089: URL url = this .getClass().getResource("domain.rdf");
090: RDFClauseSet clauseSet = new RDFClauseSet(url);
091: clauseSet.setPredicates(RDFUtils.findPredicates(url));
092: kb.add(clauseSet);
093:
094: // set up inference rules for transitive reasoning on the domains and the associated members
095:
096: Predicate isMember = new SimplePredicate("isMember",
097: new Class[] { Object.class, Object.class });
098: Predicate isDerivedMember = new SimplePredicate(
099: "isDerivedMember", new Class[] { Object.class,
100: Object.class });
101: Predicate isSubDomain = new SimplePredicate("isSubDomain",
102: new Class[] { Object.class, Object.class });
103: Predicate member = RDFPredicateRegistry
104: .findOrCreatePredicate(EXORG, "member", false);
105: Predicate subDomain = RDFPredicateRegistry
106: .findOrCreatePredicate(EXORG, "subDomain", false);
107:
108: // isMember(Member, Domain) <-- member(Member,Domain)
109: org.mandarax.kernel.Rule rule1 = lfs.rule(lfs.prereq(
110: member, lfs.variable("Member", Object.class), lfs
111: .variable("Domain", Object.class)), lfs
112: .fact(isMember, lfs
113: .variable("Member", Object.class), lfs
114: .variable("Domain", Object.class)));
115: kb.add(rule1);
116:
117: // isDerivedMember(Member,Domain) <-- isMember(Member,Domain)
118: org.mandarax.kernel.Rule rule2 = lfs.rule(lfs.prereq(
119: isMember, lfs.variable("Member", Object.class), lfs
120: .variable("Domain", Object.class)), lfs
121: .fact(isDerivedMember, lfs.variable("Member",
122: Object.class), lfs.variable("Domain",
123: Object.class)));
124: kb.add(rule2);
125:
126: // isDerivedMember(Member,Domain) <-- isSubDomain(Domain,SubDomain) and isDerivedMember(Member, SubDomain)
127: org.mandarax.kernel.Rule rule3 = lfs.rule(lfs.prereq(
128: isSubDomain, lfs.variable("Domain", Object.class),
129: lfs.variable("SubDomain", Object.class)), lfs
130: .prereq(isDerivedMember, lfs.variable("Member",
131: Object.class), lfs.variable("SubDomain",
132: Object.class)), lfs.fact(isDerivedMember,
133: lfs.variable("Member", Object.class), lfs.variable(
134: "Domain", Object.class)));
135: kb.add(rule3);
136:
137: // isSubDomain(Domain1, Domain2) <-- subDomain(Domain1, Domain2)
138: org.mandarax.kernel.Rule rule4 = lfs.rule(lfs.prereq(
139: subDomain, lfs.variable("Domain1", Object.class),
140: lfs.variable("Domain2", Object.class)), lfs.fact(
141: isSubDomain, lfs.variable("Domain1", Object.class),
142: lfs.variable("Domain2", Object.class)));
143: kb.add(rule4);
144:
145: // query knowledgebase
146:
147: // isDerivedMember(Member,Domain)?
148: Query query = lfs.query(lfs.fact(isDerivedMember, lfs
149: .variable("Member", Object.class), lfs.variable(
150: "Domain", Object.class)), "query");
151:
152: InferenceEngine ie = new ResolutionInferenceEngine2();
153:
154: // issue query and compare results
155: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
156: InferenceEngine.BUBBLE_EXCEPTIONS);
157:
158: System.out
159: .println("member is member of domain:");
160: System.out
161: .println("--------------------------------------------------------------------------------");
162: System.out.println();
163:
164: while (rs.next()) {
165: Object memb = rs.getResult(Object.class, "Member");
166: Object dom = rs.getResult(Object.class, "Domain");
167: System.out.println(memb + " is member of " + dom);
168: }
169:
170: System.out
171: .println("\r\n==> Domain http://exampleOrg.net/Domain_D.htm has no derived member (see domain.rdf)");
172:
173: } catch (Exception e) {
174: System.err.println(e);
175: }
176:
177: }
178:
179: public static void main(String[] args) {
180:
181: DomainExample de = new DomainExample();
182: try {
183: de.runExample();
184: } catch (Exception x) {
185: }
186: }
187: }
|