Commit 1f7bc30c authored by Ryan Wisnesky's avatar Ryan Wisnesky
Browse files

generalize constraint import

parent a58d1cd1
Loading
Loading
Loading
Loading
+7 −11
Changes for src/main/java/catdata/aql/Constraints.java: 7 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -43,17 +43,13 @@ public class Constraints<Ty, En, Sym, Fk, Att> implements Semantics {
	public Constraints(Schema<Ty, En, Sym, Fk, Att> schema, Collection<ED<Ty, En, Sym, Fk, Att>> eds, AqlOptions options) {
		this.eds = new HashSet<>(desugar(eds, options));
		this.schema = schema;
		for (ED<Ty, En, Sym, Fk, Att> ed : eds) {
			if (!ed.schema.equals(schema)) {
				throw new RuntimeException("The ED " + ed + "\n is on schema " + ed.schema + "\n\n, not " + schema + " as expected.");
			}
		}
		
	}

	private static <Ty, En, Sym, Fk, Att> Collection<ED<Ty, En, Sym, Fk, Att>> desugar(Collection<ED<Ty, En, Sym, Fk, Att>> eds, AqlOptions options) {
		List<ED<Ty, En, Sym, Fk, Att>> l = new LinkedList<>();
		for (ED<Ty, En, Sym, Fk, Att> x : eds) {
			l.add(new ED<>(x.schema, x.As, x.Es, x.Awh, x.Ewh, false, options));
			l.add(new ED<>(x.As, x.Es, x.Awh, x.Ewh, false, options));
			
			if (x.isUnique) {
				Ctx<Var, En> es2 = x.Es.map((v,t) -> new Pair<>(new Var(v + "0"), t));	
@@ -74,7 +70,7 @@ public class Constraints<Ty, En, Sym, Fk, Att> implements Semantics {
				for (Pair<Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> p : x.Ewh) {
					awh.add(new Pair<>(p.first.subst(subst), p.second.subst(subst)));
				}
				l.add(new ED<>(x.schema, as, new Ctx<>(), awh, ewh, false, options));
				l.add(new ED<>(as, new Ctx<>(), awh, ewh, false, options));
			}
			
		}
@@ -88,11 +84,11 @@ public class Constraints<Ty, En, Sym, Fk, Att> implements Semantics {
	
	public <Gen, Sk, X, Y> Instance<Ty, En, Sym, Fk, Att, ?, ?, ?, ?> chase(Instance<Ty, En, Sym, Fk, Att, Gen, Sk, X, Y> I, AqlOptions options) {	
		for (ED<Ty, En, Sym, Fk, Att> ed : eds) {
			Frozen<Ty, En, Sym, Fk, Att> f = ed.Q.ens.get(ED.WHICH.FRONT);
			Frozen<Ty, En, Sym, Fk, Att> f = ed.getQ(schema).ens.get(ED.WHICH.FRONT);
			if (!f.algebra().hasFreeTypeAlgebraOnJava()) {
				throw new RuntimeException("Cannot chase, unsafe use of java in front of\n" + ed);
			}
			f = ed.Q.ens.get(ED.WHICH.BACK);
			f = ed.getQ(schema).ens.get(ED.WHICH.BACK);
			if (!f.algebra().hasFreeTypeAlgebraOnJava()) {
				throw new RuntimeException("Cannot chase, unsafe use of java in back of\n" + ed);
			}
@@ -124,7 +120,7 @@ public class Constraints<Ty, En, Sym, Fk, Att> implements Semantics {
	

		for (Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>> t : T) {
			Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = t.first.Q;
			Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = t.first.getQ(schema);
			Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> A = Q.ens.get(WHICH.FRONT);
			Instance<Ty, En, Sym, Fk, Att, Var, Var, ID, Chc<Var, Pair<ID, Att>>> E = Q.ens.get(WHICH.BACK);

@@ -164,7 +160,7 @@ public class Constraints<Ty, En, Sym, Fk, Att> implements Semantics {
		Collection<Pair<ED<Ty, En, Sym, Fk, Att>, Row<WHICH, X>>> T = new LinkedList<>();

		for (ED<Ty, En, Sym, Fk, Att> ed : eds) {
			Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = ed.Q;
			Query<Ty, En, Sym, Fk, Att, WHICH, Unit, Void> Q = ed.getQ(schema);
			EvalInstance<Ty, En, Sym, Fk, Att, Gen, Sk, WHICH, Unit, Void, X, Y> QI = new EvalInstance<>(Q, I, options);
			outer: for (Row<WHICH, X> e : QI.algebra().en(WHICH.FRONT)) {
				for (Row<WHICH, X> a : QI.algebra().en(WHICH.BACK)) {
+23 −14
Changes for src/main/java/catdata/aql/ED.java: 23 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -32,7 +32,14 @@ public class ED<Ty, En, Sym, Fk, Att> {
		return ret;
	}
	
	public final Query<Ty,En,Sym,Fk,Att,WHICH,Unit,Void> Q;
	private Map<Schema<Ty, En, Sym, Fk, Att>, Query<Ty,En,Sym,Fk,Att,WHICH,Unit,Void>> cache = new HashMap<>();
	public final Query<Ty,En,Sym,Fk,Att,WHICH,Unit,Void> getQ(Schema<Ty, En, Sym, Fk, Att> schema) {
		if (!cache.containsKey(schema)) {
			Schema<Ty, WHICH, Sym, Unit, Void> zzz = getEDSchema(schema.typeSide, options);
			cache.put(schema, Query.makeQuery(is, new Ctx<>(), fks, schema, zzz, false, false)); //TODO AQL speed these can be set to true
		}
		return cache.get(schema);
	}

	@Override
	public String toString() {
@@ -81,7 +88,7 @@ public class ED<Ty, En, Sym, Fk, Att> {
		return toString;
	}

	public final Schema<Ty, En, Sym, Fk, Att> schema;
//	public final Schema<Ty, En, Sym, Fk, Att> schema;
	
	public final Ctx<Var, En> As;

@@ -107,8 +114,14 @@ public class ED<Ty, En, Sym, Fk, Att> {
		return ret;
	}
	
	public ED(Schema<Ty, En, Sym, Fk, Att> schema, Ctx<Var, En> as, Ctx<Var, En> es, Set<Pair<Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> awh, Set<Pair<Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> ewh, boolean isUnique, AqlOptions options) {
		this.schema = schema;
	Ctx<WHICH, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> 
	is = new Ctx<>();

	Ctx<Unit, Pair<Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>>, Boolean>> 
	fks = new Ctx<>();

	public ED(/* Schema<Ty, En, Sym, Fk, Att> schema, */ Ctx<Var, En> as, Ctx<Var, En> es, Set<Pair<Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> awh, Set<Pair<Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> ewh, boolean isUnique, AqlOptions options) {
		//this.schema = schema;
		As = new Ctx<>(as.map);
		Es = new Ctx<>(es.map);
		Awh = new HashSet<>(awh);
@@ -117,8 +130,6 @@ public class ED<Ty, En, Sym, Fk, Att> {
		if (!Collections.disjoint(As.keySet(), Es.keySet())) {
			throw new RuntimeException("The forall and exists clauses do not use disjoint variables.");
		}
		Ctx<WHICH, Triple<Ctx<Var, En>, Collection<Eq<Ty, En, Sym, Fk, Att, Var, Var>>, AqlOptions>> 
		is = new Ctx<>();
	
		is.put(WHICH.FRONT, new Triple<>(As, freeze(Awh), options));
		Ctx<Var, En> AsEs = new Ctx<>();
@@ -130,14 +141,12 @@ public class ED<Ty, En, Sym, Fk, Att> {
		for (Var v : As.keySet()) {
			ctx.put(v, Term.Gen(v));
		}
		Ctx<Unit, Pair<Ctx<Var, Term<Void, En, Void, Fk, Void, Var, Void>>, Boolean>> 
		fks = new Ctx<>();
		fks.put(new Unit(), new Pair<>(ctx, true));
		
		Schema<Ty, WHICH, Sym, Unit, Void> zzz = getEDSchema(schema.typeSide, options);
		Q = Query.makeQuery(is, new Ctx<>(), fks, schema, zzz, false, false); //TODO AQL speed these can be set to true
		this.options = options;
	}
	
	AqlOptions options;
	
	@Override
	public int hashCode() {
		final int prime = 31;
@@ -147,7 +156,7 @@ public class ED<Ty, En, Sym, Fk, Att> {
		result = prime * result + ((Es == null) ? 0 : Es.hashCode());
		result = prime * result + ((Ewh == null) ? 0 : Ewh.hashCode());
		result = prime * result + (isUnique ? 1231 : 1237);
		result = prime * result + ((schema == null) ? 0 : schema.hashCode());
		//result = prime * result + ((schema == null) ? 0 : schema.hashCode());
		return result;
	}

@@ -182,11 +191,11 @@ public class ED<Ty, En, Sym, Fk, Att> {
			return false;
		if (isUnique != other.isUnique)
			return false;
		if (schema == null) {
	/*	if (schema == null) {
			if (other.schema != null)
				return false;
		} else if (!schema.equals(other.schema))
			return false;
			return false; */
		return true;
	}
	
+0 −134
Changes for src/main/java/catdata/aql/exp/Antlr4Parser.java: 0 added lines, 134 removed lines.
Original line number Diff line number Diff line
package catdata.aql.exp;

import java.io.IOException;
import java.io.Reader;
import java.util.Collection;
import java.util.List;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Vocabulary;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import catdata.ParseException;
import catdata.Program;
import catdata.Triple;
import catdata.Util;
import catdata.aql.RawTerm;
import catdata.aql.grammar.AqlLexerRules;
import catdata.aql.grammar.AqlParser;

/**
 * The real parsers are called with a CharStreams.
 * Wrappers are provided for the interface methods:
 * CharStreams.fromReader(input)
 * CharStreams.fromString(input)
 * CharStreams.fromStream(input)
 * 
 * @author fredrick.eisele
 *
 */
public class Antlr4Parser implements IAqlParser {
	
	protected Antlr4Parser() {  }
	
	private AqlParser getParser(CharStream cs) {
		final AqlLexerRules lexer = new AqlLexerRules(cs);
		final CommonTokenStream tokens = new CommonTokenStream(lexer);
		return new AqlParser(tokens);
	}
	
	private Program<Exp<?>> parseProgram(CharStream cs) throws ParseException {
		final AqlParser parser = getParser(cs);
		final ParseTree tree = parser.program();
		
		final AqlLoaderListener loader = new AqlLoaderListener();
		final ParseTreeWalker walker = new ParseTreeWalker();
		walker.walk(loader, tree);
		
		return new Program<>(loader.decls, cs.toString(), loader.global_options, loader.kind);
	}
	
	public Program<Exp<?>> parseProgram(Reader rdr) throws ParseException, IOException {
		return parseProgram(CharStreams.fromReader(rdr));
	}
	public Program<Exp<?>> parseProgram(String str) throws ParseException {
		return parseProgram(CharStreams.fromString(str));
	}
	
	/**
	 * /fql/src/catdata/aql/exp/CombinatorParser.java
	 * 
	 * @return
	 */
	public Triple<List<catdata.Pair<String, String>>, RawTerm, RawTerm> 
	parseEq(String s) throws ParseException {	
		// try {
		//	return Parsers.or(term1, term2).from(TOKENIZER, IGNORED).parse(s);
		// } catch (ParserException e) {
		//	throw new ParseException(e.getLocation().column, e.getLocation().line, e);
		// }
		return null;
	}
	
	public catdata.Pair<List<catdata.Pair<String, String>>, RawTerm> 
	parseTermInCtx(String s) throws ParseException {
		// try {
		//	return Parsers.or(term1, term2).from(TOKENIZER, IGNORED).parse(s);
		//} catch (ParserException e) {
		//	throw new ParseException(e.getLocation().column, e.getLocation().line, e);
		//}
		return null;
	}

	public RawTerm 
	parseTermNoCtx(String s) throws ParseException {
		// try {
		//	return term().from(TOKENIZER, IGNORED).parse(s);
		// } catch (ParserException e) {
		//	throw new ParseException(e.getLocation().column, e.getLocation().line, e);
		// }
		return null;
	}

	public static catdata.Pair<String, String> 
	parseInfer(String s) {
		// Parser<catdata.Pair<String, String>> p = Parsers
		//		.tuple(token("literal").followedBy(token(":")), ident.followedBy(token("->")), ident)
		//		.map(x -> new catdata.Pair<>(x.b, x.c));
		// return p.from(TOKENIZER, IGNORED).parse(s);
		return null;
	}

	public static String 
	parseInfer1(String s) {
		// Parser<String> p = Parsers.tuple(token("literal"), token(":"), ident).map(x -> x.c);
		// return p.from(TOKENIZER, IGNORED).parse(s);
		return null;
	}
	
	public Collection<String> getReservedWords() {
		final Vocabulary vocab = AqlLexerRules.VOCABULARY;
		final String[] tokenNames = new String[vocab.getMaxTokenType()];
		for (int ix = 0; ix < tokenNames.length; ix++) {
			tokenNames[ix] = vocab.getLiteralName(ix);
			if (tokenNames[ix] == null) {
				tokenNames[ix] = vocab.getSymbolicName(ix);
			}
	
			if (tokenNames[ix] == null) {
				tokenNames[ix] = "<INVALID>";
			}
		}
		return Util.list(tokenNames);
	}

	@Override
	public Collection<String> getOperations() {
		// TODO Auto-generated method stub
		return null;
	}

}
+0 −2932

File deleted.

Preview size limit exceeded, changes collapsed.

+2 −2
Changes for src/main/java/catdata/aql/exp/AqlParserFactory.java: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ public abstract class AqlParserFactory {
		    return new CombinatorParser();
		}
		else if(AqlParserFactory.mode == Mode.ANTLR4) { 
		    return new Antlr4Parser();
		    return null; //new Antlr4Parser();
		} 
		else {
			return new CombinatorParser();
@@ -30,7 +30,7 @@ public abstract class AqlParserFactory {
			return new CombinatorParser();
		}
		else if(mode == Mode.ANTLR4) {
			return new Antlr4Parser();
			return null; //new Antlr4Parser();
		}
		else {
			return new CombinatorParser();
Loading