changed a bit

This commit is contained in:
Walcher 2024-01-08 18:29:06 +01:00
parent e6ef99bb8b
commit 29e5431bc7
3 changed files with 84 additions and 111 deletions

82
.idea/workspace.xml generated
View file

@ -4,15 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment=""> <list default="true" id="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" name="Changes" comment="progress of 7/1">
<change beforePath="$PROJECT_DIR$/.idea/compiler.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Update_at_08_01_2024_12_06__Changes_.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Update_at_08_01_2024_12_06__Changes_.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/jarRepositories.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/jarRepositories.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" afterDir="false" />
</list>
<list id="2bf04d9d-9125-4ed6-9018-847e14e9822c" name="Changes by danie" comment="">
<change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/ab1/impl/GRUPPE/NFAImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/ab1/impl/GRUPPE/NFAImpl.class" afterDir="false" />
</list> </list>
<list id="2bf04d9d-9125-4ed6-9018-847e14e9822c" name="Changes by danie" comment="" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -42,36 +40,6 @@
&quot;second&quot;: &quot;e0c4ebfe-e254-4a34-93d3-a6ee2cb18f94&quot; &quot;second&quot;: &quot;e0c4ebfe-e254-4a34-93d3-a6ee2cb18f94&quot;
} }
}</component> }</component>
<component name="LineStatusTrackerManager">
<file path="$PROJECT_DIR$/src/main/java/ab1/impl/GRUPPE/NFAImpl.java">
<ranges>
<range start1="9" end1="9" start2="9" end2="10" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="37" end1="37" start2="38" end2="39" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="56" end1="61" start2="58" end2="68" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="62" end1="62" start2="69" end2="78" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="67" end1="71" start2="83" end2="86" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="72" end1="72" start2="87" end2="96" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="76" end1="77" start2="100" end2="101" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="94" end1="95" start2="118" end2="119" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="111" end1="112" start2="135" end2="136" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="122" end1="123" start2="146" end2="147" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="187" end1="188" start2="211" end2="212" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="230" end1="231" start2="254" end2="255" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="242" end1="242" start2="266" end2="267" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="254" end1="273" start2="279" end2="282" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="278" end1="279" start2="287" end2="288" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="287" end1="288" start2="296" end2="300" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="290" end1="291" start2="302" end2="303" changelist="2bf04d9d-9125-4ed6-9018-847e14e9822c" />
<range start1="292" end1="293" start2="304" end2="305" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="297" end1="310" start2="309" end2="313" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="312" end1="313" start2="315" end2="317" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="314" end1="317" start2="318" end2="321" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="377" end1="388" start2="381" end2="388" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
<range start1="389" end1="389" start2="389" end2="393" changelist="2bf04d9d-9125-4ed6-9018-847e14e9822c" />
<range start1="390" end1="404" start2="394" end2="394" changelist="f5e08808-a5af-4c89-a1ee-21b3bcb9ae3e" />
</ranges>
</file>
</component>
<component name="MarkdownSettingsMigration"> <component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" /> <option name="stateVersion" value="1" />
</component> </component>
@ -83,25 +51,25 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent">{ <component name="PropertiesComponent"><![CDATA[{
&quot;keyToString&quot;: { "keyToString": {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;, "RunOnceActivity.OpenProjectViewOnStart": "true",
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, "RunOnceActivity.ShowReadmeOnStart": "true",
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;, "WebServerToolWindowFactoryState": "false",
&quot;git-widget-placeholder&quot;: &quot;main&quot;, "git-widget-placeholder": "2-playing-around",
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;, "ignore.virus.scanning.warn.message": "true",
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;, "node.js.detected.package.eslint": "true",
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;, "node.js.detected.package.tslint": "true",
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.eslint": "(autodetect)",
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.tslint": "(autodetect)",
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;, "nodejs_package_manager_path": "npm",
&quot;project.structure.last.edited&quot;: &quot;Project&quot;, "project.structure.last.edited": "Project",
&quot;project.structure.proportion&quot;: &quot;0.0&quot;, "project.structure.proportion": "0.0",
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;, "project.structure.side.proportion": "0.0",
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;, "settings.editor.selected.configurable": "preferences.pluginManager",
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot; "vue.rearranger.settings.migration": "true"
} }
}</component> }]]></component>
<component name="RunManager"> <component name="RunManager">
<configuration name="ab1.tests in ETI_Abgabe" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="ab1.tests in ETI_Abgabe" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="ETI_Abgabe" /> <module name="ETI_Abgabe" />
@ -151,6 +119,10 @@
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="progress of 7/1" />
<option name="LAST_COMMIT_MESSAGE" value="progress of 7/1" />
</component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
<breakpoints> <breakpoints>

