forked from JavaTX/JavaCompilerCore
Umstellung auf MPair beginnen
This commit is contained in:
parent
fa337c5546
commit
43a8c9e647
96
src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java
Normal file
96
src/de/dhbwstuttgart/typeinference/UnifyConstraintsSet.java
Normal file
@ -0,0 +1,96 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
import de.dhbwstuttgart.logger.Logger;
|
||||
import de.dhbwstuttgart.logger.*;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||
|
||||
public class UnifyConstraintsSet extends UndMenge<MPair> implements Iterable<UnifyOderConstraint>{
|
||||
private static final Logger log = Logger.getLogger(UnifyConstraintsSet.class.getName());
|
||||
private Menge<UnifyOderConstraint> constraintsSet;
|
||||
|
||||
public UnifyConstraintsSet(){
|
||||
constraintsSet = new Menge<UnifyOderConstraint>();
|
||||
}
|
||||
|
||||
public void add(UnifyConstraintsSet CSet){
|
||||
for(UnifyOderConstraint element : CSet)
|
||||
add(element);
|
||||
}
|
||||
public void add(UnifyOderConstraint constraint){
|
||||
constraintsSet.add(constraint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String ret ="";
|
||||
for(UnifyOderConstraint constraint : this){
|
||||
ret += constraint.toString()+"\n";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Iterator<UnifyOderConstraint> iterator() {
|
||||
return constraintsSet.iterator();
|
||||
}
|
||||
|
||||
public void filterWrongConstraints(Unifier unify) {
|
||||
/*
|
||||
* Das ConstraintsSet enthält nur OderConstraints, welche UND-Verknüpft sind.
|
||||
* Hier werden Constraints in den OderConstraints kontrolliert:
|
||||
*/
|
||||
for(UnifyOderConstraint constraint : this){
|
||||
constraint.filterWrongConstraints(unify);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Nimmt alle UndConstraints und filtert mithilfe dieser die falschen Constraints aus den OderConstraints
|
||||
* @param unifier
|
||||
*/
|
||||
public void unifyUndConstraints(Unifier unifier) {
|
||||
Vector<UnifyUndConstraint> uCons = this.filterUndConstraints();
|
||||
Vector<MPair> alleUndConstraints = new Vector<>();
|
||||
for(UnifyUndConstraint undConstraint : uCons){
|
||||
alleUndConstraints.addAll(undConstraint.getConstraintPairs());
|
||||
}
|
||||
this.filterWrongConstraints(
|
||||
(pairs)->{
|
||||
Set<MPair> undConstraintsUndPairs = new Menge<>();
|
||||
undConstraintsUndPairs.addAll(pairs);
|
||||
undConstraintsUndPairs.addAll(alleUndConstraints);
|
||||
log.debug("Versuche Pairs auszusondern:\n"+pairs, Section.TYPEINFERENCE);
|
||||
log.debug("Unifiziere:\n"+undConstraintsUndPairs, Section.TYPEINFERENCE);
|
||||
Set<Set<MPair>> unifyResult = unifier.apply(undConstraintsUndPairs);
|
||||
return unifyResult;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Aus dem ConstraintsSet [ u1, u2, ... (OderConstraint), ... uN ] werden alle
|
||||
* UndConstraints, welche sich nicht innerhalb eines OderConstraints befinden, herausgefiltert
|
||||
* @return [u1, ... , uN]
|
||||
*/
|
||||
private Vector<UnifyUndConstraint> filterUndConstraints() {
|
||||
Vector<UnifyUndConstraint> ret = new Vector<>();
|
||||
for(UnifyOderConstraint con : constraintsSet){
|
||||
UnifyUndConstraint filtered = con.filterUndConstraints();
|
||||
if(filtered != null)ret.add(filtered);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void add(UnifyUndConstraint singleConstraint) {
|
||||
UnifyOderConstraint toAdd = new UnifyOderConstraint();
|
||||
toAdd.addConstraint(singleConstraint);
|
||||
constraintsSet.add(toAdd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<MPair>> getSet() {
|
||||
return this.constraintsSet;
|
||||
}
|
||||
}
|
112
src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java
Normal file
112
src/de/dhbwstuttgart/typeinference/UnifyOderConstraint.java
Normal file
@ -0,0 +1,112 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
|
||||
import de.dhbwstuttgart.logger.Logger;
|
||||
import de.dhbwstuttgart.logger.Section;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||
|
||||
public class UnifyOderConstraint extends OderMenge<MPair>{
|
||||
private Set<UnifyUndConstraint> oderConstraintPairs;
|
||||
|
||||
private final static Logger logger = Logger.getLogger(UnifyOderConstraint.class.getName());
|
||||
|
||||
/**
|
||||
* Erstellt ein neues Oder Constraint und f<EFBFBD>gt bereits ein Constraint hinzu.
|
||||
* @param p1
|
||||
* @param p2
|
||||
|
||||
public OderConstraint(Pair p1, Pair p2){
|
||||
if(p1 == null || p2 == null)throw new NullPointerException();
|
||||
Pair constraintPair = new Pair(p1,p2);
|
||||
oderConstraintPairs = new Menge<UndConstraint>();
|
||||
this.addConstraint(constraintPair);
|
||||
}
|
||||
*/
|
||||
|
||||
public UnifyOderConstraint(){
|
||||
oderConstraintPairs = new Menge<UnifyUndConstraint>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Liefert alle in diesem OderConstraint enthaltene Constraints. Dabei gehen die Verkn<EFBFBD>pfungen (Oder/Und) verloren.
|
||||
* @return
|
||||
*/
|
||||
public Menge<MPair> getConstraintPairs(){
|
||||
Menge<MPair> ret = new Menge<MPair>();
|
||||
for(UnifyUndConstraint oC : this.oderConstraintPairs){
|
||||
ret.addAll(oC.getConstraintPairs());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Falls die Type des toAdd-Pairs nicht vom Typ RefType bzw. TypePlaceholder sind, so werden sie in einen RefType umgewandelt.
|
||||
* @param toAdd
|
||||
*/
|
||||
public void addConstraint(MPair toAdd){
|
||||
oderConstraintPairs.add(new UnifySingleConstraint(toAdd));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String ret = "[";
|
||||
for(UnifyUndConstraint p : this.getUndConstraints()){
|
||||
ret += p.toString()+ "| ";
|
||||
}
|
||||
return ret+"]";
|
||||
}
|
||||
|
||||
public Set<UnifyUndConstraint> getUndConstraints() {
|
||||
return this.oderConstraintPairs;
|
||||
/*
|
||||
Vector<UndConstraint> ret = new Vector<UndConstraint>();
|
||||
for(Pair p : this.getConstraintPairs()){
|
||||
ret.add(new UndConstraint(p.TA1,p.TA2));
|
||||
}
|
||||
return ret;
|
||||
*/
|
||||
}
|
||||
|
||||
public void addConstraint(UnifyUndConstraint constraint) {
|
||||
oderConstraintPairs.add(constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filtert die Constraints in diesem ODER-Verknüpften Constraint aus,
|
||||
* welche keinen Sinn ergeben, also beim unifizieren scheitern.
|
||||
* @param unifier - Wird für die Unifizierung benutzt
|
||||
*/
|
||||
void filterWrongConstraints(Unifier unifier) {
|
||||
Set<UnifyUndConstraint> filteredConstraints = new Menge<>();
|
||||
for(UnifyUndConstraint cons : this.getUndConstraints()){
|
||||
Set<Set<MPair>> unifierResult = unifier.apply(cons.getConstraintPairs());
|
||||
if(!unifierResult.isEmpty()){
|
||||
filteredConstraints.add(cons);
|
||||
}else{
|
||||
logger.debug("Ausgesondertes Constraint: "+cons, Section.TYPEINFERENCE);
|
||||
}
|
||||
}
|
||||
this.oderConstraintPairs = filteredConstraints;
|
||||
}
|
||||
|
||||
UnifyUndConstraint filterUndConstraints() {
|
||||
if(this.oderConstraintPairs.size()==1){
|
||||
return this.oderConstraintPairs.iterator().next();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<? extends KomplexeMenge<MPair>> getSet() {
|
||||
return this.oderConstraintPairs;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.GenericTypeVar;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
|
||||
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||
|
||||
|
||||
public class UnifySingleConstraint extends UnifyUndConstraint{
|
||||
|
||||
private MPair constraintPair; //entspricht θ condition θ'
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<MPair>> getSet() {
|
||||
Menge<EinzelElement<MPair>> ret = new Menge<>();
|
||||
ret.add(new EinzelElement<>(constraintPair));
|
||||
return ret;
|
||||
}
|
||||
|
||||
public UnifySingleConstraint(MPair toAdd) {
|
||||
this.addConstraint(toAdd);
|
||||
}
|
||||
|
||||
public MPair getPair(){
|
||||
return constraintPair;
|
||||
}
|
||||
|
||||
@Override //Methode überschreiben, damit immer nur ein Vector mit nur einem Element zurückgeliefert wird.
|
||||
public Menge<MPair> getConstraintPairs(){
|
||||
Menge<MPair> ret = new Menge<>();
|
||||
ret.add(constraintPair);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void addConstraint(MPair toAdd){
|
||||
if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich.");
|
||||
|
||||
constraintPair = toAdd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return constraintPair.toString();
|
||||
}
|
||||
}
|
44
src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java
Normal file
44
src/de/dhbwstuttgart/typeinference/UnifyUndConstraint.java
Normal file
@ -0,0 +1,44 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
import de.dhbwstuttgart.typeinference.unify.model.MPair;
|
||||
|
||||
/**
|
||||
* Stellt ein Constraint dar, welches aus mehreren Constraint-Paaren besteht. Diese gelten alle stets gleichzeitig / sind per "Und" miteinander verknüpft.
|
||||
* @author janulrich
|
||||
*
|
||||
*/
|
||||
public class UnifyUndConstraint extends UndMenge<MPair> {
|
||||
|
||||
Menge<EinzelElement<MPair>> set = new Menge<>();
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<MPair>> getSet() {
|
||||
return set;
|
||||
}
|
||||
|
||||
public Set<MPair> getConstraintPairs() {
|
||||
Set<Set<MPair>> ret = this.cartesianProduct();
|
||||
if(ret.size() != 1){
|
||||
//UndConstraints enthalten nur SingleConstraints, wodurch das Karthesische Produkt nur aus einem Element bestehen kann.
|
||||
throw new DebugException("Fehler in ConstraintPairs-Bildung");
|
||||
}
|
||||
return ret.iterator().next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String ret = this.getConstraintPairs().toString();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void add(MPair pair){
|
||||
set.add(new EinzelElement<>(pair));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user