OW2 Consortium activexml

Compare Revisions

Ignore whitespace Rev 366 → Rev 367

/Axlog2/test/fr/inria/gemo/axlog/test/SimulatorTestMaintainer.java
New file
0,0 → 1,99
package fr.inria.gemo.axlog.test;
 
import java.util.Vector;
import java.util.Map;
 
import fr.inria.gemo.axml.db.*;
import fr.inria.gemo.axml.db.exist.*;
 
import org.xmldb.api.base.Collection;
 
 
//parser DOM
import org.w3c.dom.*;
 
import fr.inria.gemo.axlog.utility.*;
 
import fr.inria.gemo.axlog.view.*;
import fr.inria.gemo.axlog.datalog.*;
 
 
public class SimulatorTestMaintainer {
IConnection conn;
Collection collection;
Document document;
Vector<Event> events;
String docID;
public SimulatorTestMaintainer(String testProgramFileName){
try{
this.conn = DBConnection.getConnection();
collection = ((ConnectionImpl)conn).getWorkingCollection();
document = Utility.obtainDOMDocument(testProgramFileName);
Utility.addIds(document.getDocumentElement());
docID = Utility.storeDomDocumentAsExistResource(document, "testProgram");
events = getEvents(document.getDocumentElement());
}
catch(Exception e){
System.err.println("[SimulatorTestMaintainer] Exception ! \n"+e.getMessage());
}
}
public static Vector<Event> getEvents(Element el){
Vector<Event> ve = new Vector<Event>(); int k=0;
for (int i=0; i<el.getChildNodes().getLength(); i++){
Node n = el.getChildNodes().item(i);
if (n instanceof Element) {
k++;
String f = ((Element) n).getElementsByTagName("function").item(0).getFirstChild().getTextContent();
String delayS = ((Element) n).getElementsByTagName("delay").item(0).getFirstChild().getTextContent();
String query = "/program/event["+k+"]/add/*";
int delay = Integer.parseInt(delayS);
ve.add(new Event( delay, f, query));
}
}
return ve;
}
public Vector<Map<String, Vector<Tuple>>> run(){
Vector<Map<String, Vector<Tuple>>> veUpdateResults = new Vector<Map<String,Vector<Tuple>>>();
try{
for (int i=0; i<events.size(); i++) {
Event e = events.get(i);
Thread.sleep(2000);
IResult result = null;
String xQuery = " for $item in doc('"+docID+"')"+e.getQuery()+" return $item";
System.out.println("[SimulatorTestMaintainer] XQuery to execute: \n"+xQuery);
try {
result = conn.createStatement().executeXQuery(xQuery);
} catch (Exception ex) {
System.err.println(ex.getMessage());
ex.printStackTrace();
}
String elS = result.iterator().next().getContentAsString();
System.out.println("\n\n\n####New update#### "+i+ "\n");
Element update =Utility.toPayLoad(elS);
veUpdateResults.add(ManagerViews.getManager().maintain(e.getFunction(), update));
}
}
catch(Exception e){
System.out.println("[SimulatorTestMaintainer] Exception ! \n"+e.getMessage());
e.printStackTrace();
}
return veUpdateResults;
}
 
}
/Axlog2/test/fr/inria/gemo/axlog/test/TestMaintainer.java
New file
0,0 → 1,102
package fr.inria.gemo.axlog.test;
/**
* @author bogdan
*/
 
import org.w3c.dom.Document;
import java.util.*;
 
import fr.inria.gemo.axlog.utility.Utility;
import junit.framework.TestCase;
 
import fr.inria.gemo.axlog.view.*;
import fr.inria.gemo.axlog.datalog.*;
 
/**
*
* Some maintainer tests
*
*/
 
