├── base_current_version
├── lojix_current_version
├── current_version
├── l2
├── lojix.name
└── src
│ └── main
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ └── l2
│ ├── PrintingTable.java
│ ├── L2FileMachine.java
│ ├── L2CallPoint.java
│ ├── L2Sentence.java
│ ├── L2Machine.java
│ ├── L2JavaInterpreter.java
│ ├── L2NativeInterpreter.java
│ └── PositionPrinter.java
├── aima-native
├── lojix.name
├── linux-llvm
│ ├── lojix.name
│ └── src
│ │ └── unittests
│ │ └── com
│ │ └── thesett
│ │ └── aima
│ │ └── logic
│ │ └── fol
│ │ └── l2
│ │ └── L2ResolvingNativeMachineLLVMTest.java
├── linux
│ └── src
│ │ └── unittests
│ │ └── com
│ │ └── thesett
│ │ └── aima
│ │ └── logic
│ │ └── fol
│ │ ├── l1
│ │ └── L1UnifyingNativeMachineLinuxTest.java
│ │ ├── l2
│ │ └── L2ResolvingNativeMachineLinuxTest.java
│ │ ├── l3
│ │ └── L3ResolvingNativeMachineLinuxTest.java
│ │ └── l0
│ │ └── L0UnifyingNativeMachineLinuxTest.java
├── src
│ └── c
│ │ ├── trace.h
│ │ └── trace.c
└── win32
│ └── src
│ └── unittests
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ ├── l0
│ └── L0UnifyingNativeMachineWindowsTest.java
│ └── l1
│ └── L1UnifyingNativeMachineWindowsTest.java
├── .gitignore
├── lib
└── jacocoagent.jar
├── etc
├── jalopy_config
│ └── settings.dat
├── jacoco
│ ├── asm-debug-all-5.0.1.jar
│ ├── org.jacoco.ant-0.7.2.201409121644.jar
│ ├── org.jacoco.agent-0.7.2.201409121644.jar
│ ├── org.jacoco.core-0.7.2.201409121644.jar
│ ├── org.jacoco.report-0.7.2.201409121644.jar
│ └── build.xml
├── site_command_line.txt
├── logging.properties
├── mylog.properties
├── logging_to_log4j.properties
├── coding_standards_suppressions.xml
├── findbugs-exclude.xml
├── dashboard-config.xml
├── license
│ └── license.txt
├── log4j.xml
└── project.properties
├── no_jalopy
├── restore_jalopy
├── wam
└── src
│ ├── prolog
│ └── wam_builtins.pl
│ └── main
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ └── wam
│ ├── optimizer
│ ├── Optimizer.java
│ └── StateMachine.java
│ ├── machine
│ ├── WAMCodeView.java
│ ├── WAMResolvingMachineDPIMonitor.java
│ ├── WAMResolvingMachineDPI.java
│ ├── WAMInternalRegisters.java
│ └── WAMMemoryLayout.java
│ ├── compiler
│ ├── WAMCallPoint.java
│ ├── WAMOptimizeableListing.java
│ ├── SymbolTableKeys.java
│ ├── WAMReservedLabel.java
│ ├── WAMLabel.java
│ └── WAMCompiler.java
│ ├── printer
│ ├── WAMCompiledQueryPrintingVisitor.java
│ ├── WAMCompiledPredicatePrintingVisitor.java
│ ├── ByteCodePrinter.java
│ ├── UnoptimizedByteCodePrinter.java
│ ├── LabelPrinter.java
│ ├── UnoptimizedLabelPrinter.java
│ ├── BasePrinter.java
│ └── PositionPrinter.java
│ ├── builtins
│ ├── BuiltInFunctor.java
│ ├── BaseBuiltIn.java
│ ├── Fail.java
│ ├── Unifies.java
│ ├── implementation.txt
│ ├── True.java
│ ├── NonUnifies.java
│ └── BuiltIn.java
│ ├── debugger
│ └── RegisterModel.java
│ ├── indexing
│ ├── CodeBufferTable.java
│ └── IntIntOpenLinearTable.java
│ └── interpreter
│ └── WAMJavaInterpreter.java
├── change_version
├── base_change_version
├── lojix_change_version
├── trace_on
├── restore_log_statements
├── trace_off
├── strip_log_statements
├── l0
└── src
│ └── main
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ └── l0
│ ├── L0FileMachine.java
│ ├── L0BaseMachine.java
│ ├── L0CompiledTerm.java
│ ├── L0InstructionSet.java
│ ├── L0CompiledProgramFunctor.java
│ └── L0CompiledQueryFunctor.java
├── l1
└── src
│ └── main
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ └── l1
│ ├── L1CompiledTerm.java
│ ├── L1CallTableEntry.java
│ ├── L1CompiledProgramFunctor.java
│ ├── L1CompiledQueryFunctor.java
│ ├── L1FileMachine.java
│ ├── L1InstructionSet.java
│ └── L1Machine.java
├── l3
└── src
│ └── main
│ └── com
│ └── thesett
│ └── aima
│ └── logic
│ └── fol
│ └── l3
│ ├── PrintingTable.java
│ ├── L3CallPoint.java
│ ├── L3CompiledQueryPrintingVisitor.java
│ ├── L3CompiledPredicatePrintingVisitor.java
│ ├── L3ReservedLabel.java
│ ├── ByteCodePrinter.java
│ ├── LabelPrinter.java
│ ├── L3Label.java
│ ├── interpreter
│ └── L3JavaInterpreter.java
│ └── PositionPrinter.java
├── README.md
└── cli
├── pom.xml
└── src
└── main
└── com
└── thesett
└── aima
└── logic
└── fol
└── interpreter
└── InteractiveParser.java
/base_current_version:
--------------------------------------------------------------------------------
1 | 0.8.11
2 |
--------------------------------------------------------------------------------
/lojix_current_version:
--------------------------------------------------------------------------------
1 | 0.8.11
2 |
--------------------------------------------------------------------------------
/current_version:
--------------------------------------------------------------------------------
1 | 1.0.3-SNAPSHOT
2 |
--------------------------------------------------------------------------------
/l2/lojix.name:
--------------------------------------------------------------------------------
1 | lojix is the name of this project.
2 |
--------------------------------------------------------------------------------
/aima-native/lojix.name:
--------------------------------------------------------------------------------
1 | lojix is the name of this project.
2 |
--------------------------------------------------------------------------------
/aima-native/linux-llvm/lojix.name:
--------------------------------------------------------------------------------
1 | lojix is the name of this project.
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | *.iml
3 | *.iws
4 | *.ipr
5 | .idea
6 | code-coverage
7 |
--------------------------------------------------------------------------------
/lib/jacocoagent.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/lib/jacocoagent.jar
--------------------------------------------------------------------------------
/etc/jalopy_config/settings.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jalopy_config/settings.dat
--------------------------------------------------------------------------------
/etc/jacoco/asm-debug-all-5.0.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jacoco/asm-debug-all-5.0.1.jar
--------------------------------------------------------------------------------
/etc/jacoco/org.jacoco.ant-0.7.2.201409121644.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jacoco/org.jacoco.ant-0.7.2.201409121644.jar
--------------------------------------------------------------------------------
/etc/site_command_line.txt:
--------------------------------------------------------------------------------
1 | -Djava.awt.headless=true clean clover2:instrument clover2:aggregate site:site dashboard:dashboard site:deploy -Pclover,native
2 |
--------------------------------------------------------------------------------
/etc/jacoco/org.jacoco.agent-0.7.2.201409121644.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jacoco/org.jacoco.agent-0.7.2.201409121644.jar
--------------------------------------------------------------------------------
/etc/jacoco/org.jacoco.core-0.7.2.201409121644.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jacoco/org.jacoco.core-0.7.2.201409121644.jar
--------------------------------------------------------------------------------
/etc/jacoco/org.jacoco.report-0.7.2.201409121644.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rupertlssmith/hak_wambook/HEAD/etc/jacoco/org.jacoco.report-0.7.2.201409121644.jar
--------------------------------------------------------------------------------
/etc/logging.properties:
--------------------------------------------------------------------------------
1 | handlers=java.util.logging.ConsoleHandler
2 |
3 | .level=ALL
4 |
5 | java.util.logging.ConsoleHandler.level=ALL
6 | java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
7 |
--------------------------------------------------------------------------------
/no_jalopy:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name 'pom.xml' -exec sed -i -e '
3 | s///JALOPY_COMMENT_OUT_END -->/g
5 | ' {} \;
6 |
--------------------------------------------------------------------------------
/restore_jalopy:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name 'pom.xml' -exec sed -i -e '
3 | s//g
4 | s/JALOPY_COMMENT_OUT_END -->//g
5 | ' {} \;
6 |
--------------------------------------------------------------------------------
/etc/mylog.properties:
--------------------------------------------------------------------------------
1 | handlers=java.util.logging.ConsoleHandler
2 |
3 | .level=ALL
4 |
5 | java.util.logging.ConsoleHandler.level=ALL
6 | java.util.logging.ConsoleHandler.formatter=uk.co.thebadgerset.aima.logic.fol.l2.MyFormatter
7 |
--------------------------------------------------------------------------------
/etc/logging_to_log4j.properties:
--------------------------------------------------------------------------------
1 | handlers=com.thesett.util.log4j.LoggingToLog4JHandler
2 |
3 | .level=ALL
4 |
5 | java.util.logging.ConsoleHandler.level=ALL
6 | java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
7 |
--------------------------------------------------------------------------------
/etc/coding_standards_suppressions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/etc/findbugs-exclude.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/wam/src/prolog/wam_builtins.pl:
--------------------------------------------------------------------------------
1 | /* Builts-ins for prolog. */
2 | true.
3 |
4 | \+(X) :- call(X),!,fail.
5 | \+(X).
6 |
7 | =(X, X).
8 |
9 | \=(X, Y) :- \+(X = Y).
10 |
11 | not(X) :- call(X),!,fail.
12 | not(X).
13 |
14 | member(X, [X|_]).
15 | member(X, [_|Tail]) :- member(X, Tail).
16 |
17 | append([], List, List).
18 | append([H|Tail1], List2, [H|Tail3]) :- append(Tail1, List2, Tail3).
19 |
--------------------------------------------------------------------------------
/change_version:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | CV=`cat current_version`
3 | echo Current version is $CV
4 |
5 | if [ -z "$1" ] ; then
6 | echo New version must be specified.
7 | exit
8 | fi
9 |
10 | echo New version is $1
11 | SEDCMD="s/$CV<\/version>/$1<\/version>/"
12 | SEDCMDCV="s/$CV/$1/"
13 |
14 | find -L . -name 'pom.xml' | xargs sed -i $SEDCMD
15 | sed -i $SEDCMDCV current_version
16 |
--------------------------------------------------------------------------------
/base_change_version:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | CV=`cat base_current_version`
3 | echo Current version is $CV
4 |
5 | if [ -z "$1" ] ; then
6 | echo New version must be specified.
7 | exit
8 | fi
9 |
10 | echo New version is $1
11 | SEDCMD="s/$CV<\/version>/$1<\/version>/"
12 | SEDCMDCV="s/$CV/$1/"
13 |
14 | find -L . -name 'pom.xml' | xargs sed -i $SEDCMD
15 | sed -i $SEDCMDCV base_current_version
16 |
--------------------------------------------------------------------------------
/lojix_change_version:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | CV=`cat lojix_current_version`
3 | echo Current version is $CV
4 |
5 | if [ -z "$1" ] ; then
6 | echo New version must be specified.
7 | exit
8 | fi
9 |
10 | echo New version is $1
11 | SEDCMD="s/$CV<\/version>/$1<\/version>/"
12 | SEDCMDCV="s/$CV/$1/"
13 |
14 | find -L . -name 'pom.xml' | xargs sed -i $SEDCMD
15 | sed -i $SEDCMDCV lojix_current_version
16 |
--------------------------------------------------------------------------------
/trace_on:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name '*.java' -exec sed -i -e '
3 | s/\/\* private static final Logger trace = Logger\.getLogger(\(.*\)); \*\//private static final java.util.logging.Logger trace = java.util.logging.Logger\.getLogger(\1);/
4 | s/\/\*\(trace\.fine.*;\)\*\//\1/
5 | /trace\.fine/{
6 | N
7 | N
8 | s/\/\*\(trace\.fine.*;\)\*\//\1/
9 | }
10 | s/private static final boolean TRACE = false; \/\/log.isLoggable(Level.FINE)/private static final boolean TRACE = trace.isLoggable(java.util.logging.Level.FINE);/' {} \;
11 |
12 |
--------------------------------------------------------------------------------
/etc/dashboard-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/restore_log_statements:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name '*.java' -exec sed -i -e '
3 | s/\/\* private static final Logger log = Logger\.getLogger(\(.*\)); \*\//private static final java.util.logging.Logger log = java.util.logging.Logger\.getLogger(\1);/
4 | s/private static final boolean DEBUG = false; \/\/log.isLoggable(Level.FINE)/private static final boolean DEBUG = log.isLoggable(java.util.logging.Level.FINE);/
5 | s/\/\*\(log\.fine.*;\)\*\//\1/
6 | /log\.fine/{
7 | N
8 | s/\/\*\(log\.fine.*;\)\*\//\1/
9 | }
10 | s/\/\*\(log\.log.*;\)\*\//\1/
11 | /log\.log/{
12 | N
13 | s/\/\*\(log\.log.*;\)\*\//\1/
14 | }' {} \;
15 |
--------------------------------------------------------------------------------
/etc/license/license.txt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
--------------------------------------------------------------------------------
/trace_off:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name '*.java' -exec sed -i -e '
3 | s/private static final java\.util\.logging\.Logger trace = java\.util\.logging\.Logger\.getLogger(\(.*\));/\/* private static final Logger trace = Logger.getLogger(\1); *\//
4 | /private static final java\.util\.logging\.Logger trace =/{
5 | /\/\*/!{
6 | N
7 | s/private static final java\.util\.logging\.Logger trace =.* java\.util\.logging\.Logger\.getLogger(\(.*\));/\/* private static final Logger trace = Logger.getLogger(\1); *\//
8 | }
9 | }
10 | s/private static final boolean TRACE = trace.isLoggable(java.util.logging.Level.FINE);/private static final boolean TRACE = false; \/\/log.isLoggable(Level.FINE)/
11 | /\/\*/! s/\(trace\.fine.*;\)/\/*\1*\//
12 | /trace\.fine/{
13 | /\/\*/!{
14 | N
15 | N
16 | s/\(trace\.fine.*;\)/\/*\1*\//
17 | }
18 | }' {} \;
19 |
--------------------------------------------------------------------------------
/strip_log_statements:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | find . -name '*.java' -exec sed -i -e '
3 | s/private static final java\.util\.logging\.Logger log = java\.util\.logging\.Logger\.getLogger(\(.*\));/\/* private static final Logger log = Logger.getLogger(\1); *\//
4 | /private static final java\.util\.logging\.Logger log =/{
5 | /\/\*/!{
6 | N
7 | s/private static final java\.util\.logging\.Logger log =.* java\.util\.logging\.Logger\.getLogger(\(.*\));/\/* private static final Logger log = Logger.getLogger(\1); *\//
8 | }
9 | }
10 | s/private static final boolean DEBUG = log.isLoggable(java.util.logging.Level.FINE);/private static final boolean DEBUG = false; \/\/log.isLoggable(Level.FINE)/
11 | /\/\*/! s/\(log\.fine.*;\)/\/*\1*\//
12 | /log\.fine/{
13 | /\/\*/!{
14 | N
15 | s/\(log\.fine.*;\)/\/*\1*\//
16 | }
17 | }
18 | /\/\*/! s/\(log\.log.*;\)/\/*\1*\//
19 | /log\.log/{
20 | /\/\*/!{
21 | N
22 | s/\(log\.log.*;\)/\/*\1*\//
23 | }
24 | }' {} \;
25 |
--------------------------------------------------------------------------------
/aima-native/linux/src/unittests/com/thesett/aima/logic/fol/l1/L1UnifyingNativeMachineLinuxTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | */
23 | public class L1UnifyingNativeMachineLinuxTest extends TestCase
24 | {
25 | public L1UnifyingNativeMachineLinuxTest(String name)
26 | {
27 | super(name);
28 | }
29 |
30 | public static Test suite() throws Exception
31 | {
32 | return L1UnifyingNativeMachineTestBase.suite();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/aima-native/linux/src/unittests/com/thesett/aima/logic/fol/l2/L2ResolvingNativeMachineLinuxTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | */
23 | public class L2ResolvingNativeMachineLinuxTest extends TestCase
24 | {
25 | public L2ResolvingNativeMachineLinuxTest(String name)
26 | {
27 | super(name);
28 | }
29 |
30 | public static Test suite() throws Exception
31 | {
32 | return L2ResolvingNativeMachineTestBase.suite();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/aima-native/linux/src/unittests/com/thesett/aima/logic/fol/l3/L3ResolvingNativeMachineLinuxTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | */
23 | public class L3ResolvingNativeMachineLinuxTest extends TestCase
24 | {
25 | public L3ResolvingNativeMachineLinuxTest(String name)
26 | {
27 | super(name);
28 | }
29 |
30 | public static Test suite() throws Exception
31 | {
32 | return L3ResolvingNativeMachineTestBase.suite();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0FileMachine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | /**
19 | * L0FileMachine is an {@link VariableAndFunctorInterner} that can load and store compiled L0 byte code to files.
20 | *
21 | *
CRC Card
22 | * | Responsbilities | Collaborations
23 | * |
|---|
| Load byte code from a file.
24 | * |
| Store byte code to a file.
25 | * |
26 | *
27 | * @author Rupert Smith
28 | * @todo Provide a way for this machine to pass the byte code to an execution machine. For example, by wrapping one.
29 | */
30 | public class L0FileMachine extends L0BaseMachine
31 | {
32 | }
33 |
--------------------------------------------------------------------------------
/aima-native/linux/src/unittests/com/thesett/aima/logic/fol/l0/L0UnifyingNativeMachineLinuxTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | *
23 | *
24 | * CRC Card
25 | *
26 | * | Responsibilities
27 | * | Collaborations
28 | * |
29 | */
30 | public class L0UnifyingNativeMachineLinuxTest extends TestCase
31 | {
32 | public L0UnifyingNativeMachineLinuxTest(String name)
33 | {
34 | super(name);
35 | }
36 |
37 | public static Test suite() throws Exception
38 | {
39 | return L0UnifyingNativeMachineTestBase.suite();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/aima-native/src/c/trace.h:
--------------------------------------------------------------------------------
1 | /* Defines some methods for pretty printing byte code instruction traces. */
2 | #ifndef _TRACE_H
3 | #define _TRACE_H
4 |
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 | /* A printf function that outputs to stderr instead of stdout. */
10 | void stderrPrintf(__const char *__restrict __format, ...);
11 |
12 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
13 | void traceIt(char* buffer);
14 |
15 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
16 | void trace0(char* mnemonic, int ip);
17 |
18 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
19 | void trace1(char* mnemonic, int ip, int reg1);
20 |
21 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
22 | void trace2(char* mnemonic, int ip, int reg1, signed char mode, int reg2, int ep);
23 |
24 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
25 | void traceFn0(char* mnemonic, int ip, int fn);
26 |
27 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
28 | void traceFn1(char* mnemonic, int ip, int reg1, int fn);
29 |
30 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
31 | void traceConst(char* mnemonic, int ip, int val);
32 |
33 | #ifdef __cplusplus
34 | }
35 | #endif
36 |
37 | #endif /* _TRACE_H */
38 |
--------------------------------------------------------------------------------
/aima-native/win32/src/unittests/com/thesett/aima/logic/fol/l0/L0UnifyingNativeMachineWindowsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | *
23 | *
24 | * CRC Card
25 | *
26 | * | Responsibilities
27 | * | Collaborations
28 | * |
29 | */
30 | public class L0UnifyingNativeMachineWindowsTest extends TestCase
31 | {
32 | public L0UnifyingNativeMachineWindowsTest(String name)
33 | {
34 | super(name);
35 | }
36 |
37 | public static Test suite() throws Exception
38 | {
39 | return L0UnifyingNativeMachineTestBase.suite();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/aima-native/win32/src/unittests/com/thesett/aima/logic/fol/l1/L1UnifyingNativeMachineWindowsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 |
21 | /**
22 | *
23 | *
24 | * CRC Card
25 | *
26 | * | Responsibilities
27 | * | Collaborations
28 | * |
29 | */
30 | public class L1UnifyingNativeMachineWindowsTest extends TestCase
31 | {
32 | public L1UnifyingNativeMachineWindowsTest(String name)
33 | {
34 | super(name);
35 | }
36 |
37 | public static Test suite() throws Exception
38 | {
39 | return L1UnifyingNativeMachineTestBase.suite();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/etc/jacoco/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0BaseMachine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import com.thesett.aima.logic.fol.VariableAndFunctorInternerImpl;
19 |
20 | /**
21 | * L0BaseMachine provides basic services common to all L0 machines.
22 | *
23 | *
CRC Card
24 | * | Responsibilities | Collaborations
25 | * |
|---|
| Provide symbol table for functors names.
26 | * |
| Provide symbol table for variable names.
27 | * |
28 | *
29 | * @author Rupert Smith
30 | */
31 | public abstract class L0BaseMachine extends VariableAndFunctorInternerImpl
32 | {
33 | /** Creates the base machine, providing variable and functor symbol tables. */
34 | protected L0BaseMachine()
35 | {
36 | super("L0_Variable_Namespace", "L0_Functor_Namespace");
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0CompiledTerm.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import com.thesett.aima.logic.fol.Term;
19 |
20 | /**
21 | * A compiled term is a handle onto a compiled down to binary code term. Compiled terms are not Java code and may be
22 | * executed outside of the JVM, but the Java retains a handle on them and provides sufficient wrapping around them that
23 | * they can be made to look as if they are a transparent abstract syntax tree within the JVM.
24 | *
25 | *
CRC Card
26 | * | Responsibilities
27 | * |
|---|
| Decompile/decode a binary term to restore its abstract syntax tree.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public interface L0CompiledTerm extends Term
33 | {
34 | /** Decompiles the term. */
35 | void decompile();
36 | }
37 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1CompiledTerm.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import com.thesett.aima.logic.fol.Term;
19 |
20 | /**
21 | * A compiled term is a handle onto a compiled down to binary code term. Compiled terms are not Java code and may be
22 | * executed outside of the JVM, but the Java retains a handle on them and provides sufficient wrapping around them that
23 | * they can be made to look as if they are a transparent abstract syntax tree within the JVM.
24 | *
25 | *
CRC Card
26 | * | Responsibilities
27 | * |
|---|
| Decompile/decode a binary term to restore its abstract syntax tree.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public interface L1CompiledTerm extends Term
33 | {
34 | /** Decompiles the term. */
35 | void decompile();
36 | }
37 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/optimizer/Optimizer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.optimizer;
17 |
18 | import com.thesett.aima.logic.fol.wam.compiler.WAMOptimizeableListing;
19 |
20 | /**
21 | * An optimized is a function over {@link WAMOptimizeableListing} that produces an optimized version of the instruction
22 | * listing.
23 | *
24 | *
CRC Card
25 | * | Responsibilities | Collaborations
26 | * |
|---|
| Apply optimizations to an instruction listing.
27 | * |
28 | *
29 | * @author Rupert Smith
30 | */
31 | public interface Optimizer
32 | {
33 | /**
34 | * Applies optimizations to an instruction listing.
35 | *
36 | * @param listing The instruction listing.
37 | * @param The type of the instruction listing.
38 | *
39 | * @return An optimized instruction listing.
40 | */
41 | T apply(WAMOptimizeableListing listing);
42 | }
43 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/optimizer/StateMachine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.optimizer;
17 |
18 | /**
19 | * StateMachine is used to implement a FSMD, that is driven by a {@link Matcher}.
20 | *
21 | *
CRC Card
22 | * | Responsibilities | Collaborations
23 | * |
|---|
| Accept the matcher that drives this state machine. |
24 | * | Accept input from the matcher. |
25 | * | Accept end of input from the matcher. |
26 | *
27 | *
28 | * @author Rupert Smith
29 | */
30 | public interface StateMachine
31 | {
32 | /**
33 | * Sets up the matcher that drives this state machine.
34 | *
35 | * @param matcher The matcher that drives this state machine.
36 | */
37 | void setMatcher(Matcher matcher);
38 |
39 | /**
40 | * Accepts the next input from the matcher.
41 | *
42 | * @param next The next input data item.
43 | */
44 | void apply(S next);
45 |
46 | /** Accepts end of input notification from the matcher. */
47 | void end();
48 | }
49 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/PrintingTable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | /**
19 | * PrintingTable collects information about the row count, and row and column sizes, in order to print information in a
20 | * table format.
21 | *
22 | *
CRC Card
23 | * | Responsibilities
24 | * |
|---|
| Collect row count, row and column size stats.
25 | * |
26 | *
27 | * @author Rupert Smith
28 | */
29 | public interface PrintingTable
30 | {
31 | /**
32 | * Updates the maximum row count.
33 | *
34 | * @param row The new maximum row count, accepted if larger than the previous value.
35 | */
36 | void setMaxRowCount(int row);
37 |
38 | /**
39 | * Updates the maximum row height.
40 | *
41 | * @param row The row to update stats for.
42 | * @param height The new maximum row height, accepted if larger than the previous value.
43 | */
44 | void setMaxRowHeight(int row, int height);
45 |
46 | /**
47 | * Updates the maximum column width.
48 | *
49 | * @param column The column to update stats for.
50 | * @param width The new maximum column width, accepted if larger than the previous value.
51 | */
52 | void setMaxColumnWidth(int column, int width);
53 | }
54 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/PrintingTable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | /**
19 | * PrintingTable collects information about the row count, and row and column sizes, in order to print information in a
20 | * table format.
21 | *
22 | *
CRC Card
23 | * | Responsibilities
24 | * |
|---|
| Collect row count, row and column size stats.
25 | * |
26 | *
27 | * @author Rupert Smith
28 | */
29 | public interface PrintingTable
30 | {
31 | /**
32 | * Updates the maximum row count.
33 | *
34 | * @param row The new maximum row count, accepted if larger than the previous value.
35 | */
36 | void setMaxRowCount(int row);
37 |
38 | /**
39 | * Updates the maximum row height.
40 | *
41 | * @param row The row to update stats for.
42 | * @param height The new maximum row height, accepted if larger than the previous value.
43 | */
44 | void setMaxRowHeight(int row, int height);
45 |
46 | /**
47 | * Updates the maximum column width.
48 | *
49 | * @param column The column to update stats for.
50 | * @param width The new maximum column width, accepted if larger than the previous value.
51 | */
52 | void setMaxColumnWidth(int column, int width);
53 | }
54 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/machine/WAMCodeView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.machine;
17 |
18 | import java.nio.ByteBuffer;
19 |
20 | /**
21 | * WAMCodeView provides a read-only view onto the code buffer of a machine.
22 | *
23 | *
CRC Card
24 | * | Responsibilities
25 | * |
|---|
| Allow portions of the code buffer to be examined. |
26 | * | Provide reverse look-ups from addresses to interned names for locations. |
27 | *
28 | *
29 | * @author Rupert Smith
30 | */
31 | public interface WAMCodeView
32 | {
33 | /**
34 | * Provides read access to the machines bytecode buffer.
35 | *
36 | * @param start The start offset within the buffer to read.
37 | * @param length Then length within the buffer to read.
38 | *
39 | * @return The requested portion of the machines bytecode buffer.
40 | */
41 | ByteBuffer getCodeBuffer(int start, int length);
42 |
43 | /**
44 | * Attempts to find a label or functor name for a given address within the code area of the machine.
45 | *
46 | * @param address The address to look up.
47 | *
48 | * @return The label or functor name matching the address, or null if none is set at that address.
49 | */
50 | Integer getNameForAddress(int address);
51 | }
52 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2FileMachine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import com.thesett.aima.logic.fol.LinkageException;
19 | import com.thesett.common.error.NotImplementedException;
20 |
21 | /**
22 | * L2FileMachine is an {@link L2Machine} that can load and store compiled L2 byte code to files.
23 | *
24 | *
CRC Card
25 | * | Responsibilities | Collaborations
26 | * |
|---|
| Load byte code from a file.
27 | * |
| Store byte code to a file.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | * @todo Provide a way for this machine to pass the byte code to an execution machine. For example, by wrapping one.
32 | */
33 | public class L2FileMachine extends L2BaseMachine
34 | {
35 | /** {@inheritDoc} */
36 | public void emmitCode(L2CompiledClause clause) throws LinkageException
37 | {
38 | throw new NotImplementedException();
39 | }
40 |
41 | /**
42 | * Extracts the raw byte code from the machine for a given call table entry.
43 | *
44 | * @param callPoint The call table entry giving the location and length of the code.
45 | *
46 | * @return The byte code at the specified location.
47 | */
48 | public byte[] retrieveCode(L2CallPoint callPoint)
49 | {
50 | throw new NotImplementedException();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2CallPoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | /**
19 | * L2CallPoint holds the fields recorded against a predicate name in the call table of an {@link L2Machine}. These
20 | * describe the predicates entry point, and the length of its byte code.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Record the size and entry point of the byte code for a predicate.
25 | * |
| Record the predicates interned name.
26 | * |
27 | *
28 | * @author Rupert Smith
29 | */
30 | public class L2CallPoint
31 | {
32 | /** The predicates entry point. */
33 | public int entryPoint;
34 |
35 | /** The predicates length. */
36 | public int length;
37 |
38 | /** The predicates interned name. */
39 | public int name;
40 |
41 | /**
42 | * Creates a call table entry for the code with the specified entry address and length.
43 | *
44 | * @param entryPoint The entry address of the code.
45 | * @param length The length of the program at the address.
46 | * @param functorName The functors interned name.
47 | */
48 | public L2CallPoint(int entryPoint, int length, int functorName)
49 | {
50 | this.entryPoint = entryPoint;
51 | this.length = length;
52 | this.name = functorName;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/L3CallPoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | /**
19 | * L3CallPoint holds the fields recorded against a predicate name in the call table of an {@link L3Machine}. These
20 | * describe the predicates entry point, and the length of its byte code.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Record the size and entry point of the byte code for a predicate.
25 | * |
| Record the predicates interned name.
26 | * |
27 | *
28 | * @author Rupert Smith
29 | */
30 | public class L3CallPoint
31 | {
32 | /** The predicates entry point. */
33 | public int entryPoint;
34 |
35 | /** The predicates length. */
36 | public int length;
37 |
38 | /** The predicates interned name. */
39 | public int name;
40 |
41 | /**
42 | * Creates a call table entry for the code with the specified entry address and length.
43 | *
44 | * @param entryPoint The entry address of the code.
45 | * @param length The length of the program at the address.
46 | * @param functorName The functors interned name.
47 | */
48 | public L3CallPoint(int entryPoint, int length, int functorName)
49 | {
50 | this.entryPoint = entryPoint;
51 | this.length = length;
52 | this.name = functorName;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1CallTableEntry.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | /**
19 | * L1CallTableEntry holds the fields recorded against a predicate name in the call table of an {@link L1Machine}. These
20 | * describe the predicates entry point, and the length of its byte code.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Record the size and entry point of the byte code for a predicate.
25 | * |
| Record the predicates interned name.
26 | * |
27 | *
28 | * @author Rupert Smith
29 | */
30 | public class L1CallTableEntry
31 | {
32 | /** The predicates entry point. */
33 | public int entryPoint;
34 |
35 | /** The predicates length. */
36 | public int length;
37 |
38 | /** The predicates interned name. */
39 | public int name;
40 |
41 | /**
42 | * Creates a call table entry for the code with the specified entry address and length.
43 | *
44 | * @param entryPoint The entry address of the code.
45 | * @param length The length of the program at the address.
46 | * @param functorName The functors interned name.
47 | */
48 | public L1CallTableEntry(int entryPoint, int length, int functorName)
49 | {
50 | this.entryPoint = entryPoint;
51 | this.length = length;
52 | this.name = functorName;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/WAMCallPoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | import com.thesett.aima.logic.fol.wam.machine.WAMMachine;
19 |
20 | /**
21 | * WAMCallPoint holds the fields recorded against a predicate name in the call table of an {@link WAMMachine}. These
22 | * describe the predicates entry point, and the length of its byte code.
23 | *
24 | *
CRC Card
25 | * | Responsibilities | Collaborations
26 | * |
|---|
| Record the size and entry point of the byte code for a predicate.
27 | * |
| Record the predicates interned name.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public class WAMCallPoint
33 | {
34 | /** The predicates entry point. */
35 | public int entryPoint;
36 |
37 | /** The predicates length. */
38 | public int length;
39 |
40 | /** The predicates interned name. */
41 | public int name;
42 |
43 | /**
44 | * Creates a call table entry for the code with the specified entry address and length.
45 | *
46 | * @param entryPoint The entry address of the code.
47 | * @param length The length of the program at the address.
48 | * @param functorName The functors interned name.
49 | */
50 | public WAMCallPoint(int entryPoint, int length, int functorName)
51 | {
52 | this.entryPoint = entryPoint;
53 | this.length = length;
54 | this.name = functorName;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0InstructionSet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | /**
19 | * Defines the L0 virtual machine instruction set as constants.
20 | *
21 | *
CRC Card
22 | * | Responsibilities | Collaborations
23 | * |
|---|
| Provide the translation of the L0 instruction set down to bytes.
24 | * |
25 | *
26 | * @author Rupert Smith
27 | */
28 | public class L0InstructionSet
29 | {
30 | // === Defines the machine instruction types.
31 |
32 | /** Instruction to write out a struc onto the heap. */
33 | public static final byte PUT_STRUC = 0x01;
34 |
35 | /** The instruction to set a register as a variable. */
36 | public static final byte SET_VAR = 0x02;
37 |
38 | /** The instruction to set a register to a heap location. */
39 | public static final byte SET_VAL = 0x03;
40 |
41 | /** The instruction to compare a register to a structure on the heap. */
42 | public static final byte GET_STRUC = 0x04;
43 |
44 | /** The instruction to unify a register with a variable. */
45 | public static final byte UNIFY_VAR = 0x05;
46 |
47 | /** The instruction to unify a register with a location on the heap. */
48 | public static final byte UNIFY_VAL = 0x06;
49 |
50 | // === Defines the heap cell marker types.
51 |
52 | /** Indicates a register on the heap. */
53 | public static final byte REF = 0x01;
54 |
55 | /** Indicates the begining of a struc on the heap. */
56 | public static final byte STR = 0x02;
57 | }
58 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/WAMOptimizeableListing.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | import java.util.List;
19 |
20 | import com.thesett.common.util.SizeableList;
21 |
22 | /**
23 | * WAMOptimizeableListing provides an instruction listing, and allows that instruction listing to be replaced with a
24 | * more optimized version.
25 | *
26 | *
CRC Card
27 | * | Responsibilities | Collaborations
28 | * |
|---|
| List the WAM instructions. |
29 | * | Allow the instructions to be replaced with a more optimized instruction listing. |
30 | * | List the original unoptimized instructions. |
31 | *
32 | *
33 | * @author Rupert Smith
34 | */
35 | public interface WAMOptimizeableListing
36 | {
37 | /**
38 | * Provides the compiled byte code instructions as an unmodifiable list.
39 | *
40 | * @return A list of the byte code instructions for this query.
41 | */
42 | List getInstructions();
43 |
44 | /**
45 | * Replaces the instruction listing with a more optimized listing.
46 | *
47 | * @param instructions An optimized instruction listing.
48 | */
49 | void setOptimizedInstructions(SizeableList instructions);
50 |
51 | /**
52 | * Provides the original unoptimized instruction listing, after the optimization replacement.
53 | *
54 | * @return The original unoptimized instruction listing.
55 | */
56 | List getUnoptimizedInstructions();
57 | }
58 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/L3CompiledQueryPrintingVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 | import com.thesett.common.util.doublemaps.SymbolTable;
21 |
22 | /**
23 | * Pretty printer for compiled queries.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Pretty print compiled queries with internal information about the compilation.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public class L3CompiledQueryPrintingVisitor extends L3CompiledTermsPrintingVisitor
33 | {
34 | /**
35 | * Creates a pretty printing visitor for clauses being compiled in L3.
36 | *
37 | * @param interner The symbol name table.
38 | * @param symbolTable The symbol table for the compilation.
39 | * @param result A string buffer to place the results in.
40 | */
41 | public L3CompiledQueryPrintingVisitor(VariableAndFunctorInterner interner,
42 | SymbolTable symbolTable, StringBuffer result)
43 | {
44 | super(interner, symbolTable, result);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public void visit(Clause clause)
49 | {
50 | if (traverser.isEnteringContext())
51 | {
52 | initializePrinters();
53 | }
54 | else if (traverser.isLeavingContext())
55 | {
56 | printTable();
57 | }
58 |
59 | super.visit(clause);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/WAMCompiledQueryPrintingVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 | import com.thesett.common.util.doublemaps.SymbolTable;
21 |
22 | /**
23 | * Pretty printer for compiled queries.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Pretty print compiled queries with internal information about the compilation.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public class WAMCompiledQueryPrintingVisitor extends WAMCompiledTermsPrintingVisitor
33 | {
34 | /**
35 | * Creates a pretty printing visitor for clauses being compiled in WAM.
36 | *
37 | * @param interner The symbol name table.
38 | * @param symbolTable The symbol table for the compilation.
39 | * @param result A string buffer to place the results in.
40 | */
41 | public WAMCompiledQueryPrintingVisitor(VariableAndFunctorInterner interner,
42 | SymbolTable symbolTable, StringBuffer result)
43 | {
44 | super(interner, symbolTable, result);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public void visit(Clause clause)
49 | {
50 | if (traverser.isEnteringContext())
51 | {
52 | initializePrinters();
53 | }
54 | else if (traverser.isLeavingContext())
55 | {
56 | printTable();
57 | }
58 |
59 | super.visit(clause);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/L3CompiledPredicatePrintingVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.Predicate;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 | import com.thesett.common.util.doublemaps.SymbolTable;
21 |
22 | /**
23 | * Pretty printer for compiled predicates.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Pretty print compiled predicates with internal information about the compilation.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public class L3CompiledPredicatePrintingVisitor extends L3CompiledTermsPrintingVisitor
33 | {
34 | /**
35 | * Creates a pretty printing visitor for clauses being compiled in L3.
36 | *
37 | * @param interner The symbol name table.
38 | * @param symbolTable The symbol table for the compilation.
39 | * @param result A string buffer to place the results in.
40 | */
41 | public L3CompiledPredicatePrintingVisitor(VariableAndFunctorInterner interner,
42 | SymbolTable symbolTable, StringBuffer result)
43 | {
44 | super(interner, symbolTable, result);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public void visit(Predicate predicate)
49 | {
50 | if (traverser.isEnteringContext())
51 | {
52 | initializePrinters();
53 | }
54 | else if (traverser.isLeavingContext())
55 | {
56 | printTable();
57 | }
58 |
59 | super.visit(predicate);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/WAMCompiledPredicatePrintingVisitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Predicate;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 | import com.thesett.common.util.doublemaps.SymbolTable;
21 |
22 | /**
23 | * Pretty printer for compiled predicates.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Pretty print compiled predicates with internal information about the compilation.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public class WAMCompiledPredicatePrintingVisitor extends WAMCompiledTermsPrintingVisitor
33 | {
34 | /**
35 | * Creates a pretty printing visitor for clauses being compiled in WAM.
36 | *
37 | * @param interner The symbol name table.
38 | * @param symbolTable The symbol table for the compilation.
39 | * @param result A string buffer to place the results in.
40 | */
41 | public WAMCompiledPredicatePrintingVisitor(VariableAndFunctorInterner interner,
42 | SymbolTable symbolTable, StringBuffer result)
43 | {
44 | super(interner, symbolTable, result);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public void visit(Predicate predicate)
49 | {
50 | if (traverser.isEnteringContext())
51 | {
52 | initializePrinters();
53 | }
54 | else if (traverser.isLeavingContext())
55 | {
56 | printTable();
57 | }
58 |
59 | super.visit(predicate);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/aima-native/src/c/trace.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "trace.h"
3 |
4 | /* Defines the addressing modes. */
5 | #define REG_ADDR 0x01
6 | #define STACK_ADDR 0x02
7 |
8 | /* A buffer to hold formatted trace statements in. */
9 | char traceBuffer[250];
10 |
11 | /* A printf function that outputs to stderr instead of stdout. */
12 | void stderrPrintf(__const char *__restrict __format, ...)
13 | {
14 | fprintf(stderr, __format);
15 | }
16 |
17 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
18 | void traceIt(char* buffer)
19 | {
20 | fprintf(stderr, "%s\n", buffer);
21 | }
22 |
23 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
24 | void trace0(char* mnemonic, int ip)
25 | {
26 | sprintf(traceBuffer, "%i: %s", ip, mnemonic);
27 | traceIt(traceBuffer);
28 | }
29 |
30 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
31 | void trace1(char* mnemonic, int ip, int reg1)
32 | {
33 | sprintf(traceBuffer, "%i: %s X%i", ip, mnemonic, reg1);
34 | traceIt(traceBuffer);
35 | }
36 |
37 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
38 | void trace2(char* mnemonic, int ip, int reg1, signed char mode, int reg2, int ep)
39 | {
40 | int reg;
41 |
42 | if (mode == REG_ADDR)
43 | {
44 | sprintf(traceBuffer, "%i: %s X%i, A%i", ip, mnemonic, reg1, reg2);
45 | }
46 | else
47 | {
48 | reg = (reg1 - ep - 3);
49 | sprintf(traceBuffer, "%i: %s Y%i, A%i", ip, mnemonic, reg, reg2);
50 | }
51 |
52 | traceIt(traceBuffer);
53 | }
54 |
55 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
56 | void traceFn0(char* mnemonic, int ip, int fn)
57 | {
58 | sprintf(traceBuffer, "%i: %s %i", ip, mnemonic, fn);
59 | traceIt(traceBuffer);
60 | }
61 |
62 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
63 | void traceFn1(char* mnemonic, int ip, int reg1, int fn)
64 | {
65 | sprintf(traceBuffer, "%i: %s X%i,%i", ip, mnemonic, reg1, fn);
66 | traceIt(traceBuffer);
67 | }
68 |
69 | /* Callback onto to 'trace' method of obj, for debugging purposes only. */
70 | void traceConst(char* mnemonic, int ip, int val)
71 | {
72 | sprintf(traceBuffer, "%i: %s %i", ip, mnemonic, val);
73 | traceIt(traceBuffer);
74 | }
75 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/BuiltInFunctor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.common.util.doublemaps.SymbolKey;
20 |
21 | /**
22 | * BuiltInFunctor provides a mapping from a functor onto its built-in implementation, in the case of built-in predicates
23 | * in Prolog.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Provide a mapping from a functor to its built-in implementation. | {@link BuiltIn}.
28 | * |
29 | *
30 | * @author Rupert Smith
31 | */
32 | public abstract class BuiltInFunctor extends Functor implements BuiltIn
33 | {
34 | /** Holds the functor that this is a built in for. */
35 | protected Functor functor;
36 |
37 | /**
38 | * Creates a built-in for the specified functor.
39 | *
40 | * @param functor The functor to create a built-in for.
41 | */
42 | public BuiltInFunctor(Functor functor)
43 | {
44 | super(functor.getName(), functor.getArguments());
45 | this.functor = functor;
46 | }
47 |
48 | /**
49 | * Provides the functor that this is a built-in for.
50 | *
51 | * @return The functor that this is a built-in for.
52 | */
53 | public Functor getFunctor()
54 | {
55 | return functor;
56 | }
57 |
58 | /** {@inheritDoc} */
59 | public void setSymbolKey(SymbolKey key)
60 | {
61 | functor.setSymbolKey(key);
62 | }
63 |
64 | /** {@inheritDoc} */
65 | public SymbolKey getSymbolKey()
66 | {
67 | return functor.getSymbolKey();
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/debugger/RegisterModel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.debugger;
17 |
18 | /**
19 | * RegisterModel describes the register and flag set of some machine, reflectively so tools can be built on top of the
20 | * register model dynamically.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Describe the register and flag set of some abstract machine.
25 | * |
26 | *
27 | * @author Rupert Smith
28 | */
29 | public interface RegisterModel
30 | {
31 | /**
32 | * Provides a list of all of the registers in the machine.
33 | *
34 | * @return A list of all of the registers in the machine.
35 | */
36 | String[] getRegisterNames();
37 |
38 | /**
39 | * Provides a list of all of the flags in the machine.
40 | *
41 | * @return A list of all of the flags in the machine.
42 | */
43 | String[] getFlagNames();
44 |
45 | /**
46 | * Provides the number of bytes that a register occupies.
47 | *
48 | * @param name The register to get the size of.
49 | *
50 | * @return The number of bytes that a register occupies.
51 | */
52 | int getRegisterSizeBytes(String name);
53 |
54 | /**
55 | * Provides the contents of a register.
56 | *
57 | * @param name The register to read.
58 | *
59 | * @return The registers value.
60 | */
61 | byte[] getRegister(String name);
62 |
63 | /**
64 | * Gets the status of a flag.
65 | *
66 | * @param name The flag to read.
67 | *
68 | * @return The flags name.
69 | */
70 | boolean getFlag(String name);
71 | }
72 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1CompiledProgramFunctor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import java.util.LinkedList;
19 | import java.util.Map;
20 | import java.util.Queue;
21 |
22 | /**
23 | * L1CompiledProgramFunctor is a {@link L1CompiledFunctor} for L1 progrmas.
24 | *
25 | * It provides a FIFO stack to order the instructions going forward for the decompilation algorithm in
26 | * {@link L1CompiledFunctor}.
27 | *
28 | *
CRC Card
29 | * | Responsibilities | Collaborations
30 | * |
|---|
| Provide FIFO ordering for instruction scan.
31 | * |
32 | *
33 | * @author Rupert Smith
34 | */
35 | public class L1CompiledProgramFunctor extends L1CompiledFunctor
36 | {
37 | /**
38 | * Builds a compiled down L1 functor on a buffer of compiled code.
39 | *
40 | * @param machine The L1 byte code machine that the functor has been compiled to.
41 | * @param callTableEntry The offset of the code buffer within its compiled to machine.
42 | * @param varNames A mapping from register to variable names.
43 | */
44 | public L1CompiledProgramFunctor(L1Machine machine, L1CallTableEntry callTableEntry, Map varNames)
45 | {
46 | super(machine, callTableEntry, varNames);
47 | }
48 |
49 | /**
50 | * Gets an instance of a queue implementation, to hold the functor and variable creating instruction start offsets
51 | * in. Programs use a FIFO queue to scan instructions forwards.
52 | *
53 | * @return A queue to hold the instruction start offsets in.
54 | */
55 | protected Queue getInstructionQueue()
56 | {
57 | return new LinkedList();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0CompiledProgramFunctor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import java.util.LinkedList;
19 | import java.util.Map;
20 | import java.util.Queue;
21 |
22 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
23 |
24 | /**
25 | * L0CompiledProgramFunctor is a {@link L0CompiledFunctor} for L0 progrmas.
26 | *
27 | * It provides a FIFO stack to order the instructions going forward for the decompilation algorithm in
28 | * {@link L0CompiledFunctor}.
29 | *
30 | *
CRC Card
31 | * | Responsibilities | Collaborations
32 | * |
|---|
| Provide FIFO ordering for instruction scan.
33 | * |
34 | *
35 | * @author Rupert Smith
36 | */
37 | public class L0CompiledProgramFunctor extends L0CompiledFunctor
38 | {
39 | /**
40 | * Builds a compiled down L0 functor on a buffer of compiled code.
41 | *
42 | * @param machine The L0 byte code machine that the functor has been compiled to.
43 | * @param code The code buffer for the functor.
44 | * @param varNames A mapping from register to variable names.
45 | */
46 | public L0CompiledProgramFunctor(VariableAndFunctorInterner machine, byte[] code, Map varNames)
47 | {
48 | super(machine, code, varNames);
49 | }
50 |
51 | /**
52 | * Gets an instance of a queue implementation, to hold the functor and variable creating instruction start offsets
53 | * in. Programs use a FIFO queue to scan instructions forwards.
54 | *
55 | * @return A queue to hold the instruction start offsets in.
56 | */
57 | protected Queue getInstructionQueue()
58 | {
59 | return new LinkedList();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1CompiledQueryFunctor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import java.util.Map;
19 | import java.util.Queue;
20 |
21 | import com.thesett.common.util.StackQueue;
22 |
23 | /**
24 | * L1CompiledQueryFunctor is a {@link L1CompiledFunctor} for L1 queries.
25 | *
26 | * It provides a LIFO stack to order the instructions in reverse for the decompilation algorithm in
27 | * {@link L1CompiledFunctor}.
28 | *
29 | *
CRC Card
30 | * | Responsibilities | Collaborations
31 | * |
|---|
| Provide LIFO ordering for instruction scan.
32 | * |
33 | *
34 | * @author Rupert Smith
35 | */
36 | public class L1CompiledQueryFunctor extends L1CompiledFunctor
37 | {
38 | /**
39 | * Builds a compiled down L1 functor on a buffer of compiled code.
40 | *
41 | * @param machine The L1 byte code machine that the functor has been compiled to.
42 | * @param callTableEntry The offset of the code buffer within its compiled to machine.
43 | * @param varNames A mapping from register to variable names.
44 | */
45 | public L1CompiledQueryFunctor(L1Machine machine, L1CallTableEntry callTableEntry, Map varNames)
46 | {
47 | super(machine, callTableEntry, varNames);
48 | }
49 |
50 | /**
51 | * Gets an instance of a queue implementation, to hold the functor and variable creating instruction start offsets
52 | * in. Queries use a LIFO stack to scan the instructions backwards.
53 | *
54 | * @return A queue to hold the instruction start offsets in.
55 | */
56 | protected Queue getInstructionQueue()
57 | {
58 | return new StackQueue();
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/l0/src/main/com/thesett/aima/logic/fol/l0/L0CompiledQueryFunctor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l0;
17 |
18 | import java.util.Map;
19 | import java.util.Queue;
20 |
21 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
22 | import com.thesett.common.util.StackQueue;
23 |
24 | /**
25 | * L0CompiledQueryFunctor is a {@link L0CompiledFunctor} for L0 queries.
26 | *
27 | * It provides a LIFO stack to order the instructions in reverse for the decompilation algorithm in
28 | * {@link L0CompiledFunctor}.
29 | *
30 | *
CRC Card
31 | * | Responsibilities | Collaborations
32 | * |
|---|
| Provide LIFO ordering for instruction scan.
33 | * |
34 | *
35 | * @author Rupert Smith
36 | */
37 | public class L0CompiledQueryFunctor extends L0CompiledFunctor
38 | {
39 | /**
40 | * Builds a compiled down L0 functor on a buffer of compiled code.
41 | *
42 | * @param machine The L0 byte code machine that the functor has been compiled to.
43 | * @param code The code buffer for the functor.
44 | * @param varNames A mapping from register to variable names.
45 | */
46 | public L0CompiledQueryFunctor(VariableAndFunctorInterner machine, byte[] code, Map varNames)
47 | {
48 | super(machine, code, varNames);
49 | }
50 |
51 | /**
52 | * Gets an instance of a queue implementation, to hold the functor and variable creating instruction start offsets
53 | * in. Queries use a LIFO stack to scan the instructions backwards.
54 | *
55 | * @return A queue to hold the instruction start offsets in.
56 | */
57 | protected Queue getInstructionQueue()
58 | {
59 | return new StackQueue();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/BaseBuiltIn.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.DefaultBuiltIn;
21 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
22 | import com.thesett.common.util.SizeableLinkedList;
23 |
24 | /**
25 | * BaseBuiltIn provides a base-class on which built-in predicates may be implemented.
26 | *
27 | *
CRC Card
28 | * | Responsibilities | Collaborations
29 | * |
|---|
30 | *
31 | * @author Rupert Smith
32 | */
33 | public abstract class BaseBuiltIn extends BuiltInFunctor
34 | {
35 | /** Holds the default built in, for standard compilation and interners and symbol tables. */
36 | protected final DefaultBuiltIn defaultBuiltIn;
37 |
38 | /**
39 | * Creates the base built in on the specified functor.
40 | *
41 | * @param functor The functor to create a built-in for.
42 | * @param defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
43 | */
44 | protected BaseBuiltIn(Functor functor, DefaultBuiltIn defaultBuiltIn)
45 | {
46 | super(functor);
47 |
48 | this.defaultBuiltIn = defaultBuiltIn;
49 | }
50 |
51 | /** {@inheritDoc} */
52 | public SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
53 | FunctorName clauseName, int bodyNumber)
54 | {
55 | return null;
56 | }
57 |
58 | /** {@inheritDoc} */
59 | public SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody,
60 | boolean isLastBody, boolean chainRule, int permVarsRemaining)
61 | {
62 | return null;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/machine/WAMResolvingMachineDPIMonitor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.machine;
17 |
18 | /**
19 | * WAMResolvingMachineDPIMonitor is a call-back interface, that can be attached to a {@link WAMResolvingMachineDPI}, in
20 | * order to receive notification of certain events published by the abstract machine through it debugging interface.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Accept notification of the machine being reset. |
25 | * | Accept notification that the machine has stepped by one instruction. |
26 | * | Accept notification that the machine is starting a code execution. |
27 | *
28 | *
29 | * @author Rupert Smith
30 | */
31 | public interface WAMResolvingMachineDPIMonitor
32 | {
33 | /**
34 | * Accepts notification that the machine has been reset.
35 | *
36 | * @param dpi The machines DPI
37 | */
38 | void onReset(WAMResolvingMachineDPI dpi);
39 |
40 | /**
41 | * Accepts notification of changes to byte code loaded into the machine.
42 | *
43 | * @param dpi The machines DPI.
44 | * @param start The start offset of the changed byte code within the machines code buffer.
45 | * @param length The length of the changed byte code within the machines code buffer.
46 | */
47 | void onCodeUpdate(WAMResolvingMachineDPI dpi, int start, int length);
48 |
49 | /**
50 | * Accepts notification that the machine is starting a code execution.
51 | *
52 | * @param dpi The machines DPI
53 | */
54 | void onExecute(WAMResolvingMachineDPI dpi);
55 |
56 | /**
57 | * Accepts notification that the machine has been stepped by one instruction.
58 | *
59 | * @param dpi The machines DPI
60 | */
61 | void onStep(WAMResolvingMachineDPI dpi);
62 | }
63 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2Sentence.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Sentence;
20 |
21 | /**
22 | * L2 is a resolution language, over first order logic. Its sentences can be in one of two forms, a program sentence,
23 | * that puts a first order term into its term store, and a query sentence, that fetches terms from its term store and
24 | * unifies with and resolves them against the term store. The two sentence types are closely related, in that a program
25 | * sentence has a head and a body and its body is essentially the same thing as a query. A query unifies against a
26 | * matching program head, and if that program has a body, it is run as a query.
27 | *
28 | *
CRC Card
29 | * | Responsibilities
30 | * |
|---|
| Create L2 programs and queries from clauses.
31 | * |
32 | *
33 | * @author Rupert Smith
34 | */
35 | public class L2Sentence implements Sentence
36 | {
37 | /** Holds the clause that forms this sentence. */
38 | Clause clause;
39 |
40 | /**
41 | * Creates a program or query sentence in L2.
42 | *
43 | * @param clause The clause that forms this sentence.
44 | */
45 | public L2Sentence(Clause clause)
46 | {
47 | this.clause = clause;
48 | }
49 |
50 | /**
51 | * Gets the wrapped sentence in the logical language over clauses.
52 | *
53 | * @return The wrapped sentence in the logical language.
54 | */
55 | public Clause getT()
56 | {
57 | return clause;
58 | }
59 |
60 | /**
61 | * Outputs this sentence as a string, mainly for debugging purposes.
62 | *
63 | * @return This sentence as a string, mainly for debugging purposes.
64 | */
65 | public String toString()
66 | {
67 | return "L2Sentence: [ clause = " + clause + " ]";
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1FileMachine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import com.thesett.common.error.NotImplementedException;
19 |
20 | /**
21 | * L1FileMachine is an {@link L1Machine} that can load and store compiled L1 byte code to files.
22 | *
23 | *
CRC Card
24 | * | Responsbilities | Collaborations
25 | * |
|---|
| Load byte code from a file.
26 | * |
| Store byte code to a file.
27 | * |
28 | *
29 | * @author Rupert Smith
30 | * @todo Provide a way for this machine to pass the byte code to an execution machine. For example, by wrapping one.
31 | */
32 | public class L1FileMachine extends L1BaseMachine
33 | {
34 | /**
35 | * Adds compiled byte code to the code area of the machine.
36 | *
37 | * @param code The compiled byte code.
38 | * @param start The start offset within the compiled code to copy into the machine.
39 | * @param end The end offset within the compiled code to copy into the machine.
40 | * @param functorName The interned name of the functor that the code is for.
41 | * @param isQuery true if the code is for a query, false if it is for a program.
42 | *
43 | * @return The start offset of the functors code within the code area of the machine.
44 | */
45 | public L1CallTableEntry addCode(byte[] code, int start, int end, int functorName, boolean isQuery)
46 | {
47 | throw new NotImplementedException();
48 | }
49 |
50 | /**
51 | * Extracts the raw byte code from the machine for a given call table entry.
52 | *
53 | * @param callTableEntry The call table entry giving the location and length of the code.
54 | *
55 | * @return The byte code at the specified location.
56 | */
57 | public byte[] getByteCode(L1CallTableEntry callTableEntry)
58 | {
59 | throw new NotImplementedException();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/machine/WAMResolvingMachineDPI.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.machine;
17 |
18 | import java.nio.IntBuffer;
19 |
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 |
22 | /**
23 | * WAMResolvingMachineDPI is a debug and profiling interface for a {@link WAMResolvingMachine}.
24 | *
25 | *
CRC Card
26 | * | Responsibilities | Collaborations
27 | * |
|---|
| Provide access to the machines code buffer. |
28 | * | Provide access to the machines data buffer, and delimiters for around different data areas. |
29 | * | Provide the current heap pointer. |
30 | *
31 | *
32 | * @author Rupert Smith
33 | */
34 | public interface WAMResolvingMachineDPI extends WAMCodeView
35 | {
36 | /**
37 | * Attaches a monitor to the abstract machine.
38 | *
39 | * @param monitor The machine monitor.
40 | */
41 | void attachMonitor(WAMResolvingMachineDPIMonitor monitor);
42 |
43 | /**
44 | * Provides read access to the the machines data area.
45 | *
46 | * @return The requested portion of the machines data area.
47 | */
48 | IntBuffer getDataBuffer();
49 |
50 | /**
51 | * Provides the internal register file and flags for the machine.
52 | *
53 | * @return The internal register file and flags for the machine.
54 | */
55 | WAMInternalRegisters getInternalRegisters();
56 |
57 | /**
58 | * Provides the internal register set describing the memory layout of the machine.
59 | *
60 | * @return The internal register set describing the memory layout of the machine.
61 | */
62 | WAMMemoryLayout getMemoryLayout();
63 |
64 | /**
65 | * Provides an interner for translating interned names against the underlying machine.
66 | *
67 | * @return An interner for translating interned names against the underlying machine.
68 | */
69 | VariableAndFunctorInterner getVariableAndFunctorInterner();
70 | }
71 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/SymbolTableKeys.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | /**
19 | * Holds symbol table key definitions. These are used as column selectors to store/retrieve information on the symbol
20 | * table.
21 | *
22 | *
CRC Card
23 | * | Responsibilities | Collaborations
24 | * |
|---|
| Hold symbol table key definitions.
25 | * |
26 | *
27 | * @author Rupert Smith
28 | */
29 | public class SymbolTableKeys
30 | {
31 | /** The symbol table key for allocations. */
32 | public static final String SYMKEY_ALLOCATION = "allocation";
33 |
34 | /** The symbol table key for the number of permanent variables remaining. */
35 | public static final String SYMKEY_PERM_VARS_REMAINING = "perm_vars_remaining";
36 |
37 | /** The symbol table key for variable occurrence counts. */
38 | public static final String SYMKEY_VAR_OCCURRENCE_COUNT = "var_occurrence_count";
39 |
40 | /** The symbol table key for variable position of occurrence. */
41 | public static final String SYMKEY_VAR_NON_ARG = "var_non_arg";
42 |
43 | /** The symbol table key for functor position of occurrence. */
44 | public static final String SYMKEY_FUNCTOR_NON_ARG = "functor_non_arg";
45 |
46 | /** The symbol table key for variable introduction type. */
47 | public static final String SYMKEY_VARIABLE_INTRO = "variable_intro";
48 |
49 | /** The symbol table key for the last functor in which a variable occurs, if it is purely in argument position. */
50 | public static final String SYMKEY_VAR_LAST_ARG_FUNCTOR = "var_last_arg_functor";
51 |
52 | /** The symbol table key for functors that are top-level within a clause. */
53 | public static final String SYMKEY_TOP_LEVEL_FUNCTOR = "top_level_functor";
54 |
55 | /** The symbol table key for permanent variable offset to hold a cut to choice point frame in. */
56 | public static final String SYMKEY_CLAUSE_PERM_CUT = "clause_perm_cut";
57 |
58 | /** The symbol table key for predicate sources. */
59 | public static final String SYMKEY_PREDICATES = "source_predicates";
60 | }
61 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | WAM Book Prolog
2 | ===============
3 |
4 | This is an implementation of the family of logic compilers described in "Warren's Abstract Machine: A Tutorial Reconstruction" by Hassan Aït-Kaci.
5 |
6 | A family of logic compilers is described in this book, starting from L0 which introduces unification, through L1, L2 and L3, which build up more of a Prolog implementation, leading the WAM, which is a more complete Prolog implementation and optimizes the simpler designs of the earlier languages.
7 |
8 | A copy of the WAM book, with errata, can be found here:
9 |
10 | https://github.com/a-yiorgos/wambook
11 |
12 | Getting Started
13 | ---------------
14 |
15 | Ensure you have Apache Maven 3.2.2 or later installed.
16 |
17 | Build with:
18 |
19 | > mvn clean install
20 |
21 | Run the WAM implementation with:
22 |
23 | > cd target
24 | > chmod +x wam
25 | > ./wam
26 |
27 | Audience
28 | --------
29 |
30 | This work may be of interest to students of compiler design. The code is well documented, and tries to faithfully implement the design described in the book.
31 |
32 | This work was originally produced in order for me to learn how such a compiler works, as well as to provide a basis for experimenting with variations on logic programming languages.
33 |
34 | In addition to Prolog, you can find examples of parsers, and elements of a modular compiler design that are fairly "text book" in their nature. The symbol table design was heavily influenced by one described in "Engineering A Compiler", by Keith Cooper and Linda Torczon, and could be re-used in other compiler projects.
35 |
36 | State of the work
37 | -----------------
38 |
39 | The languages L0 through L3 are complete as per the book.
40 |
41 | The WAM machine is missing the indexing scheme. This may get completed at a later date, especially if someone were to contribute it.
42 |
43 | The languages L0 through L2 also have native byte code machines implemented in C. Given the state-of-the-art in Java compilations and virtual machines, these are unlikely to offer a performance advantage over the machines implemented in Java.
44 |
45 | The language L2 has a JIT compilation machine implemented on top of LLVM. This is not 100% complete, but provides a decent introduction to compiling down to native code.
46 |
47 | Other Sources
48 | -------------
49 |
50 | Some parts of the source are outside of this git repository, the Prolog parser in particular, as these are also used in other projects. Take a look at this repository if you want a copy of all of the sources:
51 |
52 | https://github.com/rupertlssmith/lojix
53 |
54 | This is currently built against version 0.8.11 of lojix, to get the sources for that version checkout its tag:
55 |
56 | > git clone https://github.com/rupertlssmith/lojix.git
57 | > cd lojix
58 | > git checkout v0.8.11
59 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/L3ReservedLabel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import java.util.Collection;
19 | import java.util.LinkedList;
20 | import java.util.List;
21 |
22 | /**
23 | * L3ReservedLabel is an {@link L3CallPoint} that is an address for a label. The label in question may be forward
24 | * referenced, so its address cannot be resolved until the label is encountered as code is generated. In the situation
25 | * that a forward reference to a label is encountered, a dummy invalid address can be substituted for the label, and the
26 | * offset at which that dummy address is written can be recorded. Once the label has been resolved to a valid address,
27 | * the dummy values can be re-visited and corrected. L3ReservedLabel is an L3CallPoint that also maintains a list of
28 | * referenced from address to be corrected, once the label address is known.
29 | *
30 | * An L3ReservedLabel is created with an entry point of -1, which means that it has not been resolved to a known
31 | * address yet. Once the entry point is set to a value other than -1, it is considered to have been resolved.
32 | *
33 | *
CRC Card
34 | * | Responsibilities | Collaborations
35 | * |
|---|
| Maintain a list of referenced from address against a label.
36 | * |
37 | *
38 | * @author Rupert Smith
39 | */
40 | public class L3ReservedLabel extends L3CallPoint
41 | {
42 | /** The list of referenced from addresses. */
43 | public Collection referenceList = new LinkedList();
44 |
45 | /**
46 | * Creates a call table entry for the code with the specified entry address and length.
47 | *
48 | * @param functorName The functors interned name.
49 | */
50 | public L3ReservedLabel(int functorName)
51 | {
52 | super(-1, 0, functorName);
53 | }
54 |
55 | /**
56 | * Checks if this label has been resolved to a concrete address yet.
57 | *
58 | * @return true iff this label has been resolved to a concrete address.
59 | */
60 | public boolean isResolved()
61 | {
62 | return entryPoint != -1;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/WAMReservedLabel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | import java.util.Collection;
19 | import java.util.LinkedList;
20 | import java.util.List;
21 |
22 | /**
23 | * WAMReservedLabel is an {@link WAMCallPoint} that is an address for a label. The label in question may be forward
24 | * referenced, so its address cannot be resolved until the label is encountered as code is generated. In the situation
25 | * that a forward reference to a label is encountered, a dummy invalid address can be substituted for the label, and the
26 | * offset at which that dummy address is written can be recorded. Once the label has been resolved to a valid address,
27 | * the dummy values can be re-visited and corrected. WAMReservedLabel is an WAMCallPoint that also maintains a list of
28 | * referenced from address to be corrected, once the label address is known.
29 | *
30 | * An WAMReservedLabel is created with an entry point of -1, which means that it has not been resolved to a known
31 | * address yet. Once the entry point is set to a value other than -1, it is considered to have been resolved.
32 | *
33 | *
CRC Card
34 | * | Responsibilities | Collaborations
35 | * |
|---|
| Maintain a list of referenced from address against a label.
36 | * |
37 | *
38 | * @author Rupert Smith
39 | */
40 | public class WAMReservedLabel extends WAMCallPoint
41 | {
42 | /** The list of referenced from addresses. */
43 | public Collection referenceList = new LinkedList();
44 |
45 | /**
46 | * Creates a call table entry for the code with the specified entry address and length.
47 | *
48 | * @param functorName The functors interned name.
49 | */
50 | public WAMReservedLabel(int functorName)
51 | {
52 | super(-1, 0, functorName);
53 | }
54 |
55 | /**
56 | * Checks if this label has been resolved to a concrete address yet.
57 | *
58 | * @return true iff this label has been resolved to a concrete address.
59 | */
60 | public boolean isResolved()
61 | {
62 | return entryPoint != -1;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/Fail.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.DefaultBuiltIn;
21 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
22 | import com.thesett.common.util.SizeableLinkedList;
23 |
24 | /**
25 | * Implements the fail atom. This always fails without generating any choice points.
26 | *
27 | *
CRC Card
28 | * | Responsibilities | Collaborations
29 | * |
|---|
| Always fail, consuming the 'fail' goal.
30 | * |
31 | *
32 | * @author Rupert Smith
33 | */
34 | public class Fail extends BaseBuiltIn
35 | {
36 | /**
37 | * Creates a fail built-in to implement the specified functor.
38 | *
39 | * @param functor The functor to implement as a built-in.
40 | * @param defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
41 | */
42 | public Fail(Functor functor, DefaultBuiltIn defaultBuiltIn)
43 | {
44 | super(functor, defaultBuiltIn);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
49 | FunctorName clauseName, int bodyNumber)
50 | {
51 | return new SizeableLinkedList();
52 | }
53 |
54 | /** {@inheritDoc} */
55 | public SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody,
56 | boolean isLastBody, boolean chainRule, int permVarsRemaining)
57 | {
58 | SizeableLinkedList instructions = new SizeableLinkedList();
59 |
60 | instructions.add(new WAMInstruction(WAMInstruction.WAMInstructionSet.Execute, new FunctorName("__fail__", 0)));
61 |
62 | return instructions;
63 | }
64 |
65 | /**
66 | * Creates a string representation of this functor, mostly used for debugging purposes.
67 | *
68 | * @return A string representation of this functor.
69 | */
70 | public String toString()
71 | {
72 | return "Fail";
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/Unifies.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.DefaultBuiltIn;
21 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
22 | import com.thesett.common.util.SizeableLinkedList;
23 |
24 | /**
25 | * Unifies is the ISO Prolog built in operator '='/2. It performs a standard unification (no occurs check) on its left
26 | * and right arguments, possibly binding variables as a result of the unification, and succeeds iff the unification
27 | * succeeds.
28 | *
29 | *
CRC Card
30 | * | Responsibilities | Collaborations
31 | * |
|---|
| Unify the left and right arguments of the unify operator.
32 | * |
33 | *
34 | * @author Rupert Smith
35 | */
36 | public class Unifies extends BaseBuiltIn
37 | {
38 | /**
39 | * Creates a cut built-in to implement the specified functor.
40 | *
41 | * @param functor The functor to implement as a built-in.
42 | * @param defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
43 | */
44 | public Unifies(Functor functor, DefaultBuiltIn defaultBuiltIn)
45 | {
46 | super(functor, defaultBuiltIn);
47 | }
48 |
49 | /** {@inheritDoc} */
50 | public SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
51 | FunctorName clauseName, int bodyNumber)
52 | {
53 | return new SizeableLinkedList();
54 | }
55 |
56 | /** {@inheritDoc} */
57 | public SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody,
58 | boolean isLastBody, boolean chainRule, int permVarsRemaining)
59 | {
60 | return new SizeableLinkedList();
61 | }
62 |
63 | /**
64 | * Creates a string representation of this functor, mostly used for debugging purposes.
65 | *
66 | * @return A string representation of this functor.
67 | */
68 | public String toString()
69 | {
70 | return "Unifies: [ arguments = " + toStringArguments() + " ]";
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/indexing/CodeBufferTable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.indexing;
17 |
18 | import java.nio.ByteBuffer;
19 |
20 | /**
21 | * CodeBufferTable is an associative array interface that is kept as simple as possible, for the purpose to which it is
22 | * used. A CodeBufferTable is a map from integer keys, to integer address, that holds its mapping table in a ByteBuffer.
23 | * The table will be built at compile time, using the {@link #put(int, int)} operations, and only read from at run time,
24 | * using the {@link #get(int)} operation. Puts and gets will not be interleaved, and the number of items in the table
25 | * will be known in advance. There is no delete operation, or any other operations from standard Map interfaces.
26 | *
27 | * Prior to using the put or get operations, the table must be initialized to work on an area of a ByteBuffer, and
28 | * this is accomplished with the {@link #setup(ByteBuffer, int, int)} operation.
29 | *
30 | *
CRC Card
31 | * | Responsibilities | Collaborations
32 | * |
|---|
| Establish a table on a ByteBuffer. |
33 | * | Associate an address with a key. |
34 | * | Look up an address by key. |
35 | *
36 | *
37 | * @author Rupert Smith
38 | */
39 | public interface CodeBufferTable
40 | {
41 | /**
42 | * Initializes the table against an area within a ByteBuffer.
43 | *
44 | * @param buffer The underlying buffer to store the hash table in.
45 | * @param t The offset of the start of the hash table.
46 | * @param n The size of the hash table in bytes.
47 | */
48 | void setup(ByteBuffer buffer, int t, int n);
49 |
50 | /**
51 | * Looks up an address by key, in the hash table of size n referred to.
52 | *
53 | * @param key The key to look up.
54 | *
55 | * @return 0 iff no match is found, or the matching address.
56 | */
57 | int get(int key);
58 |
59 | /**
60 | * Inserts an address associated with a key, in the hash table of size n referred to.
61 | *
62 | * @param key The key to insert.
63 | * @param addr An address to associate with the key.
64 | */
65 | void put(int key, int addr);
66 | }
67 |
--------------------------------------------------------------------------------
/cli/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 | com.thesett
5 | wambook_cli
6 | interpreter
7 | 1.0.3-SNAPSHOT
8 |
9 | https://www.thesett.com/build_reports/lojix/interpreter
10 |
11 | jar
12 |
13 |
14 | ${basedir}/..
15 |
16 |
17 |
18 | com.thesett
19 | wambookbuild
20 | 1.0.3-SNAPSHOT
21 | ../pom.xml
22 |
23 |
24 |
25 |
26 | com.thesett
27 | common
28 | 0.8.11
29 |
30 |
31 |
32 | com.thesett
33 | logic
34 | 0.8.11
35 |
36 |
37 |
38 | com.thesett
39 | logic
40 | tests
41 | 0.8.11
42 |
43 |
44 |
45 | jline
46 | jline
47 | 1.0
48 |
49 |
50 |
51 |
52 | junit
53 | junit
54 | test
55 |
56 |
57 |
58 | com.thesett
59 | base
60 | 0.8.11
61 | test
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | org.apache.maven.plugins
72 | maven-jar-plugin
73 |
74 |
75 | package
76 |
77 | test-jar
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1InstructionSet.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | /**
19 | * Defines the L1 virtual machine instruction set as constants.
20 | *
21 | *
CRC Card
22 | * | Responsibilities | Collaborations
23 | * |
|---|
| Provide the translation of the L1 instruction set down to bytes.
24 | * |
25 | *
26 | * @author Rupert Smith
27 | */
28 | public class L1InstructionSet
29 | {
30 | // === Defines the machine instruction types.
31 |
32 | /** Instruction to write out a struc onto the heap. */
33 | public static final byte PUT_STRUC = 0x01;
34 |
35 | /** The instruction to set a register as a variable. */
36 | public static final byte SET_VAR = 0x02;
37 |
38 | /** The instruction to set a register to a heap location. */
39 | public static final byte SET_VAL = 0x03;
40 |
41 | /** The instruction to compare a register to a structure on the heap. */
42 | public static final byte GET_STRUC = 0x04;
43 |
44 | /** The instruction to unify a register with a variable. */
45 | public static final byte UNIFY_VAR = 0x05;
46 |
47 | /** The instruction to unify a register with a location on the heap. */
48 | public static final byte UNIFY_VAL = 0x06;
49 |
50 | /** The instruction to copy a heap location into an argument register. */
51 | public static final byte PUT_VAR = 0x07;
52 |
53 | /** The instruction to copy a register into an argument register. */
54 | public static final byte PUT_VAL = 0x08;
55 |
56 | /** The instruction to unify an argument register with a variable. */
57 | public static final byte GET_VAR = 0x09;
58 |
59 | /** The instruction to unify a register with a location on the heap. */
60 | public static final byte GET_VAL = 0x0a;
61 |
62 | /** The instruction to call a predicate. */
63 | public static final byte CALL = 0x0b;
64 |
65 | /** The instruction to return from a called predicate. */
66 | public static final byte PROCEED = 0x0c;
67 |
68 | // === Defines the heap cell marker types.
69 |
70 | /** Indicates a register on the heap. */
71 | public static final byte REF = 0x01;
72 |
73 | /** Indicates the begining of a struc on the heap. */
74 | public static final byte STR = 0x02;
75 | }
76 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/implementation.txt:
--------------------------------------------------------------------------------
1 | === Cut
2 |
3 | neck_cut, easy to insert that correctly.
4 | get_level a little annoying, as needs to be part of the clause prefix instructions. also complicated the assignment of permanent registers.
5 |
6 | 1. To make the permanent variable allocation algorithm work, during detection of builtins, transform cut into cut(_X), that is a new anonymous variable. This variable will also be of a special type 'DepthVariable'.
7 |
8 | 2. Make a small change to the permanent variable assignment, which is to detect the DepthVariable in body clauses after the first one, and add it the head/first body variables too. In this way, it will be counted as a permanent variable, and assigned its slot correctly.
9 |
10 | 3. When assigning the slot to DepthVariable, also update the 'cutParameterVar' field, so that compileClause knows which slot to use and to insert a get_level instruction.
11 |
12 | 4. Deep cut is detected in the permanent variable analysis phase, so compileClause knows up-front that get_level should be added to the prefix. For compileQuery, environment trimming is not important, so just allocate the get_level variable on every query, and make it the last permanent var (numPerms + 1).
13 |
14 | === Call
15 |
16 | When statically calling a predicate, its arguments are built on the heap, and referenced by registers or stack variables. Once this is set up, the predicate is called.
17 |
18 | A dynamic call should behave in exactly the same way, so call/1 needs to implement the procedure described above.
19 |
20 | When call/1 is invoked, its arguments will already have been created. What is needed is to dereference the argument, find the structure it points to, resolve that as a functor to call (or fail), assign argument registers to the sub-terms of the structure, then make the call.
21 |
22 | 1. Add a switch for invoking internal functions, and a new instruction call_internal. Built-ins with special internal functions will use this, call_call being one of them.
23 | 2. Implement call_call.
24 | Deref argument.
25 | Get structure and num args.
26 | Check that fn exists to call.
27 | Bind arguments to registers.
28 | Call it.
29 | 3. Recognize call and replace its functor with call_call.
30 |
31 | === Unifies
32 |
33 | Create LHS as query parameters.
34 | Create RHS as program parameters.
35 | No call instruction, just run the 2 together.
36 |
37 | OR
38 |
39 | Implement as:
40 |
41 | =(X, X).
42 | \=(X, Y) :- \+(X = Y).
43 |
44 | === Disjunction
45 |
46 | Does this complicate permanent register assignment? Yes, but use the same procedure to count the number of permanent
47 | registers, just include nested conjunctive/disjunctive top-level terms in the count.
48 |
49 | Problems with current inline implementations:
50 |
51 | try_me/retry_me Saves the argument registers of the current clause. In the inline case, another clause may have been
52 | called prior to this, in which case num_args is set wrong. Need num_args for this predicate, not any that might have
53 | been called in between.
54 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2Machine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import com.thesett.aima.logic.fol.LinkageException;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 |
21 | /**
22 | * L2Machine is an abstract machine capable of handling the L2 language in its compiled form.
23 | *
24 | *
CRC Card
25 | * | Responsibilities | Collaborations
26 | * |
|---|
| Provide symbol table for functors names.
27 | * |
| Provide symbol table for variable names.
28 | * |
| Accept injected byte code for functors into the machine.
29 | * |
| Provide the address of that start offset of code for named functors.
30 | * |
31 | *
32 | * @author Rupert Smith
33 | */
34 | public interface L2Machine extends VariableAndFunctorInterner
35 | {
36 | /**
37 | * Provides a call point for the named functor. This method is primarily for use when storing or linking compiled
38 | * byte code, into its binary form in a machine. The returned call point provides a binary address to use for calls
39 | * to the specified functor.
40 | *
41 | * @param functorName The interned name of the functor to get a binary call point for.
42 | *
43 | * @return The call point of the specified functor to call.
44 | */
45 | L2CallPoint resolveCallPoint(int functorName);
46 |
47 | /**
48 | * Adds compiled byte code to the code area of the machine.
49 | *
50 | * @param clause The compiled clause to add byte code to the machine for.
51 | *
52 | * @throws LinkageException If the clause to be added to the machine, cannot be added to it, because it depends on
53 | * the existence of other callable clause heads which are not in the machine.
54 | * Implementations may elect to raise this as an error at the time the functor is added to
55 | * the machine, or during execution, or simply to fail to find a solution.
56 | */
57 | void emmitCode(L2CompiledClause clause) throws LinkageException;
58 |
59 | /**
60 | * Extracts the binary byte code from the machine for a given call point.
61 | *
62 | * @param callPoint The call point giving the location of the code.
63 | *
64 | * @return The byte code at the specified location.
65 | */
66 | byte[] retrieveCode(L2CallPoint callPoint);
67 | }
68 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/True.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.DefaultBuiltIn;
21 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
22 | import com.thesett.common.util.SizeableLinkedList;
23 |
24 | /**
25 | * Implements the true atom. This always succeeds without generating any choice points.
26 | *
27 | *
CRC Card
28 | * | Responsibilities | Collaborations
29 | * |
|---|
| Always succeed, consuming the 'true' goal.
30 | * |
31 | *
32 | * @author Rupert Smith
33 | */
34 | public class True extends BaseBuiltIn
35 | {
36 | /**
37 | * Creates a true built-in to implement the specified functor.
38 | *
39 | * @param functor The functor to implement as a built in.
40 | * @param defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
41 | */
42 | public True(Functor functor, DefaultBuiltIn defaultBuiltIn)
43 | {
44 | super(functor, defaultBuiltIn);
45 | }
46 |
47 | /** {@inheritDoc} */
48 | public SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
49 | FunctorName clauseName, int bodyNumber)
50 | {
51 | return new SizeableLinkedList();
52 | }
53 |
54 | /** {@inheritDoc} */
55 | public SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody,
56 | boolean isLastBody, boolean chainRule, int permVarsRemaining)
57 | {
58 | SizeableLinkedList result = new SizeableLinkedList();
59 |
60 | if (isLastBody)
61 | {
62 | if (!chainRule)
63 | {
64 | result.add(new WAMInstruction(WAMInstruction.WAMInstructionSet.Deallocate));
65 | }
66 |
67 | result.add(new WAMInstruction(WAMInstruction.WAMInstructionSet.Proceed));
68 | }
69 |
70 | return result;
71 | }
72 |
73 | /**
74 | * Creates a string representation of this functor, mostly used for debugging purposes.
75 | *
76 | * @return A string representation of this functor.
77 | */
78 | public String toString()
79 | {
80 | return "True";
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/NonUnifies.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.DefaultBuiltIn;
21 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
22 | import com.thesett.common.util.SizeableLinkedList;
23 |
24 | /**
25 | * NonUnifies is the ISO Prolog built in operator '\='/2. It performs a standard unification (no occurrs check) on its
26 | * left and right arguments, possibly binding variables as a result of the unification, and fails iff the unification
27 | * succeeds. As failure will cause this proof step to be undone, any variable bindings resulting from the unification
28 | * will immediately be discarded.
29 | *
30 | *
CRC Card
31 | * | Responsibilities | Collaborations
32 | * |
|---|
| Check if unification of the left and right arguments of the non-unify operator fails.
33 | * |
34 | *
35 | * @author Rupert Smith
36 | */
37 | public class NonUnifies extends BaseBuiltIn
38 | {
39 | /**
40 | * Creates a cut built-in to implement the specified functor.
41 | *
42 | * @param functor The functor to implement as a built-in.
43 | * @param defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
44 | */
45 | public NonUnifies(Functor functor, DefaultBuiltIn defaultBuiltIn)
46 | {
47 | super(functor, defaultBuiltIn);
48 | }
49 |
50 | /** {@inheritDoc} */
51 | public SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
52 | FunctorName clauseName, int bodyNumber)
53 | {
54 | return new SizeableLinkedList();
55 | }
56 |
57 | /** {@inheritDoc} */
58 | public SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody,
59 | boolean isLastBody, boolean chainRule, int permVarsRemaining)
60 | {
61 | return new SizeableLinkedList();
62 | }
63 |
64 | /**
65 | * Creates a string representation of this functor, mostly used for debugging purposes.
66 | *
67 | * @return A string representation of this functor.
68 | */
69 | public String toString()
70 | {
71 | return "NonUnifies: [ arguments = " + toStringArguments() + " ]";
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/ByteCodePrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.common.util.doublemaps.DoubleKeyedMap;
23 | import com.thesett.common.util.doublemaps.SymbolTable;
24 |
25 | /**
26 | * ByteCodePrinter prints the compiled bytecode.
27 | *
28 | *
CRC Card
29 | * | Responsibilities | Collaborations
30 | * |
|---|
| Print the compiled byte code.
31 | * |
32 | *
33 | * @author Rupert Smith
34 | */
35 | public class ByteCodePrinter extends BasePrinter
36 | {
37 | /**
38 | * Creates a printer.
39 | *
40 | * @param interner The name interner.
41 | * @param symbolTable The compiler symbol table.
42 | * @param traverser The positional context traverser.
43 | * @param column The column to print to.
44 | * @param grid The grid to print to.
45 | * @param table The table to inform of cell sizes and positions.
46 | */
47 | public ByteCodePrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
48 | PositionalTermTraverser traverser, int column, DoubleKeyedMap grid, PrintingTable table)
49 | {
50 | super(interner, symbolTable, traverser, column, grid, table);
51 | }
52 |
53 | /** {@inheritDoc} */
54 | protected void enterClause(Clause clause)
55 | {
56 | if (clause instanceof L3CompiledQuery)
57 | {
58 | L3CompiledQuery query = (L3CompiledQuery) clause;
59 |
60 | for (L3Instruction instruction : query.getInstructions())
61 | {
62 | addLineToRow(instruction.toString());
63 | nextRow();
64 | }
65 | }
66 | }
67 |
68 | /** {@inheritDoc} */
69 | protected void enterPredicate(Predicate predicate)
70 | {
71 | if (predicate instanceof L3CompiledPredicate)
72 | {
73 | L3CompiledPredicate compiledPredicate = (L3CompiledPredicate) predicate;
74 |
75 | for (L3Instruction instruction : compiledPredicate.getInstructions())
76 | {
77 | addLineToRow(instruction.toString());
78 | nextRow();
79 | }
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/cli/src/main/com/thesett/aima/logic/fol/interpreter/InteractiveParser.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.interpreter;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Sentence;
20 | import com.thesett.aima.logic.fol.SentenceImpl;
21 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
22 | import com.thesett.aima.logic.fol.isoprologparser.BasePrologParser;
23 | import com.thesett.aima.logic.fol.isoprologparser.PrologParser;
24 | import com.thesett.common.parsing.SourceCodeException;
25 |
26 | /**
27 | * InteractiveParser is a Prolog parser that works when the Prolog system is being as an interactive terminal. In
28 | * addition to parsing first order logic Horn clauses, it handles the system directives for user mode, debugging,
29 | * loading files and so on.
30 | *
31 | *
CRC Card
32 | * | Responsibilities | Collaborations
33 | * |
|---|
| Parse a string as a logical term relative to a variable and functor interner.
34 | * | {@link Clause}, {@link VariableAndFunctorInterner}.
35 | * |
| Detect system directives.
36 | * |
37 | *
38 | * @author Rupert Smith
39 | */
40 | public class InteractiveParser extends BasePrologParser
41 | {
42 | /**
43 | * Creates a clause parser over an interner.
44 | *
45 | * @param interner The interner to use to intern all functor and variable names.
46 | */
47 | public InteractiveParser(VariableAndFunctorInterner interner)
48 | {
49 | super(interner);
50 | }
51 |
52 | /**
53 | * Parses the next sentence from the current token source.
54 | *
55 | * @return The fully parsed syntax tree for the next sentence, or null if no more sentences are available, for
56 | * example, because an end of file has been reached.
57 | *
58 | * @throws SourceCodeException If the source being parsed does not match the grammar.
59 | */
60 | public Sentence parse() throws SourceCodeException
61 | {
62 | return new SentenceImpl(parser.sentence());
63 | }
64 |
65 | /**
66 | * Peeks and consumes the next interactive system directive.
67 | *
68 | * @return The directive, or null if none is found.
69 | *
70 | * @throws SourceCodeException If the source being parsed does not match the grammar.
71 | */
72 | public PrologParser.Directive peekAndConsumeDirective() throws SourceCodeException
73 | {
74 | return parser.peekAndConsumeDirective();
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/l1/src/main/com/thesett/aima/logic/fol/l1/L1Machine.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l1;
17 |
18 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
19 |
20 | /**
21 | * L1Machine is an abstract machine capable of handling the L1 language in its compiled form.
22 | *
23 | *
CRC Card
24 | * | Responsibilities | Collaborations
25 | * |
|---|
| Provide symbol table for functors names.
26 | * |
| Provide symbol table for variable names.
27 | * |
| Accept injected byte code for functors into the machine.
28 | * |
| Provide the address of that start offset of code for named functors.
29 | * |
30 | *
31 | * @author Rupert Smith
32 | */
33 | public interface L1Machine extends VariableAndFunctorInterner
34 | {
35 | /**
36 | * Resets the machine, to its initial state. This should clear any programs from the machine, and clear all of its
37 | * stacks and heaps.
38 | */
39 | void reset();
40 |
41 | /**
42 | * Adds compiled byte code to the code area of the machine.
43 | *
44 | * @param code The compiled byte code.
45 | * @param start The start offset within the compiled code to copy into the machine.
46 | * @param end The end offset within the compiled code to copy into the machine.
47 | * @param functorName The interned name of the functor that the code is for.
48 | * @param isQuery true if the code is for a query, false if it is for a program.
49 | *
50 | * @return The call table entry for the functors code within the code area of the machine.
51 | */
52 | L1CallTableEntry addCode(byte[] code, int start, int end, int functorName, boolean isQuery);
53 |
54 | /**
55 | * Looks up the offset of the start of the code for the named functor.
56 | *
57 | * @param functorName The interned name of the functor to find the start address of the code for.
58 | *
59 | * @return The call table entry of the functors code within the code area of the machine, or null if the
60 | * functor is not known to the machine.
61 | */
62 | L1CallTableEntry getCodeAddress(int functorName);
63 |
64 | /**
65 | * Extracts the raw byte code from the machine for a given call table entry.
66 | *
67 | * @param callTableEntry The call table entry giving the location and length of the code.
68 | *
69 | * @return The byte code at the specified location.
70 | */
71 | byte[] getByteCode(L1CallTableEntry callTableEntry);
72 | }
73 |
--------------------------------------------------------------------------------
/aima-native/linux-llvm/src/unittests/com/thesett/aima/logic/fol/l2/L2ResolvingNativeMachineLLVMTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import junit.framework.Test;
19 | import junit.framework.TestCase;
20 | import junit.framework.TestSuite;
21 |
22 | import com.thesett.aima.logic.fol.BasicResolverUnitTestBase;
23 | import com.thesett.aima.logic.fol.BasicUnificationUnitTestBase;
24 | import com.thesett.aima.logic.fol.Clause;
25 | import com.thesett.aima.logic.fol.ConjunctionResolverUnitTestBase;
26 | import com.thesett.aima.logic.fol.LogicCompiler;
27 | import com.thesett.aima.logic.fol.Parser;
28 | import com.thesett.aima.logic.fol.interpreter.ResolutionEngine;
29 | import com.thesett.aima.logic.fol.isoprologparser.ClauseParser;
30 | import com.thesett.aima.logic.fol.isoprologparser.Token;
31 | import com.thesett.common.util.doublemaps.SymbolTableImpl;
32 |
33 | /**
34 | */
35 | public class L2ResolvingNativeMachineLLVMTest extends TestCase
36 | {
37 | /** Holds the L2 machine to run the tests through. */
38 | private static L2ResolvingMachine machine;
39 |
40 | public L2ResolvingNativeMachineLLVMTest(String name)
41 | {
42 | super(name);
43 | }
44 |
45 | public static Test suite() throws Exception
46 | {
47 | // Build a new test suite
48 | TestSuite suite = new TestSuite("L2UnifyingNativeMachine Tests");
49 |
50 | machine = L2ResolvingNativeMachine.getInstance();
51 |
52 | LogicCompiler compiler =
53 | new L2Compiler(new SymbolTableImpl(), machine);
54 | Parser parser = new ClauseParser(machine);
55 |
56 | ResolutionEngine engine =
57 | new ResolutionEngine(parser, machine, compiler, machine)
58 | {
59 | public void reset()
60 | {
61 | machine.reset();
62 | }
63 | };
64 |
65 | suite.addTest(new BasicUnificationUnitTestBase(
66 | "testBoundVarInQueryUnifiesAgainstVarInProg", engine));
67 | suite.addTest(new BasicResolverUnitTestBase(
68 | "testAnonymousVariableBindingNotPropagatedAccrossCall", engine));
69 | suite.addTest(new ConjunctionResolverUnitTestBase(
70 | "testSuccesiveConjunctiveTermsOk", engine));
71 |
72 | return suite;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/machine/WAMInternalRegisters.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.machine;
17 |
18 | /**
19 | * WAMInternalRegisters holds the register file and flags of the machines internal registers.
20 | *
21 | *
CRC Card
22 | * | Responsibilities | Collaborations
23 | * |
|---|
| Encapsulate the WAM machine internal register file and flags.
24 | * |
25 | *
26 | * @author Rupert Smith
27 | */
28 | public class WAMInternalRegisters
29 | {
30 | /** Holds the current instruction pointer into the code. */
31 | public int ip;
32 |
33 | /** Holds the heap pointer. */
34 | public int hp;
35 |
36 | /** Holds the top of heap at the latest choice point. */
37 | public int hbp;
38 |
39 | /** Holds the secondary heap pointer, used for the heap address of the next term to match. */
40 | public int sp;
41 |
42 | /** Holds the unification stack pointer. */
43 | public int up;
44 |
45 | /** Holds the environment base pointer. */
46 | public int ep;
47 |
48 | /** Holds the choice point base pointer. */
49 | public int bp;
50 |
51 | /** Holds the last call choice point pointer. */
52 | public int b0;
53 |
54 | /** Holds the trail pointer. */
55 | public int trp;
56 |
57 | /** Used to record whether the machine is in structure read or write mode. */
58 | public boolean writeMode;
59 |
60 | /**
61 | * Creates an instance of the WAM machine internal register file and flags.
62 | *
63 | * @param ip The current instruction pointer into the code.
64 | * @param hp The heap pointer.
65 | * @param hbp The top of heap at the latest choice point.
66 | * @param sp The secondary heap pointer, used for the heap address of the next term to match.
67 | * @param up The unification stack pointer.
68 | * @param ep The environment base pointer.
69 | * @param bp The choice point base pointer.
70 | * @param b0 The last call choice point pointer.
71 | * @param trp The trail pointer.
72 | * @param writeMode The write mode flag.
73 | */
74 | public WAMInternalRegisters(int ip, int hp, int hbp, int sp, int up, int ep, int bp, int b0, int trp,
75 | boolean writeMode)
76 | {
77 | this.ip = ip;
78 | this.hp = hp;
79 | this.hbp = hbp;
80 | this.sp = sp;
81 | this.up = up;
82 | this.ep = ep;
83 | this.bp = bp;
84 | this.b0 = b0;
85 | this.trp = trp;
86 | this.writeMode = writeMode;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/builtins/BuiltIn.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.builtins;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.FunctorName;
20 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
21 | import com.thesett.common.util.SizeableLinkedList;
22 |
23 | /**
24 | * BuiltIn defines the behaviour of Prolog built-in predicates.
25 | *
26 | *
CRC Card
27 | * | Responsibilities
28 | * |
|---|
| Generate instructions to set up the arguments to a call to a built-in functor. |
29 | * | Generate instructions to call to a built-in functor. |
30 | *
31 | *
32 | * @author Rupert Smith
33 | */
34 | public interface BuiltIn
35 | {
36 | /**
37 | * Compiles the arguments to a call to a body of a clause into an instruction listing in WAM.
38 | *
39 | * The name of the clause containing the body, and the position of the body within this clause are passed as
40 | * arguments, mainly so that these coordinates can be used to help make any labels generated within the generated
41 | * code unique.
42 | *
43 | * @param expression The clause body to compile.
44 | * @param isFirstBody true iff this is the first body of a program clause.
45 | * @param clauseName The name of the clause within which this body appears.
46 | * @param bodyNumber The body position within the containing clause.
47 | *
48 | * @return A listing of the instructions for the clause body in the WAM instruction set.
49 | */
50 | SizeableLinkedList compileBodyArguments(Functor expression, boolean isFirstBody,
51 | FunctorName clauseName, int bodyNumber);
52 |
53 | /**
54 | * Compiles a call to a body of a clause into an instruction listing in WAM.
55 | *
56 | * @param expression The body functor to call.
57 | * @param isFirstBody Iff this is the first body in a clause.
58 | * @param isLastBody Iff this is the last body in a clause.
59 | * @param chainRule Iff the clause is a chain rule, so has no environment frame.
60 | * @param permVarsRemaining The number of permanent variables remaining at this point in the calling clause. Used
61 | * for environment trimming.
62 | *
63 | * @return A list of instructions for the body call.
64 | */
65 | SizeableLinkedList compileBodyCall(Functor expression, boolean isFirstBody, boolean isLastBody,
66 | boolean chainRule, int permVarsRemaining);
67 | }
68 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/ByteCodePrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledPredicate;
23 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledQuery;
24 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
25 | import com.thesett.aima.logic.fol.wam.compiler.WAMOptimizeableListing;
26 | import com.thesett.common.util.doublemaps.SymbolTable;
27 | import com.thesett.text.api.model.TextTableModel;
28 |
29 | /**
30 | * ByteCodePrinter prints the compiled bytecode.
31 | *
32 | *
CRC Card
33 | * | Responsibilities | Collaborations
34 | * |
|---|
| Print the compiled byte code.
35 | * |
36 | *
37 | * @author Rupert Smith
38 | */
39 | public class ByteCodePrinter extends BasePrinter
40 | {
41 | /**
42 | * Creates a printer.
43 | *
44 | * @param interner The name interner.
45 | * @param symbolTable The compiler symbol table.
46 | * @param traverser The positional context traverser.
47 | * @param column The column to print to.
48 | * @param table The table to inform of cell sizes and positions.
49 | */
50 | public ByteCodePrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
51 | PositionalTermTraverser traverser, int column, TextTableModel table)
52 | {
53 | super(interner, symbolTable, traverser, column, table);
54 | }
55 |
56 | /** {@inheritDoc} */
57 | protected void enterClause(Clause clause)
58 | {
59 | if (clause instanceof WAMCompiledQuery)
60 | {
61 | WAMOptimizeableListing query = (WAMCompiledQuery) clause;
62 |
63 | for (WAMInstruction instruction : query.getInstructions())
64 | {
65 | addLineToRow(instruction.toString());
66 | nextRow();
67 | }
68 | }
69 | }
70 |
71 | /** {@inheritDoc} */
72 | protected void enterPredicate(Predicate predicate)
73 | {
74 | if (predicate instanceof WAMCompiledPredicate)
75 | {
76 | WAMOptimizeableListing compiledPredicate = (WAMCompiledPredicate) predicate;
77 |
78 | for (WAMInstruction instruction : compiledPredicate.getInstructions())
79 | {
80 | addLineToRow(instruction.toString());
81 | nextRow();
82 | }
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/LabelPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.common.util.doublemaps.DoubleKeyedMap;
23 | import com.thesett.common.util.doublemaps.SymbolTable;
24 |
25 | /**
26 | * LabelPrinter prints labels for any bytecode instructions that are labelled.
27 | *
28 | *
CRC Card
29 | * | Responsibilities | Collaborations
30 | * |
|---|
| Print any labels on the bytecode instructions.
31 | * |
32 | *
33 | * @author Rupert Smith
34 | */
35 | public class LabelPrinter extends BasePrinter
36 | {
37 | /**
38 | * Creates a printer.
39 | *
40 | * @param interner The name interner.
41 | * @param symbolTable The compiler symbol table.
42 | * @param traverser The positional context traverser.
43 | * @param column The column to print to.
44 | * @param grid The grid to print to.
45 | * @param table The table to inform of cell sizes and positions.
46 | */
47 | public LabelPrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
48 | PositionalTermTraverser traverser, int column, DoubleKeyedMap grid, PrintingTable table)
49 | {
50 | super(interner, symbolTable, traverser, column, grid, table);
51 | }
52 |
53 | /** {@inheritDoc} */
54 | protected void enterClause(Clause clause)
55 | {
56 | if (clause instanceof L3CompiledQuery)
57 | {
58 | L3CompiledQuery query = (L3CompiledQuery) clause;
59 |
60 | for (L3Instruction instruction : query.getInstructions())
61 | {
62 | L3Label label = instruction.getLabel();
63 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
64 | nextRow();
65 | }
66 | }
67 | }
68 |
69 | /** {@inheritDoc} */
70 | protected void enterPredicate(Predicate predicate)
71 | {
72 | if (predicate instanceof L3CompiledPredicate)
73 | {
74 | L3CompiledPredicate compiledPredicate = (L3CompiledPredicate) predicate;
75 |
76 | for (L3Instruction instruction : compiledPredicate.getInstructions())
77 | {
78 | L3Label label = instruction.getLabel();
79 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
80 | nextRow();
81 | }
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/machine/WAMMemoryLayout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.machine;
17 |
18 | /**
19 | * WAMMemoryLayout holds an internal machine register file that describes the memory layout of the machine.
20 | *
21 | *
CRC Card
22 | * | Responsibilities | Collaborations
23 | * |
|---|
| Encapsulate the WAM machine internal memory layout parameters.
24 | * |
25 | *
26 | * @author Rupert Smith
27 | */
28 | public class WAMMemoryLayout
29 | {
30 | /** Holds the start of the register file within the machines data area. */
31 | public int regBase;
32 |
33 | /** Holds the size of the register file. */
34 | public int regSize;
35 |
36 | /** Holds the start of the within the machines data area. */
37 | public int heapBase;
38 |
39 | /** Holds the size of the heap. */
40 | public int heapSize;
41 |
42 | /** Holds the start of the within the machines data area. */
43 | public int stackBase;
44 |
45 | /** Holds the size of the stack. */
46 | public int stackSize;
47 |
48 | /** Holds the start of the within the machines data area. */
49 | public int trailBase;
50 |
51 | /** Holds the size of the trail. */
52 | public int trailSize;
53 |
54 | /** Holds the start of the within the machines data area. */
55 | public int pdlBase;
56 |
57 | /** Holds the size of the pdl. */
58 | public int pdlSize;
59 |
60 | /**
61 | * Creates an instance of the WAM memory layout.
62 | *
63 | * @param regBase The start of the register file within the machines data area.
64 | * @param regSize The size of the register file.
65 | * @param heapBase The start of the within the machines data area.
66 | * @param heapSize The size of the heap.
67 | * @param stackBase the start of the within the machines data area.
68 | * @param stackSize the size of the stack.
69 | * @param trailBase the start of the within the machines data area.
70 | * @param trailSize the size of the trail.
71 | * @param pdlBase The start of the within the machines data area.
72 | * @param pdlSize The size of the pdl.
73 | */
74 | public WAMMemoryLayout(int regBase, int regSize, int heapBase, int heapSize, int stackBase, int stackSize,
75 | int trailBase, int trailSize, int pdlBase, int pdlSize)
76 | {
77 | this.regBase = regBase;
78 | this.regSize = regSize;
79 | this.heapBase = heapBase;
80 | this.heapSize = heapSize;
81 | this.stackBase = stackBase;
82 | this.stackSize = stackSize;
83 | this.trailBase = trailBase;
84 | this.trailSize = trailSize;
85 | this.pdlBase = pdlBase;
86 | this.pdlSize = pdlSize;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/UnoptimizedByteCodePrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledPredicate;
23 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledQuery;
24 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
25 | import com.thesett.aima.logic.fol.wam.compiler.WAMOptimizeableListing;
26 | import com.thesett.common.util.doublemaps.SymbolTable;
27 | import com.thesett.text.api.model.TextTableModel;
28 |
29 | /**
30 | * ByteCodePrinter prints the compiled bytecode in its unoptimzed state.
31 | *
32 | *
CRC Card
33 | * | Responsibilities | Collaborations
34 | * |
|---|
| Print the unoptimized compiled byte code.
35 | * |
36 | *
37 | * @author Rupert Smith
38 | */
39 | public class UnoptimizedByteCodePrinter extends BasePrinter
40 | {
41 | /**
42 | * Creates a printer.
43 | *
44 | * @param interner The name interner.
45 | * @param symbolTable The compiler symbol table.
46 | * @param traverser The positional context traverser.
47 | * @param column The column to print to.
48 | * @param table The table to inform of cell sizes and positions.
49 | */
50 | public UnoptimizedByteCodePrinter(VariableAndFunctorInterner interner,
51 | SymbolTable symbolTable, PositionalTermTraverser traverser, int column,
52 | TextTableModel table)
53 | {
54 | super(interner, symbolTable, traverser, column, table);
55 | }
56 |
57 | /** {@inheritDoc} */
58 | protected void enterClause(Clause clause)
59 | {
60 | if (clause instanceof WAMCompiledQuery)
61 | {
62 | WAMOptimizeableListing query = (WAMCompiledQuery) clause;
63 |
64 | for (WAMInstruction instruction : query.getUnoptimizedInstructions())
65 | {
66 | addLineToRow(instruction.toString());
67 | nextRow();
68 | }
69 | }
70 | }
71 |
72 | /** {@inheritDoc} */
73 | protected void enterPredicate(Predicate predicate)
74 | {
75 | if (predicate instanceof WAMCompiledPredicate)
76 | {
77 | WAMOptimizeableListing compiledPredicate = (WAMCompiledPredicate) predicate;
78 |
79 | for (WAMInstruction instruction : compiledPredicate.getUnoptimizedInstructions())
80 | {
81 | addLineToRow(instruction.toString());
82 | nextRow();
83 | }
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/etc/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/etc/project.properties:
--------------------------------------------------------------------------------
1 | #build.compiler=jikes
2 | #maven.compile.compilerargs=+Z0
3 |
4 | # Use 1.5 and print out warnings
5 | maven.compile.target = 1.5
6 | maven.compile.source = 1.5
7 | #maven.compile.compilerargs = -Xlint:deprecation -Xlint:unchecked
8 |
9 | # Generate the custom javaodc tag todo
10 | maven.javadoc.customtags = todo
11 | todo.name = todo
12 | todo.description = To do:
13 | todo.enabled = true
14 |
15 | # Set up log4j for testing.
16 | log4j.configuration=file:${basedir}/../mavenbuild/log4j.xml
17 |
18 | # Tell junit tests to use the default log4j.
19 | maven.junit.sysproperties=log4j.configuration
20 |
21 | # Run junit with a debug port open in a forked JVM.
22 | maven.junit.fork=true
23 | #maven.junit.jvmargs=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_shmem,server=n,address=junit,suspend=y
24 |
25 | # Tell clover to output the raw .xml report so that the dashboard can re-use it without running clover again.
26 | maven.clover.report.xml=true
27 |
28 | # Tell multiproject to work in this directory and to ignore this project.
29 | maven.multiproject.basedir=..
30 | maven.multiproject.excludes=mavenbuild/project.xml
31 |
32 | # Set up JBoss
33 | maven.jboss.home=/usr/local/jboss-4.0.5.GA
34 | maven.jboss.version=4.0
35 | maven.jboss.appconf.dir=${basedir}/etc/jboss/conf
36 | maven.jboss.deploy.dir=${basedir}/etc/jboss/deploy
37 | maven.jboss.conf.name=myjboss
38 | maven.jboss.default.lib.dir=${maven.jboss.home}/server/all/lib
39 | maven.jboss.default.conf.dir=${maven.jboss.home}/server/all/conf
40 | maven.jboss.default.deploy.dir=${maven.jboss.home}/server/all/deploy
41 |
42 | # Set up the dashboard to run in the parent directory and to ignore this project.
43 | maven.dashboard.basedir=..
44 | maven.dashboard.excludes=mavenbuild/project.xml,junitppmod-maven-plugin/project.xml,maven-jboss-plugin/project.xml,thesetear/project.xml,appserver-maven-plugin/project.xml,cactus-maven-plugin/project.xml,index-ra-test/project.xml,javasource-maven-plugin/project.xml,maven-jaxb-plugin/project.xml
45 | maven.dashboard.rungoals=false
46 | #csall commented out until checkstyle plugin reports are working.
47 | #maven.dashboard.aggregators=csall,junitpassrate,junittests,juniterrors,junitfailures,clovertpc,cloverncloc
48 | maven.dashboard.aggregators=junitpassrate,junittests,juniterrors,junitfailures,clovertpc,cloverncloc
49 |
50 | # Set up XDoclet to use customized templates instead of its defaults.
51 | maven.xdoclet.ejbdoclet.remotefacade.0.templateFile=${basedir}/../mavenbuild/etc/xdoclet/remotefacade.xdt
52 | maven.xdoclet.ejbdoclet.utilobject.0.templateFile=${basedir}/../mavenbuild/etc/xdoclet/lookup.xdt
53 | maven.xdoclet.ejbdoclet.jboss.0.jbossTemplateFile=${basedir}/../mavenbuild/etc/xdoclet/jboss_xml.xdt
54 |
55 | # Set up Jalopy so that it can find its customized configuration file.
56 | maven.jalopy.style=${basedir}/../mavenbuild/etc/jalopy_config/settings.dat
57 |
58 | # Set up check style for the coding standards.
59 | maven.checkstyle.properties=${basedir}/../mavenbuild/coding_standards.xml
60 | maven.checkstyle.check.tests=false
61 |
62 | # Set up checkstyle to fail the build on checkstyle to check that the license is included in all files.
63 | # The fail on violation property is set from the custom checkstlyeall goal in the maven.xml so that it does
64 | # not cause the site generation to fail on checkstyle errors.
65 | maven.checkstyle.header.file=${basedir}/../mavenbuild/etc/license/license.txt
66 |
67 | maven.scm.username=rupert
68 | maven.scm.password=zzxxza
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2JavaInterpreter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import java.io.PrintStream;
19 |
20 | import com.thesett.aima.logic.fol.Clause;
21 | import com.thesett.aima.logic.fol.LogicCompiler;
22 | import com.thesett.aima.logic.fol.Parser;
23 | import com.thesett.aima.logic.fol.interpreter.ResolutionEngine;
24 | import com.thesett.aima.logic.fol.interpreter.ResolutionInterpreter;
25 | import com.thesett.aima.logic.fol.isoprologparser.SentenceParser;
26 | import com.thesett.aima.logic.fol.isoprologparser.Token;
27 | import com.thesett.aima.logic.fol.isoprologparser.TokenSource;
28 | import com.thesett.common.util.doublemaps.SymbolTableImpl;
29 |
30 | /**
31 | * PrologInterpreter builds an interactive resolving interpreter using the interpreted resolution engine
32 | * {@link L2ResolvingJavaMachine}.
33 | *
34 | *
CRC Card
35 | * | Responsibilities | Collaborations
36 | * |
|---|
| Create an interpreter for L2.
37 | * | {@link SentenceParser}, {@link L2Compiler}, {@link L2ResolvingJavaMachine}
38 | * |
39 | *
40 | * @author Rupert Smith
41 | */
42 | public class L2JavaInterpreter
43 | {
44 | /** Used for debugging purposes. */
45 | /* private static final Logger log = Logger.getLogger(L2JavaInterpreter.class.getName()); */
46 |
47 | /**
48 | * Creates the interpreter and launches its top-level run loop.
49 | *
50 | * @param args The command line arguments.
51 | */
52 | public static void main(String[] args)
53 | {
54 | try
55 | {
56 | final L2ResolvingMachine machine = new L2ResolvingJavaMachine();
57 |
58 | Parser parser = new SentenceParser(machine);
59 | parser.setTokenSource(TokenSource.getTokenSourceForInputStream(System.in));
60 |
61 | LogicCompiler compiler =
62 | new L2Compiler(new SymbolTableImpl(), machine);
63 |
64 | ResolutionEngine engine =
65 | new ResolutionEngine(parser, machine, compiler, machine)
66 | {
67 | public void reset()
68 | {
69 | machine.reset();
70 | }
71 | };
72 |
73 | ResolutionInterpreter interpreter =
74 | new ResolutionInterpreter(engine);
75 |
76 | interpreter.interpreterLoop();
77 | }
78 | catch (Exception e)
79 | {
80 | /*log.log(java.util.logging.Level.SEVERE, e.getMessage(), e);*/
81 | e.printStackTrace(new PrintStream(System.err));
82 | System.exit(-1);
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/L2NativeInterpreter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import java.io.PrintStream;
19 |
20 | import com.thesett.aima.logic.fol.Clause;
21 | import com.thesett.aima.logic.fol.LogicCompiler;
22 | import com.thesett.aima.logic.fol.Parser;
23 | import com.thesett.aima.logic.fol.interpreter.ResolutionEngine;
24 | import com.thesett.aima.logic.fol.interpreter.ResolutionInterpreter;
25 | import com.thesett.aima.logic.fol.isoprologparser.SentenceParser;
26 | import com.thesett.aima.logic.fol.isoprologparser.Token;
27 | import com.thesett.aima.logic.fol.isoprologparser.TokenSource;
28 | import com.thesett.common.util.doublemaps.SymbolTableImpl;
29 |
30 | /**
31 | * PrologInterpreter builds an interactive resolving interpreter using the interpreted resolution engine
32 | * {@link L2ResolvingNativeMachine}.
33 | *
34 | *
CRC Card
35 | * | Responsibilities | Collaborations
36 | * |
|---|
| Create an interpreter for L2.
37 | * | {@link SentenceParser}, {@link L2Compiler}, {@link L2ResolvingNativeMachine}
38 | * |
39 | *
40 | * @author Rupert Smith
41 | */
42 | public class L2NativeInterpreter
43 | {
44 | /** Used for debugging purposes. */
45 | /* private static final Logger log = Logger.getLogger(L2NativeInterpreter.class.getName()); */
46 |
47 | /**
48 | * Creates the interpreter and launches its top-level run loop.
49 | *
50 | * @param args The command line arguments.
51 | */
52 | public static void main(String[] args)
53 | {
54 | try
55 | {
56 | final L2ResolvingMachine machine = new L2ResolvingNativeMachine();
57 |
58 | Parser parser = new SentenceParser(machine);
59 | parser.setTokenSource(TokenSource.getTokenSourceForInputStream(System.in));
60 |
61 | LogicCompiler compiler =
62 | new L2Compiler(new SymbolTableImpl(), machine);
63 |
64 | ResolutionEngine engine =
65 | new ResolutionEngine(parser, machine, compiler, machine)
66 | {
67 | public void reset()
68 | {
69 | machine.reset();
70 | }
71 | };
72 |
73 | ResolutionInterpreter interpreter =
74 | new ResolutionInterpreter(engine);
75 |
76 | interpreter.interpreterLoop();
77 | }
78 | catch (Exception e)
79 | {
80 | /*log.log(java.util.logging.Level.SEVERE, e.getMessage(), e);*/
81 | e.printStackTrace(new PrintStream(System.err));
82 | System.exit(-1);
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/LabelPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledPredicate;
23 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledQuery;
24 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
25 | import com.thesett.aima.logic.fol.wam.compiler.WAMLabel;
26 | import com.thesett.aima.logic.fol.wam.compiler.WAMOptimizeableListing;
27 | import com.thesett.common.util.doublemaps.SymbolTable;
28 | import com.thesett.text.api.model.TextTableModel;
29 |
30 | /**
31 | * LabelPrinter prints labels for any bytecode instructions that are labelled.
32 | *
33 | *
CRC Card
34 | * | Responsibilities | Collaborations
35 | * |
|---|
| Print any labels on the bytecode instructions.
36 | * |
37 | *
38 | * @author Rupert Smith
39 | */
40 | public class LabelPrinter extends BasePrinter
41 | {
42 | /**
43 | * Creates a printer.
44 | *
45 | * @param interner The name interner.
46 | * @param symbolTable The compiler symbol table.
47 | * @param traverser The positional context traverser.
48 | * @param column The column to print to.
49 | * @param table The table to inform of cell sizes and positions.
50 | */
51 | public LabelPrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
52 | PositionalTermTraverser traverser, int column, TextTableModel table)
53 | {
54 | super(interner, symbolTable, traverser, column, table);
55 | }
56 |
57 | /** {@inheritDoc} */
58 | protected void enterClause(Clause clause)
59 | {
60 | if (clause instanceof WAMCompiledQuery)
61 | {
62 | WAMOptimizeableListing query = (WAMCompiledQuery) clause;
63 |
64 | for (WAMInstruction instruction : query.getInstructions())
65 | {
66 | WAMLabel label = instruction.getLabel();
67 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
68 | nextRow();
69 | }
70 | }
71 | }
72 |
73 | /** {@inheritDoc} */
74 | protected void enterPredicate(Predicate predicate)
75 | {
76 | if (predicate instanceof WAMCompiledPredicate)
77 | {
78 | WAMOptimizeableListing compiledPredicate = (WAMCompiledPredicate) predicate;
79 |
80 | for (WAMInstruction instruction : compiledPredicate.getInstructions())
81 | {
82 | WAMLabel label = instruction.getLabel();
83 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
84 | nextRow();
85 | }
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/UnoptimizedLabelPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.Predicate;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledPredicate;
23 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledQuery;
24 | import com.thesett.aima.logic.fol.wam.compiler.WAMInstruction;
25 | import com.thesett.aima.logic.fol.wam.compiler.WAMLabel;
26 | import com.thesett.aima.logic.fol.wam.compiler.WAMOptimizeableListing;
27 | import com.thesett.common.util.doublemaps.SymbolTable;
28 | import com.thesett.text.api.model.TextTableModel;
29 |
30 | /**
31 | * LabelPrinter prints labels for any bytecode instructions that are labelled.
32 | *
33 | *
CRC Card
34 | * | Responsibilities | Collaborations
35 | * |
|---|
| Print any labels on the bytecode instructions.
36 | * |
37 | *
38 | * @author Rupert Smith
39 | */
40 | public class UnoptimizedLabelPrinter extends BasePrinter
41 | {
42 | /**
43 | * Creates a printer.
44 | *
45 | * @param interner The name interner.
46 | * @param symbolTable The compiler symbol table.
47 | * @param traverser The positional context traverser.
48 | * @param column The column to print to.
49 | * @param table The table to inform of cell sizes and positions.
50 | */
51 | public UnoptimizedLabelPrinter(VariableAndFunctorInterner interner,
52 | SymbolTable symbolTable, PositionalTermTraverser traverser, int column,
53 | TextTableModel table)
54 | {
55 | super(interner, symbolTable, traverser, column, table);
56 | }
57 |
58 | /** {@inheritDoc} */
59 | protected void enterClause(Clause clause)
60 | {
61 | if (clause instanceof WAMCompiledQuery)
62 | {
63 | WAMOptimizeableListing query = (WAMCompiledQuery) clause;
64 |
65 | for (WAMInstruction instruction : query.getUnoptimizedInstructions())
66 | {
67 | WAMLabel label = instruction.getLabel();
68 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
69 | nextRow();
70 | }
71 | }
72 | }
73 |
74 | /** {@inheritDoc} */
75 | protected void enterPredicate(Predicate predicate)
76 | {
77 | if (predicate instanceof WAMCompiledPredicate)
78 | {
79 | WAMOptimizeableListing compiledPredicate = (WAMCompiledPredicate) predicate;
80 |
81 | for (WAMInstruction instruction : compiledPredicate.getUnoptimizedInstructions())
82 | {
83 | WAMLabel label = instruction.getLabel();
84 | addLineToRow((label != null) ? (label.toPrettyString() + ":") : "");
85 | nextRow();
86 | }
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/BasePrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.BasePositionalVisitor;
19 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
20 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
21 | import com.thesett.common.util.TraceIndenter;
22 | import com.thesett.common.util.doublemaps.SymbolTable;
23 | import com.thesett.text.api.model.TextTableModel;
24 |
25 | /**
26 | * BasePrinter is a base class for writing debug pretty printers for compiled clauses.
27 | *
28 | * It uses positional context information from a {@link PositionalTermTraverser}, to determine whether terms are
29 | * being entered or left, and splits these down into calls on appropriate enter/leave methods. Default no-op
30 | * implementations of these methods are supplied by this base class and may be extended by specific printers to append
31 | * data into table cells, using the {@link #addLineToRow(String)} and {@link #nextRow()} methods.
32 | *
33 | *
CRC Card
34 | * | Responsibilities | Collaborations
35 | * |
|---|
| Provide default enter/leave methods for every part of a term.
36 | * |
| Accept lines of data appended into table cells.
37 | * |
38 | *
39 | * @author Rupert Smith
40 | */
41 | public class BasePrinter extends BasePositionalVisitor
42 | {
43 | /** Defines the column that this printer outputs to. */
44 | private final int currentColumn;
45 |
46 | /** Holds the current row this printer is outputting to. */
47 | protected int currentRow;
48 |
49 | /** The context depth indenter. */
50 | protected TraceIndenter indenter = new TraceIndenter(true);
51 |
52 | /** The current indent. */
53 | protected String indent = "";
54 |
55 | /** The grid of cells to output to. */
56 | private final TextTableModel table;
57 |
58 | /**
59 | * Creates a printer.
60 | *
61 | * @param interner The name interner.
62 | * @param symbolTable The compiler symbol table.
63 | * @param traverser The positional context traverser.
64 | * @param column The column to print to.
65 | * @param table The table to inform of cell sizes and positions.
66 | */
67 | public BasePrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
68 | PositionalTermTraverser traverser, int column, TextTableModel table)
69 | {
70 | super(interner, symbolTable, traverser);
71 | this.currentColumn = column;
72 | this.table = table;
73 | }
74 |
75 | /**
76 | * Adds a line of data to the current cell.
77 | *
78 | * @param line The line to print in the current cell.
79 | */
80 | protected void addLineToRow(String line)
81 | {
82 | table.put(currentColumn, currentRow, line);
83 | }
84 |
85 | /** Moves on to the next cell in the next row of the table. */
86 | protected void nextRow()
87 | {
88 | ++currentRow;
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/L3Label.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.FunctorName;
19 |
20 | /**
21 | * L3Label is a code label within a predicate.
22 | *
23 | *
CRC Card
24 | * | Responsibilities | Collaborations
25 | * |
|---|
| Represent a code label uniquely identifying it by its containing predicate name, arity, and id.
26 | * |
| Provide equality checking that considers labels with identical name, arity and id to be equal.
27 | * |
28 | *
29 | * @author Rupert Smith
30 | */
31 | public class L3Label extends FunctorName
32 | {
33 | /** Holds the label identifier. */
34 | private final int id;
35 |
36 | /**
37 | * Creates a label within a parent predicate.
38 | *
39 | * @param parent The name of the parent predicate.
40 | * @param id The unique label id.
41 | */
42 | public L3Label(FunctorName parent, int id)
43 | {
44 | super(parent.getName(), parent.getArity());
45 | this.id = id;
46 | }
47 |
48 | /**
49 | * Provides the labels id.
50 | *
51 | * @return The labels id.
52 | */
53 | public int getId()
54 | {
55 | return id;
56 | }
57 |
58 | /** {@inheritDoc} */
59 | public boolean equals(Object o)
60 | {
61 | if (this == o)
62 | {
63 | return true;
64 | }
65 |
66 | if ((o == null) || (getClass() != o.getClass()))
67 | {
68 | return false;
69 | }
70 |
71 | if (!super.equals(o))
72 | {
73 | return false;
74 | }
75 |
76 | L3Label l3Label = (L3Label) o;
77 |
78 | if (arity != l3Label.arity)
79 | {
80 | return false;
81 | }
82 |
83 | if (id != l3Label.id)
84 | {
85 | return false;
86 | }
87 |
88 | if ((name != null) ? (!name.equals(l3Label.name)) : (l3Label.name != null))
89 | {
90 | return false;
91 | }
92 |
93 | return true;
94 | }
95 |
96 | /** {@inheritDoc} */
97 | public int hashCode()
98 | {
99 | int result = super.hashCode();
100 | result = (31 * result) + id;
101 | result = (31 * result) + ((name != null) ? name.hashCode() : 0);
102 | result = (31 * result) + arity;
103 |
104 | return result;
105 | }
106 |
107 | /**
108 | * Pretty prints the label in a standard format.
109 | *
110 | * @return The label pretty printed in a standard format.
111 | */
112 | public String toPrettyString()
113 | {
114 | return (getName() + "/" + getArity() + "_" + getId());
115 | }
116 |
117 | /**
118 | * Outputs the label as a functor name and id, used mainly for debugging purposes.
119 | *
120 | * @return The label.
121 | */
122 | public String toString()
123 | {
124 | return "L3Label: [ name = " + name + ", arity = " + arity + ", id = " + id + " ]";
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/WAMLabel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | import com.thesett.aima.logic.fol.FunctorName;
19 |
20 | /**
21 | * WAMLabel is a code label within a predicate.
22 | *
23 | *
CRC Card
24 | * | Responsibilities | Collaborations
25 | * |
|---|
| Represent a code label uniquely identifying it by its containing predicate name, arity, and id.
26 | * |
| Provide equality checking that considers labels with identical name, arity and id to be equal.
27 | * |
28 | *
29 | * @author Rupert Smith
30 | */
31 | public class WAMLabel extends FunctorName
32 | {
33 | /** Holds the label identifier. */
34 | private final int id;
35 |
36 | /**
37 | * Creates a label within a parent predicate.
38 | *
39 | * @param parent The name of the parent predicate.
40 | * @param id The unique label id.
41 | */
42 | public WAMLabel(FunctorName parent, int id)
43 | {
44 | super(parent.getName(), parent.getArity());
45 | this.id = id;
46 | }
47 |
48 | /**
49 | * Provides the labels id.
50 | *
51 | * @return The labels id.
52 | */
53 | public int getId()
54 | {
55 | return id;
56 | }
57 |
58 | /** {@inheritDoc} */
59 | public boolean equals(Object o)
60 | {
61 | if (this == o)
62 | {
63 | return true;
64 | }
65 |
66 | if ((o == null) || (getClass() != o.getClass()))
67 | {
68 | return false;
69 | }
70 |
71 | if (!super.equals(o))
72 | {
73 | return false;
74 | }
75 |
76 | WAMLabel l3Label = (WAMLabel) o;
77 |
78 | if (arity != l3Label.arity)
79 | {
80 | return false;
81 | }
82 |
83 | if (id != l3Label.id)
84 | {
85 | return false;
86 | }
87 |
88 | if ((name != null) ? (!name.equals(l3Label.name)) : (l3Label.name != null))
89 | {
90 | return false;
91 | }
92 |
93 | return true;
94 | }
95 |
96 | /** {@inheritDoc} */
97 | public int hashCode()
98 | {
99 | int result = super.hashCode();
100 | result = (31 * result) + id;
101 | result = (31 * result) + ((name != null) ? name.hashCode() : 0);
102 | result = (31 * result) + arity;
103 |
104 | return result;
105 | }
106 |
107 | /**
108 | * Pretty prints the label in a standard format.
109 | *
110 | * @return The label pretty printed in a standard format.
111 | */
112 | public String toPrettyString()
113 | {
114 | return (getName() + "/" + getArity() + "_" + getId());
115 | }
116 |
117 | /**
118 | * Outputs the label as a functor name and id, used mainly for debugging purposes.
119 | *
120 | * @return The label.
121 | */
122 | public String toString()
123 | {
124 | return "WAMLabel: [ name = " + name + ", arity = " + arity + ", id = " + id + " ]";
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/l2/src/main/com/thesett/aima/logic/fol/l2/PositionPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l2;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.Variable;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import static com.thesett.aima.logic.fol.l2.L2Instruction.STACK_ADDR;
23 | import com.thesett.common.util.doublemaps.DoubleKeyedMap;
24 | import com.thesett.common.util.doublemaps.SymbolTable;
25 |
26 | /**
27 | * ByteCodePrinter prints the byte code for each compiled clause, aligned with the predicate calls and arguments leading
28 | * to its generation.
29 | *
30 | *
CRC Card
31 | * | Responsibilities | Collaborations
32 | * |
|---|
| Print the compiled byte code aligned to the source.
33 | * |
34 | *
35 | * @author Rupert Smith
36 | */
37 | public class PositionPrinter extends BasePrinter
38 | {
39 | /**
40 | * Creates a printer.
41 | *
42 | * @param interner The name interner.
43 | * @param symbolTable The compiler symbol table.
44 | * @param traverser The positional context traverser.
45 | * @param column The column to print to.
46 | * @param grid The grid to print to.
47 | * @param table The table to inform of cell sizes and positions.
48 | */
49 | public PositionPrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
50 | PositionalTermTraverser traverser, int column, DoubleKeyedMap grid, PrintingTable table)
51 | {
52 | super(interner, symbolTable, traverser, column, grid, table);
53 | }
54 |
55 | /** {@inheritDoc} */
56 | protected void enterFunctor(Functor functor)
57 | {
58 | String head = traverser.isInHead() ? "/head" : "";
59 | String last = traverser.isLastBodyFunctor() ? "/last" : "";
60 | String symKey = functor.getSymbolKey().toString();
61 |
62 | if (traverser.isTopLevel())
63 | {
64 | addLineToRow("functor(" + symKey + ")" + head + last);
65 | }
66 | else
67 | {
68 | addLineToRow("arg(" + symKey + ")");
69 | }
70 |
71 | nextRow();
72 | }
73 |
74 | /** {@inheritDoc} */
75 | protected void leaveFunctor(Functor functor)
76 | {
77 | nextRow();
78 | }
79 |
80 | /** {@inheritDoc} */
81 | protected void enterVariable(Variable variable)
82 | {
83 | Integer allocation = (Integer) symbolTable.get(variable.getSymbolKey(), "allocation");
84 | String symKey = variable.getSymbolKey().toString();
85 |
86 | String allocString = "";
87 |
88 | if (allocation != null)
89 | {
90 | int slot = (allocation & (0xff));
91 | int mode = allocation >> 8;
92 |
93 | allocString = ((mode == STACK_ADDR) ? "Y" : "X") + slot;
94 | }
95 |
96 | addLineToRow("arg/var(" + symKey + ") " + allocString);
97 | nextRow();
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/indexing/IntIntOpenLinearTable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.indexing;
17 |
18 | import java.nio.ByteBuffer;
19 |
20 | /**
21 | * IntIntOpenLinearTable implements a {@link CodeBufferTable} using open addressing and linear probing with a step size
22 | * of one.
23 | *
24 | *
CRC Card
25 | * | Responsibilities | Collaborations
26 | * |
|---|
| Establish a table on a ByteBuffer. |
27 | * | Associate an address with a key. |
28 | * | Look up an address by key. |
29 | *
30 | *
31 | * @author Rupert Smith
32 | */
33 | public class IntIntOpenLinearTable implements CodeBufferTable
34 | {
35 | /** The byte buffer holding the table data. */
36 | private ByteBuffer buffer;
37 |
38 | /** The offset of the base of the table within the byte buffer. */
39 | private int offset;
40 |
41 | /** The size of the table in number of entries (not bytes or integers). */
42 | private int size;
43 |
44 | /** {@inheritDoc} */
45 | public void setup(ByteBuffer buffer, int t, int n)
46 | {
47 | this.buffer = buffer;
48 | this.offset = t;
49 | this.size = (n >> 3); // Shift by 3 as key and value must be stored as a pair, making 8 bytes per entry.
50 | }
51 |
52 | /** {@inheritDoc} */
53 | public int get(int key)
54 | {
55 | int addr = addr(hash(key));
56 |
57 | while (true)
58 | {
59 | int tableKey = buffer.getInt(addr);
60 |
61 | if (key == tableKey)
62 | {
63 | return buffer.getInt(addr + 4);
64 | }
65 |
66 | if (tableKey == 0)
67 | {
68 | return 0;
69 | }
70 |
71 | addr += 8;
72 | }
73 | }
74 |
75 | /** {@inheritDoc} */
76 | public void put(int key, int val)
77 | {
78 | int addr = addr(hash(key));
79 |
80 | while (true)
81 | {
82 | int tableKey = buffer.getInt(addr);
83 |
84 | if (key == tableKey)
85 | {
86 | break;
87 | }
88 |
89 | if (tableKey == 0)
90 | {
91 | break;
92 | }
93 |
94 | addr += 8;
95 | }
96 |
97 | buffer.putInt(addr, key);
98 | buffer.putInt(addr + 4, val);
99 | }
100 |
101 | /**
102 | * Converts an entry offset into the table, into an address within the table, relative to the zero offset of the
103 | * underlying byte buffer.
104 | *
105 | * @param entry The entry offset.
106 | *
107 | * @return The address within the byte buffer at which the entry can be found.
108 | */
109 | private int addr(int entry)
110 | {
111 | return offset + ((entry % size) << 3);
112 | }
113 |
114 | /**
115 | * Computes a hash of the key.
116 | *
117 | * @param key The key to hash.
118 | *
119 | * @return The hashed key.
120 | */
121 | private int hash(int key)
122 | {
123 | return key;
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/interpreter/WAMJavaInterpreter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.interpreter;
17 |
18 | import java.io.PrintStream;
19 |
20 | import com.thesett.aima.logic.fol.Clause;
21 | import com.thesett.aima.logic.fol.LogicCompiler;
22 | import com.thesett.aima.logic.fol.Parser;
23 | import com.thesett.aima.logic.fol.interpreter.InteractiveParser;
24 | import com.thesett.aima.logic.fol.interpreter.ResolutionEngine;
25 | import com.thesett.aima.logic.fol.interpreter.ResolutionInterpreter;
26 | import com.thesett.aima.logic.fol.isoprologparser.SentenceParser;
27 | import com.thesett.aima.logic.fol.isoprologparser.Token;
28 | import com.thesett.aima.logic.fol.wam.compiler.InstructionCompiler;
29 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledPredicate;
30 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiledQuery;
31 | import com.thesett.aima.logic.fol.wam.compiler.WAMCompiler;
32 | import com.thesett.aima.logic.fol.wam.machine.WAMEngine;
33 | import com.thesett.aima.logic.fol.wam.machine.WAMResolvingJavaMachine;
34 | import com.thesett.aima.logic.fol.wam.machine.WAMResolvingMachine;
35 | import com.thesett.common.util.doublemaps.SymbolTableImpl;
36 |
37 | /**
38 | * PrologInterpreter builds an interactive resolving interpreter using the interpreted resolution engine
39 | * {@link WAMResolvingJavaMachine}.
40 | *
41 | *
CRC Card
42 | * | Responsibilities | Collaborations
43 | * |
|---|
| Create an interpreter for WAM.
44 | * | {@link SentenceParser}, {@link InstructionCompiler}, {@link WAMResolvingJavaMachine}
45 | * |
46 | *
47 | * @author Rupert Smith
48 | */
49 | public class WAMJavaInterpreter
50 | {
51 | /** Used for debugging purposes. */
52 | /* private static final Logger log = Logger.getLogger(WAMJavaInterpreter.class.getName()); */
53 |
54 | /**
55 | * Creates the interpreter and launches its top-level run loop.
56 | *
57 | * @param args The command line arguments.
58 | */
59 | public static void main(String[] args)
60 | {
61 | try
62 | {
63 | SymbolTableImpl symbolTable = new SymbolTableImpl();
64 |
65 | WAMResolvingMachine machine = new WAMResolvingJavaMachine(symbolTable);
66 |
67 | Parser parser = new InteractiveParser(machine);
68 |
69 | LogicCompiler compiler =
70 | new WAMCompiler(symbolTable, machine);
71 |
72 | ResolutionEngine engine =
73 | new WAMEngine(parser, machine, compiler, machine);
74 | engine.reset();
75 |
76 | ResolutionInterpreter interpreter =
77 | new ResolutionInterpreter(engine);
78 |
79 | interpreter.interpreterLoop();
80 | }
81 | catch (Exception e)
82 | {
83 | /*log.log(java.util.logging.Level.SEVERE, e.getMessage(), e);*/
84 | e.printStackTrace(new PrintStream(System.err));
85 | System.exit(-1);
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/printer/PositionPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.printer;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.Variable;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import static com.thesett.aima.logic.fol.wam.compiler.WAMInstruction.STACK_ADDR;
23 | import com.thesett.common.util.doublemaps.SymbolTable;
24 | import com.thesett.text.api.model.TextTableModel;
25 |
26 | /**
27 | * PositionPrinter prints some positional context information about functors and how they relate to their compiled form.
28 | * The position printer will prints the name of the functor, argument or variable and whether the current position is
29 | * within the functor head or last body element.
30 | *
31 | *
CRC Card
32 | * | Responsibilities | Collaborations
33 | * |
|---|
| Print the positional context information.
34 | * |
35 | *
36 | * @author Rupert Smith
37 | */
38 | public class PositionPrinter extends BasePrinter
39 | {
40 | /**
41 | * Creates a printer.
42 | *
43 | * @param interner The name interner.
44 | * @param symbolTable The compiler symbol table.
45 | * @param traverser The positional context traverser.
46 | * @param column The column to print to.
47 | * @param table The table to inform of cell sizes and positions.
48 | */
49 | public PositionPrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
50 | PositionalTermTraverser traverser, int column, TextTableModel table)
51 | {
52 | super(interner, symbolTable, traverser, column, table);
53 | }
54 |
55 | /** {@inheritDoc} */
56 | protected void enterFunctor(Functor functor)
57 | {
58 | String head = traverser.isInHead() ? "/head" : "";
59 | String last = traverser.isLastBodyFunctor() ? "/last" : "";
60 | String symKey = functor.getSymbolKey().toString();
61 |
62 | if (traverser.isTopLevel())
63 | {
64 | addLineToRow("functor(" + symKey + ")" + head + last);
65 | }
66 | else
67 | {
68 | addLineToRow("arg(" + symKey + ")");
69 | }
70 |
71 | nextRow();
72 | }
73 |
74 | /** {@inheritDoc} */
75 | protected void leaveFunctor(Functor functor)
76 | {
77 | nextRow();
78 | }
79 |
80 | /** {@inheritDoc} */
81 | protected void enterVariable(Variable variable)
82 | {
83 | Integer allocation = (Integer) symbolTable.get(variable.getSymbolKey(), "allocation");
84 | String symKey = variable.getSymbolKey().toString();
85 |
86 | String allocString = "";
87 |
88 | if (allocation != null)
89 | {
90 | int slot = (allocation & (0xff));
91 | int mode = allocation >> 8;
92 |
93 | allocString = ((mode == STACK_ADDR) ? "Y" : "X") + slot;
94 | }
95 |
96 | addLineToRow("arg/var(" + symKey + ") " + allocString);
97 | nextRow();
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/wam/src/main/com/thesett/aima/logic/fol/wam/compiler/WAMCompiler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.wam.compiler;
17 |
18 | import com.thesett.aima.logic.fol.Clause;
19 | import com.thesett.aima.logic.fol.LogicCompiler;
20 | import com.thesett.aima.logic.fol.LogicCompilerObserver;
21 | import com.thesett.aima.logic.fol.Sentence;
22 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
23 | import com.thesett.aima.logic.fol.bytecode.BaseMachine;
24 | import com.thesett.common.parsing.SourceCodeException;
25 | import com.thesett.common.util.doublemaps.SymbolTable;
26 |
27 | /**
28 | * WAMCompiler implements the {@link LogicCompiler} interface for the complete WAM compilation chain. It is a
29 | * supervising compiler, that chains together the work of the compiler pipe-line stages.
30 | *
31 | *
CRC Card
32 | * | Responsibilities | Collaborations
33 | * |
|---|
| Chain together the compiler pipe-line stages.
34 | * |
35 | *
36 | * @author Rupert Smith
37 | */
38 | public class WAMCompiler extends BaseMachine implements LogicCompiler
39 | {
40 | /** Holds the pre-compiler, for analyzing and transforming terms prior to compilation proper. */
41 | PreCompiler preCompiler;
42 |
43 | /** Holds the instruction generating compiler. */
44 | InstructionCompiler instructionCompiler;
45 |
46 | /**
47 | * Creates a new WAMCompiler.
48 | *
49 | * @param symbolTable The symbol table.
50 | * @param interner The machine to translate functor and variable names.
51 | */
52 | public WAMCompiler(SymbolTable symbolTable, VariableAndFunctorInterner interner)
53 | {
54 | super(symbolTable, interner);
55 |
56 | instructionCompiler = new InstructionCompiler(symbolTable, interner);
57 | preCompiler = new PreCompiler(symbolTable, interner, instructionCompiler);
58 |
59 | preCompiler.setCompilerObserver(new ClauseChainObserver());
60 | }
61 |
62 | /** {@inheritDoc} */
63 | public void compile(Sentence sentence) throws SourceCodeException
64 | {
65 | preCompiler.compile(sentence);
66 | }
67 |
68 | /** {@inheritDoc} */
69 | public void setCompilerObserver(LogicCompilerObserver observer)
70 | {
71 | instructionCompiler.setCompilerObserver(observer);
72 | }
73 |
74 | /** {@inheritDoc} */
75 | public void endScope() throws SourceCodeException
76 | {
77 | preCompiler.endScope();
78 | instructionCompiler.endScope();
79 | }
80 |
81 | /**
82 | * Chains compilation completion events onto the instruction compiler.
83 | */
84 | class ClauseChainObserver implements LogicCompilerObserver
85 | {
86 | /** {@inheritDoc} */
87 | public void onCompilation(Sentence sentence) throws SourceCodeException
88 | {
89 | instructionCompiler.compile(sentence);
90 | }
91 |
92 | /** {@inheritDoc} */
93 | public void onQueryCompilation(Sentence sentence) throws SourceCodeException
94 | {
95 | instructionCompiler.compile(sentence);
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/interpreter/L3JavaInterpreter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3.interpreter;
17 |
18 | import java.io.PrintStream;
19 |
20 | import com.thesett.aima.logic.fol.Clause;
21 | import com.thesett.aima.logic.fol.LogicCompiler;
22 | import com.thesett.aima.logic.fol.Parser;
23 | import com.thesett.aima.logic.fol.interpreter.ResolutionEngine;
24 | import com.thesett.aima.logic.fol.interpreter.ResolutionInterpreter;
25 | import com.thesett.aima.logic.fol.isoprologparser.SentenceParser;
26 | import com.thesett.aima.logic.fol.isoprologparser.Token;
27 | import com.thesett.aima.logic.fol.isoprologparser.TokenSource;
28 | import com.thesett.aima.logic.fol.l3.L3CompiledPredicate;
29 | import com.thesett.aima.logic.fol.l3.L3CompiledQuery;
30 | import com.thesett.aima.logic.fol.l3.L3Compiler;
31 | import com.thesett.aima.logic.fol.l3.L3ResolvingJavaMachine;
32 | import com.thesett.aima.logic.fol.l3.L3ResolvingMachine;
33 | import com.thesett.common.util.doublemaps.SymbolTableImpl;
34 |
35 | /**
36 | * PrologInterpreter builds an interactive resolving interpreter using the interpreted resolution engine
37 | * {@link L3ResolvingJavaMachine}.
38 | *
39 | *
CRC Card
40 | * | Responsibilities | Collaborations
41 | * |
|---|
| Create an interpreter for L3.
42 | * | {@link SentenceParser}, {@link L3Compiler}, {@link L3ResolvingJavaMachine}
43 | * |
44 | *
45 | * @author Rupert Smith
46 | */
47 | public class L3JavaInterpreter
48 | {
49 | /** Used for debugging purposes. */
50 | /* private static final Logger log = Logger.getLogger(L3JavaInterpreter.class.getName()); */
51 |
52 | /**
53 | * Creates the interpreter and launches its top-level run loop.
54 | *
55 | * @param args The command line arguments.
56 | */
57 | public static void main(String[] args)
58 | {
59 | try
60 | {
61 | SymbolTableImpl symbolTable = new SymbolTableImpl();
62 |
63 | final L3ResolvingMachine machine = new L3ResolvingJavaMachine(symbolTable);
64 |
65 | Parser parser = new SentenceParser(machine);
66 | parser.setTokenSource(TokenSource.getTokenSourceForInputStream(System.in));
67 |
68 | LogicCompiler compiler = new L3Compiler(symbolTable, machine);
69 |
70 | ResolutionEngine engine =
71 | new ResolutionEngine(parser, machine, compiler, machine)
72 | {
73 | public void reset()
74 | {
75 | machine.reset();
76 | }
77 | };
78 |
79 | ResolutionInterpreter interpreter =
80 | new ResolutionInterpreter(engine);
81 |
82 | interpreter.interpreterLoop();
83 | }
84 | catch (Exception e)
85 | {
86 | /*log.log(java.util.logging.Level.SEVERE, e.getMessage(), e);*/
87 | e.printStackTrace(new PrintStream(System.err));
88 | System.exit(-1);
89 | }
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/l3/src/main/com/thesett/aima/logic/fol/l3/PositionPrinter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright The Sett Ltd, 2005 to 2014.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.thesett.aima.logic.fol.l3;
17 |
18 | import com.thesett.aima.logic.fol.Functor;
19 | import com.thesett.aima.logic.fol.Variable;
20 | import com.thesett.aima.logic.fol.VariableAndFunctorInterner;
21 | import com.thesett.aima.logic.fol.compiler.PositionalTermTraverser;
22 | import static com.thesett.aima.logic.fol.l3.L3Instruction.STACK_ADDR;
23 | import com.thesett.common.util.doublemaps.DoubleKeyedMap;
24 | import com.thesett.common.util.doublemaps.SymbolTable;
25 |
26 | /**
27 | * PositionPrinter prints some positional context information about functors and how they relate to their compiled form.
28 | * The position printer will prints the name of the functor, argument or variable and whether the current position is
29 | * within the functor head or last body element.
30 | *
31 | *
CRC Card
32 | * | Responsibilities | Collaborations
33 | * |
|---|
| Print the positional context information.
34 | * |
35 | *
36 | * @author Rupert Smith
37 | */
38 | public class PositionPrinter extends BasePrinter
39 | {
40 | /**
41 | * Creates a printer.
42 | *
43 | * @param interner The name interner.
44 | * @param symbolTable The compiler symbol table.
45 | * @param traverser The positional context traverser.
46 | * @param column The column to print to.
47 | * @param grid The grid to print to.
48 | * @param table The table to inform of cell sizes and positions.
49 | */
50 | public PositionPrinter(VariableAndFunctorInterner interner, SymbolTable symbolTable,
51 | PositionalTermTraverser traverser, int column, DoubleKeyedMap grid, PrintingTable table)
52 | {
53 | super(interner, symbolTable, traverser, column, grid, table);
54 | }
55 |
56 | /** {@inheritDoc} */
57 | protected void enterFunctor(Functor functor)
58 | {
59 | String head = traverser.isInHead() ? "/head" : "";
60 | String last = traverser.isLastBodyFunctor() ? "/last" : "";
61 | String symKey = functor.getSymbolKey().toString();
62 |
63 | if (traverser.isTopLevel())
64 | {
65 | addLineToRow("functor(" + symKey + ")" + head + last);
66 | }
67 | else
68 | {
69 | addLineToRow("arg(" + symKey + ")");
70 | }
71 |
72 | nextRow();
73 | }
74 |
75 | /** {@inheritDoc} */
76 | protected void leaveFunctor(Functor functor)
77 | {
78 | nextRow();
79 | }
80 |
81 | /** {@inheritDoc} */
82 | protected void enterVariable(Variable variable)
83 | {
84 | Integer allocation = (Integer) symbolTable.get(variable.getSymbolKey(), "allocation");
85 | String symKey = variable.getSymbolKey().toString();
86 |
87 | String allocString = "";
88 |
89 | if (allocation != null)
90 | {
91 | int slot = (allocation & (0xff));
92 | int mode = allocation >> 8;
93 |
94 | allocString = ((mode == STACK_ADDR) ? "Y" : "X") + slot;
95 | }
96 |
97 | addLineToRow("arg/var(" + symKey + ") " + allocString);
98 | nextRow();
99 | }
100 | }
101 |
--------------------------------------------------------------------------------