Categories
All

PyShifterPro

Python and prolog encryption project based on “caesar cipher” encryption technique.

How to use:

  • in terminal: python PrologGenerator.py :> prologCode.pl
  • run prolog code via terminal(swipl prologCode.pl) or gui then use:
    • shift(a, X).
    • hideChar(a).
    • showChar(a).
    • guessChar(a).
    • hideString(test).
    • showString(nkdohg).
    • guessString(abc).
    • readFile.
    • dfs(a, X, z). continue pressing ;

for windows users: download and use python and prolog gui.

Examples:

#Ex.1 walk start from a with key = 3

?- walk(a,X,z).
X = d ;
X = g ;
X = j ;
X = m ;
X = p ;
X = s ;
X = v ;
X = y ;
X = b ;
X = e ;
X = h ;
X = k ;
X = n ;
X = q ;
X = t ;
X = w ;
X = z ;
X = c ;
false.

#Ex.2 encrypt string met3cs with key = 3

?- hideString(met3cs).
phw6fv
true.

#Ex.3 decrypt string phw6fv with key = 3

?- showString(phw6fv).
met3cs
true.

#Ex.4 encrypt char z with key = 3

?- hideChar(z).
c
true.

Files Structure:

examples
   \ input.txt
   \ prologCode.pl
PrologGenerator.py
input.txt
prologCode.pl

——-

prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

input.txt

khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.

PrologGenerator.py

# Encryption and Decryption Project using Prolog and Python
# \_based on "Caesar cipher" encryption technique..
#
# Author: Khaled Alam


import string



# START Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>

shiftingKey = 3      # << 3 default(Caesar cipher)
factName = 'shift'
bothPredicateName = 'guess'
encryptPredicateName = 'hide'
decryptPredicateName = 'show'


factLower = True
factUpper = False  # not handled
factDigits = True

inputFileName = 'input.txt'


# END Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>



