Skip to content

Instantly share code, notes, and snippets.

@iamandrewluca
Created December 18, 2015 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamandrewluca/2f891563c6e8d309f74a to your computer and use it in GitHub Desktop.
Save iamandrewluca/2f891563c6e8d309f74a to your computer and use it in GitHub Desktop.
Expert system for advising a programming language. (GNU Prolog)
% ------------------------------------------------------------------------------
% Generic Expert System interface
% ------------------------------------------------------------------------------
% Dynamic fact
% --------------------------------------
:- dynamic(known/3).
% --------------------------------------
% Start system
% --------------------------------------
solve :-
% using retract cause bugs?
%% asserta(known(bug, bug, bug)),
% clear database
%% retract(known(_,_,_)),
retractall(known(_,_,_)),
% start the system
goal(X),
% write the answer
write('The answer is '), write(X), nl.
solve :-
write('No answer found.').
% --------------------------------------
% Ask Single Value Attributes
% --------------------------------------
% Check if this attribute was already answered as yes
ask(Attribute, Value) :-
known(yes, Attribute, Value), !.
% if attribute was answered different from yes
ask(Attribute, Value) :-
known(_, Attribute, Value), !, fail.
% check if not multivalued
ask(Attribute, Value) :-
not(multivalued(Attribute)),
known(yes, Attribute, Value2),
Value \= Value2,
!, fail.
% ask the question
ask(Attribute, Value) :-
write(Attribute:Value), write('?: '),
read(Answer), asserta(known(Answer, Attribute, Value)),
Answer == yes.
% --------------------------------------
% Ask Multivalued Attributes
% --------------------------------------
% check if Attribute and Value already exists
menuask(Attribute, Value, _) :-
known(yes, Attribute, Value), !.
% check if Attribute exists but value is different
menuask(Attribute, Value, _) :-
known(yes, Attribute, Value2),
Value \= Value2, !, fail.
% question the user
menuask(Attribute, Value, MenuList) :-
write('What '), write(Attribute), write(' it is?:'), nl,
write(MenuList), nl, write('>'),
read(Answer),
check_answer(Answer, Attribute, Value, MenuList),
asserta(known(yes, Attribute, Answer)),
Answer == Value.
% check if answer if member of MenuList
check_answer(Answer, _, _, MenuList) :-
member(Answer, MenuList), !.
% reask the question
check_answer(Answer, Attribute, Value, MenuList) :-
write(Answer), write(' is not a legal value, try again.'), nl,
menuask(Attribute, Value, MenuList).
% ------------------------------------------------------------------------------
% MY DATABASE
% ------------------------------------------------------------------------------
% ------------------------------------------------------------------------------
% Expert System to determine which programming language
% end user wants to use for his product.
% ------------------------------------------------------------------------------
% ------------------------------------------------------------------------------
% List of programming languages
% ------------------------------------------------------------------------------
% Java, C++, C, Swift, Python, Perl, Ruby, C#, HTML, CSS, Javascript, PHP, SQL,
% Assembly, R, Prolog
% ------------------------------------------------------------------------------
% Programming languages attributes
% ------------------------------------------------------------------------------
% ------------------------------------------------------------------------------
% Domain Attributes
% ------------------------------------------------------------------------------
% App Development, Game Development, Embedded Development, Data Analysis,
% System Administration, Artificial Intelligence, Operating System Development
% ------------------------------------------------------------------------------
% Generic Device Attributes
% ------------------------------------------------------------------------------
% Desktop Devices, Mobile Devices, Web Browser, Embedded Devices, Console Devices
% ------------------------------------------------------------------------------
% Operating System Attributes
% ------------------------------------------------------------------------------
% Windows, Linux, OSx, Android, iOS, Windows Phone, Browser, Arduino,
% Raspberry PI, Xbox 360, PS4, Steam, Smart TV
% Add here top goal
% ------------------------------------------------------------------------------
% Example
% goal(X) :- my_goal(X)
goal(X) :- language(X).
% ------------------------------------------------------------------------------
% Add here multivalued attributes
% ------------------------------------------------------------------------------
% Example
% multivalued(attribute_name).
multivalued(domain).
multivalued(device).
multivalued(platform).
% Asking questions
domain(Answer) :-
menuask(domain, Answer, [apps, games, os, embedded, analysis, administration, ai]).
device(Answer) :-
menuask(device, Answer, [desktop, mobile, browser, embedded, console]).
os(Answer) :-
menuask(os, Answer, [win, linux, osx, android, ios, wp, browser, arduino, raspberry, xbox, ps4, steam, smarttv]).
% My Programming Languages
% goal(X) calls languages bellow to determine which to show.
% Foreach programming language add different combinations bellow
language(java) :- domain(apps), device(mobile), os(android).
language(java) :- domain(apps), device(browser), os(browser).
language(cpp) :- domain(games), device(desktop), os(win).
language(cpp) :- domain(games), device(desktop), os(linux).
language(cpp) :- domain(games), device(desktop), os(osx).
language(cpp) :- domain(games), device(mobile), os(android).
language(cpp) :- domain(os), device(desktop), os(win).
language(c) :- domain(os), device(desktop), os(win).
language(c) :- domain(os), device(desktop), os(linux).
language(c) :- domain(embedded), device(embedded), os(arduino).
language(c) :- domain(embedded), device(embedded), os(raspberry).
language(swift) :- domain(apps), device(desktop), os(osx).
language(swift) :- domain(apps), device(mobile), os(ios).
language(swift) :- domain(games), device(desktop), os(osx).
language(swift) :- domain(games), device(mobile), os(ios).
language(python) :- domain(apps), device(browser), os(browser).
language(python) :- domain(analysis), device(desktop), os(win).
language(perl) :- domain(apps), device(browser), os(browser).
language(perl) :- domain(administration), device(desktop), os(win).
language(ruby) :- domain(apps), device(browser), os(browser).
language(csharp) :- domain(apps), device(desktop), os(win).
language(csharp) :- domain(apps), device(browser), os(win).
language(csharp) :- domain(os), device(desktop), os(win).
language(csharp) :- domain(os), device(mobile), os(wp).
% And other languages
language(html) :- fail.
language(css) :- fail.
language(javascript) :- fail.
language(php) :- fail.
language(sql) :- fail.
language(assembly) :- fail.
language(r) :- fail.
language(prolog) :- fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment