% Stephen Konar % Final Project % CS140 -Logic Programming in Prolog % The Goal of this example is to show modern/maintstream uses of Prolog :- module(my_httpd, [ activate/1 ]). :- use_module(library(pce)). :- use_module(library('http/httpd')). :- use_module(library('http/http_open')). :- use_module(library('http/html_write')). :- use_module(library('draw/importpl')). % Only set of Input required is the desired port numer % Remember some machines may have reserved ports like 80 % activate(Port) :- new(_, my_httpd(Port)). % Instantiaion of PCE :- pce_begin_class(my_httpd, httpd, "CS140: Prolog in the modern web"). request(HTTPD, Request:sheet) :-> "A request came in.":: get(Request, path, Path), reply(Path, HTTPD). :- discontiguous reply/2. % Cut that states when root of the site is requested unify myhttpd to DCG % frames reply('/', HTTPD) :- !, send(HTTPD, reply_html, my_httpd:frames). % Original predicate page can be found in swipl library html_write % This page allows for the acceptance of background colors page(Head, Body,Color) --> html([ \['\n'], html([ head(Head), body(bgcolor(Color), Body) ]) ]). % DCG for the html version 4 frames frames --> html(html([ head(title('CS140: Prolog in the modern World')), frameset([rows('8%,92%')], [ frame([ src('/index'), name(index) ]), frame([ src('/start'), name(body) ]), frame([ src('/code'), name(bottom) ]) ]) ])). % Predicates and DCGs for indivual pages reply('/start', HTTPD) :- send(HTTPD, reply_html, my_httpd:start). % Original start page in right frame start --> page(title('start'), [ br([]), br([]), br([]), % addition of header tag h1(['Prolog Webserver project']), h2(['By Stephen Konar']) ]). reply('/index', HTTPD) :- send(HTTPD, reply_html, my_httpd:index). % DCG for left frame index --> page(title('Index'), % used paragraph instead of straight links in order to % custom format on one line [ p([a([ href('/Project'), target(body) ], [ ' Project Thoughts' ]), ' ', a([ href('/ontaria'), target(body)], [' Ontaria Web server']), ' ', a([ href('/comingsoon'),target(body)], ['In development']), ' ', a([ href('/picture'), target(body) ], [ ' PCEdraw demo' ]) ]) ]). reply('/Project', HTTPD) :- send(HTTPD,reply_html,my_httpd:project). project --> page(title('Text'), [ p(['The goal of this project was to create an autonomous Search engine optimization agent.']), p(['However, during my research, I realized thatt there are already exists many practical appliations of prolog already developed for the modern internet. Yet, except for twiki, there are very few applications still in existance.']), p(['I chose to alter my project in order to demonstrate how a terse language such as prolog can build such dynamic systems such as a webserver']), p(['A large portion of this code comes from an example provided with Swi Prolog Documentation']), a([href('http://www.google.com'),target(body)],['Google']) ],'yellow'). % attempt at streaming source code directly to additional bottom frame % reply('/code',HTTPD) :- % send(HTTPD,reply_html,my_httpd:code). % code--> % % http_open('www.google.com',In,[]), % copy_stream_data(In,user_output),close(In). % a([ href('http://www.engadget.com'),target(bottom)], % ['code']) reply('/picture', HTTPD) :- make_picture(Gr), send(HTTPD, reply, Gr, 'image/gif'). make_picture(Dev) :- new(Dev, device), drawing(xpcenetscape, Drawing), realise_drawing(Dev, Drawing). reply('/ontaria', HTTPD) :- send(HTTPD, reply_html, my_httpd:ontaria). ontaria --> page(title('ontaria'), [ p(['Ontaria web browser is an installation of semwalker which can be found ']), a([href('http://www.w3.org/2004/ontaria/'),target(body)],['here']), br([]), p(['Semwalker can be found ']), a([href('http://www.w3.org/2002//05/semwalker/'),target(body)],['here']) ],'Blue'). reply('/demo',HTTPD) :- send(HTTPD,reply_html,my_httpd:demo). demo --> page(title('Professor Cohen'), [ h1(['This is a demo']), p(['This is text that I have already typed into a file on my computer so that everyone can read the same text'])],'green'). % reply('/code', HTTPD,address) :- % send(HTTPD, reply_html, my_httpd:code). % code :- % http_open(address,In,[]), % copy_stream_data(In,user_output), % close(In). reply('/comingsoon',HTTPD) :- send(HTTPD, reply_html,my_httpd:comingsoon). comingsoon --> page(title('comingsoon'), [h1(['COMING SOON']), p(['With a bit more time, I am working on incorporating more of htmls native features into prolog. For example, instead of using a paragraph tag, one could use a header tag. Also text formatting is a big issue. This would be enhanced by increasing the scope of the page predicate in the way that back ground color was added'])],'Grey'). % Drawing imported from PceDraw drawing(xpcenetscape, [ compound(new(A, figure), drawing([ display(box(137, 74)+radius(17), point(0, 0)), display(text('XPCE', center, normal), point(52, 30)) ]), point(163, 183)), compound(new(B, figure), drawing([ display(box(137, 74)+radius(17), point(0, 0)), display(text('Netscape', center, normal), point(42, 30)) ]), point(350, 183)), connect(connection(A, B, handle(w, h/2, link, east), handle(0, h/2, link, west)) + arrows(both)) ]). :- pce_end_class(my_httpd).