forked from JavaTX/JavaCompilerCore
ConstraintPair erbt von Pair
This commit is contained in:
parent
15e9fb44b8
commit
e6606d08ee
@ -1,15 +1,9 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
public class ConstraintPair {
|
||||
public class ConstraintPair extends Pair{
|
||||
|
||||
private Pair p;
|
||||
|
||||
public ConstraintPair(ConstraintType t1, ConstraintType t2){
|
||||
p = new Pair(t1.getType(), t2.getType());
|
||||
}
|
||||
|
||||
public Pair getPair(){
|
||||
return p;
|
||||
super(t1.getType(), t2.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import de.dhbwstuttgart.logger.Logger;
|
||||
import de.dhbwstuttgart.logger.*;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
|
||||
public class ConstraintsSet implements Iterable<OderConstraint>{
|
||||
public class ConstraintsSet extends UndMenge<Pair> implements Iterable<OderConstraint>{
|
||||
private static final Logger log = Logger.getLogger(ConstraintsSet.class.getName());
|
||||
private Menge<OderConstraint> constraintsSet;
|
||||
|
||||
@ -21,19 +21,6 @@ public class ConstraintsSet implements Iterable<OderConstraint>{
|
||||
public void add(OderConstraint constraint){
|
||||
constraintsSet.add(constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert alle Constraint-Variationen
|
||||
* @return
|
||||
*/
|
||||
public Vector<Vector<UndConstraint>> getConstraints(){
|
||||
Vector<Vector<UndConstraint>> ret = new Vector<Vector<UndConstraint>>();
|
||||
for(OderConstraint con : constraintsSet){
|
||||
ret.add(con.getUndConstraints());
|
||||
}
|
||||
ret = new KarthesischesProdukt<UndConstraint>().berechneKarthesischesProdukt(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
@ -66,12 +53,12 @@ public class ConstraintsSet implements Iterable<OderConstraint>{
|
||||
}
|
||||
this.filterWrongConstraints(
|
||||
(pairs)->{
|
||||
Vector<Pair> undConstraintsUndPairs = new Vector<>();
|
||||
Menge<Pair> 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);
|
||||
Vector<Vector<Pair>> unifyResult = unifier.apply(undConstraintsUndPairs);
|
||||
Menge<Menge<Pair>> unifyResult = unifier.apply(undConstraintsUndPairs);
|
||||
return unifyResult;
|
||||
});
|
||||
}
|
||||
@ -89,4 +76,15 @@ public class ConstraintsSet implements Iterable<OderConstraint>{
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void add(UndConstraint singleConstraint) {
|
||||
OderConstraint toAdd = new OderConstraint();
|
||||
toAdd.addConstraint(singleConstraint);
|
||||
constraintsSet.add(toAdd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<Pair>> getSet() {
|
||||
return this.constraintsSet;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
|
||||
public class OderConstraint{
|
||||
public class OderConstraint extends OderMenge<Pair>{
|
||||
private Menge<UndConstraint> oderConstraintPairs;
|
||||
|
||||
private final static Logger logger = Logger.getLogger(OderConstraint.class.getName());
|
||||
@ -35,8 +35,8 @@ public class OderConstraint{
|
||||
* Liefert alle in diesem OderConstraint enthaltene Constraints. Dabei gehen die Verkn<EFBFBD>pfungen (Oder/Und) verloren.
|
||||
* @return
|
||||
*/
|
||||
public Vector<Pair> getConstraintPairs(){
|
||||
Vector<Pair> ret = new Vector<Pair>();
|
||||
public Menge<Pair> getConstraintPairs(){
|
||||
Menge<Pair> ret = new Menge<Pair>();
|
||||
for(UndConstraint oC : this.oderConstraintPairs){
|
||||
ret.addAll(oC.getConstraintPairs());
|
||||
}
|
||||
@ -85,9 +85,9 @@ public class OderConstraint{
|
||||
}
|
||||
|
||||
void filterWrongConstraints(Unifier unifier) {
|
||||
Vector<UndConstraint> filteredConstraints = new Vector<>();
|
||||
Menge<UndConstraint> filteredConstraints = new Menge<>();
|
||||
for(UndConstraint cons : this.getUndConstraints()){
|
||||
Vector<Vector<Pair>> unifierResult = unifier.apply(cons.getConstraintPairs());
|
||||
Menge<Menge<Pair>> unifierResult = unifier.apply(cons.getConstraintPairs());
|
||||
if(!unifierResult.isEmpty()){
|
||||
filteredConstraints.add(cons);
|
||||
}else{
|
||||
@ -103,5 +103,11 @@ public class OderConstraint{
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<Pair>> getSet() {
|
||||
return this.oderConstraintPairs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -18,83 +18,32 @@ import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
* Item
|
||||
*/
|
||||
|
||||
interface Mapper<A,B>{
|
||||
B applyOrSet(OderMenge<A> i);
|
||||
B applyAndSet(UndMenge<A> i);
|
||||
B applySingle(EinzelElement<A> i);
|
||||
}
|
||||
|
||||
interface KomplexeMenge<A>{
|
||||
void addItems(KomplexeMenge<A> item);
|
||||
void addItem(A item);
|
||||
Menge<? extends KomplexeMenge<A>> getSet();
|
||||
Menge<Menge<A>> cartesianProduct();
|
||||
<B> KomplexeMenge<B> map(Mapper<A,B> m);
|
||||
}
|
||||
|
||||
public class OderMenge<A> implements KomplexeMenge<A>{
|
||||
Menge<KomplexeMenge<A>> set = new Menge<>();
|
||||
|
||||
@Override
|
||||
public void addItems(KomplexeMenge<A> item) {
|
||||
set.add(item);
|
||||
}
|
||||
|
||||
/*
|
||||
public Menge<A> getItems(){
|
||||
Menge<A> ret = new Menge<>();
|
||||
for(KomplexeMenge<A> i : set){
|
||||
ret.addAll(i.getItems());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addItem(A item) {
|
||||
set.add(new EinzelElement<A>(item));
|
||||
}
|
||||
public abstract class OderMenge<A> implements KomplexeMenge<A>{
|
||||
|
||||
public abstract Menge<? extends KomplexeMenge<A>> getSet();
|
||||
|
||||
@Override
|
||||
public Menge<Menge<A>> cartesianProduct() {
|
||||
Menge<Menge<A>> ret = new Menge<>();
|
||||
for(KomplexeMenge<A> km : this.set){
|
||||
for(KomplexeMenge<A> km : this.getSet()){
|
||||
ret.addAll(km.cartesianProduct());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Menge<OderMenge<A>> getOrSets() {
|
||||
Menge<OderMenge<A>> ret = new Menge<>();
|
||||
ret.add(this);
|
||||
//for(KomplexeMenge<A> i : set){
|
||||
// ret.addAll(i.getOrSets());
|
||||
//}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<UndMenge<A>> getAndSets() {
|
||||
Menge<UndMenge<A>> ret = new Menge<>();
|
||||
for(KomplexeMenge<A> i : set){
|
||||
ret.addAll(i.getAndSets());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
String ret = "[";
|
||||
for(KomplexeMenge<A> i : set){
|
||||
for(KomplexeMenge<A> i : this.getSet()){
|
||||
ret += i.toString() + " | ";
|
||||
}
|
||||
return ret + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> KomplexeMenge<B> map(Mapper<A, B> m) {
|
||||
return m.applyOrSet(this);
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
|
||||
*
|
||||
*/
|
||||
public class SingleConstraint extends UndConstraint{
|
||||
|
||||
private Pair constraintPair; //entspricht θ condition θ'
|
||||
//private R condition; //entspricht der condition (R)
|
||||
|
||||
@ -39,8 +40,8 @@ public class SingleConstraint extends UndConstraint{
|
||||
}
|
||||
|
||||
@Override //Methode überschreiben, damit immer nur ein Vector mit nur einem Element zurückgeliefert wird.
|
||||
public Vector<Pair> getConstraintPairs(){
|
||||
Vector<Pair> ret = new Vector<Pair>();
|
||||
public Menge<Pair> getConstraintPairs(){
|
||||
Menge<Pair> ret = new Menge<Pair>();
|
||||
ret.add(constraintPair);
|
||||
return ret;
|
||||
}
|
||||
@ -48,28 +49,9 @@ public class SingleConstraint extends UndConstraint{
|
||||
public void addConstraint(ConstraintPair toAdd){
|
||||
if(constraintPair != null)throw new DebugException("Ein Constraint darf nur aus einem ConstraintPair bestehen. Das hinzufügen von "+ toAdd + " ist nicht möglich.");
|
||||
|
||||
Type p1 = toAdd.getPair().TA1;
|
||||
Type p2 = toAdd.getPair().TA2;
|
||||
Type p1 = toAdd.TA1;
|
||||
Type p2 = toAdd.TA2;
|
||||
if(p1==null || p2 == null)throw new NullPointerException();
|
||||
|
||||
|
||||
//Hier werden die GTVs zu TPH gewandelt.
|
||||
//if(p1 instanceof RefType)((RefType)p1).GTV2TPH();
|
||||
//if(p2 instanceof RefType)((RefType)p2).GTV2TPH();
|
||||
|
||||
//if((p1 instanceof GenericTypeVar))p1 = ((GenericTypeVar)p1).getTypePlaceHolder();//throw new DebugException("GenericTypeVar sind in den Constraints nicht erlaubt");//
|
||||
//if((p2 instanceof GenericTypeVar))p2 = ((GenericTypeVar)p2).getTypePlaceHolder();//throw new DebugException("GenericTypeVar sind in den Constraints nicht erlaubt");//
|
||||
|
||||
// BaseTypes werden in RefTypes umgewandelt. Constraints dürfen nur RefTypes oder TypePlaceholder enthalten, da sonst der Unify-Algorithmus nicht funktioniert.
|
||||
//if(!(p1 instanceof RefType) && !(p1 instanceof TypePlaceholder) && !(p1 instanceof GenericTypeVar))p1 = new RefType(p1);
|
||||
//if(!(p2 instanceof RefType) && !(p2 instanceof TypePlaceholder) && !(p2 instanceof GenericTypeVar))p2 = new RefType(p2);
|
||||
|
||||
//if(!(TypePlaceholder.class.isInstance(p1)) || !(RefType.class.isInstance(p1)) || !(TypePlaceholder.class.isInstance(p2)) || !(RefType.class.isInstance(p2)))
|
||||
//{//Wenn die beiden übergebenen Typen weder RefTypes noch TypePlaceholder sind:
|
||||
// throw new TypinferenzException("Ein Constraint darf nur aus TypePlaceholdern und Reftypes bestehen");
|
||||
//}
|
||||
//if(!(p1 instanceof RefType) && !(p1 instanceof TypePlaceholder))throw new DebugException("Fehler: "+p2+" kann nicht in TPH oder RefType umgewandelt werden");
|
||||
//if(!(p2 instanceof RefType) && !(p2 instanceof TypePlaceholder))throw new DebugException("Fehler: "+p2+" kann nicht in TPH oder RefType umgewandelt werden");
|
||||
|
||||
constraintPair = new Pair(p1,p2);
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package de.dhbwstuttgart.typeinference;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Vector;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.type.Type;
|
||||
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
|
||||
import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
|
||||
/**
|
||||
@ -10,14 +12,32 @@ import de.dhbwstuttgart.typeinference.unify.Unifier;
|
||||
* @author janulrich
|
||||
*
|
||||
*/
|
||||
public class UndConstraint extends OderConstraint {
|
||||
public class UndConstraint extends UndMenge<Pair> {
|
||||
|
||||
public UndConstraint(ConstraintType p1, ConstraintType p2) {
|
||||
super(p1, p2);
|
||||
Menge<EinzelElement<Pair>> set = new Menge<>();
|
||||
|
||||
@Override
|
||||
public Menge<? extends KomplexeMenge<Pair>> getSet() {
|
||||
return set;
|
||||
}
|
||||
|
||||
public UndConstraint() {
|
||||
super();
|
||||
public Menge<Pair> getConstraintPairs() {
|
||||
Menge<Menge<Pair>> 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.firstElement();
|
||||
}
|
||||
|
||||
public void addConstraint(ConstraintType type, ConstraintType rT) {
|
||||
Pair p = new ConstraintPair(type, rT);
|
||||
this.set.add(new EinzelElement<Pair>(p));
|
||||
}
|
||||
|
||||
/*
|
||||
public UndConstraint(ConstraintType p1, ConstraintType p2) {
|
||||
super(p1, p2);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -47,4 +67,5 @@ public class UndConstraint extends OderConstraint {
|
||||
UndConstraint filterUndConstraints(){
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -5,33 +5,15 @@ import java.util.Iterator;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public class UndMenge<A> implements KomplexeMenge<A>{
|
||||
Menge<KomplexeMenge<A>> set = new Menge<>();
|
||||
|
||||
@Override
|
||||
public void addItems(KomplexeMenge<A> item) {
|
||||
set.add(item);
|
||||
}
|
||||
|
||||
public void addItem(A item){
|
||||
set.add(new EinzelElement<A>(item));
|
||||
}
|
||||
|
||||
/*
|
||||
public Menge<A> getItems(){
|
||||
Menge<A> ret = new Menge<>();
|
||||
for(KomplexeMenge<A> i : set){
|
||||
ret.addAll(i.getItems());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
public abstract class UndMenge<A> implements KomplexeMenge<A>{
|
||||
|
||||
public abstract Menge<? extends KomplexeMenge<A>> getSet();
|
||||
|
||||
@Override
|
||||
public Menge<Menge<A>> cartesianProduct() {
|
||||
Menge<Menge<A>> ret = null;
|
||||
Cloner cloner = new Cloner();
|
||||
for(KomplexeMenge<A> km : this.set){
|
||||
for(KomplexeMenge<A> km : this.getSet()){
|
||||
if(ret == null){
|
||||
ret = km.cartesianProduct();
|
||||
}else{
|
||||
@ -49,29 +31,9 @@ public class UndMenge<A> implements KomplexeMenge<A>{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Menge<OderMenge<A>> getOrSets() {
|
||||
Menge<OderMenge<A>> ret = new Menge<>();
|
||||
for(KomplexeMenge<A> i : set){
|
||||
ret.addAll(i.getOrSets());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<UndMenge<A>> getAndSets() {
|
||||
Menge<UndMenge<A>> ret = new Menge<>();
|
||||
ret.add(this);
|
||||
//for(KomplexeMenge<A> i : set){
|
||||
// ret.addAll(i.getAndSets());
|
||||
//}
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
public String toString(){
|
||||
String ret = "[";
|
||||
for(KomplexeMenge<A> item : set){
|
||||
for(KomplexeMenge<A> item : this.getSet()){
|
||||
ret += item.toString() + " , ";
|
||||
}
|
||||
return ret + "]";
|
||||
@ -85,25 +47,12 @@ class EinzelElement<A> implements KomplexeMenge<A>{
|
||||
public EinzelElement(A element){
|
||||
item = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addItems(KomplexeMenge<A> item) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Menge<A> getItems() {
|
||||
Menge<A> ret = new Menge<>();
|
||||
ret.add(item);
|
||||
public Menge<KomplexeMenge<A>> getSet(){
|
||||
Menge<KomplexeMenge<A>> ret = new Menge<>();
|
||||
ret.add(this);
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void addItem(A item) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<Menge<A>> cartesianProduct() {
|
||||
@ -115,20 +64,6 @@ class EinzelElement<A> implements KomplexeMenge<A>{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Menge<OderMenge<A>> getOrSets() {
|
||||
Menge<OderMenge<A>> ret = new Menge<>();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menge<UndMenge<A>> getAndSets() {
|
||||
Menge<UndMenge<A>> ret = new Menge<>();
|
||||
return ret;
|
||||
}
|
||||
*/
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return item.toString();
|
||||
|
@ -630,6 +630,8 @@ public class Unify
|
||||
}
|
||||
}
|
||||
//Schritt 4, Teil 2: Kartesisches Produkt bilden.
|
||||
|
||||
/*
|
||||
//TODO: Vor der Bildung des Karthesischen Produkts unmögliche Kombinationen ausfiltern
|
||||
//cartProduktSets kontrollieren:
|
||||
ConstraintsSet cSet = new ConstraintsSet();
|
||||
@ -661,22 +663,12 @@ public class Unify
|
||||
cSet.filterWrongConstraints(filterUnify);
|
||||
}
|
||||
|
||||
/*
|
||||
for (Menge<Menge<Pair>> vecvecpair : cartProduktSets){
|
||||
OderConstraint orConstraints = new OderConstraint();
|
||||
for(Menge<Pair> pairs : vecvecpair){
|
||||
UndConstraint uCons = new UndConstraint();
|
||||
for(Pair p : pairs){
|
||||
uCons.addConstraint(new ConstraintPair(p.TA1));
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Menge<Menge<Pair>> bigCartProductErg = cSet.cartesianProduct();
|
||||
if(filter)log.debug("Karthesisches Produkt nach Filterung: "+bigCartProductErg);
|
||||
*/
|
||||
|
||||
/*
|
||||
///*
|
||||
//Hier wird aus den in Schritt 4, Teil 1 erzeugten Vektoren das Kartesische Produkt gebildet.
|
||||
Menge<Pair> helpvp;
|
||||
Menge<Menge<Pair>> bigCartProductErg = new Menge<Menge<Pair>>();
|
||||
@ -696,7 +688,7 @@ public class Unify
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
//*/
|
||||
|
||||
//Schritt 5: Einsetzen der Subst Regel
|
||||
//Hier werden die TPHs substituiert, und dann nach geänderten und nicht geänderten Sets sortiert.
|
||||
|
Loading…
Reference in New Issue
Block a user