View file

@ -2,12 +2,10 @@ package ab1.impl.GRUPPE;
import ab1.FinalizedStateException; import ab1.FinalizedStateException;
import ab1.NFA; import ab1.NFA;
import ab1.NFAFactory;
import ab1.Transition; import ab1.Transition;
import lombok.Getter; import lombok.Getter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class NFAImpl implements NFA { public class NFAImpl implements NFA {
private Set<String> states; private Set<String> states;
@ -68,7 +66,7 @@ public class NFAImpl implements NFA {
this.transitions.add(transition); this.transitions.add(transition);
} }
public void addAllTransition(Set<Transition> transitions) throws FinalizedStateException { public void addAllTransitions(Set<Transition> transitions) throws FinalizedStateException {
if(this.isFinalized){ if(this.isFinalized){
throw new FinalizedStateException(); throw new FinalizedStateException();
} }
@ -96,6 +94,7 @@ public class NFAImpl implements NFA {
} }
// #TODO // #TODO
// handle case where the state is duplicated
@Override @Override
public NFA union(NFA other) throws FinalizedStateException { public NFA union(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) { if (!this.isFinalized || !other.isFinalized()) {
@ -114,6 +113,7 @@ public class NFAImpl implements NFA {
} }
// #TODO // #TODO
// needs to work with a DFA not NFA
@Override @Override
public NFA intersection(NFA other) throws FinalizedStateException { public NFA intersection(NFA other) throws FinalizedStateException {
if (!this.isFinalized || !other.isFinalized()) { if (!this.isFinalized || !other.isFinalized()) {
@ -162,20 +162,19 @@ public class NFAImpl implements NFA {
// copy, but without accepting states // copy, but without accepting states
nfa.states.addAll(this.states); nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions); nfa.addAllTransitions(this.transitions);
// adding the initial state as accepting state because we have to accept the empty string // adding the initial state as accepting state because we have to accept the empty string
nfa.acceptingStates.add(this.initialState); nfa.acceptingStates.add(this.initialState);
// for each accepting state // for each accepting state
for (String acceptingState : this.acceptingStates) { for (String acceptingState : this.getAcceptingStates()) {
Transition loopBackTransition = Transition loopBackTransition =
// creating an epsilon transition (null) for each accepting state // creating an epsilon transition (null) for each accepting state
new Transition(acceptingState, null, this.initialState); new Transition(acceptingState, null, this.initialState);
if (!nfa.transitions.contains(loopBackTransition)) {
nfa.transitions.add(loopBackTransition); nfa.transitions.add(loopBackTransition);
} }
}
return nfa; return nfa;
} }
@ -186,13 +185,22 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException(); throw new FinalizedStateException();
} }
NFAImpl nfa = new NFAImpl(this.initialState); String newInitialState = "newSTART";
NFAImpl nfa = new NFAImpl(newInitialState);
// simple copy // simple copy
nfa.states.addAll(this.states); nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions); nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates); nfa.acceptingStates.addAll(this.acceptingStates);
nfa.transitions.add(new Transition(newInitialState, null, this.initialState));
for (String acceptingState : this.acceptingStates) {
nfa.transitions.add(new Transition(acceptingState, null, newInitialState));
}
/*
// for each accepting state // for each accepting state
for (String acceptingState : this.acceptingStates) { for (String acceptingState : this.acceptingStates) {
Transition loopBackTransition = Transition loopBackTransition =
@ -202,6 +210,7 @@ public class NFAImpl implements NFA {
nfa.transitions.add(loopBackTransition); nfa.transitions.add(loopBackTransition);
} }
} }
*/
return nfa; return nfa;
} }
@ -213,9 +222,14 @@ public class NFAImpl implements NFA {
throw new FinalizedStateException(); throw new FinalizedStateException();
} }
NFAImpl complementNFA = new NFAImpl(this.initialState); // create a copy of this NFA
complementNFA.states.addAll(this.states); NFAImpl nfa = new NFAImpl(this.initialState);
complementNFA.transitions.addAll(this.transitions); nfa.states.addAll(this.states);
nfa.transitions.addAll(this.transitions);
nfa.acceptingStates.addAll(this.acceptingStates);
//nfa = nfa.convertNFAtoDFA(nfa);
/* /*
for(String state : this.states){ for(String state : this.states){
@ -248,22 +262,40 @@ public class NFAImpl implements NFA {
// #TODO // #TODO
@Override @Override
public boolean acceptsWord(String word) throws FinalizedStateException { public boolean acceptsWord(String word) throws FinalizedStateException {
//check if word is accepted if (!this.isFinalized) {
throw new FinalizedStateException();
}
Set<String> currentStates = new HashSet<>();
currentStates.add(this.initialState);
currentStates = epsilonClosure(currentStates, this);
for (char letter : word.toCharArray()) {
Set<String> nextStates = new HashSet<>();
for (String state : currentStates) {
nextStates.addAll(getStateAfterTransition(currentStates, letter, this));
}
nextStates = epsilonClosure(nextStates, this);
currentStates = nextStates;
}
for (String state : currentStates) {
if (this.acceptingStates.contains(state)) {
return true;
}
}
return false; return false;
} }
public NFA convertNFAtoDFA(NFAImpl input) { public NFA convertNFAtoDFA(NFAImpl input) {
/* // all accepting states of the DFA
What do we need to do?
Helper Methods:
- epsilonClosure
- getStateAfterTransition
- determineAcceptingStates
*/
Set<String> dfaAcceptingStates = new HashSet<>(); Set<String> dfaAcceptingStates = new HashSet<>();
// all states of the DFA // all states of the DFA
List<Set<String>> dfaStates = new ArrayList<>(); List<Set<String>> dfaStates = new ArrayList<>();
@ -299,11 +331,9 @@ public class NFAImpl implements NFA {
} }
} }
// build new Transition // build new Transition
dfaTransitions.add(new Transition(getIndexOfSet(currentState, dfaStates), letter, getIndexOfSet(newState, dfaStates))); dfaTransitions.add(new Transition(getIndexOfSet(currentState, dfaStates), letter, getIndexOfSet(newState, dfaStates)));
// we got to change this to only a string is given
//dfaTransitions.add(new Transition(currentState, letter, newState));
} }
} }
@ -313,7 +343,7 @@ public class NFAImpl implements NFA {
newDFA.states.add(""+i); newDFA.states.add(""+i);
} }
newDFA.addAllTransition(dfaTransitions); newDFA.addAllTransitions(dfaTransitions);
newDFA.addAllAcceptingStates(dfaAcceptingStates); newDFA.addAllAcceptingStates(dfaAcceptingStates);
newDFA.finalizeAutomaton(); newDFA.finalizeAutomaton();
@ -322,35 +352,6 @@ public class NFAImpl implements NFA {
} }
private Set<String> epsilonClosure(Set<String> states, NFA nfa) { private Set<String> epsilonClosure(Set<String> states, NFA nfa) {
/*
Original:
boolean isAccepting = false;
int index = 0;
List<Set<String>> DFA_states = new ArrayList<>();
Set<Transition> DFA_transitions = new HashSet<>();
// start state alle verbindungen mit epsilon als set
for(String state : states) {
for (Transition transition : input.transitions) {
if (state.equals(transition.fromState()) && transition.readSymbol() == null) {
if(!isAccepting) {
for (String acceptingState : acceptingStates)
if (transition.toState().equals(acceptingState)) {
isAccepting = true;
break;
}
}
states.add(transition.toState());
// funktioniert immer noch, wenn w epsilon Übergänge hintereinander?
}
}
}
DFA_states.add(new HashSet<>(states));
*/
Set<String> closure = new HashSet<>(states); Set<String> closure = new HashSet<>(states);
Stack<String> stack = new Stack<>(); Stack<String> stack = new Stack<>();
stack.addAll(states); stack.addAll(states);