SWI Prolog专家系统

时间:2014-12-18 23:34:06

标签: prolog

我尝试在SWI Prolog中实现简单的专家系统。该系统从键盘读取输入数据并查找语言。这是我的代码:

/* Paradigms */
paradigm('Ada', 'Imperative').
paradigm('C', 'Imperative').

/* Typization */
typization('Ada', 'Statical').
typization('C', 'Explicit').

/* Compiler */
compiler('Ada', 'OpenSource').
compiler('C', 'DebugPosibility').

/* Memory */
memory('Ada', 'Stack').
memory('C', 'Pointer').

language(L, P, T, C, M) :- paradigm(L, P), typization(L, T), compiler(L, C), memory(L, M).

run :- write('\nChoose language paradigm:\n1. imperative\n2. object-oriented\n3. distributed\n4. reflexive\n5. declarative\n6. functional\n7. general programming\n'), read(P),
       write('\n\nChoose typization:\n1. statical\n2. explicit\n3. polymorfism\n4. runtime type information\n5. dynamical\n6. implicit\n7. cast without data lose\n8. implicit cast without data lose\n9. argument output at method call\n'), read(T),
       write('\n\nChoose compiler type:\n1. open-source\n2. debug posibility\n3. bootstrapping\n4. multithreading compilation\n5. conditional compilation\n6. command line interpreter\n'), read(C),
       write('\n\nChoose memory management type:\n1. stack\n2. pointer\n3. manual memory management\n4. garbage collector\n'), read(M),

       language(L, P, T, C, M),
       write(L).

当我运行此程序时,我会一直收到所有语言。但在我的测试用例中,它应该只有一种语言。当语言的所有四个谓词对于同一语言返回时,语言是可接受的L.我的错误在哪里,我该如何解决?

非常感谢!

1 个答案:

答案 0 :(得分:0)

错误在捕获输入的某处(特别是,要求用户输入与程序实际预期不相符的字符串;请注意这里)。如果使用原子并删除单引号,则包含两个测试用例的程序为:

paradigm(ada, imperative).
paradigm(c, imperative).

typization(ada, statical).
typization(c, explicit).

compiler(ada, openSource).
compiler(c, debugPosibility).

memory(ada, stack).
memory(c, pointer).

language(L, P, T, C, M) :- paradigm(L, P), typization(L, T), compiler(L, C), memory(L, M).

test1(L) :- language(L, imperative, statical, openSource, stack).
test2(L,P) :- language(L, P, statical, openSource, stack).

仅运行test1(L)会产生L=ada,并且正在运行test2(L,P)只会产生L=ada, P=imperative