public class TestMaintainer extends TestCase {
 
public void testMaintainer0(){
String dirName = "c:\\TestDocsAxlog\\testNb0";
Document docAxml = Utility.obtainDOMDocument(dirName+System.getProperty("file.separator")+"someTest.xml");
Document docQuery = Utility.obtainDOMDocument(dirName+System.getProperty("file.separator")+"testQuery.xml");
String testProgramFileName = dirName+System.getProperty("file.separator")+"testProgram.xml";
View view = new View(docQuery, docAxml);
ManagerViews.getManager().registerView(view.getId(), view);
SimulatorTestMaintainer stm = new SimulatorTestMaintainer(testProgramFileName);
Vector<Map<String, Vector<Tuple>>> results = stm.run();
//checking the test
assertTrue(results.size()==4);
//u0
Map<String, Vector<Tuple>> ru0 = results.get(0);
assertTrue(ru0.size()==1);
Vector<Tuple> vr0 = ru0.values().iterator().next();
assertTrue(vr0.size()==0);
//u1
Map<String, Vector<Tuple>> ru1 = results.get(1);
assertTrue(ru1.size()==1);
Vector<Tuple> vr1 = ru1.values().iterator().next();
assertTrue(vr1.size()==2);
//u2
Map<String, Vector<Tuple>> ru2 = results.get(2);
assertTrue(ru2.size()==1);
Vector<Tuple> vr2 = ru2.values().iterator().next();
assertTrue(vr2.size()==0);
//u3
Map<String, Vector<Tuple>> ru3 = results.get(3);
assertTrue(ru3.size()==1);
Vector<Tuple> vr3 = ru3.values().iterator().next();
assertTrue(vr3.size()==1);
ManagerViews.getManager().unregisterView(view.getId());
}
public void testMaintainer1(){
String dirName = "c:\\TestDocsAxlog\\testNb1";
Document docAxml = Utility.obtainDOMDocument(dirName+System.getProperty("file.separator")+"someTest.xml");
Document docQuery = Utility.obtainDOMDocument(dirName+System.getProperty("file.separator")+"testQuery.xml");
String testProgramFileName = dirName+System.getProperty("file.separator")+"testProgram.xml";
View view = new View(docQuery, docAxml);
ManagerViews.getManager().registerView(view.getId(), view);
SimulatorTestMaintainer stm = new SimulatorTestMaintainer(testProgramFileName);
Vector<Map<String, Vector<Tuple>>> results = stm.run();
//checking the test
assertTrue(results.size()==4);
//u0
Map<String, Vector<Tuple>> ru0 = results.get(0);
assertTrue(ru0.size()==1);
Vector<Tuple> vr0 = ru0.values().iterator().next();
assertTrue(vr0.size()==0);
//u1
Map<String, Vector<Tuple>> ru1 = results.get(1);
assertTrue(ru1.size()==1);
Vector<Tuple> vr1 = ru1.values().iterator().next();
assertTrue(vr1.size()==2);
//u2
Map<String, Vector<Tuple>> ru2 = results.get(2);
assertTrue(ru2.size()==1);
Vector<Tuple> vr2 = ru2.values().iterator().next();
assertTrue(vr2.size()==2);
//u3
Map<String, Vector<Tuple>> ru3 = results.get(3);
assertTrue(ru3.size()==1);
Vector<Tuple> vr3 = ru3.values().iterator().next();
assertTrue(vr3.size()==0);
ManagerViews.getManager().unregisterView(view.getId());
}
}
/Axlog2/test/fr/inria/gemo/axlog/test/TestCompiler.java
7,9 → 7,12
 
import org.w3c.dom.*;
 
import fr.inria.gemo.axlog.engine.compiler.*;
import fr.inria.gemo.axlog.utility.*;
 
/**
* A compiler test
*/
 
public class TestCompiler extends TestCase {
public void testCompiler1(){
/Axlog2/src/fr/inria/gemo/axlog/utility/Event.java
23,7 → 23,14
}
public String getFunction(){
return function;
}
public String getQuery(){
return query;
}
public String toString(){
String s = "[Event] Function "+function+"; something to add: "+elToAdd;
return s;
/Axlog2/src/fr/inria/gemo/axlog/utility/Utility.java
4,6 → 4,7
*/
// Java utility classes
import java.io.File;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
14,6 → 15,7
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xmldb.api.base.Resource;
import org.xmldb.api.modules.XMLResource;
 
281,4 → 283,19
}
 
 
public static Element toPayLoad(String contentAsString) throws Exception {
DocumentBuilderFactory factory = getFactory();
Document document=null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new InputSource(new StringReader(contentAsString)));
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
return document.getDocumentElement();
}
}
/Axlog2/src/fr/inria/gemo/axlog/utility/DBConnection.java
29,13 → 29,15
public static IConnection getConnection(){
try{
if (dataSource==null)
if (dataSource==null) {
dataSource= (DataSourceImpl)new HardcodedDatasourceFactory().getDataSource();
conn = dataSource.getConnection();
conn.setWorkingCollectionPath("/db/ivmCollection");
String collectionPath = dataSource.getConnection().getWorkingCollectionPath();
System.out.println("[DBConnection] Collection working space: "+collectionPath);
}
else
conn = dataSource.getConnection();
}catch(Exception e){
System.err.println("[DBConnection in Axlog] No eXist connection!");
e.printStackTrace();
/Axlog2/src/fr/inria/gemo/axlog/engine/maintainer/Maintainer.java
3,12 → 3,15
/**
* @author bogdan
*/
//DOM parser
import org.w3c.dom.*;
 
import org.w3c.dom.*;
//Java utility
import java.util.Vector;
 
import java.util.Map;
 
//datalog
import fr.inria.gemo.axlog.datalog.*;
 
public class Maintainer {
16,18 → 19,52
Map<String, Vector<fr.inria.gemo.axlog.datalog.Entity>> functionToFilters;
Map<String, fr.inria.gemo.axlog.datalog.Plan> functionToDPlan;
public Maintainer(Map<String, String> functionsToQueries,
Map<String, Vector<fr.inria.gemo.axlog.datalog.Entity>> functionToFilters){
Map<String, Vector<fr.inria.gemo.axlog.datalog.Entity>> functionToFilters,
Map<String, fr.inria.gemo.axlog.datalog.Plan> functionToDPlan){
this.functionsToQueries = functionsToQueries;
this.functionToFilters = functionToFilters;
this.functionToDPlan = functionToDPlan;
}
public void execute(String func, Document updateDoc){
public Vector<Tuple> execute(String func, Element updateEl){
/**
* To implement Delta
*/
boolean b = false;
Plan p = this.functionToDPlan.get(func);
Vector<fr.inria.gemo.axlog.datalog.Entity> filters = this.functionToFilters.get(func);
if (p!=null){
if (filters!=null) {
for (int i=0; i<filters.size() && !b; i++){
FilterOnFunction fof = (FilterOnFunction)filters.get(i);
fof.feedContext(updateEl);
fof.executeXQueryMemory(null, null);
}
Vector<Tuple> noList = p.getTuplesNewest();
for (int i=0; i<noList.size(); i++){
Tuple tuple = noList.get(i);
System.out.println("New output tuple :\n"+tuple.toS());
}
return noList;
}
// if (this.optionCleanMemoryAfterUpdate) p.cleanMemory();
}
else {
System.out.println("Nothing to be done for this message!!!");
}
return new Vector<Tuple>();
}
 
}
/Axlog2/src/fr/inria/gemo/axlog/engine/compiler/Compiler.java
16,7 → 16,6
import org.w3c.dom.*;
 
//DB
import org.xmldb.api.modules.XMLResource;
import fr.inria.gemo.axml.db.exist.*;
import fr.inria.gemo.axml.db.*;
import org.xmldb.api.base.Collection;
75,16 → 74,13
functionToDPlan = new HashMap<String, Plan>();
treePQ= XQueryGenerator.extractTPQ(queryDoc);
Utility.initializeStoreForView();
q= XQueryGenerator.customizeTPQ(XQueryGenerator.generateTPQforSubPattern(treePQ, treePQ.root, " "), docID );
System.out.println("[Compiler] XQuery is: \n"+q);
//System.out.println("[Compiler] XQuery is: \n"+q);
relationNodes = treePQ.getRelationNodes();
System.out.println("\n[Compiler] The relation nodes are: \n"+relationNodes);
98,11 → 94,11
// System.out.println("[SimulationThreadMemory] XQuery to execute : "+xq);
System.out.println("[Compilation] XQuery to execute for planning : "+xq);
// System.out.println("[Compilation] XQuery to execute for planning : "+xq);
long t1 = System.currentTimeMillis();
result = conn.createStatement().executeXQuery(xq);
long t2 = System.currentTimeMillis();
System.out.println("[SimulationThreadMemory] Planning took "+(t2-t1));
// System.out.println("[SimulationThreadMemory] Planning took "+(t2-t1));
IXMLResource plan = result.iterator().next();
172,7 → 168,7
System.out.println("\n[Compiler] The operational plan is:\n"+((Plan)p).getOperationalPlan().print(""));
//System.out.println("\n[Compiler] The operational plan is:\n"+((Plan)p).getOperationalPlan().print(""));
Set<String> sf = p.getFunctions();
233,7 → 229,7
collection = ((ConnectionImpl)conn).getWorkingCollection();
docID = Utility.storeDomDocumentAsExistResource(doc, "testProgram");
docID = Utility.storeDomDocumentAsExistResource(doc, "testDocument");
IResult result = null;
246,20 → 242,17
functionToFilters = new HashMap<String, Vector<fr.inria.gemo.axlog.datalog.Entity>>();
functionToDPlan = new HashMap<String, Plan>();
treePQ= XQueryGenerator.extractTPQ(queryDoc);
Utility.initializeStoreForView();
q= XQueryGenerator.customizeTPQ(XQueryGenerator.generateTPQforSubPattern(treePQ, treePQ.root, " "), docID );
System.out.println("[Compiler] XQuery is: \n"+q);
// System.out.println("[Compiler] XQuery is: \n"+q);
relationNodes = treePQ.getRelationNodes();
System.out.println("\n[Compiler] The relation nodes are: \n"+relationNodes);
// System.out.println("\n[Compiler] The relation nodes are: \n"+relationNodes);
 
xq= XQueryGenerator.generateXQuery(treePQ, docID);
270,11 → 263,11
// System.out.println("[SimulationThreadMemory] XQuery to execute : "+xq);
System.out.println("[Compilation] XQuery to execute for planning : "+xq);
// System.out.println("[Compilation] XQuery to execute for planning : "+xq);
long t1 = System.currentTimeMillis();
result = conn.createStatement().executeXQuery(xq);
long t2 = System.currentTimeMillis();
System.out.println("[SimulationThreadMemory] Planning took "+(t2-t1));
// System.out.println("[SimulationThreadMemory] Planning took "+(t2-t1));
IXMLResource plan = result.iterator().next();
341,16 → 334,14
Tuple t = it.next();
System.out.println(t.toS());
}
System.out.println("\n[Compiler] The operational plan is:\n"+((Plan)p).getOperationalPlan().print(""));
//System.out.println("\n[Compiler] The operational plan is:\n"+((Plan)p).getOperationalPlan().print(""));
Set<String> sf = p.getFunctions();
currentPlan = (Plan) p;
System.out.println("\n[Compiler]The functions are:\n"+p.getFunctions());
// System.out.println("\n[Compiler]The functions are:\n"+p.getFunctions());
Set<QueryNode> relNodes = treePQ.getRelationNodes();
381,8 → 372,6
System.out.println(query);
functionsToQueries.put(func, query);
*/
}
}
390,14 → 379,8
System.err.println("\n[Compiler] Compilation error ! \n"+e.getMessage());
// e.printStackTrace();
}
 
Utility.incrementCounterValue(conn);
Utility.incrementCounterValue(conn);
}
449,9 → 432,18
return functionToFilters;
}
public Map<String, fr.inria.gemo.axlog.datalog.Plan> getFunctionToPlan(){
return this.functionToDPlan;
}
public int getViewId(){
return counter;
}
public IConnection getConnection(){
return conn;
}
}
/Axlog2/src/fr/inria/gemo/axlog/view/View.java
5,14 → 5,18
*
*/
 
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
 
import org.w3c.dom.*;
 
import fr.inria.gemo.axlog.datalog.*;
 
public class View {
String id;
fr.inria.gemo.axlog.engine.compiler.Compiler compiler;
fr.inria.gemo.axlog.engine.maintainer.Maintainer maintainer;
fr.inria.gemo.axlog.engine.tuner.Tuner tuner;
21,20 → 25,30
Map<String, String> functionsToQueries;
Map<String, Vector<fr.inria.gemo.axlog.datalog.Entity>> functionToFilters;
Map<String, Plan> functionToDPlan;
public View(Document query, Document axml) {
compiler = new fr.inria.gemo.axlog.engine.compiler.Compiler(query, axml);
viewId = compiler.getViewId();
functionsToQueries = compiler.getFunctionsToQueries();
functionToFilters = compiler.getFunctionsToFilters();
functionToDPlan = compiler.getFunctionToPlan();
maintainer = new fr.inria.gemo.axlog.engine.maintainer.Maintainer(functionsToQueries,
functionToFilters);
functionToFilters, functionToDPlan);
}
public View(Document query, String docID) {
compiler = new fr.inria.gemo.axlog.engine.compiler.Compiler(query, docID);
viewId = compiler.getViewId();
functionsToQueries = compiler.getFunctionsToQueries();
functionToFilters = compiler.getFunctionsToFilters();
functionToDPlan = compiler.getFunctionToPlan();
maintainer = new fr.inria.gemo.axlog.engine.maintainer.Maintainer(functionsToQueries,
functionToFilters);
functionToFilters, functionToDPlan);
}
public fr.inria.gemo.axlog.engine.compiler.Compiler getCompiler(){
45,8 → 59,8
return ""+viewId;
}
public void executeMaintainer(String func, Document updateDoc){
maintainer.execute(func, updateDoc);
public Vector<Tuple> executeMaintainer(String func, Element update){
return maintainer.execute(func, update);
}
public void clean(){
/Axlog2/src/fr/inria/gemo/axlog/view/ManagerViews.java
9,8 → 9,10
import java.io.RandomAccessFile;
import java.util.*;
 
import fr.inria.gemo.axlog.datalog.*;
 
 
 
public class ManagerViews {
public static ManagerViews manager;
55,32 → 57,46
return false;
}
public boolean unregisterView(String id, View view){
public boolean unregisterView(String id){
System.out.println("[ManagerViews] Unregister view "+id);
if (views.containsKey(id)){
Vector<String> functions = viewToFunctions.get(id);
for (Iterator<String> it = functions.iterator(); it.hasNext();) {
String func = it.next();
Vector<String> views1 = functionToViews.get(func);
System.out.println("[ManagerViews] Unregister view "+id+" from function "+func+" , views :"+views1);
if (views1!=null){
views1.remove(func);
System.out.println("[ManagerViews] Really unregister view "+id+" from function "+func);
views1.remove(""+id);
if (views1.isEmpty()){
System.out.println("[ManagerViews] Remove function "+func);
functionToViews.remove(func);
}
else functionToViews.put(func, views1);
}
}
viewToFunctions.remove(id);
views.remove(id);
System.out.println("[ManagerViews] View to functions is :\n"+viewToFunctions);
System.out.println("[ManagerViews] Function to views is :\n"+functionToViews);
return true;
}
return false;
}
public synchronized void maintain(String func, Document updateDoc){
public synchronized Map<String, Vector<Tuple>> maintain(String func, Element update){
Vector<String> viewIds = functionToViews.get(func);
System.out.println("\n\n[ManagerViews] For function '"+func+"' the views to maintain are:"+viewIds);
HashMap<String, Vector<Tuple>> viewUpdates = new HashMap<String, Vector<Tuple>>();
for (int i=0; i<viewIds.size(); i++){
String viewId = viewIds.get(i);
View view = views.get(viewId);
view.executeMaintainer(func, updateDoc);
Vector<Tuple> vt = view.executeMaintainer(func, update);
viewUpdates.put(viewId, vt);
}
return viewUpdates;
}