# START BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#lower chars:
if factLower is True:
	for char in string.ascii_lowercase:
		if ord(char) + shiftingKey > ord('z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'

#upper chars:
if factUpper is True:
	for char in string.ascii_uppercase:
		if ord(char) + shiftingKey > ord('Z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'
#digits:
if factDigits is True:
	for digit in range(10):
	    print factName + '(' + str(digit) + ', ' + str((digit + shiftingKey) % 9) + ').'


print('shift(., *).\nshift(*,.).')


# END BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')


# START BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# >>> 1 CHAR/DIGIT <<<<<<<<<<<<<<<<<<

#encrypt 1 char or digit:
print(encryptPredicateName + 'Char(X):- ' + factName + '(X,Y), write(Y).')

#decrypt 1 char or digit:
print(decryptPredicateName + 'Char(X):- ' + factName + '(Y,X), write(Y).')

#print both guesses of 1 char or digit:
print(bothPredicateName + 'Char(X):- shift(X,Y), shift(Z,X), write(\"maybe: \"), write(Y), '+
'write(\"\\nOR\\nmaybe: \"), write(Z), write(\'\\n=========\\n\').')


print('\n')


# >>>    STRINGS   <<<<<<<<<<<<<<<<<<

print('numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).')
print('numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).')

#encrypt string:
print(encryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).')

#decrypt string:
print(decryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).')

#print both guesses of string:
print(bothPredicateName + 'String(String) :- write(\"maybe: \"), hideString(String), write(\"\\nOR\\nmaybe: \"),'+
' showString(String), write(\'\\n=========\\n\').')

# END BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')



# START BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


#Read from file
print('readFile :-')
print('open(\'' + inputFileName + '\', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.')
print('read_file(Stream,[]) :- at_end_of_stream(Stream).')
print('read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(\' : \'),'+
' write(\'\\n\'), ' + bothPredicateName + 'String(X), write(\'\\n\'), read_file(Stream,L).')

print('\n')

print('walk(X, Z, _ORG):- shift(X,Z).\nwalk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).')


# END BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')

examples/prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

examples/input.txt

'khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.
Categories
All

Smart Parking System in CLIPS

Smart Parking System Task in Clips programming language

Parking Blocks

Code:

;; @Author       Khaled
;; @Subject      4_CS_AI
;; @Proj. Desc.  {Smart Parking System} 
;;				    - Organize parking process intelligently.
;;					- Suggest nearest free position.
;;
;; @Lang.        CLIPS, BASH
;; @Env.         GUI v6.3 win | CLI v6.24 debian
;; @File         @Name "app.clp" | @Created 19-nov-2018
;; @Performance  Greedy algo. | linear O(N) | N ~= positions 
;; 
;; _________________run info___________________________
;; CLIPS> (clear) 
;; CLIPS> (load C:\Users\YOUR_PATH_DIR\app.clp)
;; CLIPS> (run)
;;###########################################################



;; ~~~~  START STYLE FUNCTIONS  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(deffunction print-lines (?howMany)
	(while (> ?howMany 0) do (printout t crlf) (bind ?howMany (- ?howMany 1)))
)

(deffunction print-spaces (?howMany)
	(while (> ?howMany 0) do (printout t " ") (bind ?howMany (- ?howMany 1)))
)
;; ~~~~  END STYLE FUNCTIONS  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





;; ~~~~  START GLOBAL VARS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(defglobal 
	?*booked*  		   = (create$ )
	?*enterAnswers*    = (create$ 1 e enter)
	?*leaveAnswers*    = (create$ 2 l leave)
	?*welcomeAnswers*  = (create$ ?*enterAnswers* ?*leaveAnswers*)
	?*exitAnswers*     = (create$ exit end out pause halt -1 3)
)
;; ~~~~  END GLOBAL VARS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




;; ~~~~  START MY CORE FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;; Constant O(1)
(deffunction wait-any-key()
	(printout t "press any key to continue! ")
	(bind ?wait (read))
)


;; Constant O(1)
(deffunction is-valid-position(?pos)
	(return (and (>= ?pos 1) (<= ?pos 20)))
)

;; Linear O(N)
(deffunction removeBooked (?pos)
	(bind ?start 1)
	(bind ?end 20)
	(bind ?tmpBooked (create$))
	(while (<= ?start ?end)
		(if (and (<> ?start ?pos) (member$ ?start ?*booked*))
			then
				(bind ?tmpBooked (insert$ ?tmpBooked 1 ?start))
		)
		(bind ?start (+ ?start 1))
	)
	(bind ?*booked* ?tmpBooked)
	(return 0)
)

;; Constant O(1)
(deffunction getDistanceOfIndex(?idx)

	;; Positions 2 Lines:
	;;  GA  [1  : 10]
	;;  TE  [20 : 11]
	
	;; Algorithm Pseudocode:-
	;;''''''''''''''''''''''
	;; if     distance <= 10  =:  distance_to_gate = idx[1:10]
	;; elseif distance <= 20  =:  distance_to_gate = 20 - idx[11:20] + 1
	
	(if (and (>= ?idx 1) (<= ?idx 10))
		then
			(return ?idx)
		else
			(if (and (>= ?idx 11) (<= ?idx 20))
				then
					(return (+ (- 20 ?idx) 1))
			)
	)
	(return -1)
)


;; Linear O(N)
(deffunction getNearestEmptyPosition ()
	(bind ?start 1)
	(bind ?end 20)
	(bind ?bestIdx 1)
	(bind ?mi 11)  ;; max possible is 10
	
	(while (<= ?start ?end)
		(if (not (member$ ?start ?*booked*))
			then
				(bind ?disOfIdx  (getDistanceOfIndex ?start))
				(if (< ?disOfIdx ?mi)
					then
						(bind ?mi ?disOfIdx)
						(bind ?bestIdx ?start)
				)
		)
		(bind ?start (+ ?start 1))
	)
	(return ?bestIdx)
)


;; ~~~~  START MY CORE FUNCTIONS  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




;; ~~~~  START MESSAGES  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(deffunction message-welcome()
	(print-lines 25) (print-spaces 20)
	(printout t "--=  Smart Parking System using CLIPS (AI)Project  =--")
	(print-lines 2)(print-spaces 30)(printout t "Organize parking process intelligently!")
	(print-lines 20) (printout t "Choose:") (print-lines 1)
	(printout t "1- Enter (1, e, E, Enter)")     (print-lines 1)
	(printout t "2- Leave (2, l, L, Leave)")     (print-lines 1)
	(printout t "3- Exit  (3, -1, exit, end, halt, out, pause)") (print-lines 2)(printout t "? ")
)

(deffunction message-select-position()
	(print-lines 25) (print-spaces 20)
	(printout t "There are 20 positions (2 lines)") (print-lines 3)
	(printout t "  Enter  |\\_   1  2   3   4   5   6   7   8   9  10")(print-lines 1)
	(printout t "  GATE   |/   20 19  18  17  16  15  14  13  12  11")(print-lines 2)
	(print-lines 5)
)

(deffunction message-leave-position()
	(print-lines 5) (printout t "What was your position? ")
)

(deffunction message-all-busy()
	(print-lines 5)(printout t "Sorry all positions are busy!")(print-lines 5)
)

(deffunction message-booked-position()
	(print-lines 5)(printout t "Sorry you entered booked position!")(print-lines 5)
)

(deffunction message-wrong-position()
	(print-lines 5)(printout t "Sorry you entered wrong position! (correct [1 : 20])")
	(print-lines 5)
)

(deffunction message-selected-position-empty()
	(print-lines 5)(printout t "Sorry selected position is already empty!")
	(print-lines 5)
)

(deffunction message-enter-ok(?pos)
	(print-lines 5)(printout t "Welcome ^_*")(print-lines 5)
	(printout t "Use position [" ?pos "] please!" )(print-lines 5)
)


(deffunction message-best-nearest(?pos)
	(print-lines 5)(printout t "[note] nearest empty position to gate is " ?pos)
	(print-lines 5)
)

(deffunction message-leave-ok()
	(print-lines 5)
	(printout t "Thank you *_^ for using our parking service, see you soon!")
	(print-lines 5)
)

;; ~~~~  END MESSAGES  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~






;; ~~~~ START POINT ~~~~
(defrule MAIN::__COOL_CPU__

	?f <- (initial-fact)
=>
	(retract ?f)
	(message-welcome)
	(bind ?input (read))
	
	;; Normalize the input
	(if (lexemep ?input) then (bind ?input (lowcase ?input)))  
	
	(if (or (member$ ?input ?*enterAnswers*)
			(member$ ?input ?*leaveAnswers*)
		)
		then
			(assert (ask-position))
	)
	(assert (answer-welcome ?input))
)



(defrule MAIN::ASKER-POSITION
	?f <- (ask-position)
	(answer-welcome ?ans)
=>
	(if (member$ ?ans ?*enterAnswers*)
		then
			(message-select-position)
			(message-best-nearest (getNearestEmptyPosition))
			(printout t "Choose? ")
			(assert (booking-now))
		else
			(message-leave-position)
			(assert (leave-now))
	)
	
	(assert (answer-position (read)))
	(retract ?f)
)




(defrule MAIN::BOOK
	?f <- (booking-now)
	?x <- (answer-welcome  ?welcome)
	(test (member$ ?welcome ?*enterAnswers*))
	?p <- (answer-position ?pos)
=>
	(if (and (is-valid-position ?pos) (not (member$ ?pos ?*booked*)))
		then
			(bind ?*booked* (insert$ ?*booked* 1 ?pos))
			(message-enter-ok ?pos)
		else
			(if (>= (length ?*booked*) 20)
				then
					(message-all-busy)
				else
					(if (not (is-valid-position ?pos))
						then
							(message-wrong-position)
						else
							(message-booked-position)
					)
			)
	)
	(wait-any-key)
	(retract ?f)
	(retract ?p)
	(retract ?x)
	(assert (initial-fact))
)


(defrule MAIN::LEAVE
	?f <- (leave-now)
	?x <- (answer-welcome  ?welcome)
	(test (member$ ?welcome ?*leaveAnswers*))
	?p <- (answer-position ?pos)
=>
	(if (member$ ?pos ?*booked*)
		then
			(removeBooked ?pos)
			(message-leave-ok)
		else
			(if (not (is-valid-position ?pos))
				then
					(message-wrong-position)
				else
					(message-selected-position-empty)
			)
	)
	(wait-any-key)
	(retract ?f)
	(retract ?x)
	(retract ?p)
	(assert (initial-fact))
)



 
(defrule MAIN::APP-WINDOW
	?f <- (answer-welcome ?ans)
	(test (not (member$ ?ans ?*exitAnswers*)))
=>
	(retract ?f)
	(assert (initial-fact))
)
 
(reset)
 
(run)
 
(exit)