Update of /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30131/src/org/tmapiutils/query/tolog/utils Modified Files: DefaultRowComparator.java InvertedComparator.java ReferenceUtils.java RowComparator.java TologContext.java VariableSet.java Log Message: Tabs -> Whitespaces Index: InvertedComparator.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/InvertedComparator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** InvertedComparator.java 8 Mar 2005 12:34:00 -0000 1.2 --- InvertedComparator.java 8 Jul 2005 16:03:22 -0000 1.3 *************** *** 1,43 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Comparator; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class InvertedComparator implements Comparator { ! ! Comparator m_comp; ! ! public InvertedComparator(Comparator comp) { ! m_comp = comp; ! } ! /* (non-Javadoc) ! * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) ! */ ! public int compare(Object o1, Object o2) { ! int c = m_comp.compare(o1, o2); ! return c * -1; ! } ! ! } ! --- 1,43 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Comparator; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class InvertedComparator implements Comparator { ! ! Comparator m_comp; ! ! public InvertedComparator(Comparator comp) { ! m_comp = comp; ! } ! /* (non-Javadoc) ! * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) ! */ ! public int compare(Object o1, Object o2) { ! int c = m_comp.compare(o1, o2); ! return c * -1; ! } ! ! } ! Index: ReferenceUtils.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/ReferenceUtils.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ReferenceUtils.java 8 Mar 2005 12:34:00 -0000 1.3 --- ReferenceUtils.java 8 Jul 2005 16:03:22 -0000 1.4 *************** *** 1,54 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Iterator; ! ! import org.tmapi.core.Locator; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class ReferenceUtils ! { ! ! public static String getTologReference(TopicMapObject tmo) { ! String ret = null; ! Locator baseloc = tmo.getTopicMap().getBaseLocator(); ! String baseAddress = baseloc.getReference(); ! Iterator it = tmo.getSourceLocators().iterator(); ! while ((ret == null) && it.hasNext()) { ! Locator l = (Locator)it.next(); ! if (l.getReference().startsWith(baseAddress) && ! l.getReference().substring(baseAddress.length()).startsWith("#")) ! { ! ret = l.getReference().substring(baseAddress.length()); ! } ! } ! ! if (ret == null) { ! return "@" + tmo.getObjectId(); ! } else { ! return ret; ! } ! } ! } ! --- 1,54 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Iterator; ! ! import org.tmapi.core.Locator; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class ReferenceUtils ! { ! ! public static String getTologReference(TopicMapObject tmo) { ! String ret = null; ! Locator baseloc = tmo.getTopicMap().getBaseLocator(); ! String baseAddress = baseloc.getReference(); ! Iterator it = tmo.getSourceLocators().iterator(); ! while ((ret == null) && it.hasNext()) { ! Locator l = (Locator)it.next(); ! if (l.getReference().startsWith(baseAddress) && ! l.getReference().substring(baseAddress.length()).startsWith("#")) ! { ! ret = l.getReference().substring(baseAddress.length()); ! } ! } ! ! if (ret == null) { ! return "@" + tmo.getObjectId(); ! } else { ! return ret; ! } ! } ! } ! Index: RowComparator.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/RowComparator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** RowComparator.java 8 Mar 2005 12:34:00 -0000 1.2 --- RowComparator.java 8 Jul 2005 16:03:22 -0000 1.3 *************** *** 1,41 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Comparator; ! import java.util.List; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public abstract class RowComparator implements Comparator { ! ! public abstract void addSortColumn(int ix); ! ! /* (non-Javadoc) ! * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) ! */ ! public int compare(Object o1, Object o2) { ! return compare((List)o1, (List)o2); ! } ! ! public abstract int compare(List row1, List row2); ! } ! --- 1,41 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Comparator; ! import java.util.List; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public abstract class RowComparator implements Comparator { ! ! public abstract void addSortColumn(int ix); ! ! /* (non-Javadoc) ! * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) ! */ ! public int compare(Object o1, Object o2) { ! return compare((List)o1, (List)o2); ! } ! ! public abstract int compare(List row1, List row2); ! } ! Index: TologContext.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/TologContext.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TologContext.java 8 Mar 2005 12:34:00 -0000 1.2 --- TologContext.java 8 Jul 2005 16:03:22 -0000 1.3 *************** *** 1,85 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Iterator; ! import java.util.Stack; ! ! import org.tmapiutils.query.tolog.parser.TologRule; ! ! import java.util.List; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class TologContext { ! ! Stack m_ruleStack = new Stack(); ! ! public void pushRule(TologRule rule, List params) { ! if (rule.getName() == null) { ! throw new RuntimeException("Attempt to push unamed rule..."); ! } ! m_ruleStack.push(new TologContextElement(rule.getName(), params)); ! } ! ! public boolean inRule(TologRule rule) { ! Iterator it = m_ruleStack.iterator(); ! while (it.hasNext()) { ! TologContextElement e = (TologContextElement)it.next(); ! if (e.ruleName.equals(rule.getName())) { ! return true; ! } ! } ! return false; ! } ! ! public void popRule(TologRule rule) { ! if (((TologContextElement)m_ruleStack.peek()).ruleName.equals(rule.getName())) { ! m_ruleStack.pop(); ! } else { ! throw new RuntimeException("Tolog context rule stack got out of sync. Expected " + m_ruleStack.peek().toString() + " got " + rule.getName()); ! } ! } ! ! public List getParams(TologRule rule) { ! for (int i = m_ruleStack.size() - 1; i >= 0; i--) { ! TologContextElement e = (TologContextElement)m_ruleStack.get(i); ! if (e.ruleName.equals(rule.getName())) { ! return e.ruleParams; ! } ! } ! return null; ! } ! ! public int getDepth() { ! return m_ruleStack.size(); ! } ! ! class TologContextElement { ! String ruleName; ! List ruleParams; ! TologContextElement(String ruleName, List ruleParams) { ! this.ruleName = ruleName; ! this.ruleParams = ruleParams; ! } ! } ! } ! --- 1,85 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.Iterator; ! import java.util.Stack; ! ! import org.tmapiutils.query.tolog.parser.TologRule; ! ! import java.util.List; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class TologContext { ! ! Stack m_ruleStack = new Stack(); ! ! public void pushRule(TologRule rule, List params) { ! if (rule.getName() == null) { ! throw new RuntimeException("Attempt to push unamed rule..."); ! } ! m_ruleStack.push(new TologContextElement(rule.getName(), params)); ! } ! ! public boolean inRule(TologRule rule) { ! Iterator it = m_ruleStack.iterator(); ! while (it.hasNext()) { ! TologContextElement e = (TologContextElement)it.next(); ! if (e.ruleName.equals(rule.getName())) { ! return true; ! } ! } ! return false; ! } ! ! public void popRule(TologRule rule) { ! if (((TologContextElement)m_ruleStack.peek()).ruleName.equals(rule.getName())) { ! m_ruleStack.pop(); ! } else { ! throw new RuntimeException("Tolog context rule stack got out of sync. Expected " + m_ruleStack.peek().toString() + " got " + rule.getName()); ! } ! } ! ! public List getParams(TologRule rule) { ! for (int i = m_ruleStack.size() - 1; i >= 0; i--) { ! TologContextElement e = (TologContextElement)m_ruleStack.get(i); ! if (e.ruleName.equals(rule.getName())) { ! return e.ruleParams; ! } ! } ! return null; ! } ! ! public int getDepth() { ! return m_ruleStack.size(); ! } ! ! class TologContextElement { ! String ruleName; ! List ruleParams; ! TologContextElement(String ruleName, List ruleParams) { ! this.ruleName = ruleName; ! this.ruleParams = ruleParams; ! } ! } ! } ! Index: DefaultRowComparator.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/DefaultRowComparator.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** DefaultRowComparator.java 8 Mar 2005 12:33:59 -0000 1.3 --- DefaultRowComparator.java 8 Jul 2005 16:03:22 -0000 1.4 *************** *** 1,76 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.ArrayList; ! import java.util.Iterator; ! import java.util.List; ! ! import org.tmapi.core.TopicName; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * Simple default sorting algorithm. Compares the values in each sort column one at a time. ! * The following comparison algorithm is applied to each pair of values: ! * <ol> ! * <li>If the values are of different classes, then sort on class name</li> ! * <li>If the values implement Comparable, then invoke the compareTo method.</li> ! * <li>If the values implement TopicName, then compare on the base name string value.</li> ! * <li>If the values implement TopicMapObject, then compare on the ID property.</li> ! * </ol> ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class DefaultRowComparator extends RowComparator { ! ! private List m_sortColumns = new ArrayList(); ! ! /* (non-Javadoc) ! * @see org.tmapiutils.query.tolog.RowComparator#addSortColumn(int) ! */ ! public void addSortColumn(int ix) { ! m_sortColumns.add(new Integer(ix)); ! } ! ! /* (non-Javadoc) ! * @see org.tmapiutils.query.tolog.RowComparator#compare(java.util.List, java.util.List) ! */ ! public int compare(List row1, List row2) { ! int ret = 0; ! Iterator cols = m_sortColumns.iterator(); ! while ((ret == 0) && (cols.hasNext())) { ! int ix = ((Integer)cols.next()).intValue(); ! Object o1 = row1.get(ix); ! Object o2 = row2.get(ix); ! ret = o1.getClass().getName().compareTo(o2.getClass().getName()); ! if (ret == 0) { ! if (o1 instanceof Comparable) { ! ret = ((Comparable)o1).compareTo(o2); ! } else if (o1 instanceof TopicName) { ! ret = ((TopicName)o1).getValue().compareTo(((TopicName)o2).getValue()); ! } else if (o1 instanceof TopicMapObject) { ! ret = ((TopicMapObject)o1).getObjectId().compareTo(((TopicMapObject)o2).getObjectId()); ! } ! } ! } ! return ret; ! } ! ! } ! --- 1,76 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.ArrayList; ! import java.util.Iterator; ! import java.util.List; ! ! import org.tmapi.core.TopicName; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * Simple default sorting algorithm. Compares the values in each sort column one at a time. ! * The following comparison algorithm is applied to each pair of values: ! * <ol> ! * <li>If the values are of different classes, then sort on class name</li> ! * <li>If the values implement Comparable, then invoke the compareTo method.</li> ! * <li>If the values implement TopicName, then compare on the base name string value.</li> ! * <li>If the values implement TopicMapObject, then compare on the ID property.</li> ! * </ol> ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class DefaultRowComparator extends RowComparator { ! ! private List m_sortColumns = new ArrayList(); ! ! /* (non-Javadoc) ! * @see org.tmapiutils.query.tolog.RowComparator#addSortColumn(int) ! */ ! public void addSortColumn(int ix) { ! m_sortColumns.add(new Integer(ix)); ! } ! ! /* (non-Javadoc) ! * @see org.tmapiutils.query.tolog.RowComparator#compare(java.util.List, java.util.List) ! */ ! public int compare(List row1, List row2) { ! int ret = 0; ! Iterator cols = m_sortColumns.iterator(); ! while ((ret == 0) && (cols.hasNext())) { ! int ix = ((Integer)cols.next()).intValue(); ! Object o1 = row1.get(ix); ! Object o2 = row2.get(ix); ! ret = o1.getClass().getName().compareTo(o2.getClass().getName()); ! if (ret == 0) { ! if (o1 instanceof Comparable) { ! ret = ((Comparable)o1).compareTo(o2); ! } else if (o1 instanceof TopicName) { ! ret = ((TopicName)o1).getValue().compareTo(((TopicName)o2).getValue()); ! } else if (o1 instanceof TopicMapObject) { ! ret = ((TopicMapObject)o1).getObjectId().compareTo(((TopicMapObject)o2).getObjectId()); ! } ! } ! } ! return ret; ! } ! ! } ! Index: VariableSet.java =================================================================== RCS file: /cvsroot/tmapi-utils/tmapi-utils/src/org/tmapiutils/query/tolog/utils/VariableSet.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** VariableSet.java 8 Mar 2005 12:34:00 -0000 1.3 --- VariableSet.java 8 Jul 2005 16:03:22 -0000 1.4 *************** *** 1,293 **** ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.ArrayList; ! import java.util.Arrays; ! import java.util.Iterator; ! import java.util.List; ! import java.util.Map; ! ! import org.apache.commons.logging.Log; ! import org.tmapiutils.query.tolog.TologProcessingException; ! import org.tmapiutils.query.tolog.parser.Variable; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class VariableSet { ! ! private List m_rows; ! private List m_cols; ! ! public VariableSet() { ! m_rows = new ArrayList(); ! m_cols = new ArrayList(); ! } ! ! public int indexOf(Variable var) { ! return m_cols.indexOf(var); ! } ! ! public int addColumn(Object colHead) ! { ! int ix = m_cols.indexOf(colHead); ! if (ix > -1) { ! return ix; ! } ! m_cols.add(colHead); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! ((List)it.next()).add(colHead); ! } ! return m_cols.size() - 1; ! } ! ! public void addRow(List values) throws TologProcessingException { ! if (values.size() != m_cols.size()) { ! throw new TologProcessingException("Row size is wrong. Expected " + m_cols.size() + " items. Got " + values.size() + " items."); ! } ! m_rows.add(values); ! } ! ! public void addRowFromMap(Map values) throws TologProcessingException { ! Iterator it = m_cols.iterator(); ! ArrayList newRow = new ArrayList(); ! while (it.hasNext()) { ! Object colHead = it.next(); ! Object val = values.get(colHead); ! if (val instanceof Variable) { ! val = values.get(val); ! } ! newRow.add(val == null ? colHead : val); ! } ! m_rows.add(newRow); ! } ! ! public List getColumn(Variable var) { ! ArrayList ret = new ArrayList(); ! int ix = indexOf(var); ! if (ix >= 0) { ! for (Iterator it = m_rows.iterator(); it.hasNext(); ) { ! ret.add(((List)it.next()).get(ix)); ! } ! } ! return ret; ! } ! ! /* ! public Map getMapping(int rowNum) { ! HashMap m = new HashMap(); ! List l = (List)m_rows.get(rowNum); ! for (int colNum = 0; colNum < m_cols.size(); colNum++) { ! Variable v = (Variable)m_cols.get(colNum); ! m.put(v, l.get(colNum)); ! } ! return Collections.unmodifiableMap(m); ! } ! */ ! public List removeRow(int rowNum) { ! return (List)m_rows.remove(rowNum); ! } ! ! public boolean isEmpty() { ! return m_rows.isEmpty(); ! } ! ! public List getColumns() { ! return m_cols; ! } ! ! public void addColumns(List vars) { ! m_cols.addAll(vars); ! Iterator newVars = vars.iterator(); ! while(newVars.hasNext()) { ! Variable v = (Variable)newVars.next(); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! ((List)it.next()).add(v); ! } ! } ! } ! ! public Object clone() { ! VariableSet ret = new VariableSet(); ! ret.m_cols = new ArrayList(m_cols); ! ret.m_rows = new ArrayList(m_rows.size()); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ret.m_rows.add(new ArrayList(srcRow)); ! } ! return ret; ! } ! ! public int size() { ! return m_rows.size(); ! } ! ! ! public void dump(Log log) { ! if (log.isDebugEnabled()) { ! log.debug("Variable Set:" + toString()); ! log.debug(" column variables:" + dumpVars(m_cols)); ! int row = 1; ! for(Iterator it = m_rows.iterator();it.hasNext(); row++) { ! log.debug(" Row " + row + ":" + dumpRow((List)it.next())); ! } ! } ! } ! ! public void dump() { ! System.out.println("Variable Set:" + toString()); ! System.out.println(" column variables:" + dumpVars(m_cols)); ! int row = 1; ! for(Iterator it = m_rows.iterator();it.hasNext(); row++) { ! System.out.println(" Row " + row + ":" + dumpRow((List)it.next())); ! } ! } ! ! private String dumpVars(List l) { ! StringBuffer ret = new StringBuffer(); ! for (Iterator it = l.iterator();it.hasNext();) { ! ret.append(it.next().toString()); ! if (it.hasNext()) ret.append(", "); ! } ! return ret.toString(); ! } ! ! private String dumpRow(List r) { ! StringBuffer ret = new StringBuffer(); ! for (Iterator it = r.iterator(); it.hasNext();) { ! Object o = it.next(); ! if (o instanceof TopicMapObject) { ! ret.append(((TopicMapObject)o).getObjectId()); ! } else { ! ret.append(o.toString()); ! } ! if (it.hasNext()) ret.append(", "); ! } ! return ret.toString(); ! } ! ! public List project(Variable [] selectList, Variable countVar) { ! List ret = new ArrayList(); ! int[] selectIxs = new int[selectList.length]; ! for (int i = 0; i < selectList.length; i++) { ! selectIxs[i] = indexOf(selectList[i]); ! } ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ArrayList projectRow = new ArrayList(selectList.length + 1); ! for (int i = 0; i < selectIxs.length; i++) { ! projectRow.add(srcRow.get(selectIxs[i])); ! } ! ! boolean add = true; ! Iterator retItems = ret.iterator(); ! while (retItems.hasNext()) { ! List l = (List)retItems.next(); ! if (l.subList(0, selectList.length).equals(projectRow)) { ! if (countVar != null) { ! Integer count = (Integer)l.get(selectList.length); ! l.set(selectList.length, new Integer(count.intValue() + 1)); ! } ! add = false; ! } ! } ! if (add) { ! if (countVar != null) { ! //HashSet countVals = new HashSet(); ! //countVals.add(m.get(countVar)); ! projectRow.add(new Integer(1)); ! } ! ret.add(projectRow); ! } ! } ! return ret; ! } ! ! /** ! * Adds one variable set to another, merging the columns ! * and duplicating column head values into empty cells. ! * @param v the variable set to be added to this one. ! * @throws TologProcessingException ! */ ! public void add(VariableSet v) throws TologProcessingException { ! if (v.m_cols.equals(m_cols)) { ! // Simplest case ! m_rows.addAll(v.m_rows); ! return; ! } ! int max = v.getColumns().size(); ! int [] colMap = new int[max]; ! for (int i = 0; i < max; i++) { ! colMap[i] = addColumn(v.getColumns().get(i)); ! } ! Iterator it = v.getRows().iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ArrayList newRow = new ArrayList(); ! newRow.addAll(m_cols); ! for (int i = 0; i < max; i++) { ! newRow.set(colMap[i], srcRow.get(i)); ! } ! m_rows.add(newRow); ! } ! } ! ! /** ! * @param objects ! */ ! public void addRow(Object[] values) throws TologProcessingException { ! ArrayList row = new ArrayList(); ! row.addAll(Arrays.asList(values)); ! addRow(row); ! } ! ! public List getRows() { ! return m_rows; ! } ! ! public List getRow(int ix) { ! return (List)m_rows.get(ix); ! } ! ! public void addMatchResults(List inputRow, VariableSet results) { ! Iterator rows = results.getRows().iterator(); ! while (rows.hasNext()) { ! List rowToAdd = new ArrayList(); ! List resultRow = (List)rows.next(); ! rowToAdd.addAll(inputRow); ! List cols = results.getColumns(); ! int max = cols.size(); ! for (int c = 0; c < max; c++) { ! if (cols.get(c) instanceof Variable) { ! int ix = indexOf((Variable)cols.get(c)); ! rowToAdd.set(ix, resultRow.get(c)); ! } ! } ! m_rows.add(rowToAdd); ! } ! } ! } ! --- 1,293 ---- ! /* ! * Copyright 2005 TMAPI Utils / Kal Ahmed, Lars Heuer ! * Copyright (c) 2000-2004 The TM4J Project. All rights reserved. ! * ! * Licensed under the Apache License, Version 2.0 (the "License"); ! * you may not use this file except in compliance with the License. ! * You may obtain a copy of the License at ! * ! * http://www.apache.org/licenses/LICENSE-2.0 ! * ! * Unless required by applicable law or agreed to in writing, software ! * distributed under the License is distributed on an "AS IS" BASIS, ! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ! * See the License for the specific language governing permissions and ! * limitations under the License. ! */ ! ! package org.tmapiutils.query.tolog.utils; ! ! import java.util.ArrayList; ! import java.util.Arrays; ! import java.util.Iterator; ! import java.util.List; ! import java.util.Map; ! ! import org.apache.commons.logging.Log; ! import org.tmapiutils.query.tolog.TologProcessingException; ! import org.tmapiutils.query.tolog.parser.Variable; ! import org.tmapi.core.TopicMapObject; ! ! /** ! * ! * @author Kal Ahmed (kal[at]techquila.com) ! * @author Lars Heuer (heuer[at]semagia.com) ! */ ! public class VariableSet { ! ! private List m_rows; ! private List m_cols; ! ! public VariableSet() { ! m_rows = new ArrayList(); ! m_cols = new ArrayList(); ! } ! ! public int indexOf(Variable var) { ! return m_cols.indexOf(var); ! } ! ! public int addColumn(Object colHead) ! { ! int ix = m_cols.indexOf(colHead); ! if (ix > -1) { ! return ix; ! } ! m_cols.add(colHead); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! ((List)it.next()).add(colHead); ! } ! return m_cols.size() - 1; ! } ! ! public void addRow(List values) throws TologProcessingException { ! if (values.size() != m_cols.size()) { ! throw new TologProcessingException("Row size is wrong. Expected " + m_cols.size() + " items. Got " + values.size() + " items."); ! } ! m_rows.add(values); ! } ! ! public void addRowFromMap(Map values) throws TologProcessingException { ! Iterator it = m_cols.iterator(); ! ArrayList newRow = new ArrayList(); ! while (it.hasNext()) { ! Object colHead = it.next(); ! Object val = values.get(colHead); ! if (val instanceof Variable) { ! val = values.get(val); ! } ! newRow.add(val == null ? colHead : val); ! } ! m_rows.add(newRow); ! } ! ! public List getColumn(Variable var) { ! ArrayList ret = new ArrayList(); ! int ix = indexOf(var); ! if (ix >= 0) { ! for (Iterator it = m_rows.iterator(); it.hasNext(); ) { ! ret.add(((List)it.next()).get(ix)); ! } ! } ! return ret; ! } ! ! /* ! public Map getMapping(int rowNum) { ! HashMap m = new HashMap(); ! List l = (List)m_rows.get(rowNum); ! for (int colNum = 0; colNum < m_cols.size(); colNum++) { ! Variable v = (Variable)m_cols.get(colNum); ! m.put(v, l.get(colNum)); ! } ! return Collections.unmodifiableMap(m); ! } ! */ ! public List removeRow(int rowNum) { ! return (List)m_rows.remove(rowNum); ! } ! ! public boolean isEmpty() { ! return m_rows.isEmpty(); ! } ! ! public List getColumns() { ! return m_cols; ! } ! ! public void addColumns(List vars) { ! m_cols.addAll(vars); ! Iterator newVars = vars.iterator(); ! while(newVars.hasNext()) { ! Variable v = (Variable)newVars.next(); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! ((List)it.next()).add(v); ! } ! } ! } ! ! public Object clone() { ! VariableSet ret = new VariableSet(); ! ret.m_cols = new ArrayList(m_cols); ! ret.m_rows = new ArrayList(m_rows.size()); ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ret.m_rows.add(new ArrayList(srcRow)); ! } ! return ret; ! } ! ! public int size() { ! return m_rows.size(); ! } ! ! ! public void dump(Log log) { ! if (log.isDebugEnabled()) { ! log.debug("Variable Set:" + toString()); ! log.debug(" column variables:" + dumpVars(m_cols)); ! int row = 1; ! for(Iterator it = m_rows.iterator();it.hasNext(); row++) { ! log.debug(" Row " + row + ":" + dumpRow((List)it.next())); ! } ! } ! } ! ! public void dump() { ! System.out.println("Variable Set:" + toString()); ! System.out.println(" column variables:" + dumpVars(m_cols)); ! int row = 1; ! for(Iterator it = m_rows.iterator();it.hasNext(); row++) { ! System.out.println(" Row " + row + ":" + dumpRow((List)it.next())); ! } ! } ! ! private String dumpVars(List l) { ! StringBuffer ret = new StringBuffer(); ! for (Iterator it = l.iterator();it.hasNext();) { ! ret.append(it.next().toString()); ! if (it.hasNext()) ret.append(", "); ! } ! return ret.toString(); ! } ! ! private String dumpRow(List r) { ! StringBuffer ret = new StringBuffer(); ! for (Iterator it = r.iterator(); it.hasNext();) { ! Object o = it.next(); ! if (o instanceof TopicMapObject) { ! ret.append(((TopicMapObject)o).getObjectId()); ! } else { ! ret.append(o.toString()); ! } ! if (it.hasNext()) ret.append(", "); ! } ! return ret.toString(); ! } ! ! public List project(Variable [] selectList, Variable countVar) { ! List ret = new ArrayList(); ! int[] selectIxs = new int[selectList.length]; ! for (int i = 0; i < selectList.length; i++) { ! selectIxs[i] = indexOf(selectList[i]); ! } ! Iterator it = m_rows.iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ArrayList projectRow = new ArrayList(selectList.length + 1); ! for (int i = 0; i < selectIxs.length; i++) { ! projectRow.add(srcRow.get(selectIxs[i])); ! } ! ! boolean add = true; ! Iterator retItems = ret.iterator(); ! while (retItems.hasNext()) { ! List l = (List)retItems.next(); ! if (l.subList(0, selectList.length).equals(projectRow)) { ! if (countVar != null) { ! Integer count = (Integer)l.get(selectList.length); ! l.set(selectList.length, new Integer(count.intValue() + 1)); ! } ! add = false; ! } ! } ! if (add) { ! if (countVar != null) { ! //HashSet countVals = new HashSet(); ! //countVals.add(m.get(countVar)); ! projectRow.add(new Integer(1)); ! } ! ret.add(projectRow); ! } ! } ! return ret; ! } ! ! /** ! * Adds one variable set to another, merging the columns ! * and duplicating column head values into empty cells. ! * @param v the variable set to be added to this one. ! * @throws TologProcessingException ! */ ! public void add(VariableSet v) throws TologProcessingException { ! if (v.m_cols.equals(m_cols)) { ! // Simplest case ! m_rows.addAll(v.m_rows); ! return; ! } ! int max = v.getColumns().size(); ! int [] colMap = new int[max]; ! for (int i = 0; i < max; i++) { ! colMap[i] = addColumn(v.getColumns().get(i)); ! } ! Iterator it = v.getRows().iterator(); ! while (it.hasNext()) { ! List srcRow = (List)it.next(); ! ArrayList newRow = new ArrayList(); ! newRow.addAll(m_cols); ! for (int i = 0; i < max; i++) { ! newRow.set(colMap[i], srcRow.get(i)); ! } ! m_rows.add(newRow); ! } ! } ! ! /** ! * @param objects ! */ ! public void addRow(Object[] values) throws TologProcessingException { ! ArrayList row = new ArrayList(); ! row.addAll(Arrays.asList(values)); ! addRow(row); ! } ! ! public List getRows() { ! return m_rows; ! } ! ! public List getRow(int ix) { ! return (List)m_rows.get(ix); ! } ! ! public void addMatchResults(List inputRow, VariableSet results) { ! Iterator rows = results.getRows().iterator(); ! while (rows.hasNext()) { ! List rowToAdd = new ArrayList(); ! List resultRow = (List)rows.next(); ! rowToAdd.addAll(inputRow); ! List cols = results.getColumns(); ! int max = cols.size(); ! for (int c = 0; c < max; c++) { ! if (cols.get(c) instanceof Variable) { ! int ix = indexOf((Variable)cols.get(c)); ! rowToAdd.set(ix, resultRow.get(c)); ! } ! } ! m_rows.add(rowToAdd); ! } ! } ! } ! |