Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -*- Mode:Prolog; coding:UTF-8; -*- */
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Gameserver-Spielengine fuer GDL Card Game
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- :- module(gdlGame,[initMatch_gdlGame/8, play_gdlGame/9, joinMatch_gdlGame/7]).
- :- use_module(library(file_systems)).
- :- use_module(library(lists)).
- :- use_module(remoteaiwrapper).
- :- ensure_loaded(simutils).
- %% joinMatch_ownGDL(+MatchId,+PlayerDetails,+Oldworld,-Newworld,-Views, -Round, -Phase) is det.
- joinMatch_gdlGame(_MatchId,_PlayerDetails,_Oldworld,_Newworld,_Views, _Round, _Phase) :-
- throw('In diesem Spiel ist ein nachtraegliches Betreten nicht moeglich.').
- %% initMatch_ownGDL(+MatchId, +MatchOptions, +PlayerList, +PlayerDetails, -Worldout, -Views, -Round, -Phase) is det.
- initMatch_gdlGame(MatchId, MatchOptions, _PlayerList, _PlayerDetails, WorldOut, Views, "1", "default") :-
- processMatchOptions(MatchId,MatchOptions,GdlCode,VisCode),
- importGdlCode(MatchId,GdlCode),
- processVisCode(VisCode,VisDict),
- % fuellt alle 2500 Kartenpositionen mit leeren Platzhalterkarten
- putInvisibleCards(ViewCards0),
- % initialisiert die World-Variable
- World = [gdl-[],
- vis-VisDict,
- view-ViewCards0,
- state-0,
- action-noop],
- % fuellt die "gdl" anhand der "init"-Fakten
- updateGdl(World,World1),
- % formt aus den Fakten in "gdl" die View
- updateView(World1,World2),
- worldget(view,World2,ViewCards),
- % gibt die erlaubten Aktionen der Spieler im aktuellen Zustand aus
- allowedActions(World2,player1,Player1AllowedActions),
- allowedActions(World2,player2,Player2AllowedActions),
- Player1View = view(player1,ViewCards,[],Player1AllowedActions,[],[],undo(0,[]),[]),
- Player2View = view(player2,ViewCards,[],Player2AllowedActions,[],[],undo(0,[]),[]),
- % Outputs an Java
- WorldOut = World2,
- Views = [Player1View,Player2View],
- debprintln('finish InitMatch'),
- true.
- %% play_ownGDL(+Player, +GuiAction, +MatchNumber, +WorldIn, -WorldOut, -Views, -Matchstate, -Round, -Phase)
- play_gdlGame(_Player, GuiAction, _MatchNumber, WorldIn, WorldOut, Views, Matchstate, "1", "default") :-
- % holt das VisualizationDictionairy
- worldget(vis,WorldIn,Vis),
- % gibt die GuiAction in die "World"-Variable
- worldput(action,WorldIn,GuiAction,World0),
- % aktualisiert die "gdl"-Fakten anhand der neuen GuiAction
- updateGdl(World0,World1),
- % bei Spielende den Sieger auswerten
- (terminal(World1) ->
- worldget(player1,Vis,Player1),
- worldget(player2,Vis,Player2),
- goal(World1,Player1,Player1GdlPoints),
- goal(World1,Player2,Player2GdlPoints),
- n_numberToNumber(Player1GdlPoints,Player1Points),
- n_numberToNumber(Player2GdlPoints,Player2Points),
- (Player1Points < Player2Points ->
- Player1Result = lost,
- Player2Result = won
- ;
- (Player1Points > Player2Points ->
- Player1Result = won,
- Player2Result = lost
- ; % Player1Points = Player2Points
- Player1Result = draw,
- Player2Result = draw
- )
- ),
- Matchstate0 = matchstate(finished,[player(player1,Player1Result,Player1Points),player(player2,Player2Result,Player2Points)])
- ;
- Matchstate0 = matchstate(running(0),[])
- ),
- % erstellt die Views für die Spieler
- updateView(World1,World2),
- worldget(view,World2,ViewCards),
- % gibt die erlaubten Aktionen der Spieler wieder
- (Matchstate0 = matchstate(running(0),[]) ->
- allowedActions(World2,player1,Player1AllowedActions),
- allowedActions(World2,player2,Player2AllowedActions)
- ; % keine Aktionen bei Spielende
- Player1AllowedActions = [],
- Player2AllowedActions = []
- ),
- Player1View = view(player1,ViewCards,[],Player1AllowedActions,[],[],undo(0,[]),[]),
- Player2View = view(player2,ViewCards,[],Player2AllowedActions,[],[],undo(0,[]),[]),
- % Outputs an Java
- WorldOut = World2,
- Views = [Player1View,Player2View],
- Matchstate = Matchstate0,
- % gibt den Matchstate im Terminal aus (TODO: an die Oberflaeche bringen)
- debprintln('Matchstate:'-Matchstate),
- true.
- % verarbeitet die Matchoptionen: gibt die Codes der angegebenen Files aus
- processMatchOptions(MatchId,MatchOptions,GdlCode,VisCode) :-
- % get GDL-Code
- (worldget(gdlfile, MatchOptions, GdlfileName) ->
- match_file_fullpath(MatchId, GdlfileName, FullGdlfileName),
- file2string(FullGdlfileName,GdlCode)
- ;
- throw('Keine GDL-File angegeben!')
- ),
- % get VIS-Code
- (worldget(visfile,MatchOptions,VisfileName) ->
- match_file_fullpath(MatchId,VisfileName,FullVisfileName),
- file2string(FullVisfileName,VisCode)
- ;
- throw('Keine VIS-File angegeben!')
- ).
- %
- % Bestimmt zu Match-ID und Dateiname vollstaendigen Pfad und wirft Ausnahmen, falls Uploadverzeichnis oder Datei nicht existieren.
- match_file_fullpath(MatchId,Filename,Fullpath) :-
- atom_number(MatchIdAtom,MatchId),
- (Filename = '' ->
- throw('Benoetigtes File nicht ausgewaehlt!!!')
- ;
- true
- ),
- uploadfolder(Ufolder),
- (directory_member_of_directory(Ufolder,MatchIdAtom,_) ->
- true
- ;
- throw('Verzeichnis mit hochgeladenen Dateien existiert nicht!')
- ),
- atom_concat(Ufolder,MatchIdAtom,FullDirPathAtom),
- file_members_of_directory(FullDirPathAtom,Set),
- (worldget(Filename,Set,Fullpath) ->
- true
- ;
- throw('Datei existiert nicht!')
- ).
- % speichert verarbeiteten GDL-Code in den temporären Matchfolder und importiert ihn
- importGdlCode(MatchId,GDLcode) :-
- gdlToProlog(GDLcode,PrologCode),
- atom_codes(PrologAtom,PrologCode),
- uploadfolder(Ufolder),
- atom_number(MatchIdAtom,MatchId),
- PrologFile = '/gdlAsProlog.pl',
- atom_concat(Ufolder,MatchIdAtom,FullDirPathAtom),
- atom_concat(FullDirPathAtom,PrologFile,FullPathFile),
- open(FullPathFile,write,StreamWrite),
- write(StreamWrite,PrologAtom),
- close(StreamWrite),
- use_module(FullPathFile).
- %
- % verarbeitet GDL-Code, sodass er als Prolog-Code verwendet werden kann
- gdlToProlog(GDLcode,PrologCode) :-
- select(-1,GDLcode,GDLcode0), % entfernt die -1 am Ende
- code_deleteComments(GDLcode0,Code0), % loescht Kommentarzeilen
- code_replace(38,Code0,44,Code1), % ersetzt "&" durch ","
- code_replace(126,Code1,[92,43],Code2), % ersetzt "~" durch "\+"
- code_numbersToAtoms(Code2,Code3), % stellt Zahlen "n_" voran
- code_deleteEmptyRows(Code3,Code4), % loescht Leerzeilen
- code_replace(32,Code4,[],Code5), % loescht alle Leerzeichen
- code_addDots(Code5,PrologCode0), % fuegt Punkte ein
- code_addWorldVar(PrologCode0,PrologCode). % fuegt die World-Variable hinzu
- % transformiert erhaltenen Visualisierungscode in ein VisualizationDictionary um
- processVisCode(VisCode,VisDict) :-
- select(-1,VisCode,Code0), % entfernt die -1 am Ende
- code_deleteComments(Code0,Code1), % loescht Kommentarzeilen
- code_deleteEmptyRows(Code1,Code), % loescht leere Zeilen
- getVisDict(Code,VisDict).
- %
- getVisDict([],[]).
- getVisDict(VisCode,[PrefixAtom-SuffixAtom]) :-
- \+member(10,VisCode),
- sublist(VisCode,[60,45],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,VisCode,PrefixLength),
- append_length(_Part,Suffix2,PartSuffix,PartLength),
- code_numbersToAtoms(Suffix2,Suffix3),
- atom_codes(PrefixAtom,Prefix),
- atom_codes(SuffixAtom,Suffix3).
- getVisDict(VisCode,[Prefix2Atom-Suffix2Atom|VisDict]) :-
- sublist(VisCode,[10],PrefixLength1,PartLength1,_SuffixLength1),
- append_length(Prefix1,PartSuffix1,VisCode,PrefixLength1),
- append_length(_Part1,Suffix1,PartSuffix1,PartLength1),
- sublist(Prefix1,[60,45],PrefixLength2,PartLength2,_SuffixLength2),
- append_length(Prefix2,PartSuffix2,Prefix1,PrefixLength2),
- append_length(_Part2,Suffix2,PartSuffix2,PartLength2),
- code_numbersToAtoms(Suffix2,Suffix3),
- atom_codes(Prefix2Atom,Prefix2),
- atom_codes(Suffix2Atom,Suffix3),
- getVisDict(Suffix1,VisDict).
- % fuellt alle Position mit unsichtbaren Platzhalterkarten
- putInvisibleCards(ViewCards) :-
- getAllPositions(AllPositions),
- putInvisibleCards(ViewCards,AllPositions).
- putInvisibleCards([],[]).
- putInvisibleCards([PositionAtom-card(none-0)|ViewCards],[Position|Positions]) :-
- atom_number(PositionAtom,Position),
- putInvisibleCards(ViewCards,Positions).
- %
- % gibt alle moeglichen Position-IDs zurueck (50x50 Grid)
- getAllPositions(AllPositions) :-
- range(1,50,Range),
- getAllPositions(AllPositions,Range,Range).
- getAllPositions([],[],_).
- getAllPositions([],_,[]).
- getAllPositions(AllPositions,[X|Xs],Ys) :-
- getAllPositions1(Positions1,X,Ys),
- getAllPositions(Positions2,Xs,Ys),
- append([Positions1,Positions2],AllPositions).
- getAllPositions1(_,_,[]).
- getAllPositions1([Z|Positions],X,[Y|Ys]) :-
- Z1 is 10000 + Y,
- Z2 is 100 * X,
- Z is Z1 + Z2,
- getAllPositions1(Positions,X,Ys).
- % bestimmt anhand der "init" und "next" Literale die Fakten des folgenden Zustands
- updateGdl(WorldIn,WorldOut) :-
- worldget(state,WorldIn,State),
- (State = 0 ->
- findall(InitVar,init(WorldIn,InitVar),Vars)
- ;
- findall(NextVar,next(WorldIn,NextVar),Vars)
- ),
- updateGdl(WorldIn,WorldOut0,Vars),
- NextState is State + 1,
- worldput(state,WorldOut0,NextState,WorldOut).
- %
- updateGdl(World,World,[]).
- updateGdl(WorldIn,WorldOut,[Var|Vars]) :-
- worldget(gdl,WorldIn,Gdl),
- worldget(state,WorldIn,State),
- State1 is State + 1,
- atom_number(State1Atom,State1),
- Var =.. [Constant|World_Terms],
- World_Terms = [_World|Terms],
- length(Terms,Length),
- atom_number(LengthAtom,Length),
- atom_concat(Constant,LengthAtom,ConstantLength),
- (worldget(ConstantLength,Gdl,GdlVars) ->
- Terms0 = [State1Atom|Terms],
- setValues(GdlVars,Terms0,GdlVars1),
- worldput(ConstantLength,Gdl,GdlVars1,Gdl1),
- worldput(gdl,WorldIn,Gdl1,World1),
- updateGdl(World1,WorldOut,Vars)
- ;
- Length1 is Length + 1,
- listsLength0(GdlVars,Length1),
- worldput(ConstantLength,Gdl,GdlVars,Gdl1),
- worldput(gdl,WorldIn,Gdl1,World1),
- updateGdl(World1,WorldOut,[Var|Vars])
- ).
- %
- setValues([],[],[]).
- setValues([Var|Vars],[Term|Terms],[[Term|Var]|VarsOut]) :-
- setValues(Vars,Terms,VarsOut).
- % bestimmt anhand des "legal" Literals die erlaubten Aktionen der Spieler im aktuellen Zustand
- allowedActions(World,Player,AllowedActions) :-
- worldget(vis,World,Vis),
- worldget(Player,Vis,PlayerGdl),
- findall(Action,legal(World,PlayerGdl,Action),Actions),
- allowedActions1(World,Actions,AllowedActions).
- allowedActions1(_,[],[]).
- allowedActions1(World,[Action|Actions],AllowedActions) :-
- worldget(vis,World,Vis),
- Action =.. [Constant|World_Terms],
- (World_Terms = [] ->
- ActionAtom = Constant
- ;
- World_Terms = [_World|Terms],
- ActionTerm =.. [Constant|Terms],
- atom_term(ActionAtom,ActionTerm)
- ),
- worldget(GameActionAtom,Vis,ActionAtom),
- (GameActionAtom = noop ->
- AllowedActions = []
- ;
- atom_term(GameActionAtom,GameActionTerm),
- GameActionTerm =.. [click,GameActionNumber],
- allowedActions1(World,Actions,AllowedActions0),
- AllowedActions = [GameActionNumber-[click]|AllowedActions0]
- ).
- % aktualisiert die darzustellenden Karten
- updateView(WorldIn,WorldOut) :-
- getGdlFacts(WorldIn,cardPosition,States,PosFacts),
- getGdlFacts(WorldIn,cardSuit,_,SuitFacts),
- getGdlFacts(WorldIn,cardValue,_,ValFacts),
- getViewCards(WorldIn,States,PosFacts,SuitFacts,ValFacts,ViewCardsNew),
- worldget(view,WorldIn,ViewCardsOld),
- updateViewCards(ViewCardsOld,ViewCardsNew,ViewCardsUpdate),
- worldput(view,WorldIn,ViewCardsUpdate,WorldOut).
- %
- % gibt Karten und ihre Position im aktuellen Zustand zurueck
- getViewCards(_,[],[],[],[],[]).
- getViewCards(World,[StateAtom|States],[Pos|PosFacts],SuitIn,ValIn,[Pos1-card(Suit1-Val1)|ViewCards]) :-
- worldget(state,World,State),
- atom_number(StateAtom,State),
- worldget(vis,World,Vis),
- worldget_np(Pos1,Vis,Pos),
- (worldget(none,Vis,None) ->
- worldget(none,Vis,None)
- ;
- None = none
- ),
- (worldget('0',Vis,Zero) ->
- worldget('0',Vis,Zero)
- ;
- Zero = '0'
- ),
- (SuitIn = [] ->
- (ValIn = [Zero|_] ->
- Suit1 = none
- ;
- Suit1 = c
- ),
- SuitFacts = []
- ;
- SuitIn = [Suit|SuitFacts],
- worldget_np(Suit1,Vis,Suit)
- ),
- (ValIn = [] ->
- (SuitIn = [None|_] ->
- Val1 = 0
- ;
- Val1 = a
- ),
- ValFacts = []
- ;
- ValIn = [Val|ValFacts],
- worldget_np(Val1,Vis,Val)
- ),
- getViewCards(World,States,PosFacts,SuitFacts,ValFacts,ViewCards).
- getViewCards(World,[_|States],[_|PosFacts],SuitIn,ValIn,ViewCards) :-
- (SuitIn = [] ->
- SuitFacts = []
- ;
- SuitIn = [_|SuitFacts]
- ),
- (ValIn = [] ->
- ValFacts = []
- ;
- ValIn = [_|ValFacts]
- ),
- getViewCards(World,States,PosFacts,SuitFacts,ValFacts,ViewCards).
- %
- % worldget-Prädikat, das keinen Spieler zurueckliefert
- worldget_np(NotPlayer,Vis,In) :-
- worldget(NotPlayer,Vis,In),
- dif(player1,NotPlayer),
- dif(player2,NotPlayer).
- %
- updateViewCards(ViewCards,[],ViewCards).
- updateViewCards(ViewCardsOld,[Key-Value|ViewCardsUpdate],ViewCardsNew) :-
- worldput(Key,ViewCardsOld,Value,ViewCardsNew1),
- updateViewCards(ViewCardsNew1,ViewCardsUpdate,ViewCardsNew).
- % gibt angeforderte Fakten zurueck
- getGdlFacts(World,Key,States,Facts) :-
- worldget(vis,World,Vis),
- worldget(Key,Vis,FactAtom),
- (FactAtom = '' ->
- States = [],
- Facts = []
- ;
- atom_codes(FactAtom,FactCode),
- termList(FactCode,FactTerms),
- getFacts(World,FactTerms,States,Facts)
- ).
- %
- termList([],[]).
- termList(FactCode,FactTerm) :-
- \+member(10,FactCode),
- atom_codes(FactAtom,FactCode),
- atom_term(FactAtom,FactTerm).
- termList(FactCode,[PrefixTerm|TermList]) :-
- sublist(FactCode,[43],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,FactCode,PrefixLength),
- append_length(_Part,Suffix,PartSuffix,PartLength),
- atom_codes(PrefixAtom,Prefix),
- atom_term(PrefixAtom,PrefixTerm),
- termList(Suffix,TermList).
- %
- getFacts(_,[],[],[]).
- getFacts(World,FactTerm,States,Facts) :-
- worldget(gdl,World,Gdl),
- FactTerm =.. [FactName_n,FactNumber_n],
- n_numberToAtom(FactName_n,FactName),
- n_numberToNumber(FactNumber_n,FactNumber),
- (worldget(FactName,Gdl,GdlFacts) ->
- GdlFacts = [States|_],
- nth0(FactNumber,GdlFacts,Facts)
- ;
- States = [],
- Facts = []
- ).
- getFacts(World,[FactTerm|FactTerms],States,Facts) :-
- worldget(gdl,World,Gdl),
- FactTerm =.. [FactName_n,FactNumber_n],
- n_numberToAtom(FactName_n,FactName),
- n_numberToNumber(FactNumber_n,FactNumber),
- (worldget(FactName,Gdl,GdlFacts) ->
- nth0(FactNumber,GdlFacts,Facts0),
- getFacts(World,FactTerms,States,Facts1),
- combineFacts(Facts,Facts0,Facts1)
- ;
- getFacts(World,FactTerms,States,Facts)
- ).
- %
- combineFacts([],[],[]).
- combineFacts([CombinedFact|CombinedFacts],[Fact0|Facts0],[Fact1|Facts1]) :-
- atom_concat(Fact0,'+',Fact0Plus),
- atom_concat(Fact0Plus,Fact1,CombinedFact),
- combineFacts(CombinedFacts,Facts0,Facts1).
- % loescht "n_" aus zu Atomen gezwungenen Zahlen
- n_numberToNumber(N_number,Number) :-
- atom_codes(N_number,N_numberCode),
- delete_n_(N_numberCode,NumberCode),
- number_codes(Number,NumberCode).
- n_numberToAtom(N_number,AtomNumber) :-
- atom_codes(N_number,N_numberCode),
- delete_n_(N_numberCode,NumberCode),
- atom_codes(AtomNumber,NumberCode).
- %
- delete_n_([],[]).
- delete_n_([110,95|Points],Numbers) :-
- delete_n_(Points,Numbers),!.
- delete_n_([Number|Points],[Number|Numbers]) :-
- delete_n_(Points,Numbers),!.
- /*------------------------ GDL Predicates ------------------------*/
- % GDL: distinct
- distinct(_,X,Y) :-
- dif(X,Y).
- % GDL: true
- true(World,Literal) :-
- worldget(state,World,State),
- atom_number(StateAtom,State),
- worldget(gdl,World,Gdl),
- Literal =.. [Constant|World_Terms],
- World_Terms = [_World|Terms],
- length(Terms,Length),
- atom_number(LengthAtom,Length),
- atom_concat(Constant,LengthAtom,ConstantLength),
- worldget(ConstantLength,Gdl,GdlTerms),
- Terms0 = [StateAtom|Terms],
- checkFacts(GdlTerms,Terms0).
- % GDL: does
- does(World,Player,Action) :-
- worldget(state,World,State),
- atom_number(StateAtom,State),
- worldget(vis,World,Vis),
- getGdlFacts(World,lead,LeadStates,LeadFacts),
- LeadTerms = [LeadStates,LeadFacts],
- PlayerTerms = [StateAtom,Player],
- checkFacts(LeadTerms,PlayerTerms),
- worldget(action,World,GameAction),
- atom_term(GameActionAtom,GameAction),
- worldget(GameActionAtom,Vis,ActionAtom),
- Action =.. [Constant|World_Terms],
- World_Terms = [_World|Terms],
- Action0 =.. [Constant|Terms],
- atom_term(ActionAtom,Action0).
- % supporting GDL Predicates
- checkFacts([],[]).
- checkFacts([GdlTerm|GdlTerms],[Term|Terms]) :-
- nth1(N,GdlTerm,Term),
- checkFacts(GdlTerms,Terms,N).
- checkFacts([],[],_).
- checkFacts([GdlTerm|GdlTerms],[Term|Terms],N) :-
- nth1(N,GdlTerm,Term),
- checkFacts(GdlTerms,Terms,N).
- /*---------------------- End: GDL Predicates ---------------------*/
- /*------------------------- GDL Parsing --------------------------*/
- % loescht ab allen ";" bis Zeilenende (Kommentare)
- code_deleteComments([],[]).
- code_deleteComments(Code,Code) :-
- \+member(59,Code).
- code_deleteComments(CodeIn,CodeOut) :-
- sublist(CodeIn,[59],PrefixLength1,_PartLength1,_SuffixLength1),
- append_length(Prefix1,Suffix1,CodeIn,PrefixLength1),
- sublist(Suffix1,[10],PrefixLength2,PartLength2,_PartLength2),
- AppendPosition is PrefixLength2 + PartLength2,
- append_length(_Prefix2,Suffix2,Suffix1,AppendPosition),
- code_deleteComments(Suffix2,Code),
- append([Prefix1,Code],CodeOut).
- % ersetzt X durch Y
- code_replace(_,[],_,[]).
- code_replace(X,Code,_,Code) :-
- (is_list(X) -> Xs = X ; Xs = [X]),
- \+sublist(Code,Xs,_,_,_).
- code_replace(X,CodeIn,Y,CodeOut) :-
- (is_list(X) -> Xs = X ; Xs = [X]),
- (is_list(Y) -> Ys = Y ; Ys = [Y]),
- sublist(CodeIn,Xs,PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(_Part,Suffix,PartSuffix,PartLength),
- code_replace(Xs,Suffix,Ys,Code),
- append([Prefix,Ys,Code],CodeOut).
- % ersetzt Zahlen "0"-"9" durch "n_0"-"n_9" (zwingt Zahlen zu Atome)
- code_numbersToAtoms(CodeIn,CodeOut) :-
- code_numbersToAtoms(48,57,CodeIn,CodeOut).
- code_numbersToAtoms(LastNumber,LastNumber,CodeIn,CodeOut) :-
- code_replace(LastNumber,CodeIn,[110,95,LastNumber],CodeOut).
- code_numbersToAtoms(Number,LastNumber,CodeIn,CodeOut) :-
- code_replace(Number,CodeIn,[110,95,Number],Code),
- NextNumber is Number +1,
- code_numbersToAtoms(NextNumber,LastNumber,Code,CodeOut).
- % loescht leere Zeilen am Anfang, Ende und nach leeren Zeilen
- code_deleteEmptyRows(CodeIn,CodeOut) :-
- CodeIn = [10|Rest],
- code_deleteEmptyRows(Rest,CodeOut).
- code_deleteEmptyRows(CodeIn,CodeOut) :-
- last(Rest,10,CodeIn),
- code_deleteEmptyRows(Rest,CodeOut).
- code_deleteEmptyRows(Code,Code) :-
- \+nextto(10,10,Code).
- code_deleteEmptyRows(CodeIn,CodeOut) :-
- code_replace([10,10],CodeIn,10,Code),
- code_deleteEmptyRows(Code,CodeOut).
- % fuegt Punkte an Zeilenenden ohne "-" oder "," ein
- code_addDots([],[]).
- code_addDots(Code,Code) :-
- last(_,46,Code),
- \+member(10,Code).
- code_addDots(CodeIn,CodeOut) :-
- append(CodeIn,[46],CodeOut),
- \+member(10,CodeIn).
- code_addDots(CodeIn,CodeOut) :-
- sublist(CodeIn,[10],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- last(Prefix,Last),
- dif(Last,45),
- dif(Last,44),
- code_addDots(Suffix,Code),
- append([Prefix,[46],Part,Code],CodeOut).
- code_addDots(CodeIn,CodeOut) :-
- sublist(CodeIn,[10],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- code_addDots(Suffix,Code),
- append([Prefix,Part,Code],CodeOut).
- % fuegt jedem Literal die Variable "World" als ersten Term hinzu
- code_addWorldVar([],[]).
- code_addWorldVar(Code,Code) :-
- \+member(10,Code),
- subseq0(Code,"World").
- code_addWorldVar(CodeIn,CodeOut) :-
- \+member(10,CodeIn),
- code_addWorldVar_check(CodeIn,CodeOut).
- code_addWorldVar(CodeIn,CodeOut) :-
- sublist(CodeIn,[10],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- code_addWorldVar_check(Prefix,Prefix1),
- code_addWorldVar(Suffix,Code),
- append([Prefix1,Part,Code],CodeOut).
- %
- code_addWorldVar_check([],[]).
- code_addWorldVar_check(CodeIn,CodeOut) :-
- \+member(40,CodeIn),
- sublist(CodeIn,[58,45],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- append([Prefix,"(World)",Part,Suffix],CodeOut).
- code_addWorldVar_check(CodeIn,CodeOut) :-
- \+member(40,CodeIn),
- sublist(CodeIn,[44],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- append([Prefix,"(World)",Part,Suffix],CodeOut).
- code_addWorldVar_check(CodeIn,CodeOut) :-
- \+member(40,CodeIn),
- sublist(CodeIn,[46],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- append([Prefix,"(World)",Part,Suffix],CodeOut).
- code_addWorldVar_check(CodeIn,CodeOut) :-
- code_addWorldVar_bracket(CodeIn,CodeOut).
- %
- code_addWorldVar_bracket([],[]).
- code_addWorldVar_bracket(Code,Code) :-
- \+member(40,Code).
- code_addWorldVar_bracket(CodeIn,CodeOut) :-
- sublist(CodeIn,[40],PrefixLength,PartLength,_SuffixLength),
- append_length(Prefix,PartSuffix,CodeIn,PrefixLength),
- append_length(Part,Suffix,PartSuffix,PartLength),
- code_addWorldVar_bracket(Suffix,Code),
- append([Prefix,Part,"World,",Code],CodeOut).
- /*----------------------- End: GDL Parsing -----------------------*/
- /*---------------------------- Utility ---------------------------*/
- % gibt eine angegebene Anzahl an leeren Listen zurueck
- listsLength0([],0).
- listsLength0([E|L],Number) :-
- length(E,0),
- NextNumber is Number - 1,
- listsLength0(L,NextNumber).
- % transformiert Atom zu Term oder Term zu Atom
- atom_term([],[]).
- atom_term(Atom,Term) :-
- (var(Atom) ->
- Term =.. [Functor|Arguments],
- (Arguments = [] ->
- Atom = Functor
- ;
- atom_concat(Functor,'(',FunctorBracket),
- atom_term(FunctorBracket,Arguments,Atom0),
- atom_concat(Atom0,')',Atom)
- )
- ;
- atom_concat(Atom,'.',AtomDot),
- atom_codes(AtomDot,AtomCode),
- read_from_codes(AtomCode,Term)
- ).
- atom_term(Atom,Atom,[]).
- atom_term(AtomIn,[Argument],AtomOut) :-
- (number(Argument) ->
- atom_number(ArgumentAtom,Argument)
- ;
- ArgumentAtom = Argument
- ),
- atom_concat(AtomIn,ArgumentAtom,AtomOut).
- atom_term(AtomIn,[Argument|Arguments],AtomOut) :-
- (number(Argument) ->
- atom_number(ArgumentAtom,Argument)
- ;
- ArgumentAtom = Argument
- ),
- atom_concat(AtomIn,ArgumentAtom,Atom0),
- atom_concat(Atom0,',',Atom),
- atom_term(Atom,Arguments,AtomOut).
- /*------------------------- End: Utility -------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement