/* * Converts the ProductFeature Object into an TriG String * representation. */ private String convertProductFeature(ProductFeature pf) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(ProductFeature.getPrefixed(pf.getNr())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("ProductFeature"))); // rdfs:label result.append(createTriplePO(RDFS.prefixed("label"), createLiteral(pf.getLabel()))); // rdfs:comment result.append(createTriplePO(RDFS.prefixed("comment"), createLiteral(pf.getComment()))); // dc:publisher result.append( createTriplePO( DC.prefixed("publisher"), createURIref(BSBM.getStandardizationInstitution(pf.getPublisher())))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(pf.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
static NodeMaker cast(NodeMaker nodeMaker, RDFDatatype datatype) { if (nodeMaker instanceof TypedNodeMaker) return new TypedNodeMaker( TypedNodeMaker.typedLiteral(datatype), ((TypedNodeMaker) nodeMaker).valueMaker(), nodeMaker.isUnique()); if (nodeMaker instanceof FixedNodeMaker) { Node node = nodeMaker.makeNode(null); return new FixedNodeMaker(XSD.cast(node, datatype), nodeMaker.isUnique()); } throw new RuntimeException("unknown nodeMaker type"); }
/* * Converts the Vendor Object into an TriG String * representation. */ private String convertVendor(Vendor vendor) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Vendor.getPrefixed(vendor.getNr())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Vendor"))); // rdfs:label result.append(createTriplePO(RDFS.prefixed("label"), createLiteral(vendor.getLabel()))); // rdfs:comment result.append(createTriplePO(RDFS.prefixed("comment"), createLiteral(vendor.getComment()))); // foaf:homepage result.append(createTriplePO(FOAF.prefixed("homepage"), createURIref(vendor.getHomepage()))); // bsbm:country result.append( createTriplePO( BSBM.prefixed("country"), createURIref(ISO3166.find(vendor.getCountryCode())))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), Vendor.getPrefixed(vendor.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(vendor.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
/* * Converts the Person Object into an TriG String * representation. */ private String convertPerson(Person person, ObjectBundle bundle) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Person.getPrefixed(person.getNr(), bundle.getPublisherNum())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), FOAF.prefixed("Person"))); // foaf:name result.append(createTriplePO(FOAF.prefixed("name"), createLiteral(person.getName()))); // foaf:mbox_sha1sum result.append( createTriplePO(FOAF.prefixed("mbox_sha1sum"), createLiteral(person.getMbox_sha1sum()))); // bsbm:country result.append( createTriplePO( BSBM.prefixed("country"), createURIref(ISO3166.find(person.getCountryCode())))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), RatingSite.getPrefixed(person.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(person.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
private void convertNotEquals(E_NotEquals expr) { logger.debug("convertNotEquals " + expr.toString()); expr.getArg1().visit(this); expr.getArg2().visit(this); Expression e2 = expression.pop(); Expression e1 = expression.pop(); // TODO Expression.FALSE and Expression.TRUE are not constants if (e1.equals(Expression.FALSE)) e1 = CONSTANT_FALSE; else if (e1.equals(Expression.TRUE)) e1 = CONSTANT_TRUE; if (e2.equals(Expression.FALSE)) e2 = CONSTANT_FALSE; else if (e2.equals(Expression.TRUE)) e2 = CONSTANT_TRUE; if (e1 instanceof AttributeExprEx && e2 instanceof Constant || e2 instanceof AttributeExprEx && e1 instanceof Constant) { AttributeExprEx variable; ConstantEx constant; if (e1 instanceof AttributeExprEx) { variable = (AttributeExprEx) e1; constant = (ConstantEx) e2; } else { variable = (AttributeExprEx) e2; constant = (ConstantEx) e1; } logger.debug("isNotEqual(" + variable + ", " + constant + ")"); NodeMaker nm = variable.getNodeMaker(); if (nm instanceof TypedNodeMaker) { ValueMaker vm = ((TypedNodeMaker) nm).valueMaker(); Node node = constant.getNode(); logger.debug("checking " + node + " with " + nm); if (XSD.isNumeric(node)) { DetermineNodeType filter = new DetermineNodeType(); nm.describeSelf(filter); RDFDatatype datatype = filter.getDatatype(); if (datatype != null && XSD.isNumeric(datatype)) { RDFDatatype numericType = XSD.getNumericType(datatype, node.getLiteralDatatype()); nm = cast(nm, numericType); node = XSD.cast(node, numericType); } } boolean empty = nm.selectNode(node, RelationalOperators.DUMMY).equals(NodeMaker.EMPTY); logger.debug("result " + new Boolean(empty)); if (!empty) { if (node.isURI()) expression.push(new Negation(vm.valueExpression(node.getURI()))); else if (node.isLiteral()) { if (XSD.isSupported(node.getLiteralDatatype())) expression.push(new Negation(vm.valueExpression(constant.value()))); else // type = boolean or an unknown type conversionFailed("cannot compare values of type " + node.getLiteralDatatypeURI(), expr); } else conversionFailed(expr); // TODO blank nodes? return; } else { expression.push(Expression.TRUE); return; } } } else if (e1 instanceof ConstantEx && e2 instanceof ConstantEx) { logger.debug("isNotEqual(" + e1 + ", " + e2 + ")"); Node c1 = ((ConstantEx) e1).getNode(); Node c2 = ((ConstantEx) e2).getNode(); boolean equals; if (XSD.isNumeric(c1) && XSD.isNumeric(c2)) { RDFDatatype datatype = XSD.getNumericType(c1.getLiteralDatatype(), c2.getLiteralDatatype()); equals = XSD.cast(c1, datatype).equals(XSD.cast(c2, datatype)); } else if (isSimpleLiteral(c1) && isSimpleLiteral(c2)) { equals = c1.getLiteralValue().equals(c2.getLiteralValue()); } else if (XSD.isString(c1) && XSD.isString(c2)) { equals = c1.getLiteralValue().equals(c2.getLiteralValue()); } else { try { equals = NodeFunctions.rdfTermEquals(c1, c2); } catch (ExprEvalException e) { equals = false; } } logger.debug("constants equal? " + new Boolean(equals)); expression.push(equals ? Expression.FALSE : Expression.TRUE); return; } else if (e1 instanceof AttributeExprEx && e2 instanceof AttributeExprEx) { logger.debug("isNotEqual(" + e1 + ", " + e2 + ")"); AttributeExprEx variable1 = (AttributeExprEx) e1; AttributeExprEx variable2 = (AttributeExprEx) e2; NodeMaker nm1 = variable1.getNodeMaker(); NodeMaker nm2 = variable2.getNodeMaker(); DetermineNodeType filter1 = new DetermineNodeType(); nm1.describeSelf(filter1); RDFDatatype datatype1 = filter1.getDatatype(); DetermineNodeType filter2 = new DetermineNodeType(); nm2.describeSelf(filter2); RDFDatatype datatype2 = filter2.getDatatype(); if (datatype1 != null && XSD.isNumeric(datatype1) && datatype2 != null && XSD.isNumeric(datatype2)) { RDFDatatype numericType = XSD.getNumericType(filter1.getDatatype(), filter2.getDatatype()); nm1 = cast(nm1, numericType); nm2 = cast(nm2, numericType); } NodeSetConstraintBuilder nodeSet = new NodeSetConstraintBuilder(); nm1.describeSelf(nodeSet); nm2.describeSelf(nodeSet); if (nodeSet.isEmpty()) { logger.debug("nodes " + nm1 + " " + nm2 + " incompatible"); expression.push(Expression.TRUE); return; } } expression.push(new Negation(Equality.create(e1, e2))); }
/* * Converts the Review Object into an TriG String * representation. */ private String convertReview(Review review, ObjectBundle bundle) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Review.getPrefixed(review.getNr(), bundle.getPublisherNum())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Review"))); // bsbm:reviewFor result.append( createTriplePO( BSBM.prefixed("reviewFor"), Product.getPrefixed(review.getProduct(), review.getProducerOfProduct()))); // rev:reviewer result.append( createTriplePO( REV.prefixed("reviewer"), Person.getPrefixed(review.getPerson(), review.getPublisher()))); // dc:title result.append(createTriplePO(DC.prefixed("title"), createLiteral(review.getTitle()))); // rev:text result.append( createTriplePO( REV.prefixed("text"), createLanguageLiteral(review.getText(), ISO3166.language[review.getLanguage()]))); // bsbm:ratingX Integer[] ratings = review.getRatings(); for (int i = 0, j = 1; i < ratings.length; i++, j++) { Integer value = ratings[i]; if (value != null) result.append( createTriplePO( BSBM.getRatingPrefix(j), // createDataTypeLiteral(value.toString(), // XSD.prefixed("integer")) value.toString())); } // bsbm:reviewDate GregorianCalendar reviewDate = new GregorianCalendar(); reviewDate.setTimeInMillis(review.getReviewDate()); String reviewDateString = DateGenerator.formatDateTime(reviewDate); result.append( createTriplePO( BSBM.prefixed("reviewDate"), createDataTypeLiteral(reviewDateString, XSD.prefixed("dateTime")))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), RatingSite.getPrefixed(review.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(review.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
/* * Converts the Product Object into an TriG String * representation. */ private String convertProduct(Product product) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(Product.getPrefixed(product.getNr(), product.getProducer())); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Product"))); // rdfs:label result.append(createTriplePO(RDFS.prefixed("label"), createLiteral(product.getLabel()))); // rdfs:comment result.append(createTriplePO(RDFS.prefixed("comment"), createLiteral(product.getComment()))); // bsbm:productType if (forwardChaining) { ProductType pt = product.getProductType(); while (pt != null) { result.append(createTriplePO(RDF.prefixed("type"), pt.getPrefixed())); pt = pt.getParent(); } } else { result.append(createTriplePO(RDF.prefixed("type"), product.getProductType().getPrefixed())); } // bsbm:productPropertyNumeric Integer[] ppn = product.getProductPropertyNumeric(); for (int i = 0, j = 1; i < ppn.length; i++, j++) { Integer value = ppn[i]; if (value != null) result.append( createTriplePO( BSBM.getProductPropertyNumericPrefix(j), // createDataTypeLiteral(value.toString(), // XSD.prefixed("integer")) value.toString())); } // bsbm:productPropertyTextual String[] ppt = product.getProductPropertyTextual(); for (int i = 0, j = 1; i < ppt.length; i++, j++) { String value = ppt[i]; if (value != null) result.append( createTriplePO( BSBM.getProductPropertyTextualPrefix(j), createDataTypeLiteral(value, XSD.prefixed("string")))); } // bsbm:productFeature Iterator<Integer> pf = product.getFeatures().iterator(); while (pf.hasNext()) { Integer value = pf.next(); result.append( createTriplePO(BSBM.prefixed("productFeature"), ProductFeature.getPrefixed(value))); } // bsbm:producer result.append( createTriplePO(BSBM.prefixed("producer"), Producer.getPrefixed(product.getProducer()))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), Producer.getPrefixed(product.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(product.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }
/* * Converts the Offer Object into an TriG String * representation. */ private String convertOffer(Offer offer) { StringBuffer result = new StringBuffer(); // First the uriref for the subject result.append(offer.getPrefixed()); result.append("\n"); // rdf:type result.append(createTriplePO(RDF.prefixed("type"), BSBM.prefixed("Offer"))); // bsbm:product int productNr = offer.getProduct(); int producerNr = Generator.getProducerOfProduct(productNr); result.append( createTriplePO(BSBM.prefixed("product"), Product.getPrefixed(productNr, producerNr))); // bsbm:vendor result.append(createTriplePO(BSBM.prefixed("vendor"), Vendor.getPrefixed(offer.getVendor()))); // bsbm:price result.append( createTriplePO( BSBM.prefixed("price"), // createDataTypeLiteral(offer.getPriceString(),BSBM.prefixed("USD")) offer.getPriceString() + "e0")); // bsbm:validFrom GregorianCalendar validFrom = new GregorianCalendar(); validFrom.setTimeInMillis(offer.getValidFrom()); String validFromString = DateGenerator.formatDateTime(validFrom); result.append( createTriplePO( BSBM.prefixed("validFrom"), createDataTypeLiteral(validFromString, XSD.prefixed("dateTime")))); // bsbm:validTo GregorianCalendar validTo = new GregorianCalendar(); validTo.setTimeInMillis(offer.getValidTo()); String validToString = DateGenerator.formatDateTime(validTo); result.append( createTriplePO( BSBM.prefixed("validTo"), createDataTypeLiteral(validToString, XSD.prefixed("dateTime")))); // bsbm:deliveryDays result.append( createTriplePO( BSBM.prefixed("deliveryDays"), // createDataTypeLiteral(offer.getDeliveryDays().toString(), // XSD.prefixed("integer")) offer.getDeliveryDays().toString())); // bsbm:offerWebpage result.append( createTriplePO(BSBM.prefixed("offerWebpage"), createURIref(offer.getOfferWebpage()))); // dc:publisher result.append( createTriplePO(DC.prefixed("publisher"), Vendor.getPrefixed(offer.getPublisher()))); // dc:date GregorianCalendar date = new GregorianCalendar(); date.setTimeInMillis(offer.getPublishDate()); String dateString = DateGenerator.formatDate(date); result.append( createTriplePOEnd( DC.prefixed("date"), createDataTypeLiteral(dateString, XSD.prefixed("date")))); return result.toString(); }