1 19 20 package com.hp.hpl.jena.ontology.impl; 23 24 25 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 28 import com.hp.hpl.jena.enhanced.*; 29 import com.hp.hpl.jena.graph.*; 30 import com.hp.hpl.jena.shared.*; 31 import com.hp.hpl.jena.ontology.*; 32 import com.hp.hpl.jena.rdf.model.*; 33 import com.hp.hpl.jena.rdf.model.impl.*; 34 import com.hp.hpl.jena.reasoner.*; 35 import com.hp.hpl.jena.reasoner.rulesys.BasicForwardRuleInfGraph; 36 import com.hp.hpl.jena.util.ResourceUtils; 37 import com.hp.hpl.jena.util.iterator.*; 38 import com.hp.hpl.jena.vocabulary.*; 39 40 import java.util.*; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 46 56 public class OntResourceImpl 57 extends ResourceImpl 58 implements OntResource 59 { 60 63 64 public static final String [] KNOWN_LANGUAGES = new String [] {OWL.NS, 65 RDF.getURI(), 66 RDFS.getURI(), 67 DAMLVocabulary.NAMESPACE_DAML_2001_03_URI, 68 XSDDatatype.XSD}; 69 70 73 78 public static Implementation factory = new Implementation() { 79 public EnhNode wrap( Node n, EnhGraph eg ) { 80 if (canWrap( n, eg )) { 81 return new OntResourceImpl( n, eg ); 82 } 83 else { 84 throw new ConversionException( "Cannot convert node " + n.toString() + " to OntResource"); 85 } 86 } 87 88 public boolean canWrap( Node node, EnhGraph eg ) { 89 return node.isURI() || node.isBlank(); 91 } 92 }; 93 94 private static final Log log = LogFactory.getLog( OntResourceImpl.class ); 95 96 99 102 110 public OntResourceImpl( Node n, EnhGraph g ) { 111 super( n, g ); 112 } 113 114 115 118 126 public Profile getProfile() { 127 return ((OntModel) getModel()).getProfile(); 128 } 129 130 137 public boolean isOntLanguageTerm() { 138 if (!isAnon()) { 139 for (int i = 0; i < KNOWN_LANGUAGES.length; i++) { 140 if (getURI().startsWith( KNOWN_LANGUAGES[i] )) { 141 return true; 142 } 143 } 144 } 145 return false; 146 } 147 148 149 151 157 public void setSameAs( Resource res ) { 158 setPropertyValue( getProfile().SAME_AS(), "SAME_AS", res ); 159 } 160 161 166 public void addSameAs( Resource res ) { 167 addPropertyValue( getProfile().SAME_AS(), "SAME_AS", res ); 168 } 169 170 176 public OntResource getSameAs() { 177 return objectAsResource( getProfile().SAME_AS(), "SAME_AS" ); 178 } 179 180 186 public ExtendedIterator listSameAs() { 187 return listAs( getProfile().SAME_AS(), "SAME_AS", OntResource.class ); 188 } 189 190 195 public boolean isSameAs( Resource res ) { 196 return hasPropertyValue( getProfile().SAME_AS(), "SAME_AS", res ); 197 } 198 199 204 public void removeSameAs( Resource res ) { 205 removePropertyValue( getProfile().SAME_AS(), "SAME_AS", res ); 206 } 207 208 210 216 public void setDifferentFrom( Resource res ) { 217 setPropertyValue( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM", res ); 218 } 219 220 225 public void addDifferentFrom( Resource res ) { 226 addPropertyValue( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM", res ); 227 } 228 229 235 public OntResource getDifferentFrom() { 236 return objectAsResource( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM" ); 237 } 238 239 245 public ExtendedIterator listDifferentFrom() { 246 return listAs( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM", OntResource.class ); 247 } 248 249 254 public boolean isDifferentFrom( Resource res ) { 255 return hasPropertyValue( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM", res ); 256 } 257 258 263 public void removeDifferentFrom( Resource res ) { 264 removePropertyValue( getProfile().DIFFERENT_FROM(), "DIFFERENT_FROM", res ); 265 } 266 267 269 274 public void setSeeAlso( Resource res ) { 275 setPropertyValue( getProfile().SEE_ALSO(), "SEE_ALSO", res ); 276 } 277 278 283 public void addSeeAlso( Resource res ) { 284 addPropertyValue( getProfile().SEE_ALSO(), "SEE_ALSO", res ); 285 } 286 287 293 public Resource getSeeAlso() { 294 return objectAsResource( getProfile().SEE_ALSO(), "SEE_ALSO" ); 295 } 296 297 303 public ExtendedIterator listSeeAlso() { 304 checkProfile( getProfile().SEE_ALSO(), "SEE_ALSO" ); 305 return WrappedIterator.create( listProperties( getProfile().SEE_ALSO() ) ) 306 .mapWith( new ObjectMapper() ); 307 } 308 309 314 public boolean hasSeeAlso( Resource res ) { 315 return hasPropertyValue( getProfile().SEE_ALSO(), "SEE_ALSO", res ); 316 } 317 318 324 public void removeSeeAlso( Resource res ) { 325 removePropertyValue( getProfile().SEE_ALSO(), "SEE_ALSO", res ); 326 } 327 328 330 336 public void setIsDefinedBy( Resource res ) { 337 setPropertyValue( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY", res ); 338 } 339 340 345 public void addIsDefinedBy( Resource res ) { 346 addPropertyValue( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY", res ); 347 } 348 349 355 public Resource getIsDefinedBy() { 356 return objectAsResource( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY" ); 357 } 358 359 365 public ExtendedIterator listIsDefinedBy() { 366 checkProfile( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY" ); 367 return WrappedIterator.create( listProperties( getProfile().IS_DEFINED_BY() ) ) 368 .mapWith( new ObjectMapper() ); 369 } 370 371 376 public boolean isDefinedBy( Resource res ) { 377 return hasPropertyValue( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY", res ); 378 } 379 380 385 public void removeDefinedBy( Resource res ) { 386 removePropertyValue( getProfile().IS_DEFINED_BY(), "IS_DEFINED_BY", res ); 387 } 388 389 390 392 398 public void setVersionInfo( String info ) { 399 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 400 removeAll( getProfile().VERSION_INFO() ); 401 addVersionInfo( info ); 402 } 403 404 409 public void addVersionInfo( String info ) { 410 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 411 addProperty( getProfile().VERSION_INFO(), getModel().createLiteral( info ) ); 412 } 413 414 420 public String getVersionInfo() { 421 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 422 try { 423 return getRequiredProperty( getProfile().VERSION_INFO() ).getString(); 424 } 425 catch (PropertyNotFoundException ignore) { 426 return null; 427 } 428 } 429 430 435 public ExtendedIterator listVersionInfo() { 436 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 437 return WrappedIterator.create( listProperties( getProfile().VERSION_INFO() ) ) 438 .mapWith( new ObjectAsStringMapper() ); 439 } 440 441 446 public boolean hasVersionInfo( String info ) { 447 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 448 return hasProperty( getProfile().VERSION_INFO(), info ); 449 } 450 451 457 public void removeVersionInfo( String info ) { 458 checkProfile( getProfile().VERSION_INFO(), "VERSION_INFO" ); 459 460 StmtIterator i = getModel().listStatements( this, getProfile().VERSION_INFO(), info ); 461 if (i.hasNext()) { 462 i.nextStatement().remove(); 463 } 464 465 i.close(); 466 } 467 468 470 477 public void setLabel( String label, String lang ) { 478 checkProfile( getProfile().LABEL(), "LABEL" ); 479 removeAll( getProfile().LABEL() ); 480 addLabel( label, lang ); 481 } 482 483 489 public void addLabel( String label, String lang ) { 490 addLabel( getModel().createLiteral( label, lang ) ); 491 } 492 493 498 public void addLabel( Literal label ) { 499 addPropertyValue( getProfile().LABEL(), "LABEL", label ); 500 } 501 502 510 public String getLabel( String lang ) { 511 checkProfile( getProfile().LABEL(), "LABEL" ); 512 if (lang == null || lang.length() == 0) { 513 try { 515 return getRequiredProperty( getProfile().LABEL() ).getString(); 516 } 517 catch (PropertyNotFoundException ignore) { 518 return null; 519 } 520 } 521 else { 522 return selectLang( listProperties( getProfile().LABEL() ), lang ); 524 } 525 } 526 527 533 public ExtendedIterator listLabels( String lang ) { 534 checkProfile( getProfile().LABEL(), "LABEL" ); 535 return WrappedIterator.create( listProperties( getProfile().LABEL() ) ) 536 .filterKeep( new LangTagFilter( lang ) ) 537 .mapWith( new ObjectMapper() ); 538 } 539 540 546 public boolean hasLabel( String label, String lang ) { 547 return hasLabel( getModel().createLiteral( label, lang ) ); 548 } 549 550 555 public boolean hasLabel( Literal label ) { 556 boolean found = false; 557 558 ExtendedIterator i = listLabels( label.getLanguage() ); 559 while (!found && i.hasNext()) { 560 found = label.equals( i.next() ); 561 } 562 563 i.close(); 564 return found; 565 } 566 567 574 public void removeLabel( String label, String lang ) { 575 removeLabel( getModel().createLiteral( label, lang ) ); 576 } 577 578 584 public void removeLabel( Literal label ) { 585 removePropertyValue( getProfile().LABEL(), "LABEL", label ); 586 } 587 588 590 597 public void setComment( String comment, String lang ) { 598 checkProfile( getProfile().COMMENT(), "COMMENT" ); 599 removeAll( getProfile().COMMENT() ); 600 addComment( comment, lang ); 601 } 602 603 609 public void addComment( String comment, String lang ) { 610 addComment( getModel().createLiteral( comment, lang ) ); 611 } 612 613 618 public void addComment( Literal comment ) { 619 checkProfile( getProfile().COMMENT(), "COMMENT" ); 620 addProperty( getProfile().COMMENT(), comment ); 621 } 622 623 631 public String getComment( String lang ) { 632 checkProfile( getProfile().COMMENT(), "COMMENT" ); 633 if (lang == null) { 634 try { 636 return getRequiredProperty( getProfile().COMMENT() ).getString(); 637 } 638 catch (PropertyNotFoundException ignore) { 639 return null; 641 } 642 } 643 else { 644 return selectLang( listProperties( getProfile().COMMENT() ), lang ); 646 } 647 } 648 649 654 public ExtendedIterator listComments( String lang ) { 655 checkProfile( getProfile().COMMENT(), "COMMENT" ); 656 return WrappedIterator.create( listProperties( getProfile().COMMENT() ) ) 657 .filterKeep( new LangTagFilter( lang ) ) 658 .mapWith( new ObjectMapper() ); 659 } 660 661 667 public boolean hasComment( String comment, String lang ) { 668 return hasComment( getModel().createLiteral( comment, lang ) ); 669 } 670 671 676 public boolean hasComment( Literal comment ) { 677 boolean found = false; 678 679 ExtendedIterator i = listComments( comment.getLanguage() ); 680 while (!found && i.hasNext()) { 681 found = comment.equals( i.next() ); 682 } 683 684 i.close(); 685 return found; 686 } 687 688 695 public void removeComment( String comment, String lang ) { 696 removeComment( getModel().createLiteral( comment, lang ) ); 697 } 698 699 705 public void removeComment( Literal comment ) { 706 removePropertyValue( getProfile().COMMENT(), "COMMENT", comment ); 707 } 708 709 710 712 720 public void setRDFType( Resource cls ) { 721 setPropertyValue( RDF.type, "rdf:type", cls ); 722 } 723 724 729 public void addRDFType( Resource cls ) { 730 addPropertyValue( RDF.type, "rdf:type", cls ); 731 } 732 733 744 public Resource getRDFType() { 745 return getRDFType( false ); 746 } 747 748 761 public Resource getRDFType( boolean direct ) { 762 ExtendedIterator i = null; 763 try { 764 i = listRDFTypes( direct ); 765 return i.hasNext() ? (Resource) i.next(): null; 766 } 767 finally { 768 i.close(); 769 } 770 } 771 772 782 public ExtendedIterator listRDFTypes( boolean direct ) { 783 Iterator i = listDirectPropertyValues( RDF.type, "rdf:type", null, getProfile().SUB_CLASS_OF(), direct, false ); 784 ExtendedIterator j = WrappedIterator.create( i ); 785 786 return UniqueExtendedIterator.create( j ); 788 } 789 790 798 public boolean hasRDFType( String uri ) { 799 return hasRDFType( getModel().getResource( uri ) ); 800 } 801 802 814 public boolean hasRDFType( Resource ontClass ) { 815 return hasRDFType( ontClass, "unknown", false ); 816 } 817 818 829 public boolean hasRDFType( Resource ontClass, boolean direct ) { 830 return hasRDFType( ontClass, "unknown", direct ); 831 } 832 833 protected boolean hasRDFType( Resource ontClass, String name, boolean direct ) { 834 checkProfile( ontClass, name ); 835 836 if (!direct) { 837 return hasPropertyValue( RDF.type, "rdf:type", ontClass ); 839 } 840 else { 841 ExtendedIterator i = null; 843 try { 844 i = listRDFTypes( true ); 845 while (i.hasNext()) { 846 if (ontClass.equals( i.next() )) { 847 return true; 848 } 849 } 850 851 return false; 852 } 853 finally { 854 i.close(); 855 } 856 } 857 } 858 859 864 public void removeRDFType( Resource cls ) { 865 removePropertyValue( RDF.type, "rdf:type", cls ); 866 } 867 868 870 877 public int getCardinality( Property p ) { 878 int n = 0; 879 for (Iterator i = UniqueExtendedIterator.create( listPropertyValues( p ) ); i.hasNext(); n++) { 880 i.next(); 881 } 882 883 return n; 884 } 885 886 887 900 public void setPropertyValue( Property property, RDFNode value ) { 901 removeAll( property ); 903 904 if (value != null) { 906 addProperty( property, value ); 907 } 908 } 909 910 911 923 public RDFNode getPropertyValue( Property property ) { 924 try { 925 return getRequiredProperty( property ).getObject(); 926 } 927 catch (PropertyNotFoundException ignore) { 928 return null; 929 } 930 } 931 932 933 941 public NodeIterator listPropertyValues( Property property ) { 942 return new NodeIteratorImpl( listProperties( property ).mapWith( new ObjectMapper() ), null ); 943 } 944 945 961 public void remove() { 962 Set stmts = new HashSet(); 963 List lists = new ArrayList(); 964 List skip = new ArrayList(); 965 Property first = getProfile().FIRST(); 966 967 for (StmtIterator i = listProperties(); i.hasNext(); stmts.add( i.next() ) ); 969 for (StmtIterator i = getModel().listStatements( null, null, this ); i.hasNext(); stmts.add( i.next() ) ); 970 971 for (Iterator i = stmts.iterator(); i.hasNext(); ) { 973 Statement s = (Statement) i.next(); 974 if (s.getPredicate().equals( first ) && 975 s.getObject().equals( this )) { 976 log.debug( toString() + " is referened from an RDFList, so will not be fully removed"); 979 skip.add( s ); 980 } 981 else if (s.getObject() instanceof Resource){ 982 Resource obj = s.getResource(); 984 if (obj.canAs( RDFList.class )) { 985 lists.add( obj ); 987 } 988 } 989 } 990 991 for (Iterator i = lists.iterator(); i.hasNext(); ) { 993 Resource r = (Resource) i.next(); 994 stmts.addAll( ((RDFListImpl) r.as( RDFList.class )).collectStatements() ); 995 } 996 997 stmts.removeAll( skip ); 999 1000 for (Iterator i = stmts.iterator(); i.hasNext(); ((Statement) i.next()).remove() ); 1002 } 1003 1004 1005 1011 public void removeProperty( Property property, RDFNode value ) { 1012 Set s = new HashSet(); 1014 for (StmtIterator i = getModel().listStatements( this, property, value ); i.hasNext(); s.add( i.nextStatement() ) ); 1015 for (Iterator i = s.iterator(); i.hasNext(); ((Statement) i.next()).remove() ); 1016 } 1017 1018 1019 1024 public AnnotationProperty asAnnotationProperty() { 1025 return (AnnotationProperty) as( AnnotationProperty.class ); 1026 } 1027 1028 1033 public OntProperty asProperty() { 1034 return (OntProperty) as( OntProperty.class ); 1035 } 1036 1037 1042 public ObjectProperty asObjectProperty() { 1043 return (ObjectProperty) as( ObjectProperty.class ); 1044 } 1045 1046 1051 public DatatypeProperty asDatatypeProperty() { 1052 return (DatatypeProperty) as( DatatypeProperty.class ); 1053 } 1054 1055 1060 public Individual asIndividual() { 1061 return (Individual) as( Individual.class ); 1062 } 1063 1064 1069 public OntClass asClass() { 1070 return (OntClass) as( OntClass.class ); 1071 } 1072 1073 1078 public Ontology asOntology() { 1079 return (Ontology) as( Ontology.class ); 1080 } 1081 1082 1087 public AllDifferent asAllDifferent() { 1088 return (AllDifferent) as( AllDifferent.class ); 1089 } 1090 1091 1096 public DataRange asDataRange() { 1097 return (DataRange) as( DataRange.class ); 1098 } 1099 1100 1101 1103 1107 public boolean isAnnotationProperty() { 1108 return getProfile().ANNOTATION_PROPERTY() != null && canAs( AnnotationProperty.class ); 1109 } 1110 1111 1115 public boolean isProperty() { 1116 return canAs( OntProperty.class ); 1117 } 1118 1119 1123 public boolean isObjectProperty() { 1124 return getProfile().OBJECT_PROPERTY() != null && canAs( ObjectProperty.class ); 1125 } 1126 1127 1131 public boolean isDatatypeProperty() { 1132 return getProfile().DATATYPE_PROPERTY() != null && canAs( DatatypeProperty.class ); 1133 } 1134 1135 1139 public boolean isIndividual() { 1140 OntModel m = (getModel() instanceof OntModel) ? (OntModel) getModel() : null; 1141 if (m != null) { 1142 if (!(m.getGraph() instanceof BasicForwardRuleInfGraph) || 1143 m.getProfile().THING() == null) { 1144 for (StmtIterator i = listProperties( RDF.type ); i.hasNext(); ) { 1147 Resource rType = i.nextStatement().getResource(); 1148 for (StmtIterator j = rType.listProperties( RDF.type ); j.hasNext(); ) { 1149 if (j.nextStatement().getResource().equals( getProfile().CLASS() )) { 1150 return true; 1153 } 1154 } 1155 } 1156 1157 return false; 1159 } 1160 else { 1161 return hasProperty( RDF.type, getProfile().THING() ); 1164 } 1165 } 1166 1167 return canAs( Individual.class ); 1169 } 1170 1171 1175 public boolean isClass() { 1176 return canAs( OntClass.class ); 1177 } 1178 1179 1183 public boolean isOntology() { 1184 return getProfile().ONTOLOGY() != null && canAs( Ontology.class ); 1185 } 1186 1187 1191 public boolean isDataRange() { 1192 return getProfile().DATARANGE() != null && canAs( DataRange.class ); 1193 } 1194 1195 1199 public boolean isAllDifferent() { 1200 return getProfile().ALL_DIFFERENT() != null && canAs( AllDifferent.class ); 1201 } 1202 1203 1204 1205 1208 1209 1210 protected static boolean hasType( Node n, EnhGraph g, Resource type ) { 1211 boolean hasType = false; 1212 ClosableIterator i = g.asGraph().find( n, RDF.type.asNode(), type.asNode() ); 1213 hasType = i.hasNext(); 1214 i.close(); 1215 return hasType; 1216 } 1217 1218 1224 protected void checkProfile( Object term, String name ) { 1225 if (term == null) { 1226 throw new ProfileException( name, getProfile() ); 1227 } 1228 } 1229 1230 1231 1237 protected String selectLang( StmtIterator stmts, String lang ) { 1238 String found = null; 1239 1240 while (stmts.hasNext()) { 1241 RDFNode n = stmts.nextStatement().getObject(); 1242 1243 if (n instanceof Literal) { 1244 Literal l = (Literal) n; 1245 String lLang = l.getLanguage(); 1246 1247 if (lang.equalsIgnoreCase( lLang )) { 1249 found = l.getString(); 1251 break; 1252 } 1253 else if (lLang != null && lLang.length() > 1 && lang.equalsIgnoreCase( lLang.substring( 0, 2 ) )) { 1254 found = l.getString(); 1257 } 1258 else if (found == null && lLang == null) { 1259 found = l.getString(); 1261 } 1262 } 1263 } 1264 1265 stmts.close(); 1266 return found; 1267 } 1268 1269 1270 protected boolean langTagMatch( String desired, String target ) { 1271 return (desired == null) || 1272 (desired.equalsIgnoreCase( target )) || 1273 (target.length() > desired.length() && desired.equalsIgnoreCase( target.substring( desired.length() ) )); 1274 } 1275 1276 1277 protected Object objectAs( Property p, String name, Class asClass ) { 1278 checkProfile( p, name ); 1279 try { 1280 return getRequiredProperty( p ).getObject().as( asClass ); 1281 } 1282 catch (PropertyNotFoundException e) { 1283 return null; 1284 } 1285 } 1286 1287 1288 1289 protected OntResource objectAsResource( Property p, String name ) { 1290 return (OntResource) objectAs( p, name, OntResource.class ); 1291 } 1292 1293 1294 1295 protected OntProperty objectAsProperty( Property p, String name ) { 1296 return (OntProperty) objectAs( p, name, OntProperty.class ); 1297 } 1298 1299 1300 1301 protected int objectAsInt( Property p, String name ) { 1302 checkProfile( p, name ); 1303 return getRequiredProperty( p ).getInt(); 1304 } 1305 1306 1307 1308 protected ExtendedIterator listAs( Property p, String name, Class cls ) { 1309 checkProfile( p, name ); 1310 return WrappedIterator.create( listProperties( p ) ).mapWith( new ObjectAsMapper( cls ) ); 1311 } 1312 1313 1314 1315 protected void addPropertyValue( Property p, String name, RDFNode value ) { 1316 checkProfile( p, name ); 1317 addProperty( p, value ); 1318 } 1319 1320 1321 protected void setPropertyValue( Property p, String name, RDFNode value ) { 1322 checkProfile( p, name ); 1323 removeAll( p ); 1324 addProperty( p, value ); 1325 } 1326 1327 1328 protected boolean hasPropertyValue( Property p, String name, RDFNode value ) { 1329 checkProfile( p, name ); 1330 return hasProperty( p, value ); 1331 } 1332 1333 1334 protected void addListPropertyValue( Property p, String name, RDFNode value ) { 1335 checkProfile( p, name ); 1336 1337 if (hasProperty( p )) { 1339 RDFNode cur = getRequiredProperty( p ).getObject(); 1340 if (!cur.canAs( RDFList.class )) { 1341 throw new OntologyException( "Tried to add a value to a list-valued property " + p + 1342 " but the current value is not a list: " + cur ); 1343 } 1344 1345 RDFList values = (RDFList) cur.as( RDFList.class ); 1346 1347 if (!values.contains( value )){ 1349 RDFList newValues = values.with( value ); 1350 1351 if (newValues != values) { 1353 removeAll( p ); 1354 addProperty( p, newValues ); 1355 } 1356 } 1357 } 1358 else { 1359 addProperty( p, ((OntModel) getModel()).createList( new RDFNode[] {value} ) ); 1361 } 1362 } 1363 1364 1365 protected RDFNode convertToType( Resource type, String name, Class cls ) { 1366 checkProfile( type, name ); 1367 if (canAs( cls )) { 1368 return as( cls ); 1370 } 1371 1372 addProperty( RDF.type, type ); 1374 return as( cls ); 1375 } 1376 1377 1389 protected ExtendedIterator listDirectPropertyValues( Property p, String name, Class cls, Property orderRel, boolean direct, boolean inverse ) { 1390 Iterator i = null; 1391 checkProfile( p, name ); 1392 1393 Property sc = p; 1394 1395 if (direct) { 1397 sc = getModel().getProperty( ReasonerRegistry.makeDirect( sc.getNode() ).getURI() ); 1398 } 1399 1400 Resource subject = inverse ? null : this; 1402 Resource object = inverse ? this : null; 1403 Map1 mapper = inverse ? (Map1) new SubjectAsMapper( cls ) : (Map1) new ObjectAsMapper( cls ); 1404 1405 OntModel m = (OntModel) getGraph(); 1407 InfGraph ig = null; 1408 if (m.getGraph() instanceof InfGraph) { 1409 ig = (InfGraph) m.getGraph(); 1410 } 1411 1412 if (!direct || ((ig != null) && ig.getReasoner().supportsProperty( sc ))) { 1414 i = getModel().listStatements( subject, sc, object ).mapWith( mapper ); 1418 } 1419 else { 1420 i = computeDirectValues( p, orderRel, inverse, subject, object, mapper ); 1421 } 1422 1423 return UniqueExtendedIterator.create( i ); 1424 } 1425 1426 1427 1438 private Iterator computeDirectValues( Property p, Property orderRel, boolean inverse, Resource subject, Resource object, Map1 mapper ) { 1439 ExtendedIterator j = getModel().listStatements( subject, p, object ) 1441 .mapWith( mapper ); 1442 1443 List s = new ArrayList(); 1445 for( ; j.hasNext(); s.add( j.next() ) ); 1446 1447 ResourceUtils.removeEquiv( s, orderRel, this ); 1451 boolean withheld = s.remove( this ); 1452 1453 1455 List partition = ResourceUtils.partition( s, orderRel ); 1457 Map equivSets = new HashMap(); 1458 1459 s.clear(); 1461 for (Iterator i = partition.iterator(); i.hasNext(); ) { 1462 List part = (List) i.next(); 1463 if (part.size() == 1) { 1465 s.add( part.get(0) ); 1466 } 1467 else { 1468 Resource r = (Resource) part.remove( 0 ); 1470 equivSets.put( r, part ); 1472 s.add( r ); 1473 } 1474 } 1475 1476 1479 s = ResourceUtils.maximalLowerElements( s, orderRel, inverse ); 1481 1482 List s2 = new ArrayList(); 1484 for (Iterator i = s.iterator(); i.hasNext(); ) { 1485 Resource r = (Resource) i.next(); 1486 s2.add( r ); 1487 if (equivSets.containsKey( r )) { 1488 s2.addAll( (List) equivSets.get( r ) ); 1489 } 1490 } 1491 1492 if (withheld) { 1494 s2.add( this ); 1495 } 1496 1497 return s2.iterator(); 1498 } 1499 1500 1501 1502 protected void removePropertyValue( Property prop, String name, RDFNode value ) { 1503 checkProfile( prop, name ); 1504 1505 StmtIterator i = getModel().listStatements( this, prop, value ); 1506 if (i.hasNext()) { 1507 i.nextStatement().remove(); 1508 } 1509 1510 i.close(); 1511 } 1512 1513 1517 1518 protected static class AsMapper 1519 implements Map1 1520 { 1521 private Class m_as; 1522 public AsMapper( Class as ) { m_as = as; } 1523 public Object map1( Object x ) { return (x instanceof Resource) ? ((Resource) x).as( m_as ) : x; } 1524 } 1525 1526 1527 protected static class SubjectAsMapper 1528 implements Map1 1529 { 1530 private Class m_as; 1531 public SubjectAsMapper( Class as ) { m_as = as; } 1532 public Object map1( Object x ) { 1533 if (x instanceof Statement) { 1534 RDFNode subj = ((Statement) x).getSubject(); 1535 return (m_as == null) ? subj : subj.as( m_as ); 1536 } 1537 else { 1538 return x; 1539 } 1540 } 1541 } 1542 1543 1544 protected static class SubjectMapper 1545 implements Map1 1546 { 1547 public Object map1( Object x ) { 1548 return (x instanceof Statement) ? ((Statement) x).getSubject() : x; 1549 } 1550 } 1551 1552 1553 protected static class ObjectAsMapper 1554 implements Map1 1555 { 1556 private Class m_as; 1557 public ObjectAsMapper( Class as ) { m_as = as; } 1558 public Object map1( Object x ) { 1559 if (x instanceof Statement) { 1560 RDFNode obj = ((Statement) x).getObject(); 1561 return (m_as == null) ? obj : obj.as( m_as ); 1562 } 1563 else { 1564 return x; 1565 } 1566 } 1567 } 1568 1569 1570 protected class ObjectAsStringMapper 1571 implements Map1 1572 { 1573 public Object map1( Object x ) { return (x instanceof Statement) ? ((Statement) x).getString() : x; } 1574 } 1575 1576 1577 protected static class ObjectMapper 1578 implements Map1 1579 { 1580 public ObjectMapper() {} 1581 public Object map1( Object x ) { return (x instanceof Statement) ? ((Statement) x).getObject() : x; } 1582 } 1583 1584 1585 protected class LangTagFilter 1586 implements Filter 1587 { 1588 protected String m_lang; 1589 public LangTagFilter( String lang ) { m_lang = lang; } 1590 public boolean accept( Object x ) { 1591 if (x instanceof Literal) { 1592 return langTagMatch( m_lang, ((Literal) x).getLanguage() ); 1593 } 1594 else if (x instanceof Statement) { 1595 return accept( ((Statement) x).getObject() ); 1597 } 1598 else { 1599 return false; 1600 } 1601 } 1602 } 1603 1604 1605 protected class SingleEqualityFilter 1606 implements Filter 1607 { 1608 private Object m_obj; 1609 public SingleEqualityFilter( Object x ) { m_obj = x; } 1610 public boolean accept( Object x ) {return m_obj.equals( x );} 1611 } 1612} 1613 1614 1615 1644 | Popular Tags |