001: /**
002: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005: * Unless required by applicable law or agreed to in writing, software distributed under the
006: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
007: * either express or implied. See the License for the specific language governing permissions
008: * and limitations under the License.
009: */package test.nz.org.take.compiler.scenario2;
010:
011: import java.io.StringReader;
012: import java.util.ArrayList;
013: import java.util.List;
014: import nz.org.take.KnowledgeBase;
015: import nz.org.take.KnowledgeSource;
016: import nz.org.take.TakeException;
017: import nz.org.take.nscript.ScriptException;
018: import nz.org.take.nscript.ScriptKnowledgeSource;
019:
020: /**
021: * Script to generate a memory KB.
022: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
023: */
024:
025: public class GenerateKB implements KnowledgeSource {
026: public static final String ROOT = "r";
027: public static final int DEPTH = 3;
028:
029: /**
030: * Generate the sources for the example.
031: * @param args
032: */
033: public KnowledgeBase getKnowledgeBase() throws TakeException {
034: // generate kb
035: KnowledgeBase kb = null;
036: try {
037: ScriptKnowledgeSource KSrc = new ScriptKnowledgeSource(
038: new StringReader(buildScript()));
039: kb = KSrc.getKnowledgeBase();
040: } catch (ScriptException e) {
041: e.printStackTrace();
042: }
043: return kb;
044: }
045:
046: /**
047: * Generate the script.
048: */
049: private String buildScript() {
050: StringBuffer b = new StringBuffer();
051:
052: appendLine(b, "// script for test scenario 1");
053: appendLine(b, "@@dc:date=30/02/2007");
054: appendLine(b, "@@dc:creator=author jens dietrich");
055:
056: // variables
057: appendLine(b, "var java.lang.String descendant,ancestor,x");
058:
059: // queries
060: appendLine(b, "@take.compilerhint.slots=person1,person2");
061: appendLine(b, "@take.compilerhint.method=isAncestor");
062: appendLine(b, "@take.compilerhint.class=AncestorRelationship");
063: appendLine(b, "query isAncestor[in,in]");
064:
065: // rules
066: appendLine(b,
067: "rule1: if isFather[descendant,ancestor] then isAncestor[descendant,ancestor]");
068: appendLine(
069: b,
070: "rule2: if isAncestor[x,ancestor] and isFather[descendant,x] then isAncestor[descendant,ancestor]");
071:
072: // facts
073: List<List<String>> generations = new ArrayList<List<String>>();
074: int counter = 0;
075: for (int i = 0; i < DEPTH; i++) {
076: List<String> previousGeneration = null;
077: if (i == 0) {
078: previousGeneration = new ArrayList<String>();
079: previousGeneration.add(ROOT);
080: generations.add(previousGeneration);
081: } else
082: previousGeneration = generations
083: .get(generations.size() - 1);
084:
085: List<String> newGeneration = new ArrayList<String>();
086: for (String ancestor : previousGeneration) {
087: for (int j = 0; j < 2; j++) {
088: counter = counter + 1;
089: String descendant = ancestor + j;
090: newGeneration.add(descendant);
091: appendLine(b, "fact" + counter, ": ",
092: "isFather[\"", descendant, "\",\"",
093: ancestor, "\"]");
094: }
095: }
096: generations.add(newGeneration);
097:
098: }
099:
100: return b.toString();
101: }
102:
103: private void appendLine(StringBuffer b, String... strings) {
104: for (String s : strings) {
105: b.append(s);
106: System.out.print(s);
107: }
108: b.append('\n');
109: System.out.println();
110: }
111:
112: }
|