00001
00002
00003
00004
00005
00006
00007 package com.dapissarenko.hierarchy;
00008
00009 import java.io.IOException;
00010 import java.util.ArrayList;
00011 import java.util.Collection;
00012 import java.util.Iterator;
00013 import java.util.Vector;
00014
00015 import org.apache.log4j.Logger;
00016 import org.opencyc.api.CycAccess;
00017 import org.opencyc.api.CycApiException;
00018 import org.opencyc.api.CycObjectFactory;
00019 import org.opencyc.cycobject.CycConstant;
00020 import org.opencyc.cycobject.CycList;
00021 import org.opencyc.cycobject.CycVariable;
00022
00023 import com.dapissarenko.hierarchy.civilServants.CivilServantData;
00024 import com.dapissarenko.hierarchy.countries.CountryData;
00025 import com.dapissarenko.hierarchy.departments.DepartmentData;
00026
00027
00028
00029
00030
00031 public class DataAccessLayer {
00032 private CycAccess cyc;
00033 private Logger logger=Logger.getLogger(getClass());
00034 public DataAccessLayer(CycAccess cyc)
00035 {
00036 this.cyc=cyc;
00037 }
00038 public Collection getDepartmentList(CycConstant mt) throws CycApiException, IOException
00039 {
00040 CycList query = null;
00041 CycList response = null;
00042 CycVariable departmentVariable = null;
00043 CycConstant item=null;
00044 DepartmentData departmentData=null;
00045 Collection collection=null;
00046 Iterator iterator=null;
00047
00048 collection=new Vector();
00049
00050 query = CycAccess.current().makeCycList("(#$isa ?DEPARTMENT #$Department)");
00051 departmentVariable = CycObjectFactory.makeCycVariable("?DEPARTMENT");
00052
00053 response = CycAccess.current().askWithVariable(query,
00054 departmentVariable, mt);
00055
00056 iterator=response.iterator();
00057 while (iterator.hasNext())
00058 {
00059 item=(CycConstant)iterator.next();
00060
00061 departmentData=new DepartmentData();
00062 departmentData.setName(item.getName());
00063 departmentData.setFieldOfWork(this.getPredicateValue("fieldOfWork", item, mt));
00064 collection.add(departmentData);
00065 }
00066
00067 return collection;
00068
00069 }
00070 public Collection getCountryList(CycConstant mt) throws CycApiException, IOException
00071 {
00072 CycList query = null;
00073 CycList response = null;
00074 CycVariable countryVariable = null;
00075 CycConstant item=null;
00076 CountryData countryData=null;
00077 Collection collection=null;
00078 Iterator iterator=null;
00079
00080 collection=new Vector();
00081
00082 query = CycAccess.current().makeCycList("(#$isa ?COUNTRY #$Country)");
00083 countryVariable = CycObjectFactory.makeCycVariable("?COUNTRY");
00084
00085 response = CycAccess.current().askWithVariable(query,
00086 countryVariable, mt);
00087
00088 iterator=response.iterator();
00089 while (iterator.hasNext())
00090 {
00091 item=(CycConstant)iterator.next();
00092
00093 countryData=new CountryData();
00094 countryData.setCountryName(item.getName());
00095 countryData.setCountryComment(this.getComment(item, mt));
00096
00097 collection.add(countryData);
00098 }
00099
00100 return collection;
00101 }
00102 private String getPredicateValue(String predicateName, CycConstant item, CycConstant mt) throws CycApiException, IOException
00103 {
00104 CycList query = null;
00105 CycList response = null;
00106 CycVariable variable = null;
00107 String value=null;
00108
00109 value=null;
00110 query = CycAccess.current().makeCycList("(#$"+predicateName + " #$"+ item.getName() + " ?VAR)");
00111 variable = CycObjectFactory.makeCycVariable("?VAR");
00112
00113 response = CycAccess.current().askWithVariable(query,
00114 variable, mt);
00115
00116 if (response.size()>0)
00117 {
00118 value=(String)response.iterator().next();
00119 }
00120
00121 return value;
00122 }
00123 private String getComment(CycConstant country, CycConstant mt) throws CycApiException, IOException
00124 {
00125 return this.getPredicateValue("comment", country, mt);
00126 }
00127 public Collection getCivilServantsList(CycConstant mt) throws CycApiException, IOException
00128 {
00129 CycList query = null;
00130 CycList response = null;
00131 CycVariable civilServantVariable = null;
00132 CycConstant item=null;
00133 CivilServantData civilServantData=null;
00134 Collection collection=null;
00135 Iterator iterator=null;
00136
00137 collection=new Vector();
00138
00139 query = CycAccess.current().makeCycList("(#$isa ?CIVIL_SERVANT #$CivilServant)");
00140 civilServantVariable = CycObjectFactory.makeCycVariable("?CIVIL_SERVANT");
00141
00142 response = CycAccess.current().askWithVariable(query,
00143 civilServantVariable, mt);
00144
00145 iterator=response.iterator();
00146 while (iterator.hasNext())
00147 {
00148 item=(CycConstant)iterator.next();
00149
00150 civilServantData=new CivilServantData();
00151 civilServantData.setFieldOfWork(this.getPredicateValue("fieldOfWork", item, mt));
00152 civilServantData.setGivenNames(this.getPredicateValue("givenNames", item, mt));
00153 civilServantData.setFamilyName(this.getPredicateValue("familyName", item, mt));
00154
00155 collection.add(civilServantData);
00156 }
00157
00158 return collection;
00159 }
00160 public Collection getHeadOfDepartmentList(CycConstant mt) throws CycApiException, IOException
00161 {
00162 CycList query = null;
00163 CycList response = null;
00164 CycVariable departmentVariable = null;
00165 CycVariable headVariable = null;
00166 CycList item=null;
00167 DepartmentData departmentData=null;
00168 Collection collection=null;
00169 ArrayList variables=null;
00170 Iterator iterator=null;
00171 CycConstant department=null;
00172 CycConstant head=null;
00173 String fullHeadName=null;
00174
00175 collection=new Vector();
00176
00177 query = CycAccess.current().makeCycList("(#$headOfDepartment ?HEAD_OF_DEPARTMENT ?DEPARTMENT)");
00178 departmentVariable = CycObjectFactory.makeCycVariable("?DEPARTMENT");
00179 headVariable = CycObjectFactory.makeCycVariable("?HEAD_OF_DEPARTMENT");
00180
00181 variables=new ArrayList();
00182 variables.add(departmentVariable);
00183 variables.add(headVariable);
00184
00185 response = CycAccess.current().askWithVariables(query,
00186 variables, mt);
00187
00188 iterator=response.iterator();
00189 while (iterator.hasNext())
00190 {
00191 item=(CycList)iterator.next();
00192
00193 head=(CycConstant)item.first();
00194 department=(CycConstant)item.second();
00195 fullHeadName=this.getFullHeadName(head, mt);
00196
00197 departmentData=new DepartmentData();
00198 departmentData.setHeadOfDepartment(fullHeadName);
00199 departmentData.setName(department.getName());
00200
00201 collection.add(departmentData);
00202 }
00203
00204 return collection;
00205
00206 }
00207 private String getFullHeadName(CycConstant person, CycConstant mt) throws CycApiException, IOException
00208 {
00209 String fullName=null;
00210
00211 fullName = this.getPredicateValue("givenNames", person, mt);
00212 fullName += " ";
00213 fullName += this.getPredicateValue("familyName", person, mt);
00214
00215 return fullName;
00216 }
00217 public String[] getRabinovichRoles(CycConstant mt) throws CycApiException, IOException
00218 {
00219 CycList query = null;
00220 CycList response = null;
00221 CycVariable propertyVariable = null;
00222 Object item=null;
00223 Vector collection=null;
00224 Iterator iterator=null;
00225
00226 collection=new Vector();
00227
00228 query = CycAccess.current().makeCycList("(#$isa #$MokiiNilovichRabinovich ?PROPERTY)");
00229 propertyVariable = CycObjectFactory.makeCycVariable("?PROPERTY");
00230
00231 response = CycAccess.current().askWithVariable(query,
00232 propertyVariable, mt);
00233
00234 iterator=response.iterator();
00235 while (iterator.hasNext())
00236 {
00237 item=iterator.next();
00238
00239 collection.add(item.toString());
00240 }
00241
00242 return (String[])collection.toArray(new String[collection.size()]);
00243 }
00244 public CivilServantData[] getCivilServantsWithSubordinates(CycConstant mt) throws CycApiException, IOException
00245 {
00246 CycList query = null;
00247 CycList response = null;
00248 CycVariable bossVariable = null;
00249 CycVariable subordinateVariable=null;
00250 CycList item=null;
00251 CivilServantData civilServantData=null;
00252 ArrayList variables=null;
00253 Collection collection=null;
00254 Iterator iterator=null;
00255
00256 collection=new Vector();
00257
00258 query = CycAccess.current().makeCycList("(#$superiors ?BOSS ?SUBORD)");
00259 bossVariable = CycObjectFactory.makeCycVariable("?BOSS");
00260 subordinateVariable = CycObjectFactory.makeCycVariable("?SUBORD");
00261
00262 variables=new ArrayList();
00263 variables.add(bossVariable);
00264 variables.add(subordinateVariable);
00265
00266 response = CycAccess.current().askWithVariables(query,
00267 variables, mt);
00268
00269 iterator=response.iterator();
00270 while (iterator.hasNext())
00271 {
00272 item=(CycList)iterator.next();
00273
00274 civilServantData=new CivilServantData();
00275 civilServantData.setFieldOfWork(this.getPredicateValue("fieldOfWork", (CycConstant)item.first(), mt));
00276 civilServantData.setGivenNames(this.getPredicateValue("givenNames", (CycConstant)item.first(), mt));
00277 civilServantData.setFamilyName(this.getPredicateValue("familyName", (CycConstant)item.first(), mt));
00278
00279 collection.add(civilServantData);
00280 }
00281
00282 return (CivilServantData[])collection.toArray(new CivilServantData[collection.size()]);
00283
00284 }
00285 public CivilServantData[] getBossOfBagaturLobo(CycConstant mt) throws CycApiException, IOException
00286 {
00287 CycList query = null;
00288 CycList response = null;
00289 CycVariable bossVariable = null;
00290 CycConstant item=null;
00291 CivilServantData civilServantData=null;
00292 ArrayList variables=null;
00293 Collection collection=null;
00294 Iterator iterator=null;
00295
00296 collection=new Vector();
00297
00298 query = CycAccess.current().makeCycList("(#$superiors ?BOSS #$BagaturLobo)");
00299 bossVariable = CycObjectFactory.makeCycVariable("?BOSS");
00300
00301 response = CycAccess.current().askWithVariable(query,
00302 bossVariable, mt);
00303
00304 iterator=response.iterator();
00305 while (iterator.hasNext())
00306 {
00307 item=(CycConstant)iterator.next();
00308
00309 civilServantData=new CivilServantData();
00310 civilServantData.setFieldOfWork(this.getPredicateValue("fieldOfWork", item, mt));
00311 civilServantData.setGivenNames(this.getPredicateValue("givenNames", item, mt));
00312 civilServantData.setFamilyName(this.getPredicateValue("familyName", item, mt));
00313
00314 collection.add(civilServantData);
00315 }
00316
00317 return (CivilServantData[])collection.toArray(new CivilServantData[collection.size()]);
00318
00319 }
00